OSDN Git Service

2001-04-27 Bryce McKinlay <bryce@waitaki.otago.ac.nz>
[pf3gnuchains/gcc-fork.git] / gcc / java / parse.y
1 /* Source code parsing and tree node generation for the GNU compiler
2    for the Java(TM) language.
3    Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4    Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
22
23 Java and all Java-based marks are trademarks or registered trademarks
24 of Sun Microsystems, Inc. in the United States and other countries.
25 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
26
27 /* This file parses java source code and issues a tree node image
28 suitable for code generation (byte code and targeted CPU assembly
29 language).
30
31 The grammar conforms to the Java grammar described in "The Java(TM)
32 Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
33 1996, ISBN 0-201-63451-1"
34
35 The following modifications were brought to the original grammar:
36
37 method_body: added the rule '| block SC_TK'
38 static_initializer: added the rule 'static block SC_TK'. 
39
40 Note: All the extra rules described above should go away when the
41       empty_statement rule will work.
42
43 statement_nsi: 'nsi' should be read no_short_if.
44
45 Some rules have been modified to support JDK1.1 inner classes
46 definitions and other extensions.  */
47
48 %{
49 #include "config.h"
50 #include "system.h"
51 #include <dirent.h>
52 #include "tree.h"
53 #include "rtl.h"
54 #include "obstack.h"
55 #include "toplev.h"
56 #include "flags.h"
57 #include "java-tree.h"
58 #include "jcf.h"
59 #include "lex.h"
60 #include "parse.h"
61 #include "zipfile.h"
62 #include "convert.h"
63 #include "buffer.h"
64 #include "xref.h"
65 #include "function.h"
66 #include "except.h"
67 #include "ggc.h"
68
69 #ifndef DIR_SEPARATOR
70 #define DIR_SEPARATOR '/'
71 #endif
72
73 /* Local function prototypes */
74 static char *java_accstring_lookup PARAMS ((int));
75 static void  classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
76 static void  variable_redefinition_error PARAMS ((tree, tree, tree, int));
77 static tree  create_class PARAMS ((int, tree, tree, tree));
78 static tree  create_interface PARAMS ((int, tree, tree));
79 static void  end_class_declaration PARAMS ((int));
80 static tree  find_field PARAMS ((tree, tree));
81 static tree lookup_field_wrapper PARAMS ((tree, tree));
82 static int   duplicate_declaration_error_p PARAMS ((tree, tree, tree));
83 static void  register_fields PARAMS ((int, tree, tree));
84 static tree parser_qualified_classname PARAMS ((tree));
85 static int  parser_check_super PARAMS ((tree, tree, tree));
86 static int  parser_check_super_interface PARAMS ((tree, tree, tree));
87 static void check_modifiers_consistency PARAMS ((int));
88 static tree lookup_cl PARAMS ((tree));
89 static tree lookup_java_method2 PARAMS ((tree, tree, int));
90 static tree method_header PARAMS ((int, tree, tree, tree));
91 static void fix_method_argument_names PARAMS ((tree ,tree));
92 static tree method_declarator PARAMS ((tree, tree));
93 static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
94   ATTRIBUTE_PRINTF_2;
95 static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list))
96   ATTRIBUTE_PRINTF (2, 0);
97 static void parse_ctor_invocation_error PARAMS ((void));
98 static tree parse_jdk1_1_error PARAMS ((const char *));
99 static void complete_class_report_errors PARAMS ((jdep *));
100 static int process_imports PARAMS ((void));
101 static void read_import_dir PARAMS ((tree));
102 static int find_in_imports_on_demand PARAMS ((tree));
103 static void find_in_imports PARAMS ((tree));
104 static void check_static_final_variable_assignment_flag PARAMS ((tree));
105 static void reset_static_final_variable_assignment_flag PARAMS ((tree));
106 static void check_final_variable_local_assignment_flag PARAMS ((tree, tree));
107 static void reset_final_variable_local_assignment_flag PARAMS ((tree));
108 static int  check_final_variable_indirect_assignment PARAMS ((tree));
109 static void check_final_variable_global_assignment_flag PARAMS ((tree));
110 static void check_inner_class_access PARAMS ((tree, tree, tree));
111 static int check_pkg_class_access PARAMS ((tree, tree));
112 static void register_package PARAMS ((tree));
113 static tree resolve_package PARAMS ((tree, tree *));
114 static tree lookup_package_type PARAMS ((const char *, int));
115 static tree resolve_class PARAMS ((tree, tree, tree, tree));
116 static void declare_local_variables PARAMS ((int, tree, tree));
117 static void source_start_java_method PARAMS ((tree));
118 static void source_end_java_method PARAMS ((void));
119 static tree find_name_in_single_imports PARAMS ((tree));
120 static void check_abstract_method_header PARAMS ((tree));
121 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
122 static tree resolve_expression_name PARAMS ((tree, tree *));
123 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
124 static int check_class_interface_creation PARAMS ((int, int, tree, 
125                                                   tree, tree, tree));
126 static tree patch_method_invocation PARAMS ((tree, tree, tree, int,
127                                             int *, tree *));
128 static int breakdown_qualified PARAMS ((tree *, tree *, tree));
129 static int in_same_package PARAMS ((tree, tree));
130 static tree resolve_and_layout PARAMS ((tree, tree));
131 static tree qualify_and_find PARAMS ((tree, tree, tree));
132 static tree resolve_no_layout PARAMS ((tree, tree));
133 static int invocation_mode PARAMS ((tree, int));
134 static tree find_applicable_accessible_methods_list PARAMS ((int, tree, 
135                                                             tree, tree));
136 static void search_applicable_methods_list PARAMS ((int, tree, tree, tree, 
137                                                    tree *, tree *));
138 static tree find_most_specific_methods_list PARAMS ((tree));
139 static int argument_types_convertible PARAMS ((tree, tree));
140 static tree patch_invoke PARAMS ((tree, tree, tree));
141 static int maybe_use_access_method PARAMS ((int, tree *, tree *));
142 static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
143 static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
144 static tree obtain_incomplete_type PARAMS ((tree));
145 static tree java_complete_lhs PARAMS ((tree));
146 static tree java_complete_tree PARAMS ((tree));
147 static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
148 static int analyze_clinit_body PARAMS ((tree));
149 static int maybe_yank_clinit PARAMS ((tree));
150 static void java_complete_expand_method PARAMS ((tree));
151 static int  unresolved_type_p PARAMS ((tree, tree *));
152 static void create_jdep_list PARAMS ((struct parser_ctxt *));
153 static tree build_expr_block PARAMS ((tree, tree));
154 static tree enter_block PARAMS ((void));
155 static tree exit_block PARAMS ((void));
156 static tree lookup_name_in_blocks PARAMS ((tree));
157 static void maybe_absorb_scoping_blocks PARAMS ((void));
158 static tree build_method_invocation PARAMS ((tree, tree));
159 static tree build_new_invocation PARAMS ((tree, tree));
160 static tree build_assignment PARAMS ((int, int, tree, tree));
161 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
162 static int check_final_assignment PARAMS ((tree ,tree));
163 static tree patch_assignment PARAMS ((tree, tree, tree ));
164 static tree patch_binop PARAMS ((tree, tree, tree));
165 static tree build_unaryop PARAMS ((int, int, tree));
166 static tree build_incdec PARAMS ((int, int, tree, int));
167 static tree patch_unaryop PARAMS ((tree, tree));
168 static tree build_cast PARAMS ((int, tree, tree));
169 static tree build_null_of_type PARAMS ((tree));
170 static tree patch_cast PARAMS ((tree, tree));
171 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
172 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
173 static int valid_cast_to_p PARAMS ((tree, tree));
174 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
175 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
176 static tree try_reference_assignconv PARAMS ((tree, tree));
177 static tree build_unresolved_array_type PARAMS ((tree));
178 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
179 static tree build_array_ref PARAMS ((int, tree, tree));
180 static tree patch_array_ref PARAMS ((tree));
181 static tree make_qualified_name PARAMS ((tree, tree, int));
182 static tree merge_qualified_name PARAMS ((tree, tree));
183 static tree make_qualified_primary PARAMS ((tree, tree, int));
184 static int resolve_qualified_expression_name PARAMS ((tree, tree *, 
185                                                      tree *, tree *));
186 static void qualify_ambiguous_name PARAMS ((tree));
187 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
188 static tree build_newarray_node PARAMS ((tree, tree, int));
189 static tree patch_newarray PARAMS ((tree));
190 static tree resolve_type_during_patch PARAMS ((tree));
191 static tree build_this PARAMS ((int));
192 static tree build_wfl_wrap PARAMS ((tree, int));
193 static tree build_return PARAMS ((int, tree));
194 static tree patch_return PARAMS ((tree));
195 static tree maybe_access_field PARAMS ((tree, tree, tree));
196 static int complete_function_arguments PARAMS ((tree));
197 static int check_for_static_method_reference PARAMS ((tree, tree, tree, 
198                                                       tree, tree));
199 static int not_accessible_p PARAMS ((tree, tree, tree, int));
200 static void check_deprecation PARAMS ((tree, tree));
201 static int class_in_current_package PARAMS ((tree));
202 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
203 static tree patch_if_else_statement PARAMS ((tree));
204 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
205 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
206 static tree patch_exit_expr PARAMS ((tree));
207 static tree build_labeled_block PARAMS ((int, tree));
208 static tree finish_labeled_statement PARAMS ((tree, tree));
209 static tree build_bc_statement PARAMS ((int, int, tree));
210 static tree patch_bc_statement PARAMS ((tree));
211 static tree patch_loop_statement PARAMS ((tree));
212 static tree build_new_loop PARAMS ((tree));
213 static tree build_loop_body PARAMS ((int, tree, int));
214 static tree finish_loop_body PARAMS ((int, tree, tree, int));
215 static tree build_debugable_stmt PARAMS ((int, tree));
216 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
217 static tree patch_switch_statement PARAMS ((tree));
218 static tree string_constant_concatenation PARAMS ((tree, tree));
219 static tree build_string_concatenation PARAMS ((tree, tree));
220 static tree patch_string_cst PARAMS ((tree));
221 static tree patch_string PARAMS ((tree));
222 static tree build_try_statement PARAMS ((int, tree, tree));
223 static tree build_try_finally_statement PARAMS ((int, tree, tree));
224 static tree patch_try_statement PARAMS ((tree));
225 static tree patch_synchronized_statement PARAMS ((tree, tree));
226 static tree patch_throw_statement PARAMS ((tree, tree));
227 static void check_thrown_exceptions PARAMS ((int, tree));
228 static int check_thrown_exceptions_do PARAMS ((tree));
229 static void purge_unchecked_exceptions PARAMS ((tree));
230 static void check_throws_clauses PARAMS ((tree, tree, tree));
231 static void finish_method_declaration PARAMS ((tree));
232 static tree build_super_invocation PARAMS ((tree));
233 static int verify_constructor_circularity PARAMS ((tree, tree));
234 static char *constructor_circularity_msg PARAMS ((tree, tree));
235 static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
236                                                           int, int));
237 static const char *get_printable_method_name PARAMS ((tree));
238 static tree patch_conditional_expr PARAMS ((tree, tree, tree));
239 static tree generate_finit PARAMS ((tree));
240 static void add_instance_initializer PARAMS ((tree));
241 static tree build_instance_initializer PARAMS ((tree));
242 static void fix_constructors PARAMS ((tree));
243 static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
244                                                             tree, int *));
245 static void craft_constructor PARAMS ((tree, tree));
246 static int verify_constructor_super PARAMS ((tree));
247 static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
248 static void start_artificial_method_body PARAMS ((tree));
249 static void end_artificial_method_body PARAMS ((tree));
250 static int check_method_redefinition PARAMS ((tree, tree));
251 static int check_method_types_complete PARAMS ((tree));
252 static void java_check_regular_methods PARAMS ((tree));
253 static void java_check_abstract_methods PARAMS ((tree));
254 static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
255 static void unreachable_stmt_error PARAMS ((tree));
256 static tree find_expr_with_wfl PARAMS ((tree));
257 static void missing_return_error PARAMS ((tree));
258 static tree build_new_array_init PARAMS ((int, tree));
259 static tree patch_new_array_init PARAMS ((tree, tree));
260 static tree maybe_build_array_element_wfl PARAMS ((tree));
261 static int array_constructor_check_entry PARAMS ((tree, tree));
262 static const char *purify_type_name PARAMS ((const char *));
263 static tree fold_constant_for_init PARAMS ((tree, tree));
264 static tree strip_out_static_field_access_decl PARAMS ((tree));
265 static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
266 static void static_ref_err PARAMS ((tree, tree, tree));
267 static void parser_add_interface PARAMS ((tree, tree, tree));
268 static void add_superinterfaces PARAMS ((tree, tree));
269 static tree jdep_resolve_class PARAMS ((jdep *));
270 static int note_possible_classname PARAMS ((const char *, int));
271 static void java_complete_expand_classes PARAMS ((void));
272 static void java_complete_expand_class PARAMS ((tree));
273 static void java_complete_expand_methods PARAMS ((tree));
274 static tree cut_identifier_in_qualified PARAMS ((tree));
275 static tree java_stabilize_reference PARAMS ((tree));
276 static tree do_unary_numeric_promotion PARAMS ((tree));
277 static char * operator_string PARAMS ((tree));
278 static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
279 static tree merge_string_cste PARAMS ((tree, tree, int));
280 static tree java_refold PARAMS ((tree));
281 static int java_decl_equiv PARAMS ((tree, tree));
282 static int binop_compound_p PARAMS ((enum tree_code));
283 static tree search_loop PARAMS ((tree));
284 static int labeled_block_contains_loop_p PARAMS ((tree, tree));
285 static int check_abstract_method_definitions PARAMS ((int, tree, tree));
286 static void java_check_abstract_method_definitions PARAMS ((tree));
287 static void java_debug_context_do PARAMS ((int));
288 static void java_parser_context_push_initialized_field PARAMS ((void));
289 static void java_parser_context_pop_initialized_field PARAMS ((void));
290 static tree reorder_static_initialized PARAMS ((tree));
291 static void java_parser_context_suspend PARAMS ((void));
292 static void java_parser_context_resume PARAMS ((void));
293 static int pop_current_osb PARAMS ((struct parser_ctxt *));
294
295 /* JDK 1.1 work. FIXME */
296
297 static tree maybe_make_nested_class_name PARAMS ((tree));
298 static void make_nested_class_name PARAMS ((tree));
299 static void set_nested_class_simple_name_value PARAMS ((tree, int));
300 static void link_nested_class_to_enclosing PARAMS ((void));
301 static tree find_as_inner_class PARAMS ((tree, tree, tree));
302 static tree find_as_inner_class_do PARAMS ((tree, tree));
303 static int check_inner_class_redefinition PARAMS ((tree, tree));
304
305 static tree build_thisn_assign PARAMS ((void));
306 static tree build_current_thisn PARAMS ((tree));
307 static tree build_access_to_thisn PARAMS ((tree, tree, int));
308 static tree maybe_build_thisn_access_method PARAMS ((tree));
309
310 static tree build_outer_field_access PARAMS ((tree, tree));
311 static tree build_outer_field_access_methods PARAMS ((tree));
312 static tree build_outer_field_access_expr PARAMS ((int, tree, tree, 
313                                                   tree, tree));
314 static tree build_outer_method_access_method PARAMS ((tree));
315 static tree build_new_access_id PARAMS ((void));
316 static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
317                                                     tree, tree));
318
319 static int outer_field_access_p PARAMS ((tree, tree));
320 static int outer_field_expanded_access_p PARAMS ((tree, tree *, 
321                                                  tree *, tree *));
322 static tree outer_field_access_fix PARAMS ((tree, tree, tree));
323 static tree build_incomplete_class_ref PARAMS ((int, tree));
324 static tree patch_incomplete_class_ref PARAMS ((tree));
325 static tree create_anonymous_class PARAMS ((int, tree));
326 static void patch_anonymous_class PARAMS ((tree, tree, tree));
327 static void add_inner_class_fields PARAMS ((tree, tree));
328
329 static tree build_dot_class_method PARAMS ((tree));
330 static tree build_dot_class_method_invocation PARAMS ((tree));
331 static void create_new_parser_context PARAMS ((int));
332 static void mark_parser_ctxt PARAMS ((void *));
333 static tree maybe_build_class_init_for_field PARAMS ((tree, tree));
334
335 /* Number of error found so far. */
336 int java_error_count; 
337 /* Number of warning found so far. */
338 int java_warning_count;
339 /* Tell when not to fold, when doing xrefs */
340 int do_not_fold;
341 /* Cyclic inheritance report, as it can be set by layout_class */
342 const char *cyclic_inheritance_report;
343  
344 /* Tell when we're within an instance initializer */
345 static int in_instance_initializer;
346
347 /* The current parser context */
348 struct parser_ctxt *ctxp;
349
350 /* List of things that were analyzed for which code will be generated */
351 struct parser_ctxt *ctxp_for_generation = NULL;
352
353 /* binop_lookup maps token to tree_code. It is used where binary
354    operations are involved and required by the parser. RDIV_EXPR
355    covers both integral/floating point division. The code is changed
356    once the type of both operator is worked out.  */
357
358 static enum tree_code binop_lookup[19] = 
359   { 
360     PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
361     LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR, 
362     BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
363     TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
364     EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
365    };
366 #define BINOP_LOOKUP(VALUE)                                             \
367   binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
368
369 /* This is the end index for binary operators that can also be used
370    in compound assignements. */
371 #define BINOP_COMPOUND_CANDIDATES 11
372
373 /* The "$L" identifier we use to create labels.  */
374 static tree label_id = NULL_TREE;
375
376 /* The "StringBuffer" identifier used for the String `+' operator. */
377 static tree wfl_string_buffer = NULL_TREE; 
378
379 /* The "append" identifier used for String `+' operator.  */
380 static tree wfl_append = NULL_TREE;
381
382 /* The "toString" identifier used for String `+' operator. */
383 static tree wfl_to_string = NULL_TREE;
384
385 /* The "java.lang" import qualified name.  */
386 static tree java_lang_id = NULL_TREE;
387
388 /* The generated `inst$' identifier used for generated enclosing
389    instance/field access functions.  */
390 static tree inst_id = NULL_TREE;
391
392 /* The "java.lang.Cloneable" qualified name.  */
393 static tree java_lang_cloneable = NULL_TREE;
394
395 /* The "java.io.Serializable" qualified name.  */
396 static tree java_io_serializable = NULL_TREE; 
397
398 /* Context and flag for static blocks */
399 static tree current_static_block = NULL_TREE;
400
401 /* The generated `write_parm_value$' identifier.  */
402 static tree wpv_id;
403
404 /* The list of all packages we've seen so far */
405 static tree package_list = NULL_TREE;
406  
407 /* Hold THIS for the scope of the current public method decl.  */
408 static tree current_this;
409
410 /* Hold a list of catch clauses list. The first element of this list is
411    the list of the catch clauses of the currently analysed try block. */
412 static tree currently_caught_type_list;
413
414 static tree src_parse_roots[1] = { NULL_TREE };
415
416 /* All classes seen from source code */
417 #define gclass_list src_parse_roots[0]
418
419 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
420    line and point it out.  */
421 /* Should point out the one that don't fit. ASCII/unicode, going
422    backward. FIXME */
423
424 #define check_modifiers(__message, __value, __mask) do {        \
425   if ((__value) & ~(__mask))                                    \
426     {                                                           \
427       int i, remainder = (__value) & ~(__mask);                 \
428       for (i = 0; i <= 10; i++)                                 \
429         if ((1 << i) & remainder)                               \
430           parse_error_context (ctxp->modifier_ctx [i], (__message), \
431                                java_accstring_lookup (1 << i)); \
432     }                                                           \
433 } while (0)
434
435 %}
436
437 %union {
438   tree node;
439   int sub_token;
440   struct {
441     int token;
442     int location;
443   } operator;
444   int value;
445 }
446
447 %{
448 #include "lex.c"
449 %}
450
451 %pure_parser
452
453 /* Things defined here have to match the order of what's in the
454    binop_lookup table.  */
455
456 %token   PLUS_TK         MINUS_TK        MULT_TK         DIV_TK    REM_TK
457 %token   LS_TK           SRS_TK          ZRS_TK
458 %token   AND_TK          XOR_TK          OR_TK
459 %token   BOOL_AND_TK BOOL_OR_TK 
460 %token   EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
461
462 /* This maps to the same binop_lookup entry than the token above */
463
464 %token   PLUS_ASSIGN_TK  MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
465 %token   REM_ASSIGN_TK   
466 %token   LS_ASSIGN_TK    SRS_ASSIGN_TK   ZRS_ASSIGN_TK
467 %token   AND_ASSIGN_TK   XOR_ASSIGN_TK   OR_ASSIGN_TK
468
469
470 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
471
472 %token   PUBLIC_TK       PRIVATE_TK         PROTECTED_TK
473 %token   STATIC_TK       FINAL_TK           SYNCHRONIZED_TK
474 %token   VOLATILE_TK     TRANSIENT_TK       NATIVE_TK
475 %token   PAD_TK          ABSTRACT_TK        MODIFIER_TK
476 %token   STRICT_TK
477
478 /* Keep those two in order, too */
479 %token   DECR_TK INCR_TK
480
481 /* From now one, things can be in any order */
482
483 %token   DEFAULT_TK      IF_TK              THROW_TK
484 %token   BOOLEAN_TK      DO_TK              IMPLEMENTS_TK
485 %token   THROWS_TK       BREAK_TK           IMPORT_TK       
486 %token   ELSE_TK         INSTANCEOF_TK      RETURN_TK
487 %token   VOID_TK         CATCH_TK           INTERFACE_TK
488 %token   CASE_TK         EXTENDS_TK         FINALLY_TK
489 %token   SUPER_TK        WHILE_TK           CLASS_TK
490 %token   SWITCH_TK       CONST_TK           TRY_TK
491 %token   FOR_TK          NEW_TK             CONTINUE_TK
492 %token   GOTO_TK         PACKAGE_TK         THIS_TK
493
494 %token   BYTE_TK         SHORT_TK           INT_TK            LONG_TK
495 %token   CHAR_TK         INTEGRAL_TK
496
497 %token   FLOAT_TK        DOUBLE_TK          FP_TK
498
499 %token   ID_TK
500
501 %token   REL_QM_TK         REL_CL_TK NOT_TK  NEG_TK
502
503 %token   ASSIGN_ANY_TK   ASSIGN_TK
504 %token   OP_TK  CP_TK  OCB_TK  CCB_TK  OSB_TK  CSB_TK  SC_TK  C_TK DOT_TK
505
506 %token   STRING_LIT_TK   CHAR_LIT_TK        INT_LIT_TK        FP_LIT_TK
507 %token   TRUE_TK         FALSE_TK           BOOL_LIT_TK       NULL_TK
508
509 %type    <value>        modifiers MODIFIER_TK final synchronized
510
511 %type    <node>         super ID_TK identifier
512 %type    <node>         name simple_name qualified_name
513 %type    <node>         type_declaration compilation_unit
514                         field_declaration method_declaration extends_interfaces
515                         interfaces interface_type_list
516                         class_member_declaration
517                         import_declarations package_declaration 
518                         type_declarations interface_body
519                         interface_member_declaration constant_declaration
520                         interface_member_declarations interface_type
521                         abstract_method_declaration interface_type_list
522 %type    <node>         class_body_declaration class_member_declaration
523                         static_initializer constructor_declaration block
524 %type    <node>         class_body_declarations constructor_header
525 %type    <node>         class_or_interface_type class_type class_type_list
526                         constructor_declarator explicit_constructor_invocation
527 %type    <node>         dim_expr dim_exprs this_or_super throws
528
529 %type    <node>         variable_declarator_id variable_declarator
530                         variable_declarators variable_initializer
531                         variable_initializers constructor_body
532                         array_initializer
533
534 %type    <node>         class_body block_end constructor_block_end
535 %type    <node>         statement statement_without_trailing_substatement
536                         labeled_statement if_then_statement label_decl
537                         if_then_else_statement while_statement for_statement
538                         statement_nsi labeled_statement_nsi do_statement
539                         if_then_else_statement_nsi while_statement_nsi
540                         for_statement_nsi statement_expression_list for_init
541                         for_update statement_expression expression_statement
542                         primary_no_new_array expression primary
543                         array_creation_expression array_type
544                         class_instance_creation_expression field_access
545                         method_invocation array_access something_dot_new
546                         argument_list postfix_expression while_expression 
547                         post_increment_expression post_decrement_expression
548                         unary_expression_not_plus_minus unary_expression
549                         pre_increment_expression pre_decrement_expression
550                         unary_expression_not_plus_minus cast_expression
551                         multiplicative_expression additive_expression
552                         shift_expression relational_expression 
553                         equality_expression and_expression 
554                         exclusive_or_expression inclusive_or_expression
555                         conditional_and_expression conditional_or_expression
556                         conditional_expression assignment_expression
557                         left_hand_side assignment for_header for_begin
558                         constant_expression do_statement_begin empty_statement
559                         switch_statement synchronized_statement throw_statement
560                         try_statement switch_expression switch_block
561                         catches catch_clause catch_clause_parameter finally
562                         anonymous_class_creation
563 %type    <node>         return_statement break_statement continue_statement
564
565 %type    <operator>     ASSIGN_TK      MULT_ASSIGN_TK  DIV_ASSIGN_TK  
566 %type    <operator>     REM_ASSIGN_TK  PLUS_ASSIGN_TK  MINUS_ASSIGN_TK
567 %type    <operator>     LS_ASSIGN_TK   SRS_ASSIGN_TK   ZRS_ASSIGN_TK
568 %type    <operator>     AND_ASSIGN_TK  XOR_ASSIGN_TK   OR_ASSIGN_TK
569 %type    <operator>     ASSIGN_ANY_TK  assignment_operator
570 %token   <operator>     EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK 
571 %token   <operator>     BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
572 %token   <operator>     DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
573 %token   <operator>     NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
574 %token   <operator>     OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
575 %type    <operator>     THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK 
576 %type    <operator>     CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
577 %type    <operator>     NEW_TK
578
579 %type    <node>         method_body 
580         
581 %type    <node>         literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
582                         STRING_LIT_TK NULL_TK VOID_TK
583
584 %type    <node>         IF_TK WHILE_TK FOR_TK
585
586 %type    <node>         formal_parameter_list formal_parameter
587                         method_declarator method_header
588
589 %type    <node>         primitive_type reference_type type 
590                         BOOLEAN_TK INTEGRAL_TK FP_TK
591
592 /* Added or modified JDK 1.1 rule types  */
593 %type    <node>         type_literals
594
595 %%
596 /* 19.2 Production from 2.3: The Syntactic Grammar  */
597 goal:
598                 {
599                   /* Register static variables with the garbage
600                      collector.  */
601                   ggc_add_tree_root (&label_id, 1);
602                   ggc_add_tree_root (&wfl_string_buffer, 1);
603                   ggc_add_tree_root (&wfl_append, 1);
604                   ggc_add_tree_root (&wfl_to_string, 1);
605                   ggc_add_tree_root (&java_lang_id, 1);
606                   ggc_add_tree_root (&inst_id, 1);
607                   ggc_add_tree_root (&java_lang_cloneable, 1);
608                   ggc_add_tree_root (&java_io_serializable, 1);
609                   ggc_add_tree_root (&current_static_block, 1);
610                   ggc_add_tree_root (&wpv_id, 1);
611                   ggc_add_tree_root (&package_list, 1);
612                   ggc_add_tree_root (&current_this, 1);
613                   ggc_add_tree_root (&currently_caught_type_list, 1);
614                   ggc_add_root (&ctxp, 1, 
615                                 sizeof (struct parser_ctxt *),
616                                 mark_parser_ctxt);
617                   ggc_add_root (&ctxp_for_generation, 1, 
618                                 sizeof (struct parser_ctxt *),
619                                 mark_parser_ctxt);
620                 }
621         compilation_unit
622                 {}
623 ;
624
625 /* 19.3 Productions from 3: Lexical structure  */
626 literal:
627         INT_LIT_TK
628 |       FP_LIT_TK
629 |       BOOL_LIT_TK
630 |       CHAR_LIT_TK
631 |       STRING_LIT_TK
632 |       NULL_TK
633 ;
634
635 /* 19.4 Productions from 4: Types, Values and Variables  */
636 type:
637         primitive_type
638 |       reference_type
639 ;
640
641 primitive_type:
642         INTEGRAL_TK
643 |       FP_TK
644 |       BOOLEAN_TK
645 ;
646
647 reference_type:
648         class_or_interface_type
649 |       array_type
650 ;
651
652 class_or_interface_type:
653         name
654 ;
655
656 class_type:
657         class_or_interface_type /* Default rule */
658 ;
659
660 interface_type:
661          class_or_interface_type
662 ;
663
664 array_type:
665         primitive_type dims
666                 { 
667                   int osb = pop_current_osb (ctxp);
668                   tree t = build_java_array_type (($1), -1);
669                   while (--osb)
670                     t = build_unresolved_array_type (t);
671                   $$ = t;
672                 }
673 |       name dims
674                 { 
675                   int osb = pop_current_osb (ctxp);
676                   tree t = $1;
677                   while (osb--)
678                     t = build_unresolved_array_type (t);
679                   $$ = t;
680                 }
681 ;
682
683 /* 19.5 Productions from 6: Names  */
684 name:
685         simple_name             /* Default rule */
686 |       qualified_name          /* Default rule */
687 ;
688
689 simple_name:
690         identifier              /* Default rule */
691 ;
692
693 qualified_name:
694         name DOT_TK identifier
695                 { $$ = make_qualified_name ($1, $3, $2.location); }
696 ;
697
698 identifier:
699         ID_TK
700 ;
701
702 /* 19.6: Production from 7: Packages  */
703 compilation_unit:
704                 {$$ = NULL;}
705 |       package_declaration
706 |       import_declarations
707 |       type_declarations
708 |       package_declaration import_declarations
709 |       package_declaration type_declarations
710 |       import_declarations type_declarations
711 |       package_declaration import_declarations type_declarations
712 ;
713
714 import_declarations:
715         import_declaration
716                 {
717                   $$ = NULL;
718                 }
719 |       import_declarations import_declaration
720                 {
721                   $$ = NULL;
722                 }
723 ;
724
725 type_declarations:
726         type_declaration
727 |       type_declarations type_declaration
728 ;
729
730 package_declaration:
731         PACKAGE_TK name SC_TK
732                 { 
733                   ctxp->package = EXPR_WFL_NODE ($2);
734                   register_package (ctxp->package);
735                 }
736 |       PACKAGE_TK error
737                 {yyerror ("Missing name"); RECOVER;}
738 |       PACKAGE_TK name error
739                 {yyerror ("';' expected"); RECOVER;}
740 ;
741
742 import_declaration:
743         single_type_import_declaration
744 |       type_import_on_demand_declaration
745 ;
746
747 single_type_import_declaration:
748         IMPORT_TK name SC_TK
749                 {
750                   tree name = EXPR_WFL_NODE ($2), last_name;
751                   int   i = IDENTIFIER_LENGTH (name)-1;
752                   const char *last = &IDENTIFIER_POINTER (name)[i];
753                   while (last != IDENTIFIER_POINTER (name))
754                     {
755                       if (last [0] == '.')
756                         break;
757                       last--;
758                     }
759                   last_name = get_identifier (++last);
760                   if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
761                     {
762                       tree err = find_name_in_single_imports (last_name);
763                       if (err && err != name)
764                         parse_error_context
765                           ($2, "Ambiguous class: `%s' and `%s'",
766                            IDENTIFIER_POINTER (name), 
767                            IDENTIFIER_POINTER (err));
768                       else
769                         REGISTER_IMPORT ($2, last_name);
770                     }
771                   else
772                     REGISTER_IMPORT ($2, last_name);
773                 }
774 |       IMPORT_TK error
775                 {yyerror ("Missing name"); RECOVER;}
776 |       IMPORT_TK name error
777                 {yyerror ("';' expected"); RECOVER;}
778 ;
779
780 type_import_on_demand_declaration:
781         IMPORT_TK name DOT_TK MULT_TK SC_TK
782                 {
783                   tree name = EXPR_WFL_NODE ($2);
784                   /* Don't import java.lang.* twice. */
785                   if (name != java_lang_id)
786                     {
787                       read_import_dir ($2);
788                       ctxp->import_demand_list = 
789                         chainon (ctxp->import_demand_list,
790                                  build_tree_list ($2, NULL_TREE));
791                     }
792                 }
793 |       IMPORT_TK name DOT_TK error
794                 {yyerror ("'*' expected"); RECOVER;}
795 |       IMPORT_TK name DOT_TK MULT_TK error
796                 {yyerror ("';' expected"); RECOVER;}
797 ;
798
799 type_declaration:
800         class_declaration
801                 { end_class_declaration (0); }
802 |       interface_declaration
803                 { end_class_declaration (0); }
804 |       empty_statement
805 |       error
806                 {
807                   YYERROR_NOW;
808                   yyerror ("Class or interface declaration expected");
809                 }
810 ;
811
812 /* 19.7 Shortened from the original:
813    modifiers: modifier | modifiers modifier
814    modifier: any of public...  */
815 modifiers:
816         MODIFIER_TK
817                 {
818                   $$ = (1 << $1);
819                 }
820 |       modifiers MODIFIER_TK
821                 {
822                   int acc = (1 << $2);
823                   if ($$ & acc)
824                     parse_error_context 
825                       (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
826                        java_accstring_lookup (acc));
827                   else
828                     {
829                       $$ |= acc;
830                     }
831                 }
832 ;
833
834 /* 19.8.1 Production from $8.1: Class Declaration */
835 class_declaration:
836         modifiers CLASS_TK identifier super interfaces
837                 { create_class ($1, $3, $4, $5); }
838         class_body
839 |       CLASS_TK identifier super interfaces 
840                 { create_class (0, $2, $3, $4); }
841         class_body
842 |       modifiers CLASS_TK error
843                 {yyerror ("Missing class name"); RECOVER;}
844 |       CLASS_TK error
845                 {yyerror ("Missing class name"); RECOVER;}
846 |       CLASS_TK identifier error
847                 {
848                   if (!ctxp->class_err) yyerror ("'{' expected"); 
849                   DRECOVER(class1);
850                 }
851 |       modifiers CLASS_TK identifier error
852                 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
853 ;
854
855 super:
856                 { $$ = NULL; }
857 |       EXTENDS_TK class_type
858                 { $$ = $2; }
859 |       EXTENDS_TK class_type error
860                 {yyerror ("'{' expected"); ctxp->class_err=1;}
861 |       EXTENDS_TK error
862                 {yyerror ("Missing super class name"); ctxp->class_err=1;}
863 ;
864
865 interfaces:
866                 { $$ = NULL_TREE; }
867 |       IMPLEMENTS_TK interface_type_list
868                 { $$ = $2; }
869 |       IMPLEMENTS_TK error
870                 {
871                   ctxp->class_err=1;
872                   yyerror ("Missing interface name"); 
873                 }
874 ;
875
876 interface_type_list:
877         interface_type
878                 { 
879                   ctxp->interface_number = 1;
880                   $$ = build_tree_list ($1, NULL_TREE);
881                 }
882 |       interface_type_list C_TK interface_type
883                 { 
884                   ctxp->interface_number++;
885                   $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
886                 }
887 |       interface_type_list C_TK error
888                 {yyerror ("Missing interface name"); RECOVER;}
889 ;
890
891 class_body:
892         OCB_TK CCB_TK
893                 { 
894                   /* Store the location of the `}' when doing xrefs */
895                   if (flag_emit_xref)
896                     DECL_END_SOURCE_LINE (GET_CPC ()) = 
897                       EXPR_WFL_ADD_COL ($2.location, 1);
898                   $$ = GET_CPC ();
899                 }
900 |       OCB_TK class_body_declarations CCB_TK
901                 { 
902                   /* Store the location of the `}' when doing xrefs */
903                   if (flag_emit_xref)
904                     DECL_END_SOURCE_LINE (GET_CPC ()) = 
905                       EXPR_WFL_ADD_COL ($3.location, 1);
906                   $$ = GET_CPC ();
907                 }
908 ;
909
910 class_body_declarations:
911         class_body_declaration
912 |       class_body_declarations class_body_declaration
913 ;
914
915 class_body_declaration:
916         class_member_declaration
917 |       static_initializer
918 |       constructor_declaration
919 |       block                   /* Added, JDK1.1, instance initializer */
920                 {
921                   TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
922                   SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
923                 }
924 ;
925
926 class_member_declaration:
927         field_declaration
928 |       method_declaration
929 |       class_declaration       /* Added, JDK1.1 inner classes */
930                 { end_class_declaration (1); }
931 |       interface_declaration   /* Added, JDK1.1 inner interfaces */
932                 { end_class_declaration (1); }
933 |       empty_statement
934 ;
935
936 /* 19.8.2 Productions from 8.3: Field Declarations  */
937 field_declaration:
938         type variable_declarators SC_TK
939                 { register_fields (0, $1, $2); }
940 |       modifiers type variable_declarators SC_TK
941                 {
942                   check_modifiers 
943                     ("Illegal modifier `%s' for field declaration",
944                      $1, FIELD_MODIFIERS);
945                   check_modifiers_consistency ($1);
946                   register_fields ($1, $2, $3);
947                 }
948 ;
949
950 variable_declarators:
951         /* Should we use build_decl_list () instead ? FIXME */
952         variable_declarator     /* Default rule */
953 |       variable_declarators C_TK variable_declarator
954                 { $$ = chainon ($1, $3); }
955 |       variable_declarators C_TK error
956                 {yyerror ("Missing term"); RECOVER;}
957 ;
958
959 variable_declarator:
960         variable_declarator_id
961                 { $$ = build_tree_list ($1, NULL_TREE); }
962 |       variable_declarator_id ASSIGN_TK variable_initializer
963                 { 
964                   if (java_error_count)
965                     $3 = NULL_TREE;
966                   $$ = build_tree_list 
967                     ($1, build_assignment ($2.token, $2.location, $1, $3));
968                 }
969 |       variable_declarator_id ASSIGN_TK error
970                 {
971                   yyerror ("Missing variable initializer");
972                   $$ = build_tree_list ($1, NULL_TREE);
973                   RECOVER;
974                 }
975 |       variable_declarator_id ASSIGN_TK variable_initializer error
976                 {
977                   yyerror ("';' expected");
978                   $$ = build_tree_list ($1, NULL_TREE);
979                   RECOVER;
980                 }
981 ;
982
983 variable_declarator_id:
984         identifier
985 |       variable_declarator_id OSB_TK CSB_TK
986                 { $$ = build_unresolved_array_type ($1); }
987 |       identifier error
988                 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
989 |       variable_declarator_id OSB_TK error
990                 { 
991                   tree node = java_lval.node;
992                   if (node && (TREE_CODE (node) == INTEGER_CST
993                                || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
994                     yyerror ("Can't specify array dimension in a declaration");
995                   else
996                     yyerror ("']' expected");
997                   DRECOVER(vdi);
998                 }
999 |       variable_declarator_id CSB_TK error
1000                 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1001 ;
1002
1003 variable_initializer:
1004         expression
1005 |       array_initializer
1006 ;
1007
1008 /* 19.8.3 Productions from 8.4: Method Declarations  */
1009 method_declaration:
1010         method_header 
1011                 {
1012                   current_function_decl = $1;
1013                   if (current_function_decl
1014                       && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1015                     source_start_java_method (current_function_decl);
1016                   else
1017                     current_function_decl = NULL_TREE;
1018                 }
1019         method_body
1020                 { finish_method_declaration ($3); }
1021 |       method_header error
1022                 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1023 ;
1024
1025 method_header:  
1026         type method_declarator throws
1027                 { $$ = method_header (0, $1, $2, $3); }
1028 |       VOID_TK method_declarator throws
1029                 { $$ = method_header (0, void_type_node, $2, $3); }
1030 |       modifiers type method_declarator throws
1031                 { $$ = method_header ($1, $2, $3, $4); }
1032 |       modifiers VOID_TK method_declarator throws
1033                 { $$ = method_header ($1, void_type_node, $3, $4); }
1034 |       type error
1035                 {
1036                   yyerror ("Invalid method declaration, method name required");
1037                   RECOVER;
1038                 }
1039 |       modifiers type error
1040                 {RECOVER;}
1041 |       VOID_TK error
1042                 {yyerror ("Identifier expected"); RECOVER;}
1043 |       modifiers VOID_TK error
1044                 {yyerror ("Identifier expected"); RECOVER;}
1045 |       modifiers error
1046                 {
1047                   yyerror ("Invalid method declaration, return type required");
1048                   RECOVER;
1049                 }
1050 ;
1051
1052 method_declarator:
1053         identifier OP_TK CP_TK
1054                 { 
1055                   ctxp->formal_parameter_number = 0;
1056                   $$ = method_declarator ($1, NULL_TREE);
1057                 }
1058 |       identifier OP_TK formal_parameter_list CP_TK
1059                 { $$ = method_declarator ($1, $3); }
1060 |       method_declarator OSB_TK CSB_TK
1061                 {
1062                   EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1063                   TREE_PURPOSE ($1) = 
1064                     build_unresolved_array_type (TREE_PURPOSE ($1));
1065                   parse_warning_context 
1066                     (wfl_operator, 
1067                      "Discouraged form of returned type specification");
1068                 }
1069 |       identifier OP_TK error
1070                 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1071 |       method_declarator OSB_TK error
1072                 {yyerror ("']' expected"); RECOVER;}
1073 ;
1074
1075 formal_parameter_list:
1076         formal_parameter
1077                 {
1078                   ctxp->formal_parameter_number = 1;
1079                 }
1080 |       formal_parameter_list C_TK formal_parameter
1081                 {
1082                   ctxp->formal_parameter_number += 1;
1083                   $$ = chainon ($1, $3);
1084                 }
1085 |       formal_parameter_list C_TK error
1086                 { yyerror ("Missing formal parameter term"); RECOVER; }
1087 ;
1088
1089 formal_parameter:
1090         type variable_declarator_id
1091                 {
1092                   $$ = build_tree_list ($2, $1);
1093                 }
1094 |       final type variable_declarator_id /* Added, JDK1.1 final parms */
1095                 { 
1096                   $$ = build_tree_list ($3, $2);
1097                   ARG_FINAL_P ($$) = 1;
1098                 }
1099 |       type error
1100                 {
1101                   yyerror ("Missing identifier"); RECOVER;
1102                   $$ = NULL_TREE;
1103                 }
1104 |       final type error
1105                 {
1106                   yyerror ("Missing identifier"); RECOVER;
1107                   $$ = NULL_TREE;
1108                 }
1109 ;
1110
1111 final:
1112         modifiers
1113                 {
1114                   check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1115                                    $1, ACC_FINAL);
1116                   if ($1 != ACC_FINAL)
1117                     MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1118                 }
1119 ;
1120
1121 throws:
1122                 { $$ = NULL_TREE; }
1123 |       THROWS_TK class_type_list
1124                 { $$ = $2; }
1125 |       THROWS_TK error
1126                 {yyerror ("Missing class type term"); RECOVER;}
1127 ;
1128
1129 class_type_list:
1130         class_type
1131                 { $$ = build_tree_list ($1, $1); }
1132 |       class_type_list C_TK class_type
1133                 { $$ = tree_cons ($3, $3, $1); }
1134 |       class_type_list C_TK error
1135                 {yyerror ("Missing class type term"); RECOVER;}
1136 ;
1137
1138 method_body:
1139         block
1140 |       SC_TK { $$ = NULL_TREE; }
1141 ;
1142
1143 /* 19.8.4 Productions from 8.5: Static Initializers  */
1144 static_initializer:
1145         static block
1146                 {
1147                   TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1148                   SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1149                   current_static_block = NULL_TREE;
1150                 }
1151 ;
1152
1153 static:                         /* Test lval.sub_token here */
1154         modifiers
1155                 {
1156                   check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1157                   /* Can't have a static initializer in an innerclass */
1158                   if ($1 | ACC_STATIC &&
1159                       GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1160                     parse_error_context 
1161                       (MODIFIER_WFL (STATIC_TK),
1162                        "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1163                        IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1164                   SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1165                 }
1166 ;
1167
1168 /* 19.8.5 Productions from 8.6: Constructor Declarations  */
1169 constructor_declaration:
1170         constructor_header
1171                 {
1172                   current_function_decl = $1;
1173                   source_start_java_method (current_function_decl);
1174                 }
1175         constructor_body
1176                 { finish_method_declaration ($3); }
1177 ;
1178
1179 constructor_header:
1180         constructor_declarator throws
1181                 { $$ = method_header (0, NULL_TREE, $1, $2); }
1182 |       modifiers constructor_declarator throws
1183                 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1184 ;
1185
1186 constructor_declarator:
1187         simple_name OP_TK CP_TK
1188                 { 
1189                   ctxp->formal_parameter_number = 0;  
1190                   $$ = method_declarator ($1, NULL_TREE);
1191                 }
1192 |       simple_name OP_TK formal_parameter_list CP_TK
1193                 { $$ = method_declarator ($1, $3); }
1194 ;
1195
1196 constructor_body:
1197         /* Unlike regular method, we always need a complete (empty)
1198            body so we can safely perform all the required code
1199            addition (super invocation and field initialization) */
1200         block_begin constructor_block_end
1201                 { 
1202                   BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1203                   $$ = $2;
1204                 }
1205 |       block_begin explicit_constructor_invocation constructor_block_end
1206                 { $$ = $3; }
1207 |       block_begin block_statements constructor_block_end
1208                 { $$ = $3; }
1209 |       block_begin explicit_constructor_invocation block_statements constructor_block_end
1210                 { $$ = $4; }
1211 ;
1212
1213 constructor_block_end:
1214         block_end
1215 ;
1216
1217 /* Error recovery for that rule moved down expression_statement: rule.  */
1218 explicit_constructor_invocation:
1219         this_or_super OP_TK CP_TK SC_TK
1220                 { 
1221                   $$ = build_method_invocation ($1, NULL_TREE); 
1222                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1223                   $$ = java_method_add_stmt (current_function_decl, $$);
1224                 }
1225 |       this_or_super OP_TK argument_list CP_TK SC_TK
1226                 { 
1227                   $$ = build_method_invocation ($1, $3); 
1228                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1229                   $$ = java_method_add_stmt (current_function_decl, $$);
1230                 }
1231         /* Added, JDK1.1 inner classes. Modified because the rule
1232            'primary' couldn't work.  */
1233 |       name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1234                 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1235 |       name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1236                 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1237 ;
1238
1239 this_or_super:                  /* Added, simplifies error diagnostics */
1240         THIS_TK
1241                 {
1242                   tree wfl = build_wfl_node (this_identifier_node);
1243                   EXPR_WFL_LINECOL (wfl) = $1.location;
1244                   $$ = wfl;
1245                 }
1246 |       SUPER_TK
1247                 {
1248                   tree wfl = build_wfl_node (super_identifier_node);
1249                   EXPR_WFL_LINECOL (wfl) = $1.location;
1250                   $$ = wfl;
1251                 }
1252 ;
1253
1254 /* 19.9 Productions from 9: Interfaces  */
1255 /* 19.9.1 Productions from 9.1: Interfaces Declarations  */
1256 interface_declaration:
1257         INTERFACE_TK identifier
1258                 { create_interface (0, $2, NULL_TREE); }
1259         interface_body
1260 |       modifiers INTERFACE_TK identifier
1261                 { create_interface ($1, $3, NULL_TREE); }
1262         interface_body
1263 |       INTERFACE_TK identifier extends_interfaces
1264                 { create_interface (0, $2, $3); }
1265         interface_body
1266 |       modifiers INTERFACE_TK identifier extends_interfaces
1267                 { create_interface ($1, $3, $4); }
1268         interface_body
1269 |       INTERFACE_TK identifier error
1270                 {yyerror ("'{' expected"); RECOVER;}
1271 |       modifiers INTERFACE_TK identifier error
1272                 {yyerror ("'{' expected"); RECOVER;}
1273 ;
1274
1275 extends_interfaces:
1276         EXTENDS_TK interface_type
1277                 { 
1278                   ctxp->interface_number = 1;
1279                   $$ = build_tree_list ($2, NULL_TREE);
1280                 }
1281 |       extends_interfaces C_TK interface_type
1282                 { 
1283                   ctxp->interface_number++;
1284                   $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1285                 }
1286 |       EXTENDS_TK error
1287                 {yyerror ("Invalid interface type"); RECOVER;}
1288 |       extends_interfaces C_TK error
1289                 {yyerror ("Missing term"); RECOVER;}
1290 ;
1291
1292 interface_body:
1293         OCB_TK CCB_TK
1294                 { $$ = NULL_TREE; }
1295 |       OCB_TK interface_member_declarations CCB_TK
1296                 { $$ = NULL_TREE; }
1297 ;
1298
1299 interface_member_declarations:
1300         interface_member_declaration
1301 |       interface_member_declarations interface_member_declaration
1302 ;
1303
1304 interface_member_declaration:
1305         constant_declaration
1306 |       abstract_method_declaration
1307 |       class_declaration       /* Added, JDK1.1 inner classes */
1308                 { end_class_declaration (1); }
1309 |       interface_declaration   /* Added, JDK1.1 inner interfaces */
1310                 { end_class_declaration (1); }
1311 ;
1312
1313 constant_declaration:
1314         field_declaration
1315 ;
1316
1317 abstract_method_declaration:
1318         method_header SC_TK
1319                 { 
1320                   check_abstract_method_header ($1);
1321                   current_function_decl = NULL_TREE; /* FIXME ? */
1322                 }
1323 |       method_header error
1324                 {yyerror ("';' expected"); RECOVER;}
1325 ;
1326
1327 /* 19.10 Productions from 10: Arrays  */
1328 array_initializer:
1329         OCB_TK CCB_TK
1330                 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1331 |       OCB_TK variable_initializers CCB_TK
1332                 { $$ = build_new_array_init ($1.location, $2); }
1333 |       OCB_TK variable_initializers C_TK CCB_TK
1334                 { $$ = build_new_array_init ($1.location, $2); }
1335 ;
1336
1337 variable_initializers:
1338         variable_initializer
1339                 { 
1340                   $$ = tree_cons (maybe_build_array_element_wfl ($1), 
1341                                   $1, NULL_TREE);
1342                 }
1343 |       variable_initializers C_TK variable_initializer
1344                 {
1345                   $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1346                 }
1347 |       variable_initializers C_TK error
1348                 {yyerror ("Missing term"); RECOVER;}
1349 ;
1350
1351 /* 19.11 Production from 14: Blocks and Statements  */
1352 block:
1353         OCB_TK CCB_TK
1354                 { 
1355                   /* Store the location of the `}' when doing xrefs */
1356                   if (current_function_decl && flag_emit_xref)
1357                     DECL_END_SOURCE_LINE (current_function_decl) = 
1358                       EXPR_WFL_ADD_COL ($2.location, 1);
1359                   $$ = empty_stmt_node; 
1360                 }
1361 |       block_begin block_statements block_end
1362                 { $$ = $3; }
1363 ;
1364
1365 block_begin:
1366         OCB_TK
1367                 { enter_block (); }
1368 ;
1369
1370 block_end:
1371         CCB_TK
1372                 { 
1373                   maybe_absorb_scoping_blocks ();
1374                   /* Store the location of the `}' when doing xrefs */
1375                   if (current_function_decl && flag_emit_xref)
1376                     DECL_END_SOURCE_LINE (current_function_decl) = 
1377                       EXPR_WFL_ADD_COL ($1.location, 1);                  
1378                   $$ = exit_block ();
1379                   if (!BLOCK_SUBBLOCKS ($$))
1380                     BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1381                 }
1382 ;
1383
1384 block_statements:
1385         block_statement
1386 |       block_statements block_statement
1387 ;
1388
1389 block_statement:
1390         local_variable_declaration_statement
1391 |       statement
1392                 { java_method_add_stmt (current_function_decl, $1); }
1393 |       class_declaration       /* Added, JDK1.1 local classes */
1394                 { 
1395                   LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1396                   end_class_declaration (1);
1397                 }
1398 ;
1399
1400 local_variable_declaration_statement:
1401         local_variable_declaration SC_TK /* Can't catch missing ';' here */
1402 ;
1403
1404 local_variable_declaration:
1405         type variable_declarators
1406                 { declare_local_variables (0, $1, $2); }
1407 |       final type variable_declarators /* Added, JDK1.1 final locals */
1408                 { declare_local_variables ($1, $2, $3); }
1409 ;
1410
1411 statement:
1412         statement_without_trailing_substatement
1413 |       labeled_statement
1414 |       if_then_statement
1415 |       if_then_else_statement
1416 |       while_statement
1417 |       for_statement
1418                 { $$ = exit_block (); }
1419 ;
1420
1421 statement_nsi:
1422         statement_without_trailing_substatement
1423 |       labeled_statement_nsi
1424 |       if_then_else_statement_nsi
1425 |       while_statement_nsi
1426 |       for_statement_nsi
1427                 { $$ = exit_block (); }
1428 ;
1429
1430 statement_without_trailing_substatement:
1431         block
1432 |       empty_statement
1433 |       expression_statement
1434 |       switch_statement
1435 |       do_statement
1436 |       break_statement
1437 |       continue_statement
1438 |       return_statement
1439 |       synchronized_statement
1440 |       throw_statement
1441 |       try_statement
1442 ;
1443
1444 empty_statement:
1445         SC_TK
1446                 { 
1447                   if (flag_extraneous_semicolon)
1448                     {
1449                       EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1450                       parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1451                     }
1452                   $$ = empty_stmt_node;
1453                 }
1454 ;
1455
1456 label_decl:
1457         identifier REL_CL_TK
1458                 {
1459                   $$ = build_labeled_block (EXPR_WFL_LINECOL ($1), 
1460                                             EXPR_WFL_NODE ($1));
1461                   pushlevel (2);
1462                   push_labeled_block ($$);
1463                   PUSH_LABELED_BLOCK ($$);
1464                 }
1465 ;
1466
1467 labeled_statement:
1468         label_decl statement
1469                 { $$ = finish_labeled_statement ($1, $2); }
1470 |       identifier error
1471                 {yyerror ("':' expected"); RECOVER;}
1472 ;
1473
1474 labeled_statement_nsi:
1475         label_decl statement_nsi
1476                 { $$ = finish_labeled_statement ($1, $2); }
1477 ;
1478
1479 /* We concentrate here a bunch of error handling rules that we couldn't write
1480    earlier, because expression_statement catches a missing ';'.  */
1481 expression_statement:
1482         statement_expression SC_TK
1483                 {
1484                   /* We have a statement. Generate a WFL around it so
1485                      we can debug it */
1486                   $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1487                   /* We know we have a statement, so set the debug
1488                      info to be eventually generate here. */
1489                   $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1490                 }
1491 |       error SC_TK 
1492                 {
1493                   YYNOT_TWICE yyerror ("Invalid expression statement");
1494                   DRECOVER (expr_stmt);
1495                 }
1496 |       error OCB_TK
1497                 {
1498                   YYNOT_TWICE yyerror ("Invalid expression statement");
1499                   DRECOVER (expr_stmt);
1500                 }
1501 |       error CCB_TK
1502                 {
1503                   YYNOT_TWICE yyerror ("Invalid expression statement");
1504                   DRECOVER (expr_stmt);
1505                 }
1506 |       this_or_super OP_TK error
1507                 {yyerror ("')' expected"); RECOVER;}
1508 |       this_or_super OP_TK CP_TK error
1509                 {
1510                   parse_ctor_invocation_error ();
1511                   RECOVER;
1512                 }
1513 |       this_or_super OP_TK argument_list error
1514                 {yyerror ("')' expected"); RECOVER;}
1515 |       this_or_super OP_TK argument_list CP_TK error
1516                 {
1517                   parse_ctor_invocation_error ();
1518                   RECOVER;
1519                 }
1520 |       name DOT_TK SUPER_TK error
1521                 {yyerror ("'(' expected"); RECOVER;}
1522 |       name DOT_TK SUPER_TK OP_TK error
1523                 {yyerror ("')' expected"); RECOVER;}
1524 |       name DOT_TK SUPER_TK OP_TK argument_list error
1525                 {yyerror ("')' expected"); RECOVER;}
1526 |       name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1527                 {yyerror ("';' expected"); RECOVER;}
1528 |       name DOT_TK SUPER_TK OP_TK CP_TK error
1529                 {yyerror ("';' expected"); RECOVER;}
1530 ;
1531
1532 statement_expression: 
1533         assignment
1534 |       pre_increment_expression
1535 |       pre_decrement_expression
1536 |       post_increment_expression
1537 |       post_decrement_expression
1538 |       method_invocation
1539 |       class_instance_creation_expression
1540 ;
1541
1542 if_then_statement:
1543         IF_TK OP_TK expression CP_TK statement
1544                 { 
1545                   $$ = build_if_else_statement ($2.location, $3, 
1546                                                 $5, NULL_TREE);
1547                 }
1548 |       IF_TK error
1549                 {yyerror ("'(' expected"); RECOVER;}
1550 |       IF_TK OP_TK error
1551                 {yyerror ("Missing term"); RECOVER;}
1552 |       IF_TK OP_TK expression error
1553                 {yyerror ("')' expected"); RECOVER;}
1554 ;
1555
1556 if_then_else_statement:
1557         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1558                 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1559 ;
1560
1561 if_then_else_statement_nsi:
1562         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1563                 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1564 ;
1565
1566 switch_statement:
1567         switch_expression
1568                 {
1569                   enter_block ();
1570                 }
1571         switch_block
1572                 { 
1573                   /* Make into "proper list" of COMPOUND_EXPRs.
1574                      I.e. make the last statment also have its own
1575                      COMPOUND_EXPR. */
1576                   maybe_absorb_scoping_blocks ();
1577                   TREE_OPERAND ($1, 1) = exit_block ();
1578                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1579                 }
1580 ;
1581
1582 switch_expression:
1583         SWITCH_TK OP_TK expression CP_TK
1584                 { 
1585                   $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1586                   EXPR_WFL_LINECOL ($$) = $2.location;
1587                 }
1588 |       SWITCH_TK error
1589                 {yyerror ("'(' expected"); RECOVER;}
1590 |       SWITCH_TK OP_TK error
1591                 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1592 |       SWITCH_TK OP_TK expression CP_TK error
1593                 {yyerror ("'{' expected"); RECOVER;}
1594 ;
1595
1596 /* Default assignment is there to avoid type node on switch_block
1597    node. */
1598
1599 switch_block:
1600         OCB_TK CCB_TK
1601                 { $$ = NULL_TREE; }
1602 |       OCB_TK switch_labels CCB_TK
1603                 { $$ = NULL_TREE; }
1604 |       OCB_TK switch_block_statement_groups CCB_TK
1605                 { $$ = NULL_TREE; }
1606 |       OCB_TK switch_block_statement_groups switch_labels CCB_TK
1607                 { $$ = NULL_TREE; }
1608 ;
1609
1610 switch_block_statement_groups: 
1611         switch_block_statement_group
1612 |       switch_block_statement_groups switch_block_statement_group
1613 ;
1614
1615 switch_block_statement_group:
1616         switch_labels block_statements
1617 ;
1618
1619 switch_labels:
1620         switch_label
1621 |       switch_labels switch_label
1622 ;
1623
1624 switch_label:
1625         CASE_TK constant_expression REL_CL_TK
1626                 { 
1627                   tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1628                   EXPR_WFL_LINECOL (lab) = $1.location;
1629                   java_method_add_stmt (current_function_decl, lab);
1630                 }
1631 |       DEFAULT_TK REL_CL_TK
1632                 { 
1633                   tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1634                   EXPR_WFL_LINECOL (lab) = $1.location;
1635                   java_method_add_stmt (current_function_decl, lab);
1636                 }
1637 |       CASE_TK error
1638                 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1639 |       CASE_TK constant_expression error
1640                 {yyerror ("':' expected"); RECOVER;}
1641 |       DEFAULT_TK error
1642                 {yyerror ("':' expected"); RECOVER;}
1643 ;
1644
1645 while_expression:
1646         WHILE_TK OP_TK expression CP_TK
1647                 { 
1648                   tree body = build_loop_body ($2.location, $3, 0);
1649                   $$ = build_new_loop (body);
1650                 }
1651 ;
1652
1653 while_statement:
1654         while_expression statement
1655                 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1656 |       WHILE_TK error
1657                 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1658 |       WHILE_TK OP_TK error
1659                 {yyerror ("Missing term and ')' expected"); RECOVER;}
1660 |       WHILE_TK OP_TK expression error
1661                 {yyerror ("')' expected"); RECOVER;}
1662 ;
1663
1664 while_statement_nsi:
1665         while_expression statement_nsi
1666                 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1667 ;
1668
1669 do_statement_begin:
1670         DO_TK
1671                 { 
1672                   tree body = build_loop_body (0, NULL_TREE, 1);
1673                   $$ = build_new_loop (body);
1674                 }
1675         /* Need error handing here. FIXME */
1676 ;
1677
1678 do_statement: 
1679         do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1680                 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1681 ;
1682
1683 for_statement:
1684         for_begin SC_TK expression SC_TK for_update CP_TK statement
1685                 {
1686                   if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1687                     $3 = build_wfl_node ($3);
1688                   $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1689                 }
1690 |       for_begin SC_TK SC_TK for_update CP_TK statement
1691                 { 
1692                   $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1693                   /* We have not condition, so we get rid of the EXIT_EXPR */
1694                   LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) = 
1695                     empty_stmt_node;
1696                 }
1697 |       for_begin SC_TK error
1698                 {yyerror ("Invalid control expression"); RECOVER;}
1699 |       for_begin SC_TK expression SC_TK error
1700                 {yyerror ("Invalid update expression"); RECOVER;}
1701 |       for_begin SC_TK SC_TK error
1702                 {yyerror ("Invalid update expression"); RECOVER;}
1703 ;
1704
1705 for_statement_nsi:
1706         for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1707                 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1708 |       for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1709                 { 
1710                   $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1711                   /* We have not condition, so we get rid of the EXIT_EXPR */
1712                   LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) = 
1713                     empty_stmt_node;
1714                 }
1715 ;
1716
1717 for_header:
1718         FOR_TK OP_TK
1719                 { 
1720                   /* This scope defined for local variable that may be
1721                      defined within the scope of the for loop */
1722                   enter_block (); 
1723                 }
1724 |       FOR_TK error
1725                 {yyerror ("'(' expected"); DRECOVER(for_1);}
1726 |       FOR_TK OP_TK error
1727                 {yyerror ("Invalid init statement"); RECOVER;}
1728 ;
1729
1730 for_begin:
1731         for_header for_init
1732                 { 
1733                   /* We now declare the loop body. The loop is
1734                      declared as a for loop. */
1735                   tree body = build_loop_body (0, NULL_TREE, 0);
1736                   $$ =  build_new_loop (body);
1737                   FOR_LOOP_P ($$) = 1;
1738                   /* The loop is added to the current block the for
1739                      statement is defined within */
1740                   java_method_add_stmt (current_function_decl, $$);
1741                 }
1742 ;
1743 for_init:                       /* Can be empty */
1744                 { $$ = empty_stmt_node; }
1745 |       statement_expression_list
1746                 { 
1747                   /* Init statement recorded within the previously
1748                      defined block scope */
1749                   $$ = java_method_add_stmt (current_function_decl, $1);
1750                 }
1751 |       local_variable_declaration
1752                 { 
1753                   /* Local variable are recorded within the previously
1754                      defined block scope */
1755                   $$ = NULL_TREE;
1756                 }
1757 |       statement_expression_list error
1758                 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1759 ;
1760
1761 for_update:                     /* Can be empty */
1762                 {$$ = empty_stmt_node;}
1763 |       statement_expression_list
1764                 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1765 ;
1766
1767 statement_expression_list:
1768         statement_expression
1769                 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1770 |       statement_expression_list C_TK statement_expression
1771                 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1772 |       statement_expression_list C_TK error
1773                 {yyerror ("Missing term"); RECOVER;}
1774 ;
1775
1776 break_statement:
1777         BREAK_TK SC_TK
1778                 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1779 |       BREAK_TK identifier SC_TK
1780                 { $$ = build_bc_statement ($1.location, 1, $2); }
1781 |       BREAK_TK error
1782                 {yyerror ("Missing term"); RECOVER;}
1783 |       BREAK_TK identifier error
1784                 {yyerror ("';' expected"); RECOVER;}
1785 ;
1786
1787 continue_statement:
1788         CONTINUE_TK SC_TK
1789                 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1790 |       CONTINUE_TK identifier SC_TK
1791                 { $$ = build_bc_statement ($1.location, 0, $2); }
1792 |       CONTINUE_TK error
1793                 {yyerror ("Missing term"); RECOVER;}
1794 |       CONTINUE_TK identifier error
1795                 {yyerror ("';' expected"); RECOVER;}
1796 ;
1797
1798 return_statement:
1799         RETURN_TK SC_TK
1800                 { $$ = build_return ($1.location, NULL_TREE); }
1801 |       RETURN_TK expression SC_TK
1802                 { $$ = build_return ($1.location, $2); }
1803 |       RETURN_TK error
1804                 {yyerror ("Missing term"); RECOVER;}
1805 |       RETURN_TK expression error
1806                 {yyerror ("';' expected"); RECOVER;}
1807 ;
1808
1809 throw_statement:
1810         THROW_TK expression SC_TK
1811                 { 
1812                   $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1813                   EXPR_WFL_LINECOL ($$) = $1.location;
1814                 }
1815 |       THROW_TK error
1816                 {yyerror ("Missing term"); RECOVER;}
1817 |       THROW_TK expression error
1818                 {yyerror ("';' expected"); RECOVER;}
1819 ;
1820
1821 synchronized_statement:
1822         synchronized OP_TK expression CP_TK block
1823                 { 
1824                   $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1825                   EXPR_WFL_LINECOL ($$) = 
1826                     EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1827                 }
1828 |       synchronized OP_TK expression CP_TK error
1829                 {yyerror ("'{' expected"); RECOVER;}
1830 |       synchronized error
1831                 {yyerror ("'(' expected"); RECOVER;}
1832 |       synchronized OP_TK error CP_TK
1833                 {yyerror ("Missing term"); RECOVER;}
1834 |       synchronized OP_TK error
1835                 {yyerror ("Missing term"); RECOVER;}
1836 ;
1837
1838 synchronized:
1839         modifiers
1840                 {
1841                   check_modifiers (
1842              "Illegal modifier `%s'. Only `synchronized' was expected here",
1843                                    $1, ACC_SYNCHRONIZED);
1844                   if ($1 != ACC_SYNCHRONIZED)
1845                     MODIFIER_WFL (SYNCHRONIZED_TK) = 
1846                       build_wfl_node (NULL_TREE);
1847                 }
1848 ;
1849
1850 try_statement:
1851         TRY_TK block catches
1852                 { $$ = build_try_statement ($1.location, $2, $3); }
1853 |       TRY_TK block finally
1854                 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1855 |       TRY_TK block catches finally
1856                 { $$ = build_try_finally_statement 
1857                     ($1.location, build_try_statement ($1.location,
1858                                                        $2, $3), $4);
1859                 }
1860 |       TRY_TK error
1861                 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1862 ;
1863
1864 catches:
1865         catch_clause
1866 |       catches catch_clause
1867                 { 
1868                   TREE_CHAIN ($2) = $1;
1869                   $$ = $2;
1870                 }
1871 ;
1872
1873 catch_clause:
1874         catch_clause_parameter block
1875                 { 
1876                   java_method_add_stmt (current_function_decl, $2);
1877                   exit_block ();
1878                   $$ = $1;
1879                 }
1880
1881 catch_clause_parameter:
1882         CATCH_TK OP_TK formal_parameter CP_TK
1883                 { 
1884                   /* We add a block to define a scope for
1885                      formal_parameter (CCBP). The formal parameter is
1886                      declared initialized by the appropriate function
1887                      call */
1888                   tree ccpb = enter_block ();
1889                   tree init = build_assignment
1890                     (ASSIGN_TK, $2.location, TREE_PURPOSE ($3), 
1891                      build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1892                   declare_local_variables (0, TREE_VALUE ($3),
1893                                            build_tree_list (TREE_PURPOSE ($3),
1894                                                             init));
1895                   $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1896                   EXPR_WFL_LINECOL ($$) = $1.location;
1897                 }
1898 |       CATCH_TK error
1899                 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1900 |       CATCH_TK OP_TK error 
1901                 {
1902                   yyerror ("Missing term or ')' expected"); 
1903                   RECOVER; $$ = NULL_TREE;
1904                 }
1905 |       CATCH_TK OP_TK error CP_TK /* That's for () */
1906                 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1907 ;
1908
1909 finally:
1910         FINALLY_TK block
1911                 { $$ = $2; }
1912 |       FINALLY_TK error
1913                 {yyerror ("'{' expected"); RECOVER; }
1914 ;
1915
1916 /* 19.12 Production from 15: Expressions  */
1917 primary:
1918         primary_no_new_array
1919 |       array_creation_expression
1920 ;
1921
1922 primary_no_new_array:
1923         literal
1924 |       THIS_TK
1925                 { $$ = build_this ($1.location); }
1926 |       OP_TK expression CP_TK
1927                 {$$ = $2;}
1928 |       class_instance_creation_expression
1929 |       field_access
1930 |       method_invocation
1931 |       array_access
1932 |       type_literals
1933         /* Added, JDK1.1 inner classes. Documentation is wrong
1934            refering to a 'ClassName' (class_name) rule that doesn't
1935            exist. Used name: instead.  */
1936 |       name DOT_TK THIS_TK
1937                 { 
1938                   tree wfl = build_wfl_node (this_identifier_node);
1939                   $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1940                 }
1941 |       OP_TK expression error 
1942                 {yyerror ("')' expected"); RECOVER;}
1943 |       name DOT_TK error
1944                 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1945 |       primitive_type DOT_TK error
1946                 {yyerror ("'class' expected" ); RECOVER;}
1947 |       VOID_TK DOT_TK error
1948                 {yyerror ("'class' expected" ); RECOVER;}
1949 ;
1950
1951 type_literals:
1952         name DOT_TK CLASS_TK
1953                 { $$ = build_incomplete_class_ref ($2.location, $1); }
1954 |       array_type DOT_TK CLASS_TK
1955                 { $$ = build_incomplete_class_ref ($2.location, $1); }
1956 |       primitive_type DOT_TK CLASS_TK
1957                 { $$ = build_class_ref ($1); }
1958 |       VOID_TK DOT_TK CLASS_TK
1959                 { $$ = build_class_ref (void_type_node); }
1960 ;
1961
1962 class_instance_creation_expression:
1963         NEW_TK class_type OP_TK argument_list CP_TK
1964                 { $$ = build_new_invocation ($2, $4); }
1965 |       NEW_TK class_type OP_TK CP_TK
1966                 { $$ = build_new_invocation ($2, NULL_TREE); }
1967 |       anonymous_class_creation
1968         /* Added, JDK1.1 inner classes, modified to use name or
1969            primary instead of primary solely which couldn't work in
1970            all situations.  */
1971 |       something_dot_new identifier OP_TK CP_TK
1972                 { 
1973                   tree ctor = build_new_invocation ($2, NULL_TREE);
1974                   $$ = make_qualified_primary ($1, ctor, 
1975                                                EXPR_WFL_LINECOL ($1));
1976                 }
1977 |       something_dot_new identifier OP_TK CP_TK class_body
1978 |       something_dot_new identifier OP_TK argument_list CP_TK
1979                 { 
1980                   tree ctor = build_new_invocation ($2, $4);
1981                   $$ = make_qualified_primary ($1, ctor, 
1982                                                EXPR_WFL_LINECOL ($1));
1983                 }
1984 |       something_dot_new identifier OP_TK argument_list CP_TK class_body
1985 |       NEW_TK error SC_TK 
1986                 {yyerror ("'(' expected"); DRECOVER(new_1);}
1987 |       NEW_TK class_type error
1988                 {yyerror ("'(' expected"); RECOVER;}
1989 |       NEW_TK class_type OP_TK error
1990                 {yyerror ("')' or term expected"); RECOVER;}
1991 |       NEW_TK class_type OP_TK argument_list error
1992                 {yyerror ("')' expected"); RECOVER;}
1993 |       something_dot_new error
1994                 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1995 |       something_dot_new identifier error
1996                 {yyerror ("'(' expected"); RECOVER;}
1997 ;
1998
1999 /* Created after JDK1.1 rules originally added to
2000    class_instance_creation_expression, but modified to use
2001    'class_type' instead of 'TypeName' (type_name) which is mentionned
2002    in the documentation but doesn't exist. */
2003
2004 anonymous_class_creation:
2005         NEW_TK class_type OP_TK argument_list CP_TK 
2006                 { create_anonymous_class ($1.location, $2); }
2007         class_body
2008                 { 
2009                   tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2010                   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2011
2012                   end_class_declaration (1);
2013
2014                   /* Now we can craft the new expression */
2015                   $$ = build_new_invocation (id, $4);
2016
2017                   /* Note that we can't possibly be here if
2018                      `class_type' is an interface (in which case the
2019                      anonymous class extends Object and implements
2020                      `class_type', hence its constructor can't have
2021                      arguments.) */
2022
2023                   /* Otherwise, the innerclass must feature a
2024                      constructor matching `argument_list'. Anonymous
2025                      classes are a bit special: it's impossible to
2026                      define constructor for them, hence constructors
2027                      must be generated following the hints provided by
2028                      the `new' expression. Whether a super constructor
2029                      of that nature exists or not is to be verified
2030                      later on in verify_constructor_super. 
2031
2032                      It's during the expansion of a `new' statement
2033                      refering to an anonymous class that a ctor will
2034                      be generated for the anonymous class, with the
2035                      right arguments. */
2036
2037                 }
2038 |       NEW_TK class_type OP_TK CP_TK 
2039                 { create_anonymous_class ($1.location, $2); }
2040         class_body         
2041                 { 
2042                   tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2043                   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2044
2045                   end_class_declaration (1);
2046
2047                   /* Now we can craft the new expression. The
2048                      statement doesn't need to be remember so that a
2049                      constructor can be generated, since its signature
2050                      is already known. */
2051                   $$ = build_new_invocation (id, NULL_TREE);
2052                 }
2053 ;
2054
2055 something_dot_new:              /* Added, not part of the specs. */
2056         name DOT_TK NEW_TK
2057                 { $$ = $1; }
2058 |       primary DOT_TK NEW_TK
2059                 { $$ = $1; }
2060 ;
2061
2062 argument_list:
2063         expression
2064                 { 
2065                   $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2066                   ctxp->formal_parameter_number = 1; 
2067                 }
2068 |       argument_list C_TK expression
2069                 {
2070                   ctxp->formal_parameter_number += 1;
2071                   $$ = tree_cons (NULL_TREE, $3, $1);
2072                 }
2073 |       argument_list C_TK error
2074                 {yyerror ("Missing term"); RECOVER;}
2075 ;
2076
2077 array_creation_expression:
2078         NEW_TK primitive_type dim_exprs
2079                 { $$ = build_newarray_node ($2, $3, 0); }
2080 |       NEW_TK class_or_interface_type dim_exprs
2081                 { $$ = build_newarray_node ($2, $3, 0); }
2082 |       NEW_TK primitive_type dim_exprs dims
2083                 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2084 |       NEW_TK class_or_interface_type dim_exprs dims
2085                 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2086         /* Added, JDK1.1 anonymous array. Initial documentation rule
2087            modified */
2088 |       NEW_TK class_or_interface_type dims array_initializer
2089                 {
2090                   char *sig;
2091                   int osb = pop_current_osb (ctxp);
2092                   while (osb--)
2093                     obstack_1grow (&temporary_obstack, '[');
2094                   sig = obstack_finish (&temporary_obstack);
2095                   $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2096                               $2, get_identifier (sig), $4);
2097                 }
2098 |       NEW_TK primitive_type dims array_initializer
2099                 { 
2100                   int osb = pop_current_osb (ctxp);
2101                   tree type = $2;
2102                   while (osb--)
2103                     type = build_java_array_type (type, -1);
2104                   $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE, 
2105                               build_pointer_type (type), NULL_TREE, $4);
2106                 }
2107 |       NEW_TK error CSB_TK
2108                 {yyerror ("'[' expected"); DRECOVER ("]");}
2109 |       NEW_TK error OSB_TK
2110                 {yyerror ("']' expected"); RECOVER;}
2111 ;
2112
2113 dim_exprs:
2114         dim_expr
2115                 { $$ = build_tree_list (NULL_TREE, $1); }
2116 |       dim_exprs dim_expr
2117                 { $$ = tree_cons (NULL_TREE, $2, $$); }
2118 ;
2119
2120 dim_expr:
2121         OSB_TK expression CSB_TK
2122                 { 
2123                   if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2124                     {
2125                       $2 = build_wfl_node ($2);
2126                       TREE_TYPE ($2) = NULL_TREE;
2127                     }
2128                   EXPR_WFL_LINECOL ($2) = $1.location;
2129                   $$ = $2;
2130                 }
2131 |       OSB_TK expression error
2132                 {yyerror ("']' expected"); RECOVER;}
2133 |       OSB_TK error
2134                 {
2135                   yyerror ("Missing term");
2136                   yyerror ("']' expected");
2137                   RECOVER;
2138                 }
2139 ;
2140
2141 dims:                           
2142         OSB_TK CSB_TK
2143                 { 
2144                   int allocate = 0;
2145                   /* If not initialized, allocate memory for the osb
2146                      numbers stack */
2147                   if (!ctxp->osb_limit)
2148                     {
2149                       allocate = ctxp->osb_limit = 32;
2150                       ctxp->osb_depth = -1;
2151                     }
2152                   /* If capacity overflown, reallocate a bigger chunk */
2153                   else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2154                     allocate = ctxp->osb_limit << 1;
2155                   
2156                   if (allocate)
2157                     {
2158                       allocate *= sizeof (int);
2159                       if (ctxp->osb_number)
2160                         ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2161                                                             allocate);
2162                       else
2163                         ctxp->osb_number = (int *)xmalloc (allocate);
2164                     }
2165                   ctxp->osb_depth++;
2166                   CURRENT_OSB (ctxp) = 1;
2167                 }
2168 |       dims OSB_TK CSB_TK
2169                 { CURRENT_OSB (ctxp)++; }
2170 |       dims OSB_TK error
2171                 { yyerror ("']' expected"); RECOVER;}
2172 ;
2173
2174 field_access:
2175         primary DOT_TK identifier
2176                 { $$ = make_qualified_primary ($1, $3, $2.location); }
2177                 /*  FIXME - REWRITE TO: 
2178                 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2179 |       SUPER_TK DOT_TK identifier
2180                 {
2181                   tree super_wfl = build_wfl_node (super_identifier_node);
2182                   EXPR_WFL_LINECOL (super_wfl) = $1.location;
2183                   $$ = make_qualified_name (super_wfl, $3, $2.location);
2184                 }
2185 |       SUPER_TK error
2186                 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2187 ;
2188
2189 method_invocation:
2190         name OP_TK CP_TK
2191                 { $$ = build_method_invocation ($1, NULL_TREE); }
2192 |       name OP_TK argument_list CP_TK
2193                 { $$ = build_method_invocation ($1, $3); }
2194 |       primary DOT_TK identifier OP_TK CP_TK
2195                 { 
2196                   if (TREE_CODE ($1) == THIS_EXPR)
2197                     $$ = build_this_super_qualified_invocation 
2198                       (1, $3, NULL_TREE, 0, $2.location);
2199                   else
2200                     {
2201                       tree invok = build_method_invocation ($3, NULL_TREE);
2202                       $$ = make_qualified_primary ($1, invok, $2.location);
2203                     }
2204                 }
2205 |       primary DOT_TK identifier OP_TK argument_list CP_TK
2206                 { 
2207                   if (TREE_CODE ($1) == THIS_EXPR)
2208                     $$ = build_this_super_qualified_invocation 
2209                       (1, $3, $5, 0, $2.location);
2210                   else
2211                     {
2212                       tree invok = build_method_invocation ($3, $5);
2213                       $$ = make_qualified_primary ($1, invok, $2.location);
2214                     }
2215                 }
2216 |       SUPER_TK DOT_TK identifier OP_TK CP_TK
2217                 { 
2218                   $$ = build_this_super_qualified_invocation 
2219                     (0, $3, NULL_TREE, $1.location, $2.location);
2220                 }
2221 |       SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2222                 {
2223                   $$ = build_this_super_qualified_invocation 
2224                     (0, $3, $5, $1.location, $2.location);
2225                 }
2226         /* Screws up thing. I let it here until I'm convinced it can
2227            be removed. FIXME
2228 |       primary DOT_TK error
2229                 {yyerror ("'(' expected"); DRECOVER(bad);} */
2230 |       SUPER_TK DOT_TK error CP_TK
2231                 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2232 |       SUPER_TK DOT_TK error DOT_TK
2233                 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2234 ;
2235
2236 array_access:
2237         name OSB_TK expression CSB_TK
2238                 { $$ = build_array_ref ($2.location, $1, $3); }
2239 |       primary_no_new_array OSB_TK expression CSB_TK
2240                 { $$ = build_array_ref ($2.location, $1, $3); }
2241 |       name OSB_TK error
2242                 {
2243                   yyerror ("Missing term and ']' expected");
2244                   DRECOVER(array_access);
2245                 }
2246 |       name OSB_TK expression error
2247                 {
2248                   yyerror ("']' expected");
2249                   DRECOVER(array_access);
2250                 }
2251 |       primary_no_new_array OSB_TK error
2252                 {
2253                   yyerror ("Missing term and ']' expected");
2254                   DRECOVER(array_access);
2255                 }
2256 |       primary_no_new_array OSB_TK expression error
2257                 {
2258                   yyerror ("']' expected");
2259                   DRECOVER(array_access);
2260                 }
2261 ;
2262
2263 postfix_expression:
2264         primary
2265 |       name
2266 |       post_increment_expression
2267 |       post_decrement_expression
2268 ;
2269
2270 post_increment_expression:
2271         postfix_expression INCR_TK
2272                 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2273 ;
2274
2275 post_decrement_expression:
2276         postfix_expression DECR_TK
2277                 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2278 ;
2279
2280 unary_expression:
2281         pre_increment_expression
2282 |       pre_decrement_expression
2283 |       PLUS_TK unary_expression
2284                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2285 |       MINUS_TK unary_expression
2286                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2287 |       unary_expression_not_plus_minus
2288 |       PLUS_TK error
2289                 {yyerror ("Missing term"); RECOVER}
2290 |       MINUS_TK error
2291                 {yyerror ("Missing term"); RECOVER}
2292 ;
2293
2294 pre_increment_expression:
2295         INCR_TK unary_expression
2296                 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2297 |       INCR_TK error
2298                 {yyerror ("Missing term"); RECOVER}
2299 ;
2300
2301 pre_decrement_expression:
2302         DECR_TK unary_expression
2303                 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2304 |       DECR_TK error
2305                 {yyerror ("Missing term"); RECOVER}
2306 ;
2307
2308 unary_expression_not_plus_minus:
2309         postfix_expression
2310 |       NOT_TK unary_expression
2311                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2312 |       NEG_TK unary_expression
2313                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2314 |       cast_expression
2315 |       NOT_TK error
2316                 {yyerror ("Missing term"); RECOVER}
2317 |       NEG_TK error
2318                 {yyerror ("Missing term"); RECOVER}
2319 ;
2320
2321 cast_expression:                /* Error handling here is potentially weak */
2322         OP_TK primitive_type dims CP_TK unary_expression
2323                 { 
2324                   tree type = $2;
2325                   int osb = pop_current_osb (ctxp);
2326                   while (osb--)
2327                     type = build_java_array_type (type, -1);
2328                   $$ = build_cast ($1.location, type, $5); 
2329                 }
2330 |       OP_TK primitive_type CP_TK unary_expression
2331                 { $$ = build_cast ($1.location, $2, $4); }
2332 |       OP_TK expression CP_TK unary_expression_not_plus_minus
2333                 { $$ = build_cast ($1.location, $2, $4); }
2334 |       OP_TK name dims CP_TK unary_expression_not_plus_minus
2335                 { 
2336                   const char *ptr;
2337                   int osb = pop_current_osb (ctxp); 
2338                   while (osb--)
2339                     obstack_1grow (&temporary_obstack, '[');
2340                   obstack_grow0 (&temporary_obstack, 
2341                                  IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2342                                  IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2343                   ptr = obstack_finish (&temporary_obstack);
2344                   EXPR_WFL_NODE ($2) = get_identifier (ptr);
2345                   $$ = build_cast ($1.location, $2, $5);
2346                 }
2347 |       OP_TK primitive_type OSB_TK error
2348                 {yyerror ("']' expected, invalid type expression");}
2349 |       OP_TK error
2350                 {
2351                   YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2352                   RECOVER;
2353                 }
2354 |       OP_TK primitive_type dims CP_TK error
2355                 {yyerror ("Missing term"); RECOVER;}
2356 |       OP_TK primitive_type CP_TK error
2357                 {yyerror ("Missing term"); RECOVER;}
2358 |       OP_TK name dims CP_TK error
2359                 {yyerror ("Missing term"); RECOVER;}
2360 ;
2361
2362 multiplicative_expression:
2363         unary_expression
2364 |       multiplicative_expression MULT_TK unary_expression
2365                 { 
2366                   $$ = build_binop (BINOP_LOOKUP ($2.token), 
2367                                     $2.location, $1, $3);
2368                 }
2369 |       multiplicative_expression DIV_TK unary_expression
2370                 {
2371                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2372                                     $1, $3); 
2373                 }
2374 |       multiplicative_expression REM_TK unary_expression
2375                 {
2376                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2377                                     $1, $3); 
2378                 }
2379 |       multiplicative_expression MULT_TK error
2380                 {yyerror ("Missing term"); RECOVER;}
2381 |       multiplicative_expression DIV_TK error
2382                 {yyerror ("Missing term"); RECOVER;}
2383 |       multiplicative_expression REM_TK error
2384                 {yyerror ("Missing term"); RECOVER;}
2385 ;
2386
2387 additive_expression:
2388         multiplicative_expression
2389 |       additive_expression PLUS_TK multiplicative_expression
2390                 {
2391                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2392                                     $1, $3); 
2393                 }
2394 |       additive_expression MINUS_TK multiplicative_expression
2395                 {
2396                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2397                                     $1, $3); 
2398                 }
2399 |       additive_expression PLUS_TK error
2400                 {yyerror ("Missing term"); RECOVER;}
2401 |       additive_expression MINUS_TK error
2402                 {yyerror ("Missing term"); RECOVER;}
2403 ;
2404
2405 shift_expression:
2406         additive_expression
2407 |       shift_expression LS_TK additive_expression
2408                 {
2409                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2410                                     $1, $3); 
2411                 }
2412 |       shift_expression SRS_TK additive_expression
2413                 {
2414                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2415                                     $1, $3); 
2416                 }
2417 |       shift_expression ZRS_TK additive_expression
2418                 {
2419                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2420                                     $1, $3); 
2421                 }
2422 |       shift_expression LS_TK error
2423                 {yyerror ("Missing term"); RECOVER;}
2424 |       shift_expression SRS_TK error
2425                 {yyerror ("Missing term"); RECOVER;}
2426 |       shift_expression ZRS_TK error
2427                 {yyerror ("Missing term"); RECOVER;}
2428 ;
2429
2430 relational_expression:
2431         shift_expression
2432 |       relational_expression LT_TK shift_expression
2433                 {
2434                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2435                                     $1, $3); 
2436                 }
2437 |       relational_expression GT_TK shift_expression
2438                 {
2439                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2440                                     $1, $3); 
2441                 }
2442 |       relational_expression LTE_TK shift_expression
2443                 {
2444                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2445                                     $1, $3); 
2446                 }
2447 |       relational_expression GTE_TK shift_expression
2448                 {
2449                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2450                                     $1, $3); 
2451                 }
2452 |       relational_expression INSTANCEOF_TK reference_type
2453                 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2454 |       relational_expression LT_TK error
2455                 {yyerror ("Missing term"); RECOVER;}
2456 |       relational_expression GT_TK error
2457                 {yyerror ("Missing term"); RECOVER;}
2458 |       relational_expression LTE_TK error
2459                 {yyerror ("Missing term"); RECOVER;}
2460 |       relational_expression GTE_TK error
2461                 {yyerror ("Missing term"); RECOVER;}
2462 |       relational_expression INSTANCEOF_TK error
2463                 {yyerror ("Invalid reference type"); RECOVER;}
2464 ;
2465
2466 equality_expression:
2467         relational_expression
2468 |       equality_expression EQ_TK relational_expression
2469                 {
2470                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2471                                     $1, $3); 
2472                 }
2473 |       equality_expression NEQ_TK relational_expression
2474                 {
2475                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2476                                     $1, $3); 
2477                 }
2478 |       equality_expression EQ_TK error
2479                 {yyerror ("Missing term"); RECOVER;}
2480 |       equality_expression NEQ_TK error
2481                 {yyerror ("Missing term"); RECOVER;}
2482 ;
2483
2484 and_expression:
2485         equality_expression
2486 |       and_expression AND_TK equality_expression
2487                 {
2488                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2489                                     $1, $3); 
2490                 }
2491 |       and_expression AND_TK error
2492                 {yyerror ("Missing term"); RECOVER;}
2493 ;
2494
2495 exclusive_or_expression:
2496         and_expression
2497 |       exclusive_or_expression XOR_TK and_expression
2498                 {
2499                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2500                                     $1, $3); 
2501                 }
2502 |       exclusive_or_expression XOR_TK error
2503                 {yyerror ("Missing term"); RECOVER;}
2504 ;
2505
2506 inclusive_or_expression:
2507         exclusive_or_expression
2508 |       inclusive_or_expression OR_TK exclusive_or_expression
2509                 {
2510                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2511                                     $1, $3); 
2512                 }
2513 |       inclusive_or_expression OR_TK error
2514                 {yyerror ("Missing term"); RECOVER;}
2515 ;
2516
2517 conditional_and_expression:
2518         inclusive_or_expression
2519 |       conditional_and_expression BOOL_AND_TK inclusive_or_expression
2520                 {
2521                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2522                                     $1, $3); 
2523                 }
2524 |       conditional_and_expression BOOL_AND_TK error
2525                 {yyerror ("Missing term"); RECOVER;}
2526 ;
2527
2528 conditional_or_expression:
2529         conditional_and_expression
2530 |       conditional_or_expression BOOL_OR_TK conditional_and_expression
2531                 {
2532                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2533                                     $1, $3); 
2534                 }
2535 |       conditional_or_expression BOOL_OR_TK error
2536                 {yyerror ("Missing term"); RECOVER;}
2537 ;
2538
2539 conditional_expression:         /* Error handling here is weak */
2540         conditional_or_expression
2541 |       conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2542                 {
2543                   $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2544                   EXPR_WFL_LINECOL ($$) = $2.location;
2545                 }
2546 |       conditional_or_expression REL_QM_TK REL_CL_TK error
2547                 {
2548                   YYERROR_NOW;
2549                   yyerror ("Missing term");
2550                   DRECOVER (1);
2551                 }
2552 |       conditional_or_expression REL_QM_TK error
2553                 {yyerror ("Missing term"); DRECOVER (2);}
2554 |       conditional_or_expression REL_QM_TK expression REL_CL_TK error
2555                 {yyerror ("Missing term"); DRECOVER (3);}
2556 ;
2557
2558 assignment_expression:
2559         conditional_expression
2560 |       assignment
2561 ;
2562
2563 assignment:
2564         left_hand_side assignment_operator assignment_expression
2565                 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2566 |       left_hand_side assignment_operator error
2567                 {
2568                   YYNOT_TWICE yyerror ("Missing term");
2569                   DRECOVER (assign);
2570                 }
2571 ;
2572
2573 left_hand_side:
2574         name
2575 |       field_access
2576 |       array_access
2577 ;
2578
2579 assignment_operator:
2580         ASSIGN_ANY_TK
2581 |       ASSIGN_TK
2582 ;
2583
2584 expression:
2585         assignment_expression
2586 ;
2587
2588 constant_expression:
2589         expression
2590 ;
2591
2592 %%
2593
2594 /* Helper function to retrieve an OSB count. Should be used when the
2595    `dims:' rule is being used.  */
2596
2597 static int
2598 pop_current_osb (ctxp)
2599      struct parser_ctxt *ctxp;
2600 {
2601   int to_return;
2602
2603   if (ctxp->osb_depth < 0)
2604     abort ();
2605   
2606   to_return = CURRENT_OSB (ctxp);
2607   ctxp->osb_depth--;
2608   
2609   return to_return;
2610 }
2611
2612 \f
2613
2614 /* This section of the code deal with save/restoring parser contexts.
2615    Add mode documentation here. FIXME */
2616
2617 /* Helper function. Create a new parser context. With
2618    COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2619    context is copied, otherwise, the new context is zeroed. The newly
2620    created context becomes the current one.  */
2621
2622 static void
2623 create_new_parser_context (copy_from_previous)
2624     int copy_from_previous;
2625 {
2626   struct parser_ctxt *new;
2627
2628   new =  (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2629   if (copy_from_previous)
2630     {
2631       memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2632       new->saved_data_ctx = 1;
2633     }
2634   else
2635     memset ((PTR) new, 0, sizeof (struct parser_ctxt));
2636       
2637   new->next = ctxp;
2638   ctxp = new;
2639 }
2640
2641 /* Create a new parser context and make it the current one. */
2642
2643 void
2644 java_push_parser_context ()
2645 {
2646   create_new_parser_context (0);
2647 }  
2648
2649 void 
2650 java_pop_parser_context (generate)
2651      int generate;
2652 {
2653   tree current;
2654   struct parser_ctxt *toFree, *next;
2655
2656   if (!ctxp)
2657     return;
2658
2659   toFree = ctxp;
2660   next = ctxp->next;
2661   if (next)
2662     {
2663       lineno = ctxp->lineno;
2664       current_class = ctxp->class_type;
2665     }
2666
2667   /* If the old and new lexers differ, then free the old one.  */
2668   if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2669     java_destroy_lexer (ctxp->lexer);
2670
2671   /* Set the single import class file flag to 0 for the current list
2672      of imported things */
2673   for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2674     IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2675
2676   /* And restore those of the previous context */
2677   if ((ctxp = next))            /* Assignment is really meant here */
2678     for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2679       IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2680   
2681   /* If we pushed a context to parse a class intended to be generated,
2682      we keep it so we can remember the class. What we could actually
2683      do is to just update a list of class names.  */
2684   if (generate)
2685     {
2686       toFree->next = ctxp_for_generation;
2687       ctxp_for_generation = toFree;
2688     }
2689   else
2690     free (toFree);
2691 }
2692
2693 /* Create a parser context for the use of saving some global
2694    variables.  */
2695
2696 void
2697 java_parser_context_save_global ()
2698 {
2699   if (!ctxp)
2700     {
2701       java_push_parser_context ();
2702       ctxp->saved_data_ctx = 1;
2703     }
2704
2705   /* If this context already stores data, create a new one suitable
2706      for data storage. */
2707   else if (ctxp->saved_data)
2708     create_new_parser_context (1);
2709
2710   ctxp->lineno = lineno;
2711   ctxp->class_type = current_class;
2712   ctxp->filename = input_filename;
2713   ctxp->function_decl = current_function_decl;
2714   ctxp->saved_data = 1;
2715 }
2716
2717 /* Restore some global variables from the previous context. Make the
2718    previous context the current one.  */
2719
2720 void
2721 java_parser_context_restore_global ()
2722 {
2723   lineno = ctxp->lineno;
2724   current_class = ctxp->class_type;
2725   input_filename = ctxp->filename;
2726   if (wfl_operator)
2727     {
2728       tree s;
2729       BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2730       EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2731     }
2732   current_function_decl = ctxp->function_decl;
2733   ctxp->saved_data = 0;
2734   if (ctxp->saved_data_ctx)
2735     java_pop_parser_context (0);
2736 }
2737
2738 /* Suspend vital data for the current class/function being parsed so
2739    that an other class can be parsed. Used to let local/anonymous
2740    classes be parsed.  */
2741
2742 static void
2743 java_parser_context_suspend ()
2744 {
2745   /* This makes debugging through java_debug_context easier */
2746   static const char *name = "<inner buffer context>";
2747
2748   /* Duplicate the previous context, use it to save the globals we're
2749      interested in */
2750   create_new_parser_context (1);
2751   ctxp->function_decl = current_function_decl;
2752   ctxp->class_type = current_class;
2753
2754   /* Then create a new context which inherits all data from the
2755      previous one. This will be the new current context  */
2756   create_new_parser_context (1);
2757
2758   /* Help debugging */
2759   ctxp->next->filename = name;
2760 }
2761
2762 /* Resume vital data for the current class/function being parsed so
2763    that an other class can be parsed. Used to let local/anonymous
2764    classes be parsed.  The trick is the data storing file position
2765    informations must be restored to their current value, so parsing
2766    can resume as if no context was ever saved. */
2767
2768 static void
2769 java_parser_context_resume ()
2770 {
2771   struct parser_ctxt *old = ctxp;             /* This one is to be discarded */
2772   struct parser_ctxt *saver = old->next;      /* This one contain saved info */
2773   struct parser_ctxt *restored = saver->next; /* This one is the old current */
2774
2775   /* We need to inherit the list of classes to complete/generate */
2776   restored->classd_list = old->classd_list;
2777   restored->class_list = old->class_list;
2778
2779   /* Restore the current class and function from the saver */
2780   current_class = saver->class_type;
2781   current_function_decl = saver->function_decl;
2782
2783   /* Retrive the restored context */
2784   ctxp = restored;
2785
2786   /* Re-installed the data for the parsing to carry on */
2787   memcpy (&ctxp->marker_begining, &old->marker_begining, 
2788           (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2789
2790   /* Buffer context can now be discarded */
2791   free (saver);
2792   free (old);
2793 }
2794
2795 /* Add a new anchor node to which all statement(s) initializing static
2796    and non static initialized upon declaration field(s) will be
2797    linked.  */
2798
2799 static void
2800 java_parser_context_push_initialized_field ()
2801 {
2802   tree node;
2803
2804   node = build_tree_list (NULL_TREE, NULL_TREE);
2805   TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2806   CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2807
2808   node = build_tree_list (NULL_TREE, NULL_TREE);
2809   TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2810   CPC_INITIALIZER_LIST (ctxp) = node;
2811
2812   node = build_tree_list (NULL_TREE, NULL_TREE);
2813   TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2814   CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2815 }
2816
2817 /* Pop the lists of initialized field. If this lists aren't empty,
2818    remember them so we can use it to create and populate the finit$
2819    or <clinit> functions. */
2820
2821 static void
2822 java_parser_context_pop_initialized_field ()
2823 {
2824   tree stmts;
2825   tree class_type = TREE_TYPE (GET_CPC ());
2826
2827   if (CPC_INITIALIZER_LIST (ctxp))
2828     {
2829       stmts = CPC_INITIALIZER_STMT (ctxp);
2830       CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2831       if (stmts && !java_error_count)
2832         TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2833     }
2834
2835   if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2836     {
2837       stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2838       CPC_STATIC_INITIALIZER_LIST (ctxp) = 
2839         TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2840       /* Keep initialization in order to enforce 8.5 */
2841       if (stmts && !java_error_count)
2842         TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2843     }
2844
2845   /* JDK 1.1 instance initializers */
2846   if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2847     {
2848       stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2849       CPC_INSTANCE_INITIALIZER_LIST (ctxp) = 
2850         TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2851       if (stmts && !java_error_count)
2852         TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2853     }
2854 }
2855
2856 static tree
2857 reorder_static_initialized (list)
2858      tree list;
2859 {
2860   /* We have to keep things in order. The alias initializer have to
2861      come first, then the initialized regular field, in reverse to
2862      keep them in lexical order. */
2863   tree marker, previous = NULL_TREE;
2864   for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2865     if (TREE_CODE (marker) == TREE_LIST 
2866         && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2867       break;
2868   
2869   /* No static initialized, the list is fine as is */
2870   if (!previous)
2871     list = TREE_CHAIN (marker);
2872
2873   /* No marker? reverse the whole list */
2874   else if (!marker)
2875     list = nreverse (list);
2876
2877   /* Otherwise, reverse what's after the marker and the new reordered
2878      sublist will replace the marker. */
2879   else
2880     {
2881       TREE_CHAIN (previous) = NULL_TREE;
2882       list = nreverse (list);
2883       list = chainon (TREE_CHAIN (marker), list);
2884     }
2885   return list;
2886 }
2887
2888 /* Helper functions to dump the parser context stack.  */
2889
2890 #define TAB_CONTEXT(C) \
2891   {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2892
2893 static void
2894 java_debug_context_do (tab)
2895      int tab;
2896 {
2897   struct parser_ctxt *copy = ctxp;
2898   while (copy)
2899     {
2900       TAB_CONTEXT (tab);
2901       fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2902       TAB_CONTEXT (tab);
2903       fprintf (stderr, "filename: %s\n", copy->filename);
2904       TAB_CONTEXT (tab);
2905       fprintf (stderr, "lineno: %d\n", copy->lineno);
2906       TAB_CONTEXT (tab);
2907       fprintf (stderr, "package: %s\n",
2908                (copy->package ? 
2909                 IDENTIFIER_POINTER (copy->package) : "<none>"));
2910       TAB_CONTEXT (tab);
2911       fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2912       TAB_CONTEXT (tab);
2913       fprintf (stderr, "saved data: %d\n", copy->saved_data);
2914       copy = copy->next;
2915       tab += 2;
2916     }
2917 }
2918
2919 /* Dump the stacked up parser contexts. Intended to be called from a
2920    debugger.  */
2921
2922 void
2923 java_debug_context ()
2924 {
2925   java_debug_context_do (0);
2926 }
2927
2928 \f
2929
2930 /* Flag for the error report routine to issue the error the first time
2931    it's called (overriding the default behavior which is to drop the
2932    first invocation and honor the second one, taking advantage of a
2933    richer context.  */
2934 static int force_error = 0;
2935
2936 /* Reporting an constructor invocation error.  */
2937 static void
2938 parse_ctor_invocation_error ()
2939 {
2940   if (DECL_CONSTRUCTOR_P (current_function_decl))
2941     yyerror ("Constructor invocation must be first thing in a constructor"); 
2942   else
2943     yyerror ("Only constructors can invoke constructors");
2944 }
2945
2946 /* Reporting JDK1.1 features not implemented.  */
2947
2948 static tree
2949 parse_jdk1_1_error (msg)
2950     const char *msg;
2951 {
2952   sorry (": `%s' JDK1.1(TM) feature", msg);
2953   java_error_count++;
2954   return empty_stmt_node;
2955 }
2956
2957 static int do_warning = 0;
2958
2959 void
2960 yyerror (msg)
2961      const char *msg;
2962 {
2963   static java_lc elc;
2964   static int  prev_lineno;
2965   static const char *prev_msg;
2966
2967   int save_lineno;
2968   char *remainder, *code_from_source;
2969   
2970   if (!force_error && prev_lineno == lineno)
2971     return;
2972
2973   /* Save current error location but report latter, when the context is
2974      richer.  */
2975   if (ctxp->java_error_flag == 0)
2976     {
2977       ctxp->java_error_flag = 1;
2978       elc = ctxp->elc;
2979       /* Do something to use the previous line if we're reaching the
2980          end of the file... */
2981 #ifdef VERBOSE_SKELETON
2982       printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2983 #endif
2984       return;
2985     }
2986
2987   /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2988   if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2989     return;
2990
2991   ctxp->java_error_flag = 0;
2992   if (do_warning)
2993     java_warning_count++;
2994   else
2995     java_error_count++;
2996   
2997   if (elc.col == 0 && msg && msg[1] == ';')
2998     {
2999       elc.col  = ctxp->p_line->char_col-1;
3000       elc.line = ctxp->p_line->lineno;
3001     }
3002
3003   save_lineno = lineno;
3004   prev_lineno = lineno = elc.line;
3005   prev_msg = msg;
3006
3007   code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3008   obstack_grow0 (&temporary_obstack, 
3009                  code_from_source, strlen (code_from_source));
3010   remainder = obstack_finish (&temporary_obstack);
3011   if (do_warning)
3012     warning ("%s.\n%s", msg, remainder);
3013   else
3014     error ("%s.\n%s", msg, remainder);
3015
3016   /* This allow us to cheaply avoid an extra 'Invalid expression
3017      statement' error report when errors have been already reported on
3018      the same line. This occurs when we report an error but don't have
3019      a synchronization point other than ';', which
3020      expression_statement is the only one to take care of.  */
3021   ctxp->prevent_ese = lineno = save_lineno;
3022 }
3023
3024 static void
3025 issue_warning_error_from_context (cl, msg, ap)
3026      tree cl;
3027      const char *msg;
3028      va_list ap;
3029 {
3030   const char *saved, *saved_input_filename;
3031   char buffer [4096];
3032   vsprintf (buffer, msg, ap);
3033   force_error = 1;
3034
3035   ctxp->elc.line = EXPR_WFL_LINENO (cl);
3036   ctxp->elc.col  = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 : 
3037                     (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3038
3039   /* We have a CL, that's a good reason for using it if it contains data */
3040   saved = ctxp->filename;
3041   if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3042     ctxp->filename = EXPR_WFL_FILENAME (cl);
3043   saved_input_filename = input_filename;
3044   input_filename = ctxp->filename;
3045   java_error (NULL);
3046   java_error (buffer);
3047   ctxp->filename = saved;
3048   input_filename = saved_input_filename;
3049   force_error = 0;
3050 }
3051
3052 /* Issue an error message at a current source line CL */
3053
3054 void
3055 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3056 {
3057 #ifndef ANSI_PROTOTYPES
3058   tree cl;
3059   const char *msg;
3060 #endif
3061   va_list ap;
3062
3063   VA_START (ap, msg);
3064 #ifndef ANSI_PROTOTYPES
3065   cl = va_arg (ap, tree);
3066   msg = va_arg (ap, const char *);
3067 #endif
3068   issue_warning_error_from_context (cl, msg, ap);
3069   va_end (ap);
3070 }
3071
3072 /* Issue a warning at a current source line CL */
3073
3074 static void
3075 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3076 {
3077 #ifndef ANSI_PROTOTYPES
3078   tree cl;
3079   const char *msg;
3080 #endif
3081   va_list ap;
3082
3083   VA_START (ap, msg);
3084 #ifndef ANSI_PROTOTYPES
3085   cl = va_arg (ap, tree);
3086   msg = va_arg (ap, const char *);
3087 #endif
3088
3089   force_error = do_warning = 1;
3090   issue_warning_error_from_context (cl, msg, ap);
3091   do_warning = force_error = 0;
3092   va_end (ap);
3093 }
3094
3095 static tree
3096 find_expr_with_wfl (node)
3097      tree node;
3098 {
3099   while (node)
3100     {
3101       char code;
3102       tree to_return;
3103
3104       switch (TREE_CODE (node))
3105         {
3106         case BLOCK:
3107           node = BLOCK_EXPR_BODY (node);
3108           continue;
3109
3110         case COMPOUND_EXPR:
3111           to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3112           if (to_return)
3113             return to_return;
3114           node = TREE_OPERAND (node, 1);
3115           continue;
3116
3117         case LOOP_EXPR:
3118           node = TREE_OPERAND (node, 0);
3119           continue;
3120           
3121         case LABELED_BLOCK_EXPR:
3122           node = TREE_OPERAND (node, 1);
3123           continue;
3124
3125         default:
3126           code = TREE_CODE_CLASS (TREE_CODE (node));
3127           if (((code == '1') || (code == '2') || (code == 'e'))
3128               && EXPR_WFL_LINECOL (node))
3129             return node;
3130           return NULL_TREE;
3131         }
3132     }
3133   return NULL_TREE;
3134 }
3135
3136 /* Issue a missing return statement error. Uses METHOD to figure the
3137    last line of the method the error occurs in.  */
3138
3139 static void
3140 missing_return_error (method)
3141      tree method;
3142 {
3143   EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3144   parse_error_context (wfl_operator, "Missing return statement");
3145 }
3146
3147 /* Issue an unreachable statement error. From NODE, find the next
3148    statement to report appropriately.  */
3149 static void
3150 unreachable_stmt_error (node)
3151      tree node;
3152 {
3153   /* Browse node to find the next expression node that has a WFL. Use
3154      the location to report the error */
3155   if (TREE_CODE (node) == COMPOUND_EXPR)
3156     node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3157   else
3158     node = find_expr_with_wfl (node);
3159
3160   if (node)
3161     {
3162       EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3163       parse_error_context (wfl_operator, "Unreachable statement");
3164     }
3165   else
3166     abort ();
3167 }
3168
3169 int
3170 java_report_errors ()
3171 {
3172   if (java_error_count)
3173     fprintf (stderr, "%d error%s", 
3174              java_error_count, (java_error_count == 1 ? "" : "s"));
3175   if (java_warning_count)
3176     fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3177              java_warning_count, (java_warning_count == 1 ? "" : "s"));
3178   if (java_error_count || java_warning_count)
3179     putc ('\n', stderr);
3180   return java_error_count;
3181 }
3182
3183 static char *
3184 java_accstring_lookup (flags)
3185      int flags;
3186 {
3187   static char buffer [80];
3188 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3189
3190   /* Access modifier looked-up first for easier report on forbidden
3191      access. */
3192   if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3193   if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3194   if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3195   if (flags & ACC_STATIC) COPY_RETURN ("static");
3196   if (flags & ACC_FINAL) COPY_RETURN ("final");
3197   if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3198   if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3199   if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3200   if (flags & ACC_NATIVE) COPY_RETURN ("native");
3201   if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3202   if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3203
3204   buffer [0] = '\0';
3205   return buffer;
3206 #undef COPY_RETURN
3207 }
3208
3209 /* Issuing error messages upon redefinition of classes, interfaces or
3210    variables. */
3211
3212 static void
3213 classitf_redefinition_error (context, id, decl, cl)
3214      const char *context;
3215      tree id, decl, cl;
3216 {
3217   parse_error_context (cl, "%s `%s' already defined in %s:%d", 
3218                        context, IDENTIFIER_POINTER (id), 
3219                        DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3220   /* Here we should point out where its redefined. It's a unicode. FIXME */
3221 }
3222
3223 static void
3224 variable_redefinition_error (context, name, type, line)
3225      tree context, name, type;
3226      int line;
3227 {
3228   const char *type_name;
3229
3230   /* Figure a proper name for type. We might haven't resolved it */
3231   if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3232     type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3233   else
3234     type_name = lang_printable_name (type, 0);
3235
3236   parse_error_context (context,
3237                        "Variable `%s' is already defined in this method and was declared `%s %s' at line %d", 
3238                        IDENTIFIER_POINTER (name),
3239                        type_name, IDENTIFIER_POINTER (name), line);
3240 }
3241
3242 static tree
3243 build_array_from_name (type, type_wfl, name, ret_name)
3244      tree type, type_wfl, name, *ret_name;
3245 {
3246   int more_dims = 0;
3247   const char *string;
3248
3249   /* Eventually get more dims */
3250   string = IDENTIFIER_POINTER (name);
3251   while (string [more_dims] == '[')
3252     more_dims++;
3253   
3254   /* If we have, then craft a new type for this variable */
3255   if (more_dims)
3256     {
3257       tree save = type;
3258
3259       name = get_identifier (&string [more_dims]);
3260
3261       /* If we have a pointer, use its type */
3262       if (TREE_CODE (type) == POINTER_TYPE)
3263         type = TREE_TYPE (type);
3264
3265       /* Building the first dimension of a primitive type uses this
3266          function */
3267       if (JPRIMITIVE_TYPE_P (type))
3268         {
3269           type = build_java_array_type (type, -1);
3270           more_dims--;
3271         }
3272       /* Otherwise, if we have a WFL for this type, use it (the type
3273          is already an array on an unresolved type, and we just keep
3274          on adding dimensions) */
3275       else if (type_wfl)
3276         {
3277           int i = 0;
3278           type = type_wfl;
3279           string = IDENTIFIER_POINTER (TYPE_NAME (save));
3280           while (string[i]  == '[')
3281             ++i;
3282           more_dims += i;
3283         }
3284
3285       /* Add all the dimensions */
3286       while (more_dims--)
3287         type = build_unresolved_array_type (type);
3288
3289       /* The type may have been incomplete in the first place */
3290       if (type_wfl)
3291         type = obtain_incomplete_type (type);
3292     }
3293
3294   if (ret_name)
3295     *ret_name = name;
3296   return type;
3297 }
3298
3299 /* Build something that the type identifier resolver will identify as
3300    being an array to an unresolved type. TYPE_WFL is a WFL on a
3301    identifier. */
3302
3303 static tree
3304 build_unresolved_array_type (type_or_wfl)
3305      tree type_or_wfl;
3306 {
3307   const char *ptr;
3308   tree wfl;
3309
3310   /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3311      just create a array type */
3312   if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3313     return build_java_array_type (type_or_wfl, -1);
3314
3315   obstack_1grow (&temporary_obstack, '[');
3316   obstack_grow0 (&temporary_obstack,
3317                  IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3318                  IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3319   ptr = obstack_finish (&temporary_obstack);
3320   wfl = build_expr_wfl (get_identifier (ptr),
3321                         EXPR_WFL_FILENAME (type_or_wfl),
3322                         EXPR_WFL_LINENO (type_or_wfl),
3323                         EXPR_WFL_COLNO (type_or_wfl));
3324   /* Re-install the existing qualifications so that the type can be
3325      resolved properly. */
3326   EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3327   return wfl;
3328 }
3329
3330 static void
3331 parser_add_interface (class_decl, interface_decl, wfl)
3332      tree class_decl, interface_decl, wfl;
3333 {
3334   if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3335     parse_error_context (wfl, "Interface `%s' repeated",
3336                          IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3337 }
3338
3339 /* Bulk of common class/interface checks. Return 1 if an error was
3340    encountered. TAG is 0 for a class, 1 for an interface.  */
3341
3342 static int
3343 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3344      int is_interface, flags;
3345      tree raw_name, qualified_name, decl, cl;
3346 {
3347   tree node;
3348   int sca = 0;                  /* Static class allowed */
3349   int icaf = 0;                 /* Inner class allowed flags */
3350   int uaaf = CLASS_MODIFIERS;   /* Usually allowed access flags */
3351
3352   if (!quiet_flag)
3353     fprintf (stderr, " %s%s %s", 
3354              (CPC_INNER_P () ? "inner" : ""),
3355              (is_interface ? "interface" : "class"), 
3356              IDENTIFIER_POINTER (qualified_name));
3357
3358   /* Scope of an interface/class type name:
3359        - Can't be imported by a single type import
3360        - Can't already exists in the package */
3361   if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3362       && (node = find_name_in_single_imports (raw_name))
3363       && !CPC_INNER_P ())
3364     {
3365       parse_error_context 
3366         (cl, "%s name `%s' clashes with imported type `%s'",
3367          (is_interface ? "Interface" : "Class"),
3368          IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3369       return 1;
3370     }
3371   if (decl && CLASS_COMPLETE_P (decl))
3372     {
3373       classitf_redefinition_error ((is_interface ? "Interface" : "Class"), 
3374                                    qualified_name, decl, cl);
3375       return 1;
3376     }
3377
3378   if (check_inner_class_redefinition (raw_name, cl))
3379     return 1;
3380
3381   /* If public, file name should match class/interface name, except
3382      when dealing with an inner class */
3383   if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3384     {
3385       const char *f;
3386
3387       /* Contains OS dependent assumption on path separator. FIXME */
3388       for (f = &input_filename [strlen (input_filename)]; 
3389            f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3390            f--)
3391         ;
3392       if (f[0] == '/' || f[0] == DIR_SEPARATOR)
3393         f++;
3394       if (strncmp (IDENTIFIER_POINTER (raw_name), 
3395                    f , IDENTIFIER_LENGTH (raw_name)) ||
3396           f [IDENTIFIER_LENGTH (raw_name)] != '.')
3397         parse_error_context
3398           (cl, "Public %s `%s' must be defined in a file called `%s.java'", 
3399                              (is_interface ? "interface" : "class"),
3400                              IDENTIFIER_POINTER (qualified_name),
3401                              IDENTIFIER_POINTER (raw_name));
3402     }
3403
3404   /* Static classes can be declared only in top level classes. Note:
3405      once static, a inner class is a top level class. */
3406   if (flags & ACC_STATIC)
3407     {
3408       /* Catch the specific error of declaring an class inner class
3409          with no toplevel enclosing class. Prevent check_modifiers from
3410          complaining a second time */
3411       if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3412         {
3413           parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes", 
3414                                IDENTIFIER_POINTER (qualified_name));
3415           sca = ACC_STATIC;
3416         }
3417       /* Else, in the context of a top-level class declaration, let
3418          `check_modifiers' do its job, otherwise, give it a go */
3419       else
3420         sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3421     }
3422
3423   /* Inner classes can be declared private or protected
3424      within their enclosing classes. */
3425   if (CPC_INNER_P ())
3426     {
3427       /* A class which is local to a block can't be public, private,
3428          protected or static. But it is created final, so allow this
3429          one. */
3430       if (current_function_decl)
3431         icaf = sca = uaaf = ACC_FINAL;
3432       else
3433         {
3434           check_modifiers_consistency (flags);
3435           icaf = ACC_PRIVATE|ACC_PROTECTED;
3436         }
3437     }
3438
3439   if (is_interface) 
3440     {
3441       if (CPC_INNER_P ())
3442         uaaf = INTERFACE_INNER_MODIFIERS;
3443       else
3444         uaaf = INTERFACE_MODIFIERS;
3445       
3446       check_modifiers ("Illegal modifier `%s' for interface declaration", 
3447                        flags, uaaf);
3448     }
3449   else
3450     check_modifiers ((current_function_decl ?
3451                       "Illegal modifier `%s' for local class declaration" :
3452                       "Illegal modifier `%s' for class declaration"),
3453                      flags, uaaf|sca|icaf);
3454   return 0;
3455 }
3456
3457 static void
3458 make_nested_class_name (cpc_list)
3459      tree cpc_list;
3460 {
3461   tree name;
3462
3463   if (!cpc_list)
3464     return;
3465   else
3466     make_nested_class_name (TREE_CHAIN (cpc_list));
3467
3468   /* Pick the qualified name when dealing with the first upmost
3469      enclosing class */
3470   name = (TREE_CHAIN (cpc_list) ? 
3471           TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3472   obstack_grow (&temporary_obstack,
3473                 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3474   /* Why is NO_DOLLAR_IN_LABEL defined? */
3475 #if 0
3476 #ifdef NO_DOLLAR_IN_LABEL
3477   internal_error ("Can't use '$' as a separator for inner classes");
3478 #endif
3479 #endif
3480   obstack_1grow (&temporary_obstack, '$');
3481 }
3482
3483 /* Can't redefine a class already defined in an earlier scope. */
3484
3485 static int
3486 check_inner_class_redefinition (raw_name, cl)
3487      tree raw_name, cl;
3488 {
3489   tree scope_list;
3490
3491   for (scope_list = GET_CPC_LIST (); scope_list; 
3492        scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3493     if (raw_name == GET_CPC_UN_NODE (scope_list))
3494       {
3495         parse_error_context 
3496           (cl, "The class name `%s' is already defined in this scope. An inner class may not have the same simple name as any of its enclosing classes",
3497            IDENTIFIER_POINTER (raw_name));
3498         return 1;
3499       }
3500   return 0;
3501 }
3502
3503 static tree
3504 find_as_inner_class (enclosing, name, cl)
3505      tree enclosing, name, cl;
3506 {
3507   tree qual, to_return;
3508   if (!enclosing)
3509     return NULL_TREE;
3510
3511   name = TYPE_NAME (name);
3512
3513   /* First search: within the scope of `enclosing', search for name */
3514   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3515     qual = EXPR_WFL_QUALIFICATION (cl);
3516   else if (cl)
3517     qual = build_tree_list (cl, NULL_TREE);
3518   else
3519     qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3520   
3521   if ((to_return = find_as_inner_class_do (qual, enclosing)))
3522     return to_return;
3523
3524   /* We're dealing with a qualified name. Try to resolve thing until
3525      we get something that is an enclosing class. */
3526   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3527     {
3528       tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3529
3530       for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl; 
3531            qual = TREE_CHAIN (qual))
3532         {
3533           acc = merge_qualified_name (acc, 
3534                                       EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3535           BUILD_PTR_FROM_NAME (ptr, acc);
3536
3537           /* Don't try to resolve ACC as a class name if it follows
3538              the current package name. We don't want to pick something
3539              that's accidentally there: for example `a.b.c' in package
3540              `a.b' shouldn't trigger loading `a' if it's there by
3541              itself. */
3542           if (ctxp->package
3543               && strstr (IDENTIFIER_POINTER (ctxp->package),
3544                          IDENTIFIER_POINTER (acc)))
3545             decl = NULL;
3546           else
3547             decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3548         }
3549
3550       /* A NULL qual and a decl means that the search ended
3551          successfully?!? We have to do something then. FIXME */
3552       
3553       if (decl)
3554         enclosing = decl;
3555       else
3556         qual = EXPR_WFL_QUALIFICATION (cl);
3557     }
3558   /* Otherwise, create a qual for the other part of the resolution. */
3559   else
3560     qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3561
3562   return find_as_inner_class_do (qual, enclosing);
3563 }
3564
3565 /* We go inside the list of sub classes and try to find a way
3566    through. */
3567
3568 static tree
3569 find_as_inner_class_do (qual, enclosing)
3570      tree qual, enclosing;
3571 {
3572   if (!qual)
3573     return NULL_TREE;
3574
3575   for (; qual && enclosing; qual = TREE_CHAIN (qual))
3576     {
3577       tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3578       tree next_enclosing = NULL_TREE;
3579       tree inner_list;
3580
3581       for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3582            inner_list; inner_list = TREE_CHAIN (inner_list))
3583         {
3584           if (TREE_VALUE (inner_list) == name_to_match)
3585             {
3586               next_enclosing = TREE_PURPOSE (inner_list);
3587               break;
3588             }
3589         }
3590       enclosing = next_enclosing;
3591     }
3592
3593   return (!qual && enclosing ? enclosing : NULL_TREE);
3594 }
3595
3596 /* Reach all inner classes and tie their unqualified name to a
3597    DECL. */
3598
3599 static void
3600 set_nested_class_simple_name_value (outer, set)
3601      tree outer;
3602      int set;
3603 {
3604   tree l;
3605
3606   for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3607     IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ? 
3608                                                 TREE_PURPOSE (l) : NULL_TREE);
3609 }
3610
3611 static void
3612 link_nested_class_to_enclosing ()
3613 {
3614   if (GET_ENCLOSING_CPC ())
3615     {
3616       tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3617       DECL_INNER_CLASS_LIST (enclosing) = 
3618         tree_cons (GET_CPC (), GET_CPC_UN (),
3619                    DECL_INNER_CLASS_LIST (enclosing));
3620       enclosing = enclosing;
3621     }
3622 }
3623
3624 static tree
3625 maybe_make_nested_class_name (name)
3626      tree name;
3627 {
3628   tree id = NULL_TREE;
3629
3630   if (CPC_INNER_P ())
3631     {
3632       make_nested_class_name (GET_CPC_LIST ());
3633       obstack_grow0 (&temporary_obstack,
3634                      IDENTIFIER_POINTER (name), 
3635                      IDENTIFIER_LENGTH (name));
3636       id = get_identifier (obstack_finish (&temporary_obstack));
3637       if (ctxp->package)
3638         QUALIFIED_P (id) = 1;
3639     }
3640   return id;
3641 }
3642
3643 /* If DECL is NULL, create and push a new DECL, record the current
3644    line CL and do other maintenance things.  */
3645
3646 static tree
3647 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3648      tree decl, raw_name, qualified_name, cl;
3649 {
3650   if (!decl)
3651     decl = push_class (make_class (), qualified_name);
3652
3653   /* Take care of the file and line business */
3654   DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3655   /* If we're emiting xrefs, store the line/col number information */
3656   if (flag_emit_xref)
3657     DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3658   else
3659     DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3660   CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3661   CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3662   CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3663     IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3664
3665   PUSH_CPC (decl, raw_name);
3666   DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3667
3668   /* Link the declaration to the already seen ones */
3669   TREE_CHAIN (decl) = ctxp->class_list;
3670   ctxp->class_list = decl;
3671
3672   /* Create a new nodes in the global lists */
3673   gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3674   all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3675
3676   /* Install a new dependency list element */
3677   create_jdep_list (ctxp);
3678
3679   SOURCE_FRONTEND_DEBUG (("Defining class/interface %s", 
3680                           IDENTIFIER_POINTER (qualified_name)));
3681   return decl;
3682 }
3683
3684 static void
3685 add_superinterfaces (decl, interface_list)
3686      tree decl, interface_list;
3687 {
3688   tree node;
3689   /* Superinterface(s): if present and defined, parser_check_super_interface ()
3690      takes care of ensuring that:
3691        - This is an accessible interface type,
3692        - Circularity detection.
3693    parser_add_interface is then called. If present but not defined,
3694    the check operation is delayed until the super interface gets
3695    defined.  */
3696   for (node = interface_list; node; node = TREE_CHAIN (node))
3697     {
3698       tree current = TREE_PURPOSE (node);
3699       tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3700       if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3701         {
3702           if (!parser_check_super_interface (idecl, decl, current))
3703             parser_add_interface (decl, idecl, current);
3704         }
3705       else
3706         register_incomplete_type (JDEP_INTERFACE,
3707                                   current, decl, NULL_TREE);
3708     }
3709 }
3710
3711 /* Create an interface in pass1 and return its decl. Return the
3712    interface's decl in pass 2.  */
3713
3714 static tree
3715 create_interface (flags, id, super)
3716      int flags;
3717      tree id, super;
3718 {
3719   tree raw_name = EXPR_WFL_NODE (id);
3720   tree q_name = parser_qualified_classname (raw_name);
3721   tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3722
3723   EXPR_WFL_NODE (id) = q_name;  /* Keep source location, even if refined. */
3724
3725   /* Basic checks: scope, redefinition, modifiers */ 
3726   if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3727     {
3728       PUSH_ERROR ();
3729       return NULL_TREE;
3730     }
3731
3732   /* Suspend the current parsing context if we're parsing an inner
3733      interface */
3734   if (CPC_INNER_P ())
3735     java_parser_context_suspend ();
3736
3737   /* Push a new context for (static) initialized upon declaration fields */
3738   java_parser_context_push_initialized_field ();
3739
3740   /* Interface modifiers check
3741        - public/abstract allowed (already done at that point)
3742        - abstract is obsolete (comes first, it's a warning, or should be)
3743        - Can't use twice the same (checked in the modifier rule) */
3744   if ((flags & ACC_ABSTRACT) && flag_redundant)
3745     parse_warning_context 
3746       (MODIFIER_WFL (ABSTRACT_TK),
3747        "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
3748
3749   /* Create a new decl if DECL is NULL, otherwise fix it */
3750   decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3751
3752   /* Set super info and mark the class a complete */
3753   set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl), 
3754                   object_type_node, ctxp->interface_number);
3755   ctxp->interface_number = 0;
3756   CLASS_COMPLETE_P (decl) = 1;
3757   add_superinterfaces (decl, super);
3758
3759   return decl;
3760 }
3761
3762 /* Anonymous class counter. Will be reset to 1 every time a non
3763    anonymous class gets created. */
3764 static int anonymous_class_counter = 1;
3765
3766 /* Patch anonymous class CLASS, by either extending or implementing
3767    DEP.  */
3768
3769 static void
3770 patch_anonymous_class (type_decl, class_decl, wfl)
3771     tree type_decl, class_decl, wfl;
3772 {
3773   tree class = TREE_TYPE (class_decl);
3774   tree type =  TREE_TYPE (type_decl);
3775   tree binfo = TYPE_BINFO (class);
3776
3777   /* If it's an interface, implement it */
3778   if (CLASS_INTERFACE (type_decl))
3779     {
3780       tree s_binfo;
3781       int length;
3782
3783       if (parser_check_super_interface (type_decl, class_decl, wfl))
3784         return;
3785
3786       s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3787       length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3788       TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3789       TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3790       /* And add the interface */
3791       parser_add_interface (class_decl, type_decl, wfl);
3792     }
3793   /* Otherwise, it's a type we want to extend */
3794   else
3795     {
3796       if (parser_check_super (type_decl, class_decl, wfl))
3797         return;
3798       BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3799     }
3800 }
3801
3802 static tree
3803 create_anonymous_class (location, type_name)
3804     int location;
3805     tree type_name;
3806 {
3807   char buffer [80];
3808   tree super = NULL_TREE, itf = NULL_TREE;
3809   tree id, type_decl, class;
3810
3811   /* The unqualified name of the anonymous class. It's just a number. */
3812   sprintf (buffer, "%d", anonymous_class_counter++);
3813   id = build_wfl_node (get_identifier (buffer));
3814   EXPR_WFL_LINECOL (id) = location;
3815
3816   /* We know about the type to extend/implement. We go ahead */
3817   if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3818     {
3819       /* Create a class which either implements on extends the designated
3820          class. The class bears an innacessible name. */
3821       if (CLASS_INTERFACE (type_decl))
3822         {
3823           /* It's OK to modify it here. It's been already used and
3824              shouldn't be reused */
3825           ctxp->interface_number = 1;
3826           /* Interfaces should presented as a list of WFLs */
3827           itf = build_tree_list (type_name, NULL_TREE);
3828         }
3829       else
3830         super = type_name;
3831     }
3832
3833   class = create_class (ACC_FINAL, id, super, itf);
3834
3835   /* We didn't know anything about the stuff. We register a dependence. */
3836   if (!type_decl)
3837     register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3838
3839   ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3840   return class;
3841 }
3842
3843 /* Create a class in pass1 and return its decl. Return class
3844    interface's decl in pass 2.  */
3845
3846 static tree
3847 create_class (flags, id, super, interfaces)
3848      int flags;
3849      tree id, super, interfaces;
3850 {
3851   tree raw_name = EXPR_WFL_NODE (id);
3852   tree class_id, decl;
3853   tree super_decl_type;
3854
3855   class_id = parser_qualified_classname (raw_name);
3856   decl = IDENTIFIER_CLASS_VALUE (class_id);
3857   EXPR_WFL_NODE (id) = class_id;
3858
3859   /* Basic check: scope, redefinition, modifiers */
3860   if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3861     {
3862       PUSH_ERROR ();
3863       return NULL_TREE;
3864     }
3865   
3866   /* Suspend the current parsing context if we're parsing an inner
3867      class or an anonymous class. */
3868   if (CPC_INNER_P ())
3869     java_parser_context_suspend ();
3870   /* Push a new context for (static) initialized upon declaration fields */
3871   java_parser_context_push_initialized_field ();
3872
3873   /* Class modifier check: 
3874        - Allowed modifier (already done at that point)
3875        - abstract AND final forbidden 
3876        - Public classes defined in the correct file */
3877   if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3878     parse_error_context
3879       (id, "Class `%s' can't be declared both abstract and final",
3880        IDENTIFIER_POINTER (raw_name));
3881
3882   /* Create a new decl if DECL is NULL, otherwise fix it */
3883   decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
3884
3885   /* If SUPER exists, use it, otherwise use Object */
3886   if (super)
3887     {
3888       /* Can't extend java.lang.Object */
3889       if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3890         {
3891           parse_error_context (id, "Can't extend `java.lang.Object'");
3892           return NULL_TREE;
3893         }
3894
3895       super_decl_type = 
3896         register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
3897     }
3898   else if (TREE_TYPE (decl) != object_type_node)
3899     super_decl_type = object_type_node;
3900   /* We're defining java.lang.Object */
3901   else
3902     super_decl_type = NULL_TREE;
3903
3904   /* A class nested in an interface is implicitly static. */
3905   if (INNER_CLASS_DECL_P (decl)
3906       && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
3907     {
3908       flags |= ACC_STATIC;
3909     }
3910
3911   /* Set super info and mark the class as complete. */
3912   set_super_info (flags, TREE_TYPE (decl), super_decl_type, 
3913                   ctxp->interface_number);
3914   ctxp->interface_number = 0;
3915   CLASS_COMPLETE_P (decl) = 1;
3916   add_superinterfaces (decl, interfaces);
3917
3918   /* Add the private this$<n> field, Replicate final locals still in
3919      scope as private final fields mangled like val$<local_name>.
3920      This doesn't not occur for top level (static) inner classes. */
3921   if (PURE_INNER_CLASS_DECL_P (decl))
3922     add_inner_class_fields (decl, current_function_decl);
3923
3924   /* If doing xref, store the location at which the inherited class
3925      (if any) was seen. */
3926   if (flag_emit_xref && super)
3927     DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3928
3929   /* Eventually sets the @deprecated tag flag */
3930   CHECK_DEPRECATED (decl);
3931
3932   /* Reset the anonymous class counter when declaring non inner classes */
3933   if (!INNER_CLASS_DECL_P (decl))
3934     anonymous_class_counter = 1;
3935
3936   return decl;
3937 }
3938
3939 /* End a class declaration: register the statements used to create
3940    finit$ and <clinit>, pop the current class and resume the prior
3941    parser context if necessary.  */
3942
3943 static void
3944 end_class_declaration (resume)
3945      int resume;
3946 {
3947   /* If an error occured, context weren't pushed and won't need to be
3948      popped by a resume. */
3949   int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3950
3951   java_parser_context_pop_initialized_field ();
3952   POP_CPC ();
3953   if (resume && no_error_occured)
3954     java_parser_context_resume ();
3955
3956   /* We're ending a class declaration, this is a good time to reset
3957      the interface cout. Note that might have been already done in
3958      create_interface, but if at that time an inner class was being
3959      dealt with, the interface count was reset in a context created
3960      for the sake of handling inner classes declaration. */
3961   ctxp->interface_number = 0;
3962 }
3963
3964 static void
3965 add_inner_class_fields (class_decl, fct_decl)
3966      tree class_decl;
3967      tree fct_decl;
3968 {
3969   tree block, marker, f;
3970
3971   f = add_field (TREE_TYPE (class_decl),
3972                  build_current_thisn (TREE_TYPE (class_decl)),
3973                  build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))), 
3974                  ACC_PRIVATE);
3975   FIELD_THISN (f) = 1;
3976
3977   if (!fct_decl)
3978     return;
3979     
3980   for (block = GET_CURRENT_BLOCK (fct_decl); 
3981        block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3982     {
3983       tree decl;
3984       for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3985         {
3986           tree name, pname;
3987           tree wfl, init, list;
3988           
3989           /* Avoid non final arguments. */
3990           if (!LOCAL_FINAL_P (decl))
3991             continue;
3992           
3993           MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3994           MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3995           wfl = build_wfl_node (name);
3996           init = build_wfl_node (pname);
3997           /* Build an initialization for the field: it will be
3998              initialized by a parameter added to finit$, bearing a
3999              mangled name of the field itself (param$<n>.) The
4000              parameter is provided to finit$ by the constructor
4001              invoking it (hence the constructor will also feature a
4002              hidden parameter, set to the value of the outer context
4003              local at the time the inner class is created.)
4004              
4005              Note: we take into account all possible locals that can
4006              be accessed by the inner class. It's actually not trivial
4007              to minimize these aliases down to the ones really
4008              used. One way to do that would be to expand all regular
4009              methods first, then finit$ to get a picture of what's
4010              used.  It works with the exception that we would have to
4011              go back on all constructor invoked in regular methods to
4012              have their invokation reworked (to include the right amount
4013              of alias initializer parameters.)
4014
4015              The only real way around, I think, is a first pass to
4016              identify locals really used in the inner class. We leave
4017              the flag FIELD_LOCAL_ALIAS_USED around for that future
4018              use.
4019              
4020              On the other hand, it only affect local inner classes,
4021              whose constructors (and finit$ call) will be featuring
4022              unecessary arguments. It's easy for a developper to keep
4023              this number of parameter down by using the `final'
4024              keyword only when necessary. For the time being, we can
4025              issue a warning on unecessary finals. FIXME */
4026           init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl), 
4027                                    wfl, init);
4028
4029           /* Register the field. The TREE_LIST holding the part
4030              initialized/initializer will be marked ARG_FINAL_P so
4031              that the created field can be marked
4032              FIELD_LOCAL_ALIAS. */
4033           list = build_tree_list (wfl, init);
4034           ARG_FINAL_P (list) = 1;
4035           register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4036         }
4037     }
4038
4039   if (!CPC_INITIALIZER_STMT (ctxp))
4040     return;
4041
4042   /* If we ever registered an alias field, insert and marker to
4043      remeber where the list ends. The second part of the list (the one
4044      featuring initialized fields) so it can be later reversed to
4045      enforce 8.5. The marker will be removed during that operation. */
4046   marker = build_tree_list (NULL_TREE, NULL_TREE);
4047   TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4048   SET_CPC_INITIALIZER_STMT (ctxp, marker);
4049 }
4050
4051 /* Can't use lookup_field () since we don't want to load the class and
4052    can't set the CLASS_LOADED_P flag */
4053
4054 static tree
4055 find_field (class, name)
4056      tree class;
4057      tree name;
4058 {
4059   tree decl;
4060   for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4061     {
4062       if (DECL_NAME (decl) == name)
4063         return decl;
4064     }
4065   return NULL_TREE;
4066 }
4067
4068 /* Wrap around lookup_field that doesn't potentially upset the value
4069    of CLASS */
4070
4071 static tree
4072 lookup_field_wrapper (class, name)
4073      tree class, name;
4074 {
4075   tree type = class;
4076   tree decl = NULL_TREE;
4077   java_parser_context_save_global ();
4078
4079   /* Last chance: if we're within the context of an inner class, we
4080      might be trying to access a local variable defined in an outer
4081      context. We try to look for it now. */
4082   if (INNER_CLASS_TYPE_P (class))
4083     {
4084       tree new_name;
4085       MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4086       decl = lookup_field (&type, new_name);
4087       if (decl && decl != error_mark_node)
4088         FIELD_LOCAL_ALIAS_USED (decl) = 1;
4089     }
4090   if (!decl || decl == error_mark_node)
4091     {
4092       type = class;
4093       decl = lookup_field (&type, name);
4094     }
4095
4096   java_parser_context_restore_global ();
4097   return decl == error_mark_node ? NULL : decl;
4098 }
4099
4100 /* Find duplicate field within the same class declarations and report
4101    the error. Returns 1 if a duplicated field was found, 0
4102    otherwise.  */
4103
4104 static int
4105 duplicate_declaration_error_p (new_field_name, new_type, cl)
4106      tree new_field_name, new_type, cl;
4107 {
4108   /* This might be modified to work with method decl as well */
4109   tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4110   if (decl)
4111     {
4112       char *t1 = xstrdup (purify_type_name
4113                          ((TREE_CODE (new_type) == POINTER_TYPE 
4114                            && TREE_TYPE (new_type) == NULL_TREE) ?
4115                           IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4116                           lang_printable_name (new_type, 1)));
4117       /* The type may not have been completed by the time we report
4118          the error */
4119       char *t2 = xstrdup (purify_type_name
4120                          ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE 
4121                            && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4122                           IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4123                           lang_printable_name (TREE_TYPE (decl), 1)));
4124       parse_error_context 
4125         (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)", 
4126          t1, IDENTIFIER_POINTER (new_field_name),
4127          t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4128          DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4129       free (t1);
4130       free (t2);
4131       return 1;
4132     }
4133   return 0;
4134 }
4135
4136 /* Field registration routine. If TYPE doesn't exist, field
4137    declarations are linked to the undefined TYPE dependency list, to
4138    be later resolved in java_complete_class () */
4139
4140 static void
4141 register_fields (flags, type, variable_list)
4142      int flags;
4143      tree type, variable_list;
4144 {
4145   tree current, saved_type;
4146   tree class_type = NULL_TREE;
4147   int saved_lineno = lineno;
4148   int must_chain = 0;
4149   tree wfl = NULL_TREE;
4150
4151   if (GET_CPC ())
4152     class_type = TREE_TYPE (GET_CPC ());
4153
4154   if (!class_type || class_type == error_mark_node)
4155     return;
4156
4157   /* If we're adding fields to interfaces, those fields are public,
4158      static, final */
4159   if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4160     {
4161       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4162                                  flags, ACC_PUBLIC, "interface field(s)");
4163       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4164                                  flags, ACC_STATIC, "interface field(s)");
4165       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4166                                  flags, ACC_FINAL, "interface field(s)");
4167       check_modifiers ("Illegal interface member modifier `%s'", flags,
4168                        INTERFACE_FIELD_MODIFIERS);
4169       flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4170     }
4171
4172   /* Obtain a suitable type for resolution, if necessary */
4173   SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4174
4175   /* If TYPE is fully resolved and we don't have a reference, make one */
4176   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4177
4178   for (current = variable_list, saved_type = type; current; 
4179        current = TREE_CHAIN (current), type = saved_type)
4180     {
4181       tree real_type;
4182       tree field_decl;
4183       tree cl = TREE_PURPOSE (current);
4184       tree init = TREE_VALUE (current);
4185       tree current_name = EXPR_WFL_NODE (cl);
4186
4187       /* Can't declare non-final static fields in inner classes */
4188       if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4189           && !(flags & ACC_FINAL))
4190         parse_error_context 
4191           (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4192            IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4193            lang_printable_name (class_type, 0));
4194
4195       /* Process NAME, as it may specify extra dimension(s) for it */
4196       type = build_array_from_name (type, wfl, current_name, &current_name);
4197
4198       /* Type adjustment. We may have just readjusted TYPE because
4199          the variable specified more dimensions. Make sure we have
4200          a reference if we can and don't have one already. Also
4201          change the name if we have an init. */
4202       if (type != saved_type)
4203         {
4204           PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4205           if (init)
4206             EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4207         }
4208
4209       real_type = GET_REAL_TYPE (type);
4210       /* Check for redeclarations */
4211       if (duplicate_declaration_error_p (current_name, real_type, cl))
4212         continue;
4213
4214       /* Set lineno to the line the field was found and create a
4215          declaration for it. Eventually sets the @deprecated tag flag. */
4216       if (flag_emit_xref)
4217         lineno = EXPR_WFL_LINECOL (cl);
4218       else
4219         lineno = EXPR_WFL_LINENO (cl);
4220       field_decl = add_field (class_type, current_name, real_type, flags);
4221       CHECK_DEPRECATED (field_decl);
4222
4223       /* If the field denotes a final instance variable, then we
4224          allocate a LANG_DECL_SPECIFIC part to keep track of its
4225          initialization. We also mark whether the field was
4226          initialized upon it's declaration. We don't do that if the
4227          created field is an alias to a final local. */
4228       if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4229         {
4230           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4231           DECL_FIELD_FINAL_WFL (field_decl) = cl;
4232           if ((flags & ACC_STATIC) && init)
4233             DECL_FIELD_FINAL_IUD (field_decl) = 1;
4234         }
4235
4236       /* If the couple initializer/initialized is marked ARG_FINAL_P,
4237          we mark the created field FIELD_LOCAL_ALIAS, so that we can
4238          hide parameters to this inner class finit$ and
4239          constructors. It also means that the field isn't final per
4240          say. */
4241       if (ARG_FINAL_P (current))
4242         {
4243           FIELD_LOCAL_ALIAS (field_decl) = 1;
4244           FIELD_FINAL (field_decl) = 0;
4245         }
4246       
4247       /* Check if we must chain. */
4248       if (must_chain)
4249         register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4250           
4251       /* If we have an initialization value tied to the field */
4252       if (init)
4253         {
4254           /* The field is declared static */
4255           if (flags & ACC_STATIC)
4256             {
4257               /* We include the field and its initialization part into
4258                  a list used to generate <clinit>. After <clinit> is
4259                  walked, field initializations will be processed and
4260                  fields initialized with known constants will be taken
4261                  out of <clinit> and have their DECL_INITIAL set
4262                  appropriately. */
4263               TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4264               SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4265               if (TREE_OPERAND (init, 1) 
4266                   && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4267                 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4268             }
4269           /* A non-static field declared with an immediate initialization is
4270              to be initialized in <init>, if any.  This field is remembered
4271              to be processed at the time of the generation of <init>. */
4272           else
4273             {
4274               TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4275               SET_CPC_INITIALIZER_STMT (ctxp, init);
4276             }
4277           MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4278           DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4279         }
4280     }
4281   lineno = saved_lineno;
4282 }
4283
4284 /* Generate finit$, using the list of initialized fields to populate
4285    its body. finit$'s parameter(s) list is adjusted to include the
4286    one(s) used to initialized the field(s) caching outer context
4287    local(s). */
4288
4289 static tree
4290 generate_finit (class_type)
4291      tree class_type;
4292 {
4293   int count = 0;
4294   tree list = TYPE_FINIT_STMT_LIST (class_type);
4295   tree mdecl, current, parms;
4296
4297   parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION, 
4298                                                   class_type, NULL_TREE, 
4299                                                   &count);
4300   CRAFTED_PARAM_LIST_FIXUP (parms);
4301   mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4302                                     finit_identifier_node, parms);
4303   fix_method_argument_names (parms, mdecl);
4304   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4305                        mdecl, NULL_TREE);
4306   DECL_FUNCTION_NAP (mdecl) = count;
4307   start_artificial_method_body (mdecl);
4308
4309   for (current = list; current; current = TREE_CHAIN (current))
4310     java_method_add_stmt (mdecl, 
4311                           build_debugable_stmt (EXPR_WFL_LINECOL (current), 
4312                                                 current));
4313   end_artificial_method_body (mdecl);
4314   return mdecl;
4315 }
4316
4317 static tree
4318 build_instance_initializer (mdecl)
4319      tree mdecl;
4320 {
4321   tree compound = NULL_TREE;
4322   tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4323   tree current;
4324
4325   for (current = stmt_list; current; current = TREE_CHAIN (current))
4326     compound = add_stmt_to_compound (compound, NULL_TREE, current);
4327
4328   return compound;
4329 }
4330
4331 static void
4332 add_instance_initializer (mdecl)
4333      tree mdecl;
4334 {
4335   tree compound = build_instance_initializer (mdecl);
4336
4337   if (compound)
4338     java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4339                                          NULL_TREE, compound));
4340 }
4341
4342 /* Shared accros method_declarator and method_header to remember the
4343    patch stage that was reached during the declaration of the method.
4344    A method DECL is built differently is there is no patch
4345    (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4346    pending on the currently defined method.  */
4347
4348 static int patch_stage;
4349
4350 /* Check the method declaration and add the method to its current
4351    class.  If the argument list is known to contain incomplete types,
4352    the method is partially added and the registration will be resume
4353    once the method arguments resolved. If TYPE is NULL, we're dealing
4354    with a constructor.  */
4355
4356 static tree
4357 method_header (flags, type, mdecl, throws)
4358      int flags;
4359      tree type, mdecl, throws;
4360 {
4361   tree type_wfl = NULL_TREE;
4362   tree meth_name = NULL_TREE;
4363   tree current, orig_arg, this_class = NULL;
4364   tree id, meth;
4365   int saved_lineno;
4366   int constructor_ok = 0, must_chain;
4367   int count;
4368
4369   if (mdecl == error_mark_node)
4370     return error_mark_node;
4371   meth = TREE_VALUE (mdecl);
4372   id = TREE_PURPOSE (mdecl);
4373   
4374   check_modifiers_consistency (flags);
4375
4376   if (GET_CPC ())
4377     this_class = TREE_TYPE (GET_CPC ());
4378
4379   if (!this_class || this_class == error_mark_node)
4380     return NULL_TREE;
4381   
4382   /* There are some forbidden modifiers for an abstract method and its
4383      class must be abstract as well.  */
4384   if (type && (flags & ACC_ABSTRACT))
4385     {
4386       ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4387       ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4388       ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4389       ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4390       ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
4391       if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4392           && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4393         parse_error_context 
4394           (id, "Class `%s' must be declared abstract to define abstract method `%s'", 
4395            IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4396            IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4397     }
4398
4399   /* Things to be checked when declaring a constructor */
4400   if (!type)
4401     {
4402       int ec = java_error_count;
4403       /* 8.6: Constructor declarations: we might be trying to define a
4404          method without specifying a return type. */
4405       if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4406         parse_error_context 
4407           (id, "Invalid method declaration, return type required");
4408       /* 8.6.3: Constructor modifiers */
4409       else
4410         {
4411           JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4412           JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4413           JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4414           JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4415           JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4416         }
4417       /* If we found error here, we don't consider it's OK to tread
4418          the method definition as a constructor, for the rest of this
4419          function */
4420       if (ec == java_error_count)
4421         constructor_ok = 1;
4422     }
4423
4424   /* Method declared within the scope of an interface are implicitly
4425      abstract and public. Conflicts with other erroneously provided
4426      modifiers are checked right after. */
4427
4428   if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4429     {
4430       /* If FLAGS isn't set because of a modifier, turn the
4431          corresponding modifier WFL to NULL so we issue a warning on
4432          the obsolete use of the modifier */
4433       if (!(flags & ACC_PUBLIC))
4434         MODIFIER_WFL (PUBLIC_TK) = NULL;
4435       if (!(flags & ACC_ABSTRACT))
4436         MODIFIER_WFL (ABSTRACT_TK) = NULL;
4437       flags |= ACC_PUBLIC;
4438       flags |= ACC_ABSTRACT;
4439     }
4440
4441   /* Inner class can't declare static methods */
4442   if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4443     {
4444       parse_error_context 
4445         (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4446          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4447          lang_printable_name (this_class, 0));
4448     }
4449
4450   /* Modifiers context reset moved up, so abstract method declaration
4451      modifiers can be later checked.  */
4452
4453   /* Set constructor returned type to void and method name to <init>,
4454      unless we found an error identifier the constructor (in which
4455      case we retain the original name) */
4456   if (!type)
4457     {
4458       type = void_type_node;
4459       if (constructor_ok)
4460         meth_name = init_identifier_node;
4461     }
4462   else
4463     meth_name = EXPR_WFL_NODE (id);
4464
4465   /* Do the returned type resolution and registration if necessary */
4466   SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4467
4468   if (meth_name)
4469     type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4470   EXPR_WFL_NODE (id) = meth_name;
4471   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4472
4473   if (must_chain)
4474     {
4475       patch_stage = JDEP_METHOD_RETURN;
4476       register_incomplete_type (patch_stage, type_wfl, id, type);
4477       TREE_TYPE (meth) = GET_REAL_TYPE (type);
4478     }
4479   else
4480     TREE_TYPE (meth) = type;
4481
4482   saved_lineno = lineno;
4483   /* When defining an abstract or interface method, the curly
4484      bracket at level 1 doesn't exist because there is no function
4485      body */
4486   lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 : 
4487             EXPR_WFL_LINENO (id));
4488
4489   /* Remember the original argument list */
4490   orig_arg = TYPE_ARG_TYPES (meth);
4491
4492   if (patch_stage)              /* includes ret type and/or all args */
4493     {
4494       jdep *jdep;
4495       meth = add_method_1 (this_class, flags, meth_name, meth);
4496       /* Patch for the return type */
4497       if (patch_stage == JDEP_METHOD_RETURN)
4498         {
4499           jdep = CLASSD_LAST (ctxp->classd_list);
4500           JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4501         }
4502       /* This is the stop JDEP. METH allows the function's signature
4503          to be computed. */
4504       register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4505     }
4506   else
4507     meth = add_method (this_class, flags, meth_name, 
4508                        build_java_signature (meth));
4509
4510   /* Remember final parameters */
4511   MARK_FINAL_PARMS (meth, orig_arg);
4512
4513   /* Fix the method argument list so we have the argument name
4514      information */
4515   fix_method_argument_names (orig_arg, meth);
4516
4517   /* Register the parameter number and re-install the current line
4518      number */
4519   DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4520   lineno = saved_lineno;
4521
4522   /* Register exception specified by the `throws' keyword for
4523      resolution and set the method decl appropriate field to the list.
4524      Note: the grammar ensures that what we get here are class
4525      types. */
4526   if (throws)
4527     {
4528       throws = nreverse (throws);
4529       for (current = throws; current; current = TREE_CHAIN (current))
4530         {
4531           register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4532                                     NULL_TREE, NULL_TREE);
4533           JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) = 
4534             &TREE_VALUE (current);
4535         }
4536       DECL_FUNCTION_THROWS (meth) = throws;
4537     }
4538
4539   if (TREE_TYPE (GET_CPC ()) != object_type_node)
4540     DECL_FUNCTION_WFL (meth) = id;
4541
4542   /* Set the flag if we correctly processed a constructor */
4543   if (constructor_ok)
4544     {
4545       DECL_CONSTRUCTOR_P (meth) = 1;
4546       /* Compute and store the number of artificial parameters declared
4547          for this constructor */
4548       for (count = 0, current = TYPE_FIELDS (this_class); current; 
4549            current = TREE_CHAIN (current))
4550         if (FIELD_LOCAL_ALIAS (current))
4551           count++;
4552       DECL_FUNCTION_NAP (meth) = count;
4553     }
4554
4555   /* Eventually set the @deprecated tag flag */
4556   CHECK_DEPRECATED (meth);
4557
4558   /* If doing xref, store column and line number information instead
4559      of the line number only. */
4560   if (flag_emit_xref)
4561     DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4562
4563   return meth;
4564 }
4565
4566 static void
4567 fix_method_argument_names (orig_arg, meth)
4568     tree orig_arg, meth;
4569 {
4570   tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4571   if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4572     {
4573       TREE_PURPOSE (arg) = this_identifier_node;
4574       arg = TREE_CHAIN (arg);
4575     }
4576   while (orig_arg != end_params_node)
4577     {
4578       TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4579       orig_arg = TREE_CHAIN (orig_arg);
4580       arg = TREE_CHAIN (arg);
4581     }
4582 }
4583
4584 /* Complete the method declaration with METHOD_BODY.  */
4585
4586 static void
4587 finish_method_declaration (method_body)
4588      tree method_body;
4589 {
4590   int flags;
4591
4592   if (!current_function_decl)
4593     return;
4594
4595   flags = get_access_flags_from_decl (current_function_decl);
4596
4597   /* 8.4.5 Method Body */
4598   if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4599     {
4600       tree name = DECL_NAME (current_function_decl);
4601       parse_error_context (DECL_FUNCTION_WFL (current_function_decl), 
4602                            "%s method `%s' can't have a body defined",
4603                            (METHOD_NATIVE (current_function_decl) ?
4604                             "Native" : "Abstract"),
4605                            IDENTIFIER_POINTER (name));
4606       method_body = NULL_TREE;
4607     }
4608   else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4609     {
4610       tree name = DECL_NAME (current_function_decl);
4611       parse_error_context
4612         (DECL_FUNCTION_WFL (current_function_decl), 
4613          "Non native and non abstract method `%s' must have a body defined",
4614          IDENTIFIER_POINTER (name));
4615       method_body = NULL_TREE;
4616     }
4617
4618   if (flag_emit_class_files && method_body 
4619       && TREE_CODE (method_body) == NOP_EXPR 
4620       && TREE_TYPE (current_function_decl) 
4621       && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4622     method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4623
4624   BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4625   maybe_absorb_scoping_blocks ();
4626   /* Exit function's body */
4627   exit_block ();
4628   /* Merge last line of the function with first line, directly in the
4629      function decl. It will be used to emit correct debug info. */
4630   if (!flag_emit_xref)
4631     DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4632
4633   /* Since function's argument's list are shared, reset the
4634      ARG_FINAL_P parameter that might have been set on some of this
4635      function parameters. */
4636   UNMARK_FINAL_PARMS (current_function_decl);
4637   
4638   /* So we don't have an irrelevant function declaration context for
4639      the next static block we'll see. */
4640   current_function_decl = NULL_TREE;
4641 }
4642
4643 /* Build a an error message for constructor circularity errors.  */
4644
4645 static char *
4646 constructor_circularity_msg (from, to)
4647      tree from, to;
4648 {
4649   static char string [4096];
4650   char *t = xstrdup (lang_printable_name (from, 0));
4651   sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4652   free (t);
4653   return string;
4654 }
4655
4656 /* Verify a circular call to METH. Return 1 if an error is found, 0
4657    otherwise.  */
4658
4659 static int
4660 verify_constructor_circularity (meth, current)
4661      tree meth, current;
4662 {
4663   static tree list = NULL_TREE;
4664   static int initialized_p;
4665   tree c;
4666
4667   /* If we haven't already registered LIST with the garbage collector,
4668      do so now.  */
4669   if (!initialized_p)
4670     {
4671       ggc_add_tree_root (&list, 1);
4672       initialized_p = 1;
4673     }
4674
4675   for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4676     {
4677       if (TREE_VALUE (c) == meth)
4678         {
4679           char *t;
4680           if (list)
4681             {
4682               tree liste;
4683               list = nreverse (list);
4684               for (liste = list; liste; liste = TREE_CHAIN (liste))
4685                 {
4686                   parse_error_context 
4687                     (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4688                      constructor_circularity_msg
4689                       (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste)))); 
4690                   java_error_count--;
4691                 }
4692             }
4693           t = xstrdup (lang_printable_name (meth, 0));
4694           parse_error_context (TREE_PURPOSE (c), 
4695                                "%s: recursive invocation of constructor `%s'",
4696                                constructor_circularity_msg (current, meth), t);
4697           free (t);
4698           list = NULL_TREE;
4699           return 1;
4700         }
4701     }
4702   for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4703     {
4704       list = tree_cons (c, current, list);
4705       if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4706         return 1;
4707       list = TREE_CHAIN (list);
4708     }
4709   return 0;
4710 }
4711
4712 /* Check modifiers that can be declared but exclusively */
4713
4714 static void
4715 check_modifiers_consistency (flags)
4716      int flags;
4717 {
4718   int acc_count = 0;
4719   tree cl = NULL_TREE;
4720
4721   THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4722   THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4723   THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4724   if (acc_count > 1)
4725     parse_error_context
4726       (cl, "Inconsistent member declaration.  At most one of `public', `private', or `protected' may be specified");
4727
4728   acc_count = 0;
4729   cl = NULL_TREE;
4730   THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4731   THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4732   if (acc_count > 1)
4733     parse_error_context (cl,
4734                          "Inconsistent member declaration.  At most one of `final' or `volatile' may be specified");
4735 }
4736
4737 /* Check the methode header METH for abstract specifics features */
4738
4739 static void
4740 check_abstract_method_header (meth)
4741      tree meth;
4742 {
4743   int flags = get_access_flags_from_decl (meth);
4744
4745   OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4746                               ACC_ABSTRACT, "abstract method",
4747                               IDENTIFIER_POINTER (DECL_NAME (meth)));
4748   OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags, 
4749                               ACC_PUBLIC, "abstract method",
4750                               IDENTIFIER_POINTER (DECL_NAME (meth)));
4751
4752   check_modifiers ("Illegal modifier `%s' for interface method",
4753                   flags, INTERFACE_METHOD_MODIFIERS);
4754 }
4755
4756 /* Create a FUNCTION_TYPE node and start augmenting it with the
4757    declared function arguments. Arguments type that can't be resolved
4758    are left as they are, but the returned node is marked as containing
4759    incomplete types.  */
4760
4761 static tree
4762 method_declarator (id, list)
4763      tree id, list;
4764 {
4765   tree arg_types = NULL_TREE, current, node;
4766   tree meth = make_node (FUNCTION_TYPE);
4767   jdep *jdep;
4768
4769   patch_stage = JDEP_NO_PATCH;
4770
4771   if (GET_CPC () == error_mark_node)
4772     return error_mark_node;
4773
4774   /* If we're dealing with an inner class constructor, we hide the
4775      this$<n> decl in the name field of its parameter declaration.  We
4776      also might have to hide the outer context local alias
4777      initializers. Not done when the class is a toplevel class. */
4778   if (PURE_INNER_CLASS_DECL_P (GET_CPC ()) 
4779       && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4780     {
4781       tree aliases_list, type, thisn;
4782       /* First the aliases, linked to the regular parameters */
4783       aliases_list =
4784         build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION, 
4785                                                 TREE_TYPE (GET_CPC ()),
4786                                                 NULL_TREE, NULL);
4787       list = chainon (nreverse (aliases_list), list);
4788
4789       /* Then this$<n> */
4790       type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4791       thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4792       list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4793                         list);
4794     }
4795   
4796   for (current = list; current; current = TREE_CHAIN (current))
4797     {
4798       int must_chain = 0;
4799       tree wfl_name = TREE_PURPOSE (current);
4800       tree type = TREE_VALUE (current);
4801       tree name = EXPR_WFL_NODE (wfl_name);
4802       tree already, arg_node;
4803       tree type_wfl = NULL_TREE;
4804       tree real_type;
4805
4806       /* Obtain a suitable type for resolution, if necessary */
4807       SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4808
4809       /* Process NAME, as it may specify extra dimension(s) for it */
4810       type = build_array_from_name (type, type_wfl, name, &name);
4811       EXPR_WFL_NODE (wfl_name) = name;
4812
4813       real_type = GET_REAL_TYPE (type);
4814       if (TREE_CODE (real_type) == RECORD_TYPE)
4815         {
4816           real_type = promote_type (real_type);
4817           if (TREE_CODE (type) == TREE_LIST)
4818             TREE_PURPOSE (type) = real_type;
4819         }
4820
4821       /* Check redefinition */
4822       for (already = arg_types; already; already = TREE_CHAIN (already))
4823         if (TREE_PURPOSE (already) == name)
4824           {
4825             parse_error_context
4826               (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4827                IDENTIFIER_POINTER (name),
4828                IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4829             break;
4830           }
4831
4832       /* If we've an incomplete argument type, we know there is a location
4833          to patch when the type get resolved, later.  */
4834       jdep = NULL;
4835       if (must_chain)
4836         {
4837           patch_stage = JDEP_METHOD;
4838           type = register_incomplete_type (patch_stage, 
4839                                            type_wfl, wfl_name, type);
4840           jdep = CLASSD_LAST (ctxp->classd_list);
4841           JDEP_MISC (jdep) = id;
4842         }
4843
4844       /* The argument node: a name and a (possibly) incomplete type.  */
4845       arg_node = build_tree_list (name, real_type);
4846       /* Remeber arguments declared final. */
4847       ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4848       
4849       if (jdep)
4850         JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4851       TREE_CHAIN (arg_node) = arg_types;
4852       arg_types = arg_node;
4853     }
4854   TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
4855   node = build_tree_list (id, meth);
4856   return node;
4857 }
4858
4859 static int
4860 unresolved_type_p (wfl, returned)
4861      tree wfl;
4862      tree *returned;
4863      
4864 {
4865   if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4866     {
4867       if (returned)
4868         {
4869           tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4870           if (decl && current_class && (decl == TYPE_NAME (current_class)))
4871             *returned = TREE_TYPE (decl);
4872           else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4873             *returned = TREE_TYPE (GET_CPC ());
4874           else
4875             *returned = NULL_TREE;
4876         }
4877       return 1;
4878     }
4879   if (returned)
4880     *returned = wfl;
4881   return 0;
4882 }
4883
4884 /* From NAME, build a qualified identifier node using the
4885    qualification from the current package definition. */
4886
4887 static tree
4888 parser_qualified_classname (name)
4889      tree name;
4890 {
4891   tree nested_class_name;
4892
4893   if ((nested_class_name = maybe_make_nested_class_name (name)))
4894     return nested_class_name;
4895
4896   if (ctxp->package)
4897     return merge_qualified_name (ctxp->package, name);
4898   else 
4899     return name;
4900 }
4901
4902 /* Called once the type a interface extends is resolved. Returns 0 if
4903    everything is OK.  */
4904
4905 static int
4906 parser_check_super_interface (super_decl, this_decl, this_wfl)
4907      tree super_decl, this_decl, this_wfl;
4908 {
4909   tree super_type = TREE_TYPE (super_decl);
4910
4911   /* Has to be an interface */
4912   if (!CLASS_INTERFACE (super_decl))
4913     {
4914       parse_error_context 
4915         (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4916          (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4917          IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4918          (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ? 
4919           "interface" : "class"),
4920          IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4921       return 1;
4922     }
4923
4924   /* Check top-level interface access. Inner classes are subject to member 
4925      access rules (6.6.1). */
4926   if (! INNER_CLASS_P (super_type)
4927       && check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4928     return 1;
4929
4930   SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4931                           IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4932                           IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4933   return 0;
4934 }
4935
4936 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4937    0 if everthing is OK.  */
4938
4939 static int
4940 parser_check_super (super_decl, this_decl, wfl)
4941      tree super_decl, this_decl, wfl;
4942 {
4943   tree super_type = TREE_TYPE (super_decl);
4944
4945   /* SUPER should be a CLASS (neither an array nor an interface) */
4946   if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4947     {
4948       parse_error_context 
4949         (wfl, "Class `%s' can't subclass %s `%s'",
4950          IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4951          (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4952          IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4953       return 1;
4954     }
4955
4956   if (CLASS_FINAL (TYPE_NAME (super_type)))
4957     {
4958       parse_error_context (wfl, "Can't subclass final classes: %s",
4959                            IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4960       return 1;
4961     }
4962
4963   /* Check top-level class scope. Inner classes are subject to member access
4964      rules (6.6.1). */
4965   if (! INNER_CLASS_P (super_type)
4966       && (check_pkg_class_access (DECL_NAME (super_decl), wfl)))
4967     return 1;
4968   
4969   SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4970                           IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4971                           IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4972   return 0;
4973 }
4974
4975 /* Create a new dependency list and link it (in a LIFO manner) to the
4976    CTXP list of type dependency list.  */
4977
4978 static void
4979 create_jdep_list (ctxp)
4980      struct parser_ctxt *ctxp;
4981 {
4982   jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));      
4983   new->first = new->last = NULL;
4984   new->next = ctxp->classd_list;
4985   ctxp->classd_list = new;
4986 }
4987
4988 static jdeplist *
4989 reverse_jdep_list (ctxp)
4990      struct parser_ctxt *ctxp;
4991 {
4992   register jdeplist *prev = NULL, *current, *next;
4993   for (current = ctxp->classd_list; current; current = next)
4994     {
4995       next = current->next;
4996       current->next = prev;
4997       prev = current;
4998     }
4999   return prev;
5000 }
5001
5002 /* Create a fake pointer based on the ID stored in
5003    TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5004    registered again. */
5005
5006 static tree
5007 obtain_incomplete_type (type_name)
5008      tree type_name;
5009 {
5010   tree ptr = NULL_TREE, name;
5011
5012   if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5013     name = EXPR_WFL_NODE (type_name);
5014   else if (INCOMPLETE_TYPE_P (type_name))
5015     name = TYPE_NAME (type_name);
5016   else
5017     abort ();
5018
5019   BUILD_PTR_FROM_NAME (ptr, name);
5020   layout_type (ptr);
5021
5022   return ptr;
5023 }
5024
5025 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5026    non NULL instead of computing a new fake type based on WFL. The new
5027    dependency is inserted in the current type dependency list, in FIFO
5028    manner.  */
5029
5030 static tree
5031 register_incomplete_type (kind, wfl, decl, ptr)
5032      int kind;
5033      tree wfl, decl, ptr;
5034 {
5035   jdep *new = (jdep *)xmalloc (sizeof (jdep));
5036
5037   if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5038     ptr = obtain_incomplete_type (wfl);
5039
5040   JDEP_KIND (new) = kind;
5041   JDEP_DECL (new) = decl;
5042   JDEP_TO_RESOLVE (new) = ptr;
5043   JDEP_WFL (new) = wfl;
5044   JDEP_CHAIN (new) = NULL;
5045   JDEP_MISC (new) = NULL_TREE;
5046   /* For some dependencies, set the enclosing class of the current
5047      class to be the enclosing context */
5048   if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE 
5049        || kind == JDEP_ANONYMOUS || kind == JDEP_FIELD)
5050       && GET_ENCLOSING_CPC ())
5051     JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5052   else
5053     JDEP_ENCLOSING (new) = GET_CPC ();
5054   JDEP_GET_PATCH (new) = (tree *)NULL;
5055
5056   JDEP_INSERT (ctxp->classd_list, new);
5057
5058   return ptr;
5059 }
5060
5061 void
5062 java_check_circular_reference ()
5063 {
5064   tree current;
5065   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5066     {
5067       tree type = TREE_TYPE (current);
5068       if (CLASS_INTERFACE (current))
5069         {
5070           /* Check all interfaces this class extends */
5071           tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5072           int n, i;
5073
5074           if (!basetype_vec)
5075             return;
5076           n = TREE_VEC_LENGTH (basetype_vec);
5077           for (i = 0; i < n; i++)
5078             {
5079               tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5080               if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node 
5081                   && interface_of_p (type, BINFO_TYPE (vec_elt)))
5082                 parse_error_context (lookup_cl (current),
5083                                      "Cyclic interface inheritance");
5084             }
5085         }
5086       else
5087         if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5088           parse_error_context (lookup_cl (current), 
5089                                "Cyclic class inheritance%s",
5090                                (cyclic_inheritance_report ?
5091                                 cyclic_inheritance_report : ""));
5092     }
5093 }
5094
5095 /* Augment the parameter list PARM with parameters crafted to
5096    initialize outer context locals aliases. Through ARTIFICIAL, a
5097    count is kept of the number of crafted parameters. MODE governs
5098    what eventually gets created: something suitable for a function
5099    creation or a function invocation, either the constructor or
5100    finit$.  */
5101
5102 static tree
5103 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5104     int mode;
5105     tree class_type, parm;
5106     int *artificial;
5107 {
5108   tree field;
5109   tree additional_parms = NULL_TREE;
5110
5111   for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5112     if (FIELD_LOCAL_ALIAS (field))
5113       {
5114         const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5115         tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5116         tree mangled_id;
5117
5118         switch (mode)
5119           {
5120           case AIPL_FUNCTION_DECLARATION:
5121             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id, 
5122                                                          &buffer [4]);
5123             purpose = build_wfl_node (mangled_id);
5124             if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5125               value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5126             else
5127               value = TREE_TYPE (field);
5128             break;
5129
5130           case AIPL_FUNCTION_CREATION:
5131             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5132                                                          &buffer [4]);
5133             value = TREE_TYPE (field);
5134             break;
5135
5136           case AIPL_FUNCTION_FINIT_INVOCATION:
5137             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id, 
5138                                                          &buffer [4]);
5139             /* Now, this is wrong. purpose should always be the NAME
5140                of something and value its matching value (decl, type,
5141                etc...) FIXME -- but there is a lot to fix. */
5142
5143             /* When invoked for this kind of operation, we already
5144                know whether a field is used or not. */
5145             purpose = TREE_TYPE (field);
5146             value = build_wfl_node (mangled_id);
5147             break;
5148
5149           case AIPL_FUNCTION_CTOR_INVOCATION:
5150             /* There are two case: the constructor invokation happends
5151                outside the local inner, in which case, locales from the outer
5152                context are directly used.
5153
5154                Otherwise, we fold to using the alias directly. */
5155             if (class_type == current_class)
5156               value = field;
5157             else
5158               {
5159                 name = get_identifier (&buffer[4]);
5160                 value = IDENTIFIER_LOCAL_VALUE (name);
5161               }
5162             break;
5163           }
5164         additional_parms = tree_cons (purpose, value, additional_parms);
5165         if (artificial)
5166           *artificial +=1;
5167       }
5168   if (additional_parms)
5169     {
5170       if (ANONYMOUS_CLASS_P (class_type) 
5171           && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5172         additional_parms = nreverse (additional_parms);
5173       parm = chainon (additional_parms, parm);
5174     }
5175
5176    return parm;
5177 }
5178
5179 /* Craft a constructor for CLASS_DECL -- what we should do when none
5180    where found. ARGS is non NULL when a special signature must be
5181    enforced. This is the case for anonymous classes.  */
5182
5183 static void
5184 craft_constructor (class_decl, args)
5185      tree class_decl, args;
5186 {
5187   tree class_type = TREE_TYPE (class_decl);
5188   tree parm = NULL_TREE;
5189   int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5190                ACC_PUBLIC : 0);
5191   int i = 0, artificial = 0;
5192   tree decl, ctor_name;
5193   char buffer [80];
5194   
5195   /* The constructor name is <init> unless we're dealing with an
5196      anonymous class, in which case the name will be fixed after having
5197      be expanded. */
5198   if (ANONYMOUS_CLASS_P (class_type))
5199     ctor_name = DECL_NAME (class_decl);
5200   else
5201     ctor_name = init_identifier_node;
5202
5203   /* If we're dealing with an inner class constructor, we hide the
5204      this$<n> decl in the name field of its parameter declaration. */
5205   if (PURE_INNER_CLASS_TYPE_P (class_type))
5206     {
5207       tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5208       parm = tree_cons (build_current_thisn (class_type),
5209                         build_pointer_type (type), parm);
5210
5211       /* Some more arguments to be hidden here. The values of the local
5212          variables of the outer context that the inner class needs to see. */
5213       parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5214                                                      class_type, parm, 
5215                                                      &artificial);
5216     }
5217
5218   /* Then if there are any args to be enforced, enforce them now */
5219   for (; args && args != end_params_node; args = TREE_CHAIN (args))
5220     {
5221       sprintf (buffer, "parm%d", i++);
5222       parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5223     }
5224
5225   CRAFTED_PARAM_LIST_FIXUP (parm);
5226   decl = create_artificial_method (class_type, flags, void_type_node, 
5227                                    ctor_name, parm);
5228   fix_method_argument_names (parm, decl);
5229   /* Now, mark the artificial parameters. */
5230   DECL_FUNCTION_NAP (decl) = artificial;
5231   DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5232 }
5233
5234
5235 /* Fix the constructors. This will be called right after circular
5236    references have been checked. It is necessary to fix constructors
5237    early even if no code generation will take place for that class:
5238    some generated constructor might be required by the class whose
5239    compilation triggered this one to be simply loaded.  */
5240
5241 void
5242 java_fix_constructors ()
5243 {
5244   tree current;
5245
5246   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5247     {
5248       tree class_type = TREE_TYPE (current);
5249       int saw_ctor = 0;
5250       tree decl;
5251
5252       if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5253         continue;
5254
5255       current_class = class_type;
5256       for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5257         {
5258           if (DECL_CONSTRUCTOR_P (decl))
5259             {
5260               fix_constructors (decl);
5261               saw_ctor = 1;
5262             }
5263         }
5264
5265       /* Anonymous class constructor can't be generated that early. */
5266       if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5267         craft_constructor (current, NULL_TREE);
5268     }
5269 }
5270
5271 /* safe_layout_class just makes sure that we can load a class without
5272    disrupting the current_class, input_file, lineno, etc, information
5273    about the class processed currently.  */
5274
5275 void
5276 safe_layout_class (class)
5277      tree class;
5278 {
5279   tree save_current_class = current_class;
5280   const char *save_input_filename = input_filename;
5281   int save_lineno = lineno;
5282
5283   layout_class (class);
5284
5285   current_class = save_current_class;
5286   input_filename = save_input_filename;
5287   lineno = save_lineno;
5288 }
5289
5290 static tree
5291 jdep_resolve_class (dep)
5292      jdep *dep;
5293 {
5294   tree decl;
5295
5296   if (JDEP_RESOLVED_P (dep))
5297     decl = JDEP_RESOLVED_DECL (dep);
5298   else
5299     {
5300       decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5301                             JDEP_DECL (dep), JDEP_WFL (dep));
5302       JDEP_RESOLVED (dep, decl);
5303     }
5304     
5305   if (!decl)
5306     complete_class_report_errors (dep);
5307   else if (PURE_INNER_CLASS_DECL_P (decl))
5308     {
5309       tree inner = TREE_TYPE (decl);
5310       if (! CLASS_LOADED_P (inner))
5311         {
5312           safe_layout_class (inner);
5313           if (TYPE_SIZE (inner) == error_mark_node)
5314             TYPE_SIZE (inner) = NULL_TREE;
5315         }
5316       check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5317     }
5318   return decl;
5319 }
5320
5321 /* Complete unsatisfied class declaration and their dependencies */
5322
5323 void
5324 java_complete_class ()
5325 {
5326   tree cclass;
5327   jdeplist *cclassd;
5328   int error_found;
5329   tree type;
5330
5331   /* Process imports */
5332   process_imports ();
5333
5334   /* Rever things so we have the right order */
5335   ctxp->class_list = nreverse (ctxp->class_list);
5336   ctxp->classd_list = reverse_jdep_list (ctxp);
5337
5338   for (cclassd = ctxp->classd_list, cclass = ctxp->class_list; 
5339        cclass && cclassd; 
5340        cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5341     {
5342       jdep *dep;
5343       for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5344         {
5345           tree decl;
5346           if (!(decl = jdep_resolve_class (dep)))
5347             continue;
5348
5349           /* Now it's time to patch */
5350           switch (JDEP_KIND (dep))
5351             {
5352             case JDEP_SUPER:
5353               /* Simply patch super */
5354               if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5355                 continue;
5356               BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO 
5357                 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5358               break;
5359
5360             case JDEP_FIELD:
5361               {
5362                 /* We do part of the job done in add_field */
5363                 tree field_decl = JDEP_DECL (dep);
5364                 tree field_type = TREE_TYPE (decl);
5365                 if (TREE_CODE (field_type) == RECORD_TYPE)
5366                   field_type = promote_type (field_type);
5367                 TREE_TYPE (field_decl) = field_type;
5368                 DECL_ALIGN (field_decl) = 0;
5369                 DECL_USER_ALIGN (field_decl) = 0;
5370                 layout_decl (field_decl, 0);
5371                 SOURCE_FRONTEND_DEBUG 
5372                   (("Completed field/var decl `%s' with `%s'",
5373                     IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5374                     IDENTIFIER_POINTER (DECL_NAME (decl))));
5375                 break;
5376               }
5377             case JDEP_METHOD:   /* We start patching a method */
5378             case JDEP_METHOD_RETURN:
5379               error_found = 0;
5380               while (1)
5381                 {
5382                   if (decl)
5383                     {
5384                       type = TREE_TYPE(decl);
5385                       if (TREE_CODE (type) == RECORD_TYPE)
5386                         type = promote_type (type);
5387                       JDEP_APPLY_PATCH (dep, type);
5388                       SOURCE_FRONTEND_DEBUG 
5389                         (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5390                            "Completing fct `%s' with ret type `%s'":
5391                            "Completing arg `%s' with type `%s'"),
5392                           IDENTIFIER_POINTER (EXPR_WFL_NODE 
5393                                               (JDEP_DECL_WFL (dep))),
5394                           IDENTIFIER_POINTER (DECL_NAME (decl))));
5395                     }
5396                   else
5397                     error_found = 1;
5398                   dep = JDEP_CHAIN (dep);
5399                   if (JDEP_KIND (dep) == JDEP_METHOD_END)
5400                     break;
5401                   else
5402                     decl = jdep_resolve_class (dep);
5403                 }
5404               if (!error_found)
5405                 {
5406                   tree mdecl = JDEP_DECL (dep), signature;
5407                   /* Recompute and reset the signature, check first that
5408                      all types are now defined. If they're not,
5409                      dont build the signature. */
5410                   if (check_method_types_complete (mdecl))
5411                     {
5412                       signature = build_java_signature (TREE_TYPE (mdecl));
5413                       set_java_signature (TREE_TYPE (mdecl), signature);
5414                     }
5415                 }
5416               else
5417                 continue;
5418               break;
5419
5420             case JDEP_INTERFACE:
5421               if (parser_check_super_interface (decl, JDEP_DECL (dep),
5422                                                 JDEP_WFL (dep)))
5423                 continue;
5424               parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5425               break;
5426
5427             case JDEP_PARM:
5428             case JDEP_VARIABLE:
5429               type = TREE_TYPE(decl);
5430               if (TREE_CODE (type) == RECORD_TYPE)
5431                 type = promote_type (type);
5432               JDEP_APPLY_PATCH (dep, type);
5433               break;
5434
5435             case JDEP_TYPE:
5436               JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5437               SOURCE_FRONTEND_DEBUG 
5438                 (("Completing a random type dependency on a '%s' node",
5439                   tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5440               break;
5441
5442             case JDEP_EXCEPTION:
5443               JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5444               SOURCE_FRONTEND_DEBUG 
5445                 (("Completing `%s' `throws' argument node",
5446                   IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5447               break;
5448
5449             case JDEP_ANONYMOUS:
5450               patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5451               break;
5452
5453             default:
5454               abort ();
5455             }
5456         }
5457     }
5458   return;
5459 }
5460
5461 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5462    array.  */
5463
5464 static tree
5465 resolve_class (enclosing, class_type, decl, cl)
5466      tree enclosing, class_type, decl, cl;
5467 {
5468   const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5469   const char *base = name;
5470   tree resolved_type = TREE_TYPE (class_type);
5471   tree resolved_type_decl;
5472   
5473   if (resolved_type != NULL_TREE)
5474     {
5475       tree resolved_type_decl = TYPE_NAME (resolved_type);
5476       if (resolved_type_decl == NULL_TREE
5477           || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5478         {
5479           resolved_type_decl = build_decl (TYPE_DECL,
5480                                            TYPE_NAME (class_type),
5481                                            resolved_type);
5482         }
5483       return resolved_type_decl;
5484     }
5485
5486   /* 1- Check to see if we have an array. If true, find what we really
5487      want to resolve  */
5488   while (name[0] == '[')
5489     name++;
5490   if (base != name)
5491     {
5492       TYPE_NAME (class_type) = get_identifier (name);
5493       WFL_STRIP_BRACKET (cl, cl);
5494     }
5495
5496   /* 2- Resolve the bare type */
5497   if (!(resolved_type_decl = do_resolve_class (enclosing, class_type, 
5498                                                decl, cl)))
5499     return NULL_TREE;
5500   resolved_type = TREE_TYPE (resolved_type_decl);
5501
5502   /* 3- If we have and array, reconstruct the array down to its nesting */
5503   if (base != name)
5504     {
5505       while (base != name)
5506         {
5507           resolved_type = build_java_array_type (resolved_type, -1);
5508           name--;
5509         }
5510       /* A TYPE_NAME that is a TYPE_DECL was set in
5511          build_java_array_type, return it. */
5512       resolved_type_decl = TYPE_NAME (resolved_type);
5513     }
5514   TREE_TYPE (class_type) = resolved_type;
5515   return resolved_type_decl;
5516 }
5517
5518 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5519    are used to report error messages.  */
5520
5521 tree
5522 do_resolve_class (enclosing, class_type, decl, cl)
5523      tree enclosing, class_type, decl, cl;
5524 {
5525   tree new_class_decl, super, start;
5526
5527   /* Do not try to replace TYPE_NAME (class_type) by a variable, since
5528      it is changed by find_in_imports{_on_demand} and (but it doesn't
5529      really matter) qualify_and_find */
5530
5531   /* 0- Search in the current class as an inner class */
5532   start = enclosing;
5533
5534   /* Maybe some code here should be added to load the class or
5535      something, at least if the class isn't an inner class and ended
5536      being loaded from class file. FIXME. */
5537   while (enclosing)
5538     {
5539       tree intermediate;
5540
5541       if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5542         return new_class_decl;
5543
5544       intermediate = enclosing;
5545       /* Explore enclosing contexts. */
5546       while (INNER_CLASS_DECL_P (intermediate))
5547         {
5548           intermediate = DECL_CONTEXT (intermediate);
5549           if ((new_class_decl = find_as_inner_class (intermediate, 
5550                                                      class_type, cl)))
5551             return new_class_decl;
5552         }
5553
5554       /* Now go to the upper classes, bail out if necessary. */
5555       super = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5556       if (!super || super == object_type_node)
5557         break;
5558
5559       if (TREE_CODE (super) == POINTER_TYPE)
5560         super = do_resolve_class (NULL, super, NULL, NULL);
5561       else
5562         super = TYPE_NAME (super);
5563  
5564       /* We may not have checked for circular inheritance yet, so do so
5565          here to prevent an infinite loop. */
5566       if (super == start)
5567         {
5568           if (!cl)
5569             cl = lookup_cl (decl);
5570           
5571           parse_error_context
5572             (cl, "Cyclic inheritance involving %s",
5573              IDENTIFIER_POINTER (DECL_NAME (enclosing)));
5574           break;
5575         }
5576       enclosing = super;
5577     }
5578
5579   /* 1- Check for the type in single imports. This will change
5580      TYPE_NAME() if something relevant is found */
5581   find_in_imports (class_type);
5582
5583   /* 2- And check for the type in the current compilation unit */
5584   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5585     {
5586       if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5587           !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5588         load_class (TYPE_NAME (class_type), 0);
5589       return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5590     }
5591
5592   /* 3- Search according to the current package definition */
5593   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5594     {
5595       if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5596                                              TYPE_NAME (class_type))))
5597         return new_class_decl;
5598     }
5599
5600   /* 4- Check the import on demands. Don't allow bar.baz to be
5601      imported from foo.* */
5602   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5603     if (find_in_imports_on_demand (class_type))
5604       return NULL_TREE;
5605
5606   /* If found in find_in_imports_on_demant, the type has already been
5607      loaded. */
5608   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5609     return new_class_decl;
5610
5611   /* 5- Try with a name qualified with the package name we've seen so far */
5612   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5613     {
5614       tree package;
5615
5616       /* If there is a current package (ctxp->package), it's the first
5617          element of package_list and we can skip it. */
5618       for (package = (ctxp->package ? 
5619                       TREE_CHAIN (package_list) : package_list);
5620            package; package = TREE_CHAIN (package))
5621         if ((new_class_decl = qualify_and_find (class_type,
5622                                                TREE_PURPOSE (package), 
5623                                                TYPE_NAME (class_type))))
5624           return new_class_decl;
5625     }
5626
5627   /* 5- Check an other compilation unit that bears the name of type */
5628   load_class (TYPE_NAME (class_type), 0);
5629   
5630   if (!cl)
5631     cl = lookup_cl (decl);
5632   
5633   /* If we don't have a value for CL, then we're being called recursively. 
5634      We can't check package access just yet, but it will be taken care of
5635      by the caller. */
5636   if (cl)
5637     {
5638       if (check_pkg_class_access (TYPE_NAME (class_type), cl))
5639         return NULL_TREE;
5640     }
5641   
5642   /* 6- Last call for a resolution */
5643   return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5644 }
5645
5646 static tree
5647 qualify_and_find (class_type, package, name)
5648      tree class_type, package, name;
5649 {
5650   tree new_qualified = merge_qualified_name (package, name);
5651   tree new_class_decl;
5652
5653   if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5654     load_class (new_qualified, 0);
5655   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5656     {
5657       if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5658           !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5659         load_class (new_qualified, 0);
5660       TYPE_NAME (class_type) = new_qualified;
5661       return IDENTIFIER_CLASS_VALUE (new_qualified);
5662     }
5663   return NULL_TREE;
5664 }
5665
5666 /* Resolve NAME and lay it out (if not done and if not the current
5667    parsed class). Return a decl node. This function is meant to be
5668    called when type resolution is necessary during the walk pass.  */
5669
5670 static tree
5671 resolve_and_layout (something, cl)
5672      tree something;
5673      tree cl;
5674 {
5675   tree decl, decl_type;
5676
5677   /* Don't do that on the current class */
5678   if (something == current_class)
5679     return TYPE_NAME (current_class);
5680
5681   /* Don't do anything for void and other primitive types */
5682   if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5683     return NULL_TREE;
5684
5685   /* Pointer types can be reall pointer types or fake pointers. When
5686      finding a real pointer, recheck for primitive types */
5687   if (TREE_CODE (something) == POINTER_TYPE)
5688     {
5689       if (TREE_TYPE (something))
5690         {
5691           something = TREE_TYPE (something);
5692           if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5693             return NULL_TREE;
5694         }
5695       else
5696         something = TYPE_NAME (something);
5697     }
5698
5699   /* Don't do anything for arrays of primitive types */
5700   if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5701       && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5702     return NULL_TREE;
5703
5704   /* Something might be a WFL */
5705   if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5706     something = EXPR_WFL_NODE (something);
5707
5708   /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5709      TYPE_DECL or a real TYPE */
5710   else if (TREE_CODE (something) != IDENTIFIER_NODE)
5711     something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5712             DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5713
5714   if (!(decl = resolve_no_layout (something, cl)))
5715     return NULL_TREE;
5716
5717   /* Resolve and layout if necessary */
5718   decl_type = TREE_TYPE (decl);
5719   layout_class_methods (decl_type);
5720   /* Check methods */
5721   if (CLASS_FROM_SOURCE_P (decl_type))
5722     java_check_methods (decl);
5723   /* Layout the type if necessary */ 
5724   if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5725     safe_layout_class (decl_type);
5726
5727   return decl;
5728 }
5729
5730 /* Resolve a class, returns its decl but doesn't perform any
5731    layout. The current parsing context is saved and restored */
5732
5733 static tree
5734 resolve_no_layout (name, cl)
5735      tree name, cl;
5736 {
5737   tree ptr, decl;
5738   BUILD_PTR_FROM_NAME (ptr, name);
5739   java_parser_context_save_global ();
5740   decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5741   java_parser_context_restore_global ();
5742   
5743   return decl;
5744 }
5745
5746 /* Called when reporting errors. Skip leader '[' in a complex array
5747    type description that failed to be resolved.  */
5748
5749 static const char *
5750 purify_type_name (name)
5751      const char *name;
5752 {
5753   while (*name && *name == '[')
5754     name++;
5755   return name;
5756 }
5757
5758 /* The type CURRENT refers to can't be found. We print error messages.  */
5759
5760 static void
5761 complete_class_report_errors (dep)
5762      jdep *dep;
5763 {
5764   const char *name;
5765
5766   if (!JDEP_WFL (dep))
5767     return;
5768
5769   name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
5770   switch (JDEP_KIND (dep))
5771     {
5772     case JDEP_SUPER:
5773       parse_error_context  
5774         (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
5775          purify_type_name (name),
5776          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5777       break;
5778     case JDEP_FIELD:
5779       parse_error_context
5780         (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
5781          purify_type_name (name),
5782          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5783       break;
5784     case JDEP_METHOD:           /* Covers arguments */
5785       parse_error_context
5786         (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
5787          purify_type_name (name),
5788          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5789          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5790       break;
5791     case JDEP_METHOD_RETURN:    /* Covers return type */
5792       parse_error_context
5793         (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'", 
5794          purify_type_name (name),
5795          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5796       break;
5797     case JDEP_INTERFACE:
5798       parse_error_context
5799         (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5800          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5801          (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5802          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5803       break;
5804     case JDEP_VARIABLE:
5805       parse_error_context
5806         (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'", 
5807          purify_type_name (IDENTIFIER_POINTER 
5808                            (EXPR_WFL_NODE (JDEP_WFL (dep)))),
5809          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5810       break;
5811     case JDEP_EXCEPTION:        /* As specified by `throws' */
5812       parse_error_context 
5813           (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5814          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5815       break;
5816     default:
5817       /* Fix for -Wall. Just break doing nothing. The error will be
5818          caught later */
5819       break;
5820     }
5821 }
5822
5823 /* Return a static string containing the DECL prototype string. If
5824    DECL is a constructor, use the class name instead of the form
5825    <init> */
5826
5827 static const char *
5828 get_printable_method_name (decl)
5829      tree decl;
5830 {
5831   const char *to_return;
5832   tree name = NULL_TREE;
5833
5834   if (DECL_CONSTRUCTOR_P (decl))
5835     {
5836       name = DECL_NAME (decl);
5837       DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
5838     }
5839       
5840   to_return = lang_printable_name (decl, 0);
5841   if (DECL_CONSTRUCTOR_P (decl))
5842     DECL_NAME (decl) = name;
5843   
5844   return to_return;
5845 }
5846
5847 /* Track method being redefined inside the same class. As a side
5848    effect, set DECL_NAME to an IDENTIFIER (prior entering this
5849    function it's a FWL, so we can track errors more accurately.)  */
5850
5851 static int
5852 check_method_redefinition (class, method)
5853      tree class, method;
5854 {
5855   tree redef, sig;
5856
5857   /* There's no need to verify <clinit> and finit$ */
5858   if (DECL_CLINIT_P (method) || DECL_FINIT_P (method))
5859     return 0;
5860
5861   sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
5862   for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5863     {
5864       if (redef == method)
5865         break;
5866       if (DECL_NAME (redef) == DECL_NAME (method)
5867           && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
5868           && !DECL_ARTIFICIAL (method))
5869         {
5870           parse_error_context 
5871             (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
5872              (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5873              get_printable_method_name (redef));
5874           return 1;
5875         }
5876     }
5877   return 0;
5878 }
5879
5880 /* Return 1 if check went ok, 0 otherwise.  */
5881 static int
5882 check_abstract_method_definitions (do_interface, class_decl, type)
5883      int do_interface;
5884      tree class_decl, type;
5885 {
5886   tree class = TREE_TYPE (class_decl);
5887   tree method, end_type;
5888   int ok = 1;
5889
5890   end_type = (do_interface ? object_type_node : type);
5891   for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5892     {
5893       tree other_super, other_method, method_sig, method_name;
5894       int found = 0;
5895       int end_type_reached = 0;
5896       
5897       if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5898         continue;
5899       
5900       /* Now verify that somewhere in between TYPE and CLASS,
5901          abstract method METHOD gets a non abstract definition
5902          that is inherited by CLASS.  */
5903       
5904       method_sig = build_java_signature (TREE_TYPE (method));
5905       method_name = DECL_NAME (method);
5906       if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5907         method_name = EXPR_WFL_NODE (method_name);
5908
5909       other_super = class;
5910       do {
5911         if (other_super == end_type)
5912           end_type_reached = 1;
5913         
5914         /* Method search */
5915         for (other_method = TYPE_METHODS (other_super); other_method;
5916             other_method = TREE_CHAIN (other_method))
5917           {
5918             tree s = build_java_signature (TREE_TYPE (other_method));
5919             tree other_name = DECL_NAME (other_method);
5920             
5921             if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5922               other_name = EXPR_WFL_NODE (other_name);
5923             if (!DECL_CLINIT_P (other_method)
5924                 && !DECL_CONSTRUCTOR_P (other_method)
5925                 && method_name == other_name
5926                 && method_sig == s
5927                 && !METHOD_ABSTRACT (other_method))
5928              {
5929                found = 1;
5930                break;
5931              }
5932           }
5933         other_super = CLASSTYPE_SUPER (other_super);
5934       } while (!end_type_reached);
5935  
5936       /* Report that abstract METHOD didn't find an implementation
5937          that CLASS can use. */
5938       if (!found)
5939         {
5940           char *t = xstrdup (lang_printable_name 
5941                             (TREE_TYPE (TREE_TYPE (method)), 0));
5942           tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5943           
5944           parse_error_context 
5945             (lookup_cl (class_decl),
5946              "Class `%s' doesn't define the abstract method `%s %s' from %s `%s'. This method must be defined or %s `%s' must be declared abstract",
5947              IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5948              t, lang_printable_name (method, 0), 
5949              (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ? 
5950               "interface" : "class"),
5951              IDENTIFIER_POINTER (ccn),
5952              (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5953              IDENTIFIER_POINTER (DECL_NAME (class_decl)));
5954           ok = 0;
5955           free (t);
5956         }
5957     }
5958
5959   if (ok && do_interface)
5960     {
5961       /* Check for implemented interfaces. */
5962       int i;
5963       tree vector = TYPE_BINFO_BASETYPES (type);
5964       for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5965         {
5966           tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5967           ok = check_abstract_method_definitions (1, class_decl, super);
5968         }
5969     }
5970
5971   return ok;
5972 }
5973
5974 /* Check that CLASS_DECL somehow implements all inherited abstract
5975    methods.  */
5976
5977 static void
5978 java_check_abstract_method_definitions (class_decl)
5979      tree class_decl;
5980 {
5981   tree class = TREE_TYPE (class_decl);
5982   tree super, vector;
5983   int i;
5984
5985   if (CLASS_ABSTRACT (class_decl))
5986     return;
5987
5988   /* Check for inherited types */
5989   super = class;
5990   do {
5991     super = CLASSTYPE_SUPER (super);
5992     check_abstract_method_definitions (0, class_decl, super);
5993   } while (super != object_type_node);
5994
5995   /* Check for implemented interfaces. */
5996   vector = TYPE_BINFO_BASETYPES (class);
5997   for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5998     {
5999       super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6000       check_abstract_method_definitions (1, class_decl, super);
6001     }
6002 }
6003
6004 /* Check all the types method DECL uses and return 1 if all of them
6005    are now complete, 0 otherwise. This is used to check whether its
6006    safe to build a method signature or not.  */
6007
6008 static int
6009 check_method_types_complete (decl)
6010      tree decl;
6011 {
6012   tree type = TREE_TYPE (decl);
6013   tree args;
6014
6015   if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6016     return 0;
6017   
6018   args = TYPE_ARG_TYPES (type);
6019   if (TREE_CODE (type) == METHOD_TYPE)
6020     args = TREE_CHAIN (args);
6021   for (; args != end_params_node; args = TREE_CHAIN (args))
6022     if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6023       return 0;
6024
6025   return 1;
6026 }
6027
6028 /* Visible interface to check methods contained in CLASS_DECL */
6029
6030 void
6031 java_check_methods (class_decl)
6032      tree class_decl;
6033 {
6034   if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6035     return;
6036
6037   if (CLASS_INTERFACE (class_decl))
6038     java_check_abstract_methods (class_decl);
6039   else
6040     java_check_regular_methods (class_decl);
6041   
6042   CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6043 }
6044
6045 /* Check all the methods of CLASS_DECL. Methods are first completed
6046    then checked according to regular method existance rules.  If no
6047    constructor for CLASS_DECL were encountered, then build its
6048    declaration.  */
6049
6050 static void
6051 java_check_regular_methods (class_decl)
6052      tree class_decl;
6053 {
6054   int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6055   tree method;
6056   tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
6057   tree found = NULL_TREE;
6058   tree mthrows;
6059
6060   /* It is not necessary to check methods defined in java.lang.Object */
6061   if (class == object_type_node)
6062     return;
6063
6064   if (!TYPE_NVIRTUALS (class))
6065     TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6066
6067   /* Should take interfaces into account. FIXME */
6068   for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6069     {
6070       tree sig;
6071       tree method_wfl = DECL_FUNCTION_WFL (method);
6072       int aflags;
6073
6074       /* Check for redefinitions */
6075       if (check_method_redefinition (class, method))
6076         continue;
6077
6078       /* If we see one constructor a mark so we don't generate the
6079          default one. Also skip other verifications: constructors
6080          can't be inherited hence hiden or overriden */
6081      if (DECL_CONSTRUCTOR_P (method))
6082        {
6083          saw_constructor = 1;
6084          continue;
6085        }
6086
6087       /* We verify things thrown by the method. They must inherits from
6088          java.lang.Throwable */
6089       for (mthrows = DECL_FUNCTION_THROWS (method);
6090            mthrows; mthrows = TREE_CHAIN (mthrows))
6091         {
6092           if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6093             parse_error_context 
6094               (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6095                IDENTIFIER_POINTER 
6096                  (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6097         }
6098
6099       sig = build_java_argument_signature (TREE_TYPE (method));
6100       found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6101
6102       /* Inner class can't declare static methods */
6103       if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6104         {
6105           char *t = xstrdup (lang_printable_name (class, 0));
6106           parse_error_context 
6107             (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6108              lang_printable_name (method, 0), t);
6109           free (t);
6110         }
6111
6112       /* Nothing overrides or it's a private method. */
6113       if (!found)
6114         continue;
6115       if (METHOD_PRIVATE (found))
6116         {
6117           found = NULL_TREE;
6118           continue;
6119         }
6120
6121       /* If `found' is declared in an interface, make sure the
6122          modifier matches. */
6123       if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found))) 
6124           && clinit_identifier_node != DECL_NAME (found)
6125           && !METHOD_PUBLIC (method))
6126         {
6127           tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6128           parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6129                                IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6130                                lang_printable_name (method, 0),
6131                                IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6132         }
6133
6134       /* Can't override a method with the same name and different return
6135          types. */
6136       if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6137         {
6138           char *t = xstrdup 
6139             (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6140           parse_error_context 
6141             (method_wfl,
6142              "Method `%s' was defined with return type `%s' in class `%s'", 
6143              lang_printable_name (found, 0), t,
6144              IDENTIFIER_POINTER 
6145                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6146           free (t);
6147         }
6148
6149       aflags = get_access_flags_from_decl (found);
6150
6151       /* Can't override final. Can't override static. */
6152       if (METHOD_FINAL (found) || METHOD_STATIC (found))
6153         {
6154           /* Static *can* override static */
6155           if (METHOD_STATIC (found) && METHOD_STATIC (method))
6156             continue;
6157           parse_error_context 
6158             (method_wfl,
6159              "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6160              (METHOD_FINAL (found) ? "Final" : "Static"),
6161              lang_printable_name (found, 0),
6162              (METHOD_FINAL (found) ? "final" : "static"),
6163              IDENTIFIER_POINTER
6164                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6165           continue;
6166         }
6167
6168       /* Static method can't override instance method. */
6169       if (METHOD_STATIC (method))
6170         {
6171           parse_error_context 
6172             (method_wfl,
6173              "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6174              lang_printable_name (found, 0),
6175              IDENTIFIER_POINTER
6176                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6177           continue;
6178         }
6179
6180       /* - Overriding/hiding public must be public
6181          - Overriding/hiding protected must be protected or public
6182          - If the overriden or hidden method has default (package)
6183            access, then the overriding or hiding method must not be
6184            private; otherwise, a compile-time error occurs.  If
6185            `found' belongs to an interface, things have been already
6186            taken care of.  */
6187       if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6188           && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6189               || (METHOD_PROTECTED (found) 
6190                   && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6191               || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6192                   && METHOD_PRIVATE (method))))
6193         {
6194           parse_error_context 
6195             (method_wfl,
6196              "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6197              (METHOD_PUBLIC (method) ? "public" : 
6198               (METHOD_PRIVATE (method) ? "private" : "protected")),
6199              IDENTIFIER_POINTER (DECL_NAME 
6200                                  (TYPE_NAME (DECL_CONTEXT (found)))));
6201           continue;
6202         }
6203
6204       /* Overriding methods must have compatible `throws' clauses on checked
6205          exceptions, if any */
6206       check_throws_clauses (method, method_wfl, found);
6207
6208       /* Inheriting multiple methods with the same signature. FIXME */
6209     }
6210   
6211   if (!TYPE_NVIRTUALS (class))
6212     TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6213
6214   /* Search for inherited abstract method not yet implemented in this
6215      class.  */
6216   java_check_abstract_method_definitions (class_decl);
6217
6218   if (!saw_constructor)
6219     abort ();
6220 }
6221
6222 /* Return a non zero value if the `throws' clause of METHOD (if any)
6223    is incompatible with the `throws' clause of FOUND (if any).  */
6224
6225 static void
6226 check_throws_clauses (method, method_wfl, found)
6227      tree method, method_wfl, found;
6228 {
6229   tree mthrows, fthrows;
6230
6231   /* Can't check these things with class loaded from bytecode. FIXME */
6232   if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6233     return;
6234
6235   for (mthrows = DECL_FUNCTION_THROWS (method);
6236        mthrows; mthrows = TREE_CHAIN (mthrows))
6237     {
6238       /* We don't verify unchecked expressions */
6239       if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6240         continue;
6241       /* Checked expression must be compatible */
6242       for (fthrows = DECL_FUNCTION_THROWS (found); 
6243            fthrows; fthrows = TREE_CHAIN (fthrows))
6244         if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6245           break;
6246       if (!fthrows)
6247         {
6248           parse_error_context 
6249             (method_wfl, "Invalid checked exception class `%s' in `throws' clause. The exception must be a subclass of an exception thrown by `%s' from class `%s'",
6250              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6251              lang_printable_name (found, 0),
6252              IDENTIFIER_POINTER 
6253                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6254         }
6255     }
6256 }
6257
6258 /* Check abstract method of interface INTERFACE */
6259
6260 static void
6261 java_check_abstract_methods (interface_decl)
6262      tree interface_decl;
6263 {
6264   int i, n;
6265   tree method, basetype_vec, found;
6266   tree interface = TREE_TYPE (interface_decl);
6267
6268   for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6269     {
6270       /* 2- Check for double definition inside the defining interface */
6271       if (check_method_redefinition (interface, method))
6272         continue;
6273
6274       /* 3- Overriding is OK as far as we preserve the return type and
6275          the thrown exceptions (FIXME) */
6276       found = lookup_java_interface_method2 (interface, method);
6277       if (found)
6278         {
6279           char *t;
6280           t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6281           parse_error_context 
6282             (DECL_FUNCTION_WFL (found),
6283              "Method `%s' was defined with return type `%s' in class `%s'",
6284              lang_printable_name (found, 0), t,
6285              IDENTIFIER_POINTER 
6286                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6287           free (t);
6288           continue;
6289         }
6290     }
6291
6292   /* 4- Inherited methods can't differ by their returned types */
6293   if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6294     return;
6295   n = TREE_VEC_LENGTH (basetype_vec);
6296   for (i = 0; i < n; i++)
6297     {
6298       tree sub_interface_method, sub_interface;
6299       tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6300       if (!vec_elt)
6301         continue;
6302       sub_interface = BINFO_TYPE (vec_elt);
6303       for (sub_interface_method = TYPE_METHODS (sub_interface); 
6304            sub_interface_method;
6305            sub_interface_method = TREE_CHAIN (sub_interface_method))
6306         {
6307           found = lookup_java_interface_method2 (interface, 
6308                                                  sub_interface_method);
6309           if (found && (found != sub_interface_method))
6310             {
6311               parse_error_context 
6312                 (lookup_cl (sub_interface_method),
6313                  "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6314                  IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6315                  lang_printable_name (found, 0),
6316                  IDENTIFIER_POINTER 
6317                    (DECL_NAME (TYPE_NAME 
6318                                (DECL_CONTEXT (sub_interface_method)))),
6319                  IDENTIFIER_POINTER 
6320                    (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6321             }
6322         }
6323     }
6324 }
6325
6326 /* Lookup methods in interfaces using their name and partial
6327    signature. Return a matching method only if their types differ.  */
6328
6329 static tree
6330 lookup_java_interface_method2 (class, method_decl)
6331      tree class, method_decl;
6332 {
6333   int i, n;
6334   tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6335
6336   if (!basetype_vec)
6337     return NULL_TREE;
6338
6339   n = TREE_VEC_LENGTH (basetype_vec);
6340   for (i = 0; i < n; i++)
6341     {
6342       tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6343       if ((BINFO_TYPE (vec_elt) != object_type_node)
6344           && (to_return = 
6345               lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6346         return to_return;
6347     }
6348   for (i = 0; i < n; i++)
6349     {
6350       to_return = lookup_java_interface_method2 
6351         (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6352       if (to_return)
6353         return to_return;
6354     }
6355
6356   return NULL_TREE;
6357 }
6358
6359 /* Lookup method using their name and partial signature. Return a
6360    matching method only if their types differ.  */
6361
6362 static tree
6363 lookup_java_method2 (clas, method_decl, do_interface)
6364      tree clas, method_decl;
6365      int do_interface;
6366 {
6367   tree method, method_signature, method_name, method_type, name;
6368
6369   method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6370   name = DECL_NAME (method_decl);
6371   method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ? 
6372                  EXPR_WFL_NODE (name) : name);
6373   method_type = TREE_TYPE (TREE_TYPE (method_decl));
6374
6375   while (clas != NULL_TREE)
6376     {
6377       for (method = TYPE_METHODS (clas);
6378            method != NULL_TREE;  method = TREE_CHAIN (method))
6379         {
6380           tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6381           tree name = DECL_NAME (method);
6382           if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6383                EXPR_WFL_NODE (name) : name) == method_name
6384               && method_sig == method_signature 
6385               && TREE_TYPE (TREE_TYPE (method)) != method_type)
6386             return method;
6387         }
6388       clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6389     }
6390   return NULL_TREE;
6391 }
6392
6393 /* Return the line that matches DECL line number, and try its best to
6394    position the column number. Used during error reports.  */
6395
6396 static tree
6397 lookup_cl (decl)
6398      tree decl;
6399 {
6400   static tree cl = NULL_TREE;
6401   char *line, *found;
6402   
6403   if (!decl)
6404     return NULL_TREE;
6405
6406   if (cl == NULL_TREE)
6407     {
6408       cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6409       ggc_add_tree_root (&cl, 1);
6410     }
6411
6412   EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6413   EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6414
6415   line = java_get_line_col (EXPR_WFL_FILENAME (cl), 
6416                             EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6417
6418   found = strstr ((const char *)line, 
6419                   (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6420   if (found)
6421     EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6422
6423   return cl;
6424 }
6425
6426 /* Look for a simple name in the single-type import list */
6427
6428 static tree
6429 find_name_in_single_imports (name)
6430      tree name;
6431 {
6432   tree node;
6433
6434   for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6435     if (TREE_VALUE (node) == name)
6436       return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6437
6438   return NULL_TREE;
6439 }
6440
6441 /* Process all single-type import. */
6442
6443 static int
6444 process_imports ()
6445 {
6446   tree import;
6447   int error_found;
6448
6449   for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6450     {
6451       tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6452       char *original_name;
6453
6454       obstack_grow0 (&temporary_obstack,
6455                      IDENTIFIER_POINTER (to_be_found),
6456                      IDENTIFIER_LENGTH (to_be_found));
6457       original_name = obstack_finish (&temporary_obstack);
6458
6459       /* Don't load twice something already defined. */
6460       if (IDENTIFIER_CLASS_VALUE (to_be_found))
6461         continue;
6462       
6463       while (1)
6464         {
6465           tree left;
6466
6467           QUALIFIED_P (to_be_found) = 1;
6468           load_class (to_be_found, 0);
6469           error_found =
6470             check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6471           
6472           /* We found it, we can bail out */
6473           if (IDENTIFIER_CLASS_VALUE (to_be_found))
6474             break;
6475
6476           /* We haven't found it. Maybe we're trying to access an
6477              inner class.  The only way for us to know is to try again
6478              after having dropped a qualifier. If we can't break it further,
6479              we have an error. */
6480           if (breakdown_qualified (&left, NULL, to_be_found))
6481             break;
6482
6483           to_be_found = left;
6484         }
6485       if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6486         {
6487           parse_error_context (TREE_PURPOSE (import),
6488                                "Class or interface `%s' not found in import",
6489                                original_name);
6490           error_found = 1;
6491         }
6492
6493       obstack_free (&temporary_obstack, original_name);
6494       if (error_found)
6495         return 1;
6496     }
6497   return 0;
6498 }
6499
6500 /* Possibly find and mark a class imported by a single-type import
6501    statement.  */
6502
6503 static void
6504 find_in_imports (class_type)
6505      tree class_type;
6506 {
6507   tree import;
6508
6509   for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6510     if (TREE_VALUE (import) == TYPE_NAME (class_type))
6511       {
6512         TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6513         QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6514       }
6515 }
6516
6517 static int
6518 note_possible_classname (name, len)
6519      const char *name;
6520      int len;
6521 {
6522   tree node;
6523   if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6524     len = len - 5;
6525   else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6526     len = len - 6;
6527   else
6528     return 0;
6529   node = ident_subst (name, len, "", '/', '.', "");
6530   IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6531   QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6532   return 1;
6533 }
6534
6535 /* Read a import directory, gathering potential match for further type
6536    references. Indifferently reads a filesystem or a ZIP archive
6537    directory.  */
6538
6539 static void
6540 read_import_dir (wfl)
6541      tree wfl;
6542 {
6543   tree package_id = EXPR_WFL_NODE (wfl);
6544   const char *package_name = IDENTIFIER_POINTER (package_id);
6545   int package_length = IDENTIFIER_LENGTH (package_id);
6546   DIR *dirp = NULL;
6547   JCF *saved_jcf = current_jcf;
6548
6549   int found = 0;
6550   int k;
6551   void *entry;
6552   struct buffer filename[1];
6553
6554
6555   if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6556     return;
6557   IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6558
6559   BUFFER_INIT (filename);
6560   buffer_grow (filename, package_length + 100);
6561
6562   for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6563     {
6564       const char *entry_name = jcf_path_name (entry);
6565       int entry_length = strlen (entry_name);
6566       if (jcf_path_is_zipfile (entry))
6567         {
6568           ZipFile *zipf;
6569           buffer_grow (filename, entry_length);
6570           memcpy (filename->data, entry_name, entry_length - 1);
6571           filename->data[entry_length-1] = '\0';
6572           zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6573           if (zipf == NULL)
6574             error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6575           else
6576             {
6577               ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6578               BUFFER_RESET (filename);
6579               for (k = 0; k < package_length; k++)
6580                 {
6581                   char ch = package_name[k];
6582                   *filename->ptr++ = ch == '.' ? '/' : ch;
6583                 }
6584               *filename->ptr++ = '/';
6585
6586               for (k = 0; k < zipf->count;  k++, zipd = ZIPDIR_NEXT (zipd))
6587                 {
6588                   const char *current_entry = ZIPDIR_FILENAME (zipd);
6589                   int current_entry_len = zipd->filename_length;
6590
6591                   if (current_entry_len >= BUFFER_LENGTH (filename)
6592                       && strncmp (filename->data, current_entry, 
6593                                   BUFFER_LENGTH (filename)) != 0)
6594                     continue;
6595                   found |= note_possible_classname (current_entry,
6596                                                     current_entry_len);
6597                 }
6598             }
6599         }
6600       else
6601         {
6602           BUFFER_RESET (filename);
6603           buffer_grow (filename, entry_length + package_length + 4);
6604           strcpy (filename->data, entry_name);
6605           filename->ptr = filename->data + entry_length;
6606           for (k = 0; k < package_length; k++)
6607             {
6608               char ch = package_name[k];
6609               *filename->ptr++ = ch == '.' ? '/' : ch;
6610             }
6611           *filename->ptr = '\0';
6612
6613           dirp = opendir (filename->data);
6614           if (dirp == NULL)
6615             continue;
6616           *filename->ptr++ = '/';
6617           for (;;)
6618             {
6619               int len; 
6620               const char *d_name;
6621               struct dirent *direntp = readdir (dirp);
6622               if (!direntp)
6623                 break;
6624               d_name = direntp->d_name;
6625               len = strlen (direntp->d_name);
6626               buffer_grow (filename, len+1);
6627               strcpy (filename->ptr, d_name);
6628               found |= note_possible_classname (filename->data + entry_length,
6629                                                 package_length+len+1);
6630             }
6631           if (dirp)
6632             closedir (dirp);
6633         }
6634     }
6635
6636   free (filename->data);
6637
6638   /* Here we should have a unified way of retrieving an entry, to be
6639      indexed. */
6640   if (!found)
6641     {
6642       static int first = 1;
6643       if (first)
6644         {
6645           error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
6646           java_error_count++;
6647           first = 0;
6648         }
6649       else
6650         parse_error_context (wfl, "Package `%s' not found in import",
6651                              package_name);
6652       current_jcf = saved_jcf;
6653       return;
6654     }
6655   current_jcf = saved_jcf;
6656 }
6657
6658 /* Possibly find a type in the import on demands specified
6659    types. Returns 1 if an error occured, 0 otherwise. Run throught the
6660    entire list, to detected potential double definitions.  */
6661                  
6662 static int
6663 find_in_imports_on_demand (class_type)
6664      tree class_type;
6665 {
6666   tree node, import, node_to_use = NULL_TREE;
6667   int seen_once = -1;
6668   tree cl = NULL_TREE;
6669
6670   for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6671     {
6672       const char *id_name;
6673       obstack_grow (&temporary_obstack, 
6674                     IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6675                     IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6676       obstack_1grow (&temporary_obstack, '.');
6677       obstack_grow0 (&temporary_obstack, 
6678                      IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6679                      IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6680       id_name = obstack_finish (&temporary_obstack);
6681               
6682       node = maybe_get_identifier (id_name);
6683       if (node && IS_A_CLASSFILE_NAME (node))
6684         {
6685           if (seen_once < 0)
6686             {
6687               cl = TREE_PURPOSE (import);
6688               seen_once = 1;
6689               node_to_use = node;
6690             }
6691           else
6692             {
6693               seen_once++;
6694               parse_error_context 
6695                 (TREE_PURPOSE (import), 
6696                  "Type `%s' also potentially defined in package `%s'",
6697                  IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6698                  IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6699             }
6700         }
6701     }
6702
6703   if (seen_once == 1)
6704     {
6705       /* Setup lineno so that it refers to the line of the import (in
6706          case we parse a class file and encounter errors */
6707       tree decl;
6708       int saved_lineno = lineno;
6709       lineno = EXPR_WFL_LINENO (cl);
6710       TYPE_NAME (class_type) = node_to_use;
6711       QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6712       decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6713       /* If there is no DECL set for the class or if the class isn't
6714          loaded and not seen in source yet, the load */
6715       if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6716                     && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6717         {
6718           load_class (node_to_use, 0);
6719           decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6720         }
6721       lineno = saved_lineno;
6722       if (! INNER_CLASS_P (TREE_TYPE (decl)))
6723         return check_pkg_class_access (TYPE_NAME (class_type), cl);
6724       else
6725         /* 6.6.1: Inner classes are subject to member access rules. */
6726         return 0;
6727     }
6728   else
6729     return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6730 }
6731
6732 /* Add package NAME to the list of package encountered so far. To
6733    speed up class lookup in do_resolve_class, we make sure a
6734    particular package is added only once.  */
6735
6736 static void
6737 register_package (name)
6738      tree name;
6739 {
6740   static struct hash_table _pht, *pht = NULL;
6741
6742   if (!pht)
6743     {
6744       hash_table_init (&_pht, hash_newfunc, 
6745                        java_hash_hash_tree_node, java_hash_compare_tree_node);
6746       pht = &_pht;
6747     }
6748   
6749   if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6750     {
6751       package_list = chainon (package_list, build_tree_list (name, NULL));
6752       hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6753     }
6754 }
6755
6756 static tree
6757 resolve_package (pkg, next)
6758      tree pkg, *next;
6759 {
6760   tree current, acc;
6761   tree type_name = NULL_TREE;
6762   const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
6763
6764   /* The trick is to determine when the package name stops and were
6765      the name of something contained in the package starts. Then we
6766      return a fully qualified name of what we want to get. */
6767
6768   /* Do a quick search on well known package names */
6769   if (!strncmp (name, "java.lang.reflect", 17))
6770     {
6771       *next = 
6772         TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6773       type_name = lookup_package_type (name, 17);
6774     }
6775   else if (!strncmp (name, "java.lang", 9))
6776     {
6777       *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6778       type_name = lookup_package_type (name, 9);
6779     }
6780
6781   /* If we found something here, return */
6782   if (type_name)
6783     return type_name; 
6784
6785   *next = EXPR_WFL_QUALIFICATION (pkg);
6786
6787   /* Try to progressively construct a type name */
6788   if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6789     for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg); 
6790          current; current = TREE_CHAIN (current))
6791       {
6792         acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6793         if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6794           {
6795             type_name = acc;
6796             /* resolve_package should be used in a loop, hence we
6797                point at this one to naturally process the next one at
6798                the next iteration. */
6799             *next = current;
6800             break;
6801           }
6802       }
6803   return type_name;
6804 }
6805
6806 static tree
6807 lookup_package_type (name, from)
6808      const char *name;
6809      int from;
6810 {
6811   char subname [128];
6812   const char *sub = &name[from+1];
6813   while (*sub != '.' && *sub)
6814     sub++;
6815   strncpy (subname, name, sub-name);
6816   subname [sub-name] = '\0';
6817   return get_identifier (subname);
6818 }
6819
6820 /* Check accessibility of inner classes according to member access rules. 
6821    DECL is the inner class, ENCLOSING_DECL is the class from which the
6822    access is being attempted. */
6823
6824 static void
6825 check_inner_class_access (decl, enclosing_decl, cl)
6826      tree decl, enclosing_decl, cl;
6827 {
6828   const char *access;
6829   tree enclosing_decl_type;
6830
6831   /* We don't issue an error message when CL is null. CL can be null
6832      as a result of processing a JDEP crafted by source_start_java_method
6833      for the purpose of patching its parm decl. But the error would
6834      have been already trapped when fixing the method's signature.
6835      DECL can also be NULL in case of earlier errors. */
6836   if (!decl || !cl)
6837     return;
6838
6839   enclosing_decl_type = TREE_TYPE (enclosing_decl);
6840
6841   if (CLASS_PRIVATE (decl))
6842     {
6843       /* Access is permitted only within the body of the top-level
6844          class in which DECL is declared. */
6845       tree top_level = decl;
6846       while (DECL_CONTEXT (top_level))
6847         top_level = DECL_CONTEXT (top_level);      
6848       while (DECL_CONTEXT (enclosing_decl))
6849         enclosing_decl = DECL_CONTEXT (enclosing_decl);
6850       if (top_level == enclosing_decl)
6851         return;      
6852       access = "private";
6853     }
6854   else if (CLASS_PROTECTED (decl))
6855     {
6856       tree decl_context;
6857       /* Access is permitted from within the same package... */
6858       if (in_same_package (decl, enclosing_decl))
6859         return;
6860       
6861       /* ... or from within the body of a subtype of the context in which
6862          DECL is declared. */
6863       decl_context = DECL_CONTEXT (decl);
6864       while (enclosing_decl)
6865         {
6866           if (CLASS_INTERFACE (decl))
6867             {
6868               if (interface_of_p (TREE_TYPE (decl_context), 
6869                                   enclosing_decl_type))
6870                 return;
6871             }
6872           else
6873             {
6874               /* Eww. The order of the arguments is different!! */
6875               if (inherits_from_p (enclosing_decl_type, 
6876                                    TREE_TYPE (decl_context)))
6877                 return;
6878             }
6879           enclosing_decl = DECL_CONTEXT (enclosing_decl);
6880         }      
6881       access = "protected";
6882     }
6883   else if (! CLASS_PUBLIC (decl))
6884     {
6885       /* Access is permitted only from within the same package as DECL. */
6886       if (in_same_package (decl, enclosing_decl))
6887         return;
6888       access = "non-public";
6889     }
6890   else
6891     /* Class is public. */
6892     return;
6893
6894   parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
6895                        (CLASS_INTERFACE (decl) ? "interface" : "class"),
6896                        lang_printable_name (decl, 0), access);
6897 }
6898
6899 /* Accessibility check for top-level classes. If CLASS_NAME is in a foreign 
6900    package, it must be PUBLIC. Return 0 if no access violations were found, 
6901    1 otherwise.  */
6902
6903 static int
6904 check_pkg_class_access (class_name, cl)
6905      tree class_name;
6906      tree cl;
6907 {
6908   tree type;
6909
6910   if (!IDENTIFIER_CLASS_VALUE (class_name))
6911     return 0;
6912
6913   if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6914     return 0;
6915
6916   if (!CLASS_PUBLIC (TYPE_NAME (type)))
6917     {
6918       /* Access to a private class within the same package is
6919          allowed. */
6920       tree l, r;
6921       breakdown_qualified (&l, &r, class_name);
6922       if (!QUALIFIED_P (class_name) && !ctxp->package)
6923         /* Both in the empty package. */
6924         return 0;
6925       if (l == ctxp->package)
6926         /* Both in the same package. */
6927         return 0;
6928
6929       parse_error_context 
6930         (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
6931          (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6932          IDENTIFIER_POINTER (class_name));
6933       return 1;
6934     }
6935   return 0;
6936 }
6937
6938 /* Local variable declaration. */
6939
6940 static void
6941 declare_local_variables (modifier, type, vlist)
6942      int modifier;
6943      tree type;
6944      tree vlist;
6945 {
6946   tree decl, current, saved_type;
6947   tree type_wfl = NULL_TREE;
6948   int must_chain = 0;
6949   int final_p = 0;
6950
6951   /* Push a new block if statements were seen between the last time we
6952      pushed a block and now. Keep a count of blocks to close */
6953   if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
6954     {
6955       tree b = enter_block ();
6956       BLOCK_IS_IMPLICIT (b) = 1;
6957     }
6958
6959   if (modifier)
6960     {
6961       int i;
6962       for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
6963       if (modifier == ACC_FINAL)
6964         final_p = 1;
6965       else 
6966         {
6967           parse_error_context 
6968             (ctxp->modifier_ctx [i], 
6969              "Only `final' is allowed as a local variables modifier");
6970           return;
6971         }
6972     }
6973
6974   /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6975      hold the TYPE value if a new incomplete has to be created (as
6976      opposed to being found already existing and reused). */
6977   SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6978
6979   /* If TYPE is fully resolved and we don't have a reference, make one */
6980   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6981
6982   /* Go through all the declared variables */
6983   for (current = vlist, saved_type = type; current;
6984        current = TREE_CHAIN (current), type = saved_type)
6985     {
6986       tree other, real_type;
6987       tree wfl  = TREE_PURPOSE (current);
6988       tree name = EXPR_WFL_NODE (wfl);
6989       tree init = TREE_VALUE (current);
6990
6991       /* Process NAME, as it may specify extra dimension(s) for it */
6992       type = build_array_from_name (type, type_wfl, name, &name);
6993
6994       /* Variable redefinition check */
6995       if ((other = lookup_name_in_blocks (name)))
6996         {
6997           variable_redefinition_error (wfl, name, TREE_TYPE (other),
6998                                        DECL_SOURCE_LINE (other));
6999           continue;
7000         }
7001
7002       /* Type adjustment. We may have just readjusted TYPE because
7003          the variable specified more dimensions. Make sure we have
7004          a reference if we can and don't have one already. */
7005       PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7006
7007       real_type = GET_REAL_TYPE (type);
7008       /* Never layout this decl. This will be done when its scope
7009          will be entered */
7010       decl = build_decl (VAR_DECL, name, real_type);
7011       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7012       LOCAL_FINAL (decl) = final_p;
7013       BLOCK_CHAIN_DECL (decl);
7014       
7015       /* If doing xreferencing, replace the line number with the WFL
7016          compound value */
7017       if (flag_emit_xref)
7018         DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7019       
7020       /* Don't try to use an INIT statement when an error was found */
7021       if (init && java_error_count)
7022         init = NULL_TREE;
7023       
7024       /* Add the initialization function to the current function's code */
7025       if (init)
7026         {
7027           /* Name might have been readjusted */
7028           EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7029           MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7030           java_method_add_stmt (current_function_decl,
7031                                 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7032                                                       init));
7033         }
7034     
7035       /* Setup dependency the type of the decl */
7036       if (must_chain)
7037         {
7038           jdep *dep;
7039           register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7040           dep = CLASSD_LAST (ctxp->classd_list);
7041           JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7042         }
7043     }
7044   SOURCE_FRONTEND_DEBUG (("Defined locals"));
7045 }
7046
7047 /* Called during parsing. Build decls from argument list.  */
7048
7049 static void
7050 source_start_java_method (fndecl)
7051      tree fndecl;
7052 {
7053   tree tem;
7054   tree parm_decl;
7055   int i;
7056
7057   if (!fndecl)
7058     return;
7059
7060   current_function_decl = fndecl;
7061
7062   /* New scope for the function */
7063   enter_block ();
7064   for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7065        tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7066     {
7067       tree type = TREE_VALUE (tem);
7068       tree name = TREE_PURPOSE (tem);
7069       
7070       /* If type is incomplete. Create an incomplete decl and ask for
7071          the decl to be patched later */
7072       if (INCOMPLETE_TYPE_P (type))
7073         {
7074           jdep *jdep;
7075           tree real_type = GET_REAL_TYPE (type);
7076           parm_decl = build_decl (PARM_DECL, name, real_type);
7077           type = obtain_incomplete_type (type);
7078           register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7079           jdep = CLASSD_LAST (ctxp->classd_list);
7080           JDEP_MISC (jdep) = name;
7081           JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7082         }
7083       else
7084         parm_decl = build_decl (PARM_DECL, name, type);
7085
7086       /* Remember if a local variable was declared final (via its
7087          TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7088       if (ARG_FINAL_P (tem))
7089         {
7090           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7091           LOCAL_FINAL (parm_decl) = 1;
7092         }
7093
7094       BLOCK_CHAIN_DECL (parm_decl);
7095     }
7096   tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7097   BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7098     nreverse (tem);
7099   DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7100   DECL_MAX_LOCALS (current_function_decl) = i;
7101 }
7102
7103 /* Called during parsing. Creates an artificial method declaration.  */
7104
7105 static tree
7106 create_artificial_method (class, flags, type, name, args)
7107      tree class;
7108      int flags;
7109      tree type, name, args;
7110 {
7111   tree mdecl;
7112
7113   java_parser_context_save_global ();
7114   lineno = 0;                                                               
7115   mdecl = make_node (FUNCTION_TYPE);                                
7116   TREE_TYPE (mdecl) = type;
7117   TYPE_ARG_TYPES (mdecl) = args;
7118   mdecl = add_method (class, flags, name, build_java_signature (mdecl)); 
7119   java_parser_context_restore_global ();
7120   DECL_ARTIFICIAL (mdecl) = 1;                                      
7121   return mdecl;
7122 }
7123
7124 /* Starts the body if an artifical method.  */
7125
7126 static void
7127 start_artificial_method_body (mdecl)
7128      tree mdecl;
7129 {
7130   DECL_SOURCE_LINE (mdecl) = 1;
7131   DECL_SOURCE_LINE_MERGE (mdecl, 1);
7132   source_start_java_method (mdecl);
7133   enter_block ();
7134 }
7135
7136 static void
7137 end_artificial_method_body (mdecl)
7138      tree mdecl;
7139 {
7140   /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7141      It has to be evaluated first. (if mdecl is current_function_decl,
7142      we have an undefined behavior if no temporary variable is used.) */
7143   tree b = exit_block ();
7144   BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7145   exit_block ();
7146 }
7147
7148 /* Terminate a function and expand its body.  */
7149
7150 static void
7151 source_end_java_method ()
7152 {
7153   tree fndecl = current_function_decl;
7154
7155   if (!fndecl)
7156     return;
7157
7158   java_parser_context_save_global ();
7159   lineno = ctxp->last_ccb_indent1;
7160
7161   /* Turn function bodies with only a NOP expr null, so they don't get
7162      generated at all and we won't get warnings when using the -W
7163      -Wall flags. */
7164   if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7165     BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7166
7167   /* Generate function's code */
7168   if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7169       && ! flag_emit_class_files
7170       && ! flag_emit_xref)
7171     expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7172
7173   /* pop out of its parameters */
7174   pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7175   poplevel (1, 0, 1);
7176   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7177
7178   /* Generate rtl for function exit.  */
7179   if (! flag_emit_class_files && ! flag_emit_xref)
7180     {
7181       lineno = DECL_SOURCE_LINE_LAST (fndecl);
7182       expand_function_end (input_filename, lineno, 0);
7183
7184       /* Run the optimizers and output assembler code for this function. */
7185       rest_of_compilation (fndecl);
7186     }
7187
7188   current_function_decl = NULL_TREE;
7189   java_parser_context_restore_global ();
7190 }
7191
7192 /* Record EXPR in the current function block. Complements compound
7193    expression second operand if necessary.  */
7194
7195 tree
7196 java_method_add_stmt (fndecl, expr)
7197      tree fndecl, expr;
7198 {
7199   if (!GET_CURRENT_BLOCK (fndecl))
7200     return NULL_TREE;
7201   return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7202 }
7203
7204 static tree
7205 add_stmt_to_block (b, type, stmt)
7206      tree b, type, stmt;
7207 {
7208   tree body = BLOCK_EXPR_BODY (b), c;
7209   
7210   if (java_error_count)
7211     return body;
7212     
7213   if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7214     return body;
7215
7216   BLOCK_EXPR_BODY (b) = c;
7217   TREE_SIDE_EFFECTS (c) = 1;
7218   return c;
7219 }
7220
7221 /* Add STMT to EXISTING if possible, otherwise create a new
7222    COMPOUND_EXPR and add STMT to it. */
7223
7224 static tree
7225 add_stmt_to_compound (existing, type, stmt)
7226      tree existing, type, stmt;
7227 {
7228   if (existing)
7229     return build (COMPOUND_EXPR, type, existing, stmt);
7230   else
7231     return stmt;
7232 }
7233
7234 void java_layout_seen_class_methods ()
7235 {
7236   tree previous_list = all_class_list;
7237   tree end = NULL_TREE;
7238   tree current;
7239
7240   while (1)
7241     {
7242       for (current = previous_list; 
7243            current != end; current = TREE_CHAIN (current))
7244         layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7245       
7246       if (previous_list != all_class_list)
7247         {
7248           end = previous_list;
7249           previous_list = all_class_list;
7250         }
7251       else
7252         break;
7253     }
7254 }
7255
7256 void
7257 java_reorder_fields ()
7258 {
7259   static tree stop_reordering = NULL_TREE;
7260   static int initialized_p;
7261   tree current;
7262
7263   /* Register STOP_REORDERING with the garbage collector.  */
7264   if (!initialized_p)
7265     {
7266       ggc_add_tree_root (&stop_reordering, 1);
7267       initialized_p = 1;
7268     }
7269
7270   for (current = gclass_list; current; current = TREE_CHAIN (current))
7271     {
7272       current_class = TREE_TYPE (TREE_VALUE (current));
7273
7274       if (current_class == stop_reordering)
7275         break;
7276
7277       /* Reverse the fields, but leave the dummy field in front.
7278          Fields are already ordered for Object and Class */
7279       if (TYPE_FIELDS (current_class) && current_class != object_type_node
7280           && current_class != class_type_node)
7281       {
7282         /* If the dummy field is there, reverse the right fields and
7283            just layout the type for proper fields offset */
7284         if (!DECL_NAME (TYPE_FIELDS (current_class)))
7285           {
7286             tree fields = TYPE_FIELDS (current_class);
7287             TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7288             TYPE_SIZE (current_class) = NULL_TREE;
7289           }
7290         /* We don't have a dummy field, we need to layout the class,
7291            after having reversed the fields */
7292         else
7293           {
7294             TYPE_FIELDS (current_class) = 
7295               nreverse (TYPE_FIELDS (current_class));
7296             TYPE_SIZE (current_class) = NULL_TREE;
7297           }
7298       }
7299     }
7300   stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7301 }
7302
7303 /* Layout the methods of all classes loaded in one way or another.
7304    Check methods of source parsed classes. Then reorder the
7305    fields and layout the classes or the type of all source parsed
7306    classes */
7307
7308 void
7309 java_layout_classes ()
7310 {
7311   tree current;
7312   int save_error_count = java_error_count;
7313
7314   /* Layout the methods of all classes seen so far */
7315   java_layout_seen_class_methods ();
7316   java_parse_abort_on_error ();
7317   all_class_list = NULL_TREE;
7318
7319   /* Then check the methods of all parsed classes */
7320   for (current = gclass_list; current; current = TREE_CHAIN (current))
7321     if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7322       java_check_methods (TREE_VALUE (current));
7323   java_parse_abort_on_error ();
7324
7325   for (current = gclass_list; current; current = TREE_CHAIN (current))
7326     {
7327       current_class = TREE_TYPE (TREE_VALUE (current));
7328       layout_class (current_class);
7329
7330       /* Error reported by the caller */
7331       if (java_error_count)
7332         return;
7333     }
7334
7335   /* We might have reloaded classes durign the process of laying out
7336      classes for code generation. We must layout the methods of those
7337      late additions, as constructor checks might use them */
7338   java_layout_seen_class_methods ();
7339   java_parse_abort_on_error ();
7340 }
7341
7342 /* Expand methods in the current set of classes rememebered for
7343    generation.  */
7344
7345 static void
7346 java_complete_expand_classes ()
7347 {
7348   tree current;
7349
7350   do_not_fold = flag_emit_xref;
7351
7352   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7353     if (!INNER_CLASS_DECL_P (current))
7354       java_complete_expand_class (current);
7355 }
7356
7357 /* Expand the methods found in OUTER, starting first by OUTER's inner
7358    classes, if any.  */
7359
7360 static void
7361 java_complete_expand_class (outer)
7362      tree outer;
7363 {
7364   tree inner_list;
7365
7366   set_nested_class_simple_name_value (outer, 1); /* Set */
7367
7368   /* We need to go after all inner classes and start expanding them,
7369      starting with most nested ones. We have to do that because nested
7370      classes might add functions to outer classes */
7371
7372   for (inner_list = DECL_INNER_CLASS_LIST (outer);
7373        inner_list; inner_list = TREE_CHAIN (inner_list))
7374     java_complete_expand_class (TREE_PURPOSE (inner_list));
7375
7376   java_complete_expand_methods (outer);
7377   set_nested_class_simple_name_value (outer, 0); /* Reset */
7378 }
7379
7380 /* Expand methods registered in CLASS_DECL. The general idea is that
7381    we expand regular methods first. This allows us get an estimate on
7382    how outer context local alias fields are really used so we can add
7383    to the constructor just enough code to initialize them properly (it
7384    also lets us generate finit$ correctly.) Then we expand the
7385    constructors and then <clinit>.  */
7386
7387 static void
7388 java_complete_expand_methods (class_decl)
7389      tree class_decl;
7390 {
7391   tree clinit, finit, decl, first_decl;
7392
7393   current_class = TREE_TYPE (class_decl);
7394
7395   /* Find whether the class has final variables */
7396   for (decl = TYPE_FIELDS (current_class); decl; decl = TREE_CHAIN (decl))
7397     if (FIELD_FINAL (decl))
7398       {
7399         TYPE_HAS_FINAL_VARIABLE (current_class) = 1;
7400         break;
7401       }
7402
7403   /* Initialize a new constant pool */
7404   init_outgoing_cpool ();
7405
7406   /* Pre-expand <clinit> to figure whether we really need it or
7407      not. If we do need it, we pre-expand the static fields so they're
7408      ready to be used somewhere else. <clinit> will be fully expanded
7409      after we processed the constructors. */
7410   first_decl = TYPE_METHODS (current_class);
7411   clinit = maybe_generate_pre_expand_clinit (current_class);
7412
7413   /* Then generate finit$ (if we need to) because constructor will
7414    try to use it.*/
7415   if (TYPE_FINIT_STMT_LIST (current_class))
7416     {
7417       finit = generate_finit (current_class);
7418       java_complete_expand_method (finit);
7419     }
7420
7421   /* Now do the constructors */
7422   for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7423     {
7424       int no_body;
7425
7426       if (!DECL_CONSTRUCTOR_P (decl))
7427         continue;
7428       
7429       no_body = !DECL_FUNCTION_BODY (decl);
7430       /* Don't generate debug info on line zero when expanding a
7431          generated constructor. */
7432       if (no_body)
7433         restore_line_number_status (1);
7434
7435       /* Reset the final local variable assignment flags */
7436       if (TYPE_HAS_FINAL_VARIABLE (current_class))
7437         reset_final_variable_local_assignment_flag (current_class);
7438
7439       java_complete_expand_method (decl);
7440
7441       /* Check for missed out final variable assignment */
7442       if (TYPE_HAS_FINAL_VARIABLE (current_class))
7443         check_final_variable_local_assignment_flag (current_class, decl);
7444       
7445       if (no_body)
7446         restore_line_number_status (0);
7447     }
7448
7449   /* First, do the ordinary methods. */
7450   for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7451     {
7452       /* Skip abstract or native methods -- but do handle native
7453          methods when generating JNI stubs.  */
7454       if (METHOD_ABSTRACT (decl)
7455           || (! flag_jni && METHOD_NATIVE (decl))
7456           || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7457         continue;
7458
7459       if (METHOD_NATIVE (decl))
7460         {
7461           tree body = build_jni_stub (decl);
7462           BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7463         }
7464
7465       java_complete_expand_method (decl);
7466     }
7467
7468   /* If there is indeed a <clinit>, fully expand it now */
7469   if (clinit)
7470     {
7471       /* Reset the final local variable assignment flags */
7472       if (TYPE_HAS_FINAL_VARIABLE (current_class))
7473         reset_static_final_variable_assignment_flag (current_class);
7474       /* Prevent the use of `this' inside <clinit> */
7475       ctxp->explicit_constructor_p = 1;
7476       java_complete_expand_method (clinit);
7477       ctxp->explicit_constructor_p = 0;
7478       /* Check for missed out static final variable assignment */
7479       if (TYPE_HAS_FINAL_VARIABLE (current_class)
7480           && !CLASS_INTERFACE (class_decl))
7481         check_static_final_variable_assignment_flag (current_class);
7482     }
7483   
7484   /* We might have generated a class$ that we now want to expand */
7485   if (TYPE_DOT_CLASS (current_class))
7486     java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7487
7488   /* Now verify constructor circularity (stop after the first one we
7489      prove wrong.) */
7490   if (!CLASS_INTERFACE (class_decl))
7491     for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7492       if (DECL_CONSTRUCTOR_P (decl) 
7493           && verify_constructor_circularity (decl, decl))
7494         break;
7495
7496   /* Final check on the initialization of final variables. */
7497   if (TYPE_HAS_FINAL_VARIABLE (current_class))
7498     {
7499       check_final_variable_global_assignment_flag (current_class);
7500       /* If we have an interface, check for uninitialized fields. */
7501       if (CLASS_INTERFACE (class_decl))
7502         check_static_final_variable_assignment_flag (current_class);
7503     }
7504
7505   /* Save the constant pool. We'll need to restore it later. */
7506   TYPE_CPOOL (current_class) = outgoing_cpool;
7507 }
7508
7509 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7510    safely used in some other methods/constructors.  */
7511
7512 static tree
7513 maybe_generate_pre_expand_clinit (class_type)
7514      tree class_type;
7515 {
7516   tree current, mdecl;
7517
7518   if (!TYPE_CLINIT_STMT_LIST (class_type))
7519     return NULL_TREE;
7520
7521   /* Go through all static fields and pre expand them */
7522   for (current = TYPE_FIELDS (class_type); current; 
7523        current = TREE_CHAIN (current))
7524     if (FIELD_STATIC (current))
7525       build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7526
7527   /* Then build the <clinit> method */
7528   mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7529                                     clinit_identifier_node, end_params_node);
7530   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7531                        mdecl, NULL_TREE);
7532   start_artificial_method_body (mdecl);
7533
7534   /* We process the list of assignment we produced as the result of
7535      the declaration of initialized static field and add them as
7536      statement to the <clinit> method. */
7537   for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7538        current = TREE_CHAIN (current))
7539     {
7540       tree stmt = current;
7541       /* We build the assignment expression that will initialize the
7542          field to its value. There are strict rules on static
7543          initializers (8.5). FIXME */
7544       if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7545         stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7546       java_method_add_stmt (mdecl, stmt);
7547     }
7548
7549   end_artificial_method_body (mdecl);
7550
7551   /* Now we want to place <clinit> as the last method (because we need
7552      it at least for interface so that it doesn't interfere with the
7553      dispatch table based lookup. */
7554   if (TREE_CHAIN (TYPE_METHODS (class_type)))
7555     {
7556       current = TREE_CHAIN (TYPE_METHODS (class_type));
7557       TYPE_METHODS (class_type) = current;
7558
7559       while (TREE_CHAIN (current))
7560         current = TREE_CHAIN (current);
7561
7562       TREE_CHAIN (current) = mdecl;
7563       TREE_CHAIN (mdecl) = NULL_TREE;
7564     }
7565
7566   return mdecl;
7567 }
7568
7569 /* Analyzes a method body and look for something that isn't a
7570    MODIFY_EXPR with a constant value.  */
7571
7572 static int
7573 analyze_clinit_body (bbody)
7574      tree bbody;
7575 {
7576   while (bbody)
7577     switch (TREE_CODE (bbody))
7578       {
7579       case BLOCK:
7580         bbody = BLOCK_EXPR_BODY (bbody);
7581         break;
7582         
7583       case EXPR_WITH_FILE_LOCATION:
7584         bbody = EXPR_WFL_NODE (bbody);
7585         break;
7586         
7587       case COMPOUND_EXPR:
7588         if (analyze_clinit_body (TREE_OPERAND (bbody, 0)))
7589           return 1;
7590         bbody = TREE_OPERAND (bbody, 1);
7591         break;
7592         
7593       case MODIFY_EXPR:
7594         /* Return 0 if the operand is constant, 1 otherwise.  */
7595         return ! TREE_CONSTANT (TREE_OPERAND (bbody, 1));
7596
7597       default:
7598         return 1;
7599       }
7600   return 0;
7601 }
7602
7603
7604 /* See whether we could get rid of <clinit>. Criteria are: all static
7605    final fields have constant initial values and the body of <clinit>
7606    is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7607
7608 static int
7609 maybe_yank_clinit (mdecl)
7610      tree mdecl;
7611 {
7612   tree type, current;
7613   tree fbody, bbody;
7614   int found = 0;
7615   
7616   if (!DECL_CLINIT_P (mdecl))
7617     return 0;
7618
7619   /* If the body isn't empty, then we keep <clinit>. Note that if
7620      we're emitting classfiles, this isn't enough not to rule it
7621      out. */
7622   fbody = DECL_FUNCTION_BODY (mdecl);
7623   bbody = BLOCK_EXPR_BODY (fbody);
7624   if (bbody && bbody != error_mark_node)
7625     bbody = BLOCK_EXPR_BODY (bbody);
7626   else
7627     return 0;
7628   if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7629     return 0;
7630   
7631   type = DECL_CONTEXT (mdecl);
7632   current = TYPE_FIELDS (type);
7633
7634   for (current = (current ? TREE_CHAIN (current) : current); 
7635        current; current = TREE_CHAIN (current))
7636     {
7637       tree f_init;
7638
7639       /* We're not interested in non static field */
7640       if (!FIELD_STATIC (current))
7641         continue;
7642
7643       /* Anything that isn't String or a basic type is ruled out -- or
7644          if we know how to deal with it (when doing things natively) we
7645          should generated an empty <clinit> so that SUID are computed
7646          correctly. */
7647       if (! JSTRING_TYPE_P (TREE_TYPE (current))
7648           && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7649         break;
7650           
7651       f_init = DECL_INITIAL (current);
7652       /* If we're emitting native code, we want static final fields to
7653          have constant initializers. If we don't meet these
7654          conditions, we keep <clinit> */
7655       if (!flag_emit_class_files
7656           && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7657         break;
7658       /* If we're emitting bytecode, we want static fields to have
7659          constant initializers or no initializer. If we don't meet
7660          these conditions, we keep <clinit> */
7661       if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7662         break;
7663     }
7664
7665   /* Now we analyze the method body and look for something that
7666      isn't a MODIFY_EXPR */
7667   if (bbody == empty_stmt_node)
7668     found = 0;
7669   else
7670     found = analyze_clinit_body (bbody);
7671
7672   if (current || found)
7673     return 0;
7674
7675   /* Get rid of <clinit> in the class' list of methods */
7676   if (TYPE_METHODS (type) == mdecl)
7677     TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7678   else
7679     for (current = TYPE_METHODS (type); current; 
7680          current = TREE_CHAIN (current))
7681       if (TREE_CHAIN (current) == mdecl)
7682         {
7683           TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7684           break;
7685         }
7686
7687   return 1;
7688 }
7689
7690
7691 /* Complete and expand a method.  */
7692
7693 static void
7694 java_complete_expand_method (mdecl)
7695      tree mdecl;
7696 {
7697   int yank_clinit = 0;
7698
7699   current_function_decl = mdecl;
7700   /* Fix constructors before expanding them */
7701   if (DECL_CONSTRUCTOR_P (mdecl))
7702     fix_constructors (mdecl);
7703   
7704   /* Expand functions that have a body */
7705   if (DECL_FUNCTION_BODY (mdecl))
7706     {
7707       tree fbody = DECL_FUNCTION_BODY (mdecl);
7708       tree block_body = BLOCK_EXPR_BODY (fbody);
7709       tree exception_copy = NULL_TREE;
7710       tree tem, *ptr;
7711
7712       current_function_decl = mdecl;
7713
7714       if (! quiet_flag)
7715         fprintf (stderr, " [%s.",
7716                  lang_printable_name (DECL_CONTEXT (mdecl), 0));
7717       announce_function (mdecl);
7718       if (! quiet_flag)
7719         fprintf (stderr, "]");
7720
7721       pushlevel (1);            /* Prepare for a parameter push */
7722       ptr = &DECL_ARGUMENTS (mdecl);
7723       tem  = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7724       while (tem)
7725         {
7726           tree next = TREE_CHAIN (tem);
7727           tree type = TREE_TYPE (tem);
7728           if (PROMOTE_PROTOTYPES
7729               && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7730               && INTEGRAL_TYPE_P (type))
7731             type = integer_type_node;
7732           DECL_ARG_TYPE (tem) = type;
7733           layout_decl (tem, 0);
7734           pushdecl (tem);
7735           *ptr = tem;
7736           ptr = &TREE_CHAIN (tem);
7737           tem = next;
7738         }
7739       *ptr = NULL_TREE;
7740       pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7741       lineno = DECL_SOURCE_LINE_FIRST (mdecl);
7742
7743       build_result_decl (mdecl);
7744
7745       current_this 
7746         = (!METHOD_STATIC (mdecl) ? 
7747            BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7748
7749       /* Purge the `throws' list of unchecked exceptions. If we're
7750          doing xref, save a copy of the list and re-install it
7751          later. */
7752       if (flag_emit_xref)
7753         exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7754
7755       purge_unchecked_exceptions (mdecl);
7756
7757       /* Install exceptions thrown with `throws' */
7758       PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7759
7760       if (block_body != NULL_TREE)
7761         {
7762           block_body = java_complete_tree (block_body);
7763
7764           if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
7765             check_for_initialization (block_body);
7766           ctxp->explicit_constructor_p = 0;
7767         }
7768
7769       BLOCK_EXPR_BODY (fbody) = block_body;
7770
7771       /* If we saw a return but couldn't evaluate it properly, we'll
7772          have an error_mark_node here. */
7773       if (block_body != error_mark_node
7774           && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
7775           && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7776           && !flag_emit_xref)
7777         missing_return_error (current_function_decl);
7778
7779       /* Check wether we could just get rid of clinit, now the picture
7780          is complete. */
7781       if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7782         complete_start_java_method (mdecl); 
7783       
7784       /* Don't go any further if we've found error(s) during the
7785          expansion */
7786       if (!java_error_count && !yank_clinit)
7787         source_end_java_method ();
7788       else
7789         {
7790           if (java_error_count)
7791             pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7792           poplevel (1, 0, 1);
7793         }
7794
7795       /* Pop the exceptions and sanity check */
7796       POP_EXCEPTIONS();
7797       if (currently_caught_type_list)
7798         abort ();
7799
7800       if (flag_emit_xref)
7801         DECL_FUNCTION_THROWS (mdecl) = exception_copy;
7802     }
7803 }
7804
7805 \f
7806
7807 /* This section of the code deals with accessing enclosing context
7808    fields either directly by using the relevant access to this$<n> or
7809    by invoking an access method crafted for that purpose.  */
7810
7811 /* Build the necessary access from an inner class to an outer
7812    class. This routine could be optimized to cache previous result
7813    (decl, current_class and returned access).  When an access method
7814    needs to be generated, it always takes the form of a read. It might
7815    be later turned into a write by calling outer_field_access_fix.  */
7816
7817 static tree
7818 build_outer_field_access (id, decl)
7819      tree id, decl;
7820 {
7821   tree access = NULL_TREE;
7822   tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7823   tree decl_ctx = DECL_CONTEXT (decl);
7824
7825   /* If the immediate enclosing context of the current class is the
7826      field decl's class or inherits from it; build the access as
7827      `this$<n>.<field>'. Note that we will break the `private' barrier
7828      if we're not emitting bytecodes. */
7829   if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
7830       && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7831     {
7832       tree thisn = build_current_thisn (current_class);
7833       access = make_qualified_primary (build_wfl_node (thisn), 
7834                                        id, EXPR_WFL_LINECOL (id));
7835     }
7836   /* Otherwise, generate access methods to outer this and access the
7837      field (either using an access method or by direct access.) */
7838   else
7839     {
7840       int lc = EXPR_WFL_LINECOL (id);
7841
7842       /* Now we chain the required number of calls to the access$0 to
7843          get a hold to the enclosing instance we need, and then we
7844          build the field access. */
7845       access = build_access_to_thisn (current_class, decl_ctx, lc);
7846
7847       /* If the field is private and we're generating bytecode, then
7848          we generate an access method */
7849       if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7850         {
7851           tree name = build_outer_field_access_methods (decl);
7852           access = build_outer_field_access_expr (lc, decl_ctx,
7853                                                   name, access, NULL_TREE);
7854         }
7855       /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7856          Once again we break the `private' access rule from a foreign
7857          class. */
7858       else
7859         access = make_qualified_primary (access, id, lc);
7860     }
7861   return resolve_expression_name (access, NULL);
7862 }
7863
7864 /* Return a non zero value if NODE describes an outer field inner
7865    access.  */
7866
7867 static int
7868 outer_field_access_p (type, decl)
7869     tree type, decl;
7870 {
7871   if (!INNER_CLASS_TYPE_P (type) 
7872       || TREE_CODE (decl) != FIELD_DECL
7873       || DECL_CONTEXT (decl) == type)
7874     return 0;
7875   
7876   /* If the inner class extends the declaration context of the field
7877      we're try to acces, then this isn't an outer field access */
7878   if (inherits_from_p (type, DECL_CONTEXT (decl)))
7879     return 0;
7880
7881   for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7882        type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7883     {
7884       if (type == DECL_CONTEXT (decl))
7885         return 1;
7886
7887       if (!DECL_CONTEXT (TYPE_NAME (type)))
7888         {
7889           /* Before we give up, see whether the field is inherited from
7890              the enclosing context we're considering. */
7891           if (inherits_from_p (type, DECL_CONTEXT (decl)))
7892             return 1;
7893           break;
7894         }
7895     }
7896
7897   return 0;
7898 }
7899
7900 /* Return a non zero value if NODE represents an outer field inner
7901    access that was been already expanded. As a side effect, it returns
7902    the name of the field being accessed and the argument passed to the
7903    access function, suitable for a regeneration of the access method
7904    call if necessary. */
7905
7906 static int
7907 outer_field_expanded_access_p (node, name, arg_type, arg)
7908     tree node, *name, *arg_type, *arg;
7909 {
7910   int identified = 0;
7911
7912   if (TREE_CODE (node) != CALL_EXPR)
7913     return 0;
7914
7915   /* Well, gcj generates slightly different tree nodes when compiling
7916      to native or bytecodes. It's the case for function calls. */
7917
7918   if (flag_emit_class_files 
7919       && TREE_CODE (node) == CALL_EXPR
7920       && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7921     identified = 1;
7922   else if (!flag_emit_class_files)
7923     {
7924       node = TREE_OPERAND (node, 0);
7925       
7926       if (node && TREE_OPERAND (node, 0)
7927           && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7928         {
7929           node = TREE_OPERAND (node, 0);
7930           if (TREE_OPERAND (node, 0)
7931               && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7932               && (OUTER_FIELD_ACCESS_IDENTIFIER_P 
7933                   (DECL_NAME (TREE_OPERAND (node, 0)))))
7934             identified = 1;
7935         }
7936     }
7937
7938   if (identified && name && arg_type && arg)
7939     {
7940       tree argument = TREE_OPERAND (node, 1);
7941       *name = DECL_NAME (TREE_OPERAND (node, 0));
7942       *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7943       *arg = TREE_VALUE (argument);
7944     }
7945   return identified;
7946 }
7947
7948 /* Detect in NODE an outer field read access from an inner class and
7949    transform it into a write with RHS as an argument. This function is
7950    called from the java_complete_lhs when an assignment to a LHS can
7951    be identified. */
7952
7953 static tree
7954 outer_field_access_fix (wfl, node, rhs)
7955     tree wfl, node, rhs;
7956 {
7957   tree name, arg_type, arg;
7958   
7959   if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7960     {
7961       /* At any rate, check whether we're trying to assign a value to
7962          a final. */
7963       tree accessed = (JDECL_P (node) ? node : 
7964                        (TREE_CODE (node) == COMPONENT_REF ? 
7965                         TREE_OPERAND (node, 1) : node));
7966       if (check_final_assignment (accessed, wfl))
7967         return error_mark_node;
7968   
7969       node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl), 
7970                                             arg_type, name, arg, rhs);
7971       return java_complete_tree (node);
7972     }
7973   return NULL_TREE;
7974 }
7975
7976 /* Construct the expression that calls an access method:
7977      <type>.access$<n>(<arg1> [, <arg2>]); 
7978
7979    ARG2 can be NULL and will be omitted in that case. It will denote a
7980    read access.  */
7981
7982 static tree
7983 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7984     int lc;
7985     tree type, access_method_name, arg1, arg2;
7986 {
7987   tree args, cn, access;
7988
7989   args = arg1 ? arg1 : 
7990     build_wfl_node (build_current_thisn (current_class));
7991   args = build_tree_list (NULL_TREE, args);
7992
7993   if (arg2)
7994     args = tree_cons (NULL_TREE, arg2, args);
7995
7996   access = build_method_invocation (build_wfl_node (access_method_name), args);
7997   cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7998   return make_qualified_primary (cn, access, lc);
7999 }
8000
8001 static tree
8002 build_new_access_id ()
8003 {
8004   static int access_n_counter = 1;
8005   char buffer [128];
8006
8007   sprintf (buffer, "access$%d", access_n_counter++);
8008   return get_identifier (buffer);
8009 }
8010
8011 /* Create the static access functions for the outer field DECL. We define a
8012    read:
8013      TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8014        return inst$.field;
8015      }
8016    and a write access:
8017      TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8018                                      TREE_TYPE (<field>) value$) {
8019        return inst$.field = value$;
8020      }
8021    We should have a usage flags on the DECL so we can lazily turn the ones
8022    we're using for code generation. FIXME.
8023 */
8024
8025 static tree
8026 build_outer_field_access_methods (decl)
8027     tree decl;
8028 {
8029   tree id, args, stmt, mdecl;
8030   
8031   if (FIELD_INNER_ACCESS_P (decl))
8032     return FIELD_INNER_ACCESS (decl);
8033
8034   MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8035  
8036   /* Create the identifier and a function named after it. */
8037   id = build_new_access_id ();
8038
8039   /* The identifier is marked as bearing the name of a generated write
8040      access function for outer field accessed from inner classes. */
8041   OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8042
8043   /* Create the read access */
8044   args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8045   TREE_CHAIN (args) = end_params_node;
8046   stmt = make_qualified_primary (build_wfl_node (inst_id),
8047                                  build_wfl_node (DECL_NAME (decl)), 0);
8048   stmt = build_return (0, stmt);
8049   mdecl = build_outer_field_access_method (DECL_CONTEXT (decl), 
8050                                            TREE_TYPE (decl), id, args, stmt);
8051   DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8052
8053   /* Create the write access method. No write access for final variable */
8054   if (!FIELD_FINAL (decl))
8055     {
8056       args = build_tree_list (inst_id, 
8057                               build_pointer_type (DECL_CONTEXT (decl)));
8058       TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8059       TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8060       stmt = make_qualified_primary (build_wfl_node (inst_id),
8061                                      build_wfl_node (DECL_NAME (decl)), 0);
8062       stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8063                                                 build_wfl_node (wpv_id)));
8064       mdecl = build_outer_field_access_method (DECL_CONTEXT (decl), 
8065                                                TREE_TYPE (decl), id, 
8066                                                args, stmt);
8067     }
8068   DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8069
8070   /* Return the access name */
8071   return FIELD_INNER_ACCESS (decl) = id;
8072 }
8073
8074 /* Build an field access method NAME.  */
8075
8076 static tree 
8077 build_outer_field_access_method (class, type, name, args, body)
8078     tree class, type, name, args, body;
8079 {
8080   tree saved_current_function_decl, mdecl;
8081
8082   /* Create the method */
8083   mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8084   fix_method_argument_names (args, mdecl);
8085   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8086
8087   /* Attach the method body. */
8088   saved_current_function_decl = current_function_decl;
8089   start_artificial_method_body (mdecl);
8090   java_method_add_stmt (mdecl, body);
8091   end_artificial_method_body (mdecl);
8092   current_function_decl = saved_current_function_decl;
8093
8094   return mdecl;
8095 }
8096
8097 \f
8098 /* This section deals with building access function necessary for
8099    certain kinds of method invocation from inner classes.  */
8100
8101 static tree
8102 build_outer_method_access_method (decl)
8103     tree decl;
8104 {
8105   tree saved_current_function_decl, mdecl;
8106   tree args = NULL_TREE, call_args = NULL_TREE;
8107   tree carg, id, body, class;
8108   char buffer [80];
8109   int parm_id_count = 0;
8110
8111   /* Test this abort with an access to a private field */
8112   if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8113     abort ();
8114
8115   /* Check the cache first */
8116   if (DECL_FUNCTION_INNER_ACCESS (decl))
8117     return DECL_FUNCTION_INNER_ACCESS (decl);
8118
8119   class = DECL_CONTEXT (decl);
8120
8121   /* Obtain an access identifier and mark it */
8122   id = build_new_access_id ();
8123   OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8124
8125   carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8126   /* Create the arguments, as much as the original */
8127   for (; carg && carg != end_params_node; 
8128        carg = TREE_CHAIN (carg))
8129     {
8130       sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8131       args = chainon (args, build_tree_list (get_identifier (buffer), 
8132                                              TREE_VALUE (carg)));
8133     }
8134   args = chainon (args, end_params_node);
8135
8136   /* Create the method */
8137   mdecl = create_artificial_method (class, ACC_STATIC, 
8138                                     TREE_TYPE (TREE_TYPE (decl)), id, args);
8139   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8140   /* There is a potential bug here. We should be able to use
8141      fix_method_argument_names, but then arg names get mixed up and
8142      eventually a constructor will have its this$0 altered and the
8143      outer context won't be assignment properly. The test case is
8144      stub.java FIXME */
8145   TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8146
8147   /* Attach the method body. */
8148   saved_current_function_decl = current_function_decl;
8149   start_artificial_method_body (mdecl);
8150
8151   /* The actual method invocation uses the same args. When invoking a
8152      static methods that way, we don't want to skip the first
8153      argument. */
8154   carg = args;
8155   if (!METHOD_STATIC (decl))
8156     carg = TREE_CHAIN (carg);
8157   for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8158     call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8159                            call_args);
8160
8161   body = build_method_invocation (build_wfl_node (DECL_NAME (decl)), 
8162                                   call_args);
8163   if (!METHOD_STATIC (decl))
8164     body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)), 
8165                                    body, 0);
8166   if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8167     body = build_return (0, body);
8168   java_method_add_stmt (mdecl,body);
8169   end_artificial_method_body (mdecl);
8170   current_function_decl = saved_current_function_decl;
8171
8172   /* Back tag the access function so it know what it accesses */
8173   DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8174
8175   /* Tag the current method so it knows it has an access generated */
8176   return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8177 }
8178
8179 \f
8180 /* This section of the code deals with building expressions to access
8181    the enclosing instance of an inner class. The enclosing instance is
8182    kept in a generated field called this$<n>, with <n> being the
8183    inner class nesting level (starting from 0.)  */
8184     
8185 /* Build an access to a given this$<n>, always chaining access call to
8186    others. Access methods to this$<n> are build on the fly if
8187    necessary. This CAN'T be used to solely access this$<n-1> from
8188    this$<n> (which alway yield to special cases and optimization, see
8189    for example build_outer_field_access).  */
8190
8191 static tree
8192 build_access_to_thisn (from, to, lc)
8193      tree from, to;
8194      int lc;
8195 {
8196   tree access = NULL_TREE;
8197
8198   while (from != to)
8199     {
8200       if (!access)
8201         {
8202           access = build_current_thisn (from);
8203           access = build_wfl_node (access);
8204         }
8205       else
8206         {
8207           tree access0_wfl, cn;
8208
8209           maybe_build_thisn_access_method (from);
8210           access0_wfl = build_wfl_node (access0_identifier_node);
8211           cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8212           EXPR_WFL_LINECOL (access0_wfl) = lc;
8213           access = build_tree_list (NULL_TREE, access);
8214           access = build_method_invocation (access0_wfl, access);
8215           access = make_qualified_primary (cn, access, lc);
8216         }
8217
8218       /* if FROM isn't an inter class, that's fine, we've done
8219          enough. What we're looking for can be accessed from there. */
8220       from = DECL_CONTEXT (TYPE_NAME (from));
8221       if (!from)
8222         break;
8223       from = TREE_TYPE (from);
8224     }
8225   return access;
8226 }
8227
8228 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8229    is returned if nothing needs to be generated. Otherwise, the method
8230    generated and a method decl is returned.  
8231
8232    NOTE: These generated methods should be declared in a class file
8233    attribute so that they can't be referred to directly.  */
8234
8235 static tree
8236 maybe_build_thisn_access_method (type)
8237     tree type;
8238 {
8239   tree mdecl, args, stmt, rtype;
8240   tree saved_current_function_decl;
8241
8242   /* If TYPE is a top-level class, no access method is required.
8243      If there already is such an access method, bail out. */
8244   if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8245     return NULL_TREE;
8246
8247   /* We generate the method. The method looks like:
8248      static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8249   */
8250   args = build_tree_list (inst_id, build_pointer_type (type));
8251   TREE_CHAIN (args) = end_params_node;
8252   rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8253   mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8254                                     access0_identifier_node, args);
8255   fix_method_argument_names (args, mdecl);
8256   layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8257   stmt = build_current_thisn (type);
8258   stmt = make_qualified_primary (build_wfl_node (inst_id), 
8259                                  build_wfl_node (stmt), 0);
8260   stmt = build_return (0, stmt);
8261
8262   saved_current_function_decl = current_function_decl;
8263   start_artificial_method_body (mdecl);
8264   java_method_add_stmt (mdecl, stmt);
8265   end_artificial_method_body (mdecl);
8266   current_function_decl = saved_current_function_decl;
8267
8268   CLASS_ACCESS0_GENERATED_P (type) = 1;
8269
8270   return mdecl;
8271 }
8272
8273 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8274    the first level of innerclassing. this$1 for the next one, etc...
8275    This function can be invoked with TYPE to NULL, available and then
8276    has to count the parser context.  */
8277
8278 static tree
8279 build_current_thisn (type)
8280     tree type;
8281 {
8282   static int saved_i = -1;
8283   static tree saved_thisn = NULL_TREE;
8284   static tree saved_type = NULL_TREE;
8285   static int saved_type_i = 0;
8286   static int initialized_p;
8287   tree decl;
8288   char buffer [80];
8289   int i = 0;
8290
8291   /* Register SAVED_THISN and SAVED_TYPE with the garbage collector.  */
8292   if (!initialized_p)
8293     {
8294       ggc_add_tree_root (&saved_thisn, 1);
8295       ggc_add_tree_root (&saved_type, 1);
8296       initialized_p = 1;
8297     }
8298
8299   if (type)
8300     {
8301       if (type == saved_type)
8302         i = saved_type_i;
8303       else
8304         {
8305           for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type)); 
8306                decl; decl = DECL_CONTEXT (decl), i++)
8307             ;
8308       
8309           saved_type = type;
8310           saved_type_i = i;
8311         }
8312     }
8313   else
8314     i = list_length (GET_CPC_LIST ())-2;
8315
8316   if (i == saved_i)
8317     return saved_thisn;
8318     
8319   sprintf (buffer, "this$%d", i);
8320   saved_i = i;
8321   saved_thisn = get_identifier (buffer);
8322   return saved_thisn;
8323 }
8324
8325 /* Return the assignement to the hidden enclosing context `this$<n>'
8326    by the second incoming parameter to the innerclass constructor. The
8327    form used is `this.this$<n> = this$<n>;'.  */
8328
8329 static tree
8330 build_thisn_assign ()
8331 {
8332   if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8333     {
8334       tree thisn = build_current_thisn (current_class);
8335       tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8336                                          build_wfl_node (thisn), 0);
8337       tree rhs = build_wfl_node (thisn);
8338       EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8339       return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8340     }
8341   return NULL_TREE;
8342 }
8343
8344 \f
8345 /* Building the synthetic `class$' used to implement the `.class' 1.1
8346    extension for non primitive types. This method looks like:
8347
8348     static Class class$(String type) throws NoClassDefFoundError
8349     {
8350       try {return (java.lang.Class.forName (String));}
8351       catch (ClassNotFoundException e) {
8352         throw new NoClassDefFoundError(e.getMessage());}
8353     } */
8354
8355 static tree
8356 build_dot_class_method (class)
8357      tree class;
8358 {
8359 #define BWF(S) build_wfl_node (get_identifier ((S)))
8360 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8361   tree args, tmp, saved_current_function_decl, mdecl;
8362   tree stmt, throw_stmt, catch, catch_block, try_block;
8363   tree catch_clause_param;
8364   tree class_not_found_exception, no_class_def_found_error;
8365
8366   static tree get_message_wfl, type_parm_wfl;
8367
8368   if (!get_message_wfl)
8369     {
8370       get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8371       type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8372       ggc_add_tree_root (&get_message_wfl, 1);
8373       ggc_add_tree_root (&type_parm_wfl, 1);
8374     }
8375
8376   /* Build the arguments */
8377   args = build_tree_list (get_identifier ("type$"),
8378                           build_pointer_type (string_type_node));
8379   TREE_CHAIN (args) = end_params_node;
8380
8381   /* Build the qualified name java.lang.Class.forName */
8382   tmp = MQN (MQN (MQN (BWF ("java"), 
8383                        BWF ("lang")), BWF ("Class")), BWF ("forName"));
8384
8385   /* For things we have to catch and throw */
8386   class_not_found_exception = 
8387     lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8388   no_class_def_found_error = 
8389     lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8390   load_class (class_not_found_exception, 1);
8391   load_class (no_class_def_found_error, 1);
8392
8393   /* Create the "class$" function */
8394   mdecl = create_artificial_method (class, ACC_STATIC, 
8395                                     build_pointer_type (class_type_node),
8396                                     classdollar_identifier_node, args);
8397   DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8398                                                   no_class_def_found_error);
8399   
8400   /* We start by building the try block. We need to build:
8401        return (java.lang.Class.forName (type)); */
8402   stmt = build_method_invocation (tmp, 
8403                                   build_tree_list (NULL_TREE, type_parm_wfl));
8404   stmt = build_return (0, stmt);
8405   /* Put it in a block. That's the try block */
8406   try_block = build_expr_block (stmt, NULL_TREE);
8407
8408   /* Now onto the catch block. We start by building the expression
8409      throwing a new exception: 
8410        throw new NoClassDefFoundError (_.getMessage); */
8411   throw_stmt = make_qualified_name (build_wfl_node (wpv_id), 
8412                                     get_message_wfl, 0);
8413   throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8414   
8415   /* Build new NoClassDefFoundError (_.getMessage) */
8416   throw_stmt = build_new_invocation 
8417     (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8418      build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8419
8420   /* Build the throw, (it's too early to use BUILD_THROW) */
8421   throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8422
8423   /* Build the catch block to encapsulate all this. We begin by
8424      building an decl for the catch clause parameter and link it to
8425      newly created block, the catch block. */
8426   catch_clause_param = 
8427     build_decl (VAR_DECL, wpv_id, 
8428                 build_pointer_type (class_not_found_exception));
8429   catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8430   
8431   /* We initialize the variable with the exception handler. */
8432   catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8433                  build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
8434   add_stmt_to_block (catch_block, NULL_TREE, catch);
8435
8436   /* We add the statement throwing the new exception */
8437   add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8438
8439   /* Build a catch expression for all this */
8440   catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8441
8442   /* Build the try/catch sequence */
8443   stmt = build_try_statement (0, try_block, catch_block);
8444
8445   fix_method_argument_names (args, mdecl);
8446   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8447   saved_current_function_decl = current_function_decl;
8448   start_artificial_method_body (mdecl);
8449   java_method_add_stmt (mdecl, stmt);
8450   end_artificial_method_body (mdecl);
8451   current_function_decl = saved_current_function_decl;
8452   TYPE_DOT_CLASS (class) = mdecl;
8453
8454   return mdecl;
8455 }
8456
8457 static tree
8458 build_dot_class_method_invocation (type)
8459      tree type;
8460 {
8461   tree sig_id, s;
8462
8463   if (TYPE_ARRAY_P (type))
8464     sig_id = build_java_signature (type);
8465   else
8466     sig_id = DECL_NAME (TYPE_NAME (type));
8467
8468   s = build_string (IDENTIFIER_LENGTH (sig_id), 
8469                     IDENTIFIER_POINTER (sig_id));
8470   return build_method_invocation (build_wfl_node (classdollar_identifier_node),
8471                                   build_tree_list (NULL_TREE, s));
8472 }
8473
8474 /* This section of the code deals with constructor.  */
8475
8476 /* Craft a body for default constructor. Patch existing constructor
8477    bodies with call to super() and field initialization statements if
8478    necessary.  */
8479
8480 static void
8481 fix_constructors (mdecl)
8482      tree mdecl;
8483 {
8484   tree body = DECL_FUNCTION_BODY (mdecl);
8485   tree thisn_assign, compound = NULL_TREE;
8486   tree class_type = DECL_CONTEXT (mdecl);
8487
8488   if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
8489     return;
8490   DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
8491
8492   if (!body)
8493     {
8494       /* It is an error for the compiler to generate a default
8495          constructor if the superclass doesn't have a constructor that
8496          takes no argument, or the same args for an anonymous class */
8497       if (verify_constructor_super (mdecl))
8498         {
8499           tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8500           tree save = DECL_NAME (mdecl);
8501           const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8502           DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8503           parse_error_context
8504             (lookup_cl (TYPE_NAME (class_type)), 
8505              "No constructor matching `%s' found in class `%s'",
8506              lang_printable_name (mdecl, 0), n);
8507           DECL_NAME (mdecl) = save;
8508         }
8509       
8510       /* The constructor body must be crafted by hand. It's the
8511          constructor we defined when we realize we didn't have the
8512          CLASSNAME() constructor */
8513       start_artificial_method_body (mdecl);
8514       
8515       /* Insert an assignment to the this$<n> hidden field, if
8516          necessary */
8517       if ((thisn_assign = build_thisn_assign ()))
8518         java_method_add_stmt (mdecl, thisn_assign);
8519
8520       /* We don't generate a super constructor invocation if we're
8521          compiling java.lang.Object. build_super_invocation takes care
8522          of that. */
8523       java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8524
8525       /* Insert the instance initializer block right here, after the
8526          super invocation. */
8527       add_instance_initializer (mdecl);
8528
8529       end_artificial_method_body (mdecl);
8530     }
8531   /* Search for an explicit constructor invocation */
8532   else 
8533     {
8534       int found = 0;
8535       tree found_call = NULL_TREE;
8536       tree main_block = BLOCK_EXPR_BODY (body);
8537       tree ii;                  /* Instance Initializer */
8538       
8539       while (body)
8540         switch (TREE_CODE (body))
8541           {
8542           case CALL_EXPR:
8543             found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8544             body = NULL_TREE;
8545             break;
8546           case COMPOUND_EXPR:
8547           case EXPR_WITH_FILE_LOCATION:
8548             found_call = body;
8549             body = TREE_OPERAND (body, 0);
8550             break;
8551           case BLOCK:
8552             found_call = body;
8553             body = BLOCK_EXPR_BODY (body);
8554             break;
8555           default:
8556             found = 0;
8557             body = NULL_TREE;
8558           }
8559
8560       /* Generate the assignment to this$<n>, if necessary */
8561       if ((thisn_assign = build_thisn_assign ()))
8562         compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8563
8564       /* The constructor is missing an invocation of super() */
8565       if (!found)
8566         compound = add_stmt_to_compound (compound, NULL_TREE,
8567                                          build_super_invocation (mdecl));
8568       /* Explicit super() invokation should take place before the
8569          instance initializer blocks. */
8570       else
8571         {
8572           compound = add_stmt_to_compound (compound, NULL_TREE,
8573                                            TREE_OPERAND (found_call, 0));
8574           TREE_OPERAND (found_call, 0) = empty_stmt_node;
8575         }
8576       
8577       /* Insert the instance initializer block right after. */
8578       if ((ii = build_instance_initializer (mdecl)))
8579         compound = add_stmt_to_compound (compound, NULL_TREE, ii);
8580
8581       /* Fix the constructor main block if we're adding extra stmts */
8582       if (compound)
8583         {
8584           compound = add_stmt_to_compound (compound, NULL_TREE,
8585                                            BLOCK_EXPR_BODY (main_block));
8586           BLOCK_EXPR_BODY (main_block) = compound;
8587         }
8588     }
8589 }
8590
8591 /* Browse constructors in the super class, searching for a constructor
8592    that doesn't take any argument. Return 0 if one is found, 1
8593    otherwise.  If the current class is an anonymous inner class, look
8594    for something that has the same signature. */
8595
8596 static int
8597 verify_constructor_super (mdecl)
8598      tree mdecl;
8599 {
8600   tree class = CLASSTYPE_SUPER (current_class);
8601   int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8602   tree sdecl;
8603
8604   if (!class)
8605     return 0;
8606
8607   if (ANONYMOUS_CLASS_P (current_class))
8608     {
8609       tree mdecl_arg_type;
8610       SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8611       for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8612         if (DECL_CONSTRUCTOR_P (sdecl))
8613           {
8614             tree m_arg_type;
8615             tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8616             if (super_inner)
8617               arg_type = TREE_CHAIN (arg_type);
8618             for (m_arg_type = mdecl_arg_type; 
8619                  (arg_type != end_params_node 
8620                   && m_arg_type != end_params_node);
8621                  arg_type = TREE_CHAIN (arg_type), 
8622                    m_arg_type = TREE_CHAIN (m_arg_type))
8623               if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
8624                 break;
8625
8626             if (arg_type == end_params_node && m_arg_type == end_params_node)
8627               return 0;
8628           }
8629     }
8630   else
8631     {
8632       for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8633         {
8634           tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8635           if (super_inner)
8636             arg = TREE_CHAIN (arg);
8637           if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8638             return 0;
8639         }
8640     }
8641   return 1;
8642 }
8643
8644 /* Generate code for all context remembered for code generation.  */
8645
8646 void
8647 java_expand_classes ()
8648 {
8649   int save_error_count = 0;
8650   static struct parser_ctxt *cur_ctxp = NULL;
8651
8652   java_parse_abort_on_error ();
8653   if (!(ctxp = ctxp_for_generation))
8654     return;
8655   java_layout_classes ();
8656   java_parse_abort_on_error ();
8657
8658   cur_ctxp = ctxp_for_generation;
8659   for (; cur_ctxp; cur_ctxp = cur_ctxp->next)
8660     {
8661       ctxp = cur_ctxp;
8662       input_filename = ctxp->filename;
8663       lang_init_source (2);            /* Error msgs have method prototypes */
8664       java_complete_expand_classes (); /* Complete and expand classes */
8665       java_parse_abort_on_error ();
8666     }
8667   input_filename = main_input_filename;
8668
8669   /* Find anonymous classes and expand their constructor, now they
8670      have been fixed. */
8671   for (cur_ctxp = ctxp_for_generation;  cur_ctxp;  cur_ctxp = cur_ctxp->next)
8672     {
8673       tree current;
8674       ctxp = cur_ctxp;
8675       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8676         {
8677           current_class = TREE_TYPE (current);
8678           if (ANONYMOUS_CLASS_P (current_class))
8679             {
8680               tree d;
8681               for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8682                 {
8683                   if (DECL_CONSTRUCTOR_P (d))
8684                     {
8685                       restore_line_number_status (1);
8686                       java_complete_expand_method (d);
8687                       restore_line_number_status (0);
8688                       break;    /* We now there are no other ones */
8689                     }
8690                 }
8691             }
8692         }
8693     }
8694
8695   /* If we've found error at that stage, don't try to generate
8696      anything, unless we're emitting xrefs or checking the syntax only
8697      (but not using -fsyntax-only for the purpose of generating
8698      bytecode. */
8699   if (java_error_count && !flag_emit_xref 
8700       && (!flag_syntax_only && !flag_emit_class_files))
8701     return;
8702
8703   /* Now things are stable, go for generation of the class data. */
8704   for (cur_ctxp = ctxp_for_generation;  cur_ctxp;  cur_ctxp = cur_ctxp->next)
8705     {
8706       tree current;
8707       ctxp = cur_ctxp;
8708       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8709         {
8710           current_class = TREE_TYPE (current);
8711           outgoing_cpool = TYPE_CPOOL (current_class);
8712           if (flag_emit_class_files)
8713             write_classfile (current_class);
8714           if (flag_emit_xref)
8715             expand_xref (current_class);
8716           else if (! flag_syntax_only)
8717             finish_class ();
8718         }
8719     }
8720 }
8721
8722 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8723    a tree list node containing RIGHT. Fore coming RIGHTs will be
8724    chained to this hook. LOCATION contains the location of the
8725    separating `.' operator.  */
8726
8727 static tree
8728 make_qualified_primary (primary, right, location)
8729      tree primary, right;
8730      int location;
8731 {
8732   tree wfl;
8733
8734   if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8735     wfl = build_wfl_wrap (primary, location);
8736   else
8737     {
8738       wfl = primary;
8739       /* If wfl wasn't qualified, we build a first anchor */
8740       if (!EXPR_WFL_QUALIFICATION (wfl))
8741         EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
8742     }
8743
8744   /* And chain them */
8745   EXPR_WFL_LINECOL (right) = location;
8746   chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8747   PRIMARY_P (wfl) =  1;
8748   return wfl;
8749 }
8750
8751 /* Simple merge of two name separated by a `.' */
8752
8753 static tree
8754 merge_qualified_name (left, right)
8755      tree left, right;
8756 {
8757   tree node;
8758   if (!left && !right)
8759     return NULL_TREE;
8760
8761   if (!left)
8762     return right;
8763
8764   if (!right)
8765     return left;
8766
8767   obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8768                 IDENTIFIER_LENGTH (left));
8769   obstack_1grow (&temporary_obstack, '.');
8770   obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8771                  IDENTIFIER_LENGTH (right));
8772   node =  get_identifier (obstack_base (&temporary_obstack));
8773   obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8774   QUALIFIED_P (node) = 1;
8775   return node;
8776 }
8777
8778 /* Merge the two parts of a qualified name into LEFT.  Set the
8779    location information of the resulting node to LOCATION, usually
8780    inherited from the location information of the `.' operator. */
8781
8782 static tree
8783 make_qualified_name (left, right, location)
8784      tree left, right;
8785      int location;
8786 {
8787 #ifdef USE_COMPONENT_REF
8788   tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8789   EXPR_WFL_LINECOL (node) = location;
8790   return node;
8791 #else
8792   tree left_id = EXPR_WFL_NODE (left);
8793   tree right_id = EXPR_WFL_NODE (right);
8794   tree wfl, merge;
8795
8796   merge = merge_qualified_name (left_id, right_id);
8797
8798   /* Left wasn't qualified and is now qualified */
8799   if (!QUALIFIED_P (left_id))
8800     {
8801       tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8802       EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8803       EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8804     }
8805   
8806   wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8807   EXPR_WFL_LINECOL (wfl) = location;
8808   chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8809
8810   EXPR_WFL_NODE (left) = merge;
8811   return left;
8812 #endif
8813 }
8814
8815 /* Extract the last identifier component of the qualified in WFL. The
8816    last identifier is removed from the linked list */
8817
8818 static tree
8819 cut_identifier_in_qualified (wfl)
8820      tree wfl;
8821 {
8822   tree q;
8823   tree previous = NULL_TREE;
8824   for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8825     if (!TREE_CHAIN (q))
8826       {
8827         if (!previous)
8828           /* Operating on a non qualified qualified WFL.  */
8829           abort ();
8830
8831         TREE_CHAIN (previous) = NULL_TREE;
8832         return TREE_PURPOSE (q);
8833       }
8834 }
8835
8836 /* Resolve the expression name NAME. Return its decl.  */
8837
8838 static tree
8839 resolve_expression_name (id, orig)
8840      tree id;
8841      tree *orig;
8842 {
8843   tree name = EXPR_WFL_NODE (id);
8844   tree decl;
8845
8846   /* 6.5.5.1: Simple expression names */
8847   if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8848     {
8849       /* 15.13.1: NAME can appear within the scope of a local variable
8850          declaration */
8851       if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8852         return decl;
8853
8854       /* 15.13.1: NAME can appear within a class declaration */
8855       else 
8856         {
8857           decl = lookup_field_wrapper (current_class, name);
8858           if (decl)
8859             {
8860               tree access = NULL_TREE;
8861               int fs = FIELD_STATIC (decl);
8862
8863               /* If we're accessing an outer scope local alias, make
8864                  sure we change the name of the field we're going to
8865                  build access to. */
8866               if (FIELD_LOCAL_ALIAS_USED (decl))
8867                 name = DECL_NAME (decl);
8868
8869               /* Instance variable (8.3.1.1) can't appear within
8870                  static method, static initializer or initializer for
8871                  a static variable. */
8872               if (!fs && METHOD_STATIC (current_function_decl))
8873                 {
8874                   static_ref_err (id, name, current_class);
8875                   return error_mark_node;
8876                 }
8877               /* Instance variables can't appear as an argument of
8878                  an explicit constructor invocation */
8879               if (!fs && ctxp->explicit_constructor_p
8880                   && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
8881                 {
8882                   parse_error_context
8883                     (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
8884                   return error_mark_node;
8885                 }
8886
8887               /* If we're processing an inner class and we're trying
8888                  to access a field belonging to an outer class, build
8889                  the access to the field */
8890               if (!fs && outer_field_access_p (current_class, decl))
8891                 {
8892                   if (CLASS_STATIC (TYPE_NAME (current_class)))
8893                     {
8894                       static_ref_err (id, DECL_NAME (decl), current_class);
8895                       return error_mark_node;
8896                     }
8897                   return build_outer_field_access (id, decl);
8898                 }
8899
8900               /* Otherwise build what it takes to access the field */
8901               access = build_field_ref ((fs ? NULL_TREE : current_this),
8902                                         DECL_CONTEXT (decl), name);
8903               if (fs)
8904                 access = maybe_build_class_init_for_field (decl, access);
8905               /* We may be asked to save the real field access node */
8906               if (orig)
8907                 *orig = access;
8908               /* And we return what we got */
8909               return access;
8910             }
8911           /* Fall down to error report on undefined variable */
8912         }
8913     }
8914   /* 6.5.5.2 Qualified Expression Names */
8915   else
8916     {
8917       if (orig)
8918         *orig = NULL_TREE;
8919       qualify_ambiguous_name (id);
8920       /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8921       /* 15.10.2: Accessing Superclass Members using super */
8922       return resolve_field_access (id, orig, NULL);
8923     }
8924
8925   /* We've got an error here */
8926   parse_error_context (id, "Undefined variable `%s'", 
8927                        IDENTIFIER_POINTER (name));
8928
8929   return error_mark_node;
8930 }
8931
8932 static void
8933 static_ref_err (wfl, field_id, class_type)
8934     tree wfl, field_id, class_type;
8935 {
8936   parse_error_context 
8937     (wfl, 
8938      "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8939      IDENTIFIER_POINTER (field_id), 
8940      IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8941 }
8942
8943 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8944    We return something suitable to generate the field access. We also
8945    return the field decl in FIELD_DECL and its type in FIELD_TYPE.  If
8946    recipient's address can be null. */
8947
8948 static tree
8949 resolve_field_access (qual_wfl, field_decl, field_type)
8950      tree qual_wfl;
8951      tree *field_decl, *field_type;
8952 {
8953   int is_static = 0;
8954   tree field_ref;
8955   tree decl, where_found, type_found;
8956
8957   if (resolve_qualified_expression_name (qual_wfl, &decl,
8958                                          &where_found, &type_found))
8959     return error_mark_node;
8960
8961   /* Resolve the LENGTH field of an array here */
8962   if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node 
8963       && type_found && TYPE_ARRAY_P (type_found) 
8964       && ! flag_emit_class_files && ! flag_emit_xref)
8965     {
8966       tree length = build_java_array_length_access (where_found);
8967       field_ref = length;
8968
8969       /* In case we're dealing with a static array, we need to
8970          initialize its class before the array length can be fetched.
8971          It's also a good time to create a DECL_RTL for the field if
8972          none already exists, otherwise if the field was declared in a
8973          class found in an external file and hasn't been (and won't
8974          be) accessed for its value, none will be created. */
8975       if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8976         {
8977           build_static_field_ref (where_found);
8978           field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8979         }
8980     }
8981   /* We might have been trying to resolve field.method(). In which
8982      case, the resolution is over and decl is the answer */
8983   else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
8984     field_ref = decl;
8985   else if (JDECL_P (decl))
8986     {
8987       if (!type_found)
8988         type_found = DECL_CONTEXT (decl);
8989       is_static = FIELD_STATIC (decl);
8990       field_ref = build_field_ref ((is_static && !flag_emit_xref? 
8991                                     NULL_TREE : where_found), 
8992                                    type_found, DECL_NAME (decl));
8993       if (field_ref == error_mark_node)
8994         return error_mark_node;
8995       if (is_static)
8996         field_ref = maybe_build_class_init_for_field (decl, field_ref);
8997     }
8998   else
8999     field_ref = decl;
9000
9001   if (field_decl)
9002     *field_decl = decl;
9003   if (field_type)
9004     *field_type = (QUAL_DECL_TYPE (decl) ? 
9005                    QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9006   return field_ref;
9007 }
9008
9009 /* If NODE is an access to f static field, strip out the class
9010    initialization part and return the field decl, otherwise, return
9011    NODE. */
9012
9013 static tree
9014 strip_out_static_field_access_decl (node)
9015     tree node;
9016 {
9017   if (TREE_CODE (node) == COMPOUND_EXPR)
9018     {
9019       tree op1 = TREE_OPERAND (node, 1);
9020       if (TREE_CODE (op1) == COMPOUND_EXPR)
9021          {
9022            tree call = TREE_OPERAND (op1, 0);
9023            if (TREE_CODE (call) == CALL_EXPR
9024                && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9025                && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9026                == soft_initclass_node)
9027              return TREE_OPERAND (op1, 1);
9028          }
9029       else if (JDECL_P (op1))
9030         return op1;
9031     }
9032   return node;
9033 }
9034
9035 /* 6.5.5.2: Qualified Expression Names */
9036
9037 static int
9038 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9039      tree wfl;
9040      tree *found_decl, *type_found, *where_found;
9041 {
9042   int from_type = 0;            /* Field search initiated from a type */
9043   int from_super = 0, from_cast = 0, from_qualified_this = 0;
9044   int previous_call_static = 0;
9045   int is_static;
9046   tree decl = NULL_TREE, type = NULL_TREE, q;
9047   /* For certain for of inner class instantiation */
9048   tree saved_current, saved_this;               
9049 #define RESTORE_THIS_AND_CURRENT_CLASS                          \
9050   { current_class = saved_current; current_this = saved_this;}
9051
9052   *type_found = *where_found = NULL_TREE;
9053
9054   for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9055     {
9056       tree qual_wfl = QUAL_WFL (q);
9057       tree ret_decl;            /* for EH checking */
9058       int location;             /* for EH checking */
9059
9060       /* 15.10.1 Field Access Using a Primary */
9061       switch (TREE_CODE (qual_wfl))
9062         {
9063         case CALL_EXPR:
9064         case NEW_CLASS_EXPR:
9065           /* If the access to the function call is a non static field,
9066              build the code to access it. */
9067           if (JDECL_P (decl) && !FIELD_STATIC (decl))
9068             {
9069               decl = maybe_access_field (decl, *where_found, 
9070                                          DECL_CONTEXT (decl));
9071               if (decl == error_mark_node)
9072                 return 1;
9073             }
9074
9075           /* And code for the function call */
9076           if (complete_function_arguments (qual_wfl))
9077             return 1;
9078
9079           /* We might have to setup a new current class and a new this
9080              for the search of an inner class, relative to the type of
9081              a expression resolved as `decl'. The current values are
9082              saved and restored shortly after */
9083           saved_current = current_class;
9084           saved_this = current_this;
9085           if (decl 
9086               && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9087                   || from_qualified_this))
9088             {
9089               /* If we still have `from_qualified_this', we have the form
9090                  <T>.this.f() and we need to build <T>.this */
9091               if (from_qualified_this)
9092                 {
9093                   decl = build_access_to_thisn (current_class, type, 0);
9094                   decl = java_complete_tree (decl);
9095                   type = TREE_TYPE (TREE_TYPE (decl));
9096                 }
9097               current_class = type;
9098               current_this = decl;
9099               from_qualified_this = 0;
9100             }
9101
9102           if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9103             CALL_USING_SUPER (qual_wfl) = 1;
9104           location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9105                       EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9106           *where_found = patch_method_invocation (qual_wfl, decl, type,
9107                                                   from_super,
9108                                                   &is_static, &ret_decl);
9109           if (*where_found == error_mark_node)
9110             {
9111               RESTORE_THIS_AND_CURRENT_CLASS;
9112               return 1;
9113             }
9114           *type_found = type = QUAL_DECL_TYPE (*where_found);
9115
9116           /* If we're creating an inner class instance, check for that
9117              an enclosing instance is in scope */
9118           if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9119               && INNER_ENCLOSING_SCOPE_CHECK (type))
9120             {
9121               parse_error_context 
9122                 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9123                  lang_printable_name (type, 0),
9124                  (!current_this ? "" :
9125                   "; an explicit one must be provided when creating this inner class"));
9126               RESTORE_THIS_AND_CURRENT_CLASS;
9127               return 1;
9128             }
9129
9130           /* In case we had to change then to resolve a inner class
9131              instantiation using a primary qualified by a `new' */
9132           RESTORE_THIS_AND_CURRENT_CLASS;
9133
9134           /* EH check. No check on access$<n> functions */
9135           if (location 
9136               && !OUTER_FIELD_ACCESS_IDENTIFIER_P 
9137                     (DECL_NAME (current_function_decl)))
9138             check_thrown_exceptions (location, ret_decl);
9139
9140           /* If the previous call was static and this one is too,
9141              build a compound expression to hold the two (because in
9142              that case, previous function calls aren't transported as
9143              forcoming function's argument. */
9144           if (previous_call_static && is_static)
9145             {
9146               decl = build (COMPOUND_EXPR, type, decl, *where_found);
9147               TREE_SIDE_EFFECTS (decl) = 1;
9148             }
9149           else
9150             {
9151               previous_call_static = is_static;
9152               decl = *where_found;
9153             }
9154           from_type = 0;
9155           continue;
9156
9157         case NEW_ARRAY_EXPR:
9158         case NEW_ANONYMOUS_ARRAY_EXPR:
9159           *where_found = decl = java_complete_tree (qual_wfl);
9160           if (decl == error_mark_node)
9161             return 1;
9162           *type_found = type = QUAL_DECL_TYPE (decl);
9163           continue;
9164
9165         case CONVERT_EXPR:
9166           *where_found = decl = java_complete_tree (qual_wfl);
9167           if (decl == error_mark_node)
9168             return 1;
9169           *type_found = type = QUAL_DECL_TYPE (decl);
9170           from_cast = 1;
9171           continue;
9172
9173         case CONDITIONAL_EXPR:
9174         case STRING_CST:
9175         case MODIFY_EXPR:
9176           *where_found = decl = java_complete_tree (qual_wfl);
9177           if (decl == error_mark_node)
9178             return 1;
9179           *type_found = type = QUAL_DECL_TYPE (decl);
9180           continue;
9181
9182         case ARRAY_REF:
9183           /* If the access to the function call is a non static field,
9184              build the code to access it. */
9185           if (JDECL_P (decl) && !FIELD_STATIC (decl))
9186             {
9187               decl = maybe_access_field (decl, *where_found, type);
9188               if (decl == error_mark_node)
9189                 return 1;
9190             }
9191           /* And code for the array reference expression */
9192           decl = java_complete_tree (qual_wfl);
9193           if (decl == error_mark_node)
9194             return 1;
9195           type = QUAL_DECL_TYPE (decl);
9196           continue;
9197
9198         case PLUS_EXPR:
9199           if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9200             return 1;
9201           if ((type = patch_string (decl)))
9202             decl = type;
9203           *where_found = QUAL_RESOLUTION (q) = decl;
9204           *type_found = type = TREE_TYPE (decl);
9205           break;
9206
9207         case CLASS_LITERAL:
9208           if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9209             return 1;
9210           *where_found = QUAL_RESOLUTION (q) = decl;
9211           *type_found = type = TREE_TYPE (decl);
9212           break;
9213
9214         default:
9215           /* Fix for -Wall Just go to the next statement. Don't
9216              continue */
9217           break;
9218         }
9219
9220       /* If we fall here, we weren't processing a (static) function call. */
9221       previous_call_static = 0;
9222
9223       /* It can be the keyword THIS */
9224       if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9225         {
9226           if (!current_this)
9227             {
9228               parse_error_context 
9229                 (wfl, "Keyword `this' used outside allowed context");
9230               return 1;
9231             }
9232           if (ctxp->explicit_constructor_p
9233               && type == current_class)
9234             {
9235               parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9236               return 1;
9237             }
9238           /* We have to generate code for intermediate acess */
9239           if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9240             {
9241               *where_found = decl = current_this;
9242               *type_found = type = QUAL_DECL_TYPE (decl);
9243             }
9244           /* We're trying to access the this from somewhere else. Make sure
9245              it's allowed before doing so. */
9246           else
9247             {
9248               if (!enclosing_context_p (type, current_class))
9249                 {
9250                   char *p  = xstrdup (lang_printable_name (type, 0));
9251                   parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'", 
9252                                        p, p, 
9253                                        lang_printable_name (current_class, 0));
9254                   free (p);
9255                   return 1;
9256                 }
9257               from_qualified_this = 1;
9258               /* If there's nothing else after that, we need to
9259                  produce something now, otherwise, the section of the
9260                  code that needs to produce <T>.this will generate
9261                  what is necessary. */
9262               if (!TREE_CHAIN (q))
9263                 {
9264                   decl = build_access_to_thisn (current_class, type, 0);
9265                   *where_found = decl = java_complete_tree (decl);
9266                   *type_found = type = TREE_TYPE (decl);
9267                 }
9268             }
9269
9270           from_type = 0;
9271           continue;
9272         }
9273
9274       /* 15.10.2 Accessing Superclass Members using SUPER */
9275       if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9276         {
9277           tree node;
9278           /* Check on the restricted use of SUPER */
9279           if (METHOD_STATIC (current_function_decl)
9280               || current_class == object_type_node)
9281             {
9282               parse_error_context 
9283                 (wfl, "Keyword `super' used outside allowed context");
9284               return 1;
9285             }
9286           /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9287           node = build_cast (EXPR_WFL_LINECOL (qual_wfl), 
9288                              CLASSTYPE_SUPER (current_class),
9289                              build_this (EXPR_WFL_LINECOL (qual_wfl)));
9290           *where_found = decl = java_complete_tree (node);
9291           if (decl == error_mark_node)
9292             return 1;
9293           *type_found = type = QUAL_DECL_TYPE (decl);
9294           from_super = from_type = 1;
9295           continue;
9296         }
9297
9298       /* 15.13.1: Can't search for field name in packages, so we
9299          assume a variable/class name was meant. */
9300       if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9301         {
9302           tree name = resolve_package (wfl, &q);
9303           if (name)
9304             {
9305               tree list;
9306               *where_found = decl = resolve_no_layout (name, qual_wfl);
9307               /* We want to be absolutely sure that the class is laid
9308                  out. We're going to search something inside it. */
9309               *type_found = type = TREE_TYPE (decl);
9310               layout_class (type);
9311               from_type = 1;
9312
9313               /* Fix them all the way down, if any are left. */
9314               if (q)
9315                 {
9316                   list = TREE_CHAIN (q);
9317                   while (list)
9318                     {
9319                       RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9320                       RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9321                       list = TREE_CHAIN (list);
9322                     }
9323                 }
9324             }
9325           else
9326             {
9327               if (from_super || from_cast)
9328                 parse_error_context 
9329                   ((from_cast ? qual_wfl : wfl),
9330                    "No variable `%s' defined in class `%s'",
9331                    IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9332                    lang_printable_name (type, 0));
9333               else
9334                 parse_error_context
9335                   (qual_wfl, "Undefined variable or class name: `%s'",
9336                    IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9337               return 1;
9338             }
9339         }
9340
9341       /* We have a type name. It's been already resolved when the
9342          expression was qualified. */
9343       else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9344         {
9345           if (!(decl = QUAL_RESOLUTION (q)))
9346             return 1;           /* Error reported already */
9347
9348           /* Sneak preview. If next we see a `new', we're facing a
9349              qualification with resulted in a type being selected
9350              instead of a field.  Report the error */
9351           if(TREE_CHAIN (q) 
9352              && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9353             {
9354               parse_error_context (qual_wfl, "Undefined variable `%s'",
9355                                    IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9356               return 1;
9357             }
9358
9359           if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
9360             {
9361               parse_error_context 
9362                 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9363                  java_accstring_lookup (get_access_flags_from_decl (decl)),
9364                  GET_TYPE_NAME (type),
9365                  IDENTIFIER_POINTER (DECL_NAME (decl)),
9366                  IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9367               return 1;
9368             }
9369           check_deprecation (qual_wfl, decl);
9370
9371           type = TREE_TYPE (decl);
9372           from_type = 1;
9373         }
9374       /* We resolve and expression name */
9375       else 
9376         {
9377           tree field_decl = NULL_TREE;
9378
9379           /* If there exists an early resolution, use it. That occurs
9380              only once and we know that there are more things to
9381              come. Don't do that when processing something after SUPER
9382              (we need more thing to be put in place below */
9383           if (!from_super && QUAL_RESOLUTION (q))
9384             {
9385               decl = QUAL_RESOLUTION (q);
9386               if (!type)
9387                 {
9388                   if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9389                     {
9390                       if (current_this)
9391                         *where_found = current_this;
9392                       else
9393                         {
9394                           static_ref_err (qual_wfl, DECL_NAME (decl),
9395                                           current_class);
9396                           return 1;
9397                         }
9398                       if (outer_field_access_p (current_class, decl))
9399                         decl = build_outer_field_access (qual_wfl, decl);
9400                     }
9401                   else
9402                     {
9403                       *where_found = TREE_TYPE (decl);
9404                       if (TREE_CODE (*where_found) == POINTER_TYPE)
9405                         *where_found = TREE_TYPE (*where_found);
9406                     }
9407                 }
9408             }
9409
9410           /* We have to search for a field, knowing the type of its
9411              container. The flag FROM_TYPE indicates that we resolved
9412              the last member of the expression as a type name, which
9413              means that for the resolution of this field, we'll look
9414              for other errors than if it was resolved as a member of
9415              an other field. */
9416           else
9417             {
9418               int is_static;
9419               tree field_decl_type; /* For layout */
9420
9421               if (!from_type && !JREFERENCE_TYPE_P (type))
9422                 {
9423                   parse_error_context 
9424                     (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9425                      IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9426                      lang_printable_name (type, 0),
9427                      IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9428                   return 1;
9429                 }
9430               
9431               field_decl = lookup_field_wrapper (type,
9432                                                  EXPR_WFL_NODE (qual_wfl));
9433
9434               /* Maybe what we're trying to access to is an inner
9435                  class, only if decl is a TYPE_DECL. */
9436               if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
9437                 {
9438                   tree ptr, inner_decl;
9439
9440                   BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9441                   inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9442                   if (inner_decl)
9443                     {
9444                       check_inner_class_access (inner_decl, decl, qual_wfl); 
9445                       type = TREE_TYPE (inner_decl);
9446                       decl = inner_decl;
9447                       from_type = 1;
9448                       continue;
9449                     }
9450                 }
9451
9452               if (field_decl == NULL_TREE)
9453                 {
9454                   parse_error_context 
9455                     (qual_wfl, "No variable `%s' defined in type `%s'",
9456                      IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)), 
9457                      GET_TYPE_NAME (type));
9458                   return 1;
9459                 }
9460               if (field_decl == error_mark_node)
9461                 return 1;
9462
9463               /* Layout the type of field_decl, since we may need
9464                  it. Don't do primitive types or loaded classes. The
9465                  situation of non primitive arrays may not handled
9466                  properly here. FIXME */
9467               if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9468                 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9469               else
9470                 field_decl_type = TREE_TYPE (field_decl);
9471               if (!JPRIMITIVE_TYPE_P (field_decl_type) 
9472                   && !CLASS_LOADED_P (field_decl_type)
9473                   && !TYPE_ARRAY_P (field_decl_type))
9474                 resolve_and_layout (field_decl_type, NULL_TREE);
9475               
9476               /* Check on accessibility here */
9477               if (not_accessible_p (current_class, field_decl,
9478                                     DECL_CONTEXT (field_decl), from_super))
9479                 {
9480                   parse_error_context 
9481                     (qual_wfl,
9482                      "Can't access %s field `%s.%s' from `%s'",
9483                      java_accstring_lookup 
9484                        (get_access_flags_from_decl (field_decl)),
9485                      GET_TYPE_NAME (type),
9486                      IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9487                      IDENTIFIER_POINTER 
9488                        (DECL_NAME (TYPE_NAME (current_class))));
9489                   return 1;
9490                 }
9491               check_deprecation (qual_wfl, field_decl);
9492               
9493               /* There are things to check when fields are accessed
9494                  from type. There are no restrictions on a static
9495                  declaration of the field when it is accessed from an
9496                  interface */
9497               is_static = FIELD_STATIC (field_decl);
9498               if (!from_super && from_type 
9499                   && !TYPE_INTERFACE_P (type) 
9500                   && !is_static 
9501                   && (current_function_decl 
9502                       && METHOD_STATIC (current_function_decl)))
9503                 {
9504                   static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9505                   return 1;
9506                 }
9507               from_cast = from_super = 0;
9508
9509               /* It's an access from a type but it isn't static, we
9510                  make it relative to `this'. */
9511               if (!is_static && from_type)
9512                 decl = current_this;
9513
9514               /* If we need to generate something to get a proper
9515                  handle on what this field is accessed from, do it
9516                  now. */
9517               if (!is_static)
9518                 {
9519                   decl = maybe_access_field (decl, *where_found, *type_found);
9520                   if (decl == error_mark_node)
9521                     return 1;
9522                 }
9523
9524               /* We want to keep the location were found it, and the type
9525                  we found. */
9526               *where_found = decl;
9527               *type_found = type;
9528
9529               /* Generate the correct expression for field access from
9530                  qualified this */
9531               if (from_qualified_this)
9532                 {
9533                   field_decl = build_outer_field_access (qual_wfl, field_decl);
9534                   from_qualified_this = 0;
9535                 }
9536
9537               /* This is the decl found and eventually the next one to
9538                  search from */
9539               decl = field_decl;
9540             }
9541           from_type = 0;
9542           type = QUAL_DECL_TYPE (decl);
9543
9544           /* Sneak preview. If decl is qualified by a `new', report
9545              the error here to be accurate on the peculiar construct */
9546           if (TREE_CHAIN (q) 
9547               && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9548               && !JREFERENCE_TYPE_P (type))
9549             {
9550               parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'", 
9551                                    lang_printable_name (type, 0));
9552               return 1;
9553             }
9554         }
9555       /* `q' might have changed due to a after package resolution
9556          re-qualification */
9557       if (!q)
9558         break;
9559     }
9560   *found_decl = decl;
9561   return 0;
9562 }
9563
9564 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9565    can't be accessed from REFERENCE (a record type). If MEMBER
9566    features a protected access, we then use WHERE which, if non null,
9567    holds the type of MEMBER's access that is checked against
9568    6.6.2.1. This function should be used when decl is a field or a
9569    method.  */
9570
9571 static int
9572 not_accessible_p (reference, member, where, from_super)
9573      tree reference, member;
9574      tree where;
9575      int from_super;
9576 {
9577   int access_flag = get_access_flags_from_decl (member);
9578
9579   /* Inner classes are processed by check_inner_class_access */
9580   if (INNER_CLASS_TYPE_P (reference))
9581     return 0;
9582
9583   /* Access always granted for members declared public */
9584   if (access_flag & ACC_PUBLIC)
9585     return 0;
9586   
9587   /* Check access on protected members */
9588   if (access_flag & ACC_PROTECTED)
9589     {
9590       /* Access granted if it occurs from within the package
9591          containing the class in which the protected member is
9592          declared */
9593       if (class_in_current_package (DECL_CONTEXT (member)))
9594         return 0;
9595
9596       /* If accessed with the form `super.member', then access is granted */
9597       if (from_super)
9598         return 0;
9599
9600       /* If where is active, access was made through a
9601          qualifier. Access is granted if the type of the qualifier is
9602          or is a sublass of the type the access made from (6.6.2.1.)  */
9603       if (where && !inherits_from_p (reference, where))
9604         return 1;
9605
9606       /* Otherwise, access is granted if occuring from the class where
9607          member is declared or a subclass of it. Find the right
9608          context to perform the check */
9609       if (PURE_INNER_CLASS_TYPE_P (reference))
9610         {
9611           while (INNER_CLASS_TYPE_P (reference))
9612             {
9613               if (inherits_from_p (reference, DECL_CONTEXT (member)))
9614                 return 0;
9615               reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9616             }
9617         }
9618       if (inherits_from_p (reference, DECL_CONTEXT (member)))
9619         return 0;
9620       return 1;
9621     }
9622
9623   /* Check access on private members. Access is granted only if it
9624      occurs from within the class in which it is declared -- that does
9625      it for innerclasses too. */
9626   if (access_flag & ACC_PRIVATE)
9627     {
9628       if (reference == DECL_CONTEXT (member))
9629         return 0;
9630       if (enclosing_context_p (reference, DECL_CONTEXT (member)))
9631         return 0;
9632       return 1;
9633     }
9634
9635   /* Default access are permitted only when occuring within the
9636      package in which the type (REFERENCE) is declared. In other words,
9637      REFERENCE is defined in the current package */
9638   if (ctxp->package)
9639     return !class_in_current_package (reference);
9640
9641   /* Otherwise, access is granted */
9642   return 0;
9643 }
9644
9645 /* Test deprecated decl access.  */
9646 static void
9647 check_deprecation (wfl, decl)
9648      tree wfl, decl;
9649 {
9650   const char *file = DECL_SOURCE_FILE (decl);
9651   /* Complain if the field is deprecated and the file it was defined
9652      in isn't compiled at the same time the file which contains its
9653      use is */
9654   if (DECL_DEPRECATED (decl) 
9655       && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9656     {
9657       char the [20];
9658       switch (TREE_CODE (decl))
9659         {
9660         case FUNCTION_DECL:
9661           strcpy (the, "method");
9662           break;
9663         case FIELD_DECL:
9664           strcpy (the, "field");
9665           break;
9666         case TYPE_DECL:
9667           strcpy (the, "class");
9668           break;
9669         default:
9670           abort ();
9671         }
9672       parse_warning_context 
9673         (wfl, "The %s `%s' in class `%s' has been deprecated", 
9674          the, lang_printable_name (decl, 0),
9675          IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9676     }
9677 }
9678
9679 /* Returns 1 if class was declared in the current package, 0 otherwise */
9680
9681 static int
9682 class_in_current_package (class)
9683      tree class;
9684 {
9685   static tree cache = NULL_TREE;
9686   int qualified_flag;
9687   tree left;
9688
9689   if (cache == class)
9690     return 1;
9691
9692   qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9693
9694   /* If the current package is empty and the name of CLASS is
9695      qualified, class isn't in the current package.  If there is a
9696      current package and the name of the CLASS is not qualified, class
9697      isn't in the current package */
9698   if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
9699     return 0;
9700
9701   /* If there is not package and the name of CLASS isn't qualified,
9702      they belong to the same unnamed package */
9703   if (!ctxp->package && !qualified_flag)
9704     return 1;
9705
9706   /* Compare the left part of the name of CLASS with the package name */
9707   breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9708   if (ctxp->package == left)
9709     {
9710       static int initialized_p;
9711       /* Register CACHE with the garbage collector.  */
9712       if (!initialized_p)
9713         {
9714           ggc_add_tree_root (&cache, 1);
9715           initialized_p = 1;
9716         }
9717
9718       cache = class;
9719       return 1;
9720     }
9721   return 0;
9722 }
9723
9724 /* This function may generate code to access DECL from WHERE. This is
9725    done only if certain conditions meet.  */
9726
9727 static tree
9728 maybe_access_field (decl, where, type)
9729   tree decl, where, type;
9730 {
9731   if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9732       && !FIELD_STATIC (decl))
9733     decl = build_field_ref (where ? where : current_this, 
9734                             (type ? type : DECL_CONTEXT (decl)),
9735                             DECL_NAME (decl));
9736   return decl;
9737 }
9738
9739 /* Build a method invocation, by patching PATCH. If non NULL
9740    and according to the situation, PRIMARY and WHERE may be
9741    used. IS_STATIC is set to 1 if the invoked function is static. */
9742
9743 static tree
9744 patch_method_invocation (patch, primary, where, from_super,
9745                         is_static, ret_decl)
9746      tree patch, primary, where;
9747      int from_super;
9748      int *is_static;
9749      tree *ret_decl;
9750 {
9751   tree wfl = TREE_OPERAND (patch, 0);
9752   tree args = TREE_OPERAND (patch, 1);
9753   tree name = EXPR_WFL_NODE (wfl);
9754   tree list;
9755   int is_static_flag = 0;
9756   int is_super_init = 0;
9757   tree this_arg = NULL_TREE;
9758   int is_array_clone_call = 0;
9759   
9760   /* Should be overriden if everything goes well. Otherwise, if
9761      something fails, it should keep this value. It stop the
9762      evaluation of a bogus assignment. See java_complete_tree,
9763      MODIFY_EXPR: for the reasons why we sometimes want to keep on
9764      evaluating an assignment */
9765   TREE_TYPE (patch) = error_mark_node;
9766
9767   /* Since lookup functions are messing with line numbers, save the
9768      context now.  */
9769   java_parser_context_save_global ();
9770
9771   /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9772
9773   /* Resolution of qualified name, excluding constructors */
9774   if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9775     {
9776       tree identifier, identifier_wfl, type, resolved;
9777       /* Extract the last IDENTIFIER of the qualified
9778          expression. This is a wfl and we will use it's location
9779          data during error report. */
9780       identifier_wfl = cut_identifier_in_qualified (wfl);
9781       identifier = EXPR_WFL_NODE (identifier_wfl);
9782       
9783       /* Given the context, IDENTIFIER is syntactically qualified
9784          as a MethodName. We need to qualify what's before */
9785       qualify_ambiguous_name (wfl);
9786       resolved = resolve_field_access (wfl, NULL, NULL);
9787
9788       if (resolved == error_mark_node)
9789         PATCH_METHOD_RETURN_ERROR ();
9790
9791       type = GET_SKIP_TYPE (resolved);
9792       resolve_and_layout (type, NULL_TREE);
9793       
9794       if (JPRIMITIVE_TYPE_P (type))
9795         {
9796           parse_error_context
9797             (identifier_wfl,
9798              "Can't invoke a method on primitive type `%s'",
9799              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9800           PATCH_METHOD_RETURN_ERROR ();         
9801         }
9802
9803       list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9804       args = nreverse (args);
9805
9806       /* We're resolving a call from a type */
9807       if (TREE_CODE (resolved) == TYPE_DECL)
9808         {
9809           if (CLASS_INTERFACE (resolved))
9810             {
9811               parse_error_context
9812                 (identifier_wfl,
9813                 "Can't make static reference to method `%s' in interface `%s'",
9814                  IDENTIFIER_POINTER (identifier), 
9815                  IDENTIFIER_POINTER (name));
9816               PATCH_METHOD_RETURN_ERROR ();
9817             }
9818           if (list && !METHOD_STATIC (list))
9819             {
9820               char *fct_name = xstrdup (lang_printable_name (list, 0));
9821               parse_error_context 
9822                 (identifier_wfl,
9823                  "Can't make static reference to method `%s %s' in class `%s'",
9824                  lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0), 
9825                  fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9826               free (fct_name);
9827               PATCH_METHOD_RETURN_ERROR ();
9828             }
9829         }
9830       else
9831         this_arg = primary = resolved;
9832       
9833       if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
9834         is_array_clone_call = 1;
9835       
9836       /* IDENTIFIER_WFL will be used to report any problem further */
9837       wfl = identifier_wfl;
9838     }
9839   /* Resolution of simple names, names generated after a primary: or
9840      constructors */
9841   else
9842     {
9843       tree class_to_search = NULL_TREE;
9844       int lc;                   /* Looking for Constructor */
9845       
9846       /* We search constructor in their target class */
9847       if (CALL_CONSTRUCTOR_P (patch))
9848         {
9849           if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9850             class_to_search = EXPR_WFL_NODE (wfl);
9851           else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) == 
9852                    this_identifier_node)
9853             class_to_search = NULL_TREE;
9854           else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9855                    super_identifier_node)
9856             {
9857               is_super_init = 1;
9858               if (CLASSTYPE_SUPER (current_class))
9859                 class_to_search = 
9860                   DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9861               else
9862                 {
9863                   parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
9864                   PATCH_METHOD_RETURN_ERROR ();
9865                 }
9866             }
9867
9868           /* Class to search is NULL if we're searching the current one */
9869           if (class_to_search)
9870             {
9871               class_to_search = resolve_and_layout (class_to_search, wfl);
9872
9873               if (!class_to_search)
9874                 {
9875                   parse_error_context 
9876                     (wfl, "Class `%s' not found in type declaration",
9877                      IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9878                   PATCH_METHOD_RETURN_ERROR ();
9879                 }
9880               
9881               /* Can't instantiate an abstract class, but we can
9882                  invoke it's constructor. It's use within the `new'
9883                  context is denied here. */
9884               if (CLASS_ABSTRACT (class_to_search) 
9885                   && TREE_CODE (patch) == NEW_CLASS_EXPR)
9886                 {
9887                   parse_error_context 
9888                     (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9889                      IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9890                   PATCH_METHOD_RETURN_ERROR ();
9891                 }
9892
9893               class_to_search = TREE_TYPE (class_to_search);
9894             }
9895           else
9896             class_to_search = current_class;
9897           lc = 1;
9898         }
9899       /* This is a regular search in the local class, unless an
9900          alternate class is specified. */
9901       else
9902         {
9903           if (where != NULL_TREE)
9904             class_to_search = where;
9905           else if (QUALIFIED_P (name))
9906             class_to_search = current_class;
9907           else
9908             {
9909               class_to_search = current_class;
9910
9911               for (;;)
9912                 {
9913                   if (has_method (class_to_search, name))
9914                     break;
9915                   if (! INNER_CLASS_TYPE_P (class_to_search))
9916                     {
9917                       parse_error_context (wfl,
9918                                            "No method named `%s' in scope",
9919                                            IDENTIFIER_POINTER (name));
9920                       PATCH_METHOD_RETURN_ERROR ();
9921                     }
9922                   class_to_search
9923                     = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
9924                 }
9925             }
9926           lc = 0;
9927         }
9928
9929       /* NAME is a simple identifier or comes from a primary. Search
9930          in the class whose declaration contain the method being
9931          invoked. */
9932       resolve_and_layout (class_to_search, NULL_TREE);
9933
9934       list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
9935       /* Don't continue if no method were found, as the next statement
9936          can't be executed then. */
9937       if (!list)
9938         PATCH_METHOD_RETURN_ERROR ();
9939       
9940       if (TYPE_ARRAY_P (class_to_search)
9941           && DECL_NAME (list) == get_identifier ("clone"))
9942         is_array_clone_call = 1;
9943
9944       /* Check for static reference if non static methods */
9945       if (check_for_static_method_reference (wfl, patch, list, 
9946                                              class_to_search, primary))
9947         PATCH_METHOD_RETURN_ERROR ();
9948
9949       /* Check for inner classes creation from illegal contexts */
9950       if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9951                  && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9952           && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9953         {
9954           parse_error_context 
9955             (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9956              lang_printable_name (class_to_search, 0),
9957              (!current_this ? "" :
9958               "; an explicit one must be provided when creating this inner class"));
9959           PATCH_METHOD_RETURN_ERROR ();
9960         }
9961
9962       /* Non static methods are called with the current object extra
9963          argument. If patch a `new TYPE()', the argument is the value
9964          returned by the object allocator. If method is resolved as a
9965          primary, use the primary otherwise use the current THIS. */
9966       args = nreverse (args);
9967       if (TREE_CODE (patch) != NEW_CLASS_EXPR)
9968         {
9969           this_arg = primary ? primary : current_this;
9970
9971           /* If we're using an access method, things are different.
9972              There are two familly of cases:
9973
9974              1) We're not generating bytecodes:
9975
9976              - LIST is non static. It's invocation is transformed from
9977                x(a1,...,an) into this$<n>.x(a1,....an).
9978              - LIST is static. It's invocation is transformed from
9979                x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9980
9981              2) We're generating bytecodes:
9982              
9983              - LIST is non static. It's invocation is transformed from
9984                x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9985              - LIST is static. It's invocation is transformed from
9986                x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9987
9988              Of course, this$<n> can be abitrary complex, ranging from
9989              this$0 (the immediate outer context) to 
9990              access$0(access$0(...(this$0))). 
9991              
9992              maybe_use_access_method returns a non zero value if the
9993              this_arg has to be moved into the (then generated) stub
9994              argument list. In the meantime, the selected function
9995              might have be replaced by a generated stub. */
9996           if (maybe_use_access_method (is_super_init, &list, &this_arg))
9997             {
9998               args = tree_cons (NULL_TREE, this_arg, args);
9999               this_arg = NULL_TREE; /* So it doesn't get chained twice */
10000             }
10001         }
10002     }
10003
10004   /* Merge point of all resolution schemes. If we have nothing, this
10005      is an error, already signaled */
10006   if (!list) 
10007     PATCH_METHOD_RETURN_ERROR ();
10008
10009   /* Check accessibility, position the is_static flag, build and
10010      return the call */
10011   if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10012                         (primary ? TREE_TYPE (TREE_TYPE (primary)) : 
10013                          NULL_TREE), from_super)
10014       /* Calls to clone() on array types are permitted as a special-case. */
10015       && !is_array_clone_call)
10016     {
10017       const char *fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10018       const char *access =
10019         java_accstring_lookup (get_access_flags_from_decl (list));
10020       const char *klass =
10021         IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10022       const char *refklass =
10023         IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10024       const char *what = (DECL_CONSTRUCTOR_P (list)
10025                           ? "constructor" : "method");
10026       /* FIXME: WFL yields the wrong message here but I don't know
10027          what else to use.  */
10028       parse_error_context (wfl,
10029                            "Can't access %s %s `%s.%s' from `%s'",
10030                            access, what, klass, fct_name, refklass);
10031       PATCH_METHOD_RETURN_ERROR ();
10032     }
10033   check_deprecation (wfl, list);
10034
10035   /* If invoking a innerclass constructor, there are hidden parameters
10036      to pass */
10037   if (TREE_CODE (patch) == NEW_CLASS_EXPR 
10038       && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10039     {
10040       /* And make sure we add the accessed local variables to be saved
10041          in field aliases. */
10042       args = build_alias_initializer_parameter_list
10043         (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10044
10045       /* Secretly pass the current_this/primary as a second argument */
10046       if (primary || current_this)
10047         {
10048           tree extra_arg;
10049           tree this_type = (current_this ?
10050                             TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10051           /* Method's (list) enclosing context */
10052           tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10053           /* If we have a primary, use it. */
10054           if (primary)
10055             extra_arg = primary;
10056           /* The current `this' is an inner class but isn't a direct
10057              enclosing context for the inner class we're trying to
10058              create. Build an access to the proper enclosing context
10059              and use it. */
10060           else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10061                    && this_type != TREE_TYPE (mec))
10062             {
10063
10064               extra_arg = build_access_to_thisn (current_class,
10065                                                  TREE_TYPE (mec), 0);
10066               extra_arg = java_complete_tree (extra_arg);
10067             }
10068           /* Otherwise, just use the current `this' as an enclosing
10069              context. */
10070           else
10071             extra_arg = current_this;
10072           args = tree_cons (NULL_TREE, extra_arg, args);
10073         }
10074       else
10075         args = tree_cons (NULL_TREE, integer_zero_node, args);
10076     }
10077
10078   /* This handles the situation where a constructor invocation needs
10079      to have an enclosing context passed as a second parameter (the
10080      constructor is one of an inner class. We extract it from the
10081      current function.  */
10082   if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10083     {
10084       tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
10085       tree extra_arg;
10086
10087       if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
10088         {
10089           extra_arg = DECL_FUNCTION_BODY (current_function_decl);
10090           extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
10091         }
10092       else
10093         {
10094           tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
10095           extra_arg = 
10096             build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
10097           extra_arg = java_complete_tree (extra_arg);
10098         }
10099       args = tree_cons (NULL_TREE, extra_arg, args);
10100     }
10101
10102   is_static_flag = METHOD_STATIC (list);
10103   if (! is_static_flag && this_arg != NULL_TREE)
10104     args = tree_cons (NULL_TREE, this_arg, args);
10105
10106   /* In the context of an explicit constructor invocation, we can't
10107      invoke any method relying on `this'. Exceptions are: we're
10108      invoking a static function, primary exists and is not the current
10109      this, we're creating a new object. */
10110   if (ctxp->explicit_constructor_p 
10111       && !is_static_flag 
10112       && (!primary || primary == current_this)
10113       && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10114     {
10115       parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
10116       PATCH_METHOD_RETURN_ERROR ();
10117     }
10118   java_parser_context_restore_global ();
10119   if (is_static) 
10120     *is_static = is_static_flag;
10121   /* Sometimes, we want the decl of the selected method. Such as for
10122      EH checking */
10123   if (ret_decl)
10124     *ret_decl = list;
10125   patch = patch_invoke (patch, list, args);
10126   if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10127     {
10128       tree finit_parms, finit_call;
10129       
10130       /* Prepare to pass hidden parameters to finit$, if any. */
10131       finit_parms = build_alias_initializer_parameter_list 
10132         (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10133       
10134       finit_call = 
10135         build_method_invocation (build_wfl_node (finit_identifier_node),
10136                                  finit_parms);
10137
10138       /* Generate the code used to initialize fields declared with an
10139          initialization statement and build a compound statement along
10140          with the super constructor invocation. */
10141       patch = build (COMPOUND_EXPR, void_type_node, patch,
10142                      java_complete_tree (finit_call));
10143       CAN_COMPLETE_NORMALLY (patch) = 1;
10144     }
10145   return patch;
10146 }
10147
10148 /* Check that we're not trying to do a static reference to a method in
10149    non static method. Return 1 if it's the case, 0 otherwise. */
10150
10151 static int
10152 check_for_static_method_reference (wfl, node, method, where, primary)
10153      tree wfl, node, method, where, primary;
10154 {
10155   if (METHOD_STATIC (current_function_decl) 
10156       && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10157     {
10158       char *fct_name = xstrdup (lang_printable_name (method, 0));
10159       parse_error_context 
10160         (wfl, "Can't make static reference to method `%s %s' in class `%s'", 
10161          lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10162          IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10163       free (fct_name);
10164       return 1;
10165     }
10166   return 0;
10167 }
10168
10169 /* Fix the invocation of *MDECL if necessary in the case of a
10170    invocation from an inner class. *THIS_ARG might be modified
10171    appropriately and an alternative access to *MDECL might be
10172    returned.  */
10173
10174 static int
10175 maybe_use_access_method (is_super_init, mdecl, this_arg)
10176      int is_super_init;
10177      tree *mdecl, *this_arg;
10178 {
10179   tree ctx;
10180   tree md = *mdecl, ta = *this_arg;
10181   int to_return = 0;
10182   int non_static_context = !METHOD_STATIC (md);
10183
10184   if (is_super_init 
10185       || DECL_CONTEXT (md) == current_class
10186       || !PURE_INNER_CLASS_TYPE_P (current_class) 
10187       || DECL_FINIT_P (md))
10188     return 0;
10189   
10190   /* If we're calling a method found in an enclosing class, generate
10191      what it takes to retrieve the right this. Don't do that if we're
10192      invoking a static method. Note that if MD's type is unrelated to
10193      CURRENT_CLASS, then the current this can be used. */
10194
10195   if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10196     {
10197       ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10198       if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10199         {
10200           ta = build_current_thisn (current_class);
10201           ta = build_wfl_node (ta);
10202         }
10203       else
10204         {
10205           tree type = ctx;
10206           while (type)
10207             {
10208               maybe_build_thisn_access_method (type);
10209               if (inherits_from_p (type, DECL_CONTEXT (md)))
10210                 {
10211                   ta = build_access_to_thisn (ctx, type, 0);
10212                   break;
10213                 }
10214               type = (DECL_CONTEXT (TYPE_NAME (type)) ? 
10215                       TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10216             }
10217         }
10218       ta = java_complete_tree (ta);
10219     }
10220
10221   /* We might have to use an access method to get to MD. We can
10222      break the method access rule as far as we're not generating
10223      bytecode */
10224   if (METHOD_PRIVATE (md) && flag_emit_class_files)
10225     {
10226       md = build_outer_method_access_method (md);
10227       to_return = 1;
10228     }
10229
10230   *mdecl = md;
10231   *this_arg = ta;
10232
10233   /* Returnin a non zero value indicates we were doing a non static
10234      method invokation that is now a static invocation. It will have
10235      callee displace `this' to insert it in the regular argument
10236      list. */
10237   return (non_static_context && to_return);
10238 }
10239
10240 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10241    mode.  */
10242
10243 static tree
10244 patch_invoke (patch, method, args)
10245      tree patch, method, args;
10246 {
10247   tree dtable, func;
10248   tree original_call, t, ta;
10249   tree check = NULL_TREE;
10250
10251   /* Last step for args: convert build-in types. If we're dealing with
10252      a new TYPE() type call, the first argument to the constructor
10253      isn't found in the incoming argument list, but delivered by
10254      `new' */
10255   t = TYPE_ARG_TYPES (TREE_TYPE (method));
10256   if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10257     t = TREE_CHAIN (t);
10258   for (ta = args; t != end_params_node && ta; 
10259        t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10260     if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10261         TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10262       TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10263
10264   /* Resolve unresolved returned type isses */
10265   t = TREE_TYPE (TREE_TYPE (method));
10266   if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10267     resolve_and_layout (TREE_TYPE (t), NULL);
10268
10269   if (flag_emit_class_files || flag_emit_xref)
10270     func = method;
10271   else
10272     {
10273       tree signature = build_java_signature (TREE_TYPE (method));
10274       switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10275         {
10276         case INVOKE_VIRTUAL:
10277           dtable = invoke_build_dtable (0, args);
10278           func = build_invokevirtual (dtable, method);
10279           break;
10280
10281         case INVOKE_NONVIRTUAL:
10282           /* If the object for the method call is null, we throw an
10283              exception.  We don't do this if the object is the current
10284              method's `this'.  In other cases we just rely on an
10285              optimization pass to eliminate redundant checks.  */
10286           if (TREE_VALUE (args) != current_this)
10287             {
10288               /* We use a save_expr here to make sure we only evaluate
10289                  the new `self' expression once.  */
10290               tree save_arg = save_expr (TREE_VALUE (args));
10291               TREE_VALUE (args) = save_arg;
10292               check = java_check_reference (save_arg, 1);
10293             }
10294           /* Fall through.  */
10295
10296         case INVOKE_SUPER:
10297         case INVOKE_STATIC:
10298           func = build_known_method_ref (method, TREE_TYPE (method),
10299                                          DECL_CONTEXT (method),
10300                                          signature, args);
10301           break;
10302
10303         case INVOKE_INTERFACE:
10304           dtable = invoke_build_dtable (1, args);
10305           func = build_invokeinterface (dtable, method);
10306           break;
10307
10308         default:
10309           abort ();
10310         }
10311
10312       /* Ensure self_type is initialized, (invokestatic). FIXME */
10313       func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10314     }
10315
10316   TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10317   TREE_OPERAND (patch, 0) = func;
10318   TREE_OPERAND (patch, 1) = args;
10319   original_call = patch;
10320
10321   /* We're processing a `new TYPE ()' form. New is called and its
10322      returned value is the first argument to the constructor. We build
10323      a COMPOUND_EXPR and use saved expression so that the overall NEW
10324      expression value is a pointer to a newly created and initialized
10325      class. */
10326   if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10327     {
10328       tree class = DECL_CONTEXT (method);
10329       tree c1, saved_new, size, new;
10330       if (flag_emit_class_files || flag_emit_xref)
10331         {
10332           TREE_TYPE (patch) = build_pointer_type (class);
10333           return patch;
10334         }
10335       if (!TYPE_SIZE (class))
10336         safe_layout_class (class);
10337       size = size_in_bytes (class);
10338       new = build (CALL_EXPR, promote_type (class),
10339                    build_address_of (alloc_object_node),
10340                    tree_cons (NULL_TREE, build_class_ref (class),
10341                               build_tree_list (NULL_TREE, 
10342                                                size_in_bytes (class))),
10343                    NULL_TREE);
10344       saved_new = save_expr (new);
10345       c1 = build_tree_list (NULL_TREE, saved_new);
10346       TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10347       TREE_OPERAND (original_call, 1) = c1;
10348       TREE_SET_CODE (original_call, CALL_EXPR);
10349       patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10350     }
10351
10352   /* If CHECK is set, then we are building a check to see if the object
10353      is NULL.  */
10354   if (check != NULL_TREE)
10355     {
10356       patch = build (COMPOUND_EXPR, TREE_TYPE (patch), check, patch);
10357       TREE_SIDE_EFFECTS (patch) = 1;
10358     }
10359
10360   return patch;
10361 }
10362
10363 static int
10364 invocation_mode (method, super)
10365      tree method;
10366      int super;
10367 {
10368   int access = get_access_flags_from_decl (method);
10369
10370   if (super)
10371     return INVOKE_SUPER;
10372
10373   if (access & ACC_STATIC)
10374     return INVOKE_STATIC;
10375
10376   /* We have to look for a constructor before we handle nonvirtual
10377      calls; otherwise the constructor will look nonvirtual.  */
10378   if (DECL_CONSTRUCTOR_P (method))
10379     return INVOKE_STATIC;
10380
10381   if (access & ACC_FINAL || access & ACC_PRIVATE)
10382     return INVOKE_NONVIRTUAL;
10383
10384   if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10385     return INVOKE_NONVIRTUAL;
10386
10387   if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10388     return INVOKE_INTERFACE;
10389
10390   return INVOKE_VIRTUAL;
10391 }
10392
10393 /* Retrieve a refined list of matching methods. It covers the step
10394    15.11.2 (Compile-Time Step 2) */
10395
10396 static tree
10397 lookup_method_invoke (lc, cl, class, name, arg_list)
10398      int lc;
10399      tree cl;
10400      tree class, name, arg_list;
10401 {
10402   tree atl = end_params_node;           /* Arg Type List */
10403   tree method, signature, list, node;
10404   const char *candidates;               /* Used for error report */
10405   char *dup;
10406
10407   /* Fix the arguments */
10408   for (node = arg_list; node; node = TREE_CHAIN (node))
10409     {
10410       tree current_arg = TREE_TYPE (TREE_VALUE (node));
10411       /* Non primitive type may have to be resolved */
10412       if (!JPRIMITIVE_TYPE_P (current_arg))
10413         resolve_and_layout (current_arg, NULL_TREE);
10414       /* And promoted */
10415       if (TREE_CODE (current_arg) == RECORD_TYPE)
10416         current_arg = promote_type (current_arg);
10417       atl = tree_cons (NULL_TREE, current_arg, atl);
10418     }
10419
10420   /* Presto. If we're dealing with an anonymous class and a
10421      constructor call, generate the right constructor now, since we
10422      know the arguments' types. */
10423
10424   if (lc && ANONYMOUS_CLASS_P (class))
10425     craft_constructor (TYPE_NAME (class), atl);
10426
10427   /* Find all candidates and then refine the list, searching for the
10428      most specific method. */
10429   list = find_applicable_accessible_methods_list (lc, class, name, atl);
10430   list = find_most_specific_methods_list (list);
10431   if (list && !TREE_CHAIN (list))
10432     return TREE_VALUE (list);
10433
10434   /* Issue an error. List candidates if any. Candidates are listed
10435      only if accessible (non accessible methods may end-up here for
10436      the sake of a better error report). */
10437   candidates = NULL;
10438   if (list)
10439     {
10440       tree current;
10441       obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10442       for (current = list; current; current = TREE_CHAIN (current))
10443         {
10444           tree cm = TREE_VALUE (current);
10445           char string [4096];
10446           if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
10447             continue;
10448           sprintf 
10449             (string, "  `%s' in `%s'%s",
10450              get_printable_method_name (cm),
10451              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10452              (TREE_CHAIN (current) ? "\n" : ""));
10453           obstack_grow (&temporary_obstack, string, strlen (string));
10454         }
10455       obstack_1grow (&temporary_obstack, '\0');
10456       candidates = obstack_finish (&temporary_obstack);
10457     }
10458   /* Issue the error message */
10459   method = make_node (FUNCTION_TYPE);
10460   TYPE_ARG_TYPES (method) = atl;
10461   signature = build_java_argument_signature (method);
10462   dup = xstrdup (lang_printable_name (class, 0));
10463   parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10464                        (lc ? "constructor" : "method"),
10465                        (lc ? dup : IDENTIFIER_POINTER (name)),
10466                        IDENTIFIER_POINTER (signature), dup,
10467                        (candidates ? candidates : ""));
10468   free (dup);
10469   return NULL_TREE;
10470 }
10471
10472 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10473    when we're looking for a constructor. */
10474
10475 static tree
10476 find_applicable_accessible_methods_list (lc, class, name, arglist)
10477      int lc;
10478      tree class, name, arglist;
10479 {
10480   static struct hash_table t, *searched_classes = NULL;
10481   static int search_not_done = 0;
10482   tree list = NULL_TREE, all_list = NULL_TREE;
10483
10484   /* Check the hash table to determine if this class has been searched 
10485      already. */
10486   if (searched_classes)
10487     {
10488       if (hash_lookup (searched_classes, 
10489                       (const hash_table_key) class, FALSE, NULL))
10490        return NULL;
10491     }
10492   else
10493     {
10494       hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10495                       java_hash_compare_tree_node);
10496       searched_classes = &t;
10497     }
10498     
10499   search_not_done++;
10500   hash_lookup (searched_classes, 
10501                (const hash_table_key) class, TRUE, NULL);
10502
10503   if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10504     {
10505       load_class (class, 1);
10506       safe_layout_class (class);
10507     }
10508
10509   /* Search interfaces */
10510   if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL 
10511       && CLASS_INTERFACE (TYPE_NAME (class)))
10512     {
10513       int i, n;
10514       tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10515       search_applicable_methods_list (lc, TYPE_METHODS (class), 
10516                                       name, arglist, &list, &all_list);
10517       n = TREE_VEC_LENGTH (basetype_vec);
10518       for (i = 1; i < n; i++)
10519         {
10520           tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10521           tree rlist;
10522
10523           rlist = find_applicable_accessible_methods_list (lc,  t, name, 
10524                                                            arglist);
10525           list = chainon (rlist, list);
10526         }
10527     }
10528   /* Search classes */
10529   else
10530     {
10531       search_applicable_methods_list (lc, TYPE_METHODS (class), 
10532                                       name, arglist, &list, &all_list);
10533
10534       /* When looking finit$ or class$, we turn LC to 1 so that we
10535          only search in class. Note that we should have found
10536          something at this point. */
10537       if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name))
10538         {
10539           lc = 1;
10540           if (!list)
10541             abort ();
10542         }
10543
10544       /* We must search all interfaces of this class */
10545       if (!lc)
10546       {
10547         tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10548         int n = TREE_VEC_LENGTH (basetype_vec), i;
10549         for (i = 1; i < n; i++)
10550           {
10551             tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10552             if (t != object_type_node)
10553               {
10554                 tree rlist
10555                   = find_applicable_accessible_methods_list (lc, t,
10556                                                              name, arglist);
10557                 list = chainon (rlist, list);
10558               }
10559           }
10560       }
10561
10562       /* Search superclass */
10563       if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10564         {
10565           tree rlist;
10566           class = CLASSTYPE_SUPER (class);
10567           rlist = find_applicable_accessible_methods_list (lc, class, 
10568                                                            name, arglist);
10569           list = chainon (rlist, list);
10570         }
10571     }
10572
10573   search_not_done--;
10574
10575   /* We're done. Reset the searched classes list and finally search
10576      java.lang.Object if it wasn't searched already. */
10577   if (!search_not_done)
10578     {
10579       if (!lc
10580           && TYPE_METHODS (object_type_node)
10581           && !hash_lookup (searched_classes, 
10582                            (const hash_table_key) object_type_node, 
10583                            FALSE, NULL))
10584         {
10585           search_applicable_methods_list (lc, 
10586                                           TYPE_METHODS (object_type_node),
10587                                           name, arglist, &list, &all_list);
10588         }
10589       hash_table_free (searched_classes);
10590       searched_classes = NULL;
10591     }
10592
10593   /* Either return the list obtained or all selected (but
10594      inaccessible) methods for better error report. */
10595   return (!list ? all_list : list);
10596 }
10597
10598 /* Effectively search for the appropriate method in method */
10599
10600 static void 
10601 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
10602      int lc;
10603      tree method, name, arglist;
10604      tree *list, *all_list;
10605 {
10606   for (; method; method = TREE_CHAIN (method))
10607     {
10608       /* When dealing with constructor, stop here, otherwise search
10609          other classes */
10610       if (lc && !DECL_CONSTRUCTOR_P (method))
10611         continue;
10612       else if (!lc && (DECL_CONSTRUCTOR_P (method) 
10613                        || (DECL_NAME (method) != name)))
10614         continue;
10615
10616       if (argument_types_convertible (method, arglist))
10617         {
10618           /* Retain accessible methods only */
10619           if (!not_accessible_p (DECL_CONTEXT (current_function_decl), 
10620                                  method, NULL_TREE, 0))
10621             *list = tree_cons (NULL_TREE, method, *list);
10622           else
10623             /* Also retain all selected method here */
10624             *all_list = tree_cons (NULL_TREE, method, *list);
10625         }
10626     }
10627 }
10628
10629 /* 15.11.2.2 Choose the Most Specific Method */
10630
10631 static tree
10632 find_most_specific_methods_list (list)
10633      tree list;
10634 {
10635   int max = 0;
10636   int abstract, candidates;
10637   tree current, new_list = NULL_TREE;
10638   for (current = list; current; current = TREE_CHAIN (current))
10639     {
10640       tree method;
10641       DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10642
10643       for (method = list; method; method = TREE_CHAIN (method))
10644         {
10645           tree method_v, current_v;
10646           /* Don't test a method against itself */
10647           if (method == current)
10648             continue;
10649
10650           method_v = TREE_VALUE (method);
10651           current_v = TREE_VALUE (current);
10652
10653           /* Compare arguments and location where methods where declared */
10654           if (argument_types_convertible (method_v, current_v))
10655             {
10656               if (valid_method_invocation_conversion_p 
10657                   (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10658                   || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10659                       && enclosing_context_p (DECL_CONTEXT (method_v),
10660                                               DECL_CONTEXT (current_v))))
10661                 {
10662                   int v = (DECL_SPECIFIC_COUNT (current_v) += 
10663                     (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10664                   max = (v > max ? v : max);
10665                 }
10666             }
10667         }
10668     }
10669
10670   /* Review the list and select the maximally specific methods */
10671   for (current = list, abstract = -1, candidates = -1;
10672        current; current = TREE_CHAIN (current))
10673     if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10674       {
10675         new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10676         abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10677         candidates++;
10678       }
10679
10680   /* If we have several and they're all abstract, just pick the
10681      closest one. */
10682   if (candidates > 0 && (candidates == abstract))
10683     {
10684       new_list = nreverse (new_list);
10685       TREE_CHAIN (new_list) = NULL_TREE;
10686     }
10687
10688   /* We have several (we couldn't find a most specific), all but one
10689      are abstract, we pick the only non abstract one. */
10690   if (candidates > 0 && (candidates == abstract+1))
10691     {
10692       for (current = new_list; current; current = TREE_CHAIN (current))
10693         if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10694           {
10695             TREE_CHAIN (current) = NULL_TREE;
10696             new_list = current;
10697           }
10698     }
10699
10700   /* If we can't find one, lower expectations and try to gather multiple
10701      maximally specific methods */
10702   while (!new_list && max)
10703     {
10704       while (--max > 0)
10705         {
10706           if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10707             new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10708         }
10709     }
10710
10711   return new_list;
10712 }
10713
10714 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
10715    converted by method invocation conversion (5.3) to the type of the
10716    corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10717    to change less often than M1. */
10718
10719 static int
10720 argument_types_convertible (m1, m2_or_arglist)
10721     tree m1, m2_or_arglist;
10722 {
10723   static tree m2_arg_value = NULL_TREE;
10724   static tree m2_arg_cache = NULL_TREE;
10725   static int initialized_p;
10726
10727   register tree m1_arg, m2_arg;
10728
10729   /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10730      collector.  */
10731   if (!initialized_p)
10732     {
10733       ggc_add_tree_root (&m2_arg_value, 1);
10734       ggc_add_tree_root (&m2_arg_cache, 1);
10735       initialized_p = 1;
10736     }
10737
10738   SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
10739
10740   if (m2_arg_value == m2_or_arglist)
10741     m2_arg = m2_arg_cache;
10742   else
10743     {
10744       /* M2_OR_ARGLIST can be a function DECL or a raw list of
10745          argument types */
10746       if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10747         {
10748           m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10749           if (!METHOD_STATIC (m2_or_arglist))
10750             m2_arg = TREE_CHAIN (m2_arg);
10751         }
10752       else
10753         m2_arg = m2_or_arglist;
10754
10755       m2_arg_value = m2_or_arglist;
10756       m2_arg_cache = m2_arg;
10757     }
10758
10759   while (m1_arg != end_params_node && m2_arg != end_params_node)
10760     {
10761       resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
10762       if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10763                                                  TREE_VALUE (m2_arg)))
10764         break;
10765       m1_arg = TREE_CHAIN (m1_arg);
10766       m2_arg = TREE_CHAIN (m2_arg);
10767     }
10768   return m1_arg == end_params_node && m2_arg == end_params_node;
10769 }
10770
10771 /* Qualification routines */
10772
10773 static void
10774 qualify_ambiguous_name (id)
10775      tree id;
10776 {
10777   tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10778     saved_current_class;
10779   int again, super_found = 0, this_found = 0, new_array_found = 0;
10780   int code;
10781
10782   /* We first qualify the first element, then derive qualification of
10783      others based on the first one. If the first element is qualified
10784      by a resolution (field or type), this resolution is stored in the
10785      QUAL_RESOLUTION of the qual element being examined. We need to
10786      save the current_class since the use of SUPER might change the
10787      its value. */
10788   saved_current_class = current_class;
10789   qual = EXPR_WFL_QUALIFICATION (id);
10790   do {
10791
10792     /* Simple qualified expression feature a qual_wfl that is a
10793        WFL. Expression derived from a primary feature more complicated
10794        things like a CALL_EXPR. Expression from primary need to be
10795        worked out to extract the part on which the qualification will
10796        take place. */
10797     qual_wfl = QUAL_WFL (qual);
10798     switch (TREE_CODE (qual_wfl))
10799       {
10800       case CALL_EXPR:
10801         qual_wfl = TREE_OPERAND (qual_wfl, 0);
10802         if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10803           {
10804             qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10805             qual_wfl = QUAL_WFL (qual);
10806           }
10807         break;
10808       case NEW_ARRAY_EXPR:
10809       case NEW_ANONYMOUS_ARRAY_EXPR:
10810         qual = TREE_CHAIN (qual);
10811         again = new_array_found = 1;
10812         continue;
10813       case CONVERT_EXPR:
10814         break;
10815       case NEW_CLASS_EXPR:
10816         qual_wfl = TREE_OPERAND (qual_wfl, 0);
10817         break;
10818       case ARRAY_REF:
10819         while (TREE_CODE (qual_wfl) == ARRAY_REF)
10820           qual_wfl = TREE_OPERAND (qual_wfl, 0);
10821         break;
10822       case STRING_CST:
10823         qual = TREE_CHAIN (qual);
10824         qual_wfl = QUAL_WFL (qual);
10825         break;
10826       case CLASS_LITERAL:
10827         qual = TREE_CHAIN (qual);
10828         qual_wfl = QUAL_WFL (qual);
10829       break;
10830       default:
10831         /* Fix for -Wall. Just break doing nothing */
10832         break;
10833       }
10834
10835     ptr_type = current_class;
10836     again = 0;
10837     code = TREE_CODE (qual_wfl);
10838
10839     /* Pos evaluation: non WFL leading expression nodes */
10840     if (code == CONVERT_EXPR
10841         && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10842       name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10843
10844     else if (code == INTEGER_CST)
10845       name = qual_wfl;
10846     
10847     else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
10848              TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10849       name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10850
10851     else if (code == TREE_LIST)
10852       name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10853
10854     else if (code == STRING_CST || code == CONDITIONAL_EXPR 
10855              || code == PLUS_EXPR)
10856       {
10857         qual = TREE_CHAIN (qual);
10858         qual_wfl = QUAL_WFL (qual);
10859         again = 1;
10860       }
10861     else 
10862       {
10863         name = EXPR_WFL_NODE (qual_wfl);
10864         if (!name)
10865           {
10866             qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10867             again = 1;
10868           }
10869       }
10870
10871     /* If we have a THIS (from a primary), we set the context accordingly */
10872     if (name == this_identifier_node)
10873       {
10874         /* This isn't really elegant. One more added irregularity
10875            before I start using COMPONENT_REF (hopefully very soon.)  */
10876         if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10877             && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10878                EXPR_WITH_FILE_LOCATION
10879             && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) == 
10880                this_identifier_node)
10881             {
10882               qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10883               qual = EXPR_WFL_QUALIFICATION (qual);
10884             }
10885         qual = TREE_CHAIN (qual);
10886         qual_wfl = QUAL_WFL (qual);
10887         if (TREE_CODE (qual_wfl) == CALL_EXPR)
10888           again = 1;
10889         else
10890           name = EXPR_WFL_NODE (qual_wfl);
10891         this_found = 1;
10892       }
10893     /* If we have a SUPER, we set the context accordingly */
10894     if (name == super_identifier_node)
10895       {
10896         current_class = CLASSTYPE_SUPER (ptr_type);
10897         /* Check that there is such a thing as a super class. If not,
10898            return.  The error will be caught later on, during the
10899            resolution */
10900         if (!current_class)
10901           {
10902             current_class = saved_current_class;
10903             return;
10904           }
10905         qual = TREE_CHAIN (qual);
10906         /* Do one more interation to set things up */
10907         super_found = again = 1;
10908       }
10909   } while (again);
10910   
10911   /* If name appears within the scope of a local variable declaration
10912      or parameter declaration, then it is an expression name. We don't
10913      carry this test out if we're in the context of the use of SUPER
10914      or THIS */
10915   if (!this_found && !super_found 
10916       && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10917       && (decl = IDENTIFIER_LOCAL_VALUE (name)))
10918     {
10919       RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10920       QUAL_RESOLUTION (qual) = decl;
10921     }
10922
10923   /* If within the class/interface NAME was found to be used there
10924      exists a (possibly inherited) field named NAME, then this is an
10925      expression name. If we saw a NEW_ARRAY_EXPR before and want to
10926      address length, it is OK. */
10927   else if ((decl = lookup_field_wrapper (ptr_type, name))
10928            || name == length_identifier_node)
10929     {
10930       RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10931       QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
10932     }
10933
10934   /* We reclassify NAME as yielding to a type name resolution if:
10935      - NAME is a class/interface declared within the compilation
10936        unit containing NAME,
10937      - NAME is imported via a single-type-import declaration,
10938      - NAME is declared in an another compilation unit of the package
10939        of the compilation unit containing NAME,
10940      - NAME is declared by exactly on type-import-on-demand declaration
10941      of the compilation unit containing NAME. 
10942      - NAME is actually a STRING_CST. */
10943   else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10944            || (decl = resolve_and_layout (name, NULL_TREE)))
10945     {
10946       RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10947       QUAL_RESOLUTION (qual) = decl;
10948     }
10949
10950   /* Method call, array references and cast are expression name */
10951   else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
10952            || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10953            || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR
10954            || TREE_CODE (QUAL_WFL (qual)) == MODIFY_EXPR)
10955     RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10956
10957   /* Check here that NAME isn't declared by more than one
10958      type-import-on-demand declaration of the compilation unit
10959      containing NAME. FIXME */
10960
10961   /* Otherwise, NAME is reclassified as a package name */
10962   else 
10963     RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10964
10965   /* Propagate the qualification accross other components of the
10966      qualified name */
10967   for (qual = TREE_CHAIN (qual); qual;
10968        qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10969     {
10970       if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10971         RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10972       else 
10973         RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10974     }
10975
10976   /* Store the global qualification for the ambiguous part of ID back
10977      into ID fields */
10978   if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10979     RESOLVE_EXPRESSION_NAME_P (id) = 1;
10980   else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10981     RESOLVE_TYPE_NAME_P (id) = 1;
10982   else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10983     RESOLVE_PACKAGE_NAME_P (id) = 1;
10984
10985   /* Restore the current class */
10986   current_class = saved_current_class;
10987 }
10988
10989 static int
10990 breakdown_qualified (left, right, source)
10991     tree *left, *right, source;
10992 {
10993   char *p, *base;
10994   int   l = IDENTIFIER_LENGTH (source);
10995
10996   base = alloca (l + 1);
10997   memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10998
10999   /* Breakdown NAME into REMAINDER . IDENTIFIER */
11000   p = base + l - 1;
11001   while (*p != '.' && p != base)
11002     p--;
11003
11004   /* We didn't find a '.'. Return an error */
11005   if (p == base)
11006     return 1;
11007
11008   *p = '\0';
11009   if (right)
11010     *right = get_identifier (p+1);
11011   *left = get_identifier (base);
11012   
11013   return 0;
11014 }
11015
11016 /* Return TRUE if two classes are from the same package. */
11017
11018 static int
11019 in_same_package (name1, name2)
11020   tree name1, name2;
11021 {
11022   tree tmp;
11023   tree pkg1;
11024   tree pkg2;
11025   
11026   if (TREE_CODE (name1) == TYPE_DECL)
11027     name1 = DECL_NAME (name1);
11028   if (TREE_CODE (name2) == TYPE_DECL)
11029     name2 = DECL_NAME (name2);
11030
11031   if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
11032     /* One in empty package. */
11033     return 0;
11034
11035   if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
11036     /* Both in empty package. */
11037     return 1;
11038
11039   breakdown_qualified (&pkg1, &tmp, name1);
11040   breakdown_qualified (&pkg2, &tmp, name2);
11041   
11042   return (pkg1 == pkg2);
11043 }
11044
11045 /* Patch tree nodes in a function body. When a BLOCK is found, push
11046    local variable decls if present.
11047    Same as java_complete_lhs, but does resolve static finals to values. */
11048
11049 static tree
11050 java_complete_tree (node)
11051      tree node;
11052 {
11053   node = java_complete_lhs (node);
11054   if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11055       && DECL_INITIAL (node) != NULL_TREE
11056       && !flag_emit_xref)
11057     {
11058       tree value = DECL_INITIAL (node);
11059       DECL_INITIAL (node) = NULL_TREE;
11060       value = fold_constant_for_init (value, node);
11061       DECL_INITIAL (node) = value;
11062       if (value != NULL_TREE)
11063         {
11064           /* fold_constant_for_init sometimes widen the original type
11065              of the constant (i.e. byte to int.) It's not desirable,
11066              especially if NODE is a function argument. */
11067           if (TREE_CODE (value) == INTEGER_CST
11068               && TREE_TYPE (node) != TREE_TYPE (value))
11069             return convert (TREE_TYPE (node), value);
11070           else
11071             return value;
11072         }
11073       else
11074         DECL_FIELD_FINAL_IUD (node) = 0;
11075     }
11076   return node;
11077 }
11078
11079 static tree
11080 java_stabilize_reference (node)
11081      tree node;
11082 {
11083   if (TREE_CODE (node) == COMPOUND_EXPR)
11084     {
11085       tree op0 = TREE_OPERAND (node, 0);
11086       tree op1 = TREE_OPERAND (node, 1);
11087       TREE_OPERAND (node, 0) = save_expr (op0);
11088       TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11089       return node;
11090     }
11091   return stabilize_reference (node);
11092 }
11093
11094 /* Patch tree nodes in a function body. When a BLOCK is found, push
11095    local variable decls if present.
11096    Same as java_complete_tree, but does not resolve static finals to values. */
11097
11098 static tree
11099 java_complete_lhs (node)
11100      tree node;
11101 {
11102   tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11103   int flag;
11104
11105   /* CONVERT_EXPR always has its type set, even though it needs to be
11106      worked out. */
11107   if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11108     return node;
11109
11110   /* The switch block implements cases processing container nodes
11111      first.  Contained nodes are always written back. Leaves come
11112      next and return a value. */
11113   switch (TREE_CODE (node))
11114     {
11115     case BLOCK:
11116
11117       /* 1- Block section.
11118          Set the local values on decl names so we can identify them
11119          faster when they're referenced. At that stage, identifiers
11120          are legal so we don't check for declaration errors. */
11121       for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11122         {
11123           DECL_CONTEXT (cn) = current_function_decl;
11124           IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11125         }
11126       if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11127           CAN_COMPLETE_NORMALLY (node) = 1;
11128       else
11129         {
11130           tree stmt = BLOCK_EXPR_BODY (node);
11131           tree *ptr;
11132           int error_seen = 0;
11133           if (TREE_CODE (stmt) == COMPOUND_EXPR)
11134             {
11135               /* Re-order from (((A; B); C); ...; Z) to 
11136                  (A; (B; (C ; (...; Z)))).
11137                  This makes it easier to scan the statements left-to-right
11138                  without using recursion (which might overflow the stack
11139                  if the block has many statements. */
11140               for (;;)
11141                 {
11142                   tree left = TREE_OPERAND (stmt, 0);
11143                   if (TREE_CODE (left) != COMPOUND_EXPR)
11144                     break;
11145                   TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11146                   TREE_OPERAND (left, 1) = stmt;
11147                   stmt = left;
11148                 }
11149               BLOCK_EXPR_BODY (node) = stmt;
11150             }
11151
11152           /* Now do the actual complete, without deep recursion for
11153              long blocks. */
11154           ptr = &BLOCK_EXPR_BODY (node);
11155           while (TREE_CODE (*ptr) == COMPOUND_EXPR
11156                  && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
11157             {
11158               tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11159               tree *next = &TREE_OPERAND (*ptr, 1);
11160               TREE_OPERAND (*ptr, 0) = cur;
11161               if (cur == empty_stmt_node)
11162                 {
11163                   /* Optimization;  makes it easier to detect empty bodies.
11164                      Most useful for <clinit> with all-constant initializer. */
11165                   *ptr = *next;
11166                   continue;
11167                 }
11168               if (TREE_CODE (cur) == ERROR_MARK)
11169                 error_seen++;
11170               else if (! CAN_COMPLETE_NORMALLY (cur))
11171                 {
11172                   wfl_op2 = *next;
11173                   for (;;)
11174                     {
11175                       if (TREE_CODE (wfl_op2) == BLOCK)
11176                         wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11177                       else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11178                         wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11179                       else
11180                         break;
11181                     }
11182                   if (TREE_CODE (wfl_op2) != CASE_EXPR
11183                       && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11184                     unreachable_stmt_error (*ptr);
11185                 }
11186               ptr = next;
11187             }
11188           *ptr = java_complete_tree (*ptr);
11189
11190           if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11191             return error_mark_node;
11192           CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11193         }
11194       /* Turn local bindings to null */
11195       for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11196         IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11197
11198       TREE_TYPE (node) = void_type_node;
11199       break;
11200
11201       /* 2- They are expressions but ultimately deal with statements */
11202
11203     case THROW_EXPR:
11204       wfl_op1 = TREE_OPERAND (node, 0);
11205       COMPLETE_CHECK_OP_0 (node);
11206       /* 14.19 A throw statement cannot complete normally. */
11207       CAN_COMPLETE_NORMALLY (node) = 0;
11208       return patch_throw_statement (node, wfl_op1);
11209
11210     case SYNCHRONIZED_EXPR:
11211       wfl_op1 = TREE_OPERAND (node, 0);
11212       return patch_synchronized_statement (node, wfl_op1);
11213
11214     case TRY_EXPR:
11215       return patch_try_statement (node);
11216
11217     case TRY_FINALLY_EXPR:
11218       COMPLETE_CHECK_OP_0 (node);
11219       COMPLETE_CHECK_OP_1 (node);
11220       if (TREE_OPERAND (node, 0) == empty_stmt_node)
11221         return TREE_OPERAND (node, 1);
11222       if (TREE_OPERAND (node, 1) == empty_stmt_node)
11223         return TREE_OPERAND (node, 0);
11224       CAN_COMPLETE_NORMALLY (node)
11225         = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11226            && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11227       TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11228       return node;
11229
11230     case CLEANUP_POINT_EXPR:
11231       COMPLETE_CHECK_OP_0 (node);
11232       TREE_TYPE (node) = void_type_node;
11233       CAN_COMPLETE_NORMALLY (node) = 
11234         CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11235       return node;
11236
11237     case WITH_CLEANUP_EXPR:
11238       COMPLETE_CHECK_OP_0 (node);
11239       COMPLETE_CHECK_OP_2 (node);
11240       CAN_COMPLETE_NORMALLY (node) = 
11241         CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11242       TREE_TYPE (node) = void_type_node;
11243       return node;
11244
11245     case LABELED_BLOCK_EXPR:
11246       PUSH_LABELED_BLOCK (node);
11247       if (LABELED_BLOCK_BODY (node))
11248         COMPLETE_CHECK_OP_1 (node);
11249       TREE_TYPE (node) = void_type_node;
11250       POP_LABELED_BLOCK ();
11251
11252       if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11253         {
11254           LABELED_BLOCK_BODY (node) = NULL_TREE;
11255           CAN_COMPLETE_NORMALLY (node) = 1;
11256         }
11257       else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11258         CAN_COMPLETE_NORMALLY (node) = 1;
11259       return node;
11260
11261     case EXIT_BLOCK_EXPR:
11262       /* We don't complete operand 1, because it's the return value of
11263          the EXIT_BLOCK_EXPR which doesn't exist it Java */
11264       return patch_bc_statement (node);
11265
11266     case CASE_EXPR:
11267       cn = java_complete_tree (TREE_OPERAND (node, 0));
11268       if (cn == error_mark_node)
11269         return cn;
11270
11271       /* First, the case expression must be constant. Values of final
11272          fields are accepted. */
11273       cn = fold (cn);
11274       if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11275           && JDECL_P (TREE_OPERAND (cn, 1))
11276           && FIELD_FINAL (TREE_OPERAND (cn, 1))
11277           && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11278         {
11279           cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11280                                        TREE_OPERAND (cn, 1));
11281         }
11282
11283       if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11284         {
11285           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11286           parse_error_context (node, "Constant expression required");
11287           return error_mark_node;
11288         }
11289
11290       nn = ctxp->current_loop;
11291
11292       /* It must be assignable to the type of the switch expression. */
11293       if (!try_builtin_assignconv (NULL_TREE, 
11294                                    TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11295         {
11296           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11297           parse_error_context 
11298             (wfl_operator,
11299              "Incompatible type for case. Can't convert `%s' to `int'",
11300              lang_printable_name (TREE_TYPE (cn), 0));
11301           return error_mark_node;
11302         }
11303
11304       cn = fold (convert (int_type_node, cn));
11305
11306       /* Multiple instance of a case label bearing the same
11307          value is checked during code generation. The case
11308          expression is allright so far. */
11309       if (TREE_CODE (cn) == VAR_DECL)
11310         cn = DECL_INITIAL (cn);
11311       TREE_OPERAND (node, 0) = cn;
11312       TREE_TYPE (node) = void_type_node;
11313       CAN_COMPLETE_NORMALLY (node) = 1;
11314       TREE_SIDE_EFFECTS (node) = 1;
11315       break;
11316
11317     case DEFAULT_EXPR:
11318       nn = ctxp->current_loop;
11319       /* Only one default label is allowed per switch statement */
11320       if (SWITCH_HAS_DEFAULT (nn))
11321         {
11322           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11323           parse_error_context (wfl_operator, 
11324                                "Duplicate case label: `default'");
11325           return error_mark_node;
11326         }
11327       else
11328         SWITCH_HAS_DEFAULT (nn) = 1;
11329       TREE_TYPE (node) = void_type_node;
11330       TREE_SIDE_EFFECTS (node) = 1;
11331       CAN_COMPLETE_NORMALLY (node) = 1;
11332       break;
11333
11334     case SWITCH_EXPR:
11335     case LOOP_EXPR:
11336       PUSH_LOOP (node);
11337       /* Check whether the loop was enclosed in a labeled
11338          statement. If not, create one, insert the loop in it and
11339          return the node */
11340       nn = patch_loop_statement (node);
11341
11342       /* Anyways, walk the body of the loop */
11343       if (TREE_CODE (node) == LOOP_EXPR)
11344         TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11345       /* Switch statement: walk the switch expression and the cases */
11346       else
11347         node = patch_switch_statement (node);
11348
11349       if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11350         nn = error_mark_node;
11351       else
11352         {
11353           TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11354           /* If we returned something different, that's because we
11355              inserted a label. Pop the label too. */
11356           if (nn != node)
11357             {
11358               if (CAN_COMPLETE_NORMALLY (node))
11359                 CAN_COMPLETE_NORMALLY (nn) = 1;
11360               POP_LABELED_BLOCK ();
11361             }
11362         }
11363       POP_LOOP ();
11364       return nn;
11365
11366     case EXIT_EXPR:
11367       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11368       return patch_exit_expr (node);
11369
11370     case COND_EXPR:
11371       /* Condition */
11372       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11373       if (TREE_OPERAND (node, 0) == error_mark_node)
11374         return error_mark_node;
11375       /* then-else branches */
11376       TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11377       if (TREE_OPERAND (node, 1) == error_mark_node)
11378         return error_mark_node;
11379       TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11380       if (TREE_OPERAND (node, 2) == error_mark_node)
11381         return error_mark_node;
11382       return patch_if_else_statement (node);
11383       break;
11384
11385     case CONDITIONAL_EXPR:
11386       /* Condition */
11387       wfl_op1 = TREE_OPERAND (node, 0);
11388       COMPLETE_CHECK_OP_0 (node);
11389       wfl_op2 = TREE_OPERAND (node, 1);
11390       COMPLETE_CHECK_OP_1 (node);
11391       wfl_op3 = TREE_OPERAND (node, 2);
11392       COMPLETE_CHECK_OP_2 (node);
11393       return patch_conditional_expr (node, wfl_op1, wfl_op2);
11394
11395       /* 3- Expression section */
11396     case COMPOUND_EXPR:
11397       wfl_op2 = TREE_OPERAND (node, 1);
11398       TREE_OPERAND (node, 0) = nn = 
11399         java_complete_tree (TREE_OPERAND (node, 0));
11400       if (wfl_op2 == empty_stmt_node)
11401         CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11402       else
11403         {
11404           if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11405             {
11406               /* An unreachable condition in a do-while statement
11407                  is *not* (technically) an unreachable statement. */
11408               nn = wfl_op2;
11409               if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11410                 nn = EXPR_WFL_NODE (nn);
11411               if (TREE_CODE (nn) != EXIT_EXPR)
11412                 {
11413                   SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11414                   parse_error_context (wfl_operator, "Unreachable statement");
11415                 }
11416             }
11417           TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11418           if (TREE_OPERAND (node, 1) == error_mark_node)
11419             return error_mark_node;
11420           CAN_COMPLETE_NORMALLY (node)
11421             = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
11422         }
11423       TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11424       break;
11425
11426     case RETURN_EXPR:
11427       /* CAN_COMPLETE_NORMALLY (node) = 0; */
11428       return patch_return (node);
11429
11430     case EXPR_WITH_FILE_LOCATION:
11431       if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11432           || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11433         {
11434           tree wfl = node;
11435           node = resolve_expression_name (node, NULL);
11436           if (node == error_mark_node)
11437             return node;
11438           /* Keep line number information somewhere were it doesn't
11439              disrupt the completion process. */
11440           if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11441             {
11442               EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11443               TREE_OPERAND (node, 1) = wfl;
11444             }
11445           CAN_COMPLETE_NORMALLY (node) = 1;
11446         }
11447       else
11448         {
11449           tree body;
11450           int save_lineno = lineno;
11451           lineno = EXPR_WFL_LINENO (node);
11452           body = java_complete_tree (EXPR_WFL_NODE (node));
11453           lineno = save_lineno;
11454           EXPR_WFL_NODE (node) = body;
11455           TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11456           CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11457           if (body == empty_stmt_node || TREE_CONSTANT (body))
11458             {
11459               /* Makes it easier to constant fold, detect empty bodies. */
11460               return body;
11461             }
11462           if (body == error_mark_node)
11463             {
11464               /* Its important for the evaluation of assignment that
11465                  this mark on the TREE_TYPE is propagated. */
11466               TREE_TYPE (node) = error_mark_node;
11467               return error_mark_node;
11468             }
11469           else
11470             TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11471           
11472         }
11473       break;
11474
11475     case NEW_ARRAY_EXPR:
11476       /* Patch all the dimensions */
11477       flag = 0;
11478       for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11479         {
11480           int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11481           tree dim = convert (int_type_node, 
11482                               java_complete_tree (TREE_VALUE (cn)));
11483           if (dim == error_mark_node)
11484             {
11485               flag = 1;
11486               continue;
11487             }
11488           else
11489             {
11490               TREE_VALUE (cn) = dim;
11491               /* Setup the location of the current dimension, for
11492                  later error report. */
11493               TREE_PURPOSE (cn) = 
11494                 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11495               EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11496             }
11497         }
11498       /* They complete the array creation expression, if no errors
11499          were found. */
11500       CAN_COMPLETE_NORMALLY (node) = 1;
11501       return (flag ? error_mark_node
11502               : force_evaluation_order (patch_newarray (node)));
11503
11504     case NEW_ANONYMOUS_ARRAY_EXPR:
11505       /* Create the array type if necessary. */
11506       if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11507         {
11508           tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11509           if (!(type = resolve_type_during_patch (type)))
11510             return error_mark_node;
11511           type = build_array_from_name (type, NULL_TREE,
11512                                         ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11513           ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11514         }
11515       node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11516                                    ANONYMOUS_ARRAY_INITIALIZER (node));
11517       if (node == error_mark_node)
11518         return error_mark_node;
11519       CAN_COMPLETE_NORMALLY (node) = 1;
11520       return node;
11521
11522     case NEW_CLASS_EXPR:
11523     case CALL_EXPR:
11524       /* Complete function's argument(s) first */
11525       if (complete_function_arguments (node))
11526         return error_mark_node;
11527       else
11528         {
11529           tree decl, wfl = TREE_OPERAND (node, 0);
11530           int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11531           int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
11532                            super_identifier_node);
11533
11534           node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
11535                                           from_super, 0, &decl);
11536           if (node == error_mark_node)
11537             return error_mark_node;
11538
11539           check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11540           /* If we call this(...), register signature and positions */
11541           if (in_this)
11542             DECL_CONSTRUCTOR_CALLS (current_function_decl) = 
11543               tree_cons (wfl, decl, 
11544                          DECL_CONSTRUCTOR_CALLS (current_function_decl));
11545           CAN_COMPLETE_NORMALLY (node) = 1;
11546           return force_evaluation_order (node);
11547         }
11548
11549     case MODIFY_EXPR:
11550       /* Save potential wfls */
11551       wfl_op1 = TREE_OPERAND (node, 0);
11552       TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11553       
11554       if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11555           && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11556           && DECL_INITIAL (nn) != NULL_TREE)
11557         {
11558           tree value;
11559           
11560           value = fold_constant_for_init (nn, nn);
11561
11562           if (value != NULL_TREE)
11563             {
11564               tree type = TREE_TYPE (value);
11565               if (JPRIMITIVE_TYPE_P (type) || 
11566                   (type == string_ptr_type_node && ! flag_emit_class_files))
11567                 return empty_stmt_node;
11568             }
11569           if (! flag_emit_class_files)
11570             DECL_INITIAL (nn) = NULL_TREE;
11571           if (CLASS_FINAL_VARIABLE_P (nn))
11572             DECL_FIELD_FINAL_IUD (nn) = 0;
11573         }
11574       wfl_op2 = TREE_OPERAND (node, 1);
11575
11576       if (TREE_OPERAND (node, 0) == error_mark_node)
11577         return error_mark_node;
11578
11579       flag = COMPOUND_ASSIGN_P (wfl_op2);
11580       if (flag)
11581         {
11582           /* This might break when accessing outer field from inner
11583              class. TESTME, FIXME */
11584           tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0)); 
11585
11586           /* Hand stabilize the lhs on both places */
11587           TREE_OPERAND (node, 0) = lvalue;
11588           TREE_OPERAND (TREE_OPERAND (node, 1), 0) = 
11589             (flag_emit_class_files ? lvalue : save_expr (lvalue));
11590
11591           /* 15.25.2.a: Left hand is not an array access. FIXME */
11592           /* Now complete the RHS. We write it back later on. */
11593           nn = java_complete_tree (TREE_OPERAND (node, 1));
11594
11595           if ((cn = patch_string (nn)))
11596             nn = cn;
11597
11598           /* The last part of the rewrite for E1 op= E2 is to have 
11599              E1 = (T)(E1 op E2), with T being the type of E1. */
11600           nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2), 
11601                                                TREE_TYPE (lvalue), nn));
11602
11603           /* If the assignment is compound and has reference type,
11604              then ensure the LHS has type String and nothing else.  */
11605           if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
11606               && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
11607             parse_error_context (wfl_op2,
11608                                  "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
11609                                  lang_printable_name (TREE_TYPE (lvalue), 0));
11610
11611           /* 15.25.2.b: Left hand is an array access. FIXME */
11612         }
11613
11614       /* If we're about to patch a NEW_ARRAY_INIT, we call a special
11615          function to complete this RHS. Note that a NEW_ARRAY_INIT
11616          might have been already fully expanded if created as a result
11617          of processing an anonymous array initializer. We avoid doing
11618          the operation twice by testing whether the node already bears
11619          a type. */
11620       else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
11621         nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
11622                                    TREE_OPERAND (node, 1));
11623       /* Otherwise we simply complete the RHS */
11624       else
11625         nn = java_complete_tree (TREE_OPERAND (node, 1));
11626
11627       if (nn == error_mark_node)
11628         return error_mark_node;
11629
11630       /* Write back the RHS as we evaluated it. */
11631       TREE_OPERAND (node, 1) = nn;
11632
11633       /* In case we're handling = with a String as a RHS, we need to
11634          produce a String out of the RHS (it might still be a
11635          STRING_CST or a StringBuffer at this stage */
11636       if ((nn = patch_string (TREE_OPERAND (node, 1))))
11637         TREE_OPERAND (node, 1) = nn;
11638
11639       if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11640                                         TREE_OPERAND (node, 1))))
11641         {
11642           /* We return error_mark_node if outer_field_access_fix
11643              detects we write into a final. */
11644           if (nn == error_mark_node)
11645             return error_mark_node;
11646           node = nn;
11647         }
11648       else
11649         {
11650           node = patch_assignment (node, wfl_op1, wfl_op2);
11651           /* Reorganize the tree if necessary. */
11652           if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node)) 
11653                        || JSTRING_P (TREE_TYPE (node))))
11654             node = java_refold (node);
11655         }
11656       
11657       CAN_COMPLETE_NORMALLY (node) = 1;
11658       return node;
11659
11660     case MULT_EXPR:
11661     case PLUS_EXPR:
11662     case MINUS_EXPR:
11663     case LSHIFT_EXPR:
11664     case RSHIFT_EXPR:
11665     case URSHIFT_EXPR:
11666     case BIT_AND_EXPR:
11667     case BIT_XOR_EXPR:
11668     case BIT_IOR_EXPR:
11669     case TRUNC_MOD_EXPR:
11670     case TRUNC_DIV_EXPR:
11671     case RDIV_EXPR:
11672     case TRUTH_ANDIF_EXPR:
11673     case TRUTH_ORIF_EXPR:
11674     case EQ_EXPR: 
11675     case NE_EXPR:
11676     case GT_EXPR:
11677     case GE_EXPR:
11678     case LT_EXPR:
11679     case LE_EXPR:
11680       /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11681          knows how to handle those cases. */
11682       wfl_op1 = TREE_OPERAND (node, 0);
11683       wfl_op2 = TREE_OPERAND (node, 1);
11684
11685       CAN_COMPLETE_NORMALLY (node) = 1;
11686       /* Don't complete string nodes if dealing with the PLUS operand. */
11687       if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
11688         {
11689           nn = java_complete_tree (wfl_op1);
11690           if (nn == error_mark_node)
11691             return error_mark_node;
11692
11693           TREE_OPERAND (node, 0) = nn;
11694         }
11695       if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
11696         {
11697           nn = java_complete_tree (wfl_op2);
11698           if (nn == error_mark_node)
11699             return error_mark_node;
11700
11701           TREE_OPERAND (node, 1) = nn;
11702         }
11703       return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
11704
11705     case INSTANCEOF_EXPR:
11706       wfl_op1 = TREE_OPERAND (node, 0);
11707       COMPLETE_CHECK_OP_0 (node);
11708       if (flag_emit_xref)
11709         {
11710           TREE_TYPE (node) = boolean_type_node;
11711           return node;
11712         }
11713       return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11714
11715     case UNARY_PLUS_EXPR:
11716     case NEGATE_EXPR:
11717     case TRUTH_NOT_EXPR:
11718     case BIT_NOT_EXPR:
11719     case PREDECREMENT_EXPR:
11720     case PREINCREMENT_EXPR:
11721     case POSTDECREMENT_EXPR:
11722     case POSTINCREMENT_EXPR:
11723     case CONVERT_EXPR:
11724       /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11725          how to handle those cases. */
11726       wfl_op1 = TREE_OPERAND (node, 0);
11727       CAN_COMPLETE_NORMALLY (node) = 1;
11728       TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11729       if (TREE_OPERAND (node, 0) == error_mark_node)
11730         return error_mark_node;
11731       node = patch_unaryop (node, wfl_op1);
11732       CAN_COMPLETE_NORMALLY (node) = 1;
11733       break;
11734
11735     case ARRAY_REF:
11736       /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11737          how to handle those cases. */
11738       wfl_op1 = TREE_OPERAND (node, 0);
11739       TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11740       if (TREE_OPERAND (node, 0) == error_mark_node)
11741         return error_mark_node;
11742       if (!flag_emit_class_files && !flag_emit_xref)
11743         TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
11744       /* The same applies to wfl_op2 */
11745       wfl_op2 = TREE_OPERAND (node, 1);
11746       TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11747       if (TREE_OPERAND (node, 1) == error_mark_node)
11748         return error_mark_node;
11749       if (!flag_emit_class_files && !flag_emit_xref)
11750         TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
11751       return patch_array_ref (node);
11752
11753     case RECORD_TYPE:
11754       return node;;
11755
11756     case COMPONENT_REF:
11757       /* The first step in the re-write of qualified name handling.  FIXME.
11758          So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
11759       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11760       if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11761         {
11762           tree name = TREE_OPERAND (node, 1);
11763           tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11764           if (field == NULL_TREE)
11765             {
11766               error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11767               return error_mark_node;
11768             }
11769           if (! FIELD_STATIC (field))
11770             {
11771               error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11772               return error_mark_node;
11773             }
11774           return field;
11775         }
11776       else
11777         abort ();
11778       break;
11779
11780     case THIS_EXPR:
11781       /* Can't use THIS in a static environment */
11782       if (!current_this)
11783         {
11784           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11785           parse_error_context (wfl_operator,
11786                                "Keyword `this' used outside allowed context");
11787           TREE_TYPE (node) = error_mark_node;
11788           return error_mark_node;
11789         }
11790       if (ctxp->explicit_constructor_p)
11791         {
11792           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11793           parse_error_context 
11794             (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
11795           TREE_TYPE (node) = error_mark_node;
11796           return error_mark_node;
11797         }
11798       return current_this;
11799       
11800     case CLASS_LITERAL:
11801       CAN_COMPLETE_NORMALLY (node) = 1;
11802       node = patch_incomplete_class_ref (node);
11803       if (node == error_mark_node)
11804         return error_mark_node;
11805       break;
11806
11807     case INSTANCE_INITIALIZERS_EXPR:
11808       in_instance_initializer++;
11809       node = java_complete_tree (TREE_OPERAND (node, 0));
11810       in_instance_initializer--;
11811       if (node != error_mark_node)
11812         TREE_TYPE (node) = void_type_node;
11813       else
11814         return error_mark_node;
11815       break;
11816
11817     default:
11818       CAN_COMPLETE_NORMALLY (node) = 1;
11819       /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
11820          and it's time to turn it into the appropriate String object */
11821       if ((nn = patch_string (node)))
11822         node = nn;
11823       else
11824         internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
11825     }
11826   return node;
11827 }
11828
11829 /* Complete function call's argument. Return a non zero value is an
11830    error was found.  */
11831
11832 static int
11833 complete_function_arguments (node)
11834      tree node;
11835 {
11836   int flag = 0;
11837   tree cn;
11838
11839   ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11840   for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11841     {
11842       tree wfl = TREE_VALUE (cn), parm, temp;
11843       parm = java_complete_tree (wfl);
11844
11845       if (parm == error_mark_node)
11846         {
11847           flag = 1;
11848           continue;
11849         }
11850       /* If have a string literal that we haven't transformed yet or a
11851          crafted string buffer, as a result of use of the the String
11852          `+' operator. Build `parm.toString()' and expand it. */
11853       if ((temp = patch_string (parm)))
11854         parm = temp;
11855       /* Inline PRIMTYPE.TYPE read access */
11856       parm = maybe_build_primttype_type_ref (parm, wfl);
11857
11858       TREE_VALUE (cn) = parm;
11859     }
11860   ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11861   return flag;
11862 }
11863
11864 /* Sometimes (for loops and variable initialized during their
11865    declaration), we want to wrap a statement around a WFL and turn it
11866    debugable.  */
11867
11868 static tree
11869 build_debugable_stmt (location, stmt)
11870     int location;
11871     tree stmt;
11872 {
11873   if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11874     {
11875       stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11876       EXPR_WFL_LINECOL (stmt) = location;
11877     }
11878   JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11879   return stmt;
11880 }
11881
11882 static tree
11883 build_expr_block (body, decls)
11884      tree body, decls;
11885 {
11886   tree node = make_node (BLOCK);
11887   BLOCK_EXPR_DECLS (node) = decls;
11888   BLOCK_EXPR_BODY (node) = body;
11889   if (body)
11890     TREE_TYPE (node) = TREE_TYPE (body);
11891   TREE_SIDE_EFFECTS (node) = 1;
11892   return node;
11893 }
11894
11895 /* Create a new function block and link it approriately to current
11896    function block chain */
11897
11898 static tree
11899 enter_block ()
11900 {
11901   tree b = build_expr_block (NULL_TREE, NULL_TREE);
11902
11903   /* Link block B supercontext to the previous block. The current
11904      function DECL is used as supercontext when enter_a_block is called
11905      for the first time for a given function. The current function body
11906      (DECL_FUNCTION_BODY) is set to be block B.  */
11907
11908   tree fndecl = current_function_decl; 
11909
11910   if (!fndecl) {
11911     BLOCK_SUPERCONTEXT (b) = current_static_block;
11912     current_static_block = b;
11913   }
11914
11915   else if (!DECL_FUNCTION_BODY (fndecl))
11916     {
11917       BLOCK_SUPERCONTEXT (b) = fndecl;
11918       DECL_FUNCTION_BODY (fndecl) = b;
11919     }
11920   else
11921     {
11922       BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11923       DECL_FUNCTION_BODY (fndecl) = b;
11924     }
11925   return b;
11926 }
11927
11928 /* Exit a block by changing the current function body
11929    (DECL_FUNCTION_BODY) to the current block super context, only if
11930    the block being exited isn't the method's top level one.  */
11931
11932 static tree
11933 exit_block ()
11934 {
11935   tree b;
11936   if (current_function_decl)
11937     {
11938       b = DECL_FUNCTION_BODY (current_function_decl);
11939       if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11940         DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11941     }
11942   else
11943     {
11944       b = current_static_block;
11945
11946       if (BLOCK_SUPERCONTEXT (b))
11947         current_static_block = BLOCK_SUPERCONTEXT (b);
11948     }
11949   return b;
11950 }
11951
11952 /* Lookup for NAME in the nested function's blocks, all the way up to
11953    the current toplevel one. It complies with Java's local variable
11954    scoping rules.  */
11955
11956 static tree
11957 lookup_name_in_blocks (name)
11958      tree name;
11959 {
11960   tree b = GET_CURRENT_BLOCK (current_function_decl);
11961
11962   while (b != current_function_decl)
11963     {
11964       tree current;
11965
11966       /* Paranoid sanity check. To be removed */
11967       if (TREE_CODE (b) != BLOCK)
11968         abort ();
11969
11970       for (current = BLOCK_EXPR_DECLS (b); current; 
11971            current = TREE_CHAIN (current))
11972         if (DECL_NAME (current) == name)
11973           return current;
11974       b = BLOCK_SUPERCONTEXT (b);
11975     }
11976   return NULL_TREE;
11977 }
11978
11979 static void
11980 maybe_absorb_scoping_blocks ()
11981 {
11982   while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
11983     {
11984       tree b = exit_block ();
11985       java_method_add_stmt (current_function_decl, b);
11986       SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11987     }
11988 }
11989
11990 \f
11991 /* This section of the source is reserved to build_* functions that
11992    are building incomplete tree nodes and the patch_* functions that
11993    are completing them.  */
11994
11995 /* Wrap a non WFL node around a WFL.  */
11996
11997 static tree
11998 build_wfl_wrap (node, location)
11999     tree node;
12000     int location;
12001 {
12002   tree wfl, node_to_insert = node;
12003   
12004   /* We want to process THIS . xxx symbolicaly, to keep it consistent
12005      with the way we're processing SUPER. A THIS from a primary as a
12006      different form than a SUPER. Turn THIS into something symbolic */
12007   if (TREE_CODE (node) == THIS_EXPR)
12008     node_to_insert = wfl = build_wfl_node (this_identifier_node);
12009   else
12010     wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12011
12012   EXPR_WFL_LINECOL (wfl) = location;
12013   EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12014   return wfl;
12015 }
12016
12017 /* Build a super() constructor invocation. Returns empty_stmt_node if
12018    we're currently dealing with the class java.lang.Object. */
12019
12020 static tree
12021 build_super_invocation (mdecl)
12022      tree mdecl;
12023 {
12024   if (DECL_CONTEXT (mdecl) == object_type_node)
12025     return empty_stmt_node;
12026   else
12027     {
12028       tree super_wfl = build_wfl_node (super_identifier_node);
12029       tree a = NULL_TREE, t;
12030       /* If we're dealing with an anonymous class, pass the arguments
12031          of the crafted constructor along. */
12032       if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12033         {
12034           SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12035           for (; t != end_params_node; t = TREE_CHAIN (t))
12036             a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12037         }
12038       return build_method_invocation (super_wfl, a);
12039     }
12040 }
12041
12042 /* Build a SUPER/THIS qualified method invocation.  */
12043
12044 static tree
12045 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
12046      int use_this;
12047      tree name, args;
12048      int lloc, rloc;
12049 {
12050   tree invok;
12051   tree wfl = 
12052     build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12053   EXPR_WFL_LINECOL (wfl) = lloc;
12054   invok = build_method_invocation (name, args);
12055   return make_qualified_primary (wfl, invok, rloc);
12056 }
12057
12058 /* Build an incomplete CALL_EXPR node. */
12059
12060 static tree
12061 build_method_invocation (name, args)
12062     tree name;
12063     tree args;
12064 {
12065   tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12066   TREE_SIDE_EFFECTS (call) = 1;
12067   EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12068   return call;
12069 }
12070
12071 /* Build an incomplete new xxx(...) node. */
12072
12073 static tree
12074 build_new_invocation (name, args)
12075     tree name, args;
12076 {
12077   tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12078   TREE_SIDE_EFFECTS (call) = 1;
12079   EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12080   return call;
12081 }
12082
12083 /* Build an incomplete assignment expression. */
12084
12085 static tree
12086 build_assignment (op, op_location, lhs, rhs)
12087      int op, op_location;
12088      tree lhs, rhs;
12089 {
12090   tree assignment;
12091   /* Build the corresponding binop if we deal with a Compound
12092      Assignment operator. Mark the binop sub-tree as part of a
12093      Compound Assignment expression */
12094   if (op != ASSIGN_TK)
12095     {
12096       rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12097       COMPOUND_ASSIGN_P (rhs) = 1;
12098     }
12099   assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12100   TREE_SIDE_EFFECTS (assignment) = 1;
12101   EXPR_WFL_LINECOL (assignment) = op_location;
12102   return assignment;
12103 }
12104
12105 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
12106
12107 char *
12108 print_int_node (node)
12109     tree node;
12110 {
12111   static char buffer [80];
12112   if (TREE_CONSTANT_OVERFLOW (node))
12113     sprintf (buffer, "<overflow>");
12114     
12115   if (TREE_INT_CST_HIGH (node) == 0)
12116     sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12117              TREE_INT_CST_LOW (node));
12118   else if (TREE_INT_CST_HIGH (node) == -1
12119            && TREE_INT_CST_LOW (node) != 0)
12120     {
12121       buffer [0] = '-';
12122       sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12123                -TREE_INT_CST_LOW (node));
12124     }
12125   else
12126     sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12127              TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12128
12129   return buffer;
12130 }
12131
12132 \f
12133
12134 /* This section of the code handle assignment check with FINAL
12135    variables.  */
12136
12137 static void
12138 reset_static_final_variable_assignment_flag (class)
12139      tree class;
12140 {
12141   tree field;
12142   for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12143     if (CLASS_FINAL_VARIABLE_P (field))
12144       DECL_FIELD_FINAL_LIIC (field) = 0;
12145 }
12146
12147 /* Figure whether all final static variable have been initialized.  */
12148
12149 static void
12150 check_static_final_variable_assignment_flag (class)
12151      tree class;
12152 {
12153   tree field;
12154
12155   for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12156     if (CLASS_FINAL_VARIABLE_P (field)
12157         && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12158       parse_error_context
12159         (DECL_FIELD_FINAL_WFL (field),
12160          "Blank static final variable `%s' may not have been initialized",
12161          IDENTIFIER_POINTER (DECL_NAME (field)));
12162 }
12163
12164 /* This function marks all final variable locally unassigned.  */
12165
12166 static void
12167 reset_final_variable_local_assignment_flag (class)
12168      tree class;
12169 {
12170   tree field;
12171   for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12172     if (FINAL_VARIABLE_P (field))
12173       DECL_FIELD_FINAL_LIIC (field) = 0;
12174 }
12175
12176 /* Figure whether all final variables have beem initialized in MDECL
12177    and mark MDECL accordingly.  */
12178
12179 static void
12180 check_final_variable_local_assignment_flag (class, mdecl)
12181      tree class;
12182      tree mdecl;
12183 {
12184   tree field;
12185   int initialized = 0;
12186   int non_initialized = 0; 
12187
12188   if (DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12189     return;
12190
12191   /* First find out whether all final variables or no final variable
12192      are initialized in this ctor. We don't take into account final
12193      variable that have been initialized upon declaration.  */
12194   for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12195     if (FINAL_VARIABLE_P (field) && !DECL_FIELD_FINAL_IUD (field))
12196       {
12197         if (DECL_FIELD_FINAL_LIIC (field))
12198           initialized++;
12199         else
12200           non_initialized++;
12201       }
12202
12203   /* There were no non initialized variable and no initialized variable.
12204      This ctor is fine. */
12205   if (!non_initialized && !initialized)
12206     DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12207   /* If no variables have been initialized, that fine. We'll check
12208      later whether this ctor calls a constructor which initializes
12209      them. We mark the ctor as not initializing all its finals. */
12210   else if (initialized == 0)
12211     DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12212   /* If we have a mixed bag, then we have a problem. We need to report
12213      all the variables we're not initializing.  */
12214   else if (initialized && non_initialized)
12215     {
12216       DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12217       for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12218         if (FIELD_FINAL (field)
12219             && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12220           {
12221             parse_error_context 
12222               (lookup_cl (mdecl),
12223                "Blank final variable `%s' may not have been initialized in this constructor",
12224                IDENTIFIER_POINTER (DECL_NAME (field)));
12225             DECL_FIELD_FINAL_IERR (field) = 1;
12226           }
12227     }
12228   /* Otherwise we know this ctor is initializing all its final
12229      variable. We mark it so. */
12230   else
12231     DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12232 }
12233
12234 /* This function recurses in a simple what through STMT and stops when
12235    it finds a constructor call. It then verifies that the called
12236    constructor initialized its final properly. Return 1 upon success,
12237    0 or -1 otherwise.  */
12238
12239 static int
12240 check_final_variable_indirect_assignment (stmt)
12241      tree stmt;
12242 {
12243   int res;
12244   switch (TREE_CODE (stmt))
12245     {
12246     case EXPR_WITH_FILE_LOCATION:
12247       return check_final_variable_indirect_assignment (EXPR_WFL_NODE (stmt));
12248     case COMPOUND_EXPR:
12249       res = check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12250       if (res)
12251         return res;
12252       return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 1));
12253     case SAVE_EXPR:
12254       return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12255     case CALL_EXPR:
12256       {
12257         tree decl = TREE_OPERAND (stmt, 0);
12258         tree fbody;
12259
12260         if (TREE_CODE (decl) != FUNCTION_DECL)
12261           decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
12262         if (TREE_CODE (decl) != FUNCTION_DECL)
12263           abort ();
12264         if (DECL_FUNCTION_ALL_FINAL_INITIALIZED (decl))
12265           return 1;
12266         if (DECL_FINIT_P (decl) || DECL_CONTEXT (decl) != current_class)
12267           return -1;
12268         fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
12269         if (fbody == error_mark_node)
12270           return -1;
12271         fbody = BLOCK_EXPR_BODY (fbody);
12272         return check_final_variable_indirect_assignment (fbody);
12273       }
12274     default:
12275       break;
12276     }
12277   return 0;
12278 }
12279
12280 /* This is the last chance to catch a final variable initialization
12281    problem. This routine will report an error if a final variable was
12282    never (globally) initialized and never reported as not having been
12283    initialized properly. */
12284
12285 static void
12286 check_final_variable_global_assignment_flag (class)
12287      tree class;
12288 {
12289   tree field, mdecl;
12290   int nnctor = 0;
12291
12292   /* We go through all natural ctors and see whether they're
12293      initializing all their final variables or not. */
12294   current_function_decl = NULL_TREE; /* For the error report. */
12295   for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
12296     if (DECL_CONSTRUCTOR_P (mdecl) && ! DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12297       {
12298         if (!DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl))
12299           {
12300             /* It doesn't. Maybe it calls a constructor that initializes
12301                them.  find out. */
12302             tree fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl));
12303             if (fbody == error_mark_node)
12304               continue;
12305             fbody = BLOCK_EXPR_BODY (fbody);
12306             if (check_final_variable_indirect_assignment (fbody) == 1)
12307               {
12308                 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12309                 nnctor++;
12310               }
12311             else
12312               parse_error_context
12313                 (lookup_cl (mdecl),
12314                  "Final variable initialization error in this constructor");
12315           }
12316         else
12317           nnctor++;
12318       }
12319
12320   /* Finally we catch final variables that never were initialized */
12321   for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12322     if (FINAL_VARIABLE_P (field)
12323         /* If the field wasn't initialized upon declaration */
12324         && !DECL_FIELD_FINAL_IUD (field)
12325         /* There wasn't natural ctor in which the field could have been
12326            initialized */
12327         && !nnctor
12328         /* If we never reported a problem with this field */
12329         && !DECL_FIELD_FINAL_IERR (field))
12330       {
12331         current_function_decl = NULL;
12332         parse_error_context
12333           (DECL_FIELD_FINAL_WFL (field),
12334            "Final variable `%s' hasn't been initialized upon its declaration",
12335            IDENTIFIER_POINTER (DECL_NAME (field)));
12336       }
12337
12338 }
12339
12340 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12341    context.  */
12342
12343 static int
12344 check_final_assignment (lvalue, wfl)
12345      tree lvalue, wfl;
12346 {
12347   if (TREE_CODE (lvalue) != COMPONENT_REF && !JDECL_P (lvalue))
12348     return 0;
12349
12350   if (TREE_CODE (lvalue) == COMPONENT_REF
12351       && JDECL_P (TREE_OPERAND (lvalue, 1)))
12352     lvalue = TREE_OPERAND (lvalue, 1);
12353
12354   if (!FIELD_FINAL (lvalue))
12355     return 0;
12356
12357   /* Now the logic. We can modify a final VARIABLE:
12358      1) in finit$, (its declaration was followed by an initialization,)
12359      2) consistently in each natural ctor, if it wasn't initialized in
12360         finit$ or once in <clinit>.  In any other cases, an error should be
12361         reported. */
12362   if (DECL_FINIT_P (current_function_decl))
12363     {
12364       DECL_FIELD_FINAL_IUD (lvalue) = 1;
12365       return 0;
12366     }
12367
12368   if (!DECL_FUNCTION_SYNTHETIC_CTOR (current_function_decl)
12369       /* Only if it wasn't given a value upon initialization */
12370       && DECL_LANG_SPECIFIC (lvalue) && !DECL_FIELD_FINAL_IUD (lvalue)
12371       /* If it was never assigned a value in this constructor */
12372       && !DECL_FIELD_FINAL_LIIC (lvalue))
12373     {
12374       /* Turn the locally assigned flag on, it will be checked later
12375          on to point out at discrepancies. */
12376       DECL_FIELD_FINAL_LIIC (lvalue) = 1;
12377       if (DECL_CLINIT_P (current_function_decl))
12378         DECL_FIELD_FINAL_IUD (lvalue) = 1;
12379       return 0;
12380     }
12381
12382   /* Other problems should be reported right away. */
12383   parse_error_context 
12384     (wfl, "Can't %sassign a value to the final variable `%s'",
12385      (FIELD_STATIC (lvalue) ? "re" : ""),
12386      IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12387
12388   /* Note that static field can be initialized once and only once. */
12389   if (FIELD_STATIC (lvalue))
12390     DECL_FIELD_FINAL_IERR (lvalue) = 1;
12391
12392   return 1;
12393 }
12394
12395 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12396    read. This is needed to avoid circularities in the implementation
12397    of these fields in libjava. */
12398
12399 static tree
12400 maybe_build_primttype_type_ref (rhs, wfl)
12401     tree rhs, wfl;
12402 {
12403   tree to_return = NULL_TREE;
12404   tree rhs_type = TREE_TYPE (rhs);
12405   if (TREE_CODE (rhs) == COMPOUND_EXPR)
12406     {
12407       tree n = TREE_OPERAND (rhs, 1);
12408       if (TREE_CODE (n) == VAR_DECL 
12409           && DECL_NAME (n) == TYPE_identifier_node
12410           && rhs_type == class_ptr_type
12411           && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
12412         {
12413           const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
12414           if (!strncmp (self_name, "java.lang.", 10))
12415             to_return = build_primtype_type_ref (self_name);
12416         }
12417     }
12418   return (to_return ? to_return : rhs );
12419 }
12420
12421 /* 15.25 Assignment operators. */
12422
12423 static tree
12424 patch_assignment (node, wfl_op1, wfl_op2)
12425      tree node;
12426      tree wfl_op1;
12427      tree wfl_op2;
12428 {
12429   tree rhs = TREE_OPERAND (node, 1);
12430   tree lvalue = TREE_OPERAND (node, 0), llvalue;
12431   tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12432   int error_found = 0;
12433   int lvalue_from_array = 0;
12434
12435   /* Can't assign to a (blank) final. */
12436   if (check_final_assignment (lvalue, wfl_op1))
12437     error_found = 1;
12438
12439   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12440
12441   /* Lhs can be a named variable */
12442   if (JDECL_P (lvalue))
12443     {
12444       lhs_type = TREE_TYPE (lvalue);
12445     }
12446   /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12447      comment on reason why */
12448   else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12449     {
12450       lhs_type = TREE_TYPE (lvalue);
12451       lvalue_from_array = 1;
12452     }
12453   /* Or a field access */
12454   else if (TREE_CODE (lvalue) == COMPONENT_REF)
12455     lhs_type = TREE_TYPE (lvalue);
12456   /* Or a function return slot */
12457   else if (TREE_CODE (lvalue) == RESULT_DECL)
12458     lhs_type = TREE_TYPE (lvalue);
12459   /* Otherwise, we might want to try to write into an optimized static
12460      final, this is an of a different nature, reported further on. */
12461   else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12462            && resolve_expression_name (wfl_op1, &llvalue))
12463     {
12464       if (!error_found && check_final_assignment (llvalue, wfl_op1))
12465         {
12466           /* What we should do instead is resetting the all the flags
12467              previously set, exchange lvalue for llvalue and continue. */
12468           error_found = 1;
12469           return error_mark_node;
12470         }
12471       else 
12472         lhs_type = TREE_TYPE (lvalue);
12473     }
12474   else 
12475     {
12476       parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12477       error_found = 1;
12478     }
12479
12480   rhs_type = TREE_TYPE (rhs);
12481   /* 5.1 Try the assignment conversion for builtin type. */
12482   new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12483
12484   /* 5.2 If it failed, try a reference conversion */
12485   if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12486     lhs_type = promote_type (rhs_type);
12487
12488   /* 15.25.2 If we have a compound assignment, convert RHS into the
12489      type of the LHS */
12490   else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12491     new_rhs = convert (lhs_type, rhs);
12492
12493   /* Explicit cast required. This is an error */
12494   if (!new_rhs)
12495     {
12496       char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12497       char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12498       tree wfl;
12499       char operation [32];      /* Max size known */
12500
12501       /* If the assignment is part of a declaration, we use the WFL of
12502          the declared variable to point out the error and call it a
12503          declaration problem. If the assignment is a genuine =
12504          operator, we call is a operator `=' problem, otherwise we
12505          call it an assignment problem. In both of these last cases,
12506          we use the WFL of the operator to indicate the error. */
12507
12508       if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12509         {
12510           wfl = wfl_op1;
12511           strcpy (operation, "declaration");
12512         }
12513       else
12514         {
12515           wfl = wfl_operator;
12516           if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12517             strcpy (operation, "assignment");
12518           else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12519             strcpy (operation, "`return'");
12520           else
12521             strcpy (operation, "`='");
12522         }
12523
12524       if (!valid_cast_to_p (rhs_type, lhs_type))
12525         parse_error_context
12526           (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12527            operation, t1, t2);
12528       else
12529         parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12530                              operation, t1, t2);
12531       free (t1); free (t2);
12532       error_found = 1;
12533     }
12534
12535   /* Inline read access to java.lang.PRIMTYPE.TYPE */
12536   if (new_rhs)
12537     new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
12538
12539   if (error_found)
12540     return error_mark_node;
12541
12542   /* 10.10: Array Store Exception runtime check */
12543   if (!flag_emit_class_files
12544       && !flag_emit_xref
12545       && lvalue_from_array 
12546       && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12547     {
12548       tree check;
12549       tree base = lvalue;
12550
12551       /* We need to retrieve the right argument for
12552          _Jv_CheckArrayStore.  This is somewhat complicated by bounds
12553          and null pointer checks, both of which wrap the operand in
12554          one layer of COMPOUND_EXPR.  */
12555       if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12556         base = TREE_OPERAND (lvalue, 0);
12557       else
12558         {
12559           base = TREE_OPERAND (base, 0);
12560           if (flag_bounds_check)
12561             base = TREE_OPERAND (base, 1);
12562           if (flag_check_references)
12563             base = TREE_OPERAND (base, 1);
12564           base = TREE_OPERAND (base, 0);        
12565         }
12566
12567       /* Build the invocation of _Jv_CheckArrayStore */
12568       new_rhs = save_expr (new_rhs);
12569       check = build (CALL_EXPR, void_type_node,
12570                      build_address_of (soft_checkarraystore_node),
12571                      tree_cons (NULL_TREE, base,
12572                                 build_tree_list (NULL_TREE, new_rhs)),
12573                      NULL_TREE);
12574       TREE_SIDE_EFFECTS (check) = 1;
12575
12576       /* We have to decide on an insertion point */
12577       if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12578         {
12579           tree t;
12580           if (flag_bounds_check)
12581             {
12582               t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12583               TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12584                 build (COMPOUND_EXPR, void_type_node, t, check);
12585             }
12586           else
12587             TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12588                                               check, TREE_OPERAND (lvalue, 1));
12589         }
12590       else 
12591         {
12592           /* Make sure the bound check will happen before the store check */
12593           if (flag_bounds_check)
12594             TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12595               build (COMPOUND_EXPR, void_type_node,
12596                      TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12597           else
12598             lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12599         }
12600     }
12601
12602   /* Final locals can be used as case values in switch
12603      statement. Prepare them for this eventuality. */
12604   if (TREE_CODE (lvalue) == VAR_DECL 
12605       && LOCAL_FINAL_P (lvalue)
12606       && TREE_CONSTANT (new_rhs)
12607       && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12608       && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12609       )
12610     {
12611       TREE_CONSTANT (lvalue) = 1;
12612       DECL_INITIAL (lvalue) = new_rhs;
12613     }
12614
12615   TREE_OPERAND (node, 0) = lvalue;
12616   TREE_OPERAND (node, 1) = new_rhs;
12617   TREE_TYPE (node) = lhs_type;
12618   return node;
12619 }
12620
12621 /* Check that type SOURCE can be cast into type DEST. If the cast
12622    can't occur at all, return 0 otherwise 1. This function is used to
12623    produce accurate error messages on the reasons why an assignment
12624    failed. */
12625
12626 static tree
12627 try_reference_assignconv (lhs_type, rhs)
12628      tree lhs_type, rhs;
12629 {
12630   tree new_rhs = NULL_TREE;
12631   tree rhs_type = TREE_TYPE (rhs);
12632
12633   if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12634     {
12635       /* `null' may be assigned to any reference type */
12636       if (rhs == null_pointer_node)
12637         new_rhs = null_pointer_node;
12638       /* Try the reference assignment conversion */
12639       else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12640         new_rhs = rhs;
12641       /* This is a magic assignment that we process differently */
12642       else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
12643         new_rhs = rhs;
12644     }
12645   return new_rhs;
12646 }
12647
12648 /* Check that RHS can be converted into LHS_TYPE by the assignment
12649    conversion (5.2), for the cases of RHS being a builtin type. Return
12650    NULL_TREE if the conversion fails or if because RHS isn't of a
12651    builtin type. Return a converted RHS if the conversion is possible.  */
12652
12653 static tree
12654 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12655      tree wfl_op1, lhs_type, rhs;
12656 {
12657   tree new_rhs = NULL_TREE;
12658   tree rhs_type = TREE_TYPE (rhs);
12659
12660   /* Handle boolean specially.  */
12661   if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12662       || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12663     {
12664       if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12665           && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12666         new_rhs = rhs;
12667     }
12668
12669   /* Zero accepted everywhere */
12670   else if (TREE_CODE (rhs) == INTEGER_CST 
12671       && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12672       && JPRIMITIVE_TYPE_P (rhs_type))
12673     new_rhs = convert (lhs_type, rhs);
12674
12675   /* 5.1.1 Try Identity Conversion,
12676      5.1.2 Try Widening Primitive Conversion */
12677   else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12678     new_rhs = convert (lhs_type, rhs);
12679
12680   /* Try a narrowing primitive conversion (5.1.3): 
12681        - expression is a constant expression of type int AND
12682        - variable is byte, short or char AND
12683        - The value of the expression is representable in the type of the 
12684          variable */
12685   else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12686            && (lhs_type == byte_type_node || lhs_type == char_type_node
12687                || lhs_type == short_type_node))
12688     {
12689       if (int_fits_type_p (rhs, lhs_type))
12690         new_rhs = convert (lhs_type, rhs);
12691       else if (wfl_op1)         /* Might be called with a NULL */
12692         parse_warning_context 
12693           (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'", 
12694            print_int_node (rhs), lang_printable_name (lhs_type, 0));
12695       /* Reported a warning that will turn into an error further
12696          down, so we don't return */
12697     }
12698
12699   return new_rhs;
12700 }
12701
12702 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12703    conversion (5.1.1) or widening primitive conversion (5.1.2).  Return
12704    0 is the conversion test fails.  This implements parts the method
12705    invocation convertion (5.3).  */
12706
12707 static int
12708 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12709      tree lhs_type, rhs_type;
12710 {
12711   /* 5.1.1: This is the identity conversion part. */
12712   if (lhs_type == rhs_type)
12713     return 1;
12714
12715   /* Reject non primitive types and boolean conversions.  */
12716   if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
12717     return 0;
12718
12719   /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12720      than a char can't be converted into a char. Short can't too, but
12721      the < test below takes care of that */
12722   if (lhs_type == char_type_node && rhs_type == byte_type_node)
12723     return 0;
12724
12725   /* Accept all promoted type here. Note, we can't use <= in the test
12726      below, because we still need to bounce out assignments of short
12727      to char and the likes */
12728   if (lhs_type == int_type_node
12729       && (rhs_type == promoted_byte_type_node
12730           || rhs_type == promoted_short_type_node
12731           || rhs_type == promoted_char_type_node
12732           || rhs_type == promoted_boolean_type_node))
12733     return 1;
12734
12735   /* From here, an integral is widened if its precision is smaller
12736      than the precision of the LHS or if the LHS is a floating point
12737      type, or the RHS is a float and the RHS a double. */
12738   if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type) 
12739        && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12740       || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12741       || (rhs_type == float_type_node && lhs_type == double_type_node))
12742     return 1;
12743
12744   return 0;
12745 }
12746
12747 /* Check that something of SOURCE type can be assigned or cast to
12748    something of DEST type at runtime. Return 1 if the operation is
12749    valid, 0 otherwise. If CAST is set to 1, we're treating the case
12750    were SOURCE is cast into DEST, which borrows a lot of the
12751    assignment check. */
12752
12753 static int
12754 valid_ref_assignconv_cast_p (source, dest, cast)
12755      tree source;
12756      tree dest;
12757      int cast;
12758 {
12759   /* SOURCE or DEST might be null if not from a declared entity. */
12760   if (!source || !dest)
12761     return 0;
12762   if (JNULLP_TYPE_P (source))
12763     return 1;
12764   if (TREE_CODE (source) == POINTER_TYPE)
12765     source = TREE_TYPE (source);
12766   if (TREE_CODE (dest) == POINTER_TYPE)
12767     dest = TREE_TYPE (dest);
12768
12769   /* If source and dest are being compiled from bytecode, they may need to
12770      be loaded. */
12771   if (CLASS_P (source) && !CLASS_LOADED_P (source))
12772     {
12773       load_class (source, 1);
12774       safe_layout_class (source);
12775     }
12776   if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12777     {
12778       load_class (dest, 1);
12779       safe_layout_class (dest);
12780     }
12781
12782   /* Case where SOURCE is a class type */
12783   if (TYPE_CLASS_P (source))
12784     {
12785       if (TYPE_CLASS_P (dest))
12786         return  (source == dest 
12787                  || inherits_from_p (source, dest)
12788                  || (cast && inherits_from_p (dest, source)));
12789       if (TYPE_INTERFACE_P (dest))
12790         {
12791           /* If doing a cast and SOURCE is final, the operation is
12792              always correct a compile time (because even if SOURCE
12793              does not implement DEST, a subclass of SOURCE might). */
12794           if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12795             return 1;
12796           /* Otherwise, SOURCE must implement DEST */
12797           return interface_of_p (dest, source);
12798         }
12799       /* DEST is an array, cast permited if SOURCE is of Object type */
12800       return (cast && source == object_type_node ? 1 : 0);
12801     }
12802   if (TYPE_INTERFACE_P (source))
12803     {
12804       if (TYPE_CLASS_P (dest))
12805         {
12806           /* If not casting, DEST must be the Object type */
12807           if (!cast)
12808             return dest == object_type_node;
12809           /* We're doing a cast. The cast is always valid is class
12810              DEST is not final, otherwise, DEST must implement SOURCE */
12811           else if (!CLASS_FINAL (TYPE_NAME (dest)))
12812             return 1;
12813           else
12814             return interface_of_p (source, dest);
12815         }
12816       if (TYPE_INTERFACE_P (dest))
12817         {
12818           /* If doing a cast, then if SOURCE and DEST contain method
12819              with the same signature but different return type, then
12820              this is a (compile time) error */
12821           if (cast)
12822             {
12823               tree method_source, method_dest;
12824               tree source_type;
12825               tree source_sig;
12826               tree source_name;
12827               for (method_source = TYPE_METHODS (source); method_source; 
12828                    method_source = TREE_CHAIN (method_source))
12829                 {
12830                   source_sig = 
12831                     build_java_argument_signature (TREE_TYPE (method_source));
12832                   source_type = TREE_TYPE (TREE_TYPE (method_source));
12833                   source_name = DECL_NAME (method_source);
12834                   for (method_dest = TYPE_METHODS (dest);
12835                        method_dest; method_dest = TREE_CHAIN (method_dest))
12836                     if (source_sig == 
12837                         build_java_argument_signature (TREE_TYPE (method_dest))
12838                         && source_name == DECL_NAME (method_dest)
12839                         && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12840                       return 0;
12841                 }
12842               return 1;
12843             }
12844           else
12845             return source == dest || interface_of_p (dest, source);
12846         }
12847       else
12848         {
12849           /* Array */
12850           return (cast
12851                   && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12852                       || (DECL_NAME (TYPE_NAME (source))
12853                           == java_io_serializable)));
12854         }
12855     }
12856   if (TYPE_ARRAY_P (source))
12857     {
12858       if (TYPE_CLASS_P (dest))
12859         return dest == object_type_node;
12860       /* Can't cast an array to an interface unless the interface is
12861          java.lang.Cloneable or java.io.Serializable.  */
12862       if (TYPE_INTERFACE_P (dest))
12863         return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12864                 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
12865       else                      /* Arrays */
12866         {
12867           tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12868           tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12869           
12870           /* In case of severe errors, they turn out null */
12871           if (!dest_element_type || !source_element_type)
12872             return 0;
12873           if (source_element_type == dest_element_type)
12874             return 1;
12875           return valid_ref_assignconv_cast_p (source_element_type,
12876                                               dest_element_type, cast);
12877         }
12878       return 0;
12879     }
12880   return 0;
12881 }
12882
12883 static int
12884 valid_cast_to_p (source, dest)
12885      tree source;
12886      tree dest;
12887 {
12888   if (TREE_CODE (source) == POINTER_TYPE)
12889     source = TREE_TYPE (source);
12890   if (TREE_CODE (dest) == POINTER_TYPE)
12891     dest = TREE_TYPE (dest);
12892
12893   if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12894     return valid_ref_assignconv_cast_p (source, dest, 1);
12895
12896   else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12897     return 1;
12898
12899   else if (TREE_CODE (source) == BOOLEAN_TYPE
12900            && TREE_CODE (dest) == BOOLEAN_TYPE)
12901     return 1;
12902
12903   return 0;
12904 }
12905
12906 static tree
12907 do_unary_numeric_promotion (arg)
12908      tree arg;
12909 {
12910   tree type = TREE_TYPE (arg);
12911   if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12912       || TREE_CODE (type) == CHAR_TYPE)
12913     arg = convert (int_type_node, arg);
12914   return arg;
12915 }
12916
12917 /* Return a non zero value if SOURCE can be converted into DEST using
12918    the method invocation conversion rule (5.3).  */
12919 static int
12920 valid_method_invocation_conversion_p (dest, source)
12921      tree dest, source;
12922 {
12923   return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
12924            && valid_builtin_assignconv_identity_widening_p (dest, source))
12925           || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12926               && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12927               && valid_ref_assignconv_cast_p (source, dest, 0)));
12928 }
12929
12930 /* Build an incomplete binop expression. */
12931
12932 static tree
12933 build_binop (op, op_location, op1, op2)
12934      enum tree_code op;
12935      int op_location;
12936      tree op1, op2;
12937 {
12938   tree binop = build (op, NULL_TREE, op1, op2);
12939   TREE_SIDE_EFFECTS (binop) = 1;
12940   /* Store the location of the operator, for better error report. The
12941      string of the operator will be rebuild based on the OP value. */
12942   EXPR_WFL_LINECOL (binop) = op_location;
12943   return binop;
12944 }
12945
12946 /* Build the string of the operator retained by NODE. If NODE is part
12947    of a compound expression, add an '=' at the end of the string. This
12948    function is called when an error needs to be reported on an
12949    operator. The string is returned as a pointer to a static character
12950    buffer. */
12951
12952 static char *
12953 operator_string (node)
12954      tree node;
12955 {
12956 #define BUILD_OPERATOR_STRING(S)                                        \
12957   {                                                                     \
12958     sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12959     return buffer;                                                      \
12960   }
12961   
12962   static char buffer [10];
12963   switch (TREE_CODE (node))
12964     {
12965     case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12966     case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12967     case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12968     case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12969     case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12970     case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12971     case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12972     case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12973     case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12974     case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12975     case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12976     case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12977     case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12978     case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12979     case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12980     case GT_EXPR: BUILD_OPERATOR_STRING (">");
12981     case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12982     case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12983     case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
12984     case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12985     case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12986     case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12987     case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12988     case PREINCREMENT_EXPR:     /* Fall through */
12989     case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12990     case PREDECREMENT_EXPR:     /* Fall through */
12991     case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12992     default:
12993       internal_error ("unregistered operator %s",
12994                       tree_code_name [TREE_CODE (node)]);
12995     }
12996   return NULL;
12997 #undef BUILD_OPERATOR_STRING
12998 }
12999
13000 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2.  */
13001
13002 static int
13003 java_decl_equiv (var_acc1, var_acc2)
13004      tree var_acc1, var_acc2;
13005 {
13006   if (JDECL_P (var_acc1))
13007     return (var_acc1 == var_acc2);
13008   
13009   return (TREE_CODE (var_acc1) == COMPONENT_REF
13010           && TREE_CODE (var_acc2) == COMPONENT_REF
13011           && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13012              == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13013           && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13014 }
13015
13016 /* Return a non zero value if CODE is one of the operators that can be
13017    used in conjunction with the `=' operator in a compound assignment.  */
13018
13019 static int
13020 binop_compound_p (code)
13021     enum tree_code code;
13022 {
13023   int i;
13024   for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13025     if (binop_lookup [i] == code)
13026       break;
13027
13028   return i < BINOP_COMPOUND_CANDIDATES;
13029 }
13030
13031 /* Reorganize after a fold to get SAVE_EXPR to generate what we want.  */
13032
13033 static tree
13034 java_refold (t)
13035      tree t;
13036 {
13037   tree c, b, ns, decl;
13038
13039   if (TREE_CODE (t) != MODIFY_EXPR)
13040     return t;
13041
13042   c = TREE_OPERAND (t, 1);
13043   if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13044          && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13045          && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13046     return t;
13047
13048   /* Now the left branch of the binary operator. */
13049   b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13050   if (! (b && TREE_CODE (b) == NOP_EXPR 
13051          && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13052     return t;
13053
13054   ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13055   if (! (ns && TREE_CODE (ns) == NOP_EXPR
13056          && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13057     return t;
13058
13059   decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13060   if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13061       /* It's got to be the an equivalent decl */
13062       && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13063     {
13064       /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13065       TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13066       /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13067       TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13068       /* Change the right part of the BINOP_EXPR */
13069       TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13070     }
13071
13072   return t;
13073 }
13074
13075 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13076    errors but we modify NODE so that it contains the type computed
13077    according to the expression, when it's fixed. Otherwise, we write
13078    error_mark_node as the type. It allows us to further the analysis
13079    of remaining nodes and detects more errors in certain cases.  */
13080
13081 static tree
13082 patch_binop (node, wfl_op1, wfl_op2)
13083      tree node;
13084      tree wfl_op1;
13085      tree wfl_op2;
13086 {
13087   tree op1 = TREE_OPERAND (node, 0);
13088   tree op2 = TREE_OPERAND (node, 1);
13089   tree op1_type = TREE_TYPE (op1);
13090   tree op2_type = TREE_TYPE (op2);
13091   tree prom_type = NULL_TREE, cn;
13092   int code = TREE_CODE (node);
13093
13094   /* If 1, tell the routine that we have to return error_mark_node
13095      after checking for the initialization of the RHS */
13096   int error_found = 0;
13097
13098   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13099
13100   /* If either op<n>_type are NULL, this might be early signs of an
13101      error situation, unless it's too early to tell (in case we're
13102      handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13103      correctly so the error can be later on reported accurately. */
13104   if (! (code == PLUS_EXPR || code == NE_EXPR 
13105          || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13106     {
13107       tree n;
13108       if (! op1_type)
13109         {
13110           n = java_complete_tree (op1);
13111           op1_type = TREE_TYPE (n);
13112         }
13113       if (! op2_type)
13114         {
13115           n = java_complete_tree (op2);
13116           op2_type = TREE_TYPE (n);
13117         }
13118     }
13119
13120   switch (code)
13121     {
13122     /* 15.16 Multiplicative operators */
13123     case MULT_EXPR:             /* 15.16.1 Multiplication Operator * */
13124     case RDIV_EXPR:             /* 15.16.2 Division Operator / */
13125     case TRUNC_DIV_EXPR:        /* 15.16.2 Integral type Division Operator / */
13126     case TRUNC_MOD_EXPR:        /* 15.16.3 Remainder operator % */
13127       if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13128         {
13129           if (!JNUMERIC_TYPE_P (op1_type))
13130             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13131           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13132             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13133           TREE_TYPE (node) = error_mark_node;
13134           error_found = 1;
13135           break;
13136         }
13137       prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13138       /* Change the division operator if necessary */
13139       if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13140         TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13141
13142       if (TREE_CODE (prom_type) == INTEGER_TYPE
13143           && flag_use_divide_subroutine
13144           && ! flag_emit_class_files
13145           && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13146         return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13147  
13148       /* This one is more complicated. FLOATs are processed by a
13149          function call to soft_fmod. Duplicate the value of the
13150          COMPOUND_ASSIGN_P flag. */
13151       if (code == TRUNC_MOD_EXPR)
13152         {
13153           tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13154           COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13155           TREE_SIDE_EFFECTS (mod)
13156             = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13157           return mod;
13158         }
13159       break;
13160
13161     /* 15.17 Additive Operators */
13162     case PLUS_EXPR:             /* 15.17.1 String Concatenation Operator + */
13163
13164       /* Operation is valid if either one argument is a string
13165          constant, a String object or a StringBuffer crafted for the
13166          purpose of the a previous usage of the String concatenation
13167          operator */
13168
13169       if (TREE_CODE (op1) == STRING_CST 
13170           || TREE_CODE (op2) == STRING_CST
13171           || JSTRING_TYPE_P (op1_type)
13172           || JSTRING_TYPE_P (op2_type)
13173           || IS_CRAFTED_STRING_BUFFER_P (op1)
13174           || IS_CRAFTED_STRING_BUFFER_P (op2))
13175         return build_string_concatenation (op1, op2);
13176
13177     case MINUS_EXPR:            /* 15.17.2 Additive Operators (+ and -) for
13178                                    Numeric Types */
13179       if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13180         {
13181           if (!JNUMERIC_TYPE_P (op1_type))
13182             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13183           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13184             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13185           TREE_TYPE (node) = error_mark_node;
13186           error_found = 1;
13187           break;
13188         }
13189       prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13190       break;
13191
13192     /* 15.18 Shift Operators */
13193     case LSHIFT_EXPR:
13194     case RSHIFT_EXPR:
13195     case URSHIFT_EXPR:
13196       if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13197         {
13198           if (!JINTEGRAL_TYPE_P (op1_type))
13199             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13200           else
13201             {
13202               if (JNUMERIC_TYPE_P (op2_type))
13203                 parse_error_context (wfl_operator,
13204                                      "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
13205                                      operator_string (node),
13206                                      lang_printable_name (op2_type, 0));
13207               else
13208                 parse_error_context (wfl_operator,
13209                                      "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral", 
13210                                      operator_string (node),
13211                                      lang_printable_name (op2_type, 0));
13212             }
13213           TREE_TYPE (node) = error_mark_node;
13214           error_found = 1;
13215           break;
13216         }
13217
13218       /* Unary numeric promotion (5.6.1) is performed on each operand
13219          separatly */
13220       op1 = do_unary_numeric_promotion (op1);
13221       op2 = do_unary_numeric_promotion (op2);
13222
13223       /* The type of the shift expression is the type of the promoted
13224          type of the left-hand operand */
13225       prom_type = TREE_TYPE (op1);
13226
13227       /* Shift int only up to 0x1f and long up to 0x3f */
13228       if (prom_type == int_type_node)
13229         op2 = fold (build (BIT_AND_EXPR, int_type_node, op2, 
13230                            build_int_2 (0x1f, 0)));
13231       else
13232         op2 = fold (build (BIT_AND_EXPR, int_type_node, op2, 
13233                            build_int_2 (0x3f, 0)));
13234
13235       /* The >>> operator is a >> operating on unsigned quantities */
13236       if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13237         {
13238           tree to_return;
13239           tree utype = unsigned_type (prom_type);
13240           op1 = convert (utype, op1);
13241           TREE_SET_CODE (node, RSHIFT_EXPR);
13242           TREE_OPERAND (node, 0) = op1;
13243           TREE_OPERAND (node, 1) = op2;
13244           TREE_TYPE (node) = utype;
13245           to_return = convert (prom_type, node);
13246           /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13247           COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13248           TREE_SIDE_EFFECTS (to_return)
13249             = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13250           return to_return;
13251         }
13252       break;
13253
13254       /* 15.19.1 Type Comparison Operator instaceof */
13255     case INSTANCEOF_EXPR:
13256
13257       TREE_TYPE (node) = boolean_type_node;
13258
13259       if (!(op2_type = resolve_type_during_patch (op2)))
13260         return error_mark_node;
13261
13262       /* The first operand must be a reference type or the null type */
13263       if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13264         error_found = 1;        /* Error reported further below */
13265
13266       /* The second operand must be a reference type */
13267       if (!JREFERENCE_TYPE_P (op2_type))
13268         {
13269           SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13270           parse_error_context
13271             (wfl_operator, "Invalid argument `%s' for `instanceof'",
13272              lang_printable_name (op2_type, 0));
13273           error_found = 1;
13274         }
13275
13276       if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13277         {
13278           /* If the first operand is null, the result is always false */
13279           if (op1 == null_pointer_node)
13280             return boolean_false_node;
13281           else if (flag_emit_class_files)
13282             {
13283               TREE_OPERAND (node, 1) = op2_type;
13284               TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13285               return node;
13286             }
13287           /* Otherwise we have to invoke instance of to figure it out */
13288           else
13289             return build_instanceof (op1, op2_type);
13290         }
13291       /* There is no way the expression operand can be an instance of
13292          the type operand. This is a compile time error. */
13293       else
13294         {
13295           char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13296           SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13297           parse_error_context 
13298             (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13299              t1, lang_printable_name (op2_type, 0));
13300           free (t1);
13301           error_found = 1;
13302         }
13303       
13304       break;
13305
13306       /* 15.21 Bitwise and Logical Operators */
13307     case BIT_AND_EXPR:
13308     case BIT_XOR_EXPR:
13309     case BIT_IOR_EXPR:
13310       if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13311         /* Binary numeric promotion is performed on both operand and the
13312            expression retain that type */
13313         prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13314
13315       else if (TREE_CODE (op1_type) == BOOLEAN_TYPE 
13316                && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13317         /* The type of the bitwise operator expression is BOOLEAN */
13318         prom_type = boolean_type_node;
13319       else
13320         {
13321           if (!JINTEGRAL_TYPE_P (op1_type))
13322             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13323           if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13324             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13325           TREE_TYPE (node) = error_mark_node;
13326           error_found = 1;
13327           /* Insert a break here if adding thing before the switch's
13328              break for this case */
13329         }
13330       break;
13331
13332       /* 15.22 Conditional-And Operator */
13333     case TRUTH_ANDIF_EXPR:
13334       /* 15.23 Conditional-Or Operator */
13335     case TRUTH_ORIF_EXPR:
13336       /* Operands must be of BOOLEAN type */
13337       if (TREE_CODE (op1_type) != BOOLEAN_TYPE || 
13338           TREE_CODE (op2_type) != BOOLEAN_TYPE)
13339         {
13340           if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13341             ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13342           if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13343             ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13344           TREE_TYPE (node) = boolean_type_node;
13345           error_found = 1;
13346           break;
13347         }
13348       /* The type of the conditional operators is BOOLEAN */
13349       prom_type = boolean_type_node;
13350       break;
13351
13352       /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13353     case LT_EXPR:
13354     case GT_EXPR:
13355     case LE_EXPR:
13356     case GE_EXPR:
13357       /* The type of each of the operands must be a primitive numeric
13358          type */
13359       if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13360         {
13361           if (!JNUMERIC_TYPE_P (op1_type))
13362             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13363           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13364             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13365           TREE_TYPE (node) = boolean_type_node;
13366           error_found = 1;
13367           break;
13368         }
13369       /* Binary numeric promotion is performed on the operands */
13370       binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13371       /* The type of the relation expression is always BOOLEAN */
13372       prom_type = boolean_type_node;
13373       break;
13374
13375       /* 15.20 Equality Operator */
13376     case EQ_EXPR:
13377     case NE_EXPR:
13378       /* It's time for us to patch the strings. */
13379       if ((cn = patch_string (op1))) 
13380        {
13381          op1 = cn;
13382          op1_type = TREE_TYPE (op1);
13383        }
13384       if ((cn = patch_string (op2))) 
13385        {
13386          op2 = cn;
13387          op2_type = TREE_TYPE (op2);
13388        }
13389       
13390       /* 15.20.1 Numerical Equality Operators == and != */
13391       /* Binary numeric promotion is performed on the operands */
13392       if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13393         binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13394       
13395       /* 15.20.2 Boolean Equality Operators == and != */
13396       else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13397           TREE_CODE (op2_type) == BOOLEAN_TYPE)
13398         ;                       /* Nothing to do here */
13399       
13400       /* 15.20.3 Reference Equality Operators == and != */
13401       /* Types have to be either references or the null type. If
13402          they're references, it must be possible to convert either
13403          type to the other by casting conversion. */
13404       else if (op1 == null_pointer_node || op2 == null_pointer_node 
13405                || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13406                    && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13407                        || valid_ref_assignconv_cast_p (op2_type, 
13408                                                        op1_type, 1))))
13409         ;                       /* Nothing to do here */
13410           
13411       /* Else we have an error figure what can't be converted into
13412          what and report the error */
13413       else
13414         {
13415           char *t1;
13416           t1 = xstrdup (lang_printable_name (op1_type, 0));
13417           parse_error_context 
13418             (wfl_operator,
13419              "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13420              operator_string (node), t1, 
13421              lang_printable_name (op2_type, 0));
13422           free (t1);
13423           TREE_TYPE (node) = boolean_type_node;
13424           error_found = 1;
13425           break;
13426         }
13427       prom_type = boolean_type_node;
13428       break;
13429     }
13430
13431   if (error_found)
13432     return error_mark_node;
13433
13434   TREE_OPERAND (node, 0) = op1;
13435   TREE_OPERAND (node, 1) = op2;
13436   TREE_TYPE (node) = prom_type;
13437   TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13438   
13439   if (flag_emit_xref)
13440     return node;
13441
13442   /* fold does not respect side-effect order as required for Java but not C.
13443    * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13444    * bytecode.
13445    */
13446   if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13447       : ! TREE_SIDE_EFFECTS (node))
13448     node = fold (node);
13449   return node;
13450 }
13451
13452 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13453    zero value, the value of CSTE comes after the valude of STRING */
13454
13455 static tree
13456 do_merge_string_cste (cste, string, string_len, after)
13457      tree cste;
13458      const char *string;
13459      int string_len, after;
13460 {
13461   const char *old = TREE_STRING_POINTER (cste);
13462   int old_len = TREE_STRING_LENGTH (cste);
13463   int len = old_len + string_len;
13464   char *new = alloca (len+1);
13465
13466   if (after)
13467     {
13468       memcpy (new, string, string_len);
13469       memcpy (&new [string_len], old, old_len);
13470     }
13471   else
13472     {
13473       memcpy (new, old, old_len);
13474       memcpy (&new [old_len], string, string_len);
13475     }
13476   new [len] = '\0';
13477   return build_string (len, new);
13478 }
13479
13480 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13481    new STRING_CST on success, NULL_TREE on failure */
13482
13483 static tree
13484 merge_string_cste (op1, op2, after)
13485      tree op1, op2;
13486      int after;
13487 {
13488   /* Handle two string constants right away */
13489   if (TREE_CODE (op2) == STRING_CST)
13490     return do_merge_string_cste (op1, TREE_STRING_POINTER (op2), 
13491                                  TREE_STRING_LENGTH (op2), after);
13492   
13493   /* Reasonable integer constant can be treated right away */
13494   if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13495     {
13496       static const char *boolean_true = "true";
13497       static const char *boolean_false = "false";
13498       static const char *null_pointer = "null";
13499       char ch[3];
13500       const char *string;
13501       
13502       if (op2 == boolean_true_node)
13503         string = boolean_true;
13504       else if (op2 == boolean_false_node)
13505         string = boolean_false;
13506       else if (op2 == null_pointer_node)
13507         string = null_pointer;
13508       else if (TREE_TYPE (op2) == char_type_node)
13509         {
13510           ch[0] = (char )TREE_INT_CST_LOW (op2);
13511           ch[1] = '\0';
13512           string = ch;
13513         }
13514       else
13515           string = print_int_node (op2);
13516       
13517       return do_merge_string_cste (op1, string, strlen (string), after);
13518     }
13519   return NULL_TREE;
13520 }
13521
13522 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13523    has to be a STRING_CST and the other part must be a STRING_CST or a
13524    INTEGRAL constant. Return a new STRING_CST if the operation
13525    succeed, NULL_TREE otherwise.
13526
13527    If the case we want to optimize for space, we might want to return
13528    NULL_TREE for each invocation of this routine. FIXME */
13529
13530 static tree
13531 string_constant_concatenation (op1, op2)
13532      tree op1, op2;
13533 {
13534   if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13535     {
13536       tree string, rest;
13537       int invert;
13538       
13539       string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13540       rest   = (string == op1 ? op2 : op1);
13541       invert = (string == op1 ? 0 : 1 );
13542       
13543       /* Walk REST, only if it looks reasonable */
13544       if (TREE_CODE (rest) != STRING_CST
13545           && !IS_CRAFTED_STRING_BUFFER_P (rest)
13546           && !JSTRING_TYPE_P (TREE_TYPE (rest))
13547           && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13548         {
13549           rest = java_complete_tree (rest);
13550           if (rest == error_mark_node)
13551             return error_mark_node;
13552           rest = fold (rest);
13553         }
13554       return merge_string_cste (string, rest, invert);
13555     }
13556   return NULL_TREE;
13557 }
13558
13559 /* Implement the `+' operator. Does static optimization if possible,
13560    otherwise create (if necessary) and append elements to a
13561    StringBuffer. The StringBuffer will be carried around until it is
13562    used for a function call or an assignment. Then toString() will be
13563    called on it to turn it into a String object. */
13564
13565 static tree
13566 build_string_concatenation (op1, op2)
13567      tree op1, op2;
13568 {
13569   tree result;
13570   int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13571
13572   if (flag_emit_xref)
13573     return build (PLUS_EXPR, string_type_node, op1, op2);
13574   
13575   /* Try to do some static optimization */
13576   if ((result = string_constant_concatenation (op1, op2)))
13577     return result;
13578
13579   /* Discard empty strings on either side of the expression */
13580   if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13581     {
13582       op1 = op2;
13583       op2 = NULL_TREE;
13584     }
13585   else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13586     op2 = NULL_TREE;
13587
13588   /* If operands are string constant, turn then into object references */
13589   if (TREE_CODE (op1) == STRING_CST)
13590     op1 = patch_string_cst (op1);
13591   if (op2 && TREE_CODE (op2) == STRING_CST)
13592     op2 = patch_string_cst (op2);
13593
13594   /* If either one of the constant is null and the other non null
13595      operand is a String object, return it. */
13596   if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13597     return op1;
13598
13599   /* If OP1 isn't already a StringBuffer, create and
13600      initialize a new one */
13601   if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13602     {
13603       /* Two solutions here: 
13604          1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13605          2) OP1 is something else, we call new StringBuffer().append(OP1).  */
13606       if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13607         op1 = BUILD_STRING_BUFFER (op1);
13608       else
13609         {
13610           tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13611           op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13612         }
13613     }
13614
13615   if (op2)
13616     {
13617       /* OP1 is no longer the last node holding a crafted StringBuffer */
13618       IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13619       /* Create a node for `{new...,xxx}.append (op2)' */
13620       if (op2)
13621         op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13622     }
13623
13624   /* Mark the last node holding a crafted StringBuffer */
13625   IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13626
13627   TREE_SIDE_EFFECTS (op1) = side_effects;
13628   return op1;
13629 }
13630
13631 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13632    StringBuffer. If no string were found to be patched, return
13633    NULL. */
13634
13635 static tree
13636 patch_string (node)
13637     tree node;
13638 {
13639   if (node == error_mark_node)
13640     return error_mark_node;
13641   if (TREE_CODE (node) == STRING_CST)
13642     return patch_string_cst (node);
13643   else if (IS_CRAFTED_STRING_BUFFER_P (node))
13644     {
13645       int saved = ctxp->explicit_constructor_p;
13646       tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13647       tree ret;
13648       /* Temporary disable forbid the use of `this'. */
13649       ctxp->explicit_constructor_p = 0;
13650       ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13651       /* String concatenation arguments must be evaluated in order too. */
13652       ret = force_evaluation_order (ret);
13653       /* Restore it at its previous value */
13654       ctxp->explicit_constructor_p = saved;
13655       return ret;
13656     }
13657   return NULL_TREE;
13658 }
13659
13660 /* Build the internal representation of a string constant.  */
13661
13662 static tree
13663 patch_string_cst (node)
13664      tree node;
13665 {
13666   int location;
13667   if (! flag_emit_class_files)
13668     {
13669       node = get_identifier (TREE_STRING_POINTER (node));
13670       location = alloc_name_constant (CONSTANT_String, node);
13671       node = build_ref_from_constant_pool (location);
13672     }
13673   TREE_TYPE (node) = string_ptr_type_node;
13674   TREE_CONSTANT (node) = 1;
13675   return node;
13676 }
13677
13678 /* Build an incomplete unary operator expression. */
13679
13680 static tree
13681 build_unaryop (op_token, op_location, op1)
13682      int op_token, op_location;
13683      tree op1;
13684 {
13685   enum tree_code op;
13686   tree unaryop;
13687   switch (op_token)
13688     {
13689     case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13690     case MINUS_TK: op = NEGATE_EXPR; break;
13691     case NEG_TK: op = TRUTH_NOT_EXPR; break;
13692     case NOT_TK: op = BIT_NOT_EXPR; break;
13693     default: abort ();
13694     }
13695
13696   unaryop = build1 (op, NULL_TREE, op1);
13697   TREE_SIDE_EFFECTS (unaryop) = 1;
13698   /* Store the location of the operator, for better error report. The
13699      string of the operator will be rebuild based on the OP value. */
13700   EXPR_WFL_LINECOL (unaryop) = op_location;
13701   return unaryop;
13702 }
13703
13704 /* Special case for the ++/-- operators, since they require an extra
13705    argument to build, which is set to NULL and patched
13706    later. IS_POST_P is 1 if the operator, 0 otherwise.  */
13707
13708 static tree
13709 build_incdec (op_token, op_location, op1, is_post_p)
13710      int op_token, op_location;
13711      tree op1;
13712      int is_post_p;
13713 {
13714   static enum tree_code lookup [2][2] = 
13715     {
13716       { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13717       { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13718     };
13719   tree node = build (lookup [is_post_p][(op_token - DECR_TK)], 
13720                      NULL_TREE, op1, NULL_TREE);
13721   TREE_SIDE_EFFECTS (node) = 1;
13722   /* Store the location of the operator, for better error report. The
13723      string of the operator will be rebuild based on the OP value. */
13724   EXPR_WFL_LINECOL (node) = op_location;
13725   return node;
13726 }     
13727
13728 /* Build an incomplete cast operator, based on the use of the
13729    CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13730    set. java_complete_tree is trained to walk a CONVERT_EXPR even
13731    though its type is already set.  */
13732
13733 static tree
13734 build_cast (location, type, exp)
13735      int location;
13736      tree type, exp;
13737 {
13738   tree node = build1 (CONVERT_EXPR, type, exp);
13739   EXPR_WFL_LINECOL (node) = location;
13740   return node;
13741 }
13742
13743 /* Build an incomplete class reference operator.  */
13744 static tree
13745 build_incomplete_class_ref (location, class_name)
13746     int location;
13747     tree class_name;
13748 {
13749   tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13750   EXPR_WFL_LINECOL (node) = location;
13751   return node;
13752 }
13753
13754 /* Complete an incomplete class reference operator.  */
13755 static tree
13756 patch_incomplete_class_ref (node)
13757     tree node;
13758 {
13759   tree type = TREE_OPERAND (node, 0);
13760   tree ref_type;
13761
13762   if (!(ref_type = resolve_type_during_patch (type)))
13763     return error_mark_node;
13764
13765   if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
13766     {
13767       /* A class referenced by `foo.class' is initialized.  */
13768       return build_class_init (ref_type, build_class_ref (ref_type));
13769     }
13770
13771   /* If we're emitting class files and we have to deal with non
13772      primitive types, we invoke (and consider generating) the
13773      synthetic static method `class$'. */
13774   if (!TYPE_DOT_CLASS (current_class))
13775       build_dot_class_method (current_class);
13776   ref_type = build_dot_class_method_invocation (ref_type);
13777   return java_complete_tree (ref_type);
13778 }
13779
13780 /* 15.14 Unary operators. We return error_mark_node in case of error,
13781    but preserve the type of NODE if the type is fixed.  */
13782
13783 static tree
13784 patch_unaryop (node, wfl_op)
13785      tree node;
13786      tree wfl_op;
13787 {
13788   tree op = TREE_OPERAND (node, 0);
13789   tree op_type = TREE_TYPE (op);
13790   tree prom_type = NULL_TREE, value, decl;
13791   int outer_field_flag = 0;
13792   int code = TREE_CODE (node);
13793   int error_found = 0;
13794
13795   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13796
13797   switch (code)
13798     {
13799       /* 15.13.2 Postfix Increment Operator ++ */
13800     case POSTINCREMENT_EXPR:
13801       /* 15.13.3 Postfix Increment Operator -- */
13802     case POSTDECREMENT_EXPR:
13803       /* 15.14.1 Prefix Increment Operator ++ */
13804     case PREINCREMENT_EXPR:
13805       /* 15.14.2 Prefix Decrement Operator -- */
13806     case PREDECREMENT_EXPR:
13807       op = decl = strip_out_static_field_access_decl (op);
13808       outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13809       /* We might be trying to change an outer field accessed using
13810          access method. */
13811       if (outer_field_flag)
13812         {
13813           /* Retrieve the decl of the field we're trying to access. We
13814              do that by first retrieving the function we would call to
13815              access the field. It has been already verified that this
13816              field isn't final */
13817           if (flag_emit_class_files)
13818             decl = TREE_OPERAND (op, 0);
13819           else
13820             decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13821           decl = DECL_FUNCTION_ACCESS_DECL (decl);
13822         }
13823       /* We really should have a JAVA_ARRAY_EXPR to avoid this */
13824       else if (!JDECL_P (decl) 
13825           && TREE_CODE (decl) != COMPONENT_REF
13826           && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13827           && TREE_CODE (decl) != INDIRECT_REF
13828           && !(TREE_CODE (decl) == COMPOUND_EXPR
13829                && TREE_OPERAND (decl, 1)
13830                && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
13831         {
13832           tree lvalue;
13833           /* Before screaming, check that we're not in fact trying to
13834              increment a optimized static final access, in which case
13835              we issue an different error message. */
13836           if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13837                 && resolve_expression_name (wfl_op, &lvalue)
13838                 && check_final_assignment (lvalue, wfl_op)))
13839             parse_error_context (wfl_operator, "Invalid argument to `%s'",
13840                                  operator_string (node));
13841           TREE_TYPE (node) = error_mark_node;
13842           error_found = 1;
13843         }
13844       
13845       if (check_final_assignment (op, wfl_op))
13846         error_found = 1;
13847
13848       /* From now on, we know that op if a variable and that it has a
13849          valid wfl. We use wfl_op to locate errors related to the
13850          ++/-- operand. */
13851       else if (!JNUMERIC_TYPE_P (op_type))
13852         {
13853           parse_error_context
13854             (wfl_op, "Invalid argument type `%s' to `%s'",
13855              lang_printable_name (op_type, 0), operator_string (node));
13856           TREE_TYPE (node) = error_mark_node;
13857           error_found = 1;
13858         }
13859       else
13860         {
13861           /* Before the addition, binary numeric promotion is performed on
13862              both operands, if really necessary */
13863           if (JINTEGRAL_TYPE_P (op_type))
13864             {
13865               value = build_int_2 (1, 0);
13866               TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13867             }
13868           else
13869             {
13870               value = build_int_2 (1, 0);
13871               TREE_TYPE (node) = 
13872                 binary_numeric_promotion (op_type, 
13873                                           TREE_TYPE (value), &op, &value);
13874             }
13875
13876           /* We remember we might be accessing an outer field */
13877           if (outer_field_flag)
13878             {
13879               /* We re-generate an access to the field */
13880               value = build (PLUS_EXPR, TREE_TYPE (op), 
13881                              build_outer_field_access (wfl_op, decl), value);
13882                                                     
13883               /* And we patch the original access$() into a write 
13884                  with plus_op as a rhs */
13885               return outer_field_access_fix (node, op, value);
13886             }
13887
13888           /* And write back into the node. */
13889           TREE_OPERAND (node, 0) = op;
13890           TREE_OPERAND (node, 1) = value;
13891           /* Convert the overall back into its original type, if
13892              necessary, and return */
13893           if (JINTEGRAL_TYPE_P (op_type))
13894             return fold (node);
13895           else
13896             return fold (convert (op_type, node));
13897         }
13898       break;
13899
13900       /* 15.14.3 Unary Plus Operator + */
13901     case UNARY_PLUS_EXPR:
13902       /* 15.14.4 Unary Minus Operator - */
13903     case NEGATE_EXPR:
13904       if (!JNUMERIC_TYPE_P (op_type))
13905         {
13906           ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13907           TREE_TYPE (node) = error_mark_node;
13908           error_found = 1;
13909         }
13910       /* Unary numeric promotion is performed on operand */
13911       else
13912         {
13913           op = do_unary_numeric_promotion (op);
13914           prom_type = TREE_TYPE (op);
13915           if (code == UNARY_PLUS_EXPR)
13916             return fold (op);
13917         }
13918       break;
13919
13920       /* 15.14.5 Bitwise Complement Operator ~ */
13921     case BIT_NOT_EXPR:
13922       if (!JINTEGRAL_TYPE_P (op_type))
13923         {
13924           ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13925           TREE_TYPE (node) = error_mark_node;
13926           error_found = 1;
13927         }
13928       else
13929         {
13930           op = do_unary_numeric_promotion (op);
13931           prom_type = TREE_TYPE (op);
13932         }
13933       break;
13934
13935       /* 15.14.6 Logical Complement Operator ! */
13936     case TRUTH_NOT_EXPR:
13937       if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13938         {
13939           ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
13940           /* But the type is known. We will report an error if further
13941              attempt of a assignment is made with this rhs */
13942           TREE_TYPE (node) = boolean_type_node;
13943           error_found = 1;
13944         }
13945       else
13946         prom_type = boolean_type_node;
13947       break;
13948
13949       /* 15.15 Cast Expression */
13950     case CONVERT_EXPR:
13951       value = patch_cast (node, wfl_operator);
13952       if (value == error_mark_node)
13953         {
13954           /* If this cast is part of an assignment, we tell the code
13955              that deals with it not to complain about a mismatch,
13956              because things have been cast, anyways */
13957           TREE_TYPE (node) = error_mark_node;
13958           error_found = 1;
13959         }
13960       else
13961         {
13962           value = fold (value);
13963           TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13964           return value;
13965         }
13966       break;
13967     }
13968   
13969   if (error_found)
13970     return error_mark_node;
13971
13972   /* There are cases where node has been replaced by something else
13973      and we don't end up returning here: UNARY_PLUS_EXPR,
13974      CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
13975   TREE_OPERAND (node, 0) = fold (op);
13976   TREE_TYPE (node) = prom_type;
13977   TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
13978   return fold (node);
13979 }
13980
13981 /* Generic type resolution that sometimes takes place during node
13982    patching. Returned the resolved type or generate an error
13983    message. Return the resolved type or NULL_TREE.  */
13984
13985 static tree
13986 resolve_type_during_patch (type)
13987      tree type;
13988 {
13989   if (unresolved_type_p (type, NULL))
13990     {
13991       tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
13992       if (!type_decl)
13993         {
13994           parse_error_context (type, 
13995                                "Class `%s' not found in type declaration",
13996                                IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13997           return NULL_TREE;
13998         }
13999       return TREE_TYPE (type_decl);
14000     }
14001   return type;
14002 }
14003 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14004    found. Otherwise NODE or something meant to replace it is returned.  */
14005
14006 static tree
14007 patch_cast (node, wfl_op)
14008      tree node;
14009      tree wfl_op;
14010 {
14011   tree op = TREE_OPERAND (node, 0);
14012   tree op_type = TREE_TYPE (op);
14013   tree cast_type = TREE_TYPE (node);
14014   char *t1;
14015
14016   /* First resolve OP_TYPE if unresolved */
14017   if (!(cast_type = resolve_type_during_patch (cast_type)))
14018     return error_mark_node;
14019
14020   /* Check on cast that are proven correct at compile time */
14021   if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14022     {
14023       /* Same type */
14024       if (cast_type == op_type)
14025         return node;
14026
14027       /* float and double type are converted to the original type main
14028          variant and then to the target type. */
14029       if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
14030         op = convert (integer_type_node, op);
14031
14032       /* Try widening/narowwing convertion. Potentially, things need
14033          to be worked out in gcc so we implement the extreme cases
14034          correctly. fold_convert() needs to be fixed. */
14035       return convert (cast_type, op);
14036     }
14037
14038   /* It's also valid to cast a boolean into a boolean */
14039   if (op_type == boolean_type_node && cast_type == boolean_type_node)
14040     return node;
14041
14042   /* null can be casted to references */
14043   if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14044     return build_null_of_type (cast_type);
14045
14046   /* The remaining legal casts involve conversion between reference
14047      types. Check for their compile time correctness. */
14048   if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type) 
14049       && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14050     {
14051       TREE_TYPE (node) = promote_type (cast_type);
14052       /* Now, the case can be determined correct at compile time if
14053          OP_TYPE can be converted into CAST_TYPE by assignment
14054          conversion (5.2) */
14055
14056       if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14057         {
14058           TREE_SET_CODE (node, NOP_EXPR);
14059           return node;
14060         }
14061
14062       if (flag_emit_class_files)
14063         {
14064           TREE_SET_CODE (node, CONVERT_EXPR);
14065           return node;
14066         }
14067
14068       /* The cast requires a run-time check */
14069       return build (CALL_EXPR, promote_type (cast_type),
14070                     build_address_of (soft_checkcast_node),
14071                     tree_cons (NULL_TREE, build_class_ref (cast_type),
14072                                build_tree_list (NULL_TREE, op)),
14073                     NULL_TREE);
14074     }
14075
14076   /* Any other casts are proven incorrect at compile time */
14077   t1 = xstrdup (lang_printable_name (op_type, 0));
14078   parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
14079                        t1, lang_printable_name (cast_type, 0));
14080   free (t1);
14081   return error_mark_node;
14082 }
14083
14084 /* Build a null constant and give it the type TYPE.  */
14085
14086 static tree
14087 build_null_of_type (type)
14088      tree type;
14089 {
14090   tree node = build_int_2 (0, 0);
14091   TREE_TYPE (node) = promote_type (type);
14092   return node;
14093 }
14094
14095 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14096    a list of indices. */
14097 static tree
14098 build_array_ref (location, array, index)
14099      int location;
14100      tree array, index;
14101 {
14102   tree node = build (ARRAY_REF, NULL_TREE, array, index);
14103   EXPR_WFL_LINECOL (node) = location;
14104   return node;
14105 }
14106
14107 /* 15.12 Array Access Expression */
14108
14109 static tree
14110 patch_array_ref (node)
14111      tree node;
14112 {
14113   tree array = TREE_OPERAND (node, 0);
14114   tree array_type  = TREE_TYPE (array);
14115   tree index = TREE_OPERAND (node, 1);
14116   tree index_type = TREE_TYPE (index);
14117   int error_found = 0;
14118
14119   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14120
14121   if (TREE_CODE (array_type) == POINTER_TYPE)
14122     array_type = TREE_TYPE (array_type);
14123
14124   /* The array reference must be an array */
14125   if (!TYPE_ARRAY_P (array_type))
14126     {
14127       parse_error_context 
14128         (wfl_operator,
14129          "`[]' can only be applied to arrays. It can't be applied to `%s'",
14130          lang_printable_name (array_type, 0));
14131       TREE_TYPE (node) = error_mark_node;
14132       error_found = 1;
14133     }
14134
14135   /* The array index undergoes unary numeric promotion. The promoted
14136      type must be int */
14137   index = do_unary_numeric_promotion (index);
14138   if (TREE_TYPE (index) != int_type_node)
14139     {
14140       if (valid_cast_to_p (index_type, int_type_node))
14141         parse_error_context (wfl_operator,
14142    "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
14143                              lang_printable_name (index_type, 0));
14144       else
14145         parse_error_context (wfl_operator,
14146           "Incompatible type for `[]'. Can't convert `%s' to `int'",
14147                              lang_printable_name (index_type, 0));
14148       TREE_TYPE (node) = error_mark_node;
14149       error_found = 1;
14150     }
14151
14152   if (error_found)
14153     return error_mark_node;
14154
14155   array_type = TYPE_ARRAY_ELEMENT (array_type);
14156
14157   if (flag_emit_class_files || flag_emit_xref)
14158     {
14159       TREE_OPERAND (node, 0) = array;
14160       TREE_OPERAND (node, 1) = index;
14161     }
14162   else
14163     {
14164       /* The save_expr is for correct evaluation order.  It would be cleaner
14165          to use force_evaluation_order (see comment there), but that is
14166          difficult when we also have to deal with bounds checking. */
14167       if (TREE_SIDE_EFFECTS (index))
14168         array = save_expr (array);
14169       node = build_java_arrayaccess (array, array_type, index);
14170       if (TREE_SIDE_EFFECTS (index))
14171         node = build (COMPOUND_EXPR, array_type, array, node);
14172     }
14173   TREE_TYPE (node) = array_type;
14174   return node;
14175 }
14176
14177 /* 15.9 Array Creation Expressions */
14178
14179 static tree
14180 build_newarray_node (type, dims, extra_dims)
14181      tree type;
14182      tree dims;
14183      int extra_dims;
14184 {
14185   tree node =
14186     build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims), 
14187            build_int_2 (extra_dims, 0));
14188   return node;
14189 }
14190
14191 static tree
14192 patch_newarray (node)
14193      tree node;
14194 {
14195   tree type = TREE_OPERAND (node, 0);
14196   tree dims = TREE_OPERAND (node, 1);
14197   tree cdim, array_type;
14198   int error_found = 0;
14199   int ndims = 0;
14200   int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14201
14202   /* Dimension types are verified. It's better for the types to be
14203      verified in order. */
14204   for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14205     {
14206       int dim_error = 0;
14207       tree dim = TREE_VALUE (cdim);
14208
14209       /* Dim might have been saved during its evaluation */
14210       dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14211
14212       /* The type of each specified dimension must be an integral type. */
14213       if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14214         dim_error = 1;
14215
14216       /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14217          promoted type must be int. */
14218       else
14219         {
14220           dim = do_unary_numeric_promotion (dim);
14221           if (TREE_TYPE (dim) != int_type_node)
14222             dim_error = 1;
14223         }
14224
14225       /* Report errors on types here */
14226       if (dim_error)
14227         {
14228           parse_error_context 
14229             (TREE_PURPOSE (cdim), 
14230              "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'", 
14231              (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14232               "Explicit cast needed to" : "Can't"),
14233              lang_printable_name (TREE_TYPE (dim), 0));
14234           error_found = 1;
14235         }
14236
14237       TREE_PURPOSE (cdim) = NULL_TREE;
14238     }
14239
14240   /* Resolve array base type if unresolved */
14241   if (!(type = resolve_type_during_patch (type)))
14242     error_found = 1;
14243
14244   if (error_found)
14245     {
14246       /* We don't want further evaluation of this bogus array creation
14247          operation */
14248       TREE_TYPE (node) = error_mark_node;
14249       return error_mark_node;
14250     }
14251
14252   /* Set array_type to the actual (promoted) array type of the result. */
14253   if (TREE_CODE (type) == RECORD_TYPE)
14254     type = build_pointer_type (type);
14255   while (--xdims >= 0)
14256     {
14257       type = promote_type (build_java_array_type (type, -1));
14258     }
14259   dims = nreverse (dims);
14260   array_type = type;
14261   for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14262     {
14263       type = array_type;
14264       array_type
14265         = build_java_array_type (type,
14266                                  TREE_CODE (cdim) == INTEGER_CST
14267                                  ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14268                                  : -1);
14269       array_type = promote_type (array_type);
14270     }
14271   dims = nreverse (dims);
14272
14273   /* The node is transformed into a function call. Things are done
14274      differently according to the number of dimensions. If the number
14275      of dimension is equal to 1, then the nature of the base type
14276      (primitive or not) matters. */
14277   if (ndims == 1)
14278     return build_new_array (type, TREE_VALUE (dims));
14279   
14280   /* Can't reuse what's already written in expr.c because it uses the
14281      JVM stack representation. Provide a build_multianewarray. FIXME */
14282   return build (CALL_EXPR, array_type,
14283                 build_address_of (soft_multianewarray_node),
14284                 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
14285                            tree_cons (NULL_TREE, 
14286                                       build_int_2 (ndims, 0), dims )),
14287                 NULL_TREE);
14288 }
14289
14290 /* 10.6 Array initializer.  */
14291
14292 /* Build a wfl for array element that don't have one, so we can
14293    pin-point errors.  */
14294
14295 static tree
14296 maybe_build_array_element_wfl (node)
14297      tree node;
14298 {
14299   if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14300     return build_expr_wfl (NULL_TREE, ctxp->filename,
14301                            ctxp->elc.line, ctxp->elc.prev_col);
14302   else
14303     return NULL_TREE;
14304 }
14305
14306 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14307    identification of initialized arrays easier to detect during walk
14308    and expansion.  */
14309
14310 static tree
14311 build_new_array_init (location, values)
14312      int location;
14313      tree values;
14314 {
14315   tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14316   tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14317   EXPR_WFL_LINECOL (to_return) = location;
14318   return to_return;
14319 }
14320
14321 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14322    occurred.  Otherwise return NODE after having set its type
14323    appropriately.  */
14324
14325 static tree
14326 patch_new_array_init (type, node)
14327      tree type, node;
14328 {
14329   int error_seen = 0;
14330   tree current, element_type;
14331   HOST_WIDE_INT length;
14332   int all_constant = 1;
14333   tree init = TREE_OPERAND (node, 0);
14334
14335   if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14336     {
14337       parse_error_context (node,
14338                            "Invalid array initializer for non-array type `%s'",
14339                            lang_printable_name (type, 1));
14340       return error_mark_node;
14341     }
14342   type = TREE_TYPE (type);
14343   element_type = TYPE_ARRAY_ELEMENT (type);
14344
14345   CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14346
14347   for (length = 0, current = CONSTRUCTOR_ELTS (init);
14348        current;  length++, current = TREE_CHAIN (current))
14349     {
14350       tree elt = TREE_VALUE (current);
14351       if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14352         {
14353           error_seen |= array_constructor_check_entry (element_type, current);
14354           elt = TREE_VALUE (current);
14355           /* When compiling to native code, STRING_CST is converted to
14356              INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14357           if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14358             all_constant = 0;
14359         }
14360       else
14361         {
14362           TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14363           TREE_PURPOSE (current) = NULL_TREE;
14364           all_constant = 0;
14365         }
14366       if (elt && TREE_CODE (elt) == TREE_LIST 
14367           && TREE_VALUE (elt) == error_mark_node)
14368         error_seen = 1;
14369     }
14370
14371   if (error_seen)
14372     return error_mark_node;
14373
14374   /* Create a new type. We can't reuse the one we have here by
14375      patching its dimension because it originally is of dimension -1
14376      hence reused by gcc. This would prevent triangular arrays. */
14377   type = build_java_array_type (element_type, length);
14378   TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14379   TREE_TYPE (node) = promote_type (type);
14380   TREE_CONSTANT (init) = all_constant;
14381   TREE_CONSTANT (node) = all_constant;
14382   return node;
14383 }
14384
14385 /* Verify that one entry of the initializer element list can be
14386    assigned to the array base type. Report 1 if an error occurred, 0
14387    otherwise.  */
14388
14389 static int
14390 array_constructor_check_entry (type, entry)
14391      tree type, entry;
14392 {
14393   char *array_type_string = NULL;       /* For error reports */
14394   tree value, type_value, new_value, wfl_value, patched;
14395   int error_seen = 0;
14396
14397   new_value = NULL_TREE;
14398   wfl_value = TREE_VALUE (entry);
14399
14400   value = java_complete_tree (TREE_VALUE (entry));
14401   /* patch_string return error_mark_node if arg is error_mark_node */
14402   if ((patched = patch_string (value)))
14403     value = patched;
14404   if (value == error_mark_node)
14405     return 1;
14406   
14407   type_value = TREE_TYPE (value);
14408   
14409   /* At anytime, try_builtin_assignconv can report a warning on
14410      constant overflow during narrowing. */
14411   SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14412   new_value = try_builtin_assignconv (wfl_operator, type, value);
14413   if (!new_value && (new_value = try_reference_assignconv (type, value)))
14414     type_value = promote_type (type);
14415
14416   /* Check and report errors */
14417   if (!new_value)
14418     {
14419       const char *msg = (!valid_cast_to_p (type_value, type) ?
14420                    "Can't" : "Explicit cast needed to");
14421       if (!array_type_string)
14422         array_type_string = xstrdup (lang_printable_name (type, 1));
14423       parse_error_context 
14424         (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14425          msg, lang_printable_name (type_value, 1), array_type_string);
14426       error_seen = 1;
14427     }
14428   
14429   if (new_value)
14430     {
14431       new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
14432       TREE_VALUE (entry) = new_value;
14433     }
14434
14435   if (array_type_string)
14436     free (array_type_string);
14437
14438   TREE_PURPOSE (entry) = NULL_TREE;
14439   return error_seen;
14440 }
14441
14442 static tree
14443 build_this (location)
14444      int location;
14445 {
14446   tree node = build_wfl_node (this_identifier_node);
14447   TREE_SET_CODE (node, THIS_EXPR);
14448   EXPR_WFL_LINECOL (node) = location;
14449   return node;
14450 }
14451
14452 /* 14.15 The return statement. It builds a modify expression that
14453    assigns the returned value to the RESULT_DECL that hold the value
14454    to be returned. */
14455
14456 static tree
14457 build_return (location, op)
14458      int location;
14459      tree op;
14460 {
14461   tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14462   EXPR_WFL_LINECOL (node) = location;
14463   node = build_debugable_stmt (location, node);
14464   return node;
14465 }
14466
14467 static tree
14468 patch_return (node)
14469      tree node;
14470 {
14471   tree return_exp = TREE_OPERAND (node, 0);
14472   tree meth = current_function_decl;
14473   tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14474   int error_found = 0;
14475
14476   TREE_TYPE (node) = error_mark_node;
14477   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14478
14479   /* It's invalid to have a return value within a function that is
14480      declared with the keyword void or that is a constructor */
14481   if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14482     error_found = 1;
14483
14484   /* It's invalid to use a return statement in a static block */
14485   if (DECL_CLINIT_P (current_function_decl))
14486     error_found = 1;
14487
14488   /* It's invalid to have a no return value within a function that
14489      isn't declared with the keyword `void' */
14490   if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14491     error_found = 2;
14492   
14493   if (in_instance_initializer)
14494     error_found = 1;
14495
14496   if (error_found)
14497     {
14498       if (in_instance_initializer)
14499         parse_error_context (wfl_operator,
14500                              "`return' inside instance initializer");
14501         
14502       else if (DECL_CLINIT_P (current_function_decl))
14503         parse_error_context (wfl_operator,
14504                              "`return' inside static initializer");
14505
14506       else if (!DECL_CONSTRUCTOR_P (meth))
14507         {
14508           char *t = xstrdup (lang_printable_name (mtype, 0));
14509           parse_error_context (wfl_operator, 
14510                                "`return' with%s value from `%s %s'",
14511                                (error_found == 1 ? "" : "out"), 
14512                                t, lang_printable_name (meth, 0));
14513           free (t);
14514         }
14515       else
14516         parse_error_context (wfl_operator, 
14517                              "`return' with value from constructor `%s'",
14518                              lang_printable_name (meth, 0));
14519       return error_mark_node;
14520     }
14521
14522   /* If we have a return_exp, build a modify expression and expand
14523      it. Note: at that point, the assignment is declared valid, but we
14524      may want to carry some more hacks */
14525   if (return_exp)
14526     {
14527       tree exp = java_complete_tree (return_exp);
14528       tree modify, patched;
14529
14530       /* If the function returned value and EXP are booleans, EXP has
14531       to be converted into the type of DECL_RESULT, which is integer
14532       (see complete_start_java_method) */
14533       if (TREE_TYPE (exp) == boolean_type_node &&
14534           TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14535         exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14536
14537       /* `null' can be assigned to a function returning a reference */
14538       if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14539           exp == null_pointer_node)
14540         exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14541
14542       if ((patched = patch_string (exp)))
14543         exp = patched;
14544       
14545       modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14546       EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14547       modify = java_complete_tree (modify);
14548
14549       if (modify != error_mark_node)
14550         {
14551           TREE_SIDE_EFFECTS (modify) = 1;
14552           TREE_OPERAND (node, 0) = modify;
14553         }
14554       else
14555         return error_mark_node;
14556     }
14557   TREE_TYPE (node) = void_type_node;
14558   TREE_SIDE_EFFECTS (node) = 1;
14559   return node;
14560 }
14561
14562 /* 14.8 The if Statement */
14563
14564 static tree
14565 build_if_else_statement (location, expression, if_body, else_body)
14566      int location;
14567      tree expression, if_body, else_body;
14568 {
14569   tree node;
14570   if (!else_body)
14571     else_body = empty_stmt_node;
14572   node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14573   EXPR_WFL_LINECOL (node) = location;
14574   node = build_debugable_stmt (location, node);
14575   return node;
14576 }
14577
14578 static tree
14579 patch_if_else_statement (node)
14580      tree node;
14581 {
14582   tree expression = TREE_OPERAND (node, 0);
14583
14584   TREE_TYPE (node) = error_mark_node;
14585   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14586
14587   /* The type of expression must be boolean */
14588   if (TREE_TYPE (expression) != boolean_type_node
14589       && TREE_TYPE (expression) != promoted_boolean_type_node)
14590     {
14591       parse_error_context 
14592         (wfl_operator, 
14593          "Incompatible type for `if'. Can't convert `%s' to `boolean'", 
14594          lang_printable_name (TREE_TYPE (expression), 0));
14595       return error_mark_node;
14596     }
14597   
14598   TREE_TYPE (node) = void_type_node;
14599   TREE_SIDE_EFFECTS (node) = 1;
14600   CAN_COMPLETE_NORMALLY (node)
14601     = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14602     | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
14603   return node;
14604 }
14605
14606 /* 14.6 Labeled Statements */
14607
14608 /* Action taken when a lableled statement is parsed. a new
14609    LABELED_BLOCK_EXPR is created. No statement is attached to the
14610    label, yet.  LABEL can be NULL_TREE for artificially-generated blocks. */
14611
14612 static tree
14613 build_labeled_block (location, label)
14614      int location;
14615      tree label;
14616 {
14617   tree label_name ;
14618   tree label_decl, node;
14619   if (label == NULL_TREE || label == continue_identifier_node)
14620     label_name = label;
14621   else
14622     {
14623       label_name = merge_qualified_name (label_id, label);
14624       /* Issue an error if we try to reuse a label that was previously
14625          declared */
14626       if (IDENTIFIER_LOCAL_VALUE (label_name))
14627         {
14628           EXPR_WFL_LINECOL (wfl_operator) = location;
14629           parse_error_context (wfl_operator,
14630             "Declaration of `%s' shadows a previous label declaration",
14631                                IDENTIFIER_POINTER (label));
14632           EXPR_WFL_LINECOL (wfl_operator) = 
14633             EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14634           parse_error_context (wfl_operator,
14635             "This is the location of the previous declaration of label `%s'",
14636                                IDENTIFIER_POINTER (label));
14637           java_error_count--;
14638         }
14639     }
14640
14641   label_decl = create_label_decl (label_name);
14642   node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14643   EXPR_WFL_LINECOL (node) = location;
14644   TREE_SIDE_EFFECTS (node) = 1;
14645   return node;
14646 }
14647
14648 /* A labeled statement LBE is attached a statement.  */
14649
14650 static tree
14651 finish_labeled_statement (lbe, statement)
14652      tree lbe;                  /* Labeled block expr */
14653      tree statement;
14654 {
14655   /* In anyways, tie the loop to its statement */
14656   LABELED_BLOCK_BODY (lbe) = statement;
14657   pop_labeled_block ();
14658   POP_LABELED_BLOCK ();
14659   return lbe;
14660 }
14661
14662 /* 14.10, 14.11, 14.12 Loop Statements */
14663
14664 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14665    list. */
14666
14667 static tree
14668 build_new_loop (loop_body)
14669      tree loop_body;
14670 {
14671   tree loop =  build (LOOP_EXPR, NULL_TREE, loop_body);
14672   TREE_SIDE_EFFECTS (loop) = 1;
14673   PUSH_LOOP (loop);
14674   return loop;
14675 }
14676
14677 /* Create a loop body according to the following structure:
14678      COMPOUND_EXPR
14679        COMPOUND_EXPR            (loop main body)
14680          EXIT_EXPR              (this order is for while/for loops.
14681          LABELED_BLOCK_EXPR      the order is reversed for do loops)
14682            LABEL_DECL           (a continue occuring here branches at the 
14683            BODY                  end of this labeled block)
14684        INCREMENT                (if any)
14685
14686   REVERSED, if non zero, tells that the loop condition expr comes
14687   after the body, like in the do-while loop.
14688
14689   To obtain a loop, the loop body structure described above is
14690   encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14691
14692    LABELED_BLOCK_EXPR
14693      LABEL_DECL                   (use this label to exit the loop)
14694      LOOP_EXPR
14695        <structure described above> */
14696
14697 static tree
14698 build_loop_body (location, condition, reversed)
14699      int location;
14700      tree condition;
14701      int reversed;
14702 {
14703   tree first, second, body;
14704
14705   condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14706   EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14707   condition = build_debugable_stmt (location, condition);
14708   TREE_SIDE_EFFECTS (condition) = 1;
14709
14710   body = build_labeled_block (0, continue_identifier_node);
14711   first = (reversed ? body : condition);
14712   second = (reversed ? condition : body);
14713   return 
14714     build (COMPOUND_EXPR, NULL_TREE, 
14715            build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14716 }
14717
14718 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14719    their order) on the current loop. Unlink the current loop from the
14720    loop list.  */
14721
14722 static tree
14723 finish_loop_body (location, condition, body, reversed)
14724      int location;
14725      tree condition, body;
14726      int reversed;
14727 {
14728   tree to_return = ctxp->current_loop;
14729   tree loop_body = LOOP_EXPR_BODY (to_return);
14730   if (condition)
14731     {
14732       tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14733       /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14734          The real EXIT_EXPR is one operand further. */
14735       EXPR_WFL_LINECOL (cnode) = location;
14736       /* This one is for accurate error reports */
14737       EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14738       TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14739     }
14740   LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14741   POP_LOOP ();
14742   return to_return;
14743 }
14744
14745 /* Tailored version of finish_loop_body for FOR loops, when FOR
14746    loops feature the condition part */
14747
14748 static tree
14749 finish_for_loop (location, condition, update, body)
14750     int location;
14751     tree condition, update, body;
14752 {
14753   /* Put the condition and the loop body in place */
14754   tree loop = finish_loop_body (location, condition, body, 0);
14755   /* LOOP is the current loop which has been now popped of the loop
14756      stack. Install the update block */
14757   LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14758   return loop;
14759 }
14760
14761 /* Try to find the loop a block might be related to. This comprises
14762    the case where the LOOP_EXPR is found as the second operand of a
14763    COMPOUND_EXPR, because the loop happens to have an initialization
14764    part, then expressed as the first operand of the COMPOUND_EXPR. If
14765    the search finds something, 1 is returned. Otherwise, 0 is
14766    returned. The search is assumed to start from a
14767    LABELED_BLOCK_EXPR's block.  */
14768
14769 static tree
14770 search_loop (statement)
14771     tree statement;
14772 {
14773   if (TREE_CODE (statement) == LOOP_EXPR)
14774     return statement;
14775
14776   if (TREE_CODE (statement) == BLOCK)
14777     statement = BLOCK_SUBBLOCKS (statement);
14778   else
14779     return NULL_TREE;
14780
14781   if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14782     while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14783       statement = TREE_OPERAND (statement, 1);
14784
14785   return (TREE_CODE (statement) == LOOP_EXPR
14786           && FOR_LOOP_P (statement) ? statement : NULL_TREE);
14787 }
14788
14789 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14790    returned otherwise.  */
14791
14792 static int
14793 labeled_block_contains_loop_p (block, loop)
14794     tree block, loop;
14795 {
14796   if (!block)
14797     return 0;
14798
14799   if (LABELED_BLOCK_BODY (block) == loop)
14800     return 1;
14801
14802   if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
14803     return 1;
14804
14805   return 0;
14806 }
14807
14808 /* If the loop isn't surrounded by a labeled statement, create one and
14809    insert LOOP as its body.  */
14810
14811 static tree
14812 patch_loop_statement (loop)
14813      tree loop;
14814 {
14815   tree loop_label;
14816
14817   TREE_TYPE (loop) = void_type_node;
14818   if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14819     return loop;
14820
14821   loop_label = build_labeled_block (0, NULL_TREE);
14822   /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14823      that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
14824   LABELED_BLOCK_BODY (loop_label) = loop;
14825   PUSH_LABELED_BLOCK (loop_label);
14826   return loop_label;
14827 }
14828
14829 /* 14.13, 14.14: break and continue Statements */
14830
14831 /* Build a break or a continue statement. a null NAME indicates an
14832    unlabeled break/continue statement.  */
14833
14834 static tree
14835 build_bc_statement (location, is_break, name)
14836      int location, is_break;
14837      tree name;
14838 {
14839   tree break_continue, label_block_expr = NULL_TREE;
14840
14841   if (name)
14842     {
14843       if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE 
14844             (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14845         /* Null means that we don't have a target for this named
14846            break/continue. In this case, we make the target to be the
14847            label name, so that the error can be reported accuratly in
14848            patch_bc_statement. */
14849         label_block_expr = EXPR_WFL_NODE (name);
14850     }
14851   /* Unlabeled break/continue will be handled during the
14852      break/continue patch operation */
14853   break_continue 
14854     = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14855
14856   IS_BREAK_STMT_P (break_continue) = is_break;
14857   TREE_SIDE_EFFECTS (break_continue) = 1;
14858   EXPR_WFL_LINECOL (break_continue) = location;
14859   break_continue = build_debugable_stmt (location, break_continue);
14860   return break_continue;
14861 }
14862
14863 /* Verification of a break/continue statement. */
14864
14865 static tree
14866 patch_bc_statement (node)
14867      tree node;
14868 {
14869   tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
14870   tree labeled_block = ctxp->current_labeled_block;
14871   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14872  
14873   /* Having an identifier here means that the target is unknown. */
14874   if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
14875     {
14876       parse_error_context (wfl_operator, "No label definition found for `%s'",
14877                            IDENTIFIER_POINTER (bc_label));
14878       return error_mark_node;
14879     }
14880   if (! IS_BREAK_STMT_P (node))
14881     {
14882       /* It's a continue statement. */
14883       for (;; labeled_block = TREE_CHAIN (labeled_block))
14884         {
14885           if (labeled_block == NULL_TREE)
14886             {
14887               if (bc_label == NULL_TREE)
14888                 parse_error_context (wfl_operator,
14889                                      "`continue' must be in loop");
14890               else
14891                 parse_error_context 
14892                   (wfl_operator, "continue label `%s' does not name a loop",
14893                    IDENTIFIER_POINTER (bc_label));
14894               return error_mark_node;
14895             }
14896           if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14897                == continue_identifier_node)
14898               && (bc_label == NULL_TREE
14899                   || TREE_CHAIN (labeled_block) == bc_label))
14900             {
14901               bc_label = labeled_block;
14902               break;
14903             }
14904         }
14905     }
14906   else if (!bc_label)
14907     { 
14908       for (;; labeled_block = TREE_CHAIN (labeled_block))
14909         {
14910           if (labeled_block == NULL_TREE)
14911             {
14912               parse_error_context (wfl_operator,
14913                                      "`break' must be in loop or switch");
14914               return error_mark_node;
14915             }
14916           target_stmt = LABELED_BLOCK_BODY (labeled_block);
14917           if (TREE_CODE (target_stmt) == SWITCH_EXPR
14918               || search_loop (target_stmt))
14919             {
14920               bc_label = labeled_block;
14921               break;
14922             }
14923         }
14924     }
14925
14926   EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
14927   CAN_COMPLETE_NORMALLY (bc_label) = 1;
14928
14929   /* Our break/continue don't return values. */
14930   TREE_TYPE (node) = void_type_node;
14931   /* Encapsulate the break within a compound statement so that it's
14932      expanded all the times by expand_expr (and not clobbered
14933      sometimes, like after a if statement) */
14934   node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14935   TREE_SIDE_EFFECTS (node) = 1;
14936   return node;
14937 }
14938
14939 /* Process the exit expression belonging to a loop. Its type must be
14940    boolean.  */
14941
14942 static tree
14943 patch_exit_expr (node)
14944      tree node;
14945 {
14946   tree expression = TREE_OPERAND (node, 0);
14947   TREE_TYPE (node) = error_mark_node;
14948   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14949
14950   /* The type of expression must be boolean */
14951   if (TREE_TYPE (expression) != boolean_type_node)
14952     {
14953       parse_error_context 
14954         (wfl_operator, 
14955     "Incompatible type for loop conditional. Can't convert `%s' to `boolean'", 
14956          lang_printable_name (TREE_TYPE (expression), 0));
14957       return error_mark_node;
14958     }
14959   /* Now we know things are allright, invert the condition, fold and
14960      return */
14961   TREE_OPERAND (node, 0) = 
14962     fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
14963
14964   if (! integer_zerop (TREE_OPERAND (node, 0))
14965       && ctxp->current_loop != NULL_TREE
14966       && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14967     CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14968   if (! integer_onep (TREE_OPERAND (node, 0)))
14969     CAN_COMPLETE_NORMALLY (node) = 1;
14970
14971
14972   TREE_TYPE (node) = void_type_node;
14973   return node;
14974 }
14975
14976 /* 14.9 Switch statement */
14977
14978 static tree
14979 patch_switch_statement (node)
14980      tree node;
14981 {
14982   tree se = TREE_OPERAND (node, 0), se_type;
14983
14984   /* Complete the switch expression */
14985   se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14986   se_type = TREE_TYPE (se);
14987   /* The type of the switch expression must be char, byte, short or
14988      int */
14989   if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
14990     {
14991       EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14992       parse_error_context (wfl_operator,
14993           "Incompatible type for `switch'. Can't convert `%s' to `int'",
14994                            lang_printable_name (se_type, 0));
14995       /* This is what java_complete_tree will check */
14996       TREE_OPERAND (node, 0) = error_mark_node;
14997       return error_mark_node;
14998     }
14999
15000   TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15001
15002   /* Ready to return */
15003   if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15004     {
15005       TREE_TYPE (node) = error_mark_node;
15006       return error_mark_node;
15007     }
15008   TREE_TYPE (node) = void_type_node;
15009   TREE_SIDE_EFFECTS (node) = 1;
15010   CAN_COMPLETE_NORMALLY (node)
15011     = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)) 
15012       || ! SWITCH_HAS_DEFAULT (node);
15013   return node;
15014 }
15015
15016 /* 14.18 The try/catch statements */
15017
15018 static tree
15019 build_try_statement (location, try_block, catches)
15020      int location;
15021      tree try_block, catches;
15022 {
15023   tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
15024   EXPR_WFL_LINECOL (node) = location;
15025   return node;
15026 }
15027
15028 static tree
15029 build_try_finally_statement (location, try_block, finally)
15030      int location;
15031      tree try_block, finally;
15032 {
15033   tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15034   EXPR_WFL_LINECOL (node) = location;
15035   return node;
15036 }
15037
15038 static tree
15039 patch_try_statement (node)
15040      tree node;
15041 {
15042   int error_found = 0;
15043   tree try = TREE_OPERAND (node, 0);
15044   /* Exception handlers are considered in left to right order */
15045   tree catch = nreverse (TREE_OPERAND (node, 1));
15046   tree current, caught_type_list = NULL_TREE;
15047
15048   /* Check catch clauses, if any. Every time we find an error, we try
15049      to process the next catch clause. We process the catch clause before
15050      the try block so that when processing the try block we can check thrown
15051      exceptions againts the caught type list. */
15052   for (current = catch; current; current = TREE_CHAIN (current))
15053     {
15054       tree carg_decl, carg_type;
15055       tree sub_current, catch_block, catch_clause;
15056       int unreachable;
15057
15058       /* At this point, the structure of the catch clause is
15059            CATCH_EXPR           (catch node)
15060              BLOCK              (with the decl of the parameter)
15061                COMPOUND_EXPR
15062                  MODIFY_EXPR   (assignment of the catch parameter)
15063                  BLOCK          (catch clause block)
15064        */
15065       catch_clause = TREE_OPERAND (current, 0);
15066       carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15067       carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15068
15069       /* Catch clauses can't have more than one parameter declared,
15070          but it's already enforced by the grammar. Make sure that the
15071          only parameter of the clause statement in of class Throwable
15072          or a subclass of Throwable, but that was done earlier. The
15073          catch clause parameter type has also been resolved. */
15074       
15075       /* Just make sure that the catch clause parameter type inherits
15076          from java.lang.Throwable */
15077       if (!inherits_from_p (carg_type, throwable_type_node))
15078         {
15079           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15080           parse_error_context (wfl_operator,
15081                                "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
15082                                lang_printable_name (carg_type, 0));
15083           error_found = 1;
15084           continue;
15085         }
15086       
15087       /* Partial check for unreachable catch statement: The catch
15088          clause is reachable iff is no earlier catch block A in
15089          the try statement such that the type of the catch
15090          clause's parameter is the same as or a subclass of the
15091          type of A's parameter */
15092       unreachable = 0;
15093       for (sub_current = catch;
15094            sub_current != current; sub_current = TREE_CHAIN (sub_current))
15095         {
15096           tree sub_catch_clause, decl;
15097           sub_catch_clause = TREE_OPERAND (sub_current, 0);
15098           decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15099
15100           if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15101             {
15102               EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15103               parse_error_context 
15104                 (wfl_operator,
15105                  "`catch' not reached because of the catch clause at line %d",
15106                  EXPR_WFL_LINENO (sub_current));
15107               unreachable = error_found = 1;
15108               break;
15109             }
15110         }
15111       /* Complete the catch clause block */
15112       catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15113       if (catch_block == error_mark_node)
15114         {
15115           error_found = 1;
15116           continue;
15117         }
15118       if (CAN_COMPLETE_NORMALLY (catch_block))
15119         CAN_COMPLETE_NORMALLY (node) = 1;
15120       TREE_OPERAND (current, 0) = catch_block;
15121
15122       if (unreachable)
15123         continue;
15124
15125       /* Things to do here: the exception must be thrown */
15126
15127       /* Link this type to the caught type list */
15128       caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15129     }
15130
15131   PUSH_EXCEPTIONS (caught_type_list);
15132   if ((try = java_complete_tree (try)) == error_mark_node)
15133     error_found = 1;
15134   if (CAN_COMPLETE_NORMALLY (try))
15135     CAN_COMPLETE_NORMALLY (node) = 1;
15136   POP_EXCEPTIONS ();
15137
15138   /* Verification ends here */
15139   if (error_found) 
15140     return error_mark_node;
15141
15142   TREE_OPERAND (node, 0) = try;
15143   TREE_OPERAND (node, 1) = catch;
15144   TREE_TYPE (node) = void_type_node;
15145   return node;
15146 }
15147
15148 /* 14.17 The synchronized Statement */
15149
15150 static tree
15151 patch_synchronized_statement (node, wfl_op1)
15152     tree node, wfl_op1;
15153 {
15154   tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15155   tree block = TREE_OPERAND (node, 1);
15156
15157   tree tmp, enter, exit, expr_decl, assignment;
15158
15159   if (expr == error_mark_node)
15160     {
15161       block = java_complete_tree (block);
15162       return expr;
15163     }
15164
15165   /* We might be trying to synchronize on a STRING_CST */
15166   if ((tmp = patch_string (expr)))
15167     expr = tmp;
15168
15169   /* The TYPE of expr must be a reference type */
15170   if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15171     {
15172       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15173       parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
15174                            lang_printable_name (TREE_TYPE (expr), 0));
15175       return error_mark_node;
15176     }
15177
15178   if (flag_emit_xref)
15179     {
15180       TREE_OPERAND (node, 0) = expr;
15181       TREE_OPERAND (node, 1) = java_complete_tree (block);
15182       CAN_COMPLETE_NORMALLY (node) = 1;
15183       return node;
15184     }
15185
15186   /* Generate a try-finally for the synchronized statement, except
15187      that the handler that catches all throw exception calls
15188      _Jv_MonitorExit and then rethrow the exception.
15189      The synchronized statement is then implemented as:
15190      TRY 
15191        {
15192          _Jv_MonitorEnter (expression)
15193          synchronized_block
15194          _Jv_MonitorExit (expression)
15195        }
15196      CATCH_ALL
15197        {
15198          e = _Jv_exception_info ();
15199          _Jv_MonitorExit (expression)
15200          Throw (e);
15201        } */
15202
15203   expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15204   BUILD_MONITOR_ENTER (enter, expr_decl);
15205   BUILD_MONITOR_EXIT (exit, expr_decl);
15206   CAN_COMPLETE_NORMALLY (enter) = 1;
15207   CAN_COMPLETE_NORMALLY (exit) = 1;
15208   assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15209   TREE_SIDE_EFFECTS (assignment) = 1;
15210   node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
15211                  build (COMPOUND_EXPR, NULL_TREE,
15212                         build (WITH_CLEANUP_EXPR, NULL_TREE,
15213                                build (COMPOUND_EXPR, NULL_TREE,
15214                                       assignment, enter),
15215                                NULL_TREE, exit),
15216                         block));
15217   node = build_expr_block (node, expr_decl);
15218
15219   return java_complete_tree (node);
15220 }
15221
15222 /* 14.16 The throw Statement */
15223
15224 static tree
15225 patch_throw_statement (node, wfl_op1)
15226     tree node, wfl_op1;
15227 {
15228   tree expr = TREE_OPERAND (node, 0);
15229   tree type = TREE_TYPE (expr);
15230   int unchecked_ok = 0, tryblock_throws_ok = 0;
15231
15232   /* Thrown expression must be assignable to java.lang.Throwable */
15233   if (!try_reference_assignconv (throwable_type_node, expr))
15234     {
15235       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15236       parse_error_context (wfl_operator,
15237     "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
15238                            lang_printable_name (type, 0));
15239       /* If the thrown expression was a reference, we further the
15240          compile-time check. */
15241       if (!JREFERENCE_TYPE_P (type))
15242         return error_mark_node;
15243     }
15244
15245   /* At least one of the following must be true */
15246
15247   /* The type of the throw expression is a not checked exception,
15248      i.e. is a unchecked expression. */
15249   unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15250
15251   SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15252   /* An instance can't throw a checked excetion unless that exception
15253      is explicitely declared in the `throws' clause of each
15254      constructor. This doesn't apply to anonymous classes, since they
15255      don't have declared constructors. */
15256   if (!unchecked_ok 
15257       && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
15258     {
15259       tree current;
15260       for (current = TYPE_METHODS (current_class); current; 
15261            current = TREE_CHAIN (current))
15262         if (DECL_CONSTRUCTOR_P (current) 
15263             && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15264           {
15265             parse_error_context (wfl_operator, "Checked exception `%s' can't be thrown in instance initializer (not all declared constructor are declaring it in their `throws' clause)", 
15266                                  lang_printable_name (TREE_TYPE (expr), 0));
15267             return error_mark_node;
15268           }
15269     }
15270
15271   /* Throw is contained in a try statement and at least one catch
15272      clause can receive the thrown expression or the current method is
15273      declared to throw such an exception. Or, the throw statement is
15274      contained in a method or constructor declaration and the type of
15275      the Expression is assignable to at least one type listed in the
15276      throws clause the declaration. */
15277   if (!unchecked_ok)
15278     tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15279   if (!(unchecked_ok || tryblock_throws_ok))
15280     {
15281       /* If there is a surrounding try block that has no matching
15282          clatch clause, report it first. A surrounding try block exits
15283          only if there is something after the list of checked
15284          exception thrown by the current function (if any). */
15285       if (IN_TRY_BLOCK_P ())
15286         parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
15287                              lang_printable_name (type, 0));
15288       /* If we have no surrounding try statement and the method doesn't have
15289          any throws, report it now. FIXME */
15290
15291       /* We report that the exception can't be throw from a try block
15292          in all circumstances but when the `throw' is inside a static
15293          block. */
15294       else if (!EXCEPTIONS_P (currently_caught_type_list) 
15295                && !tryblock_throws_ok)
15296         {
15297           if (DECL_CLINIT_P (current_function_decl))
15298             parse_error_context (wfl_operator,
15299                    "Checked exception `%s' can't be thrown in initializer",
15300                                  lang_printable_name (type, 0));
15301           else
15302             parse_error_context (wfl_operator,
15303                    "Checked exception `%s' isn't thrown from a `try' block", 
15304                                  lang_printable_name (type, 0));
15305         }
15306       /* Otherwise, the current method doesn't have the appropriate
15307          throws declaration */
15308       else
15309         parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)", 
15310                              lang_printable_name (type, 0));
15311       return error_mark_node;
15312     }
15313
15314   if (! flag_emit_class_files && ! flag_emit_xref)
15315     BUILD_THROW (node, expr);
15316
15317   /* If doing xrefs, keep the location where the `throw' was seen. */
15318   if (flag_emit_xref)
15319     EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15320   return node;
15321 }
15322
15323 /* Check that exception said to be thrown by method DECL can be
15324    effectively caught from where DECL is invoked.  */
15325
15326 static void
15327 check_thrown_exceptions (location, decl)
15328      int location;
15329      tree decl;
15330 {
15331   tree throws;
15332   /* For all the unchecked exceptions thrown by DECL */
15333   for (throws = DECL_FUNCTION_THROWS (decl); throws; 
15334        throws = TREE_CHAIN (throws)) 
15335     if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15336       {
15337 #if 1
15338         /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15339         if (DECL_NAME (decl) == get_identifier ("clone"))
15340           continue;
15341 #endif
15342         EXPR_WFL_LINECOL (wfl_operator) = location;
15343         if (DECL_FINIT_P (current_function_decl))
15344           parse_error_context
15345             (wfl_operator, "Exception `%s' can't be thrown in initializer",
15346              lang_printable_name (TREE_VALUE (throws), 0));
15347         else 
15348           {
15349             parse_error_context 
15350               (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'", 
15351                lang_printable_name (TREE_VALUE (throws), 0),
15352                (DECL_INIT_P (current_function_decl) ?
15353                 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15354                 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15355           }
15356       }
15357 }
15358
15359 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15360    try-catch blocks, OR is listed in the `throws' clause of the
15361    current method.  */
15362
15363 static int
15364 check_thrown_exceptions_do (exception)
15365      tree exception;
15366 {
15367   tree list = currently_caught_type_list;
15368   resolve_and_layout (exception, NULL_TREE);
15369   /* First, all the nested try-catch-finally at that stage. The
15370      last element contains `throws' clause exceptions, if any. */
15371   if (IS_UNCHECKED_EXCEPTION_P (exception))
15372     return 1;
15373   while (list)
15374     {
15375       tree caught;
15376       for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15377         if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15378           return 1;
15379       list = TREE_CHAIN (list);
15380     }
15381   return 0;
15382 }
15383
15384 static void
15385 purge_unchecked_exceptions (mdecl)
15386      tree mdecl;
15387 {
15388   tree throws = DECL_FUNCTION_THROWS (mdecl);
15389   tree new = NULL_TREE;
15390
15391   while (throws)
15392     {
15393       tree next = TREE_CHAIN (throws);
15394       if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15395         {
15396           TREE_CHAIN (throws) = new;
15397           new = throws;
15398         }
15399       throws = next;
15400     }
15401   /* List is inverted here, but it doesn't matter */
15402   DECL_FUNCTION_THROWS (mdecl) = new;
15403 }
15404
15405 /* 15.24 Conditional Operator ?: */
15406
15407 static tree
15408 patch_conditional_expr (node, wfl_cond, wfl_op1)
15409      tree node, wfl_cond, wfl_op1;
15410 {
15411   tree cond = TREE_OPERAND (node, 0);
15412   tree op1 = TREE_OPERAND (node, 1);
15413   tree op2 = TREE_OPERAND (node, 2);
15414   tree resulting_type = NULL_TREE;
15415   tree t1, t2, patched;
15416   int error_found = 0;
15417
15418   /* Operands of ?: might be StringBuffers crafted as a result of a
15419      string concatenation. Obtain a descent operand here.  */
15420   if ((patched = patch_string (op1)))
15421     TREE_OPERAND (node, 1) = op1 = patched;
15422   if ((patched = patch_string (op2)))
15423     TREE_OPERAND (node, 2) = op2 = patched;
15424
15425   t1 = TREE_TYPE (op1);
15426   t2 = TREE_TYPE (op2);
15427
15428   /* The first expression must be a boolean */
15429   if (TREE_TYPE (cond) != boolean_type_node)
15430     {
15431       SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15432       parse_error_context (wfl_operator,
15433                "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
15434                            lang_printable_name (TREE_TYPE (cond), 0));
15435       error_found = 1;
15436     }
15437
15438   /* Second and third can be numeric, boolean (i.e. primitive),
15439      references or null. Anything else results in an error */
15440   if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15441         || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node) 
15442             && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15443         || (t1 == boolean_type_node && t2 == boolean_type_node)))
15444     error_found = 1;
15445
15446   /* Determine the type of the conditional expression. Same types are
15447      easy to deal with */
15448   else if (t1 == t2)
15449     resulting_type = t1;
15450
15451   /* There are different rules for numeric types */
15452   else if (JNUMERIC_TYPE_P (t1))
15453     {
15454       /* if byte/short found, the resulting type is short */
15455       if ((t1 == byte_type_node && t2 == short_type_node)
15456           || (t1 == short_type_node && t2 == byte_type_node))
15457         resulting_type = short_type_node;
15458
15459       /* If t1 is a constant int and t2 is of type byte, short or char
15460          and t1's value fits in t2, then the resulting type is t2 */
15461       else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15462           && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15463         resulting_type = t2;
15464
15465       /* If t2 is a constant int and t1 is of type byte, short or char
15466          and t2's value fits in t1, then the resulting type is t1 */
15467       else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15468           && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15469         resulting_type = t1;
15470
15471       /* Otherwise, binary numeric promotion is applied and the
15472          resulting type is the promoted type of operand 1 and 2 */
15473       else 
15474         resulting_type = binary_numeric_promotion (t1, t2, 
15475                                                    &TREE_OPERAND (node, 1), 
15476                                                    &TREE_OPERAND (node, 2));
15477     }
15478
15479   /* Cases of a reference and a null type */
15480   else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15481     resulting_type = t1;
15482
15483   else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15484     resulting_type = t2;
15485
15486   /* Last case: different reference types. If a type can be converted
15487      into the other one by assignment conversion, the latter
15488      determines the type of the expression */
15489   else if ((resulting_type = try_reference_assignconv (t1, op2)))
15490     resulting_type = promote_type (t1);
15491
15492   else if ((resulting_type = try_reference_assignconv (t2, op1)))
15493     resulting_type = promote_type (t2);
15494
15495   /* If we don't have any resulting type, we're in trouble */
15496   if (!resulting_type)
15497     {
15498       char *t = xstrdup (lang_printable_name (t1, 0));
15499       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15500       parse_error_context (wfl_operator,
15501                  "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15502                            t, lang_printable_name (t2, 0));
15503       free (t);
15504       error_found = 1;
15505     }
15506
15507   if (error_found)
15508     {
15509       TREE_TYPE (node) = error_mark_node;
15510       return error_mark_node;
15511     }
15512
15513   TREE_TYPE (node) = resulting_type;
15514   TREE_SET_CODE (node, COND_EXPR);
15515   CAN_COMPLETE_NORMALLY (node) = 1;
15516   return node;
15517 }
15518
15519 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
15520
15521 static tree
15522 maybe_build_class_init_for_field (decl, expr)
15523     tree decl, expr;
15524 {
15525   tree clas = DECL_CONTEXT (decl);
15526   if (flag_emit_class_files || flag_emit_xref)
15527     return expr;
15528
15529   if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
15530       && FIELD_FINAL (decl))
15531     {
15532       tree init = DECL_INITIAL (decl);
15533       if (init != NULL_TREE)
15534         init = fold_constant_for_init (init, decl);
15535       if (init != NULL_TREE && CONSTANT_VALUE_P (init))
15536         return expr;
15537     }
15538
15539   return build_class_init (clas, expr);
15540 }
15541
15542 /* Try to constant fold NODE.
15543    If NODE is not a constant expression, return NULL_EXPR.
15544    CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15545
15546 static tree
15547 fold_constant_for_init (node, context)
15548      tree node;
15549      tree context;
15550 {
15551   tree op0, op1, val;
15552   enum tree_code code = TREE_CODE (node);
15553
15554   switch (code)
15555     {
15556     case STRING_CST:
15557     case INTEGER_CST:
15558     case REAL_CST:
15559       return node;
15560
15561     case PLUS_EXPR:
15562     case MINUS_EXPR:
15563     case MULT_EXPR:
15564     case TRUNC_MOD_EXPR:
15565     case RDIV_EXPR:
15566     case LSHIFT_EXPR:
15567     case RSHIFT_EXPR:
15568     case URSHIFT_EXPR:
15569     case BIT_AND_EXPR:
15570     case BIT_XOR_EXPR:
15571     case BIT_IOR_EXPR:
15572     case TRUTH_ANDIF_EXPR:
15573     case TRUTH_ORIF_EXPR:
15574     case EQ_EXPR: 
15575     case NE_EXPR:
15576     case GT_EXPR:
15577     case GE_EXPR:
15578     case LT_EXPR:
15579     case LE_EXPR:
15580       op0 = TREE_OPERAND (node, 0);
15581       op1 = TREE_OPERAND (node, 1);
15582       val = fold_constant_for_init (op0, context);
15583       if (val == NULL_TREE || ! TREE_CONSTANT (val))
15584         return NULL_TREE;
15585       TREE_OPERAND (node, 0) = val;
15586       val = fold_constant_for_init (op1, context);
15587       if (val == NULL_TREE || ! TREE_CONSTANT (val))
15588         return NULL_TREE;
15589       TREE_OPERAND (node, 1) = val;
15590       return patch_binop (node, op0, op1);
15591
15592     case UNARY_PLUS_EXPR:
15593     case NEGATE_EXPR:
15594     case TRUTH_NOT_EXPR:
15595     case BIT_NOT_EXPR:
15596     case CONVERT_EXPR:
15597       op0 = TREE_OPERAND (node, 0);
15598       val = fold_constant_for_init (op0, context);
15599       if (val == NULL_TREE || ! TREE_CONSTANT (val))
15600         return NULL_TREE;
15601       TREE_OPERAND (node, 0) = val;
15602       return patch_unaryop (node, op0);
15603       break;
15604
15605     case COND_EXPR:
15606       val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15607       if (val == NULL_TREE || ! TREE_CONSTANT (val))
15608         return NULL_TREE;
15609       TREE_OPERAND (node, 0) = val;
15610       val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15611       if (val == NULL_TREE || ! TREE_CONSTANT (val))
15612         return NULL_TREE;
15613       TREE_OPERAND (node, 1) = val;
15614       val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15615       if (val == NULL_TREE || ! TREE_CONSTANT (val))
15616         return NULL_TREE;
15617       TREE_OPERAND (node, 2) = val;
15618       return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15619         : TREE_OPERAND (node, 2);
15620
15621     case VAR_DECL:
15622     case FIELD_DECL:
15623       if (! FIELD_FINAL (node)
15624           || DECL_INITIAL (node) == NULL_TREE)
15625         return NULL_TREE;
15626       val = DECL_INITIAL (node);
15627       /* Guard against infinite recursion. */
15628       DECL_INITIAL (node) = NULL_TREE;
15629       val = fold_constant_for_init (val, node);
15630       DECL_INITIAL (node) = val;
15631       if (!val && CLASS_FINAL_VARIABLE_P (node))
15632         DECL_FIELD_FINAL_IUD (node) = 0;
15633       return val;
15634
15635     case EXPR_WITH_FILE_LOCATION:
15636       /* Compare java_complete_tree and resolve_expression_name. */
15637       if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15638           || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15639         {
15640           tree name = EXPR_WFL_NODE (node);
15641           tree decl;
15642           if (PRIMARY_P (node))
15643             return NULL_TREE;
15644           else if (! QUALIFIED_P (name))
15645             {
15646               decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
15647               if (decl == NULL_TREE 
15648                   || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
15649                 return NULL_TREE;
15650               return fold_constant_for_init (decl, decl);
15651             }
15652           else
15653             {
15654               /* Wait until the USE_COMPONENT_REF re-write.  FIXME. */
15655               qualify_ambiguous_name (node);
15656               if (resolve_field_access (node, &decl, NULL)
15657                   && decl != NULL_TREE)
15658                 return fold_constant_for_init (decl, decl);
15659               return NULL_TREE;
15660             }
15661         }
15662       else
15663         {
15664           op0 = TREE_OPERAND (node, 0);
15665           val = fold_constant_for_init (op0, context);
15666           if (val == NULL_TREE || ! TREE_CONSTANT (val))
15667             return NULL_TREE;
15668           TREE_OPERAND (node, 0) = val;
15669           return val;
15670         }
15671
15672 #ifdef USE_COMPONENT_REF
15673     case IDENTIFIER:
15674     case COMPONENT_REF:
15675       ?;
15676 #endif
15677
15678     default:
15679       return NULL_TREE;
15680     }
15681 }
15682
15683 #ifdef USE_COMPONENT_REF
15684 /* Context is 'T' for TypeName, 'P' for PackageName,
15685    'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15686
15687 tree
15688 resolve_simple_name (name, context)
15689      tree name;
15690      int context;
15691 {
15692 }
15693
15694 tree
15695 resolve_qualified_name (name, context)
15696      tree name;
15697      int context;
15698 {
15699 }
15700 #endif
15701
15702 /* Mark P, which is really a `struct parser_ctxt **' for GC.  */
15703
15704 static void
15705 mark_parser_ctxt (p)
15706      void *p;
15707 {
15708   struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15709   int i;
15710
15711   if (!pc)
15712     return;
15713
15714 #ifndef JC1_LITE
15715   for (i = 0; i < 11; ++i)
15716     ggc_mark_tree (pc->modifier_ctx[i]);
15717   ggc_mark_tree (pc->class_type);
15718   ggc_mark_tree (pc->function_decl);
15719   ggc_mark_tree (pc->package);
15720   ggc_mark_tree (pc->class_list);
15721   ggc_mark_tree (pc->current_parsed_class);
15722   ggc_mark_tree (pc->current_parsed_class_un);
15723   ggc_mark_tree (pc->non_static_initialized);
15724   ggc_mark_tree (pc->static_initialized);
15725   ggc_mark_tree (pc->instance_initializers);
15726   ggc_mark_tree (pc->import_list);
15727   ggc_mark_tree (pc->import_demand_list);
15728   ggc_mark_tree (pc->current_loop);
15729   ggc_mark_tree (pc->current_labeled_block);
15730 #endif /* JC1_LITE */
15731
15732   if (pc->next)
15733     mark_parser_ctxt (&pc->next);
15734 }
15735
15736 void
15737 init_src_parse ()
15738 {
15739   /* Register roots with the garbage collector.  */
15740   ggc_add_tree_root (src_parse_roots, sizeof (src_parse_roots) / sizeof(tree));
15741 }