OSDN Git Service

* attribs.c (init_attributes, decl_attributes): Use ARRAY_SIZE in
[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, 2002 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, tree));
103 static void find_in_imports PARAMS ((tree, tree));
104 static void check_inner_class_access PARAMS ((tree, tree, tree));
105 static int check_pkg_class_access PARAMS ((tree, tree, bool));
106 static void register_package PARAMS ((tree));
107 static tree resolve_package PARAMS ((tree, tree *));
108 static tree lookup_package_type PARAMS ((const char *, int));
109 static tree resolve_class PARAMS ((tree, tree, tree, tree));
110 static void declare_local_variables PARAMS ((int, tree, tree));
111 static void dump_java_tree PARAMS ((enum tree_dump_index, tree));
112 static void source_start_java_method PARAMS ((tree));
113 static void source_end_java_method PARAMS ((void));
114 static tree find_name_in_single_imports PARAMS ((tree));
115 static void check_abstract_method_header PARAMS ((tree));
116 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
117 static tree resolve_expression_name PARAMS ((tree, tree *));
118 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
119 static int check_class_interface_creation PARAMS ((int, int, tree, 
120                                                   tree, tree, tree));
121 static tree patch_method_invocation PARAMS ((tree, tree, tree, int,
122                                             int *, tree *));
123 static int breakdown_qualified PARAMS ((tree *, tree *, tree));
124 static int in_same_package PARAMS ((tree, tree));
125 static tree resolve_and_layout PARAMS ((tree, tree));
126 static tree qualify_and_find PARAMS ((tree, tree, tree));
127 static tree resolve_no_layout PARAMS ((tree, tree));
128 static int invocation_mode PARAMS ((tree, int));
129 static tree find_applicable_accessible_methods_list PARAMS ((int, tree, 
130                                                             tree, tree));
131 static void search_applicable_methods_list PARAMS ((int, tree, tree, tree, 
132                                                    tree *, tree *));
133 static tree find_most_specific_methods_list PARAMS ((tree));
134 static int argument_types_convertible PARAMS ((tree, tree));
135 static tree patch_invoke PARAMS ((tree, tree, tree));
136 static int maybe_use_access_method PARAMS ((int, tree *, tree *));
137 static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
138 static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
139 static tree check_inner_circular_reference PARAMS ((tree, tree));
140 static tree check_circular_reference PARAMS ((tree));
141 static tree obtain_incomplete_type PARAMS ((tree));
142 static tree java_complete_lhs PARAMS ((tree));
143 static tree java_complete_tree PARAMS ((tree));
144 static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
145 static int analyze_clinit_body PARAMS ((tree, tree));
146 static int maybe_yank_clinit PARAMS ((tree));
147 static void start_complete_expand_method PARAMS ((tree));
148 static void java_complete_expand_method PARAMS ((tree));
149 static void java_expand_method_bodies PARAMS ((tree));
150 static int  unresolved_type_p PARAMS ((tree, tree *));
151 static void create_jdep_list PARAMS ((struct parser_ctxt *));
152 static tree build_expr_block PARAMS ((tree, tree));
153 static tree enter_block PARAMS ((void));
154 static tree exit_block PARAMS ((void));
155 static tree lookup_name_in_blocks PARAMS ((tree));
156 static void maybe_absorb_scoping_blocks PARAMS ((void));
157 static tree build_method_invocation PARAMS ((tree, tree));
158 static tree build_new_invocation PARAMS ((tree, tree));
159 static tree build_assignment PARAMS ((int, int, tree, tree));
160 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
161 static tree patch_assignment PARAMS ((tree, tree));
162 static tree patch_binop PARAMS ((tree, tree, tree));
163 static tree build_unaryop PARAMS ((int, int, tree));
164 static tree build_incdec PARAMS ((int, int, tree, int));
165 static tree patch_unaryop PARAMS ((tree, tree));
166 static tree build_cast PARAMS ((int, tree, tree));
167 static tree build_null_of_type PARAMS ((tree));
168 static tree patch_cast PARAMS ((tree, tree));
169 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
170 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
171 static int valid_cast_to_p PARAMS ((tree, tree));
172 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
173 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
174 static tree try_reference_assignconv PARAMS ((tree, tree));
175 static tree build_unresolved_array_type PARAMS ((tree));
176 static int build_type_name_from_array_name PARAMS ((tree, tree *));
177 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
178 static tree build_array_ref PARAMS ((int, tree, tree));
179 static tree patch_array_ref PARAMS ((tree));
180 static tree make_qualified_name PARAMS ((tree, tree, int));
181 static tree merge_qualified_name PARAMS ((tree, tree));
182 static tree make_qualified_primary PARAMS ((tree, tree, int));
183 static int resolve_qualified_expression_name PARAMS ((tree, tree *, 
184                                                      tree *, tree *));
185 static void qualify_ambiguous_name PARAMS ((tree));
186 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
187 static tree build_newarray_node PARAMS ((tree, tree, int));
188 static tree patch_newarray PARAMS ((tree));
189 static tree resolve_type_during_patch PARAMS ((tree));
190 static tree build_this PARAMS ((int));
191 static tree build_wfl_wrap PARAMS ((tree, int));
192 static tree build_return PARAMS ((int, tree));
193 static tree patch_return PARAMS ((tree));
194 static tree maybe_access_field PARAMS ((tree, tree, tree));
195 static int complete_function_arguments PARAMS ((tree));
196 static int check_for_static_method_reference PARAMS ((tree, tree, tree, 
197                                                       tree, tree));
198 static int not_accessible_p PARAMS ((tree, tree, tree, int));
199 static void check_deprecation PARAMS ((tree, tree));
200 static int class_in_current_package PARAMS ((tree));
201 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
202 static tree patch_if_else_statement PARAMS ((tree));
203 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
204 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
205 static tree patch_exit_expr PARAMS ((tree));
206 static tree build_labeled_block PARAMS ((int, tree));
207 static tree finish_labeled_statement PARAMS ((tree, tree));
208 static tree build_bc_statement PARAMS ((int, int, tree));
209 static tree patch_bc_statement PARAMS ((tree));
210 static tree patch_loop_statement PARAMS ((tree));
211 static tree build_new_loop PARAMS ((tree));
212 static tree build_loop_body PARAMS ((int, tree, int));
213 static tree finish_loop_body PARAMS ((int, tree, tree, int));
214 static tree build_debugable_stmt PARAMS ((int, tree));
215 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
216 static tree patch_switch_statement PARAMS ((tree));
217 static tree string_constant_concatenation PARAMS ((tree, tree));
218 static tree build_string_concatenation PARAMS ((tree, tree));
219 static tree patch_string_cst PARAMS ((tree));
220 static tree patch_string PARAMS ((tree));
221 static tree encapsulate_with_try_catch PARAMS ((int, tree, tree, 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 bool ctors_unchecked_throws_clause_p PARAMS ((tree));
231 static void check_throws_clauses PARAMS ((tree, tree, tree));
232 static void finish_method_declaration PARAMS ((tree));
233 static tree build_super_invocation PARAMS ((tree));
234 static int verify_constructor_circularity PARAMS ((tree, tree));
235 static char *constructor_circularity_msg PARAMS ((tree, tree));
236 static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
237                                                           int, int));
238 static const char *get_printable_method_name PARAMS ((tree));
239 static tree patch_conditional_expr PARAMS ((tree, tree, tree));
240 static tree generate_finit PARAMS ((tree));
241 static tree generate_instinit PARAMS ((tree));
242 static tree build_instinit_invocation PARAMS ((tree));
243 static void fix_constructors PARAMS ((tree));
244 static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
245                                                             tree, int *));
246 static void craft_constructor PARAMS ((tree, tree));
247 static int verify_constructor_super PARAMS ((tree));
248 static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
249 static void start_artificial_method_body PARAMS ((tree));
250 static void end_artificial_method_body PARAMS ((tree));
251 static int check_method_redefinition PARAMS ((tree, tree));
252 static int check_method_types_complete PARAMS ((tree));
253 static void java_check_regular_methods PARAMS ((tree));
254 static void java_check_abstract_methods PARAMS ((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 resolve_inner_class PARAMS ((struct hash_table *, tree, tree *,
302                                          tree *, tree));
303 static tree find_as_inner_class PARAMS ((tree, tree, tree));
304 static tree find_as_inner_class_do PARAMS ((tree, tree));
305 static int check_inner_class_redefinition PARAMS ((tree, tree));
306
307 static tree build_thisn_assign PARAMS ((void));
308 static tree build_current_thisn PARAMS ((tree));
309 static tree build_access_to_thisn PARAMS ((tree, tree, int));
310 static tree maybe_build_thisn_access_method PARAMS ((tree));
311
312 static tree build_outer_field_access PARAMS ((tree, tree));
313 static tree build_outer_field_access_methods PARAMS ((tree));
314 static tree build_outer_field_access_expr PARAMS ((int, tree, tree, 
315                                                   tree, tree));
316 static tree build_outer_method_access_method PARAMS ((tree));
317 static tree build_new_access_id PARAMS ((void));
318 static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
319                                                     tree, tree));
320
321 static int outer_field_access_p PARAMS ((tree, tree));
322 static int outer_field_expanded_access_p PARAMS ((tree, tree *, 
323                                                  tree *, tree *));
324 static tree outer_field_access_fix PARAMS ((tree, tree, tree));
325 static tree build_incomplete_class_ref PARAMS ((int, tree));
326 static tree patch_incomplete_class_ref PARAMS ((tree));
327 static tree create_anonymous_class PARAMS ((int, tree));
328 static void patch_anonymous_class PARAMS ((tree, tree, tree));
329 static void add_inner_class_fields PARAMS ((tree, tree));
330
331 static tree build_dot_class_method PARAMS ((tree));
332 static tree build_dot_class_method_invocation PARAMS ((tree));
333 static void create_new_parser_context PARAMS ((int));
334 static void mark_parser_ctxt PARAMS ((void *));
335 static tree maybe_build_class_init_for_field PARAMS ((tree, tree));
336
337 static bool attach_init_test_initialization_flags PARAMS ((struct hash_entry *,
338                                                           PTR));
339 static bool emit_test_initialization PARAMS ((struct hash_entry *, PTR));
340
341 /* Number of error found so far. */
342 int java_error_count; 
343 /* Number of warning found so far. */
344 int java_warning_count;
345 /* Tell when not to fold, when doing xrefs */
346 int do_not_fold;
347 /* Cyclic inheritance report, as it can be set by layout_class */
348 const char *cyclic_inheritance_report;
349  
350 /* The current parser context */
351 struct parser_ctxt *ctxp;
352
353 /* List of things that were analyzed for which code will be generated */
354 struct parser_ctxt *ctxp_for_generation = NULL;
355
356 /* binop_lookup maps token to tree_code. It is used where binary
357    operations are involved and required by the parser. RDIV_EXPR
358    covers both integral/floating point division. The code is changed
359    once the type of both operator is worked out.  */
360
361 static const enum tree_code binop_lookup[19] = 
362   { 
363     PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
364     LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR, 
365     BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
366     TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
367     EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
368    };
369 #define BINOP_LOOKUP(VALUE)                                             \
370   binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
371
372 /* This is the end index for binary operators that can also be used
373    in compound assignements. */
374 #define BINOP_COMPOUND_CANDIDATES 11
375
376 /* The "$L" identifier we use to create labels.  */
377 static tree label_id = NULL_TREE;
378
379 /* The "StringBuffer" identifier used for the String `+' operator. */
380 static tree wfl_string_buffer = NULL_TREE; 
381
382 /* The "append" identifier used for String `+' operator.  */
383 static tree wfl_append = NULL_TREE;
384
385 /* The "toString" identifier used for String `+' operator. */
386 static tree wfl_to_string = NULL_TREE;
387
388 /* The "java.lang" import qualified name.  */
389 static tree java_lang_id = NULL_TREE;
390
391 /* The generated `inst$' identifier used for generated enclosing
392    instance/field access functions.  */
393 static tree inst_id = NULL_TREE;
394
395 /* The "java.lang.Cloneable" qualified name.  */
396 static tree java_lang_cloneable = NULL_TREE;
397
398 /* The "java.io.Serializable" qualified name.  */
399 static tree java_io_serializable = NULL_TREE; 
400
401 /* Context and flag for static blocks */
402 static tree current_static_block = NULL_TREE;
403
404 /* The generated `write_parm_value$' identifier.  */
405 static tree wpv_id;
406
407 /* The list of all packages we've seen so far */
408 static tree package_list = NULL_TREE;
409  
410 /* Hold THIS for the scope of the current method decl.  */
411 static tree current_this;
412
413 /* Hold a list of catch clauses list. The first element of this list is
414    the list of the catch clauses of the currently analysed try block. */
415 static tree currently_caught_type_list;
416
417 /* This holds a linked list of all the case labels for the current
418    switch statement.  It is only used when checking to see if there
419    are duplicate labels.  FIXME: probably this should just be attached
420    to the switch itself; then it could be referenced via
421    `ctxp->current_loop'.  */
422 static tree case_label_list; 
423
424 static tree src_parse_roots[1];
425
426 /* All classes seen from source code */
427 #define gclass_list src_parse_roots[0]
428
429 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
430    line and point it out.  */
431 /* Should point out the one that don't fit. ASCII/unicode, going
432    backward. FIXME */
433
434 #define check_modifiers(__message, __value, __mask) do {        \
435   if ((__value) & ~(__mask))                                    \
436     {                                                           \
437       int i, remainder = (__value) & ~(__mask);                 \
438       for (i = 0; i <= 10; i++)                                 \
439         if ((1 << i) & remainder)                               \
440           parse_error_context (ctxp->modifier_ctx [i], (__message), \
441                                java_accstring_lookup (1 << i)); \
442     }                                                           \
443 } while (0)
444
445 %}
446
447 %union {
448   tree node;
449   int sub_token;
450   struct {
451     int token;
452     int location;
453   } operator;
454   int value;
455 }
456
457 %{
458 #include "lex.c"
459 %}
460
461 %pure_parser
462
463 /* Things defined here have to match the order of what's in the
464    binop_lookup table.  */
465
466 %token   PLUS_TK         MINUS_TK        MULT_TK         DIV_TK    REM_TK
467 %token   LS_TK           SRS_TK          ZRS_TK
468 %token   AND_TK          XOR_TK          OR_TK
469 %token   BOOL_AND_TK BOOL_OR_TK 
470 %token   EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
471
472 /* This maps to the same binop_lookup entry than the token above */
473
474 %token   PLUS_ASSIGN_TK  MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
475 %token   REM_ASSIGN_TK   
476 %token   LS_ASSIGN_TK    SRS_ASSIGN_TK   ZRS_ASSIGN_TK
477 %token   AND_ASSIGN_TK   XOR_ASSIGN_TK   OR_ASSIGN_TK
478
479
480 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
481
482 %token   PUBLIC_TK       PRIVATE_TK         PROTECTED_TK
483 %token   STATIC_TK       FINAL_TK           SYNCHRONIZED_TK
484 %token   VOLATILE_TK     TRANSIENT_TK       NATIVE_TK
485 %token   PAD_TK          ABSTRACT_TK        STRICT_TK
486 %token   MODIFIER_TK
487
488 /* Keep those two in order, too */
489 %token   DECR_TK INCR_TK
490
491 /* From now one, things can be in any order */
492
493 %token   DEFAULT_TK      IF_TK              THROW_TK
494 %token   BOOLEAN_TK      DO_TK              IMPLEMENTS_TK
495 %token   THROWS_TK       BREAK_TK           IMPORT_TK       
496 %token   ELSE_TK         INSTANCEOF_TK      RETURN_TK
497 %token   VOID_TK         CATCH_TK           INTERFACE_TK
498 %token   CASE_TK         EXTENDS_TK         FINALLY_TK
499 %token   SUPER_TK        WHILE_TK           CLASS_TK
500 %token   SWITCH_TK       CONST_TK           TRY_TK
501 %token   FOR_TK          NEW_TK             CONTINUE_TK
502 %token   GOTO_TK         PACKAGE_TK         THIS_TK
503
504 %token   BYTE_TK         SHORT_TK           INT_TK            LONG_TK
505 %token   CHAR_TK         INTEGRAL_TK
506
507 %token   FLOAT_TK        DOUBLE_TK          FP_TK
508
509 %token   ID_TK
510
511 %token   REL_QM_TK         REL_CL_TK NOT_TK  NEG_TK
512
513 %token   ASSIGN_ANY_TK   ASSIGN_TK
514 %token   OP_TK  CP_TK  OCB_TK  CCB_TK  OSB_TK  CSB_TK  SC_TK  C_TK DOT_TK
515
516 %token   STRING_LIT_TK   CHAR_LIT_TK        INT_LIT_TK        FP_LIT_TK
517 %token   TRUE_TK         FALSE_TK           BOOL_LIT_TK       NULL_TK
518
519 %type    <value>        modifiers MODIFIER_TK final synchronized
520
521 %type    <node>         super ID_TK identifier
522 %type    <node>         name simple_name qualified_name
523 %type    <node>         type_declaration compilation_unit
524                         field_declaration method_declaration extends_interfaces
525                         interfaces interface_type_list
526                         class_member_declaration
527                         import_declarations package_declaration 
528                         type_declarations interface_body
529                         interface_member_declaration constant_declaration
530                         interface_member_declarations interface_type
531                         abstract_method_declaration interface_type_list
532 %type    <node>         class_body_declaration class_member_declaration
533                         static_initializer constructor_declaration block
534 %type    <node>         class_body_declarations constructor_header
535 %type    <node>         class_or_interface_type class_type class_type_list
536                         constructor_declarator explicit_constructor_invocation
537 %type    <node>         dim_expr dim_exprs this_or_super throws
538
539 %type    <node>         variable_declarator_id variable_declarator
540                         variable_declarators variable_initializer
541                         variable_initializers constructor_body
542                         array_initializer
543
544 %type    <node>         class_body block_end constructor_block_end
545 %type    <node>         statement statement_without_trailing_substatement
546                         labeled_statement if_then_statement label_decl
547                         if_then_else_statement while_statement for_statement
548                         statement_nsi labeled_statement_nsi do_statement
549                         if_then_else_statement_nsi while_statement_nsi
550                         for_statement_nsi statement_expression_list for_init
551                         for_update statement_expression expression_statement
552                         primary_no_new_array expression primary
553                         array_creation_expression array_type
554                         class_instance_creation_expression field_access
555                         method_invocation array_access something_dot_new
556                         argument_list postfix_expression while_expression 
557                         post_increment_expression post_decrement_expression
558                         unary_expression_not_plus_minus unary_expression
559                         pre_increment_expression pre_decrement_expression
560                         unary_expression_not_plus_minus cast_expression
561                         multiplicative_expression additive_expression
562                         shift_expression relational_expression 
563                         equality_expression and_expression 
564                         exclusive_or_expression inclusive_or_expression
565                         conditional_and_expression conditional_or_expression
566                         conditional_expression assignment_expression
567                         left_hand_side assignment for_header for_begin
568                         constant_expression do_statement_begin empty_statement
569                         switch_statement synchronized_statement throw_statement
570                         try_statement switch_expression switch_block
571                         catches catch_clause catch_clause_parameter finally
572                         anonymous_class_creation trap_overflow_corner_case
573 %type    <node>         return_statement break_statement continue_statement
574
575 %type    <operator>     ASSIGN_TK      MULT_ASSIGN_TK  DIV_ASSIGN_TK  
576 %type    <operator>     REM_ASSIGN_TK  PLUS_ASSIGN_TK  MINUS_ASSIGN_TK
577 %type    <operator>     LS_ASSIGN_TK   SRS_ASSIGN_TK   ZRS_ASSIGN_TK
578 %type    <operator>     AND_ASSIGN_TK  XOR_ASSIGN_TK   OR_ASSIGN_TK
579 %type    <operator>     ASSIGN_ANY_TK  assignment_operator
580 %token   <operator>     EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK 
581 %token   <operator>     BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
582 %token   <operator>     DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
583 %token   <operator>     NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
584 %token   <operator>     OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
585 %type    <operator>     THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK 
586 %type    <operator>     CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
587 %type    <operator>     NEW_TK
588
589 %type    <node>         method_body 
590         
591 %type    <node>         literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
592                         STRING_LIT_TK NULL_TK VOID_TK
593
594 %type    <node>         IF_TK WHILE_TK FOR_TK
595
596 %type    <node>         formal_parameter_list formal_parameter
597                         method_declarator method_header
598
599 %type    <node>         primitive_type reference_type type 
600                         BOOLEAN_TK INTEGRAL_TK FP_TK
601
602 /* Added or modified JDK 1.1 rule types  */
603 %type    <node>         type_literals
604
605 %%
606 /* 19.2 Production from 2.3: The Syntactic Grammar  */
607 goal:
608                 {
609                   /* Register static variables with the garbage
610                      collector.  */
611                   ggc_add_tree_root (&label_id, 1);
612                   ggc_add_tree_root (&wfl_string_buffer, 1);
613                   ggc_add_tree_root (&wfl_append, 1);
614                   ggc_add_tree_root (&wfl_to_string, 1);
615                   ggc_add_tree_root (&java_lang_id, 1);
616                   ggc_add_tree_root (&inst_id, 1);
617                   ggc_add_tree_root (&java_lang_cloneable, 1);
618                   ggc_add_tree_root (&java_io_serializable, 1);
619                   ggc_add_tree_root (&current_static_block, 1);
620                   ggc_add_tree_root (&wpv_id, 1);
621                   ggc_add_tree_root (&package_list, 1);
622                   ggc_add_tree_root (&current_this, 1);
623                   ggc_add_tree_root (&currently_caught_type_list, 1);
624                   ggc_add_tree_root (&case_label_list, 1);
625                   ggc_add_root (&ctxp, 1, 
626                                 sizeof (struct parser_ctxt *),
627                                 mark_parser_ctxt);
628                   ggc_add_root (&ctxp_for_generation, 1, 
629                                 sizeof (struct parser_ctxt *),
630                                 mark_parser_ctxt);
631                 }
632         compilation_unit
633                 {}
634 ;
635
636 /* 19.3 Productions from 3: Lexical structure  */
637 literal:
638         INT_LIT_TK
639 |       FP_LIT_TK
640 |       BOOL_LIT_TK
641 |       CHAR_LIT_TK
642 |       STRING_LIT_TK
643 |       NULL_TK
644 ;
645
646 /* 19.4 Productions from 4: Types, Values and Variables  */
647 type:
648         primitive_type
649 |       reference_type
650 ;
651
652 primitive_type:
653         INTEGRAL_TK
654 |       FP_TK
655 |       BOOLEAN_TK
656 ;
657
658 reference_type:
659         class_or_interface_type
660 |       array_type
661 ;
662
663 class_or_interface_type:
664         name
665 ;
666
667 class_type:
668         class_or_interface_type /* Default rule */
669 ;
670
671 interface_type:
672          class_or_interface_type
673 ;
674
675 array_type:
676         primitive_type dims
677                 { 
678                   int osb = pop_current_osb (ctxp);
679                   tree t = build_java_array_type (($1), -1);
680                   while (--osb)
681                     t = build_unresolved_array_type (t);
682                   $$ = t;
683                 }
684 |       name dims
685                 { 
686                   int osb = pop_current_osb (ctxp);
687                   tree t = $1;
688                   while (osb--)
689                     t = build_unresolved_array_type (t);
690                   $$ = t;
691                 }
692 ;
693
694 /* 19.5 Productions from 6: Names  */
695 name:
696         simple_name             /* Default rule */
697 |       qualified_name          /* Default rule */
698 ;
699
700 simple_name:
701         identifier              /* Default rule */
702 ;
703
704 qualified_name:
705         name DOT_TK identifier
706                 { $$ = make_qualified_name ($1, $3, $2.location); }
707 ;
708
709 identifier:
710         ID_TK
711 ;
712
713 /* 19.6: Production from 7: Packages  */
714 compilation_unit:
715                 {$$ = NULL;}
716 |       package_declaration
717 |       import_declarations
718 |       type_declarations
719 |       package_declaration import_declarations
720 |       package_declaration type_declarations
721 |       import_declarations type_declarations
722 |       package_declaration import_declarations type_declarations
723 ;
724
725 import_declarations:
726         import_declaration
727                 {
728                   $$ = NULL;
729                 }
730 |       import_declarations import_declaration
731                 {
732                   $$ = NULL;
733                 }
734 ;
735
736 type_declarations:
737         type_declaration
738 |       type_declarations type_declaration
739 ;
740
741 package_declaration:
742         PACKAGE_TK name SC_TK
743                 { 
744                   ctxp->package = EXPR_WFL_NODE ($2);
745                   register_package (ctxp->package);
746                 }
747 |       PACKAGE_TK error
748                 {yyerror ("Missing name"); RECOVER;}
749 |       PACKAGE_TK name error
750                 {yyerror ("';' expected"); RECOVER;}
751 ;
752
753 import_declaration:
754         single_type_import_declaration
755 |       type_import_on_demand_declaration
756 ;
757
758 single_type_import_declaration:
759         IMPORT_TK name SC_TK
760                 {
761                   tree name = EXPR_WFL_NODE ($2), last_name;
762                   int   i = IDENTIFIER_LENGTH (name)-1;
763                   const char *last = &IDENTIFIER_POINTER (name)[i];
764                   while (last != IDENTIFIER_POINTER (name))
765                     {
766                       if (last [0] == '.')
767                         break;
768                       last--;
769                     }
770                   last_name = get_identifier (++last);
771                   if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
772                     {
773                       tree err = find_name_in_single_imports (last_name);
774                       if (err && err != name)
775                         parse_error_context
776                           ($2, "Ambiguous class: `%s' and `%s'",
777                            IDENTIFIER_POINTER (name), 
778                            IDENTIFIER_POINTER (err));
779                       else
780                         REGISTER_IMPORT ($2, last_name);
781                     }
782                   else
783                     REGISTER_IMPORT ($2, last_name);
784                 }
785 |       IMPORT_TK error
786                 {yyerror ("Missing name"); RECOVER;}
787 |       IMPORT_TK name error
788                 {yyerror ("';' expected"); RECOVER;}
789 ;
790
791 type_import_on_demand_declaration:
792         IMPORT_TK name DOT_TK MULT_TK SC_TK
793                 {
794                   tree name = EXPR_WFL_NODE ($2);
795                   tree it;
796                   /* Search for duplicates. */
797                   for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
798                     if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
799                       break;
800                   /* Don't import the same thing more than once, just ignore
801                      duplicates (7.5.2) */
802                   if (! it)
803                     {
804                       read_import_dir ($2);
805                       ctxp->import_demand_list = 
806                         chainon (ctxp->import_demand_list,
807                                  build_tree_list ($2, NULL_TREE));
808                     }
809                 }
810 |       IMPORT_TK name DOT_TK error
811                 {yyerror ("'*' expected"); RECOVER;}
812 |       IMPORT_TK name DOT_TK MULT_TK error
813                 {yyerror ("';' expected"); RECOVER;}
814 ;
815
816 type_declaration:
817         class_declaration
818                 { end_class_declaration (0); }
819 |       interface_declaration
820                 { end_class_declaration (0); }
821 |       empty_statement
822 |       error
823                 {
824                   YYERROR_NOW;
825                   yyerror ("Class or interface declaration expected");
826                 }
827 ;
828
829 /* 19.7 Shortened from the original:
830    modifiers: modifier | modifiers modifier
831    modifier: any of public...  */
832 modifiers:
833         MODIFIER_TK
834                 {
835                   $$ = (1 << $1);
836                 }
837 |       modifiers MODIFIER_TK
838                 {
839                   int acc = (1 << $2);
840                   if ($$ & acc)
841                     parse_error_context 
842                       (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
843                        java_accstring_lookup (acc));
844                   else
845                     {
846                       $$ |= acc;
847                     }
848                 }
849 ;
850
851 /* 19.8.1 Production from $8.1: Class Declaration */
852 class_declaration:
853         modifiers CLASS_TK identifier super interfaces
854                 { create_class ($1, $3, $4, $5); }
855         class_body
856 |       CLASS_TK identifier super interfaces 
857                 { create_class (0, $2, $3, $4); }
858         class_body
859 |       modifiers CLASS_TK error
860                 {yyerror ("Missing class name"); RECOVER;}
861 |       CLASS_TK error
862                 {yyerror ("Missing class name"); RECOVER;}
863 |       CLASS_TK identifier error
864                 {
865                   if (!ctxp->class_err) yyerror ("'{' expected"); 
866                   DRECOVER(class1);
867                 }
868 |       modifiers CLASS_TK identifier error
869                 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
870 ;
871
872 super:
873                 { $$ = NULL; }
874 |       EXTENDS_TK class_type
875                 { $$ = $2; }
876 |       EXTENDS_TK class_type error
877                 {yyerror ("'{' expected"); ctxp->class_err=1;}
878 |       EXTENDS_TK error
879                 {yyerror ("Missing super class name"); ctxp->class_err=1;}
880 ;
881
882 interfaces:
883                 { $$ = NULL_TREE; }
884 |       IMPLEMENTS_TK interface_type_list
885                 { $$ = $2; }
886 |       IMPLEMENTS_TK error
887                 {
888                   ctxp->class_err=1;
889                   yyerror ("Missing interface name"); 
890                 }
891 ;
892
893 interface_type_list:
894         interface_type
895                 { 
896                   ctxp->interface_number = 1;
897                   $$ = build_tree_list ($1, NULL_TREE);
898                 }
899 |       interface_type_list C_TK interface_type
900                 { 
901                   ctxp->interface_number++;
902                   $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
903                 }
904 |       interface_type_list C_TK error
905                 {yyerror ("Missing interface name"); RECOVER;}
906 ;
907
908 class_body:
909         OCB_TK CCB_TK
910                 { 
911                   /* Store the location of the `}' when doing xrefs */
912                   if (flag_emit_xref)
913                     DECL_END_SOURCE_LINE (GET_CPC ()) = 
914                       EXPR_WFL_ADD_COL ($2.location, 1);
915                   $$ = GET_CPC ();
916                 }
917 |       OCB_TK class_body_declarations CCB_TK
918                 { 
919                   /* Store the location of the `}' when doing xrefs */
920                   if (flag_emit_xref)
921                     DECL_END_SOURCE_LINE (GET_CPC ()) = 
922                       EXPR_WFL_ADD_COL ($3.location, 1);
923                   $$ = GET_CPC ();
924                 }
925 ;
926
927 class_body_declarations:
928         class_body_declaration
929 |       class_body_declarations class_body_declaration
930 ;
931
932 class_body_declaration:
933         class_member_declaration
934 |       static_initializer
935 |       constructor_declaration
936 |       block                   /* Added, JDK1.1, instance initializer */
937                 {
938                   if ($1 != empty_stmt_node)
939                     {
940                       TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
941                       SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
942                     }
943                 }
944 ;
945
946 class_member_declaration:
947         field_declaration
948 |       method_declaration
949 |       class_declaration       /* Added, JDK1.1 inner classes */
950                 { end_class_declaration (1); }
951 |       interface_declaration   /* Added, JDK1.1 inner interfaces */
952                 { end_class_declaration (1); }
953 |       empty_statement
954 ;
955
956 /* 19.8.2 Productions from 8.3: Field Declarations  */
957 field_declaration:
958         type variable_declarators SC_TK
959                 { register_fields (0, $1, $2); }
960 |       modifiers type variable_declarators SC_TK
961                 {
962                   check_modifiers 
963                     ("Illegal modifier `%s' for field declaration",
964                      $1, FIELD_MODIFIERS);
965                   check_modifiers_consistency ($1);
966                   register_fields ($1, $2, $3);
967                 }
968 ;
969
970 variable_declarators:
971         /* Should we use build_decl_list () instead ? FIXME */
972         variable_declarator     /* Default rule */
973 |       variable_declarators C_TK variable_declarator
974                 { $$ = chainon ($1, $3); }
975 |       variable_declarators C_TK error
976                 {yyerror ("Missing term"); RECOVER;}
977 ;
978
979 variable_declarator:
980         variable_declarator_id
981                 { $$ = build_tree_list ($1, NULL_TREE); }
982 |       variable_declarator_id ASSIGN_TK variable_initializer
983                 { 
984                   if (java_error_count)
985                     $3 = NULL_TREE;
986                   $$ = build_tree_list 
987                     ($1, build_assignment ($2.token, $2.location, $1, $3));
988                 }
989 |       variable_declarator_id ASSIGN_TK error
990                 {
991                   yyerror ("Missing variable initializer");
992                   $$ = build_tree_list ($1, NULL_TREE);
993                   RECOVER;
994                 }
995 |       variable_declarator_id ASSIGN_TK variable_initializer error
996                 {
997                   yyerror ("';' expected");
998                   $$ = build_tree_list ($1, NULL_TREE);
999                   RECOVER;
1000                 }
1001 ;
1002
1003 variable_declarator_id:
1004         identifier
1005 |       variable_declarator_id OSB_TK CSB_TK
1006                 { $$ = build_unresolved_array_type ($1); }
1007 |       identifier error
1008                 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
1009 |       variable_declarator_id OSB_TK error
1010                 { 
1011                   tree node = java_lval.node;
1012                   if (node && (TREE_CODE (node) == INTEGER_CST
1013                                || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
1014                     yyerror ("Can't specify array dimension in a declaration");
1015                   else
1016                     yyerror ("']' expected");
1017                   DRECOVER(vdi);
1018                 }
1019 |       variable_declarator_id CSB_TK error
1020                 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1021 ;
1022
1023 variable_initializer:
1024         expression
1025 |       array_initializer
1026 ;
1027
1028 /* 19.8.3 Productions from 8.4: Method Declarations  */
1029 method_declaration:
1030         method_header 
1031                 {
1032                   current_function_decl = $1;
1033                   if (current_function_decl
1034                       && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1035                     source_start_java_method (current_function_decl);
1036                   else
1037                     current_function_decl = NULL_TREE;
1038                 }
1039         method_body
1040                 { finish_method_declaration ($3); }
1041 |       method_header error
1042                 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1043 ;
1044
1045 method_header:  
1046         type method_declarator throws
1047                 { $$ = method_header (0, $1, $2, $3); }
1048 |       VOID_TK method_declarator throws
1049                 { $$ = method_header (0, void_type_node, $2, $3); }
1050 |       modifiers type method_declarator throws
1051                 { $$ = method_header ($1, $2, $3, $4); }
1052 |       modifiers VOID_TK method_declarator throws
1053                 { $$ = method_header ($1, void_type_node, $3, $4); }
1054 |       type error
1055                 {
1056                   yyerror ("Invalid method declaration, method name required");
1057                   RECOVER;
1058                 }
1059 |       modifiers type error
1060                 {RECOVER;}
1061 |       VOID_TK error
1062                 {yyerror ("Identifier expected"); RECOVER;}
1063 |       modifiers VOID_TK error
1064                 {yyerror ("Identifier expected"); RECOVER;}
1065 |       modifiers error
1066                 {
1067                   yyerror ("Invalid method declaration, return type required");
1068                   RECOVER;
1069                 }
1070 ;
1071
1072 method_declarator:
1073         identifier OP_TK CP_TK
1074                 { 
1075                   ctxp->formal_parameter_number = 0;
1076                   $$ = method_declarator ($1, NULL_TREE);
1077                 }
1078 |       identifier OP_TK formal_parameter_list CP_TK
1079                 { $$ = method_declarator ($1, $3); }
1080 |       method_declarator OSB_TK CSB_TK
1081                 {
1082                   EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1083                   TREE_PURPOSE ($1) = 
1084                     build_unresolved_array_type (TREE_PURPOSE ($1));
1085                   parse_warning_context 
1086                     (wfl_operator, 
1087                      "Discouraged form of returned type specification");
1088                 }
1089 |       identifier OP_TK error
1090                 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1091 |       method_declarator OSB_TK error
1092                 {yyerror ("']' expected"); RECOVER;}
1093 ;
1094
1095 formal_parameter_list:
1096         formal_parameter
1097                 {
1098                   ctxp->formal_parameter_number = 1;
1099                 }
1100 |       formal_parameter_list C_TK formal_parameter
1101                 {
1102                   ctxp->formal_parameter_number += 1;
1103                   $$ = chainon ($1, $3);
1104                 }
1105 |       formal_parameter_list C_TK error
1106                 { yyerror ("Missing formal parameter term"); RECOVER; }
1107 ;
1108
1109 formal_parameter:
1110         type variable_declarator_id
1111                 {
1112                   $$ = build_tree_list ($2, $1);
1113                 }
1114 |       final type variable_declarator_id /* Added, JDK1.1 final parms */
1115                 { 
1116                   $$ = build_tree_list ($3, $2);
1117                   ARG_FINAL_P ($$) = 1;
1118                 }
1119 |       type error
1120                 {
1121                   yyerror ("Missing identifier"); RECOVER;
1122                   $$ = NULL_TREE;
1123                 }
1124 |       final type error
1125                 {
1126                   yyerror ("Missing identifier"); RECOVER;
1127                   $$ = NULL_TREE;
1128                 }
1129 ;
1130
1131 final:
1132         modifiers
1133                 {
1134                   check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1135                                    $1, ACC_FINAL);
1136                   if ($1 != ACC_FINAL)
1137                     MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1138                 }
1139 ;
1140
1141 throws:
1142                 { $$ = NULL_TREE; }
1143 |       THROWS_TK class_type_list
1144                 { $$ = $2; }
1145 |       THROWS_TK error
1146                 {yyerror ("Missing class type term"); RECOVER;}
1147 ;
1148
1149 class_type_list:
1150         class_type
1151                 { $$ = build_tree_list ($1, $1); }
1152 |       class_type_list C_TK class_type
1153                 { $$ = tree_cons ($3, $3, $1); }
1154 |       class_type_list C_TK error
1155                 {yyerror ("Missing class type term"); RECOVER;}
1156 ;
1157
1158 method_body:
1159         block
1160 |       SC_TK { $$ = NULL_TREE; }
1161 ;
1162
1163 /* 19.8.4 Productions from 8.5: Static Initializers  */
1164 static_initializer:
1165         static block
1166                 {
1167                   TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1168                   SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1169                   current_static_block = NULL_TREE;
1170                 }
1171 ;
1172
1173 static:                         /* Test lval.sub_token here */
1174         modifiers
1175                 {
1176                   check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1177                   /* Can't have a static initializer in an innerclass */
1178                   if ($1 | ACC_STATIC &&
1179                       GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1180                     parse_error_context 
1181                       (MODIFIER_WFL (STATIC_TK),
1182                        "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1183                        IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1184                   SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1185                 }
1186 ;
1187
1188 /* 19.8.5 Productions from 8.6: Constructor Declarations  */
1189 constructor_declaration:
1190         constructor_header
1191                 {
1192                   current_function_decl = $1;
1193                   source_start_java_method (current_function_decl);
1194                 }
1195         constructor_body
1196                 { finish_method_declaration ($3); }
1197 ;
1198
1199 constructor_header:
1200         constructor_declarator throws
1201                 { $$ = method_header (0, NULL_TREE, $1, $2); }
1202 |       modifiers constructor_declarator throws
1203                 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1204 ;
1205
1206 constructor_declarator:
1207         simple_name OP_TK CP_TK
1208                 { 
1209                   ctxp->formal_parameter_number = 0;  
1210                   $$ = method_declarator ($1, NULL_TREE);
1211                 }
1212 |       simple_name OP_TK formal_parameter_list CP_TK
1213                 { $$ = method_declarator ($1, $3); }
1214 ;
1215
1216 constructor_body:
1217         /* Unlike regular method, we always need a complete (empty)
1218            body so we can safely perform all the required code
1219            addition (super invocation and field initialization) */
1220         block_begin constructor_block_end
1221                 { 
1222                   BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1223                   $$ = $2;
1224                 }
1225 |       block_begin explicit_constructor_invocation constructor_block_end
1226                 { $$ = $3; }
1227 |       block_begin block_statements constructor_block_end
1228                 { $$ = $3; }
1229 |       block_begin explicit_constructor_invocation block_statements constructor_block_end
1230                 { $$ = $4; }
1231 ;
1232
1233 constructor_block_end:
1234         block_end
1235 ;
1236
1237 /* Error recovery for that rule moved down expression_statement: rule.  */
1238 explicit_constructor_invocation:
1239         this_or_super OP_TK CP_TK SC_TK
1240                 { 
1241                   $$ = build_method_invocation ($1, NULL_TREE); 
1242                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1243                   $$ = java_method_add_stmt (current_function_decl, $$);
1244                 }
1245 |       this_or_super OP_TK argument_list CP_TK SC_TK
1246                 { 
1247                   $$ = build_method_invocation ($1, $3); 
1248                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1249                   $$ = java_method_add_stmt (current_function_decl, $$);
1250                 }
1251         /* Added, JDK1.1 inner classes. Modified because the rule
1252            'primary' couldn't work.  */
1253 |       name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1254                 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1255 |       name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1256                 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1257 ;
1258
1259 this_or_super:                  /* Added, simplifies error diagnostics */
1260         THIS_TK
1261                 {
1262                   tree wfl = build_wfl_node (this_identifier_node);
1263                   EXPR_WFL_LINECOL (wfl) = $1.location;
1264                   $$ = wfl;
1265                 }
1266 |       SUPER_TK
1267                 {
1268                   tree wfl = build_wfl_node (super_identifier_node);
1269                   EXPR_WFL_LINECOL (wfl) = $1.location;
1270                   $$ = wfl;
1271                 }
1272 ;
1273
1274 /* 19.9 Productions from 9: Interfaces  */
1275 /* 19.9.1 Productions from 9.1: Interfaces Declarations  */
1276 interface_declaration:
1277         INTERFACE_TK identifier
1278                 { create_interface (0, $2, NULL_TREE); }
1279         interface_body
1280 |       modifiers INTERFACE_TK identifier
1281                 { create_interface ($1, $3, NULL_TREE); }
1282         interface_body
1283 |       INTERFACE_TK identifier extends_interfaces
1284                 { create_interface (0, $2, $3); }
1285         interface_body
1286 |       modifiers INTERFACE_TK identifier extends_interfaces
1287                 { create_interface ($1, $3, $4); }
1288         interface_body
1289 |       INTERFACE_TK identifier error
1290                 {yyerror ("'{' expected"); RECOVER;}
1291 |       modifiers INTERFACE_TK identifier error
1292                 {yyerror ("'{' expected"); RECOVER;}
1293 ;
1294
1295 extends_interfaces:
1296         EXTENDS_TK interface_type
1297                 { 
1298                   ctxp->interface_number = 1;
1299                   $$ = build_tree_list ($2, NULL_TREE);
1300                 }
1301 |       extends_interfaces C_TK interface_type
1302                 { 
1303                   ctxp->interface_number++;
1304                   $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1305                 }
1306 |       EXTENDS_TK error
1307                 {yyerror ("Invalid interface type"); RECOVER;}
1308 |       extends_interfaces C_TK error
1309                 {yyerror ("Missing term"); RECOVER;}
1310 ;
1311
1312 interface_body:
1313         OCB_TK CCB_TK
1314                 { $$ = NULL_TREE; }
1315 |       OCB_TK interface_member_declarations CCB_TK
1316                 { $$ = NULL_TREE; }
1317 ;
1318
1319 interface_member_declarations:
1320         interface_member_declaration
1321 |       interface_member_declarations interface_member_declaration
1322 ;
1323
1324 interface_member_declaration:
1325         constant_declaration
1326 |       abstract_method_declaration
1327 |       class_declaration       /* Added, JDK1.1 inner classes */
1328                 { end_class_declaration (1); }
1329 |       interface_declaration   /* Added, JDK1.1 inner interfaces */
1330                 { end_class_declaration (1); }
1331 ;
1332
1333 constant_declaration:
1334         field_declaration
1335 ;
1336
1337 abstract_method_declaration:
1338         method_header SC_TK
1339                 { 
1340                   check_abstract_method_header ($1);
1341                   current_function_decl = NULL_TREE; /* FIXME ? */
1342                 }
1343 |       method_header error
1344                 {yyerror ("';' expected"); RECOVER;}
1345 ;
1346
1347 /* 19.10 Productions from 10: Arrays  */
1348 array_initializer:
1349         OCB_TK CCB_TK
1350                 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1351 |       OCB_TK C_TK CCB_TK
1352                 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1353 |       OCB_TK variable_initializers CCB_TK
1354                 { $$ = build_new_array_init ($1.location, $2); }
1355 |       OCB_TK variable_initializers C_TK CCB_TK
1356                 { $$ = build_new_array_init ($1.location, $2); }
1357 ;
1358
1359 variable_initializers:
1360         variable_initializer
1361                 { 
1362                   $$ = tree_cons (maybe_build_array_element_wfl ($1), 
1363                                   $1, NULL_TREE);
1364                 }
1365 |       variable_initializers C_TK variable_initializer
1366                 {
1367                   $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1368                 }
1369 |       variable_initializers C_TK error
1370                 {yyerror ("Missing term"); RECOVER;}
1371 ;
1372
1373 /* 19.11 Production from 14: Blocks and Statements  */
1374 block:
1375         OCB_TK CCB_TK
1376                 { 
1377                   /* Store the location of the `}' when doing xrefs */
1378                   if (current_function_decl && flag_emit_xref)
1379                     DECL_END_SOURCE_LINE (current_function_decl) = 
1380                       EXPR_WFL_ADD_COL ($2.location, 1);
1381                   $$ = empty_stmt_node; 
1382                 }
1383 |       block_begin block_statements block_end
1384                 { $$ = $3; }
1385 ;
1386
1387 block_begin:
1388         OCB_TK
1389                 { enter_block (); }
1390 ;
1391
1392 block_end:
1393         CCB_TK
1394                 { 
1395                   maybe_absorb_scoping_blocks ();
1396                   /* Store the location of the `}' when doing xrefs */
1397                   if (current_function_decl && flag_emit_xref)
1398                     DECL_END_SOURCE_LINE (current_function_decl) = 
1399                       EXPR_WFL_ADD_COL ($1.location, 1);                  
1400                   $$ = exit_block ();
1401                   if (!BLOCK_SUBBLOCKS ($$))
1402                     BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1403                 }
1404 ;
1405
1406 block_statements:
1407         block_statement
1408 |       block_statements block_statement
1409 ;
1410
1411 block_statement:
1412         local_variable_declaration_statement
1413 |       statement
1414                 { java_method_add_stmt (current_function_decl, $1); }
1415 |       class_declaration       /* Added, JDK1.1 local classes */
1416                 { 
1417                   LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1418                   end_class_declaration (1);
1419                 }
1420 ;
1421
1422 local_variable_declaration_statement:
1423         local_variable_declaration SC_TK /* Can't catch missing ';' here */
1424 ;
1425
1426 local_variable_declaration:
1427         type variable_declarators
1428                 { declare_local_variables (0, $1, $2); }
1429 |       final type variable_declarators /* Added, JDK1.1 final locals */
1430                 { declare_local_variables ($1, $2, $3); }
1431 ;
1432
1433 statement:
1434         statement_without_trailing_substatement
1435 |       labeled_statement
1436 |       if_then_statement
1437 |       if_then_else_statement
1438 |       while_statement
1439 |       for_statement
1440                 { $$ = exit_block (); }
1441 ;
1442
1443 statement_nsi:
1444         statement_without_trailing_substatement
1445 |       labeled_statement_nsi
1446 |       if_then_else_statement_nsi
1447 |       while_statement_nsi
1448 |       for_statement_nsi
1449                 { $$ = exit_block (); }
1450 ;
1451
1452 statement_without_trailing_substatement:
1453         block
1454 |       empty_statement
1455 |       expression_statement
1456 |       switch_statement
1457 |       do_statement
1458 |       break_statement
1459 |       continue_statement
1460 |       return_statement
1461 |       synchronized_statement
1462 |       throw_statement
1463 |       try_statement
1464 ;
1465
1466 empty_statement:
1467         SC_TK
1468                 { 
1469                   if (flag_extraneous_semicolon 
1470                       && ! current_static_block 
1471                       && (! current_function_decl || 
1472                           /* Verify we're not in a inner class declaration */
1473                           (GET_CPC () != TYPE_NAME
1474                            (DECL_CONTEXT (current_function_decl)))))
1475                            
1476                     {
1477                       EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1478                       parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1479                     }
1480                   $$ = empty_stmt_node;
1481                 }
1482 ;
1483
1484 label_decl:
1485         identifier REL_CL_TK
1486                 {
1487                   $$ = build_labeled_block (EXPR_WFL_LINECOL ($1), 
1488                                             EXPR_WFL_NODE ($1));
1489                   pushlevel (2);
1490                   push_labeled_block ($$);
1491                   PUSH_LABELED_BLOCK ($$);
1492                 }
1493 ;
1494
1495 labeled_statement:
1496         label_decl statement
1497                 { $$ = finish_labeled_statement ($1, $2); }
1498 |       identifier error
1499                 {yyerror ("':' expected"); RECOVER;}
1500 ;
1501
1502 labeled_statement_nsi:
1503         label_decl statement_nsi
1504                 { $$ = finish_labeled_statement ($1, $2); }
1505 ;
1506
1507 /* We concentrate here a bunch of error handling rules that we couldn't write
1508    earlier, because expression_statement catches a missing ';'.  */
1509 expression_statement:
1510         statement_expression SC_TK
1511                 {
1512                   /* We have a statement. Generate a WFL around it so
1513                      we can debug it */
1514                   $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1515                   /* We know we have a statement, so set the debug
1516                      info to be eventually generate here. */
1517                   $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1518                 }
1519 |       error SC_TK 
1520                 {
1521                   YYNOT_TWICE yyerror ("Invalid expression statement");
1522                   DRECOVER (expr_stmt);
1523                 }
1524 |       error OCB_TK
1525                 {
1526                   YYNOT_TWICE yyerror ("Invalid expression statement");
1527                   DRECOVER (expr_stmt);
1528                 }
1529 |       error CCB_TK
1530                 {
1531                   YYNOT_TWICE yyerror ("Invalid expression statement");
1532                   DRECOVER (expr_stmt);
1533                 }
1534 |       this_or_super OP_TK error
1535                 {yyerror ("')' expected"); RECOVER;}
1536 |       this_or_super OP_TK CP_TK error
1537                 {
1538                   parse_ctor_invocation_error ();
1539                   RECOVER;
1540                 }
1541 |       this_or_super OP_TK argument_list error
1542                 {yyerror ("')' expected"); RECOVER;}
1543 |       this_or_super OP_TK argument_list CP_TK error
1544                 {
1545                   parse_ctor_invocation_error ();
1546                   RECOVER;
1547                 }
1548 |       name DOT_TK SUPER_TK error
1549                 {yyerror ("'(' expected"); RECOVER;}
1550 |       name DOT_TK SUPER_TK OP_TK error
1551                 {yyerror ("')' expected"); RECOVER;}
1552 |       name DOT_TK SUPER_TK OP_TK argument_list error
1553                 {yyerror ("')' expected"); RECOVER;}
1554 |       name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1555                 {yyerror ("';' expected"); RECOVER;}
1556 |       name DOT_TK SUPER_TK OP_TK CP_TK error
1557                 {yyerror ("';' expected"); RECOVER;}
1558 ;
1559
1560 statement_expression: 
1561         assignment
1562 |       pre_increment_expression
1563 |       pre_decrement_expression
1564 |       post_increment_expression
1565 |       post_decrement_expression
1566 |       method_invocation
1567 |       class_instance_creation_expression
1568 ;
1569
1570 if_then_statement:
1571         IF_TK OP_TK expression CP_TK statement
1572                 { 
1573                   $$ = build_if_else_statement ($2.location, $3, 
1574                                                 $5, NULL_TREE);
1575                 }
1576 |       IF_TK error
1577                 {yyerror ("'(' expected"); RECOVER;}
1578 |       IF_TK OP_TK error
1579                 {yyerror ("Missing term"); RECOVER;}
1580 |       IF_TK OP_TK expression error
1581                 {yyerror ("')' expected"); RECOVER;}
1582 ;
1583
1584 if_then_else_statement:
1585         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1586                 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1587 ;
1588
1589 if_then_else_statement_nsi:
1590         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1591                 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1592 ;
1593
1594 switch_statement:
1595         switch_expression
1596                 {
1597                   enter_block ();
1598                 }
1599         switch_block
1600                 { 
1601                   /* Make into "proper list" of COMPOUND_EXPRs.
1602                      I.e. make the last statement also have its own
1603                      COMPOUND_EXPR. */
1604                   maybe_absorb_scoping_blocks ();
1605                   TREE_OPERAND ($1, 1) = exit_block ();
1606                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1607                 }
1608 ;
1609
1610 switch_expression:
1611         SWITCH_TK OP_TK expression CP_TK
1612                 { 
1613                   $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1614                   EXPR_WFL_LINECOL ($$) = $2.location;
1615                 }
1616 |       SWITCH_TK error
1617                 {yyerror ("'(' expected"); RECOVER;}
1618 |       SWITCH_TK OP_TK error
1619                 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1620 |       SWITCH_TK OP_TK expression CP_TK error
1621                 {yyerror ("'{' expected"); RECOVER;}
1622 ;
1623
1624 /* Default assignment is there to avoid type node on switch_block
1625    node. */
1626
1627 switch_block:
1628         OCB_TK CCB_TK
1629                 { $$ = NULL_TREE; }
1630 |       OCB_TK switch_labels CCB_TK
1631                 { $$ = NULL_TREE; }
1632 |       OCB_TK switch_block_statement_groups CCB_TK
1633                 { $$ = NULL_TREE; }
1634 |       OCB_TK switch_block_statement_groups switch_labels CCB_TK
1635                 { $$ = NULL_TREE; }
1636 ;
1637
1638 switch_block_statement_groups: 
1639         switch_block_statement_group
1640 |       switch_block_statement_groups switch_block_statement_group
1641 ;
1642
1643 switch_block_statement_group:
1644         switch_labels block_statements
1645 ;
1646
1647 switch_labels:
1648         switch_label
1649 |       switch_labels switch_label
1650 ;
1651
1652 switch_label:
1653         CASE_TK constant_expression REL_CL_TK
1654                 { 
1655                   tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1656                   EXPR_WFL_LINECOL (lab) = $1.location;
1657                   java_method_add_stmt (current_function_decl, lab);
1658                 }
1659 |       DEFAULT_TK REL_CL_TK
1660                 { 
1661                   tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1662                   EXPR_WFL_LINECOL (lab) = $1.location;
1663                   java_method_add_stmt (current_function_decl, lab);
1664                 }
1665 |       CASE_TK error
1666                 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1667 |       CASE_TK constant_expression error
1668                 {yyerror ("':' expected"); RECOVER;}
1669 |       DEFAULT_TK error
1670                 {yyerror ("':' expected"); RECOVER;}
1671 ;
1672
1673 while_expression:
1674         WHILE_TK OP_TK expression CP_TK
1675                 { 
1676                   tree body = build_loop_body ($2.location, $3, 0);
1677                   $$ = build_new_loop (body);
1678                 }
1679 ;
1680
1681 while_statement:
1682         while_expression statement
1683                 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1684 |       WHILE_TK error
1685                 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1686 |       WHILE_TK OP_TK error
1687                 {yyerror ("Missing term and ')' expected"); RECOVER;}
1688 |       WHILE_TK OP_TK expression error
1689                 {yyerror ("')' expected"); RECOVER;}
1690 ;
1691
1692 while_statement_nsi:
1693         while_expression statement_nsi
1694                 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1695 ;
1696
1697 do_statement_begin:
1698         DO_TK
1699                 { 
1700                   tree body = build_loop_body (0, NULL_TREE, 1);
1701                   $$ = build_new_loop (body);
1702                 }
1703         /* Need error handing here. FIXME */
1704 ;
1705
1706 do_statement: 
1707         do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1708                 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1709 ;
1710
1711 for_statement:
1712         for_begin SC_TK expression SC_TK for_update CP_TK statement
1713                 {
1714                   if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1715                     $3 = build_wfl_node ($3);
1716                   $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1717                 }
1718 |       for_begin SC_TK SC_TK for_update CP_TK statement
1719                 { 
1720                   $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1721                   /* We have not condition, so we get rid of the EXIT_EXPR */
1722                   LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) = 
1723                     empty_stmt_node;
1724                 }
1725 |       for_begin SC_TK error
1726                 {yyerror ("Invalid control expression"); RECOVER;}
1727 |       for_begin SC_TK expression SC_TK error
1728                 {yyerror ("Invalid update expression"); RECOVER;}
1729 |       for_begin SC_TK SC_TK error
1730                 {yyerror ("Invalid update expression"); RECOVER;}
1731 ;
1732
1733 for_statement_nsi:
1734         for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1735                 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1736 |       for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1737                 { 
1738                   $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1739                   /* We have not condition, so we get rid of the EXIT_EXPR */
1740                   LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) = 
1741                     empty_stmt_node;
1742                 }
1743 ;
1744
1745 for_header:
1746         FOR_TK OP_TK
1747                 { 
1748                   /* This scope defined for local variable that may be
1749                      defined within the scope of the for loop */
1750                   enter_block (); 
1751                 }
1752 |       FOR_TK error
1753                 {yyerror ("'(' expected"); DRECOVER(for_1);}
1754 |       FOR_TK OP_TK error
1755                 {yyerror ("Invalid init statement"); RECOVER;}
1756 ;
1757
1758 for_begin:
1759         for_header for_init
1760                 { 
1761                   /* We now declare the loop body. The loop is
1762                      declared as a for loop. */
1763                   tree body = build_loop_body (0, NULL_TREE, 0);
1764                   $$ =  build_new_loop (body);
1765                   FOR_LOOP_P ($$) = 1;
1766                   /* The loop is added to the current block the for
1767                      statement is defined within */
1768                   java_method_add_stmt (current_function_decl, $$);
1769                 }
1770 ;
1771 for_init:                       /* Can be empty */
1772                 { $$ = empty_stmt_node; }
1773 |       statement_expression_list
1774                 { 
1775                   /* Init statement recorded within the previously
1776                      defined block scope */
1777                   $$ = java_method_add_stmt (current_function_decl, $1);
1778                 }
1779 |       local_variable_declaration
1780                 { 
1781                   /* Local variable are recorded within the previously
1782                      defined block scope */
1783                   $$ = NULL_TREE;
1784                 }
1785 |       statement_expression_list error
1786                 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1787 ;
1788
1789 for_update:                     /* Can be empty */
1790                 {$$ = empty_stmt_node;}
1791 |       statement_expression_list
1792                 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1793 ;
1794
1795 statement_expression_list:
1796         statement_expression
1797                 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1798 |       statement_expression_list C_TK statement_expression
1799                 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1800 |       statement_expression_list C_TK error
1801                 {yyerror ("Missing term"); RECOVER;}
1802 ;
1803
1804 break_statement:
1805         BREAK_TK SC_TK
1806                 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1807 |       BREAK_TK identifier SC_TK
1808                 { $$ = build_bc_statement ($1.location, 1, $2); }
1809 |       BREAK_TK error
1810                 {yyerror ("Missing term"); RECOVER;}
1811 |       BREAK_TK identifier error
1812                 {yyerror ("';' expected"); RECOVER;}
1813 ;
1814
1815 continue_statement:
1816         CONTINUE_TK SC_TK
1817                 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1818 |       CONTINUE_TK identifier SC_TK
1819                 { $$ = build_bc_statement ($1.location, 0, $2); }
1820 |       CONTINUE_TK error
1821                 {yyerror ("Missing term"); RECOVER;}
1822 |       CONTINUE_TK identifier error
1823                 {yyerror ("';' expected"); RECOVER;}
1824 ;
1825
1826 return_statement:
1827         RETURN_TK SC_TK
1828                 { $$ = build_return ($1.location, NULL_TREE); }
1829 |       RETURN_TK expression SC_TK
1830                 { $$ = build_return ($1.location, $2); }
1831 |       RETURN_TK error
1832                 {yyerror ("Missing term"); RECOVER;}
1833 |       RETURN_TK expression error
1834                 {yyerror ("';' expected"); RECOVER;}
1835 ;
1836
1837 throw_statement:
1838         THROW_TK expression SC_TK
1839                 { 
1840                   $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1841                   EXPR_WFL_LINECOL ($$) = $1.location;
1842                 }
1843 |       THROW_TK error
1844                 {yyerror ("Missing term"); RECOVER;}
1845 |       THROW_TK expression error
1846                 {yyerror ("';' expected"); RECOVER;}
1847 ;
1848
1849 synchronized_statement:
1850         synchronized OP_TK expression CP_TK block
1851                 { 
1852                   $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1853                   EXPR_WFL_LINECOL ($$) = 
1854                     EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1855                 }
1856 |       synchronized OP_TK expression CP_TK error
1857                 {yyerror ("'{' expected"); RECOVER;}
1858 |       synchronized error
1859                 {yyerror ("'(' expected"); RECOVER;}
1860 |       synchronized OP_TK error CP_TK
1861                 {yyerror ("Missing term"); RECOVER;}
1862 |       synchronized OP_TK error
1863                 {yyerror ("Missing term"); RECOVER;}
1864 ;
1865
1866 synchronized:
1867         modifiers
1868                 {
1869                   check_modifiers (
1870              "Illegal modifier `%s'. Only `synchronized' was expected here",
1871                                    $1, ACC_SYNCHRONIZED);
1872                   if ($1 != ACC_SYNCHRONIZED)
1873                     MODIFIER_WFL (SYNCHRONIZED_TK) = 
1874                       build_wfl_node (NULL_TREE);
1875                 }
1876 ;
1877
1878 try_statement:
1879         TRY_TK block catches
1880                 { $$ = build_try_statement ($1.location, $2, $3); }
1881 |       TRY_TK block finally
1882                 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1883 |       TRY_TK block catches finally
1884                 { $$ = build_try_finally_statement 
1885                     ($1.location, build_try_statement ($1.location,
1886                                                        $2, $3), $4);
1887                 }
1888 |       TRY_TK error
1889                 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1890 ;
1891
1892 catches:
1893         catch_clause
1894 |       catches catch_clause
1895                 { 
1896                   TREE_CHAIN ($2) = $1;
1897                   $$ = $2;
1898                 }
1899 ;
1900
1901 catch_clause:
1902         catch_clause_parameter block
1903                 { 
1904                   java_method_add_stmt (current_function_decl, $2);
1905                   exit_block ();
1906                   $$ = $1;
1907                 }
1908
1909 catch_clause_parameter:
1910         CATCH_TK OP_TK formal_parameter CP_TK
1911                 { 
1912                   /* We add a block to define a scope for
1913                      formal_parameter (CCBP). The formal parameter is
1914                      declared initialized by the appropriate function
1915                      call */
1916                   tree ccpb = enter_block ();
1917                   tree init = build_assignment
1918                     (ASSIGN_TK, $2.location, TREE_PURPOSE ($3), 
1919                      build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1920                   declare_local_variables (0, TREE_VALUE ($3),
1921                                            build_tree_list (TREE_PURPOSE ($3),
1922                                                             init));
1923                   $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1924                   EXPR_WFL_LINECOL ($$) = $1.location;
1925                 }
1926 |       CATCH_TK error
1927                 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1928 |       CATCH_TK OP_TK error 
1929                 {
1930                   yyerror ("Missing term or ')' expected"); 
1931                   RECOVER; $$ = NULL_TREE;
1932                 }
1933 |       CATCH_TK OP_TK error CP_TK /* That's for () */
1934                 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1935 ;
1936
1937 finally:
1938         FINALLY_TK block
1939                 { $$ = $2; }
1940 |       FINALLY_TK error
1941                 {yyerror ("'{' expected"); RECOVER; }
1942 ;
1943
1944 /* 19.12 Production from 15: Expressions  */
1945 primary:
1946         primary_no_new_array
1947 |       array_creation_expression
1948 ;
1949
1950 primary_no_new_array:
1951         literal
1952 |       THIS_TK
1953                 { $$ = build_this ($1.location); }
1954 |       OP_TK expression CP_TK
1955                 {$$ = $2;}
1956 |       class_instance_creation_expression
1957 |       field_access
1958 |       method_invocation
1959 |       array_access
1960 |       type_literals
1961         /* Added, JDK1.1 inner classes. Documentation is wrong
1962            refering to a 'ClassName' (class_name) rule that doesn't
1963            exist. Used name: instead.  */
1964 |       name DOT_TK THIS_TK
1965                 { 
1966                   tree wfl = build_wfl_node (this_identifier_node);
1967                   $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1968                 }
1969 |       OP_TK expression error 
1970                 {yyerror ("')' expected"); RECOVER;}
1971 |       name DOT_TK error
1972                 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1973 |       primitive_type DOT_TK error
1974                 {yyerror ("'class' expected" ); RECOVER;}
1975 |       VOID_TK DOT_TK error
1976                 {yyerror ("'class' expected" ); RECOVER;}
1977 ;
1978
1979 type_literals:
1980         name DOT_TK CLASS_TK
1981                 { $$ = build_incomplete_class_ref ($2.location, $1); }
1982 |       array_type DOT_TK CLASS_TK
1983                 { $$ = build_incomplete_class_ref ($2.location, $1); }
1984 |       primitive_type DOT_TK CLASS_TK
1985                 { $$ = build_incomplete_class_ref ($2.location, $1); }
1986 |       VOID_TK DOT_TK CLASS_TK
1987                 { 
1988                    $$ = build_incomplete_class_ref ($2.location,
1989                                                    void_type_node);
1990                 }
1991 ;
1992
1993 class_instance_creation_expression:
1994         NEW_TK class_type OP_TK argument_list CP_TK
1995                 { $$ = build_new_invocation ($2, $4); }
1996 |       NEW_TK class_type OP_TK CP_TK
1997                 { $$ = build_new_invocation ($2, NULL_TREE); }
1998 |       anonymous_class_creation
1999         /* Added, JDK1.1 inner classes, modified to use name or
2000            primary instead of primary solely which couldn't work in
2001            all situations.  */
2002 |       something_dot_new identifier OP_TK CP_TK
2003                 { 
2004                   tree ctor = build_new_invocation ($2, NULL_TREE);
2005                   $$ = make_qualified_primary ($1, ctor, 
2006                                                EXPR_WFL_LINECOL ($1));
2007                 }
2008 |       something_dot_new identifier OP_TK CP_TK class_body
2009 |       something_dot_new identifier OP_TK argument_list CP_TK
2010                 { 
2011                   tree ctor = build_new_invocation ($2, $4);
2012                   $$ = make_qualified_primary ($1, ctor, 
2013                                                EXPR_WFL_LINECOL ($1));
2014                 }
2015 |       something_dot_new identifier OP_TK argument_list CP_TK class_body
2016 |       NEW_TK error SC_TK 
2017                 {yyerror ("'(' expected"); DRECOVER(new_1);}
2018 |       NEW_TK class_type error
2019                 {yyerror ("'(' expected"); RECOVER;}
2020 |       NEW_TK class_type OP_TK error
2021                 {yyerror ("')' or term expected"); RECOVER;}
2022 |       NEW_TK class_type OP_TK argument_list error
2023                 {yyerror ("')' expected"); RECOVER;}
2024 |       something_dot_new error
2025                 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
2026 |       something_dot_new identifier error
2027                 {yyerror ("'(' expected"); RECOVER;}
2028 ;
2029
2030 /* Created after JDK1.1 rules originally added to
2031    class_instance_creation_expression, but modified to use
2032    'class_type' instead of 'TypeName' (type_name) which is mentionned
2033    in the documentation but doesn't exist. */
2034
2035 anonymous_class_creation:
2036         NEW_TK class_type OP_TK argument_list CP_TK 
2037                 { create_anonymous_class ($1.location, $2); }
2038         class_body
2039                 { 
2040                   tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2041                   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2042
2043                   end_class_declaration (1);
2044
2045                   /* Now we can craft the new expression */
2046                   $$ = build_new_invocation (id, $4);
2047
2048                   /* Note that we can't possibly be here if
2049                      `class_type' is an interface (in which case the
2050                      anonymous class extends Object and implements
2051                      `class_type', hence its constructor can't have
2052                      arguments.) */
2053
2054                   /* Otherwise, the innerclass must feature a
2055                      constructor matching `argument_list'. Anonymous
2056                      classes are a bit special: it's impossible to
2057                      define constructor for them, hence constructors
2058                      must be generated following the hints provided by
2059                      the `new' expression. Whether a super constructor
2060                      of that nature exists or not is to be verified
2061                      later on in verify_constructor_super. 
2062
2063                      It's during the expansion of a `new' statement
2064                      refering to an anonymous class that a ctor will
2065                      be generated for the anonymous class, with the
2066                      right arguments. */
2067
2068                 }
2069 |       NEW_TK class_type OP_TK CP_TK 
2070                 { create_anonymous_class ($1.location, $2); }
2071         class_body         
2072                 { 
2073                   tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2074                   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2075
2076                   end_class_declaration (1);
2077
2078                   /* Now we can craft the new expression. The
2079                      statement doesn't need to be remember so that a
2080                      constructor can be generated, since its signature
2081                      is already known. */
2082                   $$ = build_new_invocation (id, NULL_TREE);
2083                 }
2084 ;
2085
2086 something_dot_new:              /* Added, not part of the specs. */
2087         name DOT_TK NEW_TK
2088                 { $$ = $1; }
2089 |       primary DOT_TK NEW_TK
2090                 { $$ = $1; }
2091 ;
2092
2093 argument_list:
2094         expression
2095                 { 
2096                   $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2097                   ctxp->formal_parameter_number = 1; 
2098                 }
2099 |       argument_list C_TK expression
2100                 {
2101                   ctxp->formal_parameter_number += 1;
2102                   $$ = tree_cons (NULL_TREE, $3, $1);
2103                 }
2104 |       argument_list C_TK error
2105                 {yyerror ("Missing term"); RECOVER;}
2106 ;
2107
2108 array_creation_expression:
2109         NEW_TK primitive_type dim_exprs
2110                 { $$ = build_newarray_node ($2, $3, 0); }
2111 |       NEW_TK class_or_interface_type dim_exprs
2112                 { $$ = build_newarray_node ($2, $3, 0); }
2113 |       NEW_TK primitive_type dim_exprs dims
2114                 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2115 |       NEW_TK class_or_interface_type dim_exprs dims
2116                 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2117         /* Added, JDK1.1 anonymous array. Initial documentation rule
2118            modified */
2119 |       NEW_TK class_or_interface_type dims array_initializer
2120                 {
2121                   char *sig;
2122                   int osb = pop_current_osb (ctxp);
2123                   while (osb--)
2124                     obstack_grow (&temporary_obstack, "[]", 2);
2125                   obstack_1grow (&temporary_obstack, '\0');
2126                   sig = obstack_finish (&temporary_obstack);
2127                   $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2128                               $2, get_identifier (sig), $4);
2129                 }
2130 |       NEW_TK primitive_type dims array_initializer
2131                 { 
2132                   int osb = pop_current_osb (ctxp);
2133                   tree type = $2;
2134                   while (osb--)
2135                     type = build_java_array_type (type, -1);
2136                   $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE, 
2137                               build_pointer_type (type), NULL_TREE, $4);
2138                 }
2139 |       NEW_TK error CSB_TK
2140                 {yyerror ("'[' expected"); DRECOVER ("]");}
2141 |       NEW_TK error OSB_TK
2142                 {yyerror ("']' expected"); RECOVER;}
2143 ;
2144
2145 dim_exprs:
2146         dim_expr
2147                 { $$ = build_tree_list (NULL_TREE, $1); }
2148 |       dim_exprs dim_expr
2149                 { $$ = tree_cons (NULL_TREE, $2, $$); }
2150 ;
2151
2152 dim_expr:
2153         OSB_TK expression CSB_TK
2154                 { 
2155                   if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2156                     {
2157                       $2 = build_wfl_node ($2);
2158                       TREE_TYPE ($2) = NULL_TREE;
2159                     }
2160                   EXPR_WFL_LINECOL ($2) = $1.location;
2161                   $$ = $2;
2162                 }
2163 |       OSB_TK expression error
2164                 {yyerror ("']' expected"); RECOVER;}
2165 |       OSB_TK error
2166                 {
2167                   yyerror ("Missing term");
2168                   yyerror ("']' expected");
2169                   RECOVER;
2170                 }
2171 ;
2172
2173 dims:                           
2174         OSB_TK CSB_TK
2175                 { 
2176                   int allocate = 0;
2177                   /* If not initialized, allocate memory for the osb
2178                      numbers stack */
2179                   if (!ctxp->osb_limit)
2180                     {
2181                       allocate = ctxp->osb_limit = 32;
2182                       ctxp->osb_depth = -1;
2183                     }
2184                   /* If capacity overflown, reallocate a bigger chunk */
2185                   else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2186                     allocate = ctxp->osb_limit << 1;
2187                   
2188                   if (allocate)
2189                     {
2190                       allocate *= sizeof (int);
2191                       if (ctxp->osb_number)
2192                         ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2193                                                             allocate);
2194                       else
2195                         ctxp->osb_number = (int *)xmalloc (allocate);
2196                     }
2197                   ctxp->osb_depth++;
2198                   CURRENT_OSB (ctxp) = 1;
2199                 }
2200 |       dims OSB_TK CSB_TK
2201                 { CURRENT_OSB (ctxp)++; }
2202 |       dims OSB_TK error
2203                 { yyerror ("']' expected"); RECOVER;}
2204 ;
2205
2206 field_access:
2207         primary DOT_TK identifier
2208                 { $$ = make_qualified_primary ($1, $3, $2.location); }
2209                 /*  FIXME - REWRITE TO: 
2210                 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2211 |       SUPER_TK DOT_TK identifier
2212                 {
2213                   tree super_wfl = build_wfl_node (super_identifier_node);
2214                   EXPR_WFL_LINECOL (super_wfl) = $1.location;
2215                   $$ = make_qualified_name (super_wfl, $3, $2.location);
2216                 }
2217 |       SUPER_TK error
2218                 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2219 ;
2220
2221 method_invocation:
2222         name OP_TK CP_TK
2223                 { $$ = build_method_invocation ($1, NULL_TREE); }
2224 |       name OP_TK argument_list CP_TK
2225                 { $$ = build_method_invocation ($1, $3); }
2226 |       primary DOT_TK identifier OP_TK CP_TK
2227                 { 
2228                   if (TREE_CODE ($1) == THIS_EXPR)
2229                     $$ = build_this_super_qualified_invocation 
2230                       (1, $3, NULL_TREE, 0, $2.location);
2231                   else
2232                     {
2233                       tree invok = build_method_invocation ($3, NULL_TREE);
2234                       $$ = make_qualified_primary ($1, invok, $2.location);
2235                     }
2236                 }
2237 |       primary DOT_TK identifier OP_TK argument_list CP_TK
2238                 { 
2239                   if (TREE_CODE ($1) == THIS_EXPR)
2240                     $$ = build_this_super_qualified_invocation 
2241                       (1, $3, $5, 0, $2.location);
2242                   else
2243                     {
2244                       tree invok = build_method_invocation ($3, $5);
2245                       $$ = make_qualified_primary ($1, invok, $2.location);
2246                     }
2247                 }
2248 |       SUPER_TK DOT_TK identifier OP_TK CP_TK
2249                 { 
2250                   $$ = build_this_super_qualified_invocation 
2251                     (0, $3, NULL_TREE, $1.location, $2.location);
2252                 }
2253 |       SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2254                 {
2255                   $$ = build_this_super_qualified_invocation 
2256                     (0, $3, $5, $1.location, $2.location);
2257                 }
2258         /* Screws up thing. I let it here until I'm convinced it can
2259            be removed. FIXME
2260 |       primary DOT_TK error
2261                 {yyerror ("'(' expected"); DRECOVER(bad);} */
2262 |       SUPER_TK DOT_TK error CP_TK
2263                 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2264 |       SUPER_TK DOT_TK error DOT_TK
2265                 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2266 ;
2267
2268 array_access:
2269         name OSB_TK expression CSB_TK
2270                 { $$ = build_array_ref ($2.location, $1, $3); }
2271 |       primary_no_new_array OSB_TK expression CSB_TK
2272                 { $$ = build_array_ref ($2.location, $1, $3); }
2273 |       name OSB_TK error
2274                 {
2275                   yyerror ("Missing term and ']' expected");
2276                   DRECOVER(array_access);
2277                 }
2278 |       name OSB_TK expression error
2279                 {
2280                   yyerror ("']' expected");
2281                   DRECOVER(array_access);
2282                 }
2283 |       primary_no_new_array OSB_TK error
2284                 {
2285                   yyerror ("Missing term and ']' expected");
2286                   DRECOVER(array_access);
2287                 }
2288 |       primary_no_new_array OSB_TK expression error
2289                 {
2290                   yyerror ("']' expected");
2291                   DRECOVER(array_access);
2292                 }
2293 ;
2294
2295 postfix_expression:
2296         primary
2297 |       name
2298 |       post_increment_expression
2299 |       post_decrement_expression
2300 ;
2301
2302 post_increment_expression:
2303         postfix_expression INCR_TK
2304                 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2305 ;
2306
2307 post_decrement_expression:
2308         postfix_expression DECR_TK
2309                 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2310 ;
2311
2312 trap_overflow_corner_case:
2313         pre_increment_expression
2314 |       pre_decrement_expression
2315 |       PLUS_TK unary_expression
2316                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2317 |       unary_expression_not_plus_minus
2318 |       PLUS_TK error
2319                 {yyerror ("Missing term"); RECOVER}
2320 ;
2321
2322 unary_expression:
2323         trap_overflow_corner_case
2324                 {
2325                   error_if_numeric_overflow ($1);
2326                   $$ = $1;
2327                 }
2328 |       MINUS_TK trap_overflow_corner_case
2329                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2330 |       MINUS_TK error
2331                 {yyerror ("Missing term"); RECOVER}
2332 ;
2333
2334 pre_increment_expression:
2335         INCR_TK unary_expression
2336                 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2337 |       INCR_TK error
2338                 {yyerror ("Missing term"); RECOVER}
2339 ;
2340
2341 pre_decrement_expression:
2342         DECR_TK unary_expression
2343                 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2344 |       DECR_TK error
2345                 {yyerror ("Missing term"); RECOVER}
2346 ;
2347
2348 unary_expression_not_plus_minus:
2349         postfix_expression
2350 |       NOT_TK unary_expression
2351                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2352 |       NEG_TK unary_expression
2353                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2354 |       cast_expression
2355 |       NOT_TK error
2356                 {yyerror ("Missing term"); RECOVER}
2357 |       NEG_TK error
2358                 {yyerror ("Missing term"); RECOVER}
2359 ;
2360
2361 cast_expression:                /* Error handling here is potentially weak */
2362         OP_TK primitive_type dims CP_TK unary_expression
2363                 { 
2364                   tree type = $2;
2365                   int osb = pop_current_osb (ctxp);
2366                   while (osb--)
2367                     type = build_java_array_type (type, -1);
2368                   $$ = build_cast ($1.location, type, $5); 
2369                 }
2370 |       OP_TK primitive_type CP_TK unary_expression
2371                 { $$ = build_cast ($1.location, $2, $4); }
2372 |       OP_TK expression CP_TK unary_expression_not_plus_minus
2373                 { $$ = build_cast ($1.location, $2, $4); }
2374 |       OP_TK name dims CP_TK unary_expression_not_plus_minus
2375                 { 
2376                   const char *ptr;
2377                   int osb = pop_current_osb (ctxp); 
2378                   obstack_grow (&temporary_obstack, 
2379                                 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2380                                 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2381                   while (osb--)
2382                     obstack_grow (&temporary_obstack, "[]", 2);
2383                   obstack_1grow (&temporary_obstack, '\0');
2384                   ptr = obstack_finish (&temporary_obstack);
2385                   EXPR_WFL_NODE ($2) = get_identifier (ptr);
2386                   $$ = build_cast ($1.location, $2, $5);
2387                 }
2388 |       OP_TK primitive_type OSB_TK error
2389                 {yyerror ("']' expected, invalid type expression");}
2390 |       OP_TK error
2391                 {
2392                   YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2393                   RECOVER;
2394                 }
2395 |       OP_TK primitive_type dims CP_TK error
2396                 {yyerror ("Missing term"); RECOVER;}
2397 |       OP_TK primitive_type CP_TK error
2398                 {yyerror ("Missing term"); RECOVER;}
2399 |       OP_TK name dims CP_TK error
2400                 {yyerror ("Missing term"); RECOVER;}
2401 ;
2402
2403 multiplicative_expression:
2404         unary_expression
2405 |       multiplicative_expression MULT_TK unary_expression
2406                 { 
2407                   $$ = build_binop (BINOP_LOOKUP ($2.token), 
2408                                     $2.location, $1, $3);
2409                 }
2410 |       multiplicative_expression DIV_TK unary_expression
2411                 {
2412                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2413                                     $1, $3); 
2414                 }
2415 |       multiplicative_expression REM_TK unary_expression
2416                 {
2417                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2418                                     $1, $3); 
2419                 }
2420 |       multiplicative_expression MULT_TK error
2421                 {yyerror ("Missing term"); RECOVER;}
2422 |       multiplicative_expression DIV_TK error
2423                 {yyerror ("Missing term"); RECOVER;}
2424 |       multiplicative_expression REM_TK error
2425                 {yyerror ("Missing term"); RECOVER;}
2426 ;
2427
2428 additive_expression:
2429         multiplicative_expression
2430 |       additive_expression PLUS_TK multiplicative_expression
2431                 {
2432                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2433                                     $1, $3); 
2434                 }
2435 |       additive_expression MINUS_TK multiplicative_expression
2436                 {
2437                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2438                                     $1, $3); 
2439                 }
2440 |       additive_expression PLUS_TK error
2441                 {yyerror ("Missing term"); RECOVER;}
2442 |       additive_expression MINUS_TK error
2443                 {yyerror ("Missing term"); RECOVER;}
2444 ;
2445
2446 shift_expression:
2447         additive_expression
2448 |       shift_expression LS_TK additive_expression
2449                 {
2450                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2451                                     $1, $3); 
2452                 }
2453 |       shift_expression SRS_TK additive_expression
2454                 {
2455                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2456                                     $1, $3); 
2457                 }
2458 |       shift_expression ZRS_TK additive_expression
2459                 {
2460                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2461                                     $1, $3); 
2462                 }
2463 |       shift_expression LS_TK error
2464                 {yyerror ("Missing term"); RECOVER;}
2465 |       shift_expression SRS_TK error
2466                 {yyerror ("Missing term"); RECOVER;}
2467 |       shift_expression ZRS_TK error
2468                 {yyerror ("Missing term"); RECOVER;}
2469 ;
2470
2471 relational_expression:
2472         shift_expression
2473 |       relational_expression LT_TK shift_expression
2474                 {
2475                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2476                                     $1, $3); 
2477                 }
2478 |       relational_expression GT_TK shift_expression
2479                 {
2480                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2481                                     $1, $3); 
2482                 }
2483 |       relational_expression LTE_TK shift_expression
2484                 {
2485                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2486                                     $1, $3); 
2487                 }
2488 |       relational_expression GTE_TK shift_expression
2489                 {
2490                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2491                                     $1, $3); 
2492                 }
2493 |       relational_expression INSTANCEOF_TK reference_type
2494                 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2495 |       relational_expression LT_TK error
2496                 {yyerror ("Missing term"); RECOVER;}
2497 |       relational_expression GT_TK error
2498                 {yyerror ("Missing term"); RECOVER;}
2499 |       relational_expression LTE_TK error
2500                 {yyerror ("Missing term"); RECOVER;}
2501 |       relational_expression GTE_TK error
2502                 {yyerror ("Missing term"); RECOVER;}
2503 |       relational_expression INSTANCEOF_TK error
2504                 {yyerror ("Invalid reference type"); RECOVER;}
2505 ;
2506
2507 equality_expression:
2508         relational_expression
2509 |       equality_expression EQ_TK relational_expression
2510                 {
2511                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2512                                     $1, $3); 
2513                 }
2514 |       equality_expression NEQ_TK relational_expression
2515                 {
2516                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2517                                     $1, $3); 
2518                 }
2519 |       equality_expression EQ_TK error
2520                 {yyerror ("Missing term"); RECOVER;}
2521 |       equality_expression NEQ_TK error
2522                 {yyerror ("Missing term"); RECOVER;}
2523 ;
2524
2525 and_expression:
2526         equality_expression
2527 |       and_expression AND_TK equality_expression
2528                 {
2529                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2530                                     $1, $3); 
2531                 }
2532 |       and_expression AND_TK error
2533                 {yyerror ("Missing term"); RECOVER;}
2534 ;
2535
2536 exclusive_or_expression:
2537         and_expression
2538 |       exclusive_or_expression XOR_TK and_expression
2539                 {
2540                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2541                                     $1, $3); 
2542                 }
2543 |       exclusive_or_expression XOR_TK error
2544                 {yyerror ("Missing term"); RECOVER;}
2545 ;
2546
2547 inclusive_or_expression:
2548         exclusive_or_expression
2549 |       inclusive_or_expression OR_TK exclusive_or_expression
2550                 {
2551                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2552                                     $1, $3); 
2553                 }
2554 |       inclusive_or_expression OR_TK error
2555                 {yyerror ("Missing term"); RECOVER;}
2556 ;
2557
2558 conditional_and_expression:
2559         inclusive_or_expression
2560 |       conditional_and_expression BOOL_AND_TK inclusive_or_expression
2561                 {
2562                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2563                                     $1, $3); 
2564                 }
2565 |       conditional_and_expression BOOL_AND_TK error
2566                 {yyerror ("Missing term"); RECOVER;}
2567 ;
2568
2569 conditional_or_expression:
2570         conditional_and_expression
2571 |       conditional_or_expression BOOL_OR_TK conditional_and_expression
2572                 {
2573                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2574                                     $1, $3); 
2575                 }
2576 |       conditional_or_expression BOOL_OR_TK error
2577                 {yyerror ("Missing term"); RECOVER;}
2578 ;
2579
2580 conditional_expression:         /* Error handling here is weak */
2581         conditional_or_expression
2582 |       conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2583                 {
2584                   $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2585                   EXPR_WFL_LINECOL ($$) = $2.location;
2586                 }
2587 |       conditional_or_expression REL_QM_TK REL_CL_TK error
2588                 {
2589                   YYERROR_NOW;
2590                   yyerror ("Missing term");
2591                   DRECOVER (1);
2592                 }
2593 |       conditional_or_expression REL_QM_TK error
2594                 {yyerror ("Missing term"); DRECOVER (2);}
2595 |       conditional_or_expression REL_QM_TK expression REL_CL_TK error
2596                 {yyerror ("Missing term"); DRECOVER (3);}
2597 ;
2598
2599 assignment_expression:
2600         conditional_expression
2601 |       assignment
2602 ;
2603
2604 assignment:
2605         left_hand_side assignment_operator assignment_expression
2606                 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2607 |       left_hand_side assignment_operator error
2608                 {
2609                   YYNOT_TWICE yyerror ("Missing term");
2610                   DRECOVER (assign);
2611                 }
2612 ;
2613
2614 left_hand_side:
2615         name
2616 |       field_access
2617 |       array_access
2618 ;
2619
2620 assignment_operator:
2621         ASSIGN_ANY_TK
2622 |       ASSIGN_TK
2623 ;
2624
2625 expression:
2626         assignment_expression
2627 ;
2628
2629 constant_expression:
2630         expression
2631 ;
2632
2633 %%
2634
2635 /* Helper function to retrieve an OSB count. Should be used when the
2636    `dims:' rule is being used.  */
2637
2638 static int
2639 pop_current_osb (ctxp)
2640      struct parser_ctxt *ctxp;
2641 {
2642   int to_return;
2643
2644   if (ctxp->osb_depth < 0)
2645     abort ();
2646   
2647   to_return = CURRENT_OSB (ctxp);
2648   ctxp->osb_depth--;
2649   
2650   return to_return;
2651 }
2652
2653 \f
2654
2655 /* This section of the code deal with save/restoring parser contexts.
2656    Add mode documentation here. FIXME */
2657
2658 /* Helper function. Create a new parser context. With
2659    COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2660    context is copied, otherwise, the new context is zeroed. The newly
2661    created context becomes the current one.  */
2662
2663 static void
2664 create_new_parser_context (copy_from_previous)
2665     int copy_from_previous;
2666 {
2667   struct parser_ctxt *new;
2668
2669   new =  (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2670   if (copy_from_previous)
2671     {
2672       memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2673       new->saved_data_ctx = 1;
2674     }
2675   else
2676     memset ((PTR) new, 0, sizeof (struct parser_ctxt));
2677       
2678   new->next = ctxp;
2679   ctxp = new;
2680 }
2681
2682 /* Create a new parser context and make it the current one. */
2683
2684 void
2685 java_push_parser_context ()
2686 {
2687   create_new_parser_context (0);
2688 }  
2689
2690 void 
2691 java_pop_parser_context (generate)
2692      int generate;
2693 {
2694   tree current;
2695   struct parser_ctxt *toFree, *next;
2696
2697   if (!ctxp)
2698     return;
2699
2700   toFree = ctxp;
2701   next = ctxp->next;
2702   if (next)
2703     {
2704       lineno = ctxp->lineno;
2705       current_class = ctxp->class_type;
2706     }
2707
2708   /* If the old and new lexers differ, then free the old one.  */
2709   if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2710     java_destroy_lexer (ctxp->lexer);
2711
2712   /* Set the single import class file flag to 0 for the current list
2713      of imported things */
2714   for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2715     IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2716
2717   /* And restore those of the previous context */
2718   if ((ctxp = next))            /* Assignment is really meant here */
2719     for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2720       IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2721   
2722   /* If we pushed a context to parse a class intended to be generated,
2723      we keep it so we can remember the class. What we could actually
2724      do is to just update a list of class names.  */
2725   if (generate)
2726     {
2727       toFree->next = ctxp_for_generation;
2728       ctxp_for_generation = toFree;
2729     }
2730   else
2731     free (toFree);
2732 }
2733
2734 /* Create a parser context for the use of saving some global
2735    variables.  */
2736
2737 void
2738 java_parser_context_save_global ()
2739 {
2740   if (!ctxp)
2741     {
2742       java_push_parser_context ();
2743       ctxp->saved_data_ctx = 1;
2744     }
2745
2746   /* If this context already stores data, create a new one suitable
2747      for data storage. */
2748   else if (ctxp->saved_data)
2749     create_new_parser_context (1);
2750
2751   ctxp->lineno = lineno;
2752   ctxp->class_type = current_class;
2753   ctxp->filename = input_filename;
2754   ctxp->function_decl = current_function_decl;
2755   ctxp->saved_data = 1;
2756 }
2757
2758 /* Restore some global variables from the previous context. Make the
2759    previous context the current one.  */
2760
2761 void
2762 java_parser_context_restore_global ()
2763 {
2764   lineno = ctxp->lineno;
2765   current_class = ctxp->class_type;
2766   input_filename = ctxp->filename;
2767   if (wfl_operator)
2768     {
2769       tree s;
2770       BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2771       EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2772     }
2773   current_function_decl = ctxp->function_decl;
2774   ctxp->saved_data = 0;
2775   if (ctxp->saved_data_ctx)
2776     java_pop_parser_context (0);
2777 }
2778
2779 /* Suspend vital data for the current class/function being parsed so
2780    that an other class can be parsed. Used to let local/anonymous
2781    classes be parsed.  */
2782
2783 static void
2784 java_parser_context_suspend ()
2785 {
2786   /* This makes debugging through java_debug_context easier */
2787   static const char *const name = "<inner buffer context>";
2788
2789   /* Duplicate the previous context, use it to save the globals we're
2790      interested in */
2791   create_new_parser_context (1);
2792   ctxp->function_decl = current_function_decl;
2793   ctxp->class_type = current_class;
2794
2795   /* Then create a new context which inherits all data from the
2796      previous one. This will be the new current context  */
2797   create_new_parser_context (1);
2798
2799   /* Help debugging */
2800   ctxp->next->filename = name;
2801 }
2802
2803 /* Resume vital data for the current class/function being parsed so
2804    that an other class can be parsed. Used to let local/anonymous
2805    classes be parsed.  The trick is the data storing file position
2806    informations must be restored to their current value, so parsing
2807    can resume as if no context was ever saved. */
2808
2809 static void
2810 java_parser_context_resume ()
2811 {
2812   struct parser_ctxt *old = ctxp;             /* This one is to be discarded */
2813   struct parser_ctxt *saver = old->next;      /* This one contain saved info */
2814   struct parser_ctxt *restored = saver->next; /* This one is the old current */
2815
2816   /* We need to inherit the list of classes to complete/generate */
2817   restored->classd_list = old->classd_list;
2818   restored->class_list = old->class_list;
2819
2820   /* Restore the current class and function from the saver */
2821   current_class = saver->class_type;
2822   current_function_decl = saver->function_decl;
2823
2824   /* Retrive the restored context */
2825   ctxp = restored;
2826
2827   /* Re-installed the data for the parsing to carry on */
2828   memcpy (&ctxp->marker_begining, &old->marker_begining, 
2829           (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2830
2831   /* Buffer context can now be discarded */
2832   free (saver);
2833   free (old);
2834 }
2835
2836 /* Add a new anchor node to which all statement(s) initializing static
2837    and non static initialized upon declaration field(s) will be
2838    linked.  */
2839
2840 static void
2841 java_parser_context_push_initialized_field ()
2842 {
2843   tree node;
2844
2845   node = build_tree_list (NULL_TREE, NULL_TREE);
2846   TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2847   CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2848
2849   node = build_tree_list (NULL_TREE, NULL_TREE);
2850   TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2851   CPC_INITIALIZER_LIST (ctxp) = node;
2852
2853   node = build_tree_list (NULL_TREE, NULL_TREE);
2854   TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2855   CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2856 }
2857
2858 /* Pop the lists of initialized field. If this lists aren't empty,
2859    remember them so we can use it to create and populate the finit$
2860    or <clinit> functions. */
2861
2862 static void
2863 java_parser_context_pop_initialized_field ()
2864 {
2865   tree stmts;
2866   tree class_type = TREE_TYPE (GET_CPC ());
2867
2868   if (CPC_INITIALIZER_LIST (ctxp))
2869     {
2870       stmts = CPC_INITIALIZER_STMT (ctxp);
2871       CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2872       if (stmts && !java_error_count)
2873         TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2874     }
2875
2876   if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2877     {
2878       stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2879       CPC_STATIC_INITIALIZER_LIST (ctxp) = 
2880         TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2881       /* Keep initialization in order to enforce 8.5 */
2882       if (stmts && !java_error_count)
2883         TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2884     }
2885
2886   /* JDK 1.1 instance initializers */
2887   if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2888     {
2889       stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2890       CPC_INSTANCE_INITIALIZER_LIST (ctxp) = 
2891         TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2892       if (stmts && !java_error_count)
2893         TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2894     }
2895 }
2896
2897 static tree
2898 reorder_static_initialized (list)
2899      tree list;
2900 {
2901   /* We have to keep things in order. The alias initializer have to
2902      come first, then the initialized regular field, in reverse to
2903      keep them in lexical order. */
2904   tree marker, previous = NULL_TREE;
2905   for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2906     if (TREE_CODE (marker) == TREE_LIST 
2907         && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2908       break;
2909   
2910   /* No static initialized, the list is fine as is */
2911   if (!previous)
2912     list = TREE_CHAIN (marker);
2913
2914   /* No marker? reverse the whole list */
2915   else if (!marker)
2916     list = nreverse (list);
2917
2918   /* Otherwise, reverse what's after the marker and the new reordered
2919      sublist will replace the marker. */
2920   else
2921     {
2922       TREE_CHAIN (previous) = NULL_TREE;
2923       list = nreverse (list);
2924       list = chainon (TREE_CHAIN (marker), list);
2925     }
2926   return list;
2927 }
2928
2929 /* Helper functions to dump the parser context stack.  */
2930
2931 #define TAB_CONTEXT(C) \
2932   {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2933
2934 static void
2935 java_debug_context_do (tab)
2936      int tab;
2937 {
2938   struct parser_ctxt *copy = ctxp;
2939   while (copy)
2940     {
2941       TAB_CONTEXT (tab);
2942       fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2943       TAB_CONTEXT (tab);
2944       fprintf (stderr, "filename: %s\n", copy->filename);
2945       TAB_CONTEXT (tab);
2946       fprintf (stderr, "lineno: %d\n", copy->lineno);
2947       TAB_CONTEXT (tab);
2948       fprintf (stderr, "package: %s\n",
2949                (copy->package ? 
2950                 IDENTIFIER_POINTER (copy->package) : "<none>"));
2951       TAB_CONTEXT (tab);
2952       fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2953       TAB_CONTEXT (tab);
2954       fprintf (stderr, "saved data: %d\n", copy->saved_data);
2955       copy = copy->next;
2956       tab += 2;
2957     }
2958 }
2959
2960 /* Dump the stacked up parser contexts. Intended to be called from a
2961    debugger.  */
2962
2963 void
2964 java_debug_context ()
2965 {
2966   java_debug_context_do (0);
2967 }
2968
2969 \f
2970
2971 /* Flag for the error report routine to issue the error the first time
2972    it's called (overriding the default behavior which is to drop the
2973    first invocation and honor the second one, taking advantage of a
2974    richer context.  */
2975 static int force_error = 0;
2976
2977 /* Reporting an constructor invocation error.  */
2978 static void
2979 parse_ctor_invocation_error ()
2980 {
2981   if (DECL_CONSTRUCTOR_P (current_function_decl))
2982     yyerror ("Constructor invocation must be first thing in a constructor"); 
2983   else
2984     yyerror ("Only constructors can invoke constructors");
2985 }
2986
2987 /* Reporting JDK1.1 features not implemented.  */
2988
2989 static tree
2990 parse_jdk1_1_error (msg)
2991     const char *msg;
2992 {
2993   sorry (": `%s' JDK1.1(TM) feature", msg);
2994   java_error_count++;
2995   return empty_stmt_node;
2996 }
2997
2998 static int do_warning = 0;
2999
3000 void
3001 yyerror (msg)
3002      const char *msg;
3003 {
3004   static java_lc elc;
3005   static int  prev_lineno;
3006   static const char *prev_msg;
3007
3008   int save_lineno;
3009   char *remainder, *code_from_source;
3010   
3011   if (!force_error && prev_lineno == lineno)
3012     return;
3013
3014   /* Save current error location but report latter, when the context is
3015      richer.  */
3016   if (ctxp->java_error_flag == 0)
3017     {
3018       ctxp->java_error_flag = 1;
3019       elc = ctxp->elc;
3020       /* Do something to use the previous line if we're reaching the
3021          end of the file... */
3022 #ifdef VERBOSE_SKELETON
3023       printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
3024 #endif
3025       return;
3026     }
3027
3028   /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3029   if (!force_error && msg == prev_msg && prev_lineno == elc.line)
3030     return;
3031
3032   ctxp->java_error_flag = 0;
3033   if (do_warning)
3034     java_warning_count++;
3035   else
3036     java_error_count++;
3037   
3038   if (elc.col == 0 && msg && msg[1] == ';')
3039     {
3040       elc.col  = ctxp->p_line->char_col-1;
3041       elc.line = ctxp->p_line->lineno;
3042     }
3043
3044   save_lineno = lineno;
3045   prev_lineno = lineno = elc.line;
3046   prev_msg = msg;
3047
3048   code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3049   obstack_grow0 (&temporary_obstack, 
3050                  code_from_source, strlen (code_from_source));
3051   remainder = obstack_finish (&temporary_obstack);
3052   if (do_warning)
3053     warning ("%s.\n%s", msg, remainder);
3054   else
3055     error ("%s.\n%s", msg, remainder);
3056
3057   /* This allow us to cheaply avoid an extra 'Invalid expression
3058      statement' error report when errors have been already reported on
3059      the same line. This occurs when we report an error but don't have
3060      a synchronization point other than ';', which
3061      expression_statement is the only one to take care of.  */
3062   ctxp->prevent_ese = lineno = save_lineno;
3063 }
3064
3065 static void
3066 issue_warning_error_from_context (cl, msg, ap)
3067      tree cl;
3068      const char *msg;
3069      va_list ap;
3070 {
3071   const char *saved, *saved_input_filename;
3072   char buffer [4096];
3073   vsprintf (buffer, msg, ap);
3074   force_error = 1;
3075
3076   ctxp->elc.line = EXPR_WFL_LINENO (cl);
3077   ctxp->elc.col  = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 : 
3078                     (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3079
3080   /* We have a CL, that's a good reason for using it if it contains data */
3081   saved = ctxp->filename;
3082   if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3083     ctxp->filename = EXPR_WFL_FILENAME (cl);
3084   saved_input_filename = input_filename;
3085   input_filename = ctxp->filename;
3086   java_error (NULL);
3087   java_error (buffer);
3088   ctxp->filename = saved;
3089   input_filename = saved_input_filename;
3090   force_error = 0;
3091 }
3092
3093 /* Issue an error message at a current source line CL */
3094
3095 void
3096 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3097 {
3098   VA_OPEN (ap, msg);
3099   VA_FIXEDARG (ap, tree, cl);
3100   VA_FIXEDARG (ap, const char *, msg);
3101   issue_warning_error_from_context (cl, msg, ap);
3102   VA_CLOSE (ap);
3103 }
3104
3105 /* Issue a warning at a current source line CL */
3106
3107 static void
3108 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3109 {
3110   VA_OPEN (ap, msg);
3111   VA_FIXEDARG (ap, tree, cl);
3112   VA_FIXEDARG (ap, const char *, msg);
3113
3114   force_error = do_warning = 1;
3115   issue_warning_error_from_context (cl, msg, ap);
3116   do_warning = force_error = 0;
3117   VA_CLOSE (ap);
3118 }
3119
3120 static tree
3121 find_expr_with_wfl (node)
3122      tree node;
3123 {
3124   while (node)
3125     {
3126       char code;
3127       tree to_return;
3128
3129       switch (TREE_CODE (node))
3130         {
3131         case BLOCK:
3132           node = BLOCK_EXPR_BODY (node);
3133           continue;
3134
3135         case COMPOUND_EXPR:
3136           to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3137           if (to_return)
3138             return to_return;
3139           node = TREE_OPERAND (node, 1);
3140           continue;
3141
3142         case LOOP_EXPR:
3143           node = TREE_OPERAND (node, 0);
3144           continue;
3145           
3146         case LABELED_BLOCK_EXPR:
3147           node = TREE_OPERAND (node, 1);
3148           continue;
3149
3150         default:
3151           code = TREE_CODE_CLASS (TREE_CODE (node));
3152           if (((code == '1') || (code == '2') || (code == 'e'))
3153               && EXPR_WFL_LINECOL (node))
3154             return node;
3155           return NULL_TREE;
3156         }
3157     }
3158   return NULL_TREE;
3159 }
3160
3161 /* Issue a missing return statement error. Uses METHOD to figure the
3162    last line of the method the error occurs in.  */
3163
3164 static void
3165 missing_return_error (method)
3166      tree method;
3167 {
3168   EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3169   parse_error_context (wfl_operator, "Missing return statement");
3170 }
3171
3172 /* Issue an unreachable statement error. From NODE, find the next
3173    statement to report appropriately.  */
3174 static void
3175 unreachable_stmt_error (node)
3176      tree node;
3177 {
3178   /* Browse node to find the next expression node that has a WFL. Use
3179      the location to report the error */
3180   if (TREE_CODE (node) == COMPOUND_EXPR)
3181     node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3182   else
3183     node = find_expr_with_wfl (node);
3184
3185   if (node)
3186     {
3187       EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3188       parse_error_context (wfl_operator, "Unreachable statement");
3189     }
3190   else
3191     abort ();
3192 }
3193
3194 int
3195 java_report_errors ()
3196 {
3197   if (java_error_count)
3198     fprintf (stderr, "%d error%s", 
3199              java_error_count, (java_error_count == 1 ? "" : "s"));
3200   if (java_warning_count)
3201     fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3202              java_warning_count, (java_warning_count == 1 ? "" : "s"));
3203   if (java_error_count || java_warning_count)
3204     putc ('\n', stderr);
3205   return java_error_count;
3206 }
3207
3208 static char *
3209 java_accstring_lookup (flags)
3210      int flags;
3211 {
3212   static char buffer [80];
3213 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3214
3215   /* Access modifier looked-up first for easier report on forbidden
3216      access. */
3217   if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3218   if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3219   if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3220   if (flags & ACC_STATIC) COPY_RETURN ("static");
3221   if (flags & ACC_FINAL) COPY_RETURN ("final");
3222   if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3223   if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3224   if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3225   if (flags & ACC_NATIVE) COPY_RETURN ("native");
3226   if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3227   if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3228
3229   buffer [0] = '\0';
3230   return buffer;
3231 #undef COPY_RETURN
3232 }
3233
3234 /* Issuing error messages upon redefinition of classes, interfaces or
3235    variables. */
3236
3237 static void
3238 classitf_redefinition_error (context, id, decl, cl)
3239      const char *context;
3240      tree id, decl, cl;
3241 {
3242   parse_error_context (cl, "%s `%s' already defined in %s:%d", 
3243                        context, IDENTIFIER_POINTER (id), 
3244                        DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3245   /* Here we should point out where its redefined. It's a unicode. FIXME */
3246 }
3247
3248 static void
3249 variable_redefinition_error (context, name, type, line)
3250      tree context, name, type;
3251      int line;
3252 {
3253   const char *type_name;
3254
3255   /* Figure a proper name for type. We might haven't resolved it */
3256   if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3257     type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3258   else
3259     type_name = lang_printable_name (type, 0);
3260
3261   parse_error_context (context,
3262                        "Variable `%s' is already defined in this method and was declared `%s %s' at line %d", 
3263                        IDENTIFIER_POINTER (name),
3264                        type_name, IDENTIFIER_POINTER (name), line);
3265 }
3266
3267 /* If ANAME is terminated with `[]', it indicates an array. This
3268    function returns the number of `[]' found and if this number is
3269    greater than zero, it extracts the array type name and places it in
3270    the node pointed to by TRIMMED unless TRIMMED is null.  */
3271
3272 static int
3273 build_type_name_from_array_name (aname, trimmed)
3274      tree aname;
3275      tree *trimmed;
3276 {
3277   const char *name = IDENTIFIER_POINTER (aname);
3278   int len = IDENTIFIER_LENGTH (aname);
3279   int array_dims;
3280
3281   STRING_STRIP_BRACKETS (name, len, array_dims);
3282
3283   if (array_dims && trimmed)
3284     *trimmed = get_identifier_with_length (name, len);
3285
3286   return array_dims;
3287 }
3288
3289 static tree
3290 build_array_from_name (type, type_wfl, name, ret_name)
3291      tree type, type_wfl, name, *ret_name;
3292 {
3293   int more_dims = 0;
3294
3295   /* Eventually get more dims */
3296   more_dims = build_type_name_from_array_name (name, &name);
3297   
3298   /* If we have, then craft a new type for this variable */
3299   if (more_dims)
3300     {
3301       tree save = type;
3302
3303       /* If we have a pointer, use its type */
3304       if (TREE_CODE (type) == POINTER_TYPE)
3305         type = TREE_TYPE (type);
3306
3307       /* Building the first dimension of a primitive type uses this
3308          function */
3309       if (JPRIMITIVE_TYPE_P (type))
3310         {
3311           type = build_java_array_type (type, -1);
3312           more_dims--;
3313         }
3314       /* Otherwise, if we have a WFL for this type, use it (the type
3315          is already an array on an unresolved type, and we just keep
3316          on adding dimensions) */
3317       else if (type_wfl)
3318         {
3319           type = type_wfl;
3320           more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3321                                                         NULL);
3322         }
3323
3324       /* Add all the dimensions */
3325       while (more_dims--)
3326         type = build_unresolved_array_type (type);
3327
3328       /* The type may have been incomplete in the first place */
3329       if (type_wfl)
3330         type = obtain_incomplete_type (type);
3331     }
3332
3333   if (ret_name)
3334     *ret_name = name;
3335   return type;
3336 }
3337
3338 /* Build something that the type identifier resolver will identify as
3339    being an array to an unresolved type. TYPE_WFL is a WFL on a
3340    identifier. */
3341
3342 static tree
3343 build_unresolved_array_type (type_or_wfl)
3344      tree type_or_wfl;
3345 {
3346   const char *ptr;
3347   tree wfl;
3348
3349   /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3350      just create a array type */
3351   if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3352     return build_java_array_type (type_or_wfl, -1);
3353
3354   obstack_grow (&temporary_obstack,
3355                  IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3356                  IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3357   obstack_grow0 (&temporary_obstack, "[]", 2);
3358   ptr = obstack_finish (&temporary_obstack);
3359   wfl = build_expr_wfl (get_identifier (ptr),
3360                         EXPR_WFL_FILENAME (type_or_wfl),
3361                         EXPR_WFL_LINENO (type_or_wfl),
3362                         EXPR_WFL_COLNO (type_or_wfl));
3363   /* Re-install the existing qualifications so that the type can be
3364      resolved properly. */
3365   EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3366   return wfl;
3367 }
3368
3369 static void
3370 parser_add_interface (class_decl, interface_decl, wfl)
3371      tree class_decl, interface_decl, wfl;
3372 {
3373   if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3374     parse_error_context (wfl, "Interface `%s' repeated",
3375                          IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3376 }
3377
3378 /* Bulk of common class/interface checks. Return 1 if an error was
3379    encountered. TAG is 0 for a class, 1 for an interface.  */
3380
3381 static int
3382 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3383      int is_interface, flags;
3384      tree raw_name, qualified_name, decl, cl;
3385 {
3386   tree node;
3387   int sca = 0;                  /* Static class allowed */
3388   int icaf = 0;                 /* Inner class allowed flags */
3389   int uaaf = CLASS_MODIFIERS;   /* Usually allowed access flags */
3390
3391   if (!quiet_flag)
3392     fprintf (stderr, " %s%s %s", 
3393              (CPC_INNER_P () ? "inner" : ""),
3394              (is_interface ? "interface" : "class"), 
3395              IDENTIFIER_POINTER (qualified_name));
3396
3397   /* Scope of an interface/class type name:
3398        - Can't be imported by a single type import
3399        - Can't already exists in the package */
3400   if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3401       && (node = find_name_in_single_imports (raw_name))
3402       && !CPC_INNER_P ())
3403     {
3404       parse_error_context 
3405         (cl, "%s name `%s' clashes with imported type `%s'",
3406          (is_interface ? "Interface" : "Class"),
3407          IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3408       return 1;
3409     }
3410   if (decl && CLASS_COMPLETE_P (decl))
3411     {
3412       classitf_redefinition_error ((is_interface ? "Interface" : "Class"), 
3413                                    qualified_name, decl, cl);
3414       return 1;
3415     }
3416
3417   if (check_inner_class_redefinition (raw_name, cl))
3418     return 1;
3419
3420   /* If public, file name should match class/interface name, except
3421      when dealing with an inner class */
3422   if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3423     {
3424       const char *f;
3425
3426       /* Contains OS dependent assumption on path separator. FIXME */
3427       for (f = &input_filename [strlen (input_filename)]; 
3428            f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3429            f--)
3430         ;
3431       if (f[0] == '/' || f[0] == DIR_SEPARATOR)
3432         f++;
3433       if (strncmp (IDENTIFIER_POINTER (raw_name), 
3434                    f , IDENTIFIER_LENGTH (raw_name)) ||
3435           f [IDENTIFIER_LENGTH (raw_name)] != '.')
3436         parse_error_context
3437           (cl, "Public %s `%s' must be defined in a file called `%s.java'", 
3438                              (is_interface ? "interface" : "class"),
3439                              IDENTIFIER_POINTER (qualified_name),
3440                              IDENTIFIER_POINTER (raw_name));
3441     }
3442
3443   /* Static classes can be declared only in top level classes. Note:
3444      once static, a inner class is a top level class. */
3445   if (flags & ACC_STATIC)
3446     {
3447       /* Catch the specific error of declaring an class inner class
3448          with no toplevel enclosing class. Prevent check_modifiers from
3449          complaining a second time */
3450       if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3451         {
3452           parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes", 
3453                                IDENTIFIER_POINTER (qualified_name));
3454           sca = ACC_STATIC;
3455         }
3456       /* Else, in the context of a top-level class declaration, let
3457          `check_modifiers' do its job, otherwise, give it a go */
3458       else
3459         sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3460     }
3461
3462   /* Inner classes can be declared private or protected
3463      within their enclosing classes. */
3464   if (CPC_INNER_P ())
3465     {
3466       /* A class which is local to a block can't be public, private,
3467          protected or static. But it is created final, so allow this
3468          one. */
3469       if (current_function_decl)
3470         icaf = sca = uaaf = ACC_FINAL;
3471       else
3472         {
3473           check_modifiers_consistency (flags);
3474           icaf = ACC_PROTECTED;
3475           if (! CLASS_INTERFACE (GET_CPC ()))
3476             icaf |= ACC_PRIVATE;
3477         }
3478     }
3479
3480   if (is_interface) 
3481     {
3482       if (CPC_INNER_P ())
3483         uaaf = INTERFACE_INNER_MODIFIERS;
3484       else
3485         uaaf = INTERFACE_MODIFIERS;
3486       
3487       check_modifiers ("Illegal modifier `%s' for interface declaration", 
3488                        flags, uaaf);
3489     }
3490   else
3491     check_modifiers ((current_function_decl ?
3492                       "Illegal modifier `%s' for local class declaration" :
3493                       "Illegal modifier `%s' for class declaration"),
3494                      flags, uaaf|sca|icaf);
3495   return 0;
3496 }
3497
3498 static void
3499 make_nested_class_name (cpc_list)
3500      tree cpc_list;
3501 {
3502   tree name;
3503
3504   if (!cpc_list)
3505     return;
3506   else
3507     make_nested_class_name (TREE_CHAIN (cpc_list));
3508
3509   /* Pick the qualified name when dealing with the first upmost
3510      enclosing class */
3511   name = (TREE_CHAIN (cpc_list) ? 
3512           TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3513   obstack_grow (&temporary_obstack,
3514                 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3515   obstack_1grow (&temporary_obstack, '$');
3516 }
3517
3518 /* Can't redefine a class already defined in an earlier scope. */
3519
3520 static int
3521 check_inner_class_redefinition (raw_name, cl)
3522      tree raw_name, cl;
3523 {
3524   tree scope_list;
3525
3526   for (scope_list = GET_CPC_LIST (); scope_list; 
3527        scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3528     if (raw_name == GET_CPC_UN_NODE (scope_list))
3529       {
3530         parse_error_context 
3531           (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",
3532            IDENTIFIER_POINTER (raw_name));
3533         return 1;
3534       }
3535   return 0;
3536 }
3537
3538 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. If we fail,
3539    we remember ENCLOSING and SUPER.  */
3540
3541 static tree
3542 resolve_inner_class (circularity_hash, cl, enclosing, super, class_type)
3543      struct hash_table *circularity_hash;
3544      tree cl, *enclosing, *super, class_type;
3545 {
3546   tree local_enclosing = *enclosing;
3547   tree local_super = NULL_TREE;
3548
3549   while (local_enclosing)
3550     {
3551       tree intermediate, decl;
3552
3553       hash_lookup (circularity_hash, 
3554                    (const  hash_table_key) local_enclosing, TRUE, NULL);
3555
3556       if ((decl = find_as_inner_class (local_enclosing, class_type, cl)))
3557         return decl;
3558
3559       intermediate = local_enclosing;
3560       /* Explore enclosing contexts. */
3561       while (INNER_CLASS_DECL_P (intermediate))
3562         {
3563           intermediate = DECL_CONTEXT (intermediate);
3564           if ((decl = find_as_inner_class (intermediate, class_type, cl)))
3565             return decl;
3566         }
3567
3568       /* Now go to the upper classes, bail out if necessary. We will
3569          analyze the returned SUPER and act accordingly (see
3570          do_resolve_class.) */
3571       local_super = CLASSTYPE_SUPER (TREE_TYPE (local_enclosing));
3572       if (!local_super || local_super == object_type_node)
3573         break;
3574
3575       if (TREE_CODE (local_super) == POINTER_TYPE)
3576         local_super = do_resolve_class (NULL, local_super, NULL, NULL);
3577       else
3578         local_super = TYPE_NAME (local_super);
3579
3580       /* We may not have checked for circular inheritance yet, so do so
3581          here to prevent an infinite loop. */
3582       if (hash_lookup (circularity_hash,
3583                        (const hash_table_key) local_super, FALSE, NULL))
3584         {
3585           if (!cl)
3586             cl = lookup_cl (local_enclosing);
3587           
3588           parse_error_context
3589             (cl, "Cyclic inheritance involving %s",
3590              IDENTIFIER_POINTER (DECL_NAME (local_enclosing)));
3591           local_enclosing = NULL_TREE;
3592         }
3593       else
3594         local_enclosing = local_super;
3595     }
3596
3597   /* We failed. Return LOCAL_SUPER and LOCAL_ENCLOSING. */
3598   *super = local_super;
3599   *enclosing = local_enclosing;
3600
3601   return NULL_TREE;
3602 }
3603
3604 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3605    qualified. */
3606
3607 static tree
3608 find_as_inner_class (enclosing, name, cl)
3609      tree enclosing, name, cl;
3610 {
3611   tree qual, to_return;
3612   if (!enclosing)
3613     return NULL_TREE;
3614
3615   name = TYPE_NAME (name);
3616
3617   /* First search: within the scope of `enclosing', search for name */
3618   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3619     qual = EXPR_WFL_QUALIFICATION (cl);
3620   else if (cl)
3621     qual = build_tree_list (cl, NULL_TREE);
3622   else
3623     qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3624   
3625   if ((to_return = find_as_inner_class_do (qual, enclosing)))
3626     return to_return;
3627
3628   /* We're dealing with a qualified name. Try to resolve thing until
3629      we get something that is an enclosing class. */
3630   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3631     {
3632       tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3633
3634       for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl; 
3635            qual = TREE_CHAIN (qual))
3636         {
3637           acc = merge_qualified_name (acc, 
3638                                       EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3639           BUILD_PTR_FROM_NAME (ptr, acc);
3640           decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3641         }
3642
3643       /* A NULL qual and a decl means that the search ended
3644          successfully?!? We have to do something then. FIXME */
3645       
3646       if (decl)
3647         enclosing = decl;
3648       else
3649         qual = EXPR_WFL_QUALIFICATION (cl);
3650     }
3651   /* Otherwise, create a qual for the other part of the resolution. */
3652   else
3653     qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3654   
3655   return find_as_inner_class_do (qual, enclosing);
3656 }
3657
3658 /* We go inside the list of sub classes and try to find a way
3659    through. */
3660
3661 static tree
3662 find_as_inner_class_do (qual, enclosing)
3663      tree qual, enclosing;
3664 {
3665   if (!qual)
3666     return NULL_TREE;
3667
3668   for (; qual && enclosing; qual = TREE_CHAIN (qual))
3669     {
3670       tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3671       tree next_enclosing = NULL_TREE;
3672       tree inner_list;
3673
3674       for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3675            inner_list; inner_list = TREE_CHAIN (inner_list))
3676         {
3677           if (TREE_VALUE (inner_list) == name_to_match)
3678             {
3679               next_enclosing = TREE_PURPOSE (inner_list);
3680               break;
3681             }
3682         }
3683       enclosing = next_enclosing;
3684     }
3685
3686   return (!qual && enclosing ? enclosing : NULL_TREE);
3687 }
3688
3689 /* Reach all inner classes and tie their unqualified name to a
3690    DECL. */
3691
3692 static void
3693 set_nested_class_simple_name_value (outer, set)
3694      tree outer;
3695      int set;
3696 {
3697   tree l;
3698
3699   for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3700     IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ? 
3701                                                 TREE_PURPOSE (l) : NULL_TREE);
3702 }
3703
3704 static void
3705 link_nested_class_to_enclosing ()
3706 {
3707   if (GET_ENCLOSING_CPC ())
3708     {
3709       tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3710       DECL_INNER_CLASS_LIST (enclosing) = 
3711         tree_cons (GET_CPC (), GET_CPC_UN (),
3712                    DECL_INNER_CLASS_LIST (enclosing));
3713     }
3714 }
3715
3716 static tree
3717 maybe_make_nested_class_name (name)
3718      tree name;
3719 {
3720   tree id = NULL_TREE;
3721
3722   if (CPC_INNER_P ())
3723     {
3724       make_nested_class_name (GET_CPC_LIST ());
3725       obstack_grow0 (&temporary_obstack,
3726                      IDENTIFIER_POINTER (name), 
3727                      IDENTIFIER_LENGTH (name));
3728       id = get_identifier (obstack_finish (&temporary_obstack));
3729       if (ctxp->package)
3730         QUALIFIED_P (id) = 1;
3731     }
3732   return id;
3733 }
3734
3735 /* If DECL is NULL, create and push a new DECL, record the current
3736    line CL and do other maintenance things.  */
3737
3738 static tree
3739 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3740      tree decl, raw_name, qualified_name, cl;
3741 {
3742   if (!decl)
3743     decl = push_class (make_class (), qualified_name);
3744
3745   /* Take care of the file and line business */
3746   DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3747   /* If we're emiting xrefs, store the line/col number information */
3748   if (flag_emit_xref)
3749     DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3750   else
3751     DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3752   CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3753   CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3754   CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3755     IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3756
3757   PUSH_CPC (decl, raw_name);
3758   DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3759
3760   /* Link the declaration to the already seen ones */
3761   TREE_CHAIN (decl) = ctxp->class_list;
3762   ctxp->class_list = decl;
3763
3764   /* Create a new nodes in the global lists */
3765   gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3766   all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3767
3768   /* Install a new dependency list element */
3769   create_jdep_list (ctxp);
3770
3771   SOURCE_FRONTEND_DEBUG (("Defining class/interface %s", 
3772                           IDENTIFIER_POINTER (qualified_name)));
3773   return decl;
3774 }
3775
3776 static void
3777 add_superinterfaces (decl, interface_list)
3778      tree decl, interface_list;
3779 {
3780   tree node;
3781   /* Superinterface(s): if present and defined, parser_check_super_interface ()
3782      takes care of ensuring that:
3783        - This is an accessible interface type,
3784        - Circularity detection.
3785    parser_add_interface is then called. If present but not defined,
3786    the check operation is delayed until the super interface gets
3787    defined.  */
3788   for (node = interface_list; node; node = TREE_CHAIN (node))
3789     {
3790       tree current = TREE_PURPOSE (node);
3791       tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3792       if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3793         {
3794           if (!parser_check_super_interface (idecl, decl, current))
3795             parser_add_interface (decl, idecl, current);
3796         }
3797       else
3798         register_incomplete_type (JDEP_INTERFACE,
3799                                   current, decl, NULL_TREE);
3800     }
3801 }
3802
3803 /* Create an interface in pass1 and return its decl. Return the
3804    interface's decl in pass 2.  */
3805
3806 static tree
3807 create_interface (flags, id, super)
3808      int flags;
3809      tree id, super;
3810 {
3811   tree raw_name = EXPR_WFL_NODE (id);
3812   tree q_name = parser_qualified_classname (raw_name);
3813   tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3814
3815   /* Certain syntax errors are making SUPER be like ID. Avoid this
3816      case. */
3817   if (ctxp->class_err && id == super)
3818     super = NULL;
3819
3820   EXPR_WFL_NODE (id) = q_name;  /* Keep source location, even if refined. */
3821
3822   /* Basic checks: scope, redefinition, modifiers */ 
3823   if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3824     {
3825       PUSH_ERROR ();
3826       return NULL_TREE;
3827     }
3828
3829   /* Suspend the current parsing context if we're parsing an inner
3830      interface */
3831   if (CPC_INNER_P ())
3832     {
3833       java_parser_context_suspend ();
3834       /* Interface members are public. */
3835       if (CLASS_INTERFACE (GET_CPC ()))
3836         flags |= ACC_PUBLIC;
3837     }
3838
3839   /* Push a new context for (static) initialized upon declaration fields */
3840   java_parser_context_push_initialized_field ();
3841
3842   /* Interface modifiers check
3843        - public/abstract allowed (already done at that point)
3844        - abstract is obsolete (comes first, it's a warning, or should be)
3845        - Can't use twice the same (checked in the modifier rule) */
3846   if ((flags & ACC_ABSTRACT) && flag_redundant)
3847     parse_warning_context 
3848       (MODIFIER_WFL (ABSTRACT_TK),
3849        "Redundant use of `abstract' modifier. Interface `%s' is implicitly abstract", IDENTIFIER_POINTER (raw_name));
3850
3851   /* Create a new decl if DECL is NULL, otherwise fix it */
3852   decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3853
3854   /* Set super info and mark the class a complete */
3855   set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl), 
3856                   object_type_node, ctxp->interface_number);
3857   ctxp->interface_number = 0;
3858   CLASS_COMPLETE_P (decl) = 1;
3859   add_superinterfaces (decl, super);
3860
3861   return decl;
3862 }
3863
3864 /* Anonymous class counter. Will be reset to 1 every time a non
3865    anonymous class gets created. */
3866 static int anonymous_class_counter = 1;
3867
3868 /* Patch anonymous class CLASS, by either extending or implementing
3869    DEP.  */
3870
3871 static void
3872 patch_anonymous_class (type_decl, class_decl, wfl)
3873     tree type_decl, class_decl, wfl;
3874 {
3875   tree class = TREE_TYPE (class_decl);
3876   tree type =  TREE_TYPE (type_decl);
3877   tree binfo = TYPE_BINFO (class);
3878
3879   /* If it's an interface, implement it */
3880   if (CLASS_INTERFACE (type_decl))
3881     {
3882       tree s_binfo;
3883       int length;
3884
3885       if (parser_check_super_interface (type_decl, class_decl, wfl))
3886         return;
3887
3888       s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3889       length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3890       TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3891       TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3892       /* And add the interface */
3893       parser_add_interface (class_decl, type_decl, wfl);
3894     }
3895   /* Otherwise, it's a type we want to extend */
3896   else
3897     {
3898       if (parser_check_super (type_decl, class_decl, wfl))
3899         return;
3900       BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3901     }
3902 }
3903
3904 static tree
3905 create_anonymous_class (location, type_name)
3906     int location;
3907     tree type_name;
3908 {
3909   char buffer [80];
3910   tree super = NULL_TREE, itf = NULL_TREE;
3911   tree id, type_decl, class;
3912
3913   /* The unqualified name of the anonymous class. It's just a number. */
3914   sprintf (buffer, "%d", anonymous_class_counter++);
3915   id = build_wfl_node (get_identifier (buffer));
3916   EXPR_WFL_LINECOL (id) = location;
3917
3918   /* We know about the type to extend/implement. We go ahead */
3919   if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3920     {
3921       /* Create a class which either implements on extends the designated
3922          class. The class bears an innacessible name. */
3923       if (CLASS_INTERFACE (type_decl))
3924         {
3925           /* It's OK to modify it here. It's been already used and
3926              shouldn't be reused */
3927           ctxp->interface_number = 1;
3928           /* Interfaces should presented as a list of WFLs */
3929           itf = build_tree_list (type_name, NULL_TREE);
3930         }
3931       else
3932         super = type_name;
3933     }
3934
3935   class = create_class (ACC_FINAL, id, super, itf);
3936
3937   /* We didn't know anything about the stuff. We register a dependence. */
3938   if (!type_decl)
3939     register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3940
3941   ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3942   return class;
3943 }
3944
3945 /* Create a class in pass1 and return its decl. Return class
3946    interface's decl in pass 2.  */
3947
3948 static tree
3949 create_class (flags, id, super, interfaces)
3950      int flags;
3951      tree id, super, interfaces;
3952 {
3953   tree raw_name = EXPR_WFL_NODE (id);
3954   tree class_id, decl;
3955   tree super_decl_type;
3956
3957   /* Certain syntax errors are making SUPER be like ID. Avoid this
3958      case. */
3959   if (ctxp->class_err && id == super)
3960     super = NULL;
3961
3962   class_id = parser_qualified_classname (raw_name);
3963   decl = IDENTIFIER_CLASS_VALUE (class_id);
3964   EXPR_WFL_NODE (id) = class_id;
3965
3966   /* Basic check: scope, redefinition, modifiers */
3967   if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3968     {
3969       PUSH_ERROR ();
3970       return NULL_TREE;
3971     }
3972   
3973   /* Suspend the current parsing context if we're parsing an inner
3974      class or an anonymous class. */
3975   if (CPC_INNER_P ())
3976     {
3977       java_parser_context_suspend ();
3978       /* Interface members are public. */
3979       if (CLASS_INTERFACE (GET_CPC ()))
3980         flags |= ACC_PUBLIC;
3981     }
3982     
3983   /* Push a new context for (static) initialized upon declaration fields */
3984   java_parser_context_push_initialized_field ();
3985
3986   /* Class modifier check: 
3987        - Allowed modifier (already done at that point)
3988        - abstract AND final forbidden 
3989        - Public classes defined in the correct file */
3990   if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3991     parse_error_context
3992       (id, "Class `%s' can't be declared both abstract and final",
3993        IDENTIFIER_POINTER (raw_name));
3994
3995   /* Create a new decl if DECL is NULL, otherwise fix it */
3996   decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
3997
3998   /* If SUPER exists, use it, otherwise use Object */
3999   if (super)
4000     {
4001       /* Can't extend java.lang.Object */
4002       if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4003         {
4004           parse_error_context (id, "Can't extend `java.lang.Object'");
4005           return NULL_TREE;
4006         }
4007
4008       super_decl_type = 
4009         register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4010     }
4011   else if (TREE_TYPE (decl) != object_type_node)
4012     super_decl_type = object_type_node;
4013   /* We're defining java.lang.Object */
4014   else
4015     super_decl_type = NULL_TREE;
4016
4017   /* A class nested in an interface is implicitly static. */
4018   if (INNER_CLASS_DECL_P (decl)
4019       && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4020     {
4021       flags |= ACC_STATIC;
4022     }
4023
4024   /* Set super info and mark the class as complete. */
4025   set_super_info (flags, TREE_TYPE (decl), super_decl_type, 
4026                   ctxp->interface_number);
4027   ctxp->interface_number = 0;
4028   CLASS_COMPLETE_P (decl) = 1;
4029   add_superinterfaces (decl, interfaces);
4030
4031   /* Add the private this$<n> field, Replicate final locals still in
4032      scope as private final fields mangled like val$<local_name>.
4033      This doesn't not occur for top level (static) inner classes. */
4034   if (PURE_INNER_CLASS_DECL_P (decl))
4035     add_inner_class_fields (decl, current_function_decl);
4036
4037   /* If doing xref, store the location at which the inherited class
4038      (if any) was seen. */
4039   if (flag_emit_xref && super)
4040     DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
4041
4042   /* Eventually sets the @deprecated tag flag */
4043   CHECK_DEPRECATED (decl);
4044
4045   /* Reset the anonymous class counter when declaring non inner classes */
4046   if (!INNER_CLASS_DECL_P (decl))
4047     anonymous_class_counter = 1;
4048
4049   return decl;
4050 }
4051
4052 /* End a class declaration: register the statements used to create
4053    finit$ and <clinit>, pop the current class and resume the prior
4054    parser context if necessary.  */
4055
4056 static void
4057 end_class_declaration (resume)
4058      int resume;
4059 {
4060   /* If an error occurred, context weren't pushed and won't need to be
4061      popped by a resume. */
4062   int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4063
4064   if (GET_CPC () != error_mark_node)
4065     dump_java_tree (TDI_class, GET_CPC ());
4066
4067   java_parser_context_pop_initialized_field ();
4068   POP_CPC ();
4069   if (resume && no_error_occurred)
4070     java_parser_context_resume ();
4071
4072   /* We're ending a class declaration, this is a good time to reset
4073      the interface cout. Note that might have been already done in
4074      create_interface, but if at that time an inner class was being
4075      dealt with, the interface count was reset in a context created
4076      for the sake of handling inner classes declaration. */
4077   ctxp->interface_number = 0;
4078 }
4079
4080 static void
4081 add_inner_class_fields (class_decl, fct_decl)
4082      tree class_decl;
4083      tree fct_decl;
4084 {
4085   tree block, marker, f;
4086
4087   f = add_field (TREE_TYPE (class_decl),
4088                  build_current_thisn (TREE_TYPE (class_decl)),
4089                  build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))), 
4090                  ACC_PRIVATE);
4091   FIELD_THISN (f) = 1;
4092
4093   if (!fct_decl)
4094     return;
4095     
4096   for (block = GET_CURRENT_BLOCK (fct_decl); 
4097        block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4098     {
4099       tree decl;
4100       for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4101         {
4102           tree name, pname;
4103           tree wfl, init, list;
4104           
4105           /* Avoid non final arguments. */
4106           if (!LOCAL_FINAL_P (decl))
4107             continue;
4108           
4109           MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4110           MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4111           wfl = build_wfl_node (name);
4112           init = build_wfl_node (pname);
4113           /* Build an initialization for the field: it will be
4114              initialized by a parameter added to finit$, bearing a
4115              mangled name of the field itself (param$<n>.) The
4116              parameter is provided to finit$ by the constructor
4117              invoking it (hence the constructor will also feature a
4118              hidden parameter, set to the value of the outer context
4119              local at the time the inner class is created.)
4120              
4121              Note: we take into account all possible locals that can
4122              be accessed by the inner class. It's actually not trivial
4123              to minimize these aliases down to the ones really
4124              used. One way to do that would be to expand all regular
4125              methods first, then finit$ to get a picture of what's
4126              used.  It works with the exception that we would have to
4127              go back on all constructor invoked in regular methods to
4128              have their invokation reworked (to include the right amount
4129              of alias initializer parameters.)
4130
4131              The only real way around, I think, is a first pass to
4132              identify locals really used in the inner class. We leave
4133              the flag FIELD_LOCAL_ALIAS_USED around for that future
4134              use.
4135              
4136              On the other hand, it only affect local inner classes,
4137              whose constructors (and finit$ call) will be featuring
4138              unecessary arguments. It's easy for a developper to keep
4139              this number of parameter down by using the `final'
4140              keyword only when necessary. For the time being, we can
4141              issue a warning on unecessary finals. FIXME */
4142           init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl), 
4143                                    wfl, init);
4144
4145           /* Register the field. The TREE_LIST holding the part
4146              initialized/initializer will be marked ARG_FINAL_P so
4147              that the created field can be marked
4148              FIELD_LOCAL_ALIAS. */
4149           list = build_tree_list (wfl, init);
4150           ARG_FINAL_P (list) = 1;
4151           register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4152         }
4153     }
4154
4155   if (!CPC_INITIALIZER_STMT (ctxp))
4156     return;
4157
4158   /* If we ever registered an alias field, insert and marker to
4159      remeber where the list ends. The second part of the list (the one
4160      featuring initialized fields) so it can be later reversed to
4161      enforce 8.5. The marker will be removed during that operation. */
4162   marker = build_tree_list (NULL_TREE, NULL_TREE);
4163   TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4164   SET_CPC_INITIALIZER_STMT (ctxp, marker);
4165 }
4166
4167 /* Can't use lookup_field () since we don't want to load the class and
4168    can't set the CLASS_LOADED_P flag */
4169
4170 static tree
4171 find_field (class, name)
4172      tree class;
4173      tree name;
4174 {
4175   tree decl;
4176   for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4177     {
4178       if (DECL_NAME (decl) == name)
4179         return decl;
4180     }
4181   return NULL_TREE;
4182 }
4183
4184 /* Wrap around lookup_field that doesn't potentially upset the value
4185    of CLASS */
4186
4187 static tree
4188 lookup_field_wrapper (class, name)
4189      tree class, name;
4190 {
4191   tree type = class;
4192   tree decl = NULL_TREE;
4193   java_parser_context_save_global ();
4194
4195   /* Last chance: if we're within the context of an inner class, we
4196      might be trying to access a local variable defined in an outer
4197      context. We try to look for it now. */
4198   if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4199     {
4200       tree new_name;
4201       MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4202       decl = lookup_field (&type, new_name);
4203       if (decl && decl != error_mark_node)
4204         FIELD_LOCAL_ALIAS_USED (decl) = 1;
4205     }
4206   if (!decl || decl == error_mark_node)
4207     {
4208       type = class;
4209       decl = lookup_field (&type, name);
4210     }
4211
4212   java_parser_context_restore_global ();
4213   return decl == error_mark_node ? NULL : decl;
4214 }
4215
4216 /* Find duplicate field within the same class declarations and report
4217    the error. Returns 1 if a duplicated field was found, 0
4218    otherwise.  */
4219
4220 static int
4221 duplicate_declaration_error_p (new_field_name, new_type, cl)
4222      tree new_field_name, new_type, cl;
4223 {
4224   /* This might be modified to work with method decl as well */
4225   tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4226   if (decl)
4227     {
4228       char *t1 = xstrdup (purify_type_name
4229                          ((TREE_CODE (new_type) == POINTER_TYPE 
4230                            && TREE_TYPE (new_type) == NULL_TREE) ?
4231                           IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4232                           lang_printable_name (new_type, 1)));
4233       /* The type may not have been completed by the time we report
4234          the error */
4235       char *t2 = xstrdup (purify_type_name
4236                          ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE 
4237                            && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4238                           IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4239                           lang_printable_name (TREE_TYPE (decl), 1)));
4240       parse_error_context 
4241         (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)", 
4242          t1, IDENTIFIER_POINTER (new_field_name),
4243          t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4244          DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4245       free (t1);
4246       free (t2);
4247       return 1;
4248     }
4249   return 0;
4250 }
4251
4252 /* Field registration routine. If TYPE doesn't exist, field
4253    declarations are linked to the undefined TYPE dependency list, to
4254    be later resolved in java_complete_class () */
4255
4256 static void
4257 register_fields (flags, type, variable_list)
4258      int flags;
4259      tree type, variable_list;
4260 {
4261   tree current, saved_type;
4262   tree class_type = NULL_TREE;
4263   int saved_lineno = lineno;
4264   int must_chain = 0;
4265   tree wfl = NULL_TREE;
4266
4267   if (GET_CPC ())
4268     class_type = TREE_TYPE (GET_CPC ());
4269
4270   if (!class_type || class_type == error_mark_node)
4271     return;
4272
4273   /* If we're adding fields to interfaces, those fields are public,
4274      static, final */
4275   if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4276     {
4277       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4278                                  flags, ACC_PUBLIC, "interface field(s)");
4279       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4280                                  flags, ACC_STATIC, "interface field(s)");
4281       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4282                                  flags, ACC_FINAL, "interface field(s)");
4283       check_modifiers ("Illegal interface member modifier `%s'", flags,
4284                        INTERFACE_FIELD_MODIFIERS);
4285       flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4286     }
4287
4288   /* Obtain a suitable type for resolution, if necessary */
4289   SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4290
4291   /* If TYPE is fully resolved and we don't have a reference, make one */
4292   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4293
4294   for (current = variable_list, saved_type = type; current; 
4295        current = TREE_CHAIN (current), type = saved_type)
4296     {
4297       tree real_type;
4298       tree field_decl;
4299       tree cl = TREE_PURPOSE (current);
4300       tree init = TREE_VALUE (current);
4301       tree current_name = EXPR_WFL_NODE (cl);
4302
4303       /* Can't declare non-final static fields in inner classes */
4304       if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4305           && !(flags & ACC_FINAL))
4306         parse_error_context 
4307           (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4308            IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4309            lang_printable_name (class_type, 0));
4310
4311       /* Process NAME, as it may specify extra dimension(s) for it */
4312       type = build_array_from_name (type, wfl, current_name, &current_name);
4313
4314       /* Type adjustment. We may have just readjusted TYPE because
4315          the variable specified more dimensions. Make sure we have
4316          a reference if we can and don't have one already. Also
4317          change the name if we have an init. */
4318       if (type != saved_type)
4319         {
4320           PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4321           if (init)
4322             EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4323         }
4324
4325       real_type = GET_REAL_TYPE (type);
4326       /* Check for redeclarations */
4327       if (duplicate_declaration_error_p (current_name, real_type, cl))
4328         continue;
4329
4330       /* Set lineno to the line the field was found and create a
4331          declaration for it. Eventually sets the @deprecated tag flag. */
4332       if (flag_emit_xref)
4333         lineno = EXPR_WFL_LINECOL (cl);
4334       else
4335         lineno = EXPR_WFL_LINENO (cl);
4336       field_decl = add_field (class_type, current_name, real_type, flags);
4337       CHECK_DEPRECATED (field_decl);
4338
4339       /* If the field denotes a final instance variable, then we
4340          allocate a LANG_DECL_SPECIFIC part to keep track of its
4341          initialization. We also mark whether the field was
4342          initialized upon its declaration. We don't do that if the
4343          created field is an alias to a final local. */
4344       if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4345         {
4346           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4347           DECL_FIELD_FINAL_WFL (field_decl) = cl;
4348         }
4349
4350       /* If the couple initializer/initialized is marked ARG_FINAL_P,
4351          we mark the created field FIELD_LOCAL_ALIAS, so that we can
4352          hide parameters to this inner class finit$ and
4353          constructors. It also means that the field isn't final per
4354          say. */
4355       if (ARG_FINAL_P (current))
4356         {
4357           FIELD_LOCAL_ALIAS (field_decl) = 1;
4358           FIELD_FINAL (field_decl) = 0;
4359         }
4360       
4361       /* Check if we must chain. */
4362       if (must_chain)
4363         register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4364           
4365       /* If we have an initialization value tied to the field */
4366       if (init)
4367         {
4368           /* The field is declared static */
4369           if (flags & ACC_STATIC)
4370             {
4371               /* We include the field and its initialization part into
4372                  a list used to generate <clinit>. After <clinit> is
4373                  walked, field initializations will be processed and
4374                  fields initialized with known constants will be taken
4375                  out of <clinit> and have their DECL_INITIAL set
4376                  appropriately. */
4377               TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4378               SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4379               if (TREE_OPERAND (init, 1) 
4380                   && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4381                 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4382             }
4383           /* A non-static field declared with an immediate initialization is
4384              to be initialized in <init>, if any.  This field is remembered
4385              to be processed at the time of the generation of <init>. */
4386           else
4387             {
4388               TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4389               SET_CPC_INITIALIZER_STMT (ctxp, init);
4390             }
4391           MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4392           DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4393         }
4394     }
4395   lineno = saved_lineno;
4396 }
4397
4398 /* Generate finit$, using the list of initialized fields to populate
4399    its body. finit$'s parameter(s) list is adjusted to include the
4400    one(s) used to initialized the field(s) caching outer context
4401    local(s).  */
4402
4403 static tree
4404 generate_finit (class_type)
4405      tree class_type;
4406 {
4407   int count = 0;
4408   tree list = TYPE_FINIT_STMT_LIST (class_type);
4409   tree mdecl, current, parms;
4410
4411   parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION, 
4412                                                   class_type, NULL_TREE, 
4413                                                   &count);
4414   CRAFTED_PARAM_LIST_FIXUP (parms);
4415   mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4416                                     finit_identifier_node, parms);
4417   fix_method_argument_names (parms, mdecl);
4418   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4419                        mdecl, NULL_TREE);
4420   DECL_FUNCTION_NAP (mdecl) = count;
4421   start_artificial_method_body (mdecl);
4422
4423   for (current = list; current; current = TREE_CHAIN (current))
4424     java_method_add_stmt (mdecl, 
4425                           build_debugable_stmt (EXPR_WFL_LINECOL (current), 
4426                                                 current));
4427   end_artificial_method_body (mdecl);
4428   return mdecl;
4429 }
4430
4431 /* Generate a function to run the instance initialization code. The
4432    private method is called `instinit$'. Unless we're dealing with an
4433    anonymous class, we determine whether all ctors of CLASS_TYPE
4434    declare a checked exception in their `throws' clause in order to
4435    see whether it's necessary to encapsulate the instance initializer
4436    statements in a try/catch/rethrow sequence.  */
4437
4438 static tree
4439 generate_instinit (class_type)
4440      tree class_type;
4441 {
4442   tree current;
4443   tree compound = NULL_TREE;
4444   tree parms = tree_cons (this_identifier_node,
4445                           build_pointer_type (class_type), end_params_node);
4446   tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4447                                          void_type_node,
4448                                          instinit_identifier_node, parms);
4449
4450   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4451                        mdecl, NULL_TREE);
4452
4453   /* Gather all the statements in a compound */
4454   for (current = TYPE_II_STMT_LIST (class_type); 
4455        current; current = TREE_CHAIN (current))
4456     compound = add_stmt_to_compound (compound, NULL_TREE, current);
4457
4458   /* We need to encapsulate COMPOUND by a try/catch statement to
4459      rethrow exceptions that might occur in the instance initializer.
4460      We do that only if all ctors of CLASS_TYPE are set to catch a
4461      checked exception. This doesn't apply to anonymous classes (since
4462      they don't have declared ctors.) */
4463   if (!ANONYMOUS_CLASS_P (class_type) && 
4464       ctors_unchecked_throws_clause_p (class_type))
4465     {
4466       compound = encapsulate_with_try_catch (0, exception_type_node, compound, 
4467                                              build1 (THROW_EXPR, NULL_TREE,
4468                                                      build_wfl_node (wpv_id)));
4469       DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4470                                                       exception_type_node);
4471     }
4472
4473   start_artificial_method_body (mdecl);
4474   java_method_add_stmt (mdecl, compound);
4475   end_artificial_method_body (mdecl);
4476
4477   return mdecl;
4478 }
4479
4480 /* FIXME */
4481 static tree
4482 build_instinit_invocation (class_type)
4483      tree class_type;
4484 {
4485   tree to_return = NULL_TREE;
4486
4487   if (TYPE_II_STMT_LIST (class_type))
4488     {
4489       tree parm = build_tree_list (NULL_TREE,
4490                                    build_wfl_node (this_identifier_node));
4491       to_return =
4492         build_method_invocation (build_wfl_node (instinit_identifier_node),
4493                                  parm);
4494     }
4495   return to_return;
4496 }
4497
4498 /* Shared accros method_declarator and method_header to remember the
4499    patch stage that was reached during the declaration of the method.
4500    A method DECL is built differently is there is no patch
4501    (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4502    pending on the currently defined method.  */
4503
4504 static int patch_stage;
4505
4506 /* Check the method declaration and add the method to its current
4507    class.  If the argument list is known to contain incomplete types,
4508    the method is partially added and the registration will be resume
4509    once the method arguments resolved. If TYPE is NULL, we're dealing
4510    with a constructor.  */
4511
4512 static tree
4513 method_header (flags, type, mdecl, throws)
4514      int flags;
4515      tree type, mdecl, throws;
4516 {
4517   tree type_wfl = NULL_TREE;
4518   tree meth_name = NULL_TREE;
4519   tree current, orig_arg, this_class = NULL;
4520   tree id, meth;
4521   int saved_lineno;
4522   int constructor_ok = 0, must_chain;
4523   int count;
4524
4525   if (mdecl == error_mark_node)
4526     return error_mark_node;
4527   meth = TREE_VALUE (mdecl);
4528   id = TREE_PURPOSE (mdecl);
4529   
4530   check_modifiers_consistency (flags);
4531
4532   if (GET_CPC ())
4533     this_class = TREE_TYPE (GET_CPC ());
4534
4535   if (!this_class || this_class == error_mark_node)
4536     return NULL_TREE;
4537   
4538   /* There are some forbidden modifiers for an abstract method and its
4539      class must be abstract as well.  */
4540   if (type && (flags & ACC_ABSTRACT))
4541     {
4542       ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4543       ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4544       ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4545       ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4546       ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED, id, "Synchronized");
4547       ABSTRACT_CHECK (flags, ACC_STRICT, id, "Strictfp");
4548       if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4549           && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4550         parse_error_context 
4551           (id, "Class `%s' must be declared abstract to define abstract method `%s'", 
4552            IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4553            IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4554     }
4555
4556   /* Things to be checked when declaring a constructor */
4557   if (!type)
4558     {
4559       int ec = java_error_count;
4560       /* 8.6: Constructor declarations: we might be trying to define a
4561          method without specifying a return type. */
4562       if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4563         parse_error_context 
4564           (id, "Invalid method declaration, return type required");
4565       /* 8.6.3: Constructor modifiers */
4566       else
4567         {
4568           JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4569           JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4570           JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4571           JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4572           JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4573           JCONSTRUCTOR_CHECK (flags, ACC_STRICT, id, "strictfp");
4574         }
4575       /* If we found error here, we don't consider it's OK to tread
4576          the method definition as a constructor, for the rest of this
4577          function */
4578       if (ec == java_error_count)
4579         constructor_ok = 1;
4580     }
4581
4582   /* Method declared within the scope of an interface are implicitly
4583      abstract and public. Conflicts with other erroneously provided
4584      modifiers are checked right after. */
4585
4586   if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4587     {
4588       /* If FLAGS isn't set because of a modifier, turn the
4589          corresponding modifier WFL to NULL so we issue a warning on
4590          the obsolete use of the modifier */
4591       if (!(flags & ACC_PUBLIC))
4592         MODIFIER_WFL (PUBLIC_TK) = NULL;
4593       if (!(flags & ACC_ABSTRACT))
4594         MODIFIER_WFL (ABSTRACT_TK) = NULL;
4595       flags |= ACC_PUBLIC;
4596       flags |= ACC_ABSTRACT;
4597     }
4598
4599   /* Inner class can't declare static methods */
4600   if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4601     {
4602       parse_error_context 
4603         (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4604          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4605          lang_printable_name (this_class, 0));
4606     }
4607
4608   /* Modifiers context reset moved up, so abstract method declaration
4609      modifiers can be later checked.  */
4610
4611   /* Set constructor returned type to void and method name to <init>,
4612      unless we found an error identifier the constructor (in which
4613      case we retain the original name) */
4614   if (!type)
4615     {
4616       type = void_type_node;
4617       if (constructor_ok)
4618         meth_name = init_identifier_node;
4619     }
4620   else
4621     meth_name = EXPR_WFL_NODE (id);
4622
4623   /* Do the returned type resolution and registration if necessary */
4624   SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4625
4626   if (meth_name)
4627     type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4628   EXPR_WFL_NODE (id) = meth_name;
4629   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4630
4631   if (must_chain)
4632     {
4633       patch_stage = JDEP_METHOD_RETURN;
4634       register_incomplete_type (patch_stage, type_wfl, id, type);
4635       TREE_TYPE (meth) = GET_REAL_TYPE (type);
4636     }
4637   else
4638     TREE_TYPE (meth) = type;
4639
4640   saved_lineno = lineno;
4641   /* When defining an abstract or interface method, the curly
4642      bracket at level 1 doesn't exist because there is no function
4643      body */
4644   lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 : 
4645             EXPR_WFL_LINENO (id));
4646
4647   /* Remember the original argument list */
4648   orig_arg = TYPE_ARG_TYPES (meth);
4649
4650   if (patch_stage)              /* includes ret type and/or all args */
4651     {
4652       jdep *jdep;
4653       meth = add_method_1 (this_class, flags, meth_name, meth);
4654       /* Patch for the return type */
4655       if (patch_stage == JDEP_METHOD_RETURN)
4656         {
4657           jdep = CLASSD_LAST (ctxp->classd_list);
4658           JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4659         }
4660       /* This is the stop JDEP. METH allows the function's signature
4661          to be computed. */
4662       register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4663     }
4664   else
4665     meth = add_method (this_class, flags, meth_name, 
4666                        build_java_signature (meth));
4667
4668   /* Remember final parameters */
4669   MARK_FINAL_PARMS (meth, orig_arg);
4670
4671   /* Fix the method argument list so we have the argument name
4672      information */
4673   fix_method_argument_names (orig_arg, meth);
4674
4675   /* Register the parameter number and re-install the current line
4676      number */
4677   DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4678   lineno = saved_lineno;
4679
4680   /* Register exception specified by the `throws' keyword for
4681      resolution and set the method decl appropriate field to the list.
4682      Note: the grammar ensures that what we get here are class
4683      types. */
4684   if (throws)
4685     {
4686       throws = nreverse (throws);
4687       for (current = throws; current; current = TREE_CHAIN (current))
4688         {
4689           register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4690                                     NULL_TREE, NULL_TREE);
4691           JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) = 
4692             &TREE_VALUE (current);
4693         }
4694       DECL_FUNCTION_THROWS (meth) = throws;
4695     }
4696
4697   if (TREE_TYPE (GET_CPC ()) != object_type_node)
4698     DECL_FUNCTION_WFL (meth) = id;
4699
4700   /* Set the flag if we correctly processed a constructor */
4701   if (constructor_ok)
4702     {
4703       DECL_CONSTRUCTOR_P (meth) = 1;
4704       /* Compute and store the number of artificial parameters declared
4705          for this constructor */
4706       for (count = 0, current = TYPE_FIELDS (this_class); current; 
4707            current = TREE_CHAIN (current))
4708         if (FIELD_LOCAL_ALIAS (current))
4709           count++;
4710       DECL_FUNCTION_NAP (meth) = count;
4711     }
4712
4713   /* Eventually set the @deprecated tag flag */
4714   CHECK_DEPRECATED (meth);
4715
4716   /* If doing xref, store column and line number information instead
4717      of the line number only. */
4718   if (flag_emit_xref)
4719     DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4720
4721   return meth;
4722 }
4723
4724 static void
4725 fix_method_argument_names (orig_arg, meth)
4726     tree orig_arg, meth;
4727 {
4728   tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4729   if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4730     {
4731       TREE_PURPOSE (arg) = this_identifier_node;
4732       arg = TREE_CHAIN (arg);
4733     }
4734   while (orig_arg != end_params_node)
4735     {
4736       TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4737       orig_arg = TREE_CHAIN (orig_arg);
4738       arg = TREE_CHAIN (arg);
4739     }
4740 }
4741
4742 /* Complete the method declaration with METHOD_BODY.  */
4743
4744 static void
4745 finish_method_declaration (method_body)
4746      tree method_body;
4747 {
4748   int flags;
4749
4750   if (!current_function_decl)
4751     return;
4752
4753   flags = get_access_flags_from_decl (current_function_decl);
4754
4755   /* 8.4.5 Method Body */
4756   if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4757     {
4758       tree name = DECL_NAME (current_function_decl);
4759       parse_error_context (DECL_FUNCTION_WFL (current_function_decl), 
4760                            "%s method `%s' can't have a body defined",
4761                            (METHOD_NATIVE (current_function_decl) ?
4762                             "Native" : "Abstract"),
4763                            IDENTIFIER_POINTER (name));
4764       method_body = NULL_TREE;
4765     }
4766   else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4767     {
4768       tree name = DECL_NAME (current_function_decl);
4769       parse_error_context
4770         (DECL_FUNCTION_WFL (current_function_decl), 
4771          "Non native and non abstract method `%s' must have a body defined",
4772          IDENTIFIER_POINTER (name));
4773       method_body = NULL_TREE;
4774     }
4775
4776   if (flag_emit_class_files && method_body 
4777       && TREE_CODE (method_body) == NOP_EXPR 
4778       && TREE_TYPE (current_function_decl) 
4779       && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4780     method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4781
4782   BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4783   maybe_absorb_scoping_blocks ();
4784   /* Exit function's body */
4785   exit_block ();
4786   /* Merge last line of the function with first line, directly in the
4787      function decl. It will be used to emit correct debug info. */
4788   if (!flag_emit_xref)
4789     DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4790
4791   /* Since function's argument's list are shared, reset the
4792      ARG_FINAL_P parameter that might have been set on some of this
4793      function parameters. */
4794   UNMARK_FINAL_PARMS (current_function_decl);
4795   
4796   /* So we don't have an irrelevant function declaration context for
4797      the next static block we'll see. */
4798   current_function_decl = NULL_TREE;
4799 }
4800
4801 /* Build a an error message for constructor circularity errors.  */
4802
4803 static char *
4804 constructor_circularity_msg (from, to)
4805      tree from, to;
4806 {
4807   static char string [4096];
4808   char *t = xstrdup (lang_printable_name (from, 0));
4809   sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4810   free (t);
4811   return string;
4812 }
4813
4814 /* Verify a circular call to METH. Return 1 if an error is found, 0
4815    otherwise.  */
4816
4817 static int
4818 verify_constructor_circularity (meth, current)
4819      tree meth, current;
4820 {
4821   static tree list = NULL_TREE;
4822   static int initialized_p;
4823   tree c;
4824
4825   /* If we haven't already registered LIST with the garbage collector,
4826      do so now.  */
4827   if (!initialized_p)
4828     {
4829       ggc_add_tree_root (&list, 1);
4830       initialized_p = 1;
4831     }
4832
4833   for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4834     {
4835       if (TREE_VALUE (c) == meth)
4836         {
4837           char *t;
4838           if (list)
4839             {
4840               tree liste;
4841               list = nreverse (list);
4842               for (liste = list; liste; liste = TREE_CHAIN (liste))
4843                 {
4844                   parse_error_context 
4845                     (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4846                      constructor_circularity_msg
4847                       (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste)))); 
4848                   java_error_count--;
4849                 }
4850             }
4851           t = xstrdup (lang_printable_name (meth, 0));
4852           parse_error_context (TREE_PURPOSE (c), 
4853                                "%s: recursive invocation of constructor `%s'",
4854                                constructor_circularity_msg (current, meth), t);
4855           free (t);
4856           list = NULL_TREE;
4857           return 1;
4858         }
4859     }
4860   for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4861     {
4862       list = tree_cons (c, current, list);
4863       if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4864         return 1;
4865       list = TREE_CHAIN (list);
4866     }
4867   return 0;
4868 }
4869
4870 /* Check modifiers that can be declared but exclusively */
4871
4872 static void
4873 check_modifiers_consistency (flags)
4874      int flags;
4875 {
4876   int acc_count = 0;
4877   tree cl = NULL_TREE;
4878
4879   THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4880   THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4881   THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4882   if (acc_count > 1)
4883     parse_error_context
4884       (cl, "Inconsistent member declaration.  At most one of `public', `private', or `protected' may be specified");
4885
4886   acc_count = 0;
4887   cl = NULL_TREE;
4888   THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4889   THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4890   if (acc_count > 1)
4891     parse_error_context (cl,
4892                          "Inconsistent member declaration.  At most one of `final' or `volatile' may be specified");
4893 }
4894
4895 /* Check the methode header METH for abstract specifics features */
4896
4897 static void
4898 check_abstract_method_header (meth)
4899      tree meth;
4900 {
4901   int flags = get_access_flags_from_decl (meth);
4902
4903   OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4904                               ACC_ABSTRACT, "abstract method",
4905                               IDENTIFIER_POINTER (DECL_NAME (meth)));
4906   OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags, 
4907                               ACC_PUBLIC, "abstract method",
4908                               IDENTIFIER_POINTER (DECL_NAME (meth)));
4909
4910   check_modifiers ("Illegal modifier `%s' for interface method",
4911                   flags, INTERFACE_METHOD_MODIFIERS);
4912 }
4913
4914 /* Create a FUNCTION_TYPE node and start augmenting it with the
4915    declared function arguments. Arguments type that can't be resolved
4916    are left as they are, but the returned node is marked as containing
4917    incomplete types.  */
4918
4919 static tree
4920 method_declarator (id, list)
4921      tree id, list;
4922 {
4923   tree arg_types = NULL_TREE, current, node;
4924   tree meth = make_node (FUNCTION_TYPE);
4925   jdep *jdep;
4926
4927   patch_stage = JDEP_NO_PATCH;
4928
4929   if (GET_CPC () == error_mark_node)
4930     return error_mark_node;
4931
4932   /* If we're dealing with an inner class constructor, we hide the
4933      this$<n> decl in the name field of its parameter declaration.  We
4934      also might have to hide the outer context local alias
4935      initializers. Not done when the class is a toplevel class. */
4936   if (PURE_INNER_CLASS_DECL_P (GET_CPC ()) 
4937       && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4938     {
4939       tree aliases_list, type, thisn;
4940       /* First the aliases, linked to the regular parameters */
4941       aliases_list =
4942         build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION, 
4943                                                 TREE_TYPE (GET_CPC ()),
4944                                                 NULL_TREE, NULL);
4945       list = chainon (nreverse (aliases_list), list);
4946
4947       /* Then this$<n> */
4948       type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4949       thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4950       list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4951                         list);
4952     }
4953   
4954   for (current = list; current; current = TREE_CHAIN (current))
4955     {
4956       int must_chain = 0;
4957       tree wfl_name = TREE_PURPOSE (current);
4958       tree type = TREE_VALUE (current);
4959       tree name = EXPR_WFL_NODE (wfl_name);
4960       tree already, arg_node;
4961       tree type_wfl = NULL_TREE;
4962       tree real_type;
4963
4964       /* Obtain a suitable type for resolution, if necessary */
4965       SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4966
4967       /* Process NAME, as it may specify extra dimension(s) for it */
4968       type = build_array_from_name (type, type_wfl, name, &name);
4969       EXPR_WFL_NODE (wfl_name) = name;
4970
4971       real_type = GET_REAL_TYPE (type);
4972       if (TREE_CODE (real_type) == RECORD_TYPE)
4973         {
4974           real_type = promote_type (real_type);
4975           if (TREE_CODE (type) == TREE_LIST)
4976             TREE_PURPOSE (type) = real_type;
4977         }
4978
4979       /* Check redefinition */
4980       for (already = arg_types; already; already = TREE_CHAIN (already))
4981         if (TREE_PURPOSE (already) == name)
4982           {
4983             parse_error_context
4984               (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4985                IDENTIFIER_POINTER (name),
4986                IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4987             break;
4988           }
4989
4990       /* If we've an incomplete argument type, we know there is a location
4991          to patch when the type get resolved, later.  */
4992       jdep = NULL;
4993       if (must_chain)
4994         {
4995           patch_stage = JDEP_METHOD;
4996           type = register_incomplete_type (patch_stage, 
4997                                            type_wfl, wfl_name, type);
4998           jdep = CLASSD_LAST (ctxp->classd_list);
4999           JDEP_MISC (jdep) = id;
5000         }
5001
5002       /* The argument node: a name and a (possibly) incomplete type.  */
5003       arg_node = build_tree_list (name, real_type);
5004       /* Remeber arguments declared final. */
5005       ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5006       
5007       if (jdep)
5008         JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5009       TREE_CHAIN (arg_node) = arg_types;
5010       arg_types = arg_node;
5011     }
5012   TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5013   node = build_tree_list (id, meth);
5014   return node;
5015 }
5016
5017 static int
5018 unresolved_type_p (wfl, returned)
5019      tree wfl;
5020      tree *returned;
5021      
5022 {
5023   if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5024     {
5025       if (returned)
5026         {
5027           tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5028           if (decl && current_class && (decl == TYPE_NAME (current_class)))
5029             *returned = TREE_TYPE (decl);
5030           else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5031             *returned = TREE_TYPE (GET_CPC ());
5032           else
5033             *returned = NULL_TREE;
5034         }
5035       return 1;
5036     }
5037   if (returned)
5038     *returned = wfl;
5039   return 0;
5040 }
5041
5042 /* From NAME, build a qualified identifier node using the
5043    qualification from the current package definition. */
5044
5045 static tree
5046 parser_qualified_classname (name)
5047      tree name;
5048 {
5049   tree nested_class_name;
5050
5051   if ((nested_class_name = maybe_make_nested_class_name (name)))
5052     return nested_class_name;
5053
5054   if (ctxp->package)
5055     return merge_qualified_name (ctxp->package, name);
5056   else 
5057     return name;
5058 }
5059
5060 /* Called once the type a interface extends is resolved. Returns 0 if
5061    everything is OK.  */
5062
5063 static int
5064 parser_check_super_interface (super_decl, this_decl, this_wfl)
5065      tree super_decl, this_decl, this_wfl;
5066 {
5067   tree super_type = TREE_TYPE (super_decl);
5068
5069   /* Has to be an interface */
5070   if (!CLASS_INTERFACE (super_decl))
5071     {
5072       parse_error_context 
5073         (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
5074          (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5075          IDENTIFIER_POINTER (DECL_NAME (super_decl)),
5076          (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ? 
5077           "interface" : "class"),
5078          IDENTIFIER_POINTER (DECL_NAME (this_decl)));
5079       return 1;
5080     }
5081
5082   /* Check top-level interface access. Inner classes are subject to member 
5083      access rules (6.6.1). */
5084   if (! INNER_CLASS_P (super_type)
5085       && check_pkg_class_access (DECL_NAME (super_decl),
5086                                  lookup_cl (this_decl), true))
5087     return 1;
5088
5089   SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5090                           IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5091                           IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5092   return 0;
5093 }
5094
5095 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5096    0 if everthing is OK.  */
5097
5098 static int
5099 parser_check_super (super_decl, this_decl, wfl)
5100      tree super_decl, this_decl, wfl;
5101 {
5102   tree super_type = TREE_TYPE (super_decl);
5103
5104   /* SUPER should be a CLASS (neither an array nor an interface) */
5105   if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5106     {
5107       parse_error_context 
5108         (wfl, "Class `%s' can't subclass %s `%s'",
5109          IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5110          (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5111          IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5112       return 1;
5113     }
5114
5115   if (CLASS_FINAL (TYPE_NAME (super_type)))
5116     {
5117       parse_error_context (wfl, "Can't subclass final classes: %s",
5118                            IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5119       return 1;
5120     }
5121
5122   /* Check top-level class scope. Inner classes are subject to member access
5123      rules (6.6.1). */
5124   if (! INNER_CLASS_P (super_type)
5125       && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true)))
5126     return 1;
5127   
5128   SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5129                           IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5130                           IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5131   return 0;
5132 }
5133
5134 /* Create a new dependency list and link it (in a LIFO manner) to the
5135    CTXP list of type dependency list.  */
5136
5137 static void
5138 create_jdep_list (ctxp)
5139      struct parser_ctxt *ctxp;
5140 {
5141   jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));      
5142   new->first = new->last = NULL;
5143   new->next = ctxp->classd_list;
5144   ctxp->classd_list = new;
5145 }
5146
5147 static jdeplist *
5148 reverse_jdep_list (ctxp)
5149      struct parser_ctxt *ctxp;
5150 {
5151   register jdeplist *prev = NULL, *current, *next;
5152   for (current = ctxp->classd_list; current; current = next)
5153     {
5154       next = current->next;
5155       current->next = prev;
5156       prev = current;
5157     }
5158   return prev;
5159 }
5160
5161 /* Create a fake pointer based on the ID stored in
5162    TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5163    registered again. */
5164
5165 static tree
5166 obtain_incomplete_type (type_name)
5167      tree type_name;
5168 {
5169   tree ptr = NULL_TREE, name;
5170
5171   if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5172     name = EXPR_WFL_NODE (type_name);
5173   else if (INCOMPLETE_TYPE_P (type_name))
5174     name = TYPE_NAME (type_name);
5175   else
5176     abort ();
5177
5178   BUILD_PTR_FROM_NAME (ptr, name);
5179   layout_type (ptr);
5180
5181   return ptr;
5182 }
5183
5184 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5185    non NULL instead of computing a new fake type based on WFL. The new
5186    dependency is inserted in the current type dependency list, in FIFO
5187    manner.  */
5188
5189 static tree
5190 register_incomplete_type (kind, wfl, decl, ptr)
5191      int kind;
5192      tree wfl, decl, ptr;
5193 {
5194   jdep *new = (jdep *)xmalloc (sizeof (jdep));
5195
5196   if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5197     ptr = obtain_incomplete_type (wfl);
5198
5199   JDEP_KIND (new) = kind;
5200   JDEP_DECL (new) = decl;
5201   JDEP_TO_RESOLVE (new) = ptr;
5202   JDEP_WFL (new) = wfl;
5203   JDEP_CHAIN (new) = NULL;
5204   JDEP_MISC (new) = NULL_TREE;
5205   /* For some dependencies, set the enclosing class of the current
5206      class to be the enclosing context */
5207   if ((kind == JDEP_INTERFACE  || kind == JDEP_ANONYMOUS)
5208       && GET_ENCLOSING_CPC ())
5209     JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5210   else if (kind == JDEP_SUPER)
5211     JDEP_ENCLOSING (new) = (GET_ENCLOSING_CPC () ? 
5212                             TREE_VALUE (GET_ENCLOSING_CPC ()) : NULL_TREE);
5213   else
5214     JDEP_ENCLOSING (new) = GET_CPC ();
5215   JDEP_GET_PATCH (new) = (tree *)NULL;
5216
5217   JDEP_INSERT (ctxp->classd_list, new);
5218
5219   return ptr;
5220 }
5221
5222 /* This checks for circular references with innerclasses. We start
5223    from SOURCE and should never reach TARGET. Extended/implemented
5224    types in SOURCE have their enclosing context checked not to reach
5225    TARGET. When the last enclosing context of SOURCE is reached, its
5226    extended/implemented types are also checked not to reach TARGET.
5227    In case of error, WFL of the offending type is returned; NULL_TREE
5228    otherwise.  */
5229
5230 static tree
5231 check_inner_circular_reference (source, target)
5232      tree source;
5233      tree target;
5234 {
5235   tree basetype_vec = TYPE_BINFO_BASETYPES (source);
5236   tree ctx, cl;
5237   int i;
5238
5239   if (!basetype_vec)
5240     return NULL_TREE;
5241   
5242   for (i = 0; i < TREE_VEC_LENGTH (basetype_vec); i++)
5243     {
5244       tree su = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
5245
5246       if (inherits_from_p (su, target))
5247         return lookup_cl (TYPE_NAME (su));
5248       
5249       for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5250         {
5251           /* An enclosing context shouldn't be TARGET */
5252           if (ctx == TYPE_NAME (target))
5253             return lookup_cl (TYPE_NAME (su));
5254
5255           /* When we reach the enclosing last context, start a check
5256              on it, with the same target */
5257           if (! DECL_CONTEXT (ctx) &&
5258               (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5259             return cl;
5260         }
5261     }
5262   return NULL_TREE;
5263 }
5264
5265 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5266    offending type if a circularity is detected. NULL_TREE is returned
5267    otherwise. TYPE can be an interface or a class.   */
5268
5269 static tree
5270 check_circular_reference (type)
5271      tree type;
5272 {
5273   tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5274   int i;
5275
5276   if (!basetype_vec)
5277     return NULL_TREE;
5278
5279   if (! CLASS_INTERFACE (TYPE_NAME (type)))
5280     {
5281       if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5282         return lookup_cl (TYPE_NAME (type));
5283       return NULL_TREE;
5284     }
5285     
5286   for (i = 0; i < TREE_VEC_LENGTH (basetype_vec); i++)
5287     {
5288       tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5289       if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5290           && interface_of_p (type, BINFO_TYPE (vec_elt)))
5291         return lookup_cl (TYPE_NAME (BINFO_TYPE (vec_elt)));
5292     }
5293   return NULL_TREE;
5294 }
5295
5296 void
5297 java_check_circular_reference ()
5298 {
5299   tree current;
5300   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5301     {
5302       tree type = TREE_TYPE (current);
5303       tree cl;
5304
5305       cl = check_circular_reference (type);
5306       if (! cl)
5307         cl = check_inner_circular_reference (type, type);
5308       if (cl)
5309         parse_error_context (cl, "Cyclic class inheritance%s",
5310                              (cyclic_inheritance_report ?
5311                               cyclic_inheritance_report : ""));
5312     }
5313 }
5314
5315 /* Augment the parameter list PARM with parameters crafted to
5316    initialize outer context locals aliases. Through ARTIFICIAL, a
5317    count is kept of the number of crafted parameters. MODE governs
5318    what eventually gets created: something suitable for a function
5319    creation or a function invocation, either the constructor or
5320    finit$.  */
5321
5322 static tree
5323 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5324     int mode;
5325     tree class_type, parm;
5326     int *artificial;
5327 {
5328   tree field;
5329   tree additional_parms = NULL_TREE;
5330
5331   for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5332     if (FIELD_LOCAL_ALIAS (field))
5333       {
5334         const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5335         tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5336         tree mangled_id;
5337
5338         switch (mode)
5339           {
5340           case AIPL_FUNCTION_DECLARATION:
5341             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id, 
5342                                                          &buffer [4]);
5343             purpose = build_wfl_node (mangled_id);
5344             if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5345               value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5346             else
5347               value = TREE_TYPE (field);
5348             break;
5349
5350           case AIPL_FUNCTION_CREATION:
5351             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5352                                                          &buffer [4]);
5353             value = TREE_TYPE (field);
5354             break;
5355
5356           case AIPL_FUNCTION_FINIT_INVOCATION:
5357             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id, 
5358                                                          &buffer [4]);
5359             /* Now, this is wrong. purpose should always be the NAME
5360                of something and value its matching value (decl, type,
5361                etc...) FIXME -- but there is a lot to fix. */
5362
5363             /* When invoked for this kind of operation, we already
5364                know whether a field is used or not. */
5365             purpose = TREE_TYPE (field);
5366             value = build_wfl_node (mangled_id);
5367             break;
5368
5369           case AIPL_FUNCTION_CTOR_INVOCATION:
5370             /* There are two case: the constructor invokation happends
5371                outside the local inner, in which case, locales from the outer
5372                context are directly used.
5373
5374                Otherwise, we fold to using the alias directly. */
5375             if (class_type == current_class)
5376               value = field;
5377             else
5378               {
5379                 name = get_identifier (&buffer[4]);
5380                 value = IDENTIFIER_LOCAL_VALUE (name);
5381               }
5382             break;
5383           }
5384         additional_parms = tree_cons (purpose, value, additional_parms);
5385         if (artificial)
5386           *artificial +=1;
5387       }
5388   if (additional_parms)
5389     {
5390       if (ANONYMOUS_CLASS_P (class_type) 
5391           && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5392         additional_parms = nreverse (additional_parms);
5393       parm = chainon (additional_parms, parm);
5394     }
5395
5396    return parm;
5397 }
5398
5399 /* Craft a constructor for CLASS_DECL -- what we should do when none
5400    where found. ARGS is non NULL when a special signature must be
5401    enforced. This is the case for anonymous classes.  */
5402
5403 static void
5404 craft_constructor (class_decl, args)
5405      tree class_decl, args;
5406 {
5407   tree class_type = TREE_TYPE (class_decl);
5408   tree parm = NULL_TREE;
5409   int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5410                ACC_PUBLIC : 0);
5411   int i = 0, artificial = 0;
5412   tree decl, ctor_name;
5413   char buffer [80];
5414   
5415   /* The constructor name is <init> unless we're dealing with an
5416      anonymous class, in which case the name will be fixed after having
5417      be expanded. */
5418   if (ANONYMOUS_CLASS_P (class_type))
5419     ctor_name = DECL_NAME (class_decl);
5420   else
5421     ctor_name = init_identifier_node;
5422
5423   /* If we're dealing with an inner class constructor, we hide the
5424      this$<n> decl in the name field of its parameter declaration. */
5425   if (PURE_INNER_CLASS_TYPE_P (class_type))
5426     {
5427       tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5428       parm = tree_cons (build_current_thisn (class_type),
5429                         build_pointer_type (type), parm);
5430
5431       /* Some more arguments to be hidden here. The values of the local
5432          variables of the outer context that the inner class needs to see. */
5433       parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5434                                                      class_type, parm, 
5435                                                      &artificial);
5436     }
5437
5438   /* Then if there are any args to be enforced, enforce them now */
5439   for (; args && args != end_params_node; args = TREE_CHAIN (args))
5440     {
5441       sprintf (buffer, "parm%d", i++);
5442       parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5443     }
5444
5445   CRAFTED_PARAM_LIST_FIXUP (parm);
5446   decl = create_artificial_method (class_type, flags, void_type_node, 
5447                                    ctor_name, parm);
5448   fix_method_argument_names (parm, decl);
5449   /* Now, mark the artificial parameters. */
5450   DECL_FUNCTION_NAP (decl) = artificial;
5451   DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5452 }
5453
5454
5455 /* Fix the constructors. This will be called right after circular
5456    references have been checked. It is necessary to fix constructors
5457    early even if no code generation will take place for that class:
5458    some generated constructor might be required by the class whose
5459    compilation triggered this one to be simply loaded.  */
5460
5461 void
5462 java_fix_constructors ()
5463 {
5464   tree current;
5465
5466   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5467     {
5468       tree class_type = TREE_TYPE (current);
5469       int saw_ctor = 0;
5470       tree decl;
5471
5472       if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5473         continue;
5474
5475       current_class = class_type;
5476       for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5477         {
5478           if (DECL_CONSTRUCTOR_P (decl))
5479             {
5480               fix_constructors (decl);
5481               saw_ctor = 1;
5482             }
5483         }
5484
5485       /* Anonymous class constructor can't be generated that early. */
5486       if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5487         craft_constructor (current, NULL_TREE);
5488     }
5489 }
5490
5491 /* safe_layout_class just makes sure that we can load a class without
5492    disrupting the current_class, input_file, lineno, etc, information
5493    about the class processed currently.  */
5494
5495 void
5496 safe_layout_class (class)
5497      tree class;
5498 {
5499   tree save_current_class = current_class;
5500   const char *save_input_filename = input_filename;
5501   int save_lineno = lineno;
5502
5503   layout_class (class);
5504
5505   current_class = save_current_class;
5506   input_filename = save_input_filename;
5507   lineno = save_lineno;
5508 }
5509
5510 static tree
5511 jdep_resolve_class (dep)
5512      jdep *dep;
5513 {
5514   tree decl;
5515
5516   if (JDEP_RESOLVED_P (dep))
5517     decl = JDEP_RESOLVED_DECL (dep);
5518   else
5519     {
5520       decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5521                             JDEP_DECL (dep), JDEP_WFL (dep));
5522       JDEP_RESOLVED (dep, decl);
5523     }
5524     
5525   if (!decl)
5526     complete_class_report_errors (dep);
5527   else if (PURE_INNER_CLASS_DECL_P (decl))
5528     {
5529       tree inner = TREE_TYPE (decl);
5530       if (! CLASS_LOADED_P (inner))
5531         {
5532           safe_layout_class (inner);
5533           if (TYPE_SIZE (inner) == error_mark_node)
5534             TYPE_SIZE (inner) = NULL_TREE;
5535         }
5536       check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5537     }
5538   return decl;
5539 }
5540
5541 /* Complete unsatisfied class declaration and their dependencies */
5542
5543 void
5544 java_complete_class ()
5545 {
5546   tree cclass;
5547   jdeplist *cclassd;
5548   int error_found;
5549   tree type;
5550
5551   /* Process imports */
5552   process_imports ();
5553
5554   /* Rever things so we have the right order */
5555   ctxp->class_list = nreverse (ctxp->class_list);
5556   ctxp->classd_list = reverse_jdep_list (ctxp);
5557
5558   for (cclassd = ctxp->classd_list, cclass = ctxp->class_list; 
5559        cclass && cclassd; 
5560        cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5561     {
5562       jdep *dep;
5563
5564       /* We keep the compilation unit imports in the class so that
5565          they can be used later to resolve type dependencies that
5566          aren't necessary to solve now. */
5567       TYPE_IMPORT_LIST (TREE_TYPE (cclass)) = ctxp->import_list;
5568       TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (cclass)) = ctxp->import_demand_list;
5569
5570       for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5571         {
5572           tree decl;
5573           if (!(decl = jdep_resolve_class (dep)))
5574             continue;
5575
5576           /* Now it's time to patch */
5577           switch (JDEP_KIND (dep))
5578             {
5579             case JDEP_SUPER:
5580               /* Simply patch super */
5581               if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5582                 continue;
5583               BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO 
5584                 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5585               break;
5586
5587             case JDEP_FIELD:
5588               {
5589                 /* We do part of the job done in add_field */
5590                 tree field_decl = JDEP_DECL (dep);
5591                 tree field_type = TREE_TYPE (decl);
5592                 if (TREE_CODE (field_type) == RECORD_TYPE)
5593                   field_type = promote_type (field_type);
5594                 TREE_TYPE (field_decl) = field_type;
5595                 DECL_ALIGN (field_decl) = 0;
5596                 DECL_USER_ALIGN (field_decl) = 0;
5597                 layout_decl (field_decl, 0);
5598                 SOURCE_FRONTEND_DEBUG 
5599                   (("Completed field/var decl `%s' with `%s'",
5600                     IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5601                     IDENTIFIER_POINTER (DECL_NAME (decl))));
5602                 break;
5603               }
5604             case JDEP_METHOD:   /* We start patching a method */
5605             case JDEP_METHOD_RETURN:
5606               error_found = 0;
5607               while (1)
5608                 {
5609                   if (decl)
5610                     {
5611                       type = TREE_TYPE(decl);
5612                       if (TREE_CODE (type) == RECORD_TYPE)
5613                         type = promote_type (type);
5614                       JDEP_APPLY_PATCH (dep, type);
5615                       SOURCE_FRONTEND_DEBUG 
5616                         (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5617                            "Completing fct `%s' with ret type `%s'":
5618                            "Completing arg `%s' with type `%s'"),
5619                           IDENTIFIER_POINTER (EXPR_WFL_NODE 
5620                                               (JDEP_DECL_WFL (dep))),
5621                           IDENTIFIER_POINTER (DECL_NAME (decl))));
5622                     }
5623                   else
5624                     error_found = 1;
5625                   dep = JDEP_CHAIN (dep);
5626                   if (JDEP_KIND (dep) == JDEP_METHOD_END)
5627                     break;
5628                   else
5629                     decl = jdep_resolve_class (dep);
5630                 }
5631               if (!error_found)
5632                 {
5633                   tree mdecl = JDEP_DECL (dep), signature;
5634                   /* Recompute and reset the signature, check first that
5635                      all types are now defined. If they're not,
5636                      don't build the signature. */
5637                   if (check_method_types_complete (mdecl))
5638                     {
5639                       signature = build_java_signature (TREE_TYPE (mdecl));
5640                       set_java_signature (TREE_TYPE (mdecl), signature);
5641                     }
5642                 }
5643               else
5644                 continue;
5645               break;
5646
5647             case JDEP_INTERFACE:
5648               if (parser_check_super_interface (decl, JDEP_DECL (dep),
5649                                                 JDEP_WFL (dep)))
5650                 continue;
5651               parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5652               break;
5653
5654             case JDEP_PARM:
5655             case JDEP_VARIABLE:
5656               type = TREE_TYPE(decl);
5657               if (TREE_CODE (type) == RECORD_TYPE)
5658                 type = promote_type (type);
5659               JDEP_APPLY_PATCH (dep, type);
5660               break;
5661
5662             case JDEP_TYPE:
5663               JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5664               SOURCE_FRONTEND_DEBUG 
5665                 (("Completing a random type dependency on a '%s' node",
5666                   tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5667               break;
5668
5669             case JDEP_EXCEPTION:
5670               JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5671               SOURCE_FRONTEND_DEBUG 
5672                 (("Completing `%s' `throws' argument node",
5673                   IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5674               break;
5675
5676             case JDEP_ANONYMOUS:
5677               patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5678               break;
5679
5680             default:
5681               abort ();
5682             }
5683         }
5684     }
5685   return;
5686 }
5687
5688 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5689    array.  */
5690
5691 static tree
5692 resolve_class (enclosing, class_type, decl, cl)
5693      tree enclosing, class_type, decl, cl;
5694 {
5695   tree tname = TYPE_NAME (class_type);
5696   tree resolved_type = TREE_TYPE (class_type);
5697   int array_dims = 0;
5698   tree resolved_type_decl;
5699   
5700   if (resolved_type != NULL_TREE)
5701     {
5702       tree resolved_type_decl = TYPE_NAME (resolved_type);
5703       if (resolved_type_decl == NULL_TREE
5704           || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5705         {
5706           resolved_type_decl = build_decl (TYPE_DECL,
5707                                            TYPE_NAME (class_type),
5708                                            resolved_type);
5709         }
5710       return resolved_type_decl;
5711     }
5712
5713   /* 1- Check to see if we have an array. If true, find what we really
5714      want to resolve  */
5715   if ((array_dims = build_type_name_from_array_name (tname,
5716                                                      &TYPE_NAME (class_type))))
5717     WFL_STRIP_BRACKET (cl, cl);
5718
5719   /* 2- Resolve the bare type */
5720   if (!(resolved_type_decl = do_resolve_class (enclosing, class_type, 
5721                                                decl, cl)))
5722     return NULL_TREE;
5723   resolved_type = TREE_TYPE (resolved_type_decl);
5724
5725   /* 3- If we have and array, reconstruct the array down to its nesting */
5726   if (array_dims)
5727     {
5728       for (; array_dims; array_dims--)
5729         resolved_type = build_java_array_type (resolved_type, -1);
5730       resolved_type_decl = TYPE_NAME (resolved_type);
5731     }
5732   TREE_TYPE (class_type) = resolved_type;
5733   return resolved_type_decl;
5734 }
5735
5736 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5737    are used to report error messages. Do not try to replace TYPE_NAME
5738    (class_type) by a variable, since it is changed by
5739    find_in_imports{_on_demand} and (but it doesn't really matter)
5740    qualify_and_find.  */
5741
5742 tree
5743 do_resolve_class (enclosing, class_type, decl, cl)
5744      tree enclosing, class_type, decl, cl;
5745 {
5746   tree new_class_decl = NULL_TREE, super = NULL_TREE;
5747   tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5748   tree decl_result;
5749   struct hash_table _ht, *circularity_hash = &_ht;
5750
5751   /* This hash table is used to register the classes we're going
5752      through when searching the current class as an inner class, in
5753      order to detect circular references. Remember to free it before
5754      returning the section 0- of this function. */
5755   hash_table_init (circularity_hash, hash_newfunc,
5756                    java_hash_hash_tree_node, java_hash_compare_tree_node);
5757
5758   /* 0- Search in the current class as an inner class.
5759      Maybe some code here should be added to load the class or
5760      something, at least if the class isn't an inner class and ended
5761      being loaded from class file. FIXME. */
5762   while (enclosing)
5763     {
5764       new_class_decl = resolve_inner_class (circularity_hash, cl, &enclosing,
5765                                             &super, class_type);
5766       if (new_class_decl)
5767         break;
5768
5769       /* If we haven't found anything because SUPER reached Object and
5770          ENCLOSING happens to be an innerclass, try the enclosing context. */
5771       if ((!super || super == object_type_node) && 
5772           enclosing && INNER_CLASS_DECL_P (enclosing))
5773         enclosing = DECL_CONTEXT (enclosing);
5774       else
5775         enclosing = NULL_TREE;
5776     }
5777
5778   hash_table_free (circularity_hash);
5779
5780   if (new_class_decl)
5781     return new_class_decl;
5782
5783   /* 1- Check for the type in single imports. This will change
5784      TYPE_NAME() if something relevant is found */
5785   find_in_imports (saved_enclosing_type, class_type);
5786
5787   /* 2- And check for the type in the current compilation unit */
5788   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5789     {
5790       if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5791           !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5792         load_class (TYPE_NAME (class_type), 0);
5793       return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5794     }
5795
5796   /* 3- Search according to the current package definition */
5797   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5798     {
5799       if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5800                                              TYPE_NAME (class_type))))
5801         return new_class_decl;
5802     }
5803
5804   /* 4- Check the import on demands. Don't allow bar.baz to be
5805      imported from foo.* */
5806   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5807     if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5808       return NULL_TREE;
5809
5810   /* If found in find_in_imports_on_demant, the type has already been
5811      loaded. */
5812   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5813     return new_class_decl;
5814
5815   /* 5- Try with a name qualified with the package name we've seen so far */
5816   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5817     {
5818       tree package;
5819
5820       /* If there is a current package (ctxp->package), it's the first
5821          element of package_list and we can skip it. */
5822       for (package = (ctxp->package ? 
5823                       TREE_CHAIN (package_list) : package_list);
5824            package; package = TREE_CHAIN (package))
5825         if ((new_class_decl = qualify_and_find (class_type,
5826                                                TREE_PURPOSE (package), 
5827                                                TYPE_NAME (class_type))))
5828           return new_class_decl;
5829     }
5830
5831   /* 5- Check an other compilation unit that bears the name of type */
5832   load_class (TYPE_NAME (class_type), 0);
5833   
5834   if (!cl)
5835     cl = lookup_cl (decl);
5836   
5837   /* If we don't have a value for CL, then we're being called recursively. 
5838      We can't check package access just yet, but it will be taken care of
5839      by the caller. */
5840   if (cl)
5841     {
5842       if (check_pkg_class_access (TYPE_NAME (class_type), cl, true))
5843         return NULL_TREE;
5844     }
5845
5846   /* 6- Last call for a resolution */
5847   decl_result = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5848
5849   /* The final lookup might have registered a.b.c into a.b$c If we
5850      failed at the first lookup, progressively change the name if
5851      applicable and use the matching DECL instead. */
5852   if (!decl_result && QUALIFIED_P (TYPE_NAME (class_type)))
5853     {
5854       tree name = TYPE_NAME (class_type);
5855       char *separator;
5856       do {
5857
5858        /* Reach the last '.', and if applicable, replace it by a `$' and
5859           see if this exists as a type. */
5860        if ((separator = strrchr (IDENTIFIER_POINTER (name), '.')))
5861          {
5862            int c = *separator;
5863            *separator = '$';
5864            name = get_identifier (IDENTIFIER_POINTER (name));
5865            *separator = c;
5866            decl_result = IDENTIFIER_CLASS_VALUE (name);
5867          }
5868       } while (!decl_result && separator);
5869     }
5870   return decl_result;
5871 }
5872
5873 static tree
5874 qualify_and_find (class_type, package, name)
5875      tree class_type, package, name;
5876 {
5877   tree new_qualified = merge_qualified_name (package, name);
5878   tree new_class_decl;
5879
5880   if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5881     load_class (new_qualified, 0);
5882   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5883     {
5884       if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5885           !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5886         load_class (new_qualified, 0);
5887       TYPE_NAME (class_type) = new_qualified;
5888       return IDENTIFIER_CLASS_VALUE (new_qualified);
5889     }
5890   return NULL_TREE;
5891 }
5892
5893 /* Resolve NAME and lay it out (if not done and if not the current
5894    parsed class). Return a decl node. This function is meant to be
5895    called when type resolution is necessary during the walk pass.  */
5896
5897 static tree
5898 resolve_and_layout (something, cl)
5899      tree something;
5900      tree cl;
5901 {
5902   tree decl, decl_type;
5903
5904   /* Don't do that on the current class */
5905   if (something == current_class)
5906     return TYPE_NAME (current_class);
5907
5908   /* Don't do anything for void and other primitive types */
5909   if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5910     return NULL_TREE;
5911
5912   /* Pointer types can be reall pointer types or fake pointers. When
5913      finding a real pointer, recheck for primitive types */
5914   if (TREE_CODE (something) == POINTER_TYPE)
5915     {
5916       if (TREE_TYPE (something))
5917         {
5918           something = TREE_TYPE (something);
5919           if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5920             return NULL_TREE;
5921         }
5922       else
5923         something = TYPE_NAME (something);
5924     }
5925
5926   /* Don't do anything for arrays of primitive types */
5927   if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5928       && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5929     return NULL_TREE;
5930
5931   /* Something might be a WFL */
5932   if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5933     something = EXPR_WFL_NODE (something);
5934
5935   /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5936      TYPE_DECL or a real TYPE */
5937   else if (TREE_CODE (something) != IDENTIFIER_NODE)
5938     something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5939             DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5940
5941   if (!(decl = resolve_no_layout (something, cl)))
5942     return NULL_TREE;
5943
5944   /* Resolve and layout if necessary */
5945   decl_type = TREE_TYPE (decl);
5946   layout_class_methods (decl_type);
5947   /* Check methods */
5948   if (CLASS_FROM_SOURCE_P (decl_type))
5949     java_check_methods (decl);
5950   /* Layout the type if necessary */ 
5951   if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5952     safe_layout_class (decl_type);
5953
5954   return decl;
5955 }
5956
5957 /* Resolve a class, returns its decl but doesn't perform any
5958    layout. The current parsing context is saved and restored */
5959
5960 static tree
5961 resolve_no_layout (name, cl)
5962      tree name, cl;
5963 {
5964   tree ptr, decl;
5965   BUILD_PTR_FROM_NAME (ptr, name);
5966   java_parser_context_save_global ();
5967   decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5968   java_parser_context_restore_global ();
5969   
5970   return decl;
5971 }
5972
5973 /* Called when reporting errors. Skip the '[]'s in a complex array
5974    type description that failed to be resolved. purify_type_name can't
5975    use an identifier tree.  */
5976
5977 static const char *
5978 purify_type_name (name)
5979      const char *name;
5980 {
5981   int len = strlen (name);
5982   int bracket_found;
5983
5984   STRING_STRIP_BRACKETS (name, len, bracket_found);
5985   if (bracket_found)
5986     {
5987       char *stripped_name = xmemdup (name, len, len+1);
5988       stripped_name [len] = '\0';
5989       return stripped_name;
5990     }
5991   return name;
5992 }
5993
5994 /* The type CURRENT refers to can't be found. We print error messages.  */
5995
5996 static void
5997 complete_class_report_errors (dep)
5998      jdep *dep;
5999 {
6000   const char *name;
6001
6002   if (!JDEP_WFL (dep))
6003     return;
6004
6005   name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
6006   switch (JDEP_KIND (dep))
6007     {
6008     case JDEP_SUPER:
6009       parse_error_context  
6010         (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
6011          purify_type_name (name),
6012          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6013       break;
6014     case JDEP_FIELD:
6015       parse_error_context
6016         (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
6017          purify_type_name (name),
6018          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6019       break;
6020     case JDEP_METHOD:           /* Covers arguments */
6021       parse_error_context
6022         (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
6023          purify_type_name (name),
6024          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
6025          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
6026       break;
6027     case JDEP_METHOD_RETURN:    /* Covers return type */
6028       parse_error_context
6029         (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'", 
6030          purify_type_name (name),
6031          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
6032       break;
6033     case JDEP_INTERFACE:
6034       parse_error_context
6035         (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
6036          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
6037          (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
6038          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6039       break;
6040     case JDEP_VARIABLE:
6041       parse_error_context
6042         (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'", 
6043          purify_type_name (IDENTIFIER_POINTER 
6044                            (EXPR_WFL_NODE (JDEP_WFL (dep)))),
6045          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6046       break;
6047     case JDEP_EXCEPTION:        /* As specified by `throws' */
6048       parse_error_context 
6049           (JDEP_WFL (dep), "Class `%s' not found in `throws'",
6050          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
6051       break;
6052     default:
6053       /* Fix for -Wall. Just break doing nothing. The error will be
6054          caught later */
6055       break;
6056     }
6057 }
6058
6059 /* Return a static string containing the DECL prototype string. If
6060    DECL is a constructor, use the class name instead of the form
6061    <init> */
6062
6063 static const char *
6064 get_printable_method_name (decl)
6065      tree decl;
6066 {
6067   const char *to_return;
6068   tree name = NULL_TREE;
6069
6070   if (DECL_CONSTRUCTOR_P (decl))
6071     {
6072       name = DECL_NAME (decl);
6073       DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6074     }
6075       
6076   to_return = lang_printable_name (decl, 0);
6077   if (DECL_CONSTRUCTOR_P (decl))
6078     DECL_NAME (decl) = name;
6079   
6080   return to_return;
6081 }
6082
6083 /* Track method being redefined inside the same class. As a side
6084    effect, set DECL_NAME to an IDENTIFIER (prior entering this
6085    function it's a FWL, so we can track errors more accurately.)  */
6086
6087 static int
6088 check_method_redefinition (class, method)
6089      tree class, method;
6090 {
6091   tree redef, sig;
6092
6093   /* There's no need to verify <clinit> and finit$ and instinit$ */
6094   if (DECL_CLINIT_P (method)
6095       || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6096     return 0;
6097
6098   sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6099   for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6100     {
6101       if (redef == method)
6102         break;
6103       if (DECL_NAME (redef) == DECL_NAME (method)
6104           && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6105           && !DECL_ARTIFICIAL (method))
6106         {
6107           parse_error_context 
6108             (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
6109              (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6110              get_printable_method_name (redef));
6111           return 1;
6112         }
6113     }
6114   return 0;
6115 }
6116
6117 /* Return 1 if check went ok, 0 otherwise.  */
6118 static int
6119 check_abstract_method_definitions (do_interface, class_decl, type)
6120      int do_interface;
6121      tree class_decl, type;
6122 {
6123   tree class = TREE_TYPE (class_decl);
6124   tree method, end_type;
6125   int ok = 1;
6126
6127   end_type = (do_interface ? object_type_node : type);
6128   for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6129     {
6130       tree other_super, other_method, method_sig, method_name;
6131       int found = 0;
6132       int end_type_reached = 0;
6133       
6134       if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6135         continue;
6136       
6137       /* Now verify that somewhere in between TYPE and CLASS,
6138          abstract method METHOD gets a non abstract definition
6139          that is inherited by CLASS.  */
6140       
6141       method_sig = build_java_signature (TREE_TYPE (method));
6142       method_name = DECL_NAME (method);
6143       if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6144         method_name = EXPR_WFL_NODE (method_name);
6145
6146       other_super = class;
6147       do {
6148         if (other_super == end_type)
6149           end_type_reached = 1;
6150         
6151         /* Method search */
6152         for (other_method = TYPE_METHODS (other_super); other_method;
6153             other_method = TREE_CHAIN (other_method))
6154           {
6155             tree s = build_java_signature (TREE_TYPE (other_method));
6156             tree other_name = DECL_NAME (other_method);
6157             
6158             if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6159               other_name = EXPR_WFL_NODE (other_name);
6160             if (!DECL_CLINIT_P (other_method)
6161                 && !DECL_CONSTRUCTOR_P (other_method)
6162                 && method_name == other_name
6163                 && method_sig == s
6164                 && !METHOD_ABSTRACT (other_method))
6165              {
6166                found = 1;
6167                break;
6168              }
6169           }
6170         other_super = CLASSTYPE_SUPER (other_super);
6171       } while (!end_type_reached);
6172  
6173       /* Report that abstract METHOD didn't find an implementation
6174          that CLASS can use. */
6175       if (!found)
6176         {
6177           char *t = xstrdup (lang_printable_name 
6178                             (TREE_TYPE (TREE_TYPE (method)), 0));
6179           tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6180           
6181           parse_error_context 
6182             (lookup_cl (class_decl),
6183              "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",
6184              IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6185              t, lang_printable_name (method, 0), 
6186              (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ? 
6187               "interface" : "class"),
6188              IDENTIFIER_POINTER (ccn),
6189              (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6190              IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6191           ok = 0;
6192           free (t);
6193         }
6194     }
6195
6196   if (ok && do_interface)
6197     {
6198       /* Check for implemented interfaces. */
6199       int i;
6200       tree vector = TYPE_BINFO_BASETYPES (type);
6201       for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
6202         {
6203           tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6204           ok = check_abstract_method_definitions (1, class_decl, super);
6205         }
6206     }
6207
6208   return ok;
6209 }
6210
6211 /* Check that CLASS_DECL somehow implements all inherited abstract
6212    methods.  */
6213
6214 static void
6215 java_check_abstract_method_definitions (class_decl)
6216      tree class_decl;
6217 {
6218   tree class = TREE_TYPE (class_decl);
6219   tree super, vector;
6220   int i;
6221
6222   if (CLASS_ABSTRACT (class_decl))
6223     return;
6224
6225   /* Check for inherited types */
6226   super = class;
6227   do {
6228     super = CLASSTYPE_SUPER (super);
6229     check_abstract_method_definitions (0, class_decl, super);
6230   } while (super != object_type_node);
6231
6232   /* Check for implemented interfaces. */
6233   vector = TYPE_BINFO_BASETYPES (class);
6234   for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
6235     {
6236       super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6237       check_abstract_method_definitions (1, class_decl, super);
6238     }
6239 }
6240
6241 /* Check all the types method DECL uses and return 1 if all of them
6242    are now complete, 0 otherwise. This is used to check whether its
6243    safe to build a method signature or not.  */
6244
6245 static int
6246 check_method_types_complete (decl)
6247      tree decl;
6248 {
6249   tree type = TREE_TYPE (decl);
6250   tree args;
6251
6252   if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6253     return 0;
6254   
6255   args = TYPE_ARG_TYPES (type);
6256   if (TREE_CODE (type) == METHOD_TYPE)
6257     args = TREE_CHAIN (args);
6258   for (; args != end_params_node; args = TREE_CHAIN (args))
6259     if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6260       return 0;
6261
6262   return 1;
6263 }
6264
6265 /* Visible interface to check methods contained in CLASS_DECL */
6266
6267 void
6268 java_check_methods (class_decl)
6269      tree class_decl;
6270 {
6271   if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6272     return;
6273
6274   if (CLASS_INTERFACE (class_decl))
6275     java_check_abstract_methods (class_decl);
6276   else
6277     java_check_regular_methods (class_decl);
6278   
6279   CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6280 }
6281
6282 /* Check all the methods of CLASS_DECL. Methods are first completed
6283    then checked according to regular method existence rules.  If no
6284    constructor for CLASS_DECL were encountered, then build its
6285    declaration.  */
6286
6287 static void
6288 java_check_regular_methods (class_decl)
6289      tree class_decl;
6290 {
6291   int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6292   tree method;
6293   tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
6294   tree found = NULL_TREE;
6295   tree mthrows;
6296
6297   /* It is not necessary to check methods defined in java.lang.Object */
6298   if (class == object_type_node)
6299     return;
6300
6301   if (!TYPE_NVIRTUALS (class))
6302     TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6303
6304   /* Should take interfaces into account. FIXME */
6305   for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6306     {
6307       tree sig;
6308       tree method_wfl = DECL_FUNCTION_WFL (method);
6309       int aflags;
6310
6311       /* Check for redefinitions */
6312       if (check_method_redefinition (class, method))
6313         continue;
6314
6315       /* If we see one constructor a mark so we don't generate the
6316          default one. Also skip other verifications: constructors
6317          can't be inherited hence hiden or overriden */
6318      if (DECL_CONSTRUCTOR_P (method))
6319        {
6320          saw_constructor = 1;
6321          continue;
6322        }
6323
6324       /* We verify things thrown by the method. They must inherits from
6325          java.lang.Throwable */
6326       for (mthrows = DECL_FUNCTION_THROWS (method);
6327            mthrows; mthrows = TREE_CHAIN (mthrows))
6328         {
6329           if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6330             parse_error_context 
6331               (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6332                IDENTIFIER_POINTER 
6333                  (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6334         }
6335
6336       sig = build_java_argument_signature (TREE_TYPE (method));
6337       found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6338
6339       /* Inner class can't declare static methods */
6340       if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6341         {
6342           char *t = xstrdup (lang_printable_name (class, 0));
6343           parse_error_context 
6344             (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6345              lang_printable_name (method, 0), t);
6346           free (t);
6347         }
6348
6349       /* Nothing overrides or it's a private method. */
6350       if (!found)
6351         continue;
6352       if (METHOD_PRIVATE (found))
6353         {
6354           found = NULL_TREE;
6355           continue;
6356         }
6357
6358       /* If `found' is declared in an interface, make sure the
6359          modifier matches. */
6360       if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found))) 
6361           && clinit_identifier_node != DECL_NAME (found)
6362           && !METHOD_PUBLIC (method))
6363         {
6364           tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6365           parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6366                                IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6367                                lang_printable_name (method, 0),
6368                                IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6369         }
6370
6371       /* Can't override a method with the same name and different return
6372          types. */
6373       if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6374         {
6375           char *t = xstrdup 
6376             (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6377           parse_error_context 
6378             (method_wfl,
6379              "Method `%s' was defined with return type `%s' in class `%s'", 
6380              lang_printable_name (found, 0), t,
6381              IDENTIFIER_POINTER 
6382                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6383           free (t);
6384         }
6385
6386       aflags = get_access_flags_from_decl (found);
6387
6388       /* Can't override final. Can't override static. */
6389       if (METHOD_FINAL (found) || METHOD_STATIC (found))
6390         {
6391           /* Static *can* override static */
6392           if (METHOD_STATIC (found) && METHOD_STATIC (method))
6393             continue;
6394           parse_error_context 
6395             (method_wfl,
6396              "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6397              (METHOD_FINAL (found) ? "Final" : "Static"),
6398              lang_printable_name (found, 0),
6399              (METHOD_FINAL (found) ? "final" : "static"),
6400              IDENTIFIER_POINTER
6401                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6402           continue;
6403         }
6404
6405       /* Static method can't override instance method. */
6406       if (METHOD_STATIC (method))
6407         {
6408           parse_error_context 
6409             (method_wfl,
6410              "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6411              lang_printable_name (found, 0),
6412              IDENTIFIER_POINTER
6413                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6414           continue;
6415         }
6416
6417       /* - Overriding/hiding public must be public
6418          - Overriding/hiding protected must be protected or public
6419          - If the overriden or hidden method has default (package)
6420            access, then the overriding or hiding method must not be
6421            private; otherwise, a compile-time error occurs.  If
6422            `found' belongs to an interface, things have been already
6423            taken care of.  */
6424       if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6425           && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6426               || (METHOD_PROTECTED (found) 
6427                   && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6428               || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6429                   && METHOD_PRIVATE (method))))
6430         {
6431           parse_error_context 
6432             (method_wfl,
6433              "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6434              (METHOD_PUBLIC (method) ? "public" : 
6435               (METHOD_PRIVATE (method) ? "private" : "protected")),
6436              IDENTIFIER_POINTER (DECL_NAME 
6437                                  (TYPE_NAME (DECL_CONTEXT (found)))));
6438           continue;
6439         }
6440
6441       /* Overriding methods must have compatible `throws' clauses on checked
6442          exceptions, if any */
6443       check_throws_clauses (method, method_wfl, found);
6444
6445       /* Inheriting multiple methods with the same signature. FIXME */
6446     }
6447   
6448   if (!TYPE_NVIRTUALS (class))
6449     TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6450
6451   /* Search for inherited abstract method not yet implemented in this
6452      class.  */
6453   java_check_abstract_method_definitions (class_decl);
6454
6455   if (!saw_constructor)
6456     abort ();
6457 }
6458
6459 /* Return a non zero value if the `throws' clause of METHOD (if any)
6460    is incompatible with the `throws' clause of FOUND (if any).  */
6461
6462 static void
6463 check_throws_clauses (method, method_wfl, found)
6464      tree method, method_wfl, found;
6465 {
6466   tree mthrows, fthrows;
6467
6468   /* Can't check these things with class loaded from bytecode. FIXME */
6469   if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6470     return;
6471
6472   for (mthrows = DECL_FUNCTION_THROWS (method);
6473        mthrows; mthrows = TREE_CHAIN (mthrows))
6474     {
6475       /* We don't verify unchecked expressions */
6476       if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6477         continue;
6478       /* Checked expression must be compatible */
6479       for (fthrows = DECL_FUNCTION_THROWS (found); 
6480            fthrows; fthrows = TREE_CHAIN (fthrows))
6481         if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6482           break;
6483       if (!fthrows)
6484         {
6485           parse_error_context 
6486             (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'",
6487              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6488              lang_printable_name (found, 0),
6489              IDENTIFIER_POINTER 
6490                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6491         }
6492     }
6493 }
6494
6495 /* Check abstract method of interface INTERFACE */
6496
6497 static void
6498 java_check_abstract_methods (interface_decl)
6499      tree interface_decl;
6500 {
6501   int i, n;
6502   tree method, basetype_vec, found;
6503   tree interface = TREE_TYPE (interface_decl);
6504
6505   for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6506     {
6507       /* 2- Check for double definition inside the defining interface */
6508       if (check_method_redefinition (interface, method))
6509         continue;
6510
6511       /* 3- Overriding is OK as far as we preserve the return type and
6512          the thrown exceptions (FIXME) */
6513       found = lookup_java_interface_method2 (interface, method);
6514       if (found)
6515         {
6516           char *t;
6517           t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6518           parse_error_context 
6519             (DECL_FUNCTION_WFL (found),
6520              "Method `%s' was defined with return type `%s' in class `%s'",
6521              lang_printable_name (found, 0), t,
6522              IDENTIFIER_POINTER 
6523                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6524           free (t);
6525           continue;
6526         }
6527     }
6528
6529   /* 4- Inherited methods can't differ by their returned types */
6530   if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6531     return;
6532   n = TREE_VEC_LENGTH (basetype_vec);
6533   for (i = 0; i < n; i++)
6534     {
6535       tree sub_interface_method, sub_interface;
6536       tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6537       if (!vec_elt)
6538         continue;
6539       sub_interface = BINFO_TYPE (vec_elt);
6540       for (sub_interface_method = TYPE_METHODS (sub_interface); 
6541            sub_interface_method;
6542            sub_interface_method = TREE_CHAIN (sub_interface_method))
6543         {
6544           found = lookup_java_interface_method2 (interface, 
6545                                                  sub_interface_method);
6546           if (found && (found != sub_interface_method))
6547             {
6548               parse_error_context 
6549                 (lookup_cl (sub_interface_method),
6550                  "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6551                  IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6552                  lang_printable_name (found, 0),
6553                  IDENTIFIER_POINTER 
6554                    (DECL_NAME (TYPE_NAME 
6555                                (DECL_CONTEXT (sub_interface_method)))),
6556                  IDENTIFIER_POINTER 
6557                    (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6558             }
6559         }
6560     }
6561 }
6562
6563 /* Lookup methods in interfaces using their name and partial
6564    signature. Return a matching method only if their types differ.  */
6565
6566 static tree
6567 lookup_java_interface_method2 (class, method_decl)
6568      tree class, method_decl;
6569 {
6570   int i, n;
6571   tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6572
6573   if (!basetype_vec)
6574     return NULL_TREE;
6575
6576   n = TREE_VEC_LENGTH (basetype_vec);
6577   for (i = 0; i < n; i++)
6578     {
6579       tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6580       if ((BINFO_TYPE (vec_elt) != object_type_node)
6581           && (to_return = 
6582               lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6583         return to_return;
6584     }
6585   for (i = 0; i < n; i++)
6586     {
6587       to_return = lookup_java_interface_method2 
6588         (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6589       if (to_return)
6590         return to_return;
6591     }
6592
6593   return NULL_TREE;
6594 }
6595
6596 /* Lookup method using their name and partial signature. Return a
6597    matching method only if their types differ.  */
6598
6599 static tree
6600 lookup_java_method2 (clas, method_decl, do_interface)
6601      tree clas, method_decl;
6602      int do_interface;
6603 {
6604   tree method, method_signature, method_name, method_type, name;
6605
6606   method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6607   name = DECL_NAME (method_decl);
6608   method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ? 
6609                  EXPR_WFL_NODE (name) : name);
6610   method_type = TREE_TYPE (TREE_TYPE (method_decl));
6611
6612   while (clas != NULL_TREE)
6613     {
6614       for (method = TYPE_METHODS (clas);
6615            method != NULL_TREE;  method = TREE_CHAIN (method))
6616         {
6617           tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6618           tree name = DECL_NAME (method);
6619           if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6620                EXPR_WFL_NODE (name) : name) == method_name
6621               && method_sig == method_signature 
6622               && TREE_TYPE (TREE_TYPE (method)) != method_type)
6623             return method;
6624         }
6625       clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6626     }
6627   return NULL_TREE;
6628 }
6629
6630 /* Return the line that matches DECL line number, and try its best to
6631    position the column number. Used during error reports.  */
6632
6633 static tree
6634 lookup_cl (decl)
6635      tree decl;
6636 {
6637   static tree cl = NULL_TREE;
6638   char *line, *found;
6639   
6640   if (!decl)
6641     return NULL_TREE;
6642
6643   if (cl == NULL_TREE)
6644     {
6645       cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6646       ggc_add_tree_root (&cl, 1);
6647     }
6648
6649   EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6650   EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6651
6652   line = java_get_line_col (EXPR_WFL_FILENAME (cl), 
6653                             EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6654
6655   found = strstr ((const char *)line, 
6656                   (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6657   if (found)
6658     EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6659
6660   return cl;
6661 }
6662
6663 /* Look for a simple name in the single-type import list */
6664
6665 static tree
6666 find_name_in_single_imports (name)
6667      tree name;
6668 {
6669   tree node;
6670
6671   for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6672     if (TREE_VALUE (node) == name)
6673       return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6674
6675   return NULL_TREE;
6676 }
6677
6678 /* Process all single-type import. */
6679
6680 static int
6681 process_imports ()
6682 {
6683   tree import;
6684   int error_found;
6685
6686   for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6687     {
6688       tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6689       char *original_name;
6690
6691       obstack_grow0 (&temporary_obstack,
6692                      IDENTIFIER_POINTER (to_be_found),
6693                      IDENTIFIER_LENGTH (to_be_found));
6694       original_name = obstack_finish (&temporary_obstack);
6695
6696       /* Don't load twice something already defined. */
6697       if (IDENTIFIER_CLASS_VALUE (to_be_found))
6698         continue;
6699       
6700       while (1)
6701         {
6702           tree left;
6703
6704           QUALIFIED_P (to_be_found) = 1;
6705           load_class (to_be_found, 0);
6706           error_found =
6707             check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true);
6708           
6709           /* We found it, we can bail out */
6710           if (IDENTIFIER_CLASS_VALUE (to_be_found))
6711             break;
6712
6713           /* We haven't found it. Maybe we're trying to access an
6714              inner class.  The only way for us to know is to try again
6715              after having dropped a qualifier. If we can't break it further,
6716              we have an error. */
6717           if (breakdown_qualified (&left, NULL, to_be_found))
6718             break;
6719
6720           to_be_found = left;
6721         }
6722       if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6723         {
6724           parse_error_context (TREE_PURPOSE (import),
6725                                "Class or interface `%s' not found in import",
6726                                original_name);
6727           error_found = 1;
6728         }
6729
6730       obstack_free (&temporary_obstack, original_name);
6731       if (error_found)
6732         return 1;
6733     }
6734   return 0;
6735 }
6736
6737 /* Possibly find and mark a class imported by a single-type import
6738    statement.  */
6739
6740 static void
6741 find_in_imports (enclosing_type, class_type)
6742      tree enclosing_type;
6743      tree class_type;
6744 {
6745   tree import = (enclosing_type ? TYPE_IMPORT_LIST (enclosing_type) : 
6746                  ctxp->import_list);
6747   while (import)
6748     {
6749       if (TREE_VALUE (import) == TYPE_NAME (class_type))
6750         {
6751           TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6752           QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6753           return;
6754         }
6755       import = TREE_CHAIN (import);
6756     }
6757 }
6758
6759 static int
6760 note_possible_classname (name, len)
6761      const char *name;
6762      int len;
6763 {
6764   tree node;
6765   if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6766     len = len - 5;
6767   else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6768     len = len - 6;
6769   else
6770     return 0;
6771   node = ident_subst (name, len, "", '/', '.', "");
6772   IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6773   QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6774   return 1;
6775 }
6776
6777 /* Read a import directory, gathering potential match for further type
6778    references. Indifferently reads a filesystem or a ZIP archive
6779    directory.  */
6780
6781 static void
6782 read_import_dir (wfl)
6783      tree wfl;
6784 {
6785   tree package_id = EXPR_WFL_NODE (wfl);
6786   const char *package_name = IDENTIFIER_POINTER (package_id);
6787   int package_length = IDENTIFIER_LENGTH (package_id);
6788   DIR *dirp = NULL;
6789   JCF *saved_jcf = current_jcf;
6790
6791   int found = 0;
6792   int k;
6793   void *entry;
6794   struct buffer filename[1];
6795
6796   if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6797     return;
6798   IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6799
6800   BUFFER_INIT (filename);
6801   buffer_grow (filename, package_length + 100);
6802
6803   for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6804     {
6805       const char *entry_name = jcf_path_name (entry);
6806       int entry_length = strlen (entry_name);
6807       if (jcf_path_is_zipfile (entry))
6808         {
6809           ZipFile *zipf;
6810           buffer_grow (filename, entry_length);
6811           memcpy (filename->data, entry_name, entry_length - 1);
6812           filename->data[entry_length-1] = '\0';
6813           zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6814           if (zipf == NULL)
6815             error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6816           else
6817             {
6818               ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6819               BUFFER_RESET (filename);
6820               for (k = 0; k < package_length; k++)
6821                 {
6822                   char ch = package_name[k];
6823                   *filename->ptr++ = ch == '.' ? '/' : ch;
6824                 }
6825               *filename->ptr++ = '/';
6826
6827               for (k = 0; k < zipf->count;  k++, zipd = ZIPDIR_NEXT (zipd))
6828                 {
6829                   const char *current_entry = ZIPDIR_FILENAME (zipd);
6830                   int current_entry_len = zipd->filename_length;
6831
6832                   if (current_entry_len >= BUFFER_LENGTH (filename)
6833                       && strncmp (filename->data, current_entry, 
6834                                   BUFFER_LENGTH (filename)) != 0)
6835                     continue;
6836                   found |= note_possible_classname (current_entry,
6837                                                     current_entry_len);
6838                 }
6839             }
6840         }
6841       else
6842         {
6843           BUFFER_RESET (filename);
6844           buffer_grow (filename, entry_length + package_length + 4);
6845           strcpy (filename->data, entry_name);
6846           filename->ptr = filename->data + entry_length;
6847           for (k = 0; k < package_length; k++)
6848             {
6849               char ch = package_name[k];
6850               *filename->ptr++ = ch == '.' ? '/' : ch;
6851             }
6852           *filename->ptr = '\0';
6853
6854           dirp = opendir (filename->data);
6855           if (dirp == NULL)
6856             continue;
6857           *filename->ptr++ = '/';
6858           for (;;)
6859             {
6860               int len; 
6861               const char *d_name;
6862               struct dirent *direntp = readdir (dirp);
6863               if (!direntp)
6864                 break;
6865               d_name = direntp->d_name;
6866               len = strlen (direntp->d_name);
6867               buffer_grow (filename, len+1);
6868               strcpy (filename->ptr, d_name);
6869               found |= note_possible_classname (filename->data + entry_length,
6870                                                 package_length+len+1);
6871             }
6872           if (dirp)
6873             closedir (dirp);
6874         }
6875     }
6876
6877   free (filename->data);
6878
6879   /* Here we should have a unified way of retrieving an entry, to be
6880      indexed. */
6881   if (!found)
6882     {
6883       static int first = 1;
6884       if (first)
6885         {
6886           error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives", package_name);
6887           java_error_count++;
6888           first = 0;
6889         }
6890       else
6891         parse_error_context (wfl, "Package `%s' not found in import",
6892                              package_name);
6893       current_jcf = saved_jcf;
6894       return;
6895     }
6896   current_jcf = saved_jcf;
6897 }
6898
6899 /* Possibly find a type in the import on demands specified
6900    types. Returns 1 if an error occurred, 0 otherwise. Run through the
6901    entire list, to detected potential double definitions.  */
6902                  
6903 static int
6904 find_in_imports_on_demand (enclosing_type, class_type)
6905      tree enclosing_type;
6906      tree class_type;
6907 {
6908   tree class_type_name = TYPE_NAME (class_type);
6909   tree import = (enclosing_type ? TYPE_IMPORT_DEMAND_LIST (enclosing_type) :
6910                   ctxp->import_demand_list);
6911   tree cl = NULL_TREE;
6912   int seen_once = -1;   /* -1 when not set, 1 if seen once, >1 otherwise. */
6913   int to_return = -1;   /* -1 when not set, 0 or 1 otherwise */
6914   tree node;
6915
6916   for (; import; import = TREE_CHAIN (import))
6917     {
6918       int saved_lineno = lineno;
6919       int access_check;
6920       const char *id_name;
6921       tree decl, type_name_copy;
6922
6923       obstack_grow (&temporary_obstack, 
6924                     IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6925                     IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6926       obstack_1grow (&temporary_obstack, '.');
6927       obstack_grow0 (&temporary_obstack, 
6928                      IDENTIFIER_POINTER (class_type_name),
6929                      IDENTIFIER_LENGTH (class_type_name));
6930       id_name = obstack_finish (&temporary_obstack);
6931               
6932       if (! (node = maybe_get_identifier (id_name)))
6933         continue;
6934
6935       /* Setup lineno so that it refers to the line of the import (in
6936          case we parse a class file and encounter errors */
6937       lineno = EXPR_WFL_LINENO (TREE_PURPOSE (import));
6938
6939       type_name_copy = TYPE_NAME (class_type);
6940       TYPE_NAME (class_type) = node;
6941       QUALIFIED_P (node) = 1;
6942       decl = IDENTIFIER_CLASS_VALUE (node);
6943       access_check = -1;
6944       /* If there is no DECL set for the class or if the class isn't
6945          loaded and not seen in source yet, then load */
6946       if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6947                     && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6948         {
6949           load_class (node, 0);
6950           decl = IDENTIFIER_CLASS_VALUE (node);
6951         }
6952       if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
6953         access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
6954                                                false);
6955       else
6956         /* 6.6.1: Inner classes are subject to member access rules. */
6957         access_check = 0;
6958
6959       lineno = saved_lineno;
6960
6961       /* If the loaded class is not accessible or couldn't be loaded,
6962          we restore the original TYPE_NAME and process the next
6963          import. */
6964       if (access_check || !decl)
6965         {
6966           TYPE_NAME (class_type) = type_name_copy;
6967           continue;
6968         }
6969       
6970       /* If the loaded class is accessible, we keep a tab on it to
6971          detect and report multiple inclusions. */
6972       if (IS_A_CLASSFILE_NAME (node))
6973         {
6974           if (seen_once < 0)
6975             {
6976               cl = TREE_PURPOSE (import);
6977               seen_once = 1;
6978             }
6979           else if (seen_once >= 0)
6980             {
6981               tree location = (cl ? cl : TREE_PURPOSE (import));
6982               tree package = (cl ? EXPR_WFL_NODE (cl) : 
6983                               EXPR_WFL_NODE (TREE_PURPOSE (import)));
6984               seen_once++;
6985               parse_error_context 
6986                 (location,
6987                  "Type `%s' also potentially defined in package `%s'",
6988                  IDENTIFIER_POINTER (TYPE_NAME (class_type)), 
6989                  IDENTIFIER_POINTER (package));
6990             }
6991         }
6992       to_return = access_check;
6993     }
6994
6995   if (seen_once == 1)
6996     return to_return;
6997   else
6998     return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6999 }
7000
7001 /* Add package NAME to the list of package encountered so far. To
7002    speed up class lookup in do_resolve_class, we make sure a
7003    particular package is added only once.  */
7004
7005 static void
7006 register_package (name)
7007      tree name;
7008 {
7009   static struct hash_table _pht, *pht = NULL;
7010
7011   if (!pht)
7012     {
7013       hash_table_init (&_pht, hash_newfunc, 
7014                        java_hash_hash_tree_node, java_hash_compare_tree_node);
7015       pht = &_pht;
7016     }
7017   
7018   if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
7019     {
7020       package_list = chainon (package_list, build_tree_list (name, NULL));
7021       hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
7022     }
7023 }
7024
7025 static tree
7026 resolve_package (pkg, next)
7027      tree pkg, *next;
7028 {
7029   tree current, acc;
7030   tree type_name = NULL_TREE;
7031   const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
7032
7033   /* The trick is to determine when the package name stops and were
7034      the name of something contained in the package starts. Then we
7035      return a fully qualified name of what we want to get. */
7036
7037   /* Do a quick search on well known package names */
7038   if (!strncmp (name, "java.lang.reflect", 17))
7039     {
7040       *next = 
7041         TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
7042       type_name = lookup_package_type (name, 17);
7043     }
7044   else if (!strncmp (name, "java.lang", 9))
7045     {
7046       *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
7047       type_name = lookup_package_type (name, 9);
7048     }
7049
7050   /* If we found something here, return */
7051   if (type_name)
7052     return type_name; 
7053
7054   *next = EXPR_WFL_QUALIFICATION (pkg);
7055
7056   /* Try to progressively construct a type name */
7057   if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
7058     for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg); 
7059          current; current = TREE_CHAIN (current))
7060       {
7061         /* If we don't have what we're expecting, exit now. TYPE_NAME
7062            will be null and the error caught later. */
7063         if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
7064           break;
7065         acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
7066         if ((type_name = resolve_no_layout (acc, NULL_TREE)))
7067           {
7068             type_name = acc;
7069             /* resolve_package should be used in a loop, hence we
7070                point at this one to naturally process the next one at
7071                the next iteration. */
7072             *next = current;
7073             break;
7074           }
7075       }
7076   return type_name;
7077 }
7078
7079 static tree
7080 lookup_package_type (name, from)
7081      const char *name;
7082      int from;
7083 {
7084   char subname [128];
7085   const char *sub = &name[from+1];
7086   while (*sub != '.' && *sub)
7087     sub++;
7088   strncpy (subname, name, sub-name);
7089   subname [sub-name] = '\0';
7090   return get_identifier (subname);
7091 }
7092
7093 /* Check accessibility of inner classes according to member access rules. 
7094    DECL is the inner class, ENCLOSING_DECL is the class from which the
7095    access is being attempted. */
7096
7097 static void
7098 check_inner_class_access (decl, enclosing_decl, cl)
7099      tree decl, enclosing_decl, cl;
7100 {
7101   const char *access;
7102   tree enclosing_decl_type;
7103
7104   /* We don't issue an error message when CL is null. CL can be null
7105      as a result of processing a JDEP crafted by source_start_java_method
7106      for the purpose of patching its parm decl. But the error would
7107      have been already trapped when fixing the method's signature.
7108      DECL can also be NULL in case of earlier errors. */
7109   if (!decl || !cl)
7110     return;
7111
7112   enclosing_decl_type = TREE_TYPE (enclosing_decl);
7113
7114   if (CLASS_PRIVATE (decl))
7115     {
7116       /* Access is permitted only within the body of the top-level
7117          class in which DECL is declared. */
7118       tree top_level = decl;
7119       while (DECL_CONTEXT (top_level))
7120         top_level = DECL_CONTEXT (top_level);      
7121       while (DECL_CONTEXT (enclosing_decl))
7122         enclosing_decl = DECL_CONTEXT (enclosing_decl);
7123       if (top_level == enclosing_decl)
7124         return;      
7125       access = "private";
7126     }
7127   else if (CLASS_PROTECTED (decl))
7128     {
7129       tree decl_context;
7130       /* Access is permitted from within the same package... */
7131       if (in_same_package (decl, enclosing_decl))
7132         return;
7133       
7134       /* ... or from within the body of a subtype of the context in which
7135          DECL is declared. */
7136       decl_context = DECL_CONTEXT (decl);
7137       while (enclosing_decl)
7138         {
7139           if (CLASS_INTERFACE (decl))
7140             {
7141               if (interface_of_p (TREE_TYPE (decl_context), 
7142                                   enclosing_decl_type))
7143                 return;
7144             }
7145           else
7146             {
7147               /* Eww. The order of the arguments is different!! */
7148               if (inherits_from_p (enclosing_decl_type, 
7149                                    TREE_TYPE (decl_context)))
7150                 return;
7151             }
7152           enclosing_decl = DECL_CONTEXT (enclosing_decl);
7153         }      
7154       access = "protected";
7155     }
7156   else if (! CLASS_PUBLIC (decl))
7157     {
7158       /* Access is permitted only from within the same package as DECL. */
7159       if (in_same_package (decl, enclosing_decl))
7160         return;
7161       access = "non-public";
7162     }
7163   else
7164     /* Class is public. */
7165     return;
7166
7167   parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7168                        (CLASS_INTERFACE (decl) ? "interface" : "class"),
7169                        lang_printable_name (decl, 0), access);
7170 }
7171
7172 /* Accessibility check for top-level classes. If CLASS_NAME is in a
7173    foreign package, it must be PUBLIC. Return 0 if no access
7174    violations were found, 1 otherwise. If VERBOSE is true and an error
7175    was found, it is reported and accounted for.  */
7176
7177 static int
7178 check_pkg_class_access (class_name, cl, verbose)
7179      tree class_name;
7180      tree cl;
7181      bool verbose;
7182 {
7183   tree type;
7184
7185   if (!IDENTIFIER_CLASS_VALUE (class_name))
7186     return 0;
7187
7188   if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7189     return 0;
7190
7191   if (!CLASS_PUBLIC (TYPE_NAME (type)))
7192     {
7193       /* Access to a private class within the same package is
7194          allowed. */
7195       tree l, r;
7196       breakdown_qualified (&l, &r, class_name);
7197       if (!QUALIFIED_P (class_name) && !ctxp->package)
7198         /* Both in the empty package. */
7199         return 0;
7200       if (l == ctxp->package)
7201         /* Both in the same package. */
7202         return 0;
7203
7204       if (verbose)
7205         parse_error_context 
7206           (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
7207            (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7208            IDENTIFIER_POINTER (class_name));
7209       return 1;
7210     }
7211   return 0;
7212 }
7213
7214 /* Local variable declaration. */
7215
7216 static void
7217 declare_local_variables (modifier, type, vlist)
7218      int modifier;
7219      tree type;
7220      tree vlist;
7221 {
7222   tree decl, current, saved_type;
7223   tree type_wfl = NULL_TREE;
7224   int must_chain = 0;
7225   int final_p = 0;
7226
7227   /* Push a new block if statements were seen between the last time we
7228      pushed a block and now. Keep a count of blocks to close */
7229   if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7230     {
7231       tree b = enter_block ();
7232       BLOCK_IS_IMPLICIT (b) = 1;
7233     }
7234
7235   if (modifier)
7236     {
7237       int i;
7238       for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
7239       if (modifier == ACC_FINAL)
7240         final_p = 1;
7241       else 
7242         {
7243           parse_error_context 
7244             (ctxp->modifier_ctx [i], 
7245              "Only `final' is allowed as a local variables modifier");
7246           return;
7247         }
7248     }
7249
7250   /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7251      hold the TYPE value if a new incomplete has to be created (as
7252      opposed to being found already existing and reused). */
7253   SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7254
7255   /* If TYPE is fully resolved and we don't have a reference, make one */
7256   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7257
7258   /* Go through all the declared variables */
7259   for (current = vlist, saved_type = type; current;
7260        current = TREE_CHAIN (current), type = saved_type)
7261     {
7262       tree other, real_type;
7263       tree wfl  = TREE_PURPOSE (current);
7264       tree name = EXPR_WFL_NODE (wfl);
7265       tree init = TREE_VALUE (current);
7266
7267       /* Process NAME, as it may specify extra dimension(s) for it */
7268       type = build_array_from_name (type, type_wfl, name, &name);
7269
7270       /* Variable redefinition check */
7271       if ((other = lookup_name_in_blocks (name)))
7272         {
7273           variable_redefinition_error (wfl, name, TREE_TYPE (other),
7274                                        DECL_SOURCE_LINE (other));
7275           continue;
7276         }
7277
7278       /* Type adjustment. We may have just readjusted TYPE because
7279          the variable specified more dimensions. Make sure we have
7280          a reference if we can and don't have one already. */
7281       PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7282
7283       real_type = GET_REAL_TYPE (type);
7284       /* Never layout this decl. This will be done when its scope
7285          will be entered */
7286       decl = build_decl (VAR_DECL, name, real_type);
7287       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7288       DECL_FINAL (decl) = final_p;
7289       BLOCK_CHAIN_DECL (decl);
7290       
7291       /* If doing xreferencing, replace the line number with the WFL
7292          compound value */
7293       if (flag_emit_xref)
7294         DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7295       
7296       /* Don't try to use an INIT statement when an error was found */
7297       if (init && java_error_count)
7298         init = NULL_TREE;
7299       
7300       /* Add the initialization function to the current function's code */
7301       if (init)
7302         {
7303           /* Name might have been readjusted */
7304           EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7305           MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7306           java_method_add_stmt (current_function_decl,
7307                                 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7308                                                       init));
7309         }
7310     
7311       /* Setup dependency the type of the decl */
7312       if (must_chain)
7313         {
7314           jdep *dep;
7315           register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7316           dep = CLASSD_LAST (ctxp->classd_list);
7317           JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7318         }
7319     }
7320   SOURCE_FRONTEND_DEBUG (("Defined locals"));
7321 }
7322
7323 /* Called during parsing. Build decls from argument list.  */
7324
7325 static void
7326 source_start_java_method (fndecl)
7327      tree fndecl;
7328 {
7329   tree tem;
7330   tree parm_decl;
7331   int i;
7332
7333   if (!fndecl)
7334     return;
7335
7336   current_function_decl = fndecl;
7337
7338   /* New scope for the function */
7339   enter_block ();
7340   for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7341        tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7342     {
7343       tree type = TREE_VALUE (tem);
7344       tree name = TREE_PURPOSE (tem);
7345       
7346       /* If type is incomplete. Create an incomplete decl and ask for
7347          the decl to be patched later */
7348       if (INCOMPLETE_TYPE_P (type))
7349         {
7350           jdep *jdep;
7351           tree real_type = GET_REAL_TYPE (type);
7352           parm_decl = build_decl (PARM_DECL, name, real_type);
7353           type = obtain_incomplete_type (type);
7354           register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7355           jdep = CLASSD_LAST (ctxp->classd_list);
7356           JDEP_MISC (jdep) = name;
7357           JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7358         }
7359       else
7360         parm_decl = build_decl (PARM_DECL, name, type);
7361
7362       /* Remember if a local variable was declared final (via its
7363          TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7364       if (ARG_FINAL_P (tem))
7365         {
7366           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7367           DECL_FINAL (parm_decl) = 1;
7368         }
7369
7370       BLOCK_CHAIN_DECL (parm_decl);
7371     }
7372   tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7373   BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7374     nreverse (tem);
7375   DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7376   DECL_MAX_LOCALS (current_function_decl) = i;
7377 }
7378
7379 /* Called during parsing. Creates an artificial method declaration.  */
7380
7381 static tree
7382 create_artificial_method (class, flags, type, name, args)
7383      tree class;
7384      int flags;
7385      tree type, name, args;
7386 {
7387   tree mdecl;
7388
7389   java_parser_context_save_global ();
7390   lineno = 0;                                                               
7391   mdecl = make_node (FUNCTION_TYPE);                                
7392   TREE_TYPE (mdecl) = type;
7393   TYPE_ARG_TYPES (mdecl) = args;
7394   mdecl = add_method (class, flags, name, build_java_signature (mdecl)); 
7395   java_parser_context_restore_global ();
7396   DECL_ARTIFICIAL (mdecl) = 1;                                      
7397   return mdecl;
7398 }
7399
7400 /* Starts the body if an artificial method.  */
7401
7402 static void
7403 start_artificial_method_body (mdecl)
7404      tree mdecl;
7405 {
7406   DECL_SOURCE_LINE (mdecl) = 1;
7407   DECL_SOURCE_LINE_MERGE (mdecl, 1);
7408   source_start_java_method (mdecl);
7409   enter_block ();
7410 }
7411
7412 static void
7413 end_artificial_method_body (mdecl)
7414      tree mdecl;
7415 {
7416   /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7417      It has to be evaluated first. (if mdecl is current_function_decl,
7418      we have an undefined behavior if no temporary variable is used.) */
7419   tree b = exit_block ();
7420   BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7421   exit_block ();
7422 }
7423
7424 /* Dump a tree of some kind.  This is a convenience wrapper for the
7425    dump_* functions in tree-dump.c.  */
7426 static void
7427 dump_java_tree (phase, t)
7428      enum tree_dump_index phase;
7429      tree t;
7430 {
7431   FILE *stream;
7432   int flags;
7433
7434   stream = dump_begin (phase, &flags);
7435   if (stream)
7436     {
7437       dump_node (t, flags, stream);
7438       dump_end (phase, stream);
7439     }
7440 }
7441
7442 /* Terminate a function and expand its body.  */
7443
7444 static void
7445 source_end_java_method ()
7446 {
7447   tree fndecl = current_function_decl;
7448
7449   if (!fndecl)
7450     return;
7451
7452   java_parser_context_save_global ();
7453   lineno = ctxp->last_ccb_indent1;
7454
7455   /* Turn function bodies with only a NOP expr null, so they don't get
7456      generated at all and we won't get warnings when using the -W
7457      -Wall flags. */
7458   if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7459     BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7460
7461   /* We've generated all the trees for this function, and it has been
7462      patched.  Dump it to a file if the user requested it.  */
7463   dump_java_tree (TDI_original, fndecl);
7464
7465   /* Generate function's code */
7466   if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7467       && ! flag_emit_class_files
7468       && ! flag_emit_xref)
7469     expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7470
7471   /* pop out of its parameters */
7472   pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7473   poplevel (1, 0, 1);
7474   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7475
7476   /* Generate rtl for function exit.  */
7477   if (! flag_emit_class_files && ! flag_emit_xref)
7478     {
7479       lineno = DECL_SOURCE_LINE_LAST (fndecl);
7480       expand_function_end (input_filename, lineno, 0);
7481
7482       /* Run the optimizers and output assembler code for this function. */
7483       rest_of_compilation (fndecl);
7484     }
7485
7486   current_function_decl = NULL_TREE;
7487   java_parser_context_restore_global ();
7488 }
7489
7490 /* Record EXPR in the current function block. Complements compound
7491    expression second operand if necessary.  */
7492
7493 tree
7494 java_method_add_stmt (fndecl, expr)
7495      tree fndecl, expr;
7496 {
7497   if (!GET_CURRENT_BLOCK (fndecl))
7498     return NULL_TREE;
7499   return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7500 }
7501
7502 static tree
7503 add_stmt_to_block (b, type, stmt)
7504      tree b, type, stmt;
7505 {
7506   tree body = BLOCK_EXPR_BODY (b), c;
7507   
7508   if (java_error_count)
7509     return body;
7510     
7511   if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7512     return body;
7513
7514   BLOCK_EXPR_BODY (b) = c;
7515   TREE_SIDE_EFFECTS (c) = 1;
7516   return c;
7517 }
7518
7519 /* Add STMT to EXISTING if possible, otherwise create a new
7520    COMPOUND_EXPR and add STMT to it. */
7521
7522 static tree
7523 add_stmt_to_compound (existing, type, stmt)
7524      tree existing, type, stmt;
7525 {
7526   if (existing)
7527     return build (COMPOUND_EXPR, type, existing, stmt);
7528   else
7529     return stmt;
7530 }
7531
7532 void java_layout_seen_class_methods ()
7533 {
7534   tree previous_list = all_class_list;
7535   tree end = NULL_TREE;
7536   tree current;
7537
7538   while (1)
7539     {
7540       for (current = previous_list; 
7541            current != end; current = TREE_CHAIN (current))
7542         layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7543       
7544       if (previous_list != all_class_list)
7545         {
7546           end = previous_list;
7547           previous_list = all_class_list;
7548         }
7549       else
7550         break;
7551     }
7552 }
7553
7554 void
7555 java_reorder_fields ()
7556 {
7557   static tree stop_reordering = NULL_TREE;
7558   static int initialized_p;
7559   tree current;
7560
7561   /* Register STOP_REORDERING with the garbage collector.  */
7562   if (!initialized_p)
7563     {
7564       ggc_add_tree_root (&stop_reordering, 1);
7565       initialized_p = 1;
7566     }
7567
7568   for (current = gclass_list; current; current = TREE_CHAIN (current))
7569     {
7570       current_class = TREE_TYPE (TREE_VALUE (current));
7571
7572       if (current_class == stop_reordering)
7573         break;
7574
7575       /* Reverse the fields, but leave the dummy field in front.
7576          Fields are already ordered for Object and Class */
7577       if (TYPE_FIELDS (current_class) && current_class != object_type_node
7578           && current_class != class_type_node)
7579       {
7580         /* If the dummy field is there, reverse the right fields and
7581            just layout the type for proper fields offset */
7582         if (!DECL_NAME (TYPE_FIELDS (current_class)))
7583           {
7584             tree fields = TYPE_FIELDS (current_class);
7585             TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7586             TYPE_SIZE (current_class) = NULL_TREE;
7587           }
7588         /* We don't have a dummy field, we need to layout the class,
7589            after having reversed the fields */
7590         else
7591           {
7592             TYPE_FIELDS (current_class) = 
7593               nreverse (TYPE_FIELDS (current_class));
7594             TYPE_SIZE (current_class) = NULL_TREE;
7595           }
7596       }
7597     }
7598   /* There are cases were gclass_list will be empty. */
7599   if (gclass_list)
7600     stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7601 }
7602
7603 /* Layout the methods of all classes loaded in one way or another.
7604    Check methods of source parsed classes. Then reorder the
7605    fields and layout the classes or the type of all source parsed
7606    classes */
7607
7608 void
7609 java_layout_classes ()
7610 {
7611   tree current;
7612   int save_error_count = java_error_count;
7613
7614   /* Layout the methods of all classes seen so far */
7615   java_layout_seen_class_methods ();
7616   java_parse_abort_on_error ();
7617   all_class_list = NULL_TREE;
7618
7619   /* Then check the methods of all parsed classes */
7620   for (current = gclass_list; current; current = TREE_CHAIN (current))
7621     if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7622       java_check_methods (TREE_VALUE (current));
7623   java_parse_abort_on_error ();
7624
7625   for (current = gclass_list; current; current = TREE_CHAIN (current))
7626     {
7627       current_class = TREE_TYPE (TREE_VALUE (current));
7628       layout_class (current_class);
7629
7630       /* Error reported by the caller */
7631       if (java_error_count)
7632         return;
7633     }
7634
7635   /* We might have reloaded classes durign the process of laying out
7636      classes for code generation. We must layout the methods of those
7637      late additions, as constructor checks might use them */
7638   java_layout_seen_class_methods ();
7639   java_parse_abort_on_error ();
7640 }
7641
7642 /* Expand methods in the current set of classes rememebered for
7643    generation.  */
7644
7645 static void
7646 java_complete_expand_classes ()
7647 {
7648   tree current;
7649
7650   do_not_fold = flag_emit_xref;
7651
7652   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7653     if (!INNER_CLASS_DECL_P (current))
7654       java_complete_expand_class (current);
7655 }
7656
7657 /* Expand the methods found in OUTER, starting first by OUTER's inner
7658    classes, if any.  */
7659
7660 static void
7661 java_complete_expand_class (outer)
7662      tree outer;
7663 {
7664   tree inner_list;
7665
7666   set_nested_class_simple_name_value (outer, 1); /* Set */
7667
7668   /* We need to go after all inner classes and start expanding them,
7669      starting with most nested ones. We have to do that because nested
7670      classes might add functions to outer classes */
7671
7672   for (inner_list = DECL_INNER_CLASS_LIST (outer);
7673        inner_list; inner_list = TREE_CHAIN (inner_list))
7674     java_complete_expand_class (TREE_PURPOSE (inner_list));
7675
7676   java_complete_expand_methods (outer);
7677   set_nested_class_simple_name_value (outer, 0); /* Reset */
7678 }
7679
7680 /* Expand methods registered in CLASS_DECL. The general idea is that
7681    we expand regular methods first. This allows us get an estimate on
7682    how outer context local alias fields are really used so we can add
7683    to the constructor just enough code to initialize them properly (it
7684    also lets us generate finit$ correctly.) Then we expand the
7685    constructors and then <clinit>.  */
7686
7687 static void
7688 java_complete_expand_methods (class_decl)
7689      tree class_decl;
7690 {
7691   tree clinit, decl, first_decl;
7692
7693   current_class = TREE_TYPE (class_decl);
7694
7695   /* Initialize a new constant pool */
7696   init_outgoing_cpool ();
7697
7698   /* Pre-expand <clinit> to figure whether we really need it or
7699      not. If we do need it, we pre-expand the static fields so they're
7700      ready to be used somewhere else. <clinit> will be fully expanded
7701      after we processed the constructors. */
7702   first_decl = TYPE_METHODS (current_class);
7703   clinit = maybe_generate_pre_expand_clinit (current_class);
7704
7705   /* Then generate finit$ (if we need to) because constructors will
7706    try to use it.*/
7707   if (TYPE_FINIT_STMT_LIST (current_class))
7708     java_complete_expand_method (generate_finit (current_class));
7709
7710   /* Then generate instinit$ (if we need to) because constructors will
7711      try to use it. */
7712   if (TYPE_II_STMT_LIST (current_class))
7713     java_complete_expand_method (generate_instinit (current_class));
7714
7715   /* Now do the constructors */
7716   for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7717     {
7718       int no_body;
7719
7720       if (!DECL_CONSTRUCTOR_P (decl))
7721         continue;
7722       
7723       no_body = !DECL_FUNCTION_BODY (decl);
7724       /* Don't generate debug info on line zero when expanding a
7725          generated constructor. */
7726       if (no_body)
7727         restore_line_number_status (1);
7728
7729       java_complete_expand_method (decl);
7730
7731       if (no_body)
7732         restore_line_number_status (0);
7733     }
7734
7735   /* First, do the ordinary methods. */
7736   for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7737     {
7738       /* Ctors aren't part of this batch. */
7739       if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7740         continue;
7741       
7742       /* Skip abstract or native methods -- but do handle native
7743          methods when generating JNI stubs.  */
7744       if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7745         {
7746           DECL_FUNCTION_BODY (decl) = NULL_TREE;
7747           continue;
7748         }
7749
7750       if (METHOD_NATIVE (decl))
7751         {
7752           tree body;
7753           current_function_decl = decl;
7754           body = build_jni_stub (decl);
7755           BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7756         }
7757
7758       java_complete_expand_method (decl);
7759     }
7760
7761   /* If there is indeed a <clinit>, fully expand it now */
7762   if (clinit)
7763     {
7764       /* Prevent the use of `this' inside <clinit> */
7765       ctxp->explicit_constructor_p = 1;
7766       java_complete_expand_method (clinit);
7767       ctxp->explicit_constructor_p = 0;
7768     }
7769   
7770   /* We might have generated a class$ that we now want to expand */
7771   if (TYPE_DOT_CLASS (current_class))
7772     java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7773
7774   /* Now verify constructor circularity (stop after the first one we
7775      prove wrong.) */
7776   if (!CLASS_INTERFACE (class_decl))
7777     for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7778       if (DECL_CONSTRUCTOR_P (decl) 
7779           && verify_constructor_circularity (decl, decl))
7780         break;
7781
7782   /* Save the constant pool. We'll need to restore it later. */
7783   TYPE_CPOOL (current_class) = outgoing_cpool;
7784 }
7785
7786 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7787    safely used in some other methods/constructors.  */
7788
7789 static tree
7790 maybe_generate_pre_expand_clinit (class_type)
7791      tree class_type;
7792 {
7793   tree current, mdecl;
7794
7795   if (!TYPE_CLINIT_STMT_LIST (class_type))
7796     return NULL_TREE;
7797
7798   /* Go through all static fields and pre expand them */
7799   for (current = TYPE_FIELDS (class_type); current; 
7800        current = TREE_CHAIN (current))
7801     if (FIELD_STATIC (current))
7802       build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7803
7804   /* Then build the <clinit> method */
7805   mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7806                                     clinit_identifier_node, end_params_node);
7807   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7808                        mdecl, NULL_TREE);
7809   start_artificial_method_body (mdecl);
7810
7811   /* We process the list of assignment we produced as the result of
7812      the declaration of initialized static field and add them as
7813      statement to the <clinit> method. */
7814   for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7815        current = TREE_CHAIN (current))
7816     {
7817       tree stmt = current;
7818       /* We build the assignment expression that will initialize the
7819          field to its value. There are strict rules on static
7820          initializers (8.5). FIXME */
7821       if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7822         stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7823       java_method_add_stmt (mdecl, stmt);
7824     }
7825
7826   end_artificial_method_body (mdecl);
7827
7828   /* Now we want to place <clinit> as the last method (because we need
7829      it at least for interface so that it doesn't interfere with the
7830      dispatch table based lookup. */
7831   if (TREE_CHAIN (TYPE_METHODS (class_type)))
7832     {
7833       current = TREE_CHAIN (TYPE_METHODS (class_type));
7834       TYPE_METHODS (class_type) = current;
7835
7836       while (TREE_CHAIN (current))
7837         current = TREE_CHAIN (current);
7838
7839       TREE_CHAIN (current) = mdecl;
7840       TREE_CHAIN (mdecl) = NULL_TREE;
7841     }
7842
7843   return mdecl;
7844 }
7845
7846 /* Analyzes a method body and look for something that isn't a
7847    MODIFY_EXPR with a constant value.  */
7848
7849 static int
7850 analyze_clinit_body (this_class, bbody)
7851      tree this_class, bbody;
7852 {
7853   while (bbody)
7854     switch (TREE_CODE (bbody))
7855       {
7856       case BLOCK:
7857         bbody = BLOCK_EXPR_BODY (bbody);
7858         break;
7859         
7860       case EXPR_WITH_FILE_LOCATION:
7861         bbody = EXPR_WFL_NODE (bbody);
7862         break;
7863         
7864       case COMPOUND_EXPR:
7865         if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
7866           return 1;
7867         bbody = TREE_OPERAND (bbody, 1);
7868         break;
7869         
7870       case MODIFY_EXPR:
7871         /* If we're generating to class file and we're dealing with an
7872            array initialization, we return 1 to keep <clinit> */
7873         if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
7874             && flag_emit_class_files)
7875           return 1;
7876
7877         /* There are a few cases where we're required to keep
7878            <clinit>:
7879            - If this is an assignment whose operand is not constant,
7880            - If this is an assignment to a non-initialized field,
7881            - If this field is not a member of the current class.
7882         */
7883         return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
7884                 || ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
7885                 || DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
7886
7887       default:
7888         return 1;
7889       }
7890   return 0;
7891 }
7892
7893
7894 /* See whether we could get rid of <clinit>. Criteria are: all static
7895    final fields have constant initial values and the body of <clinit>
7896    is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7897
7898 static int
7899 maybe_yank_clinit (mdecl)
7900      tree mdecl;
7901 {
7902   tree type, current;
7903   tree fbody, bbody;
7904   
7905   if (!DECL_CLINIT_P (mdecl))
7906     return 0;
7907
7908   /* If the body isn't empty, then we keep <clinit>. Note that if
7909      we're emitting classfiles, this isn't enough not to rule it
7910      out. */
7911   fbody = DECL_FUNCTION_BODY (mdecl);
7912   bbody = BLOCK_EXPR_BODY (fbody);
7913   if (bbody && bbody != error_mark_node)
7914     bbody = BLOCK_EXPR_BODY (bbody);
7915   else
7916     return 0;
7917   if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7918     return 0;
7919
7920   type = DECL_CONTEXT (mdecl);
7921   current = TYPE_FIELDS (type);
7922
7923   for (current = (current ? TREE_CHAIN (current) : current); 
7924        current; current = TREE_CHAIN (current))
7925     {
7926       tree f_init;
7927
7928       /* We're not interested in non-static fields.  */
7929       if (!FIELD_STATIC (current))
7930         continue;
7931
7932       /* Nor in fields without initializers. */
7933       f_init = DECL_INITIAL (current);
7934       if (f_init == NULL_TREE)
7935         continue;
7936
7937       /* Anything that isn't String or a basic type is ruled out -- or
7938          if we know how to deal with it (when doing things natively) we
7939          should generated an empty <clinit> so that SUID are computed
7940          correctly. */
7941       if (! JSTRING_TYPE_P (TREE_TYPE (current))
7942           && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7943         return 0;
7944
7945       if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
7946         return 0;
7947     }
7948
7949   /* Now we analyze the method body and look for something that
7950      isn't a MODIFY_EXPR */
7951   if (bbody != empty_stmt_node && analyze_clinit_body (type, bbody))
7952     return 0;
7953
7954   /* Get rid of <clinit> in the class' list of methods */
7955   if (TYPE_METHODS (type) == mdecl)
7956     TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7957   else
7958     for (current = TYPE_METHODS (type); current; 
7959          current = TREE_CHAIN (current))
7960       if (TREE_CHAIN (current) == mdecl)
7961         {
7962           TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7963           break;
7964         }
7965
7966   return 1;
7967 }
7968
7969 /* Install the argument from MDECL. Suitable to completion and
7970    expansion of mdecl's body.  */
7971
7972 static void
7973 start_complete_expand_method (mdecl)
7974      tree mdecl;
7975 {
7976   tree tem;
7977
7978   pushlevel (1);                /* Prepare for a parameter push */
7979   tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7980   DECL_ARGUMENTS (mdecl) = tem;
7981
7982   for (; tem; tem = TREE_CHAIN (tem))
7983     {
7984       /* TREE_CHAIN (tem) will change after pushdecl. */ 
7985       tree next = TREE_CHAIN (tem);
7986       tree type = TREE_TYPE (tem);
7987       if (PROMOTE_PROTOTYPES
7988           && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7989           && INTEGRAL_TYPE_P (type))
7990         type = integer_type_node;
7991       DECL_ARG_TYPE (tem) = type;
7992       layout_decl (tem, 0);
7993       pushdecl (tem);
7994       /* Re-install the next so that the list is kept and the loop
7995          advances. */
7996       TREE_CHAIN (tem) = next;
7997     }
7998   pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7999   lineno = DECL_SOURCE_LINE_FIRST (mdecl);
8000   build_result_decl (mdecl);
8001 }
8002
8003
8004 /* Complete and expand a method.  */
8005
8006 static void
8007 java_complete_expand_method (mdecl)
8008      tree mdecl;
8009 {
8010   tree fbody, block_body, exception_copy;
8011
8012   current_function_decl = mdecl;
8013   /* Fix constructors before expanding them */
8014   if (DECL_CONSTRUCTOR_P (mdecl))
8015     fix_constructors (mdecl);
8016   
8017   /* Expand functions that have a body */
8018   if (!DECL_FUNCTION_BODY (mdecl))
8019     return;
8020
8021   fbody = DECL_FUNCTION_BODY (mdecl);
8022   block_body = BLOCK_EXPR_BODY (fbody);
8023   exception_copy = NULL_TREE;
8024
8025   current_function_decl = mdecl;
8026
8027   if (! quiet_flag)
8028     fprintf (stderr, " [%s.",
8029              lang_printable_name (DECL_CONTEXT (mdecl), 0));
8030   announce_function (mdecl);
8031   if (! quiet_flag)
8032     fprintf (stderr, "]");
8033   
8034   /* Prepare the function for tree completion */
8035   start_complete_expand_method (mdecl);
8036
8037   /* Install the current this */
8038   current_this = (!METHOD_STATIC (mdecl) ? 
8039                   BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
8040
8041   /* Purge the `throws' list of unchecked exceptions (we save a copy
8042      of the list and re-install it later.) */
8043   exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
8044   purge_unchecked_exceptions (mdecl);
8045   
8046   /* Install exceptions thrown with `throws' */
8047   PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
8048   
8049   if (block_body != NULL_TREE)
8050     {
8051       block_body = java_complete_tree (block_body);
8052       
8053       /* Before we check initialization, attached all class initialization
8054          variable to the block_body */
8055       hash_traverse (&DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8056                      attach_init_test_initialization_flags, block_body);
8057       
8058       if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
8059         {
8060           check_for_initialization (block_body, mdecl);
8061           
8062           /* Go through all the flags marking the initialization of
8063              static variables and see whether they're definitively
8064              assigned, in which case the type is remembered as
8065              definitively initialized in MDECL. */
8066           if (STATIC_CLASS_INIT_OPT_P ())
8067             {
8068               /* Always register the context as properly initialized in
8069                  MDECL. This used with caution helps removing extra
8070                  initialization of self. */
8071               if (METHOD_STATIC (mdecl))
8072                 hash_lookup (&DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
8073                              (hash_table_key) DECL_CONTEXT (mdecl),
8074                              TRUE, NULL);
8075             }
8076         }
8077       ctxp->explicit_constructor_p = 0;
8078     }
8079   
8080   BLOCK_EXPR_BODY (fbody) = block_body;
8081   
8082   /* If we saw a return but couldn't evaluate it properly, we'll have
8083      an error_mark_node here. */
8084   if (block_body != error_mark_node
8085       && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
8086       && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
8087       && !flag_emit_xref)
8088     missing_return_error (current_function_decl);
8089
8090   /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
8091   maybe_yank_clinit (mdecl);
8092
8093   /* Pop the current level, with special measures if we found errors. */
8094   if (java_error_count)
8095     pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8096   poplevel (1, 0, 1);
8097
8098   /* Pop the exceptions and sanity check */
8099   POP_EXCEPTIONS();
8100   if (currently_caught_type_list)
8101     abort ();
8102
8103   /* Restore the copy of the list of exceptions if emitting xrefs. */
8104   DECL_FUNCTION_THROWS (mdecl) = exception_copy;
8105 }
8106
8107 /* For with each class for which there's code to generate. */
8108
8109 static void
8110 java_expand_method_bodies (class)
8111      tree class;
8112 {
8113   tree decl;
8114   for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8115     {
8116       if (!DECL_FUNCTION_BODY (decl))
8117         continue;
8118
8119       current_function_decl = decl;
8120
8121       /* It's time to assign the variable flagging static class
8122          initialization based on which classes invoked static methods
8123          are definitely initializing. This should be flagged. */
8124       if (STATIC_CLASS_INIT_OPT_P ())
8125         {
8126           tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
8127           for (; list != NULL_TREE;  list = TREE_CHAIN (list))
8128             {
8129               /* Executed for each statement calling a static function.
8130                  LIST is a TREE_LIST whose PURPOSE is the called function
8131                  and VALUE is a compound whose second operand can be patched
8132                  with static class initialization flag assignments.  */
8133
8134               tree called_method = TREE_PURPOSE (list);
8135               tree compound = TREE_VALUE (list);
8136               tree assignment_compound_list
8137                 = build_tree_list (called_method, NULL);
8138
8139               /* For each class definitely initialized in
8140                  CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8141                  assignment to the class initialization flag. */
8142               hash_traverse (&DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8143                              emit_test_initialization,
8144                              assignment_compound_list);
8145
8146               if (TREE_VALUE (assignment_compound_list))
8147                 TREE_OPERAND (compound, 1)
8148                   = TREE_VALUE (assignment_compound_list);
8149             }
8150         }
8151
8152       /* Prepare the function for RTL expansion */  
8153       start_complete_expand_method (decl);
8154
8155       /* Expand function start, generate initialization flag
8156          assignment, and handle synchronized methods. */
8157       complete_start_java_method (decl);
8158
8159       /* Expand the rest of the function body and terminate
8160          expansion. */
8161       source_end_java_method ();
8162     }
8163 }
8164
8165 \f
8166
8167 /* This section of the code deals with accessing enclosing context
8168    fields either directly by using the relevant access to this$<n> or
8169    by invoking an access method crafted for that purpose.  */
8170
8171 /* Build the necessary access from an inner class to an outer
8172    class. This routine could be optimized to cache previous result
8173    (decl, current_class and returned access).  When an access method
8174    needs to be generated, it always takes the form of a read. It might
8175    be later turned into a write by calling outer_field_access_fix.  */
8176
8177 static tree
8178 build_outer_field_access (id, decl)
8179      tree id, decl;
8180 {
8181   tree access = NULL_TREE;
8182   tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8183   tree decl_ctx = DECL_CONTEXT (decl);
8184
8185   /* If the immediate enclosing context of the current class is the
8186      field decl's class or inherits from it; build the access as
8187      `this$<n>.<field>'. Note that we will break the `private' barrier
8188      if we're not emitting bytecodes. */
8189   if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8190       && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
8191     {
8192       tree thisn = build_current_thisn (current_class);
8193       access = make_qualified_primary (build_wfl_node (thisn), 
8194                                        id, EXPR_WFL_LINECOL (id));
8195     }
8196   /* Otherwise, generate access methods to outer this and access the
8197      field (either using an access method or by direct access.) */
8198   else
8199     {
8200       int lc = EXPR_WFL_LINECOL (id);
8201
8202       /* Now we chain the required number of calls to the access$0 to
8203          get a hold to the enclosing instance we need, and then we
8204          build the field access. */
8205       access = build_access_to_thisn (current_class, decl_ctx, lc);
8206
8207       /* If the field is private and we're generating bytecode, then
8208          we generate an access method */
8209       if (FIELD_PRIVATE (decl) && flag_emit_class_files )
8210         {
8211           tree name = build_outer_field_access_methods (decl);
8212           access = build_outer_field_access_expr (lc, decl_ctx,
8213                                                   name, access, NULL_TREE);
8214         }
8215       /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
8216          Once again we break the `private' access rule from a foreign
8217          class. */
8218       else
8219         access = make_qualified_primary (access, id, lc);
8220     }
8221   return resolve_expression_name (access, NULL);
8222 }
8223
8224 /* Return a non zero value if NODE describes an outer field inner
8225    access.  */
8226
8227 static int
8228 outer_field_access_p (type, decl)
8229     tree type, decl;
8230 {
8231   if (!INNER_CLASS_TYPE_P (type) 
8232       || TREE_CODE (decl) != FIELD_DECL
8233       || DECL_CONTEXT (decl) == type)
8234     return 0;
8235   
8236   /* If the inner class extends the declaration context of the field
8237      we're try to acces, then this isn't an outer field access */
8238   if (inherits_from_p (type, DECL_CONTEXT (decl)))
8239     return 0;
8240
8241   for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
8242        type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
8243     {
8244       if (type == DECL_CONTEXT (decl))
8245         return 1;
8246
8247       if (!DECL_CONTEXT (TYPE_NAME (type)))
8248         {
8249           /* Before we give up, see whether the field is inherited from
8250              the enclosing context we're considering. */
8251           if (inherits_from_p (type, DECL_CONTEXT (decl)))
8252             return 1;
8253           break;
8254         }
8255     }
8256
8257   return 0;
8258 }
8259
8260 /* Return a non zero value if NODE represents an outer field inner
8261    access that was been already expanded. As a side effect, it returns
8262    the name of the field being accessed and the argument passed to the
8263    access function, suitable for a regeneration of the access method
8264    call if necessary. */
8265
8266 static int
8267 outer_field_expanded_access_p (node, name, arg_type, arg)
8268     tree node, *name, *arg_type, *arg;
8269 {
8270   int identified = 0;
8271
8272   if (TREE_CODE (node) != CALL_EXPR)
8273     return 0;
8274
8275   /* Well, gcj generates slightly different tree nodes when compiling
8276      to native or bytecodes. It's the case for function calls. */
8277
8278   if (flag_emit_class_files 
8279       && TREE_CODE (node) == CALL_EXPR
8280       && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8281     identified = 1;
8282   else if (!flag_emit_class_files)
8283     {
8284       node = TREE_OPERAND (node, 0);
8285       
8286       if (node && TREE_OPERAND (node, 0)
8287           && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8288         {
8289           node = TREE_OPERAND (node, 0);
8290           if (TREE_OPERAND (node, 0)
8291               && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8292               && (OUTER_FIELD_ACCESS_IDENTIFIER_P 
8293                   (DECL_NAME (TREE_OPERAND (node, 0)))))
8294             identified = 1;
8295         }
8296     }
8297
8298   if (identified && name && arg_type && arg)
8299     {
8300       tree argument = TREE_OPERAND (node, 1);
8301       *name = DECL_NAME (TREE_OPERAND (node, 0));
8302       *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8303       *arg = TREE_VALUE (argument);
8304     }
8305   return identified;
8306 }
8307
8308 /* Detect in NODE an outer field read access from an inner class and
8309    transform it into a write with RHS as an argument. This function is
8310    called from the java_complete_lhs when an assignment to a LHS can
8311    be identified. */
8312
8313 static tree
8314 outer_field_access_fix (wfl, node, rhs)
8315     tree wfl, node, rhs;
8316 {
8317   tree name, arg_type, arg;
8318   
8319   if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
8320     {
8321       node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl), 
8322                                             arg_type, name, arg, rhs);
8323       return java_complete_tree (node);
8324     }
8325   return NULL_TREE;
8326 }
8327
8328 /* Construct the expression that calls an access method:
8329      <type>.access$<n>(<arg1> [, <arg2>]); 
8330
8331    ARG2 can be NULL and will be omitted in that case. It will denote a
8332    read access.  */
8333
8334 static tree
8335 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
8336     int lc;
8337     tree type, access_method_name, arg1, arg2;
8338 {
8339   tree args, cn, access;
8340
8341   args = arg1 ? arg1 : 
8342     build_wfl_node (build_current_thisn (current_class));
8343   args = build_tree_list (NULL_TREE, args);
8344
8345   if (arg2)
8346     args = tree_cons (NULL_TREE, arg2, args);
8347
8348   access = build_method_invocation (build_wfl_node (access_method_name), args);
8349   cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8350   return make_qualified_primary (cn, access, lc);
8351 }
8352
8353 static tree
8354 build_new_access_id ()
8355 {
8356   static int access_n_counter = 1;
8357   char buffer [128];
8358
8359   sprintf (buffer, "access$%d", access_n_counter++);
8360   return get_identifier (buffer);
8361 }
8362
8363 /* Create the static access functions for the outer field DECL. We define a
8364    read:
8365      TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8366        return inst$.field;
8367      }
8368    and a write access:
8369      TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8370                                      TREE_TYPE (<field>) value$) {
8371        return inst$.field = value$;
8372      }
8373    We should have a usage flags on the DECL so we can lazily turn the ones
8374    we're using for code generation. FIXME.
8375 */
8376
8377 static tree
8378 build_outer_field_access_methods (decl)
8379     tree decl;
8380 {
8381   tree id, args, stmt, mdecl;
8382   
8383   if (FIELD_INNER_ACCESS_P (decl))
8384     return FIELD_INNER_ACCESS (decl);
8385
8386   MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8387  
8388   /* Create the identifier and a function named after it. */
8389   id = build_new_access_id ();
8390
8391   /* The identifier is marked as bearing the name of a generated write
8392      access function for outer field accessed from inner classes. */
8393   OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8394
8395   /* Create the read access */
8396   args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8397   TREE_CHAIN (args) = end_params_node;
8398   stmt = make_qualified_primary (build_wfl_node (inst_id),
8399                                  build_wfl_node (DECL_NAME (decl)), 0);
8400   stmt = build_return (0, stmt);
8401   mdecl = build_outer_field_access_method (DECL_CONTEXT (decl), 
8402                                            TREE_TYPE (decl), id, args, stmt);
8403   DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8404
8405   /* Create the write access method. No write access for final variable */
8406   if (!FIELD_FINAL (decl))
8407     {
8408       args = build_tree_list (inst_id, 
8409                               build_pointer_type (DECL_CONTEXT (decl)));
8410       TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8411       TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8412       stmt = make_qualified_primary (build_wfl_node (inst_id),
8413                                      build_wfl_node (DECL_NAME (decl)), 0);
8414       stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8415                                                 build_wfl_node (wpv_id)));
8416       mdecl = build_outer_field_access_method (DECL_CONTEXT (decl), 
8417                                                TREE_TYPE (decl), id, 
8418                                                args, stmt);
8419     }
8420   DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8421
8422   /* Return the access name */
8423   return FIELD_INNER_ACCESS (decl) = id;
8424 }
8425
8426 /* Build an field access method NAME.  */
8427
8428 static tree 
8429 build_outer_field_access_method (class, type, name, args, body)
8430     tree class, type, name, args, body;
8431 {
8432   tree saved_current_function_decl, mdecl;
8433
8434   /* Create the method */
8435   mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8436   fix_method_argument_names (args, mdecl);
8437   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8438
8439   /* Attach the method body. */
8440   saved_current_function_decl = current_function_decl;
8441   start_artificial_method_body (mdecl);
8442   java_method_add_stmt (mdecl, body);
8443   end_artificial_method_body (mdecl);
8444   current_function_decl = saved_current_function_decl;
8445
8446   return mdecl;
8447 }
8448
8449 \f
8450 /* This section deals with building access function necessary for
8451    certain kinds of method invocation from inner classes.  */
8452
8453 static tree
8454 build_outer_method_access_method (decl)
8455     tree decl;
8456 {
8457   tree saved_current_function_decl, mdecl;
8458   tree args = NULL_TREE, call_args = NULL_TREE;
8459   tree carg, id, body, class;
8460   char buffer [80];
8461   int parm_id_count = 0;
8462
8463   /* Test this abort with an access to a private field */
8464   if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8465     abort ();
8466
8467   /* Check the cache first */
8468   if (DECL_FUNCTION_INNER_ACCESS (decl))
8469     return DECL_FUNCTION_INNER_ACCESS (decl);
8470
8471   class = DECL_CONTEXT (decl);
8472
8473   /* Obtain an access identifier and mark it */
8474   id = build_new_access_id ();
8475   OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8476
8477   carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8478   /* Create the arguments, as much as the original */
8479   for (; carg && carg != end_params_node; 
8480        carg = TREE_CHAIN (carg))
8481     {
8482       sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8483       args = chainon (args, build_tree_list (get_identifier (buffer), 
8484                                              TREE_VALUE (carg)));
8485     }
8486   args = chainon (args, end_params_node);
8487
8488   /* Create the method */
8489   mdecl = create_artificial_method (class, ACC_STATIC, 
8490                                     TREE_TYPE (TREE_TYPE (decl)), id, args);
8491   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8492   /* There is a potential bug here. We should be able to use
8493      fix_method_argument_names, but then arg names get mixed up and
8494      eventually a constructor will have its this$0 altered and the
8495      outer context won't be assignment properly. The test case is
8496      stub.java FIXME */
8497   TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8498
8499   /* Attach the method body. */
8500   saved_current_function_decl = current_function_decl;
8501   start_artificial_method_body (mdecl);
8502
8503   /* The actual method invocation uses the same args. When invoking a
8504      static methods that way, we don't want to skip the first
8505      argument. */
8506   carg = args;
8507   if (!METHOD_STATIC (decl))
8508     carg = TREE_CHAIN (carg);
8509   for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8510     call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8511                            call_args);
8512
8513   body = build_method_invocation (build_wfl_node (DECL_NAME (decl)), 
8514                                   call_args);
8515   if (!METHOD_STATIC (decl))
8516     body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)), 
8517                                    body, 0);
8518   if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8519     body = build_return (0, body);
8520   java_method_add_stmt (mdecl,body);
8521   end_artificial_method_body (mdecl);
8522   current_function_decl = saved_current_function_decl;
8523
8524   /* Back tag the access function so it know what it accesses */
8525   DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8526
8527   /* Tag the current method so it knows it has an access generated */
8528   return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8529 }
8530
8531 \f
8532 /* This section of the code deals with building expressions to access
8533    the enclosing instance of an inner class. The enclosing instance is
8534    kept in a generated field called this$<n>, with <n> being the
8535    inner class nesting level (starting from 0.)  */
8536     
8537 /* Build an access to a given this$<n>, always chaining access call to
8538    others. Access methods to this$<n> are build on the fly if
8539    necessary. This CAN'T be used to solely access this$<n-1> from
8540    this$<n> (which alway yield to special cases and optimization, see
8541    for example build_outer_field_access).  */
8542
8543 static tree
8544 build_access_to_thisn (from, to, lc)
8545      tree from, to;
8546      int lc;
8547 {
8548   tree access = NULL_TREE;
8549
8550   while (from != to)
8551     {
8552       if (!access)
8553         {
8554           access = build_current_thisn (from);
8555           access = build_wfl_node (access);
8556         }
8557       else
8558         {
8559           tree access0_wfl, cn;
8560
8561           maybe_build_thisn_access_method (from);
8562           access0_wfl = build_wfl_node (access0_identifier_node);
8563           cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8564           EXPR_WFL_LINECOL (access0_wfl) = lc;
8565           access = build_tree_list (NULL_TREE, access);
8566           access = build_method_invocation (access0_wfl, access);
8567           access = make_qualified_primary (cn, access, lc);
8568         }
8569
8570       /* if FROM isn't an inter class, that's fine, we've done
8571          enough. What we're looking for can be accessed from there. */
8572       from = DECL_CONTEXT (TYPE_NAME (from));
8573       if (!from)
8574         break;
8575       from = TREE_TYPE (from);
8576     }
8577   return access;
8578 }
8579
8580 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8581    is returned if nothing needs to be generated. Otherwise, the method
8582    generated and a method decl is returned.  
8583
8584    NOTE: These generated methods should be declared in a class file
8585    attribute so that they can't be referred to directly.  */
8586
8587 static tree
8588 maybe_build_thisn_access_method (type)
8589     tree type;
8590 {
8591   tree mdecl, args, stmt, rtype;
8592   tree saved_current_function_decl;
8593
8594   /* If TYPE is a top-level class, no access method is required.
8595      If there already is such an access method, bail out. */
8596   if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8597     return NULL_TREE;
8598
8599   /* We generate the method. The method looks like:
8600      static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8601   */
8602   args = build_tree_list (inst_id, build_pointer_type (type));
8603   TREE_CHAIN (args) = end_params_node;
8604   rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8605   mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8606                                     access0_identifier_node, args);
8607   fix_method_argument_names (args, mdecl);
8608   layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8609   stmt = build_current_thisn (type);
8610   stmt = make_qualified_primary (build_wfl_node (inst_id), 
8611                                  build_wfl_node (stmt), 0);
8612   stmt = build_return (0, stmt);
8613
8614   saved_current_function_decl = current_function_decl;
8615   start_artificial_method_body (mdecl);
8616   java_method_add_stmt (mdecl, stmt);
8617   end_artificial_method_body (mdecl);
8618   current_function_decl = saved_current_function_decl;
8619
8620   CLASS_ACCESS0_GENERATED_P (type) = 1;
8621
8622   return mdecl;
8623 }
8624
8625 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8626    the first level of innerclassing. this$1 for the next one, etc...
8627    This function can be invoked with TYPE to NULL, available and then
8628    has to count the parser context.  */
8629
8630 static tree
8631 build_current_thisn (type)
8632     tree type;
8633 {
8634   static int saved_i = -1;
8635   static tree saved_thisn = NULL_TREE;
8636   static tree saved_type = NULL_TREE;
8637   static int saved_type_i = 0;
8638   static int initialized_p;
8639   tree decl;
8640   char buffer [24];
8641   int i = 0;
8642
8643   /* Register SAVED_THISN and SAVED_TYPE with the garbage collector.  */
8644   if (!initialized_p)
8645     {
8646       ggc_add_tree_root (&saved_thisn, 1);
8647       ggc_add_tree_root (&saved_type, 1);
8648       initialized_p = 1;
8649     }
8650
8651   if (type)
8652     {
8653       if (type == saved_type)
8654         i = saved_type_i;
8655       else
8656         {
8657           for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type)); 
8658                decl; decl = DECL_CONTEXT (decl), i++)
8659             ;
8660       
8661           saved_type = type;
8662           saved_type_i = i;
8663         }
8664     }
8665   else
8666     i = list_length (GET_CPC_LIST ())-2;
8667
8668   if (i == saved_i)
8669     return saved_thisn;
8670     
8671   sprintf (buffer, "this$%d", i);
8672   saved_i = i;
8673   saved_thisn = get_identifier (buffer);
8674   return saved_thisn;
8675 }
8676
8677 /* Return the assignement to the hidden enclosing context `this$<n>'
8678    by the second incoming parameter to the innerclass constructor. The
8679    form used is `this.this$<n> = this$<n>;'.  */
8680
8681 static tree
8682 build_thisn_assign ()
8683 {
8684   if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8685     {
8686       tree thisn = build_current_thisn (current_class);
8687       tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8688                                          build_wfl_node (thisn), 0);
8689       tree rhs = build_wfl_node (thisn);
8690       EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8691       return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8692     }
8693   return NULL_TREE;
8694 }
8695
8696 \f
8697 /* Building the synthetic `class$' used to implement the `.class' 1.1
8698    extension for non primitive types. This method looks like:
8699
8700     static Class class$(String type) throws NoClassDefFoundError
8701     {
8702       try {return (java.lang.Class.forName (String));}
8703       catch (ClassNotFoundException e) {
8704         throw new NoClassDefFoundError(e.getMessage());}
8705     } */
8706
8707 static tree
8708 build_dot_class_method (class)
8709      tree class;
8710 {
8711 #define BWF(S) build_wfl_node (get_identifier ((S)))
8712 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8713   tree args, tmp, saved_current_function_decl, mdecl;
8714   tree stmt, throw_stmt;
8715
8716   static tree get_message_wfl, type_parm_wfl;
8717
8718   if (!get_message_wfl)
8719     {
8720       get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8721       type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8722       ggc_add_tree_root (&get_message_wfl, 1);
8723       ggc_add_tree_root (&type_parm_wfl, 1);
8724     }
8725
8726   /* Build the arguments */
8727   args = build_tree_list (get_identifier ("type$"),
8728                           build_pointer_type (string_type_node));
8729   TREE_CHAIN (args) = end_params_node;
8730
8731   /* Build the qualified name java.lang.Class.forName */
8732   tmp = MQN (MQN (MQN (BWF ("java"), 
8733                        BWF ("lang")), BWF ("Class")), BWF ("forName"));
8734   load_class (class_not_found_type_node, 1);
8735   load_class (no_class_def_found_type_node, 1);
8736   
8737   /* Create the "class$" function */
8738   mdecl = create_artificial_method (class, ACC_STATIC, 
8739                                     build_pointer_type (class_type_node),
8740                                     classdollar_identifier_node, args);
8741   DECL_FUNCTION_THROWS (mdecl) = 
8742     build_tree_list (NULL_TREE, no_class_def_found_type_node);
8743
8744   /* We start by building the try block. We need to build:
8745        return (java.lang.Class.forName (type)); */
8746   stmt = build_method_invocation (tmp, 
8747                                   build_tree_list (NULL_TREE, type_parm_wfl));
8748   stmt = build_return (0, stmt);
8749
8750   /* Now onto the catch block. We start by building the expression
8751      throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8752   throw_stmt = make_qualified_name (build_wfl_node (wpv_id), 
8753                                     get_message_wfl, 0);
8754   throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8755   
8756   /* Build new NoClassDefFoundError (_.getMessage) */
8757   throw_stmt = build_new_invocation 
8758     (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8759      build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8760
8761   /* Build the throw, (it's too early to use BUILD_THROW) */
8762   throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8763
8764   /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8765   stmt = encapsulate_with_try_catch (0, class_not_found_type_node,
8766                                      stmt, throw_stmt);
8767
8768   fix_method_argument_names (args, mdecl);
8769   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8770   saved_current_function_decl = current_function_decl;
8771   start_artificial_method_body (mdecl);
8772   java_method_add_stmt (mdecl, stmt);
8773   end_artificial_method_body (mdecl);
8774   current_function_decl = saved_current_function_decl;
8775   TYPE_DOT_CLASS (class) = mdecl;
8776
8777   return mdecl;
8778 }
8779
8780 static tree
8781 build_dot_class_method_invocation (type)
8782      tree type;
8783 {
8784   tree sig_id, s;
8785
8786   if (TYPE_ARRAY_P (type))
8787     sig_id = build_java_signature (type);
8788   else
8789     sig_id = DECL_NAME (TYPE_NAME (type));
8790
8791   /* Ensure that the proper name separator is used */
8792   sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
8793                                IDENTIFIER_LENGTH (sig_id));
8794
8795   s = build_string (IDENTIFIER_LENGTH (sig_id), 
8796                     IDENTIFIER_POINTER (sig_id));
8797   return build_method_invocation (build_wfl_node (classdollar_identifier_node),
8798                                   build_tree_list (NULL_TREE, s));
8799 }
8800
8801 /* This section of the code deals with constructor.  */
8802
8803 /* Craft a body for default constructor. Patch existing constructor
8804    bodies with call to super() and field initialization statements if
8805    necessary.  */
8806
8807 static void
8808 fix_constructors (mdecl)
8809      tree mdecl;
8810 {
8811   tree iii;                     /* Instance Initializer Invocation */
8812   tree body = DECL_FUNCTION_BODY (mdecl);
8813   tree thisn_assign, compound = NULL_TREE;
8814   tree class_type = DECL_CONTEXT (mdecl);
8815
8816   if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
8817     return;
8818   DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
8819
8820   if (!body)
8821     {
8822       /* It is an error for the compiler to generate a default
8823          constructor if the superclass doesn't have a constructor that
8824          takes no argument, or the same args for an anonymous class */
8825       if (verify_constructor_super (mdecl))
8826         {
8827           tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8828           tree save = DECL_NAME (mdecl);
8829           const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8830           DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8831           parse_error_context
8832             (lookup_cl (TYPE_NAME (class_type)), 
8833              "No constructor matching `%s' found in class `%s'",
8834              lang_printable_name (mdecl, 0), n);
8835           DECL_NAME (mdecl) = save;
8836         }
8837       
8838       /* The constructor body must be crafted by hand. It's the
8839          constructor we defined when we realize we didn't have the
8840          CLASSNAME() constructor */
8841       start_artificial_method_body (mdecl);
8842       
8843       /* Insert an assignment to the this$<n> hidden field, if
8844          necessary */
8845       if ((thisn_assign = build_thisn_assign ()))
8846         java_method_add_stmt (mdecl, thisn_assign);
8847
8848       /* We don't generate a super constructor invocation if we're
8849          compiling java.lang.Object. build_super_invocation takes care
8850          of that. */
8851       java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8852
8853       /* FIXME */
8854       if ((iii = build_instinit_invocation (class_type)))
8855         java_method_add_stmt (mdecl, iii);
8856
8857       end_artificial_method_body (mdecl);
8858     }
8859   /* Search for an explicit constructor invocation */
8860   else 
8861     {
8862       int found = 0;
8863       int invokes_this = 0;
8864       tree found_call = NULL_TREE;
8865       tree main_block = BLOCK_EXPR_BODY (body);
8866       
8867       while (body)
8868         switch (TREE_CODE (body))
8869           {
8870           case CALL_EXPR:
8871             found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8872             if (CALL_THIS_CONSTRUCTOR_P (body))
8873               invokes_this = 1;
8874             body = NULL_TREE;
8875             break;
8876           case COMPOUND_EXPR:
8877           case EXPR_WITH_FILE_LOCATION:
8878             found_call = body;
8879             body = TREE_OPERAND (body, 0);
8880             break;
8881           case BLOCK:
8882             found_call = body;
8883             body = BLOCK_EXPR_BODY (body);
8884             break;
8885           default:
8886             found = 0;
8887             body = NULL_TREE;
8888           }
8889
8890       /* Generate the assignment to this$<n>, if necessary */
8891       if ((thisn_assign = build_thisn_assign ()))
8892         compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8893
8894       /* The constructor is missing an invocation of super() */
8895       if (!found)
8896         compound = add_stmt_to_compound (compound, NULL_TREE,
8897                                          build_super_invocation (mdecl));
8898       /* Explicit super() invokation should take place before the
8899          instance initializer blocks. */
8900       else
8901         {
8902           compound = add_stmt_to_compound (compound, NULL_TREE,
8903                                            TREE_OPERAND (found_call, 0));
8904           TREE_OPERAND (found_call, 0) = empty_stmt_node;
8905         }
8906       
8907       DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
8908
8909       /* Insert the instance initializer block right after. */
8910       if (!invokes_this && (iii = build_instinit_invocation (class_type)))
8911         compound = add_stmt_to_compound (compound, NULL_TREE, iii);
8912
8913       /* Fix the constructor main block if we're adding extra stmts */
8914       if (compound)
8915         {
8916           compound = add_stmt_to_compound (compound, NULL_TREE,
8917                                            BLOCK_EXPR_BODY (main_block));
8918           BLOCK_EXPR_BODY (main_block) = compound;
8919         }
8920     }
8921 }
8922
8923 /* Browse constructors in the super class, searching for a constructor
8924    that doesn't take any argument. Return 0 if one is found, 1
8925    otherwise.  If the current class is an anonymous inner class, look
8926    for something that has the same signature. */
8927
8928 static int
8929 verify_constructor_super (mdecl)
8930      tree mdecl;
8931 {
8932   tree class = CLASSTYPE_SUPER (current_class);
8933   int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8934   tree sdecl;
8935
8936   if (!class)
8937     return 0;
8938
8939   if (ANONYMOUS_CLASS_P (current_class))
8940     {
8941       tree mdecl_arg_type;
8942       SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8943       for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8944         if (DECL_CONSTRUCTOR_P (sdecl))
8945           {
8946             tree m_arg_type;
8947             tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8948             if (super_inner)
8949               arg_type = TREE_CHAIN (arg_type);
8950             for (m_arg_type = mdecl_arg_type; 
8951                  (arg_type != end_params_node 
8952                   && m_arg_type != end_params_node);
8953                  arg_type = TREE_CHAIN (arg_type), 
8954                    m_arg_type = TREE_CHAIN (m_arg_type))
8955               if (!valid_method_invocation_conversion_p 
8956                      (TREE_VALUE (arg_type),
8957                       TREE_VALUE (m_arg_type)))
8958                 break;
8959
8960             if (arg_type == end_params_node && m_arg_type == end_params_node)
8961               return 0;
8962           }
8963     }
8964   else
8965     {
8966       for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8967         {
8968           tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8969           if (super_inner)
8970             arg = TREE_CHAIN (arg);
8971           if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8972             return 0;
8973         }
8974     }
8975   return 1;
8976 }
8977
8978 /* Generate code for all context remembered for code generation.  */
8979
8980 void
8981 java_expand_classes ()
8982 {
8983   int save_error_count = 0;
8984   static struct parser_ctxt *cur_ctxp = NULL;
8985
8986   java_parse_abort_on_error ();
8987   if (!(ctxp = ctxp_for_generation))
8988     return;
8989   java_layout_classes ();
8990   java_parse_abort_on_error ();
8991
8992   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
8993     {
8994       ctxp = cur_ctxp;
8995       input_filename = ctxp->filename;
8996       lang_init_source (2);            /* Error msgs have method prototypes */
8997       java_complete_expand_classes (); /* Complete and expand classes */
8998       java_parse_abort_on_error ();
8999     }
9000   input_filename = main_input_filename;
9001
9002   /* Find anonymous classes and expand their constructor, now they
9003      have been fixed. */
9004   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9005     {
9006       tree current;
9007       ctxp = cur_ctxp;
9008       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9009         {
9010           current_class = TREE_TYPE (current);
9011           if (ANONYMOUS_CLASS_P (current_class))
9012             {
9013               tree d;
9014               for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9015                 {
9016                   if (DECL_CONSTRUCTOR_P (d))
9017                     {
9018                       restore_line_number_status (1);
9019                       java_complete_expand_method (d);
9020                       restore_line_number_status (0);
9021                       break;    /* We now there are no other ones */
9022                     }
9023                 }
9024             }
9025         }
9026     }
9027
9028   /* If we've found error at that stage, don't try to generate
9029      anything, unless we're emitting xrefs or checking the syntax only
9030      (but not using -fsyntax-only for the purpose of generating
9031      bytecode. */
9032   if (java_error_count && !flag_emit_xref 
9033       && (!flag_syntax_only && !flag_emit_class_files))
9034     return;
9035
9036   /* Now things are stable, go for generation of the class data. */
9037
9038   /* We pessimistically marked all fields external until we knew
9039      what set of classes we were planning to compile.  Now mark
9040      those that will be generated locally as not external.  */
9041   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9042     {
9043       tree current;
9044       ctxp = cur_ctxp;
9045       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9046         {
9047           tree class = TREE_TYPE (current);
9048           tree field;
9049           for (field = TYPE_FIELDS (class); field ; field = TREE_CHAIN (field))
9050             if (FIELD_STATIC (field))
9051               DECL_EXTERNAL (field) = 0;
9052         }
9053     }
9054
9055   /* Compile the classes.  */
9056   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9057     {
9058       tree current;
9059       ctxp = cur_ctxp;
9060       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9061         {
9062           current_class = TREE_TYPE (current);
9063           outgoing_cpool = TYPE_CPOOL (current_class);
9064           if (flag_emit_class_files)
9065             write_classfile (current_class);
9066           if (flag_emit_xref)
9067             expand_xref (current_class);
9068           else if (! flag_syntax_only)
9069             {
9070               java_expand_method_bodies (current_class);
9071               finish_class ();
9072             }
9073         }
9074     }
9075 }
9076
9077 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9078    a tree list node containing RIGHT. Fore coming RIGHTs will be
9079    chained to this hook. LOCATION contains the location of the
9080    separating `.' operator.  */
9081
9082 static tree
9083 make_qualified_primary (primary, right, location)
9084      tree primary, right;
9085      int location;
9086 {
9087   tree wfl;
9088
9089   if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9090     wfl = build_wfl_wrap (primary, location);
9091   else
9092     {
9093       wfl = primary;
9094       /* If wfl wasn't qualified, we build a first anchor */
9095       if (!EXPR_WFL_QUALIFICATION (wfl))
9096         EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9097     }
9098
9099   /* And chain them */
9100   EXPR_WFL_LINECOL (right) = location;
9101   chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9102   PRIMARY_P (wfl) =  1;
9103   return wfl;
9104 }
9105
9106 /* Simple merge of two name separated by a `.' */
9107
9108 static tree
9109 merge_qualified_name (left, right)
9110      tree left, right;
9111 {
9112   tree node;
9113   if (!left && !right)
9114     return NULL_TREE;
9115
9116   if (!left)
9117     return right;
9118
9119   if (!right)
9120     return left;
9121
9122   obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9123                 IDENTIFIER_LENGTH (left));
9124   obstack_1grow (&temporary_obstack, '.');
9125   obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9126                  IDENTIFIER_LENGTH (right));
9127   node =  get_identifier (obstack_base (&temporary_obstack));
9128   obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9129   QUALIFIED_P (node) = 1;
9130   return node;
9131 }
9132
9133 /* Merge the two parts of a qualified name into LEFT.  Set the
9134    location information of the resulting node to LOCATION, usually
9135    inherited from the location information of the `.' operator. */
9136
9137 static tree
9138 make_qualified_name (left, right, location)
9139      tree left, right;
9140      int location;
9141 {
9142 #ifdef USE_COMPONENT_REF
9143   tree node = build (COMPONENT_REF, NULL_TREE, left, right);
9144   EXPR_WFL_LINECOL (node) = location;
9145   return node;
9146 #else
9147   tree left_id = EXPR_WFL_NODE (left);
9148   tree right_id = EXPR_WFL_NODE (right);
9149   tree wfl, merge;
9150
9151   merge = merge_qualified_name (left_id, right_id);
9152
9153   /* Left wasn't qualified and is now qualified */
9154   if (!QUALIFIED_P (left_id))
9155     {
9156       tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9157       EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9158       EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9159     }
9160   
9161   wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9162   EXPR_WFL_LINECOL (wfl) = location;
9163   chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9164
9165   EXPR_WFL_NODE (left) = merge;
9166   return left;
9167 #endif
9168 }
9169
9170 /* Extract the last identifier component of the qualified in WFL. The
9171    last identifier is removed from the linked list */
9172
9173 static tree
9174 cut_identifier_in_qualified (wfl)
9175      tree wfl;
9176 {
9177   tree q;
9178   tree previous = NULL_TREE;
9179   for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9180     if (!TREE_CHAIN (q))
9181       {
9182         if (!previous)
9183           /* Operating on a non qualified qualified WFL.  */
9184           abort ();
9185
9186         TREE_CHAIN (previous) = NULL_TREE;
9187         return TREE_PURPOSE (q);
9188       }
9189 }
9190
9191 /* Resolve the expression name NAME. Return its decl.  */
9192
9193 static tree
9194 resolve_expression_name (id, orig)
9195      tree id;
9196      tree *orig;
9197 {
9198   tree name = EXPR_WFL_NODE (id);
9199   tree decl;
9200
9201   /* 6.5.5.1: Simple expression names */
9202   if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9203     {
9204       /* 15.13.1: NAME can appear within the scope of a local variable
9205          declaration */
9206       if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9207         return decl;
9208
9209       /* 15.13.1: NAME can appear within a class declaration */
9210       else 
9211         {
9212           decl = lookup_field_wrapper (current_class, name);
9213           if (decl)
9214             {
9215               tree access = NULL_TREE;
9216               int fs = FIELD_STATIC (decl);
9217
9218               /* If we're accessing an outer scope local alias, make
9219                  sure we change the name of the field we're going to
9220                  build access to. */
9221               if (FIELD_LOCAL_ALIAS_USED (decl))
9222                 name = DECL_NAME (decl);
9223
9224               /* Instance variable (8.3.1.1) can't appear within
9225                  static method, static initializer or initializer for
9226                  a static variable. */
9227               if (!fs && METHOD_STATIC (current_function_decl))
9228                 {
9229                   static_ref_err (id, name, current_class);
9230                   return error_mark_node;
9231                 }
9232               /* Instance variables can't appear as an argument of
9233                  an explicit constructor invocation */
9234               if (!fs && ctxp->explicit_constructor_p
9235                   && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9236                 {
9237                   parse_error_context
9238                     (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9239                   return error_mark_node;
9240                 }
9241
9242               /* If we're processing an inner class and we're trying
9243                  to access a field belonging to an outer class, build
9244                  the access to the field */
9245               if (!fs && outer_field_access_p (current_class, decl))
9246                 {
9247                   if (CLASS_STATIC (TYPE_NAME (current_class)))
9248                     {
9249                       static_ref_err (id, DECL_NAME (decl), current_class);
9250                       return error_mark_node;
9251                     }
9252                   access = build_outer_field_access (id, decl);
9253                   if (orig)
9254                     *orig = access;
9255                   return access;
9256                 }
9257
9258               /* Otherwise build what it takes to access the field */
9259               access = build_field_ref ((fs ? NULL_TREE : current_this),
9260                                         DECL_CONTEXT (decl), name);
9261               if (fs)
9262                 access = maybe_build_class_init_for_field (decl, access);
9263               /* We may be asked to save the real field access node */
9264               if (orig)
9265                 *orig = access;
9266               /* And we return what we got */
9267               return access;
9268             }
9269           /* Fall down to error report on undefined variable */
9270         }
9271     }
9272   /* 6.5.5.2 Qualified Expression Names */
9273   else
9274     {
9275       if (orig)
9276         *orig = NULL_TREE;
9277       qualify_ambiguous_name (id);
9278       /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9279       /* 15.10.2: Accessing Superclass Members using super */
9280       return resolve_field_access (id, orig, NULL);
9281     }
9282
9283   /* We've got an error here */
9284   if (INNER_CLASS_TYPE_P (current_class))
9285     parse_error_context (id, 
9286                          "Local variable `%s' can't be accessed from within the inner class `%s' unless it is declared final",
9287                          IDENTIFIER_POINTER (name),
9288                          IDENTIFIER_POINTER (DECL_NAME
9289                                              (TYPE_NAME (current_class))));
9290   else
9291     parse_error_context (id, "Undefined variable `%s'", 
9292                          IDENTIFIER_POINTER (name));
9293
9294   return error_mark_node;
9295 }
9296
9297 static void
9298 static_ref_err (wfl, field_id, class_type)
9299     tree wfl, field_id, class_type;
9300 {
9301   parse_error_context 
9302     (wfl, 
9303      "Can't make a static reference to nonstatic variable `%s' in class `%s'",
9304      IDENTIFIER_POINTER (field_id), 
9305      IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9306 }
9307
9308 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9309    We return something suitable to generate the field access. We also
9310    return the field decl in FIELD_DECL and its type in FIELD_TYPE.  If
9311    recipient's address can be null. */
9312
9313 static tree
9314 resolve_field_access (qual_wfl, field_decl, field_type)
9315      tree qual_wfl;
9316      tree *field_decl, *field_type;
9317 {
9318   int is_static = 0;
9319   tree field_ref;
9320   tree decl, where_found, type_found;
9321
9322   if (resolve_qualified_expression_name (qual_wfl, &decl,
9323                                          &where_found, &type_found))
9324     return error_mark_node;
9325
9326   /* Resolve the LENGTH field of an array here */
9327   if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node 
9328       && type_found && TYPE_ARRAY_P (type_found) 
9329       && ! flag_emit_class_files && ! flag_emit_xref)
9330     {
9331       tree length = build_java_array_length_access (where_found);
9332       field_ref = length;
9333
9334       /* In case we're dealing with a static array, we need to
9335          initialize its class before the array length can be fetched.
9336          It's also a good time to create a DECL_RTL for the field if
9337          none already exists, otherwise if the field was declared in a
9338          class found in an external file and hasn't been (and won't
9339          be) accessed for its value, none will be created. */
9340       if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9341         {
9342           build_static_field_ref (where_found);
9343           field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9344         }
9345     }
9346   /* We might have been trying to resolve field.method(). In which
9347      case, the resolution is over and decl is the answer */
9348   else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9349     field_ref = decl;
9350   else if (JDECL_P (decl))
9351     {
9352       if (!type_found)
9353         type_found = DECL_CONTEXT (decl);
9354       is_static = FIELD_STATIC (decl);
9355       field_ref = build_field_ref ((is_static && !flag_emit_xref? 
9356                                     NULL_TREE : where_found), 
9357                                    type_found, DECL_NAME (decl));
9358       if (field_ref == error_mark_node)
9359         return error_mark_node;
9360       if (is_static)
9361         field_ref = maybe_build_class_init_for_field (decl, field_ref);
9362     }
9363   else
9364     field_ref = decl;
9365
9366   if (field_decl)
9367     *field_decl = decl;
9368   if (field_type)
9369     *field_type = (QUAL_DECL_TYPE (decl) ? 
9370                    QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9371   return field_ref;
9372 }
9373
9374 /* If NODE is an access to f static field, strip out the class
9375    initialization part and return the field decl, otherwise, return
9376    NODE. */
9377
9378 static tree
9379 strip_out_static_field_access_decl (node)
9380     tree node;
9381 {
9382   if (TREE_CODE (node) == COMPOUND_EXPR)
9383     {
9384       tree op1 = TREE_OPERAND (node, 1);
9385       if (TREE_CODE (op1) == COMPOUND_EXPR)
9386          {
9387            tree call = TREE_OPERAND (op1, 0);
9388            if (TREE_CODE (call) == CALL_EXPR
9389                && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9390                && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9391                == soft_initclass_node)
9392              return TREE_OPERAND (op1, 1);
9393          }
9394       else if (JDECL_P (op1))
9395         return op1;
9396     }
9397   return node;
9398 }
9399
9400 /* 6.5.5.2: Qualified Expression Names */
9401
9402 static int
9403 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9404      tree wfl;
9405      tree *found_decl, *type_found, *where_found;
9406 {
9407   int from_type = 0;            /* Field search initiated from a type */
9408   int from_super = 0, from_cast = 0, from_qualified_this = 0;
9409   int previous_call_static = 0;
9410   int is_static;
9411   tree decl = NULL_TREE, type = NULL_TREE, q;
9412   /* For certain for of inner class instantiation */
9413   tree saved_current, saved_this;               
9414 #define RESTORE_THIS_AND_CURRENT_CLASS                          \
9415   { current_class = saved_current; current_this = saved_this;}
9416
9417   *type_found = *where_found = NULL_TREE;
9418
9419   for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9420     {
9421       tree qual_wfl = QUAL_WFL (q);
9422       tree ret_decl;            /* for EH checking */
9423       int location;             /* for EH checking */
9424
9425       /* 15.10.1 Field Access Using a Primary */
9426       switch (TREE_CODE (qual_wfl))
9427         {
9428         case CALL_EXPR:
9429         case NEW_CLASS_EXPR:
9430           /* If the access to the function call is a non static field,
9431              build the code to access it. */
9432           if (JDECL_P (decl) && !FIELD_STATIC (decl))
9433             {
9434               decl = maybe_access_field (decl, *where_found, 
9435                                          DECL_CONTEXT (decl));
9436               if (decl == error_mark_node)
9437                 return 1;
9438             }
9439
9440           /* And code for the function call */
9441           if (complete_function_arguments (qual_wfl))
9442             return 1;
9443
9444           /* We might have to setup a new current class and a new this
9445              for the search of an inner class, relative to the type of
9446              a expression resolved as `decl'. The current values are
9447              saved and restored shortly after */
9448           saved_current = current_class;
9449           saved_this = current_this;
9450           if (decl 
9451               && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9452                   || from_qualified_this))
9453             {
9454               /* If we still have `from_qualified_this', we have the form
9455                  <T>.this.f() and we need to build <T>.this */
9456               if (from_qualified_this)
9457                 {
9458                   decl = build_access_to_thisn (current_class, type, 0);
9459                   decl = java_complete_tree (decl);
9460                   type = TREE_TYPE (TREE_TYPE (decl));
9461                 }
9462               current_class = type;
9463               current_this = decl;
9464               from_qualified_this = 0;
9465             }
9466
9467           if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9468             CALL_USING_SUPER (qual_wfl) = 1;
9469           location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9470                       EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9471           *where_found = patch_method_invocation (qual_wfl, decl, type,
9472                                                   from_super,
9473                                                   &is_static, &ret_decl);
9474           if (*where_found == error_mark_node)
9475             {
9476               RESTORE_THIS_AND_CURRENT_CLASS;
9477               return 1;
9478             }
9479           *type_found = type = QUAL_DECL_TYPE (*where_found);
9480
9481           /* If we're creating an inner class instance, check for that
9482              an enclosing instance is in scope */
9483           if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9484               && INNER_ENCLOSING_SCOPE_CHECK (type))
9485             {
9486               parse_error_context 
9487                 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9488                  lang_printable_name (type, 0),
9489                  (!current_this ? "" :
9490                   "; an explicit one must be provided when creating this inner class"));
9491               RESTORE_THIS_AND_CURRENT_CLASS;
9492               return 1;
9493             }
9494
9495           /* In case we had to change then to resolve a inner class
9496              instantiation using a primary qualified by a `new' */
9497           RESTORE_THIS_AND_CURRENT_CLASS;
9498
9499           /* EH check. No check on access$<n> functions */
9500           if (location 
9501               && !OUTER_FIELD_ACCESS_IDENTIFIER_P 
9502                     (DECL_NAME (current_function_decl)))
9503             check_thrown_exceptions (location, ret_decl);
9504
9505           /* If the previous call was static and this one is too,
9506              build a compound expression to hold the two (because in
9507              that case, previous function calls aren't transported as
9508              forcoming function's argument. */
9509           if (previous_call_static && is_static)
9510             {
9511               decl = build (COMPOUND_EXPR, TREE_TYPE (*where_found),
9512                             decl, *where_found);
9513               TREE_SIDE_EFFECTS (decl) = 1;
9514             }
9515           else
9516             {
9517               previous_call_static = is_static;
9518               decl = *where_found;
9519             }
9520           from_type = 0;
9521           continue;
9522
9523         case NEW_ARRAY_EXPR:
9524         case NEW_ANONYMOUS_ARRAY_EXPR:
9525           *where_found = decl = java_complete_tree (qual_wfl);
9526           if (decl == error_mark_node)
9527             return 1;
9528           *type_found = type = QUAL_DECL_TYPE (decl);
9529           continue;
9530
9531         case CONVERT_EXPR:
9532           *where_found = decl = java_complete_tree (qual_wfl);
9533           if (decl == error_mark_node)
9534             return 1;
9535           *type_found = type = QUAL_DECL_TYPE (decl);
9536           from_cast = 1;
9537           continue;
9538
9539         case CONDITIONAL_EXPR:
9540         case STRING_CST:
9541         case MODIFY_EXPR:
9542           *where_found = decl = java_complete_tree (qual_wfl);
9543           if (decl == error_mark_node)
9544             return 1;
9545           *type_found = type = QUAL_DECL_TYPE (decl);
9546           continue;
9547
9548         case ARRAY_REF:
9549           /* If the access to the function call is a non static field,
9550              build the code to access it. */
9551           if (JDECL_P (decl) && !FIELD_STATIC (decl))
9552             {
9553               decl = maybe_access_field (decl, *where_found, type);
9554               if (decl == error_mark_node)
9555                 return 1;
9556             }
9557           /* And code for the array reference expression */
9558           decl = java_complete_tree (qual_wfl);
9559           if (decl == error_mark_node)
9560             return 1;
9561           type = QUAL_DECL_TYPE (decl);
9562           continue;
9563
9564         case PLUS_EXPR:
9565           if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9566             return 1;
9567           if ((type = patch_string (decl)))
9568             decl = type;
9569           *where_found = QUAL_RESOLUTION (q) = decl;
9570           *type_found = type = TREE_TYPE (decl);
9571           break;
9572
9573         case CLASS_LITERAL:
9574           if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9575             return 1;
9576           *where_found = QUAL_RESOLUTION (q) = decl;
9577           *type_found = type = TREE_TYPE (decl);
9578           break;
9579
9580         default:
9581           /* Fix for -Wall Just go to the next statement. Don't
9582              continue */
9583           break;
9584         }
9585
9586       /* If we fall here, we weren't processing a (static) function call. */
9587       previous_call_static = 0;
9588
9589       /* It can be the keyword THIS */
9590       if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9591           && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9592         {
9593           if (!current_this)
9594             {
9595               parse_error_context 
9596                 (wfl, "Keyword `this' used outside allowed context");
9597               return 1;
9598             }
9599           if (ctxp->explicit_constructor_p
9600               && type == current_class)
9601             {
9602               parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9603               return 1;
9604             }
9605           /* We have to generate code for intermediate access */
9606           if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9607             {
9608               *where_found = decl = current_this;
9609               *type_found = type = QUAL_DECL_TYPE (decl);
9610             }
9611           /* We're trying to access the this from somewhere else. Make sure
9612              it's allowed before doing so. */
9613           else
9614             {
9615               if (!enclosing_context_p (type, current_class))
9616                 {
9617                   char *p  = xstrdup (lang_printable_name (type, 0));
9618                   parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'", 
9619                                        p, p, 
9620                                        lang_printable_name (current_class, 0));
9621                   free (p);
9622                   return 1;
9623                 }
9624               from_qualified_this = 1;
9625               /* If there's nothing else after that, we need to
9626                  produce something now, otherwise, the section of the
9627                  code that needs to produce <T>.this will generate
9628                  what is necessary. */
9629               if (!TREE_CHAIN (q))
9630                 {
9631                   decl = build_access_to_thisn (current_class, type, 0);
9632                   *where_found = decl = java_complete_tree (decl);
9633                   *type_found = type = TREE_TYPE (decl);
9634                 }
9635             }
9636
9637           from_type = 0;
9638           continue;
9639         }
9640
9641       /* 15.10.2 Accessing Superclass Members using SUPER */
9642       if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9643           && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9644         {
9645           tree node;
9646           /* Check on the restricted use of SUPER */
9647           if (METHOD_STATIC (current_function_decl)
9648               || current_class == object_type_node)
9649             {
9650               parse_error_context 
9651                 (wfl, "Keyword `super' used outside allowed context");
9652               return 1;
9653             }
9654           /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9655           node = build_cast (EXPR_WFL_LINECOL (qual_wfl), 
9656                              CLASSTYPE_SUPER (current_class),
9657                              build_this (EXPR_WFL_LINECOL (qual_wfl)));
9658           *where_found = decl = java_complete_tree (node);
9659           if (decl == error_mark_node)
9660             return 1;
9661           *type_found = type = QUAL_DECL_TYPE (decl);
9662           from_super = from_type = 1;
9663           continue;
9664         }
9665
9666       /* 15.13.1: Can't search for field name in packages, so we
9667          assume a variable/class name was meant. */
9668       if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9669         {
9670           tree name = resolve_package (wfl, &q);
9671           if (name)
9672             {
9673               tree list;
9674               *where_found = decl = resolve_no_layout (name, qual_wfl);
9675               /* We want to be absolutely sure that the class is laid
9676                  out. We're going to search something inside it. */
9677               *type_found = type = TREE_TYPE (decl);
9678               layout_class (type);
9679               from_type = 1;
9680
9681               /* Fix them all the way down, if any are left. */
9682               if (q)
9683                 {
9684                   list = TREE_CHAIN (q);
9685                   while (list)
9686                     {
9687                       RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9688                       RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9689                       list = TREE_CHAIN (list);
9690                     }
9691                 }
9692             }
9693           else
9694             {
9695               if (from_super || from_cast)
9696                 parse_error_context 
9697                   ((from_cast ? qual_wfl : wfl),
9698                    "No variable `%s' defined in class `%s'",
9699                    IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9700                    lang_printable_name (type, 0));
9701               else
9702                 parse_error_context
9703                   (qual_wfl, "Undefined variable or class name: `%s'",
9704                    IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9705               return 1;
9706             }
9707         }
9708
9709       /* We have a type name. It's been already resolved when the
9710          expression was qualified. */
9711       else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
9712         {
9713           decl = QUAL_RESOLUTION (q);
9714
9715           /* Sneak preview. If next we see a `new', we're facing a
9716              qualification with resulted in a type being selected
9717              instead of a field.  Report the error */
9718           if(TREE_CHAIN (q) 
9719              && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9720             {
9721               parse_error_context (qual_wfl, "Undefined variable `%s'",
9722                                    IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9723               return 1;
9724             }
9725
9726           if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
9727             {
9728               parse_error_context 
9729                 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9730                  java_accstring_lookup (get_access_flags_from_decl (decl)),
9731                  GET_TYPE_NAME (type),
9732                  IDENTIFIER_POINTER (DECL_NAME (decl)),
9733                  IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9734               return 1;
9735             }
9736           check_deprecation (qual_wfl, decl);
9737
9738           type = TREE_TYPE (decl);
9739           from_type = 1;
9740         }
9741       /* We resolve an expression name */
9742       else 
9743         {
9744           tree field_decl = NULL_TREE;
9745
9746           /* If there exists an early resolution, use it. That occurs
9747              only once and we know that there are more things to
9748              come. Don't do that when processing something after SUPER
9749              (we need more thing to be put in place below */
9750           if (!from_super && QUAL_RESOLUTION (q))
9751             {
9752               decl = QUAL_RESOLUTION (q);
9753               if (!type)
9754                 {
9755                   if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9756                     {
9757                       if (current_this)
9758                         *where_found = current_this;
9759                       else
9760                         {
9761                           static_ref_err (qual_wfl, DECL_NAME (decl),
9762                                           current_class);
9763                           return 1;
9764                         }
9765                       if (outer_field_access_p (current_class, decl))
9766                         decl = build_outer_field_access (qual_wfl, decl);
9767                     }
9768                   else
9769                     {
9770                       *where_found = TREE_TYPE (decl);
9771                       if (TREE_CODE (*where_found) == POINTER_TYPE)
9772                         *where_found = TREE_TYPE (*where_found);
9773                     }
9774                 }
9775             }
9776
9777           /* Report and error if we're using a numerical litteral as a
9778              qualifier. It can only be an INTEGER_CST. */
9779           else if (TREE_CODE (qual_wfl) == INTEGER_CST)
9780             {
9781               parse_error_context
9782                 (wfl, "Can't use type `%s' as a qualifier",
9783                  lang_printable_name (TREE_TYPE (qual_wfl), 0));
9784               return 1;
9785             }
9786
9787           /* We have to search for a field, knowing the type of its
9788              container. The flag FROM_TYPE indicates that we resolved
9789              the last member of the expression as a type name, which
9790              means that for the resolution of this field, we'll look
9791              for other errors than if it was resolved as a member of
9792              an other field. */
9793           else
9794             {
9795               int is_static;
9796               tree field_decl_type; /* For layout */
9797
9798               if (!from_type && !JREFERENCE_TYPE_P (type))
9799                 {
9800                   parse_error_context 
9801                     (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9802                      IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9803                      lang_printable_name (type, 0),
9804                      IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9805                   return 1;
9806                 }
9807               
9808               field_decl = lookup_field_wrapper (type,
9809                                                  EXPR_WFL_NODE (qual_wfl));
9810
9811               /* Maybe what we're trying to access to is an inner
9812                  class, only if decl is a TYPE_DECL. */
9813               if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
9814                 {
9815                   tree ptr, inner_decl;
9816
9817                   BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9818                   inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9819                   if (inner_decl)
9820                     {
9821                       check_inner_class_access (inner_decl, decl, qual_wfl); 
9822                       type = TREE_TYPE (inner_decl);
9823                       decl = inner_decl;
9824                       from_type = 1;
9825                       continue;
9826                     }
9827                 }
9828
9829               if (field_decl == NULL_TREE)
9830                 {
9831                   parse_error_context 
9832                     (qual_wfl, "No variable `%s' defined in type `%s'",
9833                      IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)), 
9834                      GET_TYPE_NAME (type));
9835                   return 1;
9836                 }
9837               if (field_decl == error_mark_node)
9838                 return 1;
9839
9840               /* Layout the type of field_decl, since we may need
9841                  it. Don't do primitive types or loaded classes. The
9842                  situation of non primitive arrays may not handled
9843                  properly here. FIXME */
9844               if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9845                 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9846               else
9847                 field_decl_type = TREE_TYPE (field_decl);
9848               if (!JPRIMITIVE_TYPE_P (field_decl_type) 
9849                   && !CLASS_LOADED_P (field_decl_type)
9850                   && !TYPE_ARRAY_P (field_decl_type))
9851                 resolve_and_layout (field_decl_type, NULL_TREE);
9852               
9853               /* Check on accessibility here */
9854               if (not_accessible_p (current_class, field_decl,
9855                                     DECL_CONTEXT (field_decl), from_super))
9856                 {
9857                   parse_error_context 
9858                     (qual_wfl,
9859                      "Can't access %s field `%s.%s' from `%s'",
9860                      java_accstring_lookup 
9861                        (get_access_flags_from_decl (field_decl)),
9862                      GET_TYPE_NAME (type),
9863                      IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9864                      IDENTIFIER_POINTER 
9865                        (DECL_NAME (TYPE_NAME (current_class))));
9866                   return 1;
9867                 }
9868               check_deprecation (qual_wfl, field_decl);
9869               
9870               /* There are things to check when fields are accessed
9871                  from type. There are no restrictions on a static
9872                  declaration of the field when it is accessed from an
9873                  interface */
9874               is_static = FIELD_STATIC (field_decl);
9875               if (!from_super && from_type 
9876                   && !TYPE_INTERFACE_P (type) 
9877                   && !is_static 
9878                   && (current_function_decl 
9879                       && METHOD_STATIC (current_function_decl)))
9880                 {
9881                   static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9882                   return 1;
9883                 }
9884               from_cast = from_super = 0;
9885
9886               /* It's an access from a type but it isn't static, we
9887                  make it relative to `this'. */
9888               if (!is_static && from_type)
9889                 decl = current_this;
9890
9891               /* If we need to generate something to get a proper
9892                  handle on what this field is accessed from, do it
9893                  now. */
9894               if (!is_static)
9895                 {
9896                   decl = maybe_access_field (decl, *where_found, *type_found);
9897                   if (decl == error_mark_node)
9898                     return 1;
9899                 }
9900
9901               /* We want to keep the location were found it, and the type
9902                  we found. */
9903               *where_found = decl;
9904               *type_found = type;
9905
9906               /* Generate the correct expression for field access from
9907                  qualified this */
9908               if (from_qualified_this)
9909                 {
9910                   field_decl = build_outer_field_access (qual_wfl, field_decl);
9911                   from_qualified_this = 0;
9912                 }
9913
9914               /* This is the decl found and eventually the next one to
9915                  search from */
9916               decl = field_decl;
9917             }
9918           from_type = 0;
9919           type = QUAL_DECL_TYPE (decl);
9920
9921           /* Sneak preview. If decl is qualified by a `new', report
9922              the error here to be accurate on the peculiar construct */
9923           if (TREE_CHAIN (q) 
9924               && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9925               && !JREFERENCE_TYPE_P (type))
9926             {
9927               parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'", 
9928                                    lang_printable_name (type, 0));
9929               return 1;
9930             }
9931         }
9932       /* `q' might have changed due to a after package resolution
9933          re-qualification */
9934       if (!q)
9935         break;
9936     }
9937   *found_decl = decl;
9938   return 0;
9939 }
9940
9941 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9942    can't be accessed from REFERENCE (a record type). If MEMBER
9943    features a protected access, we then use WHERE which, if non null,
9944    holds the type of MEMBER's access that is checked against
9945    6.6.2.1. This function should be used when decl is a field or a
9946    method.  */
9947
9948 static int
9949 not_accessible_p (reference, member, where, from_super)
9950      tree reference, member;
9951      tree where;
9952      int from_super;
9953 {
9954   int access_flag = get_access_flags_from_decl (member);
9955
9956   /* Inner classes are processed by check_inner_class_access */
9957   if (INNER_CLASS_TYPE_P (reference))
9958     return 0;
9959
9960   /* Access always granted for members declared public */
9961   if (access_flag & ACC_PUBLIC)
9962     return 0;
9963   
9964   /* Check access on protected members */
9965   if (access_flag & ACC_PROTECTED)
9966     {
9967       /* Access granted if it occurs from within the package
9968          containing the class in which the protected member is
9969          declared */
9970       if (class_in_current_package (DECL_CONTEXT (member)))
9971         return 0;
9972
9973       /* If accessed with the form `super.member', then access is granted */
9974       if (from_super)
9975         return 0;
9976
9977       /* If where is active, access was made through a
9978          qualifier. Access is granted if the type of the qualifier is
9979          or is a sublass of the type the access made from (6.6.2.1.)  */
9980       if (where && !inherits_from_p (reference, where))
9981         return 1;
9982
9983       /* Otherwise, access is granted if occurring from the class where
9984          member is declared or a subclass of it. Find the right
9985          context to perform the check */
9986       if (PURE_INNER_CLASS_TYPE_P (reference))
9987         {
9988           while (INNER_CLASS_TYPE_P (reference))
9989             {
9990               if (inherits_from_p (reference, DECL_CONTEXT (member)))
9991                 return 0;
9992               reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9993             }
9994         }
9995       if (inherits_from_p (reference, DECL_CONTEXT (member)))
9996         return 0;
9997       return 1;
9998     }
9999
10000   /* Check access on private members. Access is granted only if it
10001      occurs from within the class in which it is declared -- that does
10002      it for innerclasses too. */
10003   if (access_flag & ACC_PRIVATE)
10004     {
10005       if (reference == DECL_CONTEXT (member))
10006         return 0;
10007       if (enclosing_context_p (reference, DECL_CONTEXT (member)))
10008         return 0;
10009       return 1;
10010     }
10011
10012   /* Default access are permitted only when occurring within the
10013      package in which the type (REFERENCE) is declared. In other words,
10014      REFERENCE is defined in the current package */
10015   if (ctxp->package)
10016     return !class_in_current_package (reference);
10017
10018   /* Otherwise, access is granted */
10019   return 0;
10020 }
10021
10022 /* Test deprecated decl access.  */
10023 static void
10024 check_deprecation (wfl, decl)
10025      tree wfl, decl;
10026 {
10027   const char *file = DECL_SOURCE_FILE (decl);
10028   /* Complain if the field is deprecated and the file it was defined
10029      in isn't compiled at the same time the file which contains its
10030      use is */
10031   if (DECL_DEPRECATED (decl) 
10032       && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
10033     {
10034       char the [20];
10035       switch (TREE_CODE (decl))
10036         {
10037         case FUNCTION_DECL:
10038           strcpy (the, "method");
10039           break;
10040         case FIELD_DECL:
10041         case VAR_DECL:
10042           strcpy (the, "field");
10043           break;
10044         case TYPE_DECL:
10045           parse_warning_context (wfl, "The class `%s' has been deprecated",
10046                                  IDENTIFIER_POINTER (DECL_NAME (decl)));
10047           return;
10048         default:
10049           abort ();
10050         }
10051       /* Don't issue a message if the context as been deprecated as a
10052          whole. */
10053       if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
10054         parse_warning_context 
10055           (wfl, "The %s `%s' in class `%s' has been deprecated", 
10056            the, lang_printable_name (decl, 0),
10057            IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
10058     }
10059 }
10060
10061 /* Returns 1 if class was declared in the current package, 0 otherwise */
10062
10063 static int
10064 class_in_current_package (class)
10065      tree class;
10066 {
10067   static tree cache = NULL_TREE;
10068   int qualified_flag;
10069   tree left;
10070
10071   if (cache == class)
10072     return 1;
10073
10074   qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
10075
10076   /* If the current package is empty and the name of CLASS is
10077      qualified, class isn't in the current package.  If there is a
10078      current package and the name of the CLASS is not qualified, class
10079      isn't in the current package */
10080   if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
10081     return 0;
10082
10083   /* If there is not package and the name of CLASS isn't qualified,
10084      they belong to the same unnamed package */
10085   if (!ctxp->package && !qualified_flag)
10086     return 1;
10087
10088   /* Compare the left part of the name of CLASS with the package name */
10089   breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
10090   if (ctxp->package == left)
10091     {
10092       static int initialized_p;
10093       /* Register CACHE with the garbage collector.  */
10094       if (!initialized_p)
10095         {
10096           ggc_add_tree_root (&cache, 1);
10097           initialized_p = 1;
10098         }
10099
10100       cache = class;
10101       return 1;
10102     }
10103   return 0;
10104 }
10105
10106 /* This function may generate code to access DECL from WHERE. This is
10107    done only if certain conditions meet.  */
10108
10109 static tree
10110 maybe_access_field (decl, where, type)
10111   tree decl, where, type;
10112 {
10113   if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10114       && !FIELD_STATIC (decl))
10115     decl = build_field_ref (where ? where : current_this, 
10116                             (type ? type : DECL_CONTEXT (decl)),
10117                             DECL_NAME (decl));
10118   return decl;
10119 }
10120
10121 /* Build a method invocation, by patching PATCH. If non NULL
10122    and according to the situation, PRIMARY and WHERE may be
10123    used. IS_STATIC is set to 1 if the invoked function is static. */
10124
10125 static tree
10126 patch_method_invocation (patch, primary, where, from_super,
10127                         is_static, ret_decl)
10128      tree patch, primary, where;
10129      int from_super;
10130      int *is_static;
10131      tree *ret_decl;
10132 {
10133   tree wfl = TREE_OPERAND (patch, 0);
10134   tree args = TREE_OPERAND (patch, 1);
10135   tree name = EXPR_WFL_NODE (wfl);
10136   tree list;
10137   int is_static_flag = 0;
10138   int is_super_init = 0;
10139   tree this_arg = NULL_TREE;
10140   int is_array_clone_call = 0;
10141   
10142   /* Should be overriden if everything goes well. Otherwise, if
10143      something fails, it should keep this value. It stop the
10144      evaluation of a bogus assignment. See java_complete_tree,
10145      MODIFY_EXPR: for the reasons why we sometimes want to keep on
10146      evaluating an assignment */
10147   TREE_TYPE (patch) = error_mark_node;
10148
10149   /* Since lookup functions are messing with line numbers, save the
10150      context now.  */
10151   java_parser_context_save_global ();
10152
10153   /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10154
10155   /* Resolution of qualified name, excluding constructors */
10156   if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10157     {
10158       tree identifier, identifier_wfl, type, resolved;
10159       /* Extract the last IDENTIFIER of the qualified
10160          expression. This is a wfl and we will use it's location
10161          data during error report. */
10162       identifier_wfl = cut_identifier_in_qualified (wfl);
10163       identifier = EXPR_WFL_NODE (identifier_wfl);
10164       
10165       /* Given the context, IDENTIFIER is syntactically qualified
10166          as a MethodName. We need to qualify what's before */
10167       qualify_ambiguous_name (wfl);
10168       resolved = resolve_field_access (wfl, NULL, NULL);
10169
10170       if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10171          && FIELD_FINAL (resolved) 
10172          && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10173          && !flag_emit_class_files && !flag_emit_xref)
10174        resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10175
10176       if (resolved == error_mark_node)
10177         PATCH_METHOD_RETURN_ERROR ();
10178
10179       type = GET_SKIP_TYPE (resolved);
10180       resolve_and_layout (type, NULL_TREE);
10181       
10182       if (JPRIMITIVE_TYPE_P (type))
10183         {
10184           parse_error_context
10185             (identifier_wfl,
10186              "Can't invoke a method on primitive type `%s'",
10187              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10188           PATCH_METHOD_RETURN_ERROR ();         
10189         }
10190
10191       list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10192       args = nreverse (args);
10193
10194       /* We're resolving a call from a type */
10195       if (TREE_CODE (resolved) == TYPE_DECL)
10196         {
10197           if (CLASS_INTERFACE (resolved))
10198             {
10199               parse_error_context
10200                 (identifier_wfl,
10201                 "Can't make static reference to method `%s' in interface `%s'",
10202                  IDENTIFIER_POINTER (identifier), 
10203                  IDENTIFIER_POINTER (name));
10204               PATCH_METHOD_RETURN_ERROR ();
10205             }
10206           if (list && !METHOD_STATIC (list))
10207             {
10208               char *fct_name = xstrdup (lang_printable_name (list, 0));
10209               parse_error_context 
10210                 (identifier_wfl,
10211                  "Can't make static reference to method `%s %s' in class `%s'",
10212                  lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0), 
10213                  fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10214               free (fct_name);
10215               PATCH_METHOD_RETURN_ERROR ();
10216             }
10217         }
10218       else
10219         this_arg = primary = resolved;
10220       
10221       if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10222         is_array_clone_call = 1;
10223       
10224       /* IDENTIFIER_WFL will be used to report any problem further */
10225       wfl = identifier_wfl;
10226     }
10227   /* Resolution of simple names, names generated after a primary: or
10228      constructors */
10229   else
10230     {
10231       tree class_to_search = NULL_TREE;
10232       int lc;                   /* Looking for Constructor */
10233       
10234       /* We search constructor in their target class */
10235       if (CALL_CONSTRUCTOR_P (patch))
10236         {
10237           if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10238             class_to_search = EXPR_WFL_NODE (wfl);
10239           else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) == 
10240                    this_identifier_node)
10241             class_to_search = NULL_TREE;
10242           else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10243                    super_identifier_node)
10244             {
10245               is_super_init = 1;
10246               if (CLASSTYPE_SUPER (current_class))
10247                 class_to_search = 
10248                   DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10249               else
10250                 {
10251                   parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10252                   PATCH_METHOD_RETURN_ERROR ();
10253                 }
10254             }
10255
10256           /* Class to search is NULL if we're searching the current one */
10257           if (class_to_search)
10258             {
10259               class_to_search = resolve_and_layout (class_to_search, wfl);
10260
10261               if (!class_to_search)
10262                 {
10263                   parse_error_context 
10264                     (wfl, "Class `%s' not found in type declaration",
10265                      IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10266                   PATCH_METHOD_RETURN_ERROR ();
10267                 }
10268               
10269               /* Can't instantiate an abstract class, but we can
10270                  invoke it's constructor. It's use within the `new'
10271                  context is denied here. */
10272               if (CLASS_ABSTRACT (class_to_search) 
10273                   && TREE_CODE (patch) == NEW_CLASS_EXPR)
10274                 {
10275                   parse_error_context 
10276                     (wfl, "Class `%s' is an abstract class. It can't be instantiated",
10277                      IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10278                   PATCH_METHOD_RETURN_ERROR ();
10279                 }
10280
10281               class_to_search = TREE_TYPE (class_to_search);
10282             }
10283           else
10284             class_to_search = current_class;
10285           lc = 1;
10286         }
10287       /* This is a regular search in the local class, unless an
10288          alternate class is specified. */
10289       else
10290         {
10291           if (where != NULL_TREE)
10292             class_to_search = where;
10293           else if (QUALIFIED_P (name))
10294             class_to_search = current_class;
10295           else
10296             {
10297               class_to_search = current_class;
10298
10299               for (;;)
10300                 {
10301                   if (has_method (class_to_search, name))
10302                     break;
10303                   if (! INNER_CLASS_TYPE_P (class_to_search))
10304                     {
10305                       parse_error_context (wfl,
10306                                            "No method named `%s' in scope",
10307                                            IDENTIFIER_POINTER (name));
10308                       PATCH_METHOD_RETURN_ERROR ();
10309                     }
10310                   class_to_search
10311                     = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10312                 }
10313             }
10314           lc = 0;
10315         }
10316
10317       /* NAME is a simple identifier or comes from a primary. Search
10318          in the class whose declaration contain the method being
10319          invoked. */
10320       resolve_and_layout (class_to_search, NULL_TREE);
10321
10322       list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10323       /* Don't continue if no method were found, as the next statement
10324          can't be executed then. */
10325       if (!list)
10326         PATCH_METHOD_RETURN_ERROR ();
10327       
10328       if (TYPE_ARRAY_P (class_to_search)
10329           && DECL_NAME (list) == get_identifier ("clone"))
10330         is_array_clone_call = 1;
10331
10332       /* Check for static reference if non static methods */
10333       if (check_for_static_method_reference (wfl, patch, list, 
10334                                              class_to_search, primary))
10335         PATCH_METHOD_RETURN_ERROR ();
10336
10337       /* Check for inner classes creation from illegal contexts */
10338       if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10339                  && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10340           && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10341           && !DECL_INIT_P (current_function_decl))
10342         {
10343           parse_error_context 
10344             (wfl, "No enclosing instance for inner class `%s' is in scope%s",
10345              lang_printable_name (class_to_search, 0),
10346              (!current_this ? "" :
10347               "; an explicit one must be provided when creating this inner class"));
10348           PATCH_METHOD_RETURN_ERROR ();
10349         }
10350
10351       /* Non static methods are called with the current object extra
10352          argument. If patch a `new TYPE()', the argument is the value
10353          returned by the object allocator. If method is resolved as a
10354          primary, use the primary otherwise use the current THIS. */
10355       args = nreverse (args);
10356       if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10357         {
10358           this_arg = primary ? primary : current_this;
10359
10360           /* If we're using an access method, things are different.
10361              There are two familly of cases:
10362
10363              1) We're not generating bytecodes:
10364
10365              - LIST is non static. It's invocation is transformed from
10366                x(a1,...,an) into this$<n>.x(a1,....an).
10367              - LIST is static. It's invocation is transformed from
10368                x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10369
10370              2) We're generating bytecodes:
10371              
10372              - LIST is non static. It's invocation is transformed from
10373                x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10374              - LIST is static. It's invocation is transformed from
10375                x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10376
10377              Of course, this$<n> can be abitrary complex, ranging from
10378              this$0 (the immediate outer context) to 
10379              access$0(access$0(...(this$0))). 
10380              
10381              maybe_use_access_method returns a non zero value if the
10382              this_arg has to be moved into the (then generated) stub
10383              argument list. In the meantime, the selected function
10384              might have be replaced by a generated stub. */
10385           if (!primary &&
10386               maybe_use_access_method (is_super_init, &list, &this_arg))
10387             {
10388               args = tree_cons (NULL_TREE, this_arg, args);
10389               this_arg = NULL_TREE; /* So it doesn't get chained twice */
10390             }
10391         }
10392     }
10393
10394   /* Merge point of all resolution schemes. If we have nothing, this
10395      is an error, already signaled */
10396   if (!list) 
10397     PATCH_METHOD_RETURN_ERROR ();
10398
10399   /* Check accessibility, position the is_static flag, build and
10400      return the call */
10401   if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10402                         (primary ? TREE_TYPE (TREE_TYPE (primary)) : 
10403                          NULL_TREE), from_super)
10404       /* Calls to clone() on array types are permitted as a special-case. */
10405       && !is_array_clone_call)
10406     {
10407       const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10408       const char *const access =
10409         java_accstring_lookup (get_access_flags_from_decl (list));
10410       const char *const klass =
10411         IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10412       const char *const refklass =
10413         IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10414       const char *const what = (DECL_CONSTRUCTOR_P (list)
10415                                 ? "constructor" : "method");
10416       /* FIXME: WFL yields the wrong message here but I don't know
10417          what else to use.  */
10418       parse_error_context (wfl,
10419                            "Can't access %s %s `%s.%s' from `%s'",
10420                            access, what, klass, fct_name, refklass);
10421       PATCH_METHOD_RETURN_ERROR ();
10422     }
10423
10424   /* Deprecation check: check whether the method being invoked or the
10425      instance-being-created's type are deprecated. */
10426   if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10427     check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10428   else
10429     check_deprecation (wfl, list);
10430
10431   /* If invoking a innerclass constructor, there are hidden parameters
10432      to pass */
10433   if (TREE_CODE (patch) == NEW_CLASS_EXPR 
10434       && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10435     {
10436       /* And make sure we add the accessed local variables to be saved
10437          in field aliases. */
10438       args = build_alias_initializer_parameter_list
10439         (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10440
10441       /* Secretly pass the current_this/primary as a second argument */
10442       if (primary || current_this)
10443         {
10444           tree extra_arg;
10445           tree this_type = (current_this ?
10446                             TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10447           /* Method's (list) enclosing context */
10448           tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10449           /* If we have a primary, use it. */
10450           if (primary)
10451             extra_arg = primary;
10452           /* The current `this' is an inner class but isn't a direct
10453              enclosing context for the inner class we're trying to
10454              create. Build an access to the proper enclosing context
10455              and use it. */
10456           else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10457                    && this_type != TREE_TYPE (mec))
10458             {
10459
10460               extra_arg = build_access_to_thisn (current_class,
10461                                                  TREE_TYPE (mec), 0);
10462               extra_arg = java_complete_tree (extra_arg);
10463             }
10464           /* Otherwise, just use the current `this' as an enclosing
10465              context. */
10466           else
10467             extra_arg = current_this;
10468           args = tree_cons (NULL_TREE, extra_arg, args);
10469         }
10470       else
10471         args = tree_cons (NULL_TREE, integer_zero_node, args);
10472     }
10473
10474   /* This handles the situation where a constructor invocation needs
10475      to have an enclosing context passed as a second parameter (the
10476      constructor is one of an inner class. We extract it from the
10477      current function.  */
10478   if ((is_super_init || 
10479        (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10480       && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10481     {
10482       tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
10483       tree extra_arg;
10484
10485       if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
10486         {
10487           extra_arg = DECL_FUNCTION_BODY (current_function_decl);
10488           extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
10489         }
10490       else
10491         {
10492           tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
10493           extra_arg = 
10494             build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
10495           extra_arg = java_complete_tree (extra_arg);
10496         }
10497       args = tree_cons (NULL_TREE, extra_arg, args);
10498     }
10499
10500   is_static_flag = METHOD_STATIC (list);
10501   if (! is_static_flag && this_arg != NULL_TREE)
10502     args = tree_cons (NULL_TREE, this_arg, args);
10503
10504   /* In the context of an explicit constructor invocation, we can't
10505      invoke any method relying on `this'. Exceptions are: we're
10506      invoking a static function, primary exists and is not the current
10507      this, we're creating a new object. */
10508   if (ctxp->explicit_constructor_p 
10509       && !is_static_flag 
10510       && (!primary || primary == current_this)
10511       && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10512     {
10513       parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
10514       PATCH_METHOD_RETURN_ERROR ();
10515     }
10516   java_parser_context_restore_global ();
10517   if (is_static) 
10518     *is_static = is_static_flag;
10519   /* Sometimes, we want the decl of the selected method. Such as for
10520      EH checking */
10521   if (ret_decl)
10522     *ret_decl = list;
10523   patch = patch_invoke (patch, list, args);
10524
10525   /* Now is a good time to insert the call to finit$ */
10526   if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10527     {
10528       tree finit_parms, finit_call;
10529       
10530       /* Prepare to pass hidden parameters to finit$, if any. */
10531       finit_parms = build_alias_initializer_parameter_list 
10532         (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10533       
10534       finit_call = 
10535         build_method_invocation (build_wfl_node (finit_identifier_node),
10536                                  finit_parms);
10537
10538       /* Generate the code used to initialize fields declared with an
10539          initialization statement and build a compound statement along
10540          with the super constructor invocation. */
10541       CAN_COMPLETE_NORMALLY (patch) = 1;
10542       patch = build (COMPOUND_EXPR, void_type_node, patch,
10543                      java_complete_tree (finit_call));
10544     }
10545   return patch;
10546 }
10547
10548 /* Check that we're not trying to do a static reference to a method in
10549    non static method. Return 1 if it's the case, 0 otherwise. */
10550
10551 static int
10552 check_for_static_method_reference (wfl, node, method, where, primary)
10553      tree wfl, node, method, where, primary;
10554 {
10555   if (METHOD_STATIC (current_function_decl) 
10556       && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10557     {
10558       char *fct_name = xstrdup (lang_printable_name (method, 0));
10559       parse_error_context 
10560         (wfl, "Can't make static reference to method `%s %s' in class `%s'", 
10561          lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10562          IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10563       free (fct_name);
10564       return 1;
10565     }
10566   return 0;
10567 }
10568
10569 /* Fix the invocation of *MDECL if necessary in the case of a
10570    invocation from an inner class. *THIS_ARG might be modified
10571    appropriately and an alternative access to *MDECL might be
10572    returned.  */
10573
10574 static int
10575 maybe_use_access_method (is_super_init, mdecl, this_arg)
10576      int is_super_init;
10577      tree *mdecl, *this_arg;
10578 {
10579   tree ctx;
10580   tree md = *mdecl, ta = *this_arg;
10581   int to_return = 0;
10582   int non_static_context = !METHOD_STATIC (md);
10583
10584   if (is_super_init 
10585       || DECL_CONTEXT (md) == current_class
10586       || !PURE_INNER_CLASS_TYPE_P (current_class) 
10587       || DECL_FINIT_P (md)
10588       || DECL_INSTINIT_P (md))
10589     return 0;
10590   
10591   /* If we're calling a method found in an enclosing class, generate
10592      what it takes to retrieve the right this. Don't do that if we're
10593      invoking a static method. Note that if MD's type is unrelated to
10594      CURRENT_CLASS, then the current this can be used. */
10595
10596   if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10597     {
10598       ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10599       if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10600         {
10601           ta = build_current_thisn (current_class);
10602           ta = build_wfl_node (ta);
10603         }
10604       else
10605         {
10606           tree type = ctx;
10607           while (type)
10608             {
10609               maybe_build_thisn_access_method (type);
10610               if (inherits_from_p (type, DECL_CONTEXT (md)))
10611                 {
10612                   ta = build_access_to_thisn (ctx, type, 0);
10613                   break;
10614                 }
10615               type = (DECL_CONTEXT (TYPE_NAME (type)) ? 
10616                       TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10617             }
10618         }
10619       ta = java_complete_tree (ta);
10620     }
10621
10622   /* We might have to use an access method to get to MD. We can
10623      break the method access rule as far as we're not generating
10624      bytecode */
10625   if (METHOD_PRIVATE (md) && flag_emit_class_files)
10626     {
10627       md = build_outer_method_access_method (md);
10628       to_return = 1;
10629     }
10630
10631   *mdecl = md;
10632   *this_arg = ta;
10633
10634   /* Returnin a non zero value indicates we were doing a non static
10635      method invokation that is now a static invocation. It will have
10636      callee displace `this' to insert it in the regular argument
10637      list. */
10638   return (non_static_context && to_return);
10639 }
10640
10641 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10642    mode.  */
10643
10644 static tree
10645 patch_invoke (patch, method, args)
10646      tree patch, method, args;
10647 {
10648   tree dtable, func;
10649   tree original_call, t, ta;
10650   tree check = NULL_TREE;
10651
10652   /* Last step for args: convert build-in types. If we're dealing with
10653      a new TYPE() type call, the first argument to the constructor
10654      isn't found in the incoming argument list, but delivered by
10655      `new' */
10656   t = TYPE_ARG_TYPES (TREE_TYPE (method));
10657   if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10658     t = TREE_CHAIN (t);
10659   for (ta = args; t != end_params_node && ta; 
10660        t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10661     if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10662         TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10663       TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10664
10665   /* Resolve unresolved returned type isses */
10666   t = TREE_TYPE (TREE_TYPE (method));
10667   if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10668     resolve_and_layout (TREE_TYPE (t), NULL);
10669
10670   if (flag_emit_class_files || flag_emit_xref)
10671     func = method;
10672   else
10673     {
10674       switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10675         {
10676         case INVOKE_VIRTUAL:
10677           dtable = invoke_build_dtable (0, args);
10678           func = build_invokevirtual (dtable, method);
10679           break;
10680
10681         case INVOKE_NONVIRTUAL:
10682           /* If the object for the method call is null, we throw an
10683              exception.  We don't do this if the object is the current
10684              method's `this'.  In other cases we just rely on an
10685              optimization pass to eliminate redundant checks.  */
10686           if (TREE_VALUE (args) != current_this)
10687             {
10688               /* We use a save_expr here to make sure we only evaluate
10689                  the new `self' expression once.  */
10690               tree save_arg = save_expr (TREE_VALUE (args));
10691               TREE_VALUE (args) = save_arg;
10692               check = java_check_reference (save_arg, 1);
10693             }
10694           /* Fall through.  */
10695
10696         case INVOKE_SUPER:
10697         case INVOKE_STATIC:
10698           {
10699             tree signature = build_java_signature (TREE_TYPE (method));
10700             func = build_known_method_ref (method, TREE_TYPE (method),
10701                                            DECL_CONTEXT (method),
10702                                            signature, args);
10703           }
10704           break;
10705
10706         case INVOKE_INTERFACE:
10707           dtable = invoke_build_dtable (1, args);
10708           func = build_invokeinterface (dtable, method);
10709           break;
10710
10711         default:
10712           abort ();
10713         }
10714
10715       /* Ensure self_type is initialized, (invokestatic). FIXME */
10716       func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10717     }
10718
10719   TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10720   TREE_OPERAND (patch, 0) = func;
10721   TREE_OPERAND (patch, 1) = args;
10722   patch = check_for_builtin (method, patch);
10723   original_call = patch;
10724
10725   /* We're processing a `new TYPE ()' form. New is called and its
10726      returned value is the first argument to the constructor. We build
10727      a COMPOUND_EXPR and use saved expression so that the overall NEW
10728      expression value is a pointer to a newly created and initialized
10729      class. */
10730   if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10731     {
10732       tree class = DECL_CONTEXT (method);
10733       tree c1, saved_new, size, new;
10734       tree alloc_node;
10735
10736       if (flag_emit_class_files || flag_emit_xref)
10737         {
10738           TREE_TYPE (patch) = build_pointer_type (class);
10739           return patch;
10740         }
10741       if (!TYPE_SIZE (class))
10742         safe_layout_class (class);
10743       size = size_in_bytes (class);
10744       alloc_node =
10745         (class_has_finalize_method (class) ? alloc_object_node
10746                                            : alloc_no_finalizer_node);
10747       new = build (CALL_EXPR, promote_type (class),
10748                    build_address_of (alloc_node),
10749                    tree_cons (NULL_TREE, build_class_ref (class),
10750                               build_tree_list (NULL_TREE, 
10751                                                size_in_bytes (class))),
10752                    NULL_TREE);
10753       saved_new = save_expr (new);
10754       c1 = build_tree_list (NULL_TREE, saved_new);
10755       TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10756       TREE_OPERAND (original_call, 1) = c1;
10757       TREE_SET_CODE (original_call, CALL_EXPR);
10758       patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10759     }
10760
10761   /* If CHECK is set, then we are building a check to see if the object
10762      is NULL.  */
10763   if (check != NULL_TREE)
10764     {
10765       patch = build (COMPOUND_EXPR, TREE_TYPE (patch), check, patch);
10766       TREE_SIDE_EFFECTS (patch) = 1;
10767     }
10768
10769   /* In order to be able to modify PATCH later, we SAVE_EXPR it and
10770      put it as the first expression of a COMPOUND_EXPR. The second
10771      expression being an empty statement to be later patched if
10772      necessary. We remember a TREE_LIST (the PURPOSE is the method,
10773      the VALUE is the compound) in a hashtable and return a
10774      COMPOUND_EXPR built so that the result of the evaluation of the
10775      original PATCH node is returned. */
10776   if (STATIC_CLASS_INIT_OPT_P ()
10777       && current_function_decl && METHOD_STATIC (method))
10778     {
10779       tree list;
10780       tree fndecl = current_function_decl;
10781       tree save = save_expr (patch);
10782       tree type = TREE_TYPE (patch);
10783
10784       patch = build (COMPOUND_EXPR, type, save, empty_stmt_node);
10785       list = tree_cons (method, patch,
10786                         DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
10787
10788       DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
10789
10790       patch = build (COMPOUND_EXPR, type, patch, save);
10791     }
10792
10793   return patch;
10794 }
10795
10796 static int
10797 invocation_mode (method, super)
10798      tree method;
10799      int super;
10800 {
10801   int access = get_access_flags_from_decl (method);
10802
10803   if (super)
10804     return INVOKE_SUPER;
10805
10806   if (access & ACC_STATIC)
10807     return INVOKE_STATIC;
10808
10809   /* We have to look for a constructor before we handle nonvirtual
10810      calls; otherwise the constructor will look nonvirtual.  */
10811   if (DECL_CONSTRUCTOR_P (method))
10812     return INVOKE_STATIC;
10813
10814   if (access & ACC_FINAL || access & ACC_PRIVATE)
10815     return INVOKE_NONVIRTUAL;
10816
10817   if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10818     return INVOKE_NONVIRTUAL;
10819
10820   if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10821     return INVOKE_INTERFACE;
10822
10823   return INVOKE_VIRTUAL;
10824 }
10825
10826 /* Retrieve a refined list of matching methods. It covers the step
10827    15.11.2 (Compile-Time Step 2) */
10828
10829 static tree
10830 lookup_method_invoke (lc, cl, class, name, arg_list)
10831      int lc;
10832      tree cl;
10833      tree class, name, arg_list;
10834 {
10835   tree atl = end_params_node;           /* Arg Type List */
10836   tree method, signature, list, node;
10837   const char *candidates;               /* Used for error report */
10838   char *dup;
10839
10840   /* Fix the arguments */
10841   for (node = arg_list; node; node = TREE_CHAIN (node))
10842     {
10843       tree current_arg = TREE_TYPE (TREE_VALUE (node));
10844       /* Non primitive type may have to be resolved */
10845       if (!JPRIMITIVE_TYPE_P (current_arg))
10846         resolve_and_layout (current_arg, NULL_TREE);
10847       /* And promoted */
10848       if (TREE_CODE (current_arg) == RECORD_TYPE)
10849         current_arg = promote_type (current_arg);
10850       atl = tree_cons (NULL_TREE, current_arg, atl);
10851     }
10852
10853   /* Presto. If we're dealing with an anonymous class and a
10854      constructor call, generate the right constructor now, since we
10855      know the arguments' types. */
10856
10857   if (lc && ANONYMOUS_CLASS_P (class))
10858     craft_constructor (TYPE_NAME (class), atl);
10859
10860   /* Find all candidates and then refine the list, searching for the
10861      most specific method. */
10862   list = find_applicable_accessible_methods_list (lc, class, name, atl);
10863   list = find_most_specific_methods_list (list);
10864   if (list && !TREE_CHAIN (list))
10865     return TREE_VALUE (list);
10866
10867   /* Issue an error. List candidates if any. Candidates are listed
10868      only if accessible (non accessible methods may end-up here for
10869      the sake of a better error report). */
10870   candidates = NULL;
10871   if (list)
10872     {
10873       tree current;
10874       obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10875       for (current = list; current; current = TREE_CHAIN (current))
10876         {
10877           tree cm = TREE_VALUE (current);
10878           char string [4096];
10879           if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
10880             continue;
10881           sprintf 
10882             (string, "  `%s' in `%s'%s",
10883              get_printable_method_name (cm),
10884              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10885              (TREE_CHAIN (current) ? "\n" : ""));
10886           obstack_grow (&temporary_obstack, string, strlen (string));
10887         }
10888       obstack_1grow (&temporary_obstack, '\0');
10889       candidates = obstack_finish (&temporary_obstack);
10890     }
10891   /* Issue the error message */
10892   method = make_node (FUNCTION_TYPE);
10893   TYPE_ARG_TYPES (method) = atl;
10894   signature = build_java_argument_signature (method);
10895   dup = xstrdup (lang_printable_name (class, 0));
10896   parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10897                        (lc ? "constructor" : "method"),
10898                        (lc ? dup : IDENTIFIER_POINTER (name)),
10899                        IDENTIFIER_POINTER (signature), dup,
10900                        (candidates ? candidates : ""));
10901   free (dup);
10902   return NULL_TREE;
10903 }
10904
10905 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10906    when we're looking for a constructor. */
10907
10908 static tree
10909 find_applicable_accessible_methods_list (lc, class, name, arglist)
10910      int lc;
10911      tree class, name, arglist;
10912 {
10913   static struct hash_table t, *searched_classes = NULL;
10914   static int search_not_done = 0;
10915   tree list = NULL_TREE, all_list = NULL_TREE;
10916
10917   /* Check the hash table to determine if this class has been searched 
10918      already. */
10919   if (searched_classes)
10920     {
10921       if (hash_lookup (searched_classes, 
10922                       (const hash_table_key) class, FALSE, NULL))
10923        return NULL;
10924     }
10925   else
10926     {
10927       hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10928                       java_hash_compare_tree_node);
10929       searched_classes = &t;
10930     }
10931     
10932   search_not_done++;
10933   hash_lookup (searched_classes, 
10934                (const hash_table_key) class, TRUE, NULL);
10935
10936   if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10937     {
10938       load_class (class, 1);
10939       safe_layout_class (class);
10940     }
10941
10942   /* Search interfaces */
10943   if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL 
10944       && CLASS_INTERFACE (TYPE_NAME (class)))
10945     {
10946       int i, n;
10947       tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10948       search_applicable_methods_list (lc, TYPE_METHODS (class), 
10949                                       name, arglist, &list, &all_list);
10950       n = TREE_VEC_LENGTH (basetype_vec);
10951       for (i = 1; i < n; i++)
10952         {
10953           tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10954           tree rlist;
10955
10956           rlist = find_applicable_accessible_methods_list (lc,  t, name, 
10957                                                            arglist);
10958           list = chainon (rlist, list);
10959         }
10960     }
10961   /* Search classes */
10962   else
10963     {
10964       search_applicable_methods_list (lc, TYPE_METHODS (class), 
10965                                       name, arglist, &list, &all_list);
10966
10967       /* When looking finit$, class$ or instinit$, we turn LC to 1 so
10968          that we only search in class. Note that we should have found
10969          something at this point. */
10970       if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
10971         {
10972           lc = 1;
10973           if (!list)
10974             abort ();
10975         }
10976
10977       /* We must search all interfaces of this class */
10978       if (!lc)
10979       {
10980         tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10981         int n = TREE_VEC_LENGTH (basetype_vec), i;
10982         for (i = 1; i < n; i++)
10983           {
10984             tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10985             if (t != object_type_node)
10986               {
10987                 tree rlist
10988                   = find_applicable_accessible_methods_list (lc, t,
10989                                                              name, arglist);
10990                 list = chainon (rlist, list);
10991               }
10992           }
10993       }
10994
10995       /* Search superclass */
10996       if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10997         {
10998           tree rlist;
10999           class = CLASSTYPE_SUPER (class);
11000           rlist = find_applicable_accessible_methods_list (lc, class, 
11001                                                            name, arglist);
11002           list = chainon (rlist, list);
11003         }
11004     }
11005
11006   search_not_done--;
11007
11008   /* We're done. Reset the searched classes list and finally search
11009      java.lang.Object if it wasn't searched already. */
11010   if (!search_not_done)
11011     {
11012       if (!lc
11013           && TYPE_METHODS (object_type_node)
11014           && !hash_lookup (searched_classes, 
11015                            (const hash_table_key) object_type_node, 
11016                            FALSE, NULL))
11017         {
11018           search_applicable_methods_list (lc, 
11019                                           TYPE_METHODS (object_type_node),
11020                                           name, arglist, &list, &all_list);
11021         }
11022       hash_table_free (searched_classes);
11023       searched_classes = NULL;
11024     }
11025
11026   /* Either return the list obtained or all selected (but
11027      inaccessible) methods for better error report. */
11028   return (!list ? all_list : list);
11029 }
11030
11031 /* Effectively search for the appropriate method in method */
11032
11033 static void 
11034 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
11035      int lc;
11036      tree method, name, arglist;
11037      tree *list, *all_list;
11038 {
11039   for (; method; method = TREE_CHAIN (method))
11040     {
11041       /* When dealing with constructor, stop here, otherwise search
11042          other classes */
11043       if (lc && !DECL_CONSTRUCTOR_P (method))
11044         continue;
11045       else if (!lc && (DECL_CONSTRUCTOR_P (method) 
11046                        || (DECL_NAME (method) != name)))
11047         continue;
11048
11049       if (argument_types_convertible (method, arglist))
11050         {
11051           /* Retain accessible methods only */
11052           if (!not_accessible_p (DECL_CONTEXT (current_function_decl), 
11053                                  method, NULL_TREE, 0))
11054             *list = tree_cons (NULL_TREE, method, *list);
11055           else
11056             /* Also retain all selected method here */
11057             *all_list = tree_cons (NULL_TREE, method, *list);
11058         }
11059     }
11060 }
11061
11062 /* 15.11.2.2 Choose the Most Specific Method */
11063
11064 static tree
11065 find_most_specific_methods_list (list)
11066      tree list;
11067 {
11068   int max = 0;
11069   int abstract, candidates;
11070   tree current, new_list = NULL_TREE;
11071   for (current = list; current; current = TREE_CHAIN (current))
11072     {
11073       tree method;
11074       DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
11075
11076       for (method = list; method; method = TREE_CHAIN (method))
11077         {
11078           tree method_v, current_v;
11079           /* Don't test a method against itself */
11080           if (method == current)
11081             continue;
11082
11083           method_v = TREE_VALUE (method);
11084           current_v = TREE_VALUE (current);
11085
11086           /* Compare arguments and location where methods where declared */
11087           if (argument_types_convertible (method_v, current_v))
11088             {
11089               if (valid_method_invocation_conversion_p 
11090                   (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
11091                   || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
11092                       && enclosing_context_p (DECL_CONTEXT (method_v),
11093                                               DECL_CONTEXT (current_v))))
11094                 {
11095                   int v = (DECL_SPECIFIC_COUNT (current_v) += 
11096                     (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
11097                   max = (v > max ? v : max);
11098                 }
11099             }
11100         }
11101     }
11102
11103   /* Review the list and select the maximally specific methods */
11104   for (current = list, abstract = -1, candidates = -1;
11105        current; current = TREE_CHAIN (current))
11106     if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11107       {
11108         new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11109         abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11110         candidates++;
11111       }
11112
11113   /* If we have several and they're all abstract, just pick the
11114      closest one. */
11115   if (candidates > 0 && (candidates == abstract))
11116     {
11117       new_list = nreverse (new_list);
11118       TREE_CHAIN (new_list) = NULL_TREE;
11119     }
11120
11121   /* We have several (we couldn't find a most specific), all but one
11122      are abstract, we pick the only non abstract one. */
11123   if (candidates > 0 && (candidates == abstract+1))
11124     {
11125       for (current = new_list; current; current = TREE_CHAIN (current))
11126         if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11127           {
11128             TREE_CHAIN (current) = NULL_TREE;
11129             new_list = current;
11130           }
11131     }
11132
11133   /* If we can't find one, lower expectations and try to gather multiple
11134      maximally specific methods */
11135   while (!new_list && max)
11136     {
11137       while (--max > 0)
11138         {
11139           if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11140             new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11141         }
11142     }
11143
11144   return new_list;
11145 }
11146
11147 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
11148    converted by method invocation conversion (5.3) to the type of the
11149    corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11150    to change less often than M1. */
11151
11152 static int
11153 argument_types_convertible (m1, m2_or_arglist)
11154     tree m1, m2_or_arglist;
11155 {
11156   static tree m2_arg_value = NULL_TREE;
11157   static tree m2_arg_cache = NULL_TREE;
11158   static int initialized_p;
11159
11160   register tree m1_arg, m2_arg;
11161
11162   /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
11163      collector.  */
11164   if (!initialized_p)
11165     {
11166       ggc_add_tree_root (&m2_arg_value, 1);
11167       ggc_add_tree_root (&m2_arg_cache, 1);
11168       initialized_p = 1;
11169     }
11170
11171   SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11172
11173   if (m2_arg_value == m2_or_arglist)
11174     m2_arg = m2_arg_cache;
11175   else
11176     {
11177       /* M2_OR_ARGLIST can be a function DECL or a raw list of
11178          argument types */
11179       if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11180         {
11181           m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11182           if (!METHOD_STATIC (m2_or_arglist))
11183             m2_arg = TREE_CHAIN (m2_arg);
11184         }
11185       else
11186         m2_arg = m2_or_arglist;
11187
11188       m2_arg_value = m2_or_arglist;
11189       m2_arg_cache = m2_arg;
11190     }
11191
11192   while (m1_arg != end_params_node && m2_arg != end_params_node)
11193     {
11194       resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11195       if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11196                                                  TREE_VALUE (m2_arg)))
11197         break;
11198       m1_arg = TREE_CHAIN (m1_arg);
11199       m2_arg = TREE_CHAIN (m2_arg);
11200     }
11201   return m1_arg == end_params_node && m2_arg == end_params_node;
11202 }
11203
11204 /* Qualification routines */
11205
11206 static void
11207 qualify_ambiguous_name (id)
11208      tree id;
11209 {
11210   tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
11211     saved_current_class;
11212   int again, super_found = 0, this_found = 0, new_array_found = 0;
11213   int code;
11214
11215   /* We first qualify the first element, then derive qualification of
11216      others based on the first one. If the first element is qualified
11217      by a resolution (field or type), this resolution is stored in the
11218      QUAL_RESOLUTION of the qual element being examined. We need to
11219      save the current_class since the use of SUPER might change the
11220      its value. */
11221   saved_current_class = current_class;
11222   qual = EXPR_WFL_QUALIFICATION (id);
11223   do {
11224
11225     /* Simple qualified expression feature a qual_wfl that is a
11226        WFL. Expression derived from a primary feature more complicated
11227        things like a CALL_EXPR. Expression from primary need to be
11228        worked out to extract the part on which the qualification will
11229        take place. */
11230     qual_wfl = QUAL_WFL (qual);
11231     switch (TREE_CODE (qual_wfl))
11232       {
11233       case CALL_EXPR:
11234         qual_wfl = TREE_OPERAND (qual_wfl, 0);
11235         if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
11236           {
11237             qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11238             qual_wfl = QUAL_WFL (qual);
11239           }
11240         break;
11241       case NEW_ARRAY_EXPR:
11242       case NEW_ANONYMOUS_ARRAY_EXPR:
11243         qual = TREE_CHAIN (qual);
11244         again = new_array_found = 1;
11245         continue;
11246       case CONVERT_EXPR:
11247         break;
11248       case NEW_CLASS_EXPR:
11249         qual_wfl = TREE_OPERAND (qual_wfl, 0);
11250         break;
11251       case ARRAY_REF:
11252         while (TREE_CODE (qual_wfl) == ARRAY_REF)
11253           qual_wfl = TREE_OPERAND (qual_wfl, 0);
11254         break;
11255       case STRING_CST:
11256         qual = TREE_CHAIN (qual);
11257         qual_wfl = QUAL_WFL (qual);
11258         break;
11259       case CLASS_LITERAL:
11260         qual = TREE_CHAIN (qual);
11261         qual_wfl = QUAL_WFL (qual);
11262       break;
11263       default:
11264         /* Fix for -Wall. Just break doing nothing */
11265         break;
11266       }
11267
11268     ptr_type = current_class;
11269     again = 0;
11270     code = TREE_CODE (qual_wfl);
11271
11272     /* Pos evaluation: non WFL leading expression nodes */
11273     if (code == CONVERT_EXPR
11274         && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
11275       name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
11276
11277     else if (code == INTEGER_CST)
11278       name = qual_wfl;
11279     
11280     else if (code == CONVERT_EXPR &&
11281              TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11282       name = TREE_OPERAND (qual_wfl, 0);
11283     
11284     else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
11285              TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11286       name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
11287
11288     else if (code == TREE_LIST)
11289       name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
11290
11291     else if (code == STRING_CST || code == CONDITIONAL_EXPR 
11292              || code == PLUS_EXPR)
11293       {
11294         qual = TREE_CHAIN (qual);
11295         qual_wfl = QUAL_WFL (qual);
11296         again = 1;
11297       }
11298     else
11299       {
11300         name = EXPR_WFL_NODE (qual_wfl);
11301         if (!name)
11302           {
11303             qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11304             again = 1;
11305           }
11306       }
11307
11308     /* If we have a THIS (from a primary), we set the context accordingly */
11309     if (name == this_identifier_node)
11310       {
11311         /* This isn't really elegant. One more added irregularity
11312            before I start using COMPONENT_REF (hopefully very soon.)  */
11313         if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
11314             && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
11315                EXPR_WITH_FILE_LOCATION
11316             && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) == 
11317                this_identifier_node)
11318             {
11319               qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
11320               qual = EXPR_WFL_QUALIFICATION (qual);
11321             }
11322         qual = TREE_CHAIN (qual);
11323         qual_wfl = QUAL_WFL (qual);
11324         if (TREE_CODE (qual_wfl) == CALL_EXPR)
11325           again = 1;
11326         else if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION)
11327           name = EXPR_WFL_NODE (qual_wfl);
11328         else if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
11329           name = TREE_OPERAND (qual_wfl, 0);
11330         this_found = 1;
11331       }
11332     /* If we have a SUPER, we set the context accordingly */
11333     if (name == super_identifier_node)
11334       {
11335         current_class = CLASSTYPE_SUPER (ptr_type);
11336         /* Check that there is such a thing as a super class. If not,
11337            return.  The error will be caught later on, during the
11338            resolution */
11339         if (!current_class)
11340           {
11341             current_class = saved_current_class;
11342             return;
11343           }
11344         qual = TREE_CHAIN (qual);
11345         /* Do one more interation to set things up */
11346         super_found = again = 1;
11347       }
11348   } while (again);
11349   
11350   /* If name appears within the scope of a local variable declaration
11351      or parameter declaration, then it is an expression name. We don't
11352      carry this test out if we're in the context of the use of SUPER
11353      or THIS */
11354   if (!this_found && !super_found 
11355       && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
11356       && (decl = IDENTIFIER_LOCAL_VALUE (name)))
11357     {
11358       RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11359       QUAL_RESOLUTION (qual) = decl;
11360     }
11361
11362   /* If within the class/interface NAME was found to be used there
11363      exists a (possibly inherited) field named NAME, then this is an
11364      expression name. If we saw a NEW_ARRAY_EXPR before and want to
11365      address length, it is OK. */
11366   else if ((decl = lookup_field_wrapper (ptr_type, name))
11367            || name == length_identifier_node)
11368     {
11369       RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11370       QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
11371     }
11372
11373   /* We reclassify NAME as yielding to a type name resolution if:
11374      - NAME is a class/interface declared within the compilation
11375        unit containing NAME,
11376      - NAME is imported via a single-type-import declaration,
11377      - NAME is declared in an another compilation unit of the package
11378        of the compilation unit containing NAME,
11379      - NAME is declared by exactly on type-import-on-demand declaration
11380      of the compilation unit containing NAME. 
11381      - NAME is actually a STRING_CST.
11382      This can't happen if the expression was qualified by `this.' */
11383   else if (! this_found &&
11384            (TREE_CODE (name) == STRING_CST ||
11385             TREE_CODE (name) == INTEGER_CST ||
11386             (decl = resolve_and_layout (name, NULL_TREE))))
11387     {
11388       RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11389       QUAL_RESOLUTION (qual) = decl;
11390     }
11391
11392   /* Method call, array references and cast are expression name */
11393   else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
11394            || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
11395            || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR
11396            || TREE_CODE (QUAL_WFL (qual)) == MODIFY_EXPR)
11397     RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11398
11399   /* Check here that NAME isn't declared by more than one
11400      type-import-on-demand declaration of the compilation unit
11401      containing NAME. FIXME */
11402
11403   /* Otherwise, NAME is reclassified as a package name */
11404   else 
11405     RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11406
11407   /* Propagate the qualification accross other components of the
11408      qualified name */
11409   for (qual = TREE_CHAIN (qual); qual;
11410        qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11411     {
11412       if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11413         RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11414       else 
11415         RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
11416     }
11417
11418   /* Store the global qualification for the ambiguous part of ID back
11419      into ID fields */
11420   if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
11421     RESOLVE_EXPRESSION_NAME_P (id) = 1;
11422   else if (RESOLVE_TYPE_NAME_P (qual_wfl))
11423     RESOLVE_TYPE_NAME_P (id) = 1;
11424   else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11425     RESOLVE_PACKAGE_NAME_P (id) = 1;
11426
11427   /* Restore the current class */
11428   current_class = saved_current_class;
11429 }
11430
11431 static int
11432 breakdown_qualified (left, right, source)
11433     tree *left, *right, source;
11434 {
11435   char *p, *base;
11436   int   l = IDENTIFIER_LENGTH (source);
11437
11438   base = alloca (l + 1);
11439   memcpy (base, IDENTIFIER_POINTER (source), l + 1);
11440
11441   /* Breakdown NAME into REMAINDER . IDENTIFIER */
11442   p = base + l - 1;
11443   while (*p != '.' && p != base)
11444     p--;
11445
11446   /* We didn't find a '.'. Return an error */
11447   if (p == base)
11448     return 1;
11449
11450   *p = '\0';
11451   if (right)
11452     *right = get_identifier (p+1);
11453   *left = get_identifier (base);
11454   
11455   return 0;
11456 }
11457
11458 /* Return TRUE if two classes are from the same package. */
11459
11460 static int
11461 in_same_package (name1, name2)
11462   tree name1, name2;
11463 {
11464   tree tmp;
11465   tree pkg1;
11466   tree pkg2;
11467   
11468   if (TREE_CODE (name1) == TYPE_DECL)
11469     name1 = DECL_NAME (name1);
11470   if (TREE_CODE (name2) == TYPE_DECL)
11471     name2 = DECL_NAME (name2);
11472
11473   if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
11474     /* One in empty package. */
11475     return 0;
11476
11477   if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
11478     /* Both in empty package. */
11479     return 1;
11480
11481   breakdown_qualified (&pkg1, &tmp, name1);
11482   breakdown_qualified (&pkg2, &tmp, name2);
11483   
11484   return (pkg1 == pkg2);
11485 }
11486
11487 /* Patch tree nodes in a function body. When a BLOCK is found, push
11488    local variable decls if present.
11489    Same as java_complete_lhs, but does resolve static finals to values. */
11490
11491 static tree
11492 java_complete_tree (node)
11493      tree node;
11494 {
11495   node = java_complete_lhs (node);
11496   if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11497       && DECL_INITIAL (node) != NULL_TREE
11498       && !flag_emit_xref)
11499     {
11500       tree value = DECL_INITIAL (node);
11501       DECL_INITIAL (node) = NULL_TREE;
11502       value = fold_constant_for_init (value, node);
11503       DECL_INITIAL (node) = value;
11504       if (value != NULL_TREE)
11505         {
11506           /* fold_constant_for_init sometimes widens the original type
11507              of the constant (i.e. byte to int). It's not desirable,
11508              especially if NODE is a function argument. */
11509           if ((TREE_CODE (value) == INTEGER_CST
11510                || TREE_CODE (value) == REAL_CST)
11511               && TREE_TYPE (node) != TREE_TYPE (value))
11512             return convert (TREE_TYPE (node), value);
11513           else
11514             return value;
11515         }
11516     }
11517   return node;
11518 }
11519
11520 static tree
11521 java_stabilize_reference (node)
11522      tree node;
11523 {
11524   if (TREE_CODE (node) == COMPOUND_EXPR)
11525     {
11526       tree op0 = TREE_OPERAND (node, 0);
11527       tree op1 = TREE_OPERAND (node, 1);
11528       TREE_OPERAND (node, 0) = save_expr (op0);
11529       TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11530       return node;
11531     }
11532   return stabilize_reference (node);
11533 }
11534
11535 /* Patch tree nodes in a function body. When a BLOCK is found, push
11536    local variable decls if present.
11537    Same as java_complete_tree, but does not resolve static finals to values. */
11538
11539 static tree
11540 java_complete_lhs (node)
11541      tree node;
11542 {
11543   tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11544   int flag;
11545
11546   /* CONVERT_EXPR always has its type set, even though it needs to be
11547      worked out. */
11548   if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11549     return node;
11550
11551   /* The switch block implements cases processing container nodes
11552      first.  Contained nodes are always written back. Leaves come
11553      next and return a value. */
11554   switch (TREE_CODE (node))
11555     {
11556     case BLOCK:
11557
11558       /* 1- Block section.
11559          Set the local values on decl names so we can identify them
11560          faster when they're referenced. At that stage, identifiers
11561          are legal so we don't check for declaration errors. */
11562       for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11563         {
11564           DECL_CONTEXT (cn) = current_function_decl;
11565           IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11566         }
11567       if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11568           CAN_COMPLETE_NORMALLY (node) = 1;
11569       else
11570         {
11571           tree stmt = BLOCK_EXPR_BODY (node);
11572           tree *ptr;
11573           int error_seen = 0;
11574           if (TREE_CODE (stmt) == COMPOUND_EXPR)
11575             {
11576               /* Re-order from (((A; B); C); ...; Z) to 
11577                  (A; (B; (C ; (...; Z)))).
11578                  This makes it easier to scan the statements left-to-right
11579                  without using recursion (which might overflow the stack
11580                  if the block has many statements. */
11581               for (;;)
11582                 {
11583                   tree left = TREE_OPERAND (stmt, 0);
11584                   if (TREE_CODE (left) != COMPOUND_EXPR)
11585                     break;
11586                   TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11587                   TREE_OPERAND (left, 1) = stmt;
11588                   stmt = left;
11589                 }
11590               BLOCK_EXPR_BODY (node) = stmt;
11591             }
11592
11593           /* Now do the actual complete, without deep recursion for
11594              long blocks. */
11595           ptr = &BLOCK_EXPR_BODY (node);
11596           while (TREE_CODE (*ptr) == COMPOUND_EXPR
11597                  && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
11598             {
11599               tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11600               tree *next = &TREE_OPERAND (*ptr, 1);
11601               TREE_OPERAND (*ptr, 0) = cur;
11602               if (cur == empty_stmt_node)
11603                 {
11604                   /* Optimization;  makes it easier to detect empty bodies.
11605                      Most useful for <clinit> with all-constant initializer. */
11606                   *ptr = *next;
11607                   continue;
11608                 }
11609               if (TREE_CODE (cur) == ERROR_MARK)
11610                 error_seen++;
11611               else if (! CAN_COMPLETE_NORMALLY (cur))
11612                 {
11613                   wfl_op2 = *next;
11614                   for (;;)
11615                     {
11616                       if (TREE_CODE (wfl_op2) == BLOCK)
11617                         wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11618                       else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11619                         wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11620                       else
11621                         break;
11622                     }
11623                   if (TREE_CODE (wfl_op2) != CASE_EXPR
11624                       && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11625                     unreachable_stmt_error (*ptr);
11626                 }
11627               ptr = next;
11628             }
11629           *ptr = java_complete_tree (*ptr);
11630
11631           if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11632             return error_mark_node;
11633           CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11634         }
11635       /* Turn local bindings to null */
11636       for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11637         IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11638
11639       TREE_TYPE (node) = void_type_node;
11640       break;
11641
11642       /* 2- They are expressions but ultimately deal with statements */
11643
11644     case THROW_EXPR:
11645       wfl_op1 = TREE_OPERAND (node, 0);
11646       COMPLETE_CHECK_OP_0 (node);
11647       /* 14.19 A throw statement cannot complete normally. */
11648       CAN_COMPLETE_NORMALLY (node) = 0;
11649       return patch_throw_statement (node, wfl_op1);
11650
11651     case SYNCHRONIZED_EXPR:
11652       wfl_op1 = TREE_OPERAND (node, 0);
11653       return patch_synchronized_statement (node, wfl_op1);
11654
11655     case TRY_EXPR:
11656       return patch_try_statement (node);
11657
11658     case TRY_FINALLY_EXPR:
11659       COMPLETE_CHECK_OP_0 (node);
11660       COMPLETE_CHECK_OP_1 (node);
11661       if (TREE_OPERAND (node, 0) == empty_stmt_node)
11662         return TREE_OPERAND (node, 1);
11663       if (TREE_OPERAND (node, 1) == empty_stmt_node)
11664         return TREE_OPERAND (node, 0);
11665       CAN_COMPLETE_NORMALLY (node)
11666         = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11667            && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11668       TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11669       return node;
11670
11671     case LABELED_BLOCK_EXPR:
11672       PUSH_LABELED_BLOCK (node);
11673       if (LABELED_BLOCK_BODY (node))
11674         COMPLETE_CHECK_OP_1 (node);
11675       TREE_TYPE (node) = void_type_node;
11676       POP_LABELED_BLOCK ();
11677
11678       if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11679         {
11680           LABELED_BLOCK_BODY (node) = NULL_TREE;
11681           CAN_COMPLETE_NORMALLY (node) = 1;
11682         }
11683       else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11684         CAN_COMPLETE_NORMALLY (node) = 1;
11685       return node;
11686
11687     case EXIT_BLOCK_EXPR:
11688       /* We don't complete operand 1, because it's the return value of
11689          the EXIT_BLOCK_EXPR which doesn't exist it Java */
11690       return patch_bc_statement (node);
11691
11692     case CASE_EXPR:
11693       cn = java_complete_tree (TREE_OPERAND (node, 0));
11694       if (cn == error_mark_node)
11695         return cn;
11696
11697       /* First, the case expression must be constant. Values of final
11698          fields are accepted. */
11699       cn = fold (cn);
11700       if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11701           && JDECL_P (TREE_OPERAND (cn, 1))
11702           && FIELD_FINAL (TREE_OPERAND (cn, 1))
11703           && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11704         {
11705           cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11706                                        TREE_OPERAND (cn, 1));
11707         }
11708       /* Accept final locals too. */
11709       else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn))
11710         cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11711
11712       if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11713         {
11714           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11715           parse_error_context (node, "Constant expression required");
11716           return error_mark_node;
11717         }
11718
11719       nn = ctxp->current_loop;
11720
11721       /* It must be assignable to the type of the switch expression. */
11722       if (!try_builtin_assignconv (NULL_TREE, 
11723                                    TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11724         {
11725           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11726           parse_error_context 
11727             (wfl_operator,
11728              "Incompatible type for case. Can't convert `%s' to `int'",
11729              lang_printable_name (TREE_TYPE (cn), 0));
11730           return error_mark_node;
11731         }
11732
11733       cn = fold (convert (int_type_node, cn));
11734       TREE_CONSTANT_OVERFLOW (cn) = 0;
11735       CAN_COMPLETE_NORMALLY (cn) = 1;
11736
11737       /* Save the label on a list so that we can later check for
11738          duplicates.  */
11739       case_label_list = tree_cons (node, cn, case_label_list);
11740
11741       /* Multiple instance of a case label bearing the same value is
11742          checked later. The case expression is all right so far. */
11743       if (TREE_CODE (cn) == VAR_DECL)
11744         cn = DECL_INITIAL (cn);
11745       TREE_OPERAND (node, 0) = cn;
11746       TREE_TYPE (node) = void_type_node;
11747       CAN_COMPLETE_NORMALLY (node) = 1;
11748       TREE_SIDE_EFFECTS (node) = 1;
11749       break;
11750
11751     case DEFAULT_EXPR:
11752       nn = ctxp->current_loop;
11753       /* Only one default label is allowed per switch statement */
11754       if (SWITCH_HAS_DEFAULT (nn))
11755         {
11756           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11757           parse_error_context (wfl_operator, 
11758                                "Duplicate case label: `default'");
11759           return error_mark_node;
11760         }
11761       else
11762         SWITCH_HAS_DEFAULT (nn) = 1;
11763       TREE_TYPE (node) = void_type_node;
11764       TREE_SIDE_EFFECTS (node) = 1;
11765       CAN_COMPLETE_NORMALLY (node) = 1;
11766       break;
11767
11768     case SWITCH_EXPR:
11769     case LOOP_EXPR:
11770       PUSH_LOOP (node);
11771       /* Check whether the loop was enclosed in a labeled
11772          statement. If not, create one, insert the loop in it and
11773          return the node */
11774       nn = patch_loop_statement (node);
11775
11776       /* Anyways, walk the body of the loop */
11777       if (TREE_CODE (node) == LOOP_EXPR)
11778         TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11779       /* Switch statement: walk the switch expression and the cases */
11780       else
11781         node = patch_switch_statement (node);
11782
11783       if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11784         nn = error_mark_node;
11785       else
11786         {
11787           TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11788           /* If we returned something different, that's because we
11789              inserted a label. Pop the label too. */
11790           if (nn != node)
11791             {
11792               if (CAN_COMPLETE_NORMALLY (node))
11793                 CAN_COMPLETE_NORMALLY (nn) = 1;
11794               POP_LABELED_BLOCK ();
11795             }
11796         }
11797       POP_LOOP ();
11798       return nn;
11799
11800     case EXIT_EXPR:
11801       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11802       return patch_exit_expr (node);
11803
11804     case COND_EXPR:
11805       /* Condition */
11806       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11807       if (TREE_OPERAND (node, 0) == error_mark_node)
11808         return error_mark_node;
11809       /* then-else branches */
11810       TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11811       if (TREE_OPERAND (node, 1) == error_mark_node)
11812         return error_mark_node;
11813       TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11814       if (TREE_OPERAND (node, 2) == error_mark_node)
11815         return error_mark_node;
11816       return patch_if_else_statement (node);
11817       break;
11818
11819     case CONDITIONAL_EXPR:
11820       /* Condition */
11821       wfl_op1 = TREE_OPERAND (node, 0);
11822       COMPLETE_CHECK_OP_0 (node);
11823       wfl_op2 = TREE_OPERAND (node, 1);
11824       COMPLETE_CHECK_OP_1 (node);
11825       wfl_op3 = TREE_OPERAND (node, 2);
11826       COMPLETE_CHECK_OP_2 (node);
11827       return patch_conditional_expr (node, wfl_op1, wfl_op2);
11828
11829       /* 3- Expression section */
11830     case COMPOUND_EXPR:
11831       wfl_op2 = TREE_OPERAND (node, 1);
11832       TREE_OPERAND (node, 0) = nn = 
11833         java_complete_tree (TREE_OPERAND (node, 0));
11834       if (wfl_op2 == empty_stmt_node)
11835         CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11836       else
11837         {
11838           if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11839             {
11840               /* An unreachable condition in a do-while statement
11841                  is *not* (technically) an unreachable statement. */
11842               nn = wfl_op2;
11843               if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11844                 nn = EXPR_WFL_NODE (nn);
11845               if (TREE_CODE (nn) != EXIT_EXPR)
11846                 {
11847                   SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11848                   parse_error_context (wfl_operator, "Unreachable statement");
11849                 }
11850             }
11851           TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11852           if (TREE_OPERAND (node, 1) == error_mark_node)
11853             return error_mark_node;
11854           /* Even though we might allow the case where the first
11855              operand doesn't return normally, we still should compute
11856              CAN_COMPLETE_NORMALLY correctly.  */
11857           CAN_COMPLETE_NORMALLY (node)
11858             = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11859                && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11860         }
11861       TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11862       break;
11863
11864     case RETURN_EXPR:
11865       /* CAN_COMPLETE_NORMALLY (node) = 0; */
11866       return patch_return (node);
11867
11868     case EXPR_WITH_FILE_LOCATION:
11869       if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11870           || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11871         {
11872           tree wfl = node;
11873           node = resolve_expression_name (node, NULL);
11874           if (node == error_mark_node)
11875             return node;
11876           /* Keep line number information somewhere were it doesn't
11877              disrupt the completion process. */
11878           if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11879             {
11880               EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11881               TREE_OPERAND (node, 1) = wfl;
11882             }
11883           CAN_COMPLETE_NORMALLY (node) = 1;
11884         }
11885       else
11886         {
11887           tree body;
11888           int save_lineno = lineno;
11889           lineno = EXPR_WFL_LINENO (node);
11890           body = java_complete_tree (EXPR_WFL_NODE (node));
11891           lineno = save_lineno;
11892           EXPR_WFL_NODE (node) = body;
11893           TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11894           CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11895           if (body == empty_stmt_node || TREE_CONSTANT (body))
11896             {
11897               /* Makes it easier to constant fold, detect empty bodies. */
11898               return body;
11899             }
11900           if (body == error_mark_node)
11901             {
11902               /* Its important for the evaluation of assignment that
11903                  this mark on the TREE_TYPE is propagated. */
11904               TREE_TYPE (node) = error_mark_node;
11905               return error_mark_node;
11906             }
11907           else
11908             TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11909           
11910         }
11911       break;
11912
11913     case NEW_ARRAY_EXPR:
11914       /* Patch all the dimensions */
11915       flag = 0;
11916       for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11917         {
11918           int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11919           tree dim = convert (int_type_node, 
11920                               java_complete_tree (TREE_VALUE (cn)));
11921           if (dim == error_mark_node)
11922             {
11923               flag = 1;
11924               continue;
11925             }
11926           else
11927             {
11928               TREE_VALUE (cn) = dim;
11929               /* Setup the location of the current dimension, for
11930                  later error report. */
11931               TREE_PURPOSE (cn) = 
11932                 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11933               EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11934             }
11935         }
11936       /* They complete the array creation expression, if no errors
11937          were found. */
11938       CAN_COMPLETE_NORMALLY (node) = 1;
11939       return (flag ? error_mark_node
11940               : force_evaluation_order (patch_newarray (node)));
11941
11942     case NEW_ANONYMOUS_ARRAY_EXPR:
11943       /* Create the array type if necessary. */
11944       if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11945         {
11946           tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11947           if (!(type = resolve_type_during_patch (type)))
11948             return error_mark_node;
11949           type = build_array_from_name (type, NULL_TREE,
11950                                         ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11951           ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11952         }
11953       node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11954                                    ANONYMOUS_ARRAY_INITIALIZER (node));
11955       if (node == error_mark_node)
11956         return error_mark_node;
11957       CAN_COMPLETE_NORMALLY (node) = 1;
11958       return node;
11959
11960     case NEW_CLASS_EXPR:
11961     case CALL_EXPR:
11962       /* Complete function's argument(s) first */
11963       if (complete_function_arguments (node))
11964         return error_mark_node;
11965       else
11966         {
11967           tree decl, wfl = TREE_OPERAND (node, 0);
11968           int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11969           int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
11970                            super_identifier_node);
11971
11972           node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
11973                                           from_super, 0, &decl);
11974           if (node == error_mark_node)
11975             return error_mark_node;
11976
11977           check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11978           /* If we call this(...), register signature and positions */
11979           if (in_this)
11980             DECL_CONSTRUCTOR_CALLS (current_function_decl) = 
11981               tree_cons (wfl, decl, 
11982                          DECL_CONSTRUCTOR_CALLS (current_function_decl));
11983           CAN_COMPLETE_NORMALLY (node) = 1;
11984           return force_evaluation_order (node);
11985         }
11986
11987     case MODIFY_EXPR:
11988       /* Save potential wfls */
11989       wfl_op1 = TREE_OPERAND (node, 0);
11990       TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11991       
11992       if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11993           && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11994           && DECL_INITIAL (nn) != NULL_TREE)
11995         {
11996           tree value;
11997           
11998           value = fold_constant_for_init (nn, nn);
11999
12000           /* When we have a primitype type, or a string and we're not
12001              emitting a class file, we actually don't want to generate
12002              anything for the assignment. */
12003           if (value != NULL_TREE &&
12004               (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) || 
12005                (TREE_TYPE (value) == string_ptr_type_node &&
12006                 ! flag_emit_class_files)))
12007             {
12008               /* Prepare node for patch_assignment */
12009               TREE_OPERAND (node, 1) = value;
12010               /* Call patch assignment to verify the assignment */
12011               if (patch_assignment (node, wfl_op1) == error_mark_node)
12012                 return error_mark_node;
12013               /* Set DECL_INITIAL properly (a conversion might have
12014                  been decided by patch_assignment) and return the
12015                  empty statement. */
12016               else
12017                 {
12018                   tree patched = patch_string (TREE_OPERAND (node, 1));
12019                   if (patched)
12020                     DECL_INITIAL (nn) = patched;
12021                   else
12022                     DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12023                   DECL_FIELD_FINAL_IUD (nn) = 1;
12024                   return empty_stmt_node;
12025                 }
12026             }
12027           if (! flag_emit_class_files)
12028             DECL_INITIAL (nn) = NULL_TREE;
12029         }
12030       wfl_op2 = TREE_OPERAND (node, 1);
12031
12032       if (TREE_OPERAND (node, 0) == error_mark_node)
12033         return error_mark_node;
12034
12035       flag = COMPOUND_ASSIGN_P (wfl_op2);
12036       if (flag)
12037         {
12038           /* This might break when accessing outer field from inner
12039              class. TESTME, FIXME */
12040           tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0)); 
12041
12042           /* Hand stabilize the lhs on both places */
12043           TREE_OPERAND (node, 0) = lvalue;
12044           TREE_OPERAND (TREE_OPERAND (node, 1), 0) = 
12045             (flag_emit_class_files ? lvalue : save_expr (lvalue));
12046
12047           /* 15.25.2.a: Left hand is not an array access. FIXME */
12048           /* Now complete the RHS. We write it back later on. */
12049           nn = java_complete_tree (TREE_OPERAND (node, 1));
12050
12051           if ((cn = patch_string (nn)))
12052             nn = cn;
12053
12054           /* The last part of the rewrite for E1 op= E2 is to have 
12055              E1 = (T)(E1 op E2), with T being the type of E1. */
12056           nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2), 
12057                                                TREE_TYPE (lvalue), nn));
12058
12059           /* If the assignment is compound and has reference type,
12060              then ensure the LHS has type String and nothing else.  */
12061           if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
12062               && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
12063             parse_error_context (wfl_op2,
12064                                  "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
12065                                  lang_printable_name (TREE_TYPE (lvalue), 0));
12066
12067           /* 15.25.2.b: Left hand is an array access. FIXME */
12068         }
12069
12070       /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12071          function to complete this RHS. Note that a NEW_ARRAY_INIT
12072          might have been already fully expanded if created as a result
12073          of processing an anonymous array initializer. We avoid doing
12074          the operation twice by testing whether the node already bears
12075          a type. */
12076       else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12077         nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12078                                    TREE_OPERAND (node, 1));
12079       /* Otherwise we simply complete the RHS */
12080       else
12081         nn = java_complete_tree (TREE_OPERAND (node, 1));
12082
12083       if (nn == error_mark_node)
12084         return error_mark_node;
12085
12086       /* Write back the RHS as we evaluated it. */
12087       TREE_OPERAND (node, 1) = nn;
12088
12089       /* In case we're handling = with a String as a RHS, we need to
12090          produce a String out of the RHS (it might still be a
12091          STRING_CST or a StringBuffer at this stage */
12092       if ((nn = patch_string (TREE_OPERAND (node, 1))))
12093         TREE_OPERAND (node, 1) = nn;
12094
12095       if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12096                                         TREE_OPERAND (node, 1))))
12097         {
12098           /* We return error_mark_node if outer_field_access_fix
12099              detects we write into a final. */
12100           if (nn == error_mark_node)
12101             return error_mark_node;
12102           node = nn;
12103         }
12104       else
12105         {
12106           node = patch_assignment (node, wfl_op1);
12107           /* Reorganize the tree if necessary. */
12108           if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node)) 
12109                        || JSTRING_P (TREE_TYPE (node))))
12110             node = java_refold (node);
12111         }
12112
12113       /* Seek to set DECL_INITIAL to a proper value, since it might have
12114          undergone a conversion in patch_assignment. We do that only when
12115          it's necessary to have DECL_INITIAL properly set. */
12116       nn = TREE_OPERAND (node, 0);
12117       if (TREE_CODE (nn) == VAR_DECL 
12118           && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12119           && FIELD_STATIC (nn) && FIELD_FINAL (nn) 
12120           && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12121               || TREE_TYPE (nn) == string_ptr_type_node))
12122         DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12123
12124       CAN_COMPLETE_NORMALLY (node) = 1;
12125       return node;
12126
12127     case MULT_EXPR:
12128     case PLUS_EXPR:
12129     case MINUS_EXPR:
12130     case LSHIFT_EXPR:
12131     case RSHIFT_EXPR:
12132     case URSHIFT_EXPR:
12133     case BIT_AND_EXPR:
12134     case BIT_XOR_EXPR:
12135     case BIT_IOR_EXPR:
12136     case TRUNC_MOD_EXPR:
12137     case TRUNC_DIV_EXPR:
12138     case RDIV_EXPR:
12139     case TRUTH_ANDIF_EXPR:
12140     case TRUTH_ORIF_EXPR:
12141     case EQ_EXPR: 
12142     case NE_EXPR:
12143     case GT_EXPR:
12144     case GE_EXPR:
12145     case LT_EXPR:
12146     case LE_EXPR:
12147       /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12148          knows how to handle those cases. */
12149       wfl_op1 = TREE_OPERAND (node, 0);
12150       wfl_op2 = TREE_OPERAND (node, 1);
12151
12152       CAN_COMPLETE_NORMALLY (node) = 1;
12153       /* Don't complete string nodes if dealing with the PLUS operand. */
12154       if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12155         {
12156           nn = java_complete_tree (wfl_op1);
12157           if (nn == error_mark_node)
12158             return error_mark_node;
12159
12160           TREE_OPERAND (node, 0) = nn;
12161         }
12162       if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12163         {
12164           nn = java_complete_tree (wfl_op2);
12165           if (nn == error_mark_node)
12166             return error_mark_node;
12167
12168           TREE_OPERAND (node, 1) = nn;
12169         }
12170       return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
12171
12172     case INSTANCEOF_EXPR:
12173       wfl_op1 = TREE_OPERAND (node, 0);
12174       COMPLETE_CHECK_OP_0 (node);
12175       if (flag_emit_xref)
12176         {
12177           TREE_TYPE (node) = boolean_type_node;
12178           return node;
12179         }
12180       return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
12181
12182     case UNARY_PLUS_EXPR:
12183     case NEGATE_EXPR:
12184     case TRUTH_NOT_EXPR:
12185     case BIT_NOT_EXPR:
12186     case PREDECREMENT_EXPR:
12187     case PREINCREMENT_EXPR:
12188     case POSTDECREMENT_EXPR:
12189     case POSTINCREMENT_EXPR:
12190     case CONVERT_EXPR:
12191       /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12192          how to handle those cases. */
12193       wfl_op1 = TREE_OPERAND (node, 0);
12194       CAN_COMPLETE_NORMALLY (node) = 1;
12195       TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12196       if (TREE_OPERAND (node, 0) == error_mark_node)
12197         return error_mark_node;
12198       node = patch_unaryop (node, wfl_op1);
12199       CAN_COMPLETE_NORMALLY (node) = 1;
12200       break;
12201
12202     case ARRAY_REF:
12203       /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12204          how to handle those cases. */
12205       wfl_op1 = TREE_OPERAND (node, 0);
12206       TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12207       if (TREE_OPERAND (node, 0) == error_mark_node)
12208         return error_mark_node;
12209       if (!flag_emit_class_files && !flag_emit_xref)
12210         TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12211       /* The same applies to wfl_op2 */
12212       wfl_op2 = TREE_OPERAND (node, 1);
12213       TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12214       if (TREE_OPERAND (node, 1) == error_mark_node)
12215         return error_mark_node;
12216       if (!flag_emit_class_files && !flag_emit_xref)
12217         TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12218       return patch_array_ref (node);
12219
12220     case RECORD_TYPE:
12221       return node;;
12222
12223     case COMPONENT_REF:
12224       /* The first step in the re-write of qualified name handling.  FIXME.
12225          So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
12226       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
12227       if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
12228         {
12229           tree name = TREE_OPERAND (node, 1);
12230           tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
12231           if (field == NULL_TREE)
12232             {
12233               error ("missing static field `%s'", IDENTIFIER_POINTER (name));
12234               return error_mark_node;
12235             }
12236           if (! FIELD_STATIC (field))
12237             {
12238               error ("not a static field `%s'", IDENTIFIER_POINTER (name));
12239               return error_mark_node;
12240             }
12241           return field;
12242         }
12243       else
12244         abort ();
12245       break;
12246
12247     case THIS_EXPR:
12248       /* Can't use THIS in a static environment */
12249       if (!current_this)
12250         {
12251           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12252           parse_error_context (wfl_operator,
12253                                "Keyword `this' used outside allowed context");
12254           TREE_TYPE (node) = error_mark_node;
12255           return error_mark_node;
12256         }
12257       if (ctxp->explicit_constructor_p)
12258         {
12259           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12260           parse_error_context 
12261             (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
12262           TREE_TYPE (node) = error_mark_node;
12263           return error_mark_node;
12264         }
12265       return current_this;
12266       
12267     case CLASS_LITERAL:
12268       CAN_COMPLETE_NORMALLY (node) = 1;
12269       node = patch_incomplete_class_ref (node);
12270       if (node == error_mark_node)
12271         return error_mark_node;
12272       break;
12273
12274     default:
12275       CAN_COMPLETE_NORMALLY (node) = 1;
12276       /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12277          and it's time to turn it into the appropriate String object */
12278       if ((nn = patch_string (node)))
12279         node = nn;
12280       else
12281         internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12282     }
12283   return node;
12284 }
12285
12286 /* Complete function call's argument. Return a non zero value is an
12287    error was found.  */
12288
12289 static int
12290 complete_function_arguments (node)
12291      tree node;
12292 {
12293   int flag = 0;
12294   tree cn;
12295
12296   ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12297   for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12298     {
12299       tree wfl = TREE_VALUE (cn), parm, temp;
12300       parm = java_complete_tree (wfl);
12301
12302       if (parm == error_mark_node)
12303         {
12304           flag = 1;
12305           continue;
12306         }
12307       /* If have a string literal that we haven't transformed yet or a
12308          crafted string buffer, as a result of use of the the String
12309          `+' operator. Build `parm.toString()' and expand it. */
12310       if ((temp = patch_string (parm)))
12311         parm = temp;
12312
12313       TREE_VALUE (cn) = parm;
12314     }
12315   ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12316   return flag;
12317 }
12318
12319 /* Sometimes (for loops and variable initialized during their
12320    declaration), we want to wrap a statement around a WFL and turn it
12321    debugable.  */
12322
12323 static tree
12324 build_debugable_stmt (location, stmt)
12325     int location;
12326     tree stmt;
12327 {
12328   if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12329     {
12330       stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12331       EXPR_WFL_LINECOL (stmt) = location;
12332     }
12333   JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12334   return stmt;
12335 }
12336
12337 static tree
12338 build_expr_block (body, decls)
12339      tree body, decls;
12340 {
12341   tree node = make_node (BLOCK);
12342   BLOCK_EXPR_DECLS (node) = decls;
12343   BLOCK_EXPR_BODY (node) = body;
12344   if (body)
12345     TREE_TYPE (node) = TREE_TYPE (body);
12346   TREE_SIDE_EFFECTS (node) = 1;
12347   return node;
12348 }
12349
12350 /* Create a new function block and link it appropriately to current
12351    function block chain */
12352
12353 static tree
12354 enter_block ()
12355 {
12356   tree b = build_expr_block (NULL_TREE, NULL_TREE);
12357
12358   /* Link block B supercontext to the previous block. The current
12359      function DECL is used as supercontext when enter_a_block is called
12360      for the first time for a given function. The current function body
12361      (DECL_FUNCTION_BODY) is set to be block B.  */
12362
12363   tree fndecl = current_function_decl; 
12364
12365   if (!fndecl) {
12366     BLOCK_SUPERCONTEXT (b) = current_static_block;
12367     current_static_block = b;
12368   }
12369
12370   else if (!DECL_FUNCTION_BODY (fndecl))
12371     {
12372       BLOCK_SUPERCONTEXT (b) = fndecl;
12373       DECL_FUNCTION_BODY (fndecl) = b;
12374     }
12375   else
12376     {
12377       BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12378       DECL_FUNCTION_BODY (fndecl) = b;
12379     }
12380   return b;
12381 }
12382
12383 /* Exit a block by changing the current function body
12384    (DECL_FUNCTION_BODY) to the current block super context, only if
12385    the block being exited isn't the method's top level one.  */
12386
12387 static tree
12388 exit_block ()
12389 {
12390   tree b;
12391   if (current_function_decl)
12392     {
12393       b = DECL_FUNCTION_BODY (current_function_decl);
12394       if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12395         DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12396     }
12397   else
12398     {
12399       b = current_static_block;
12400
12401       if (BLOCK_SUPERCONTEXT (b))
12402         current_static_block = BLOCK_SUPERCONTEXT (b);
12403     }
12404   return b;
12405 }
12406
12407 /* Lookup for NAME in the nested function's blocks, all the way up to
12408    the current toplevel one. It complies with Java's local variable
12409    scoping rules.  */
12410
12411 static tree
12412 lookup_name_in_blocks (name)
12413      tree name;
12414 {
12415   tree b = GET_CURRENT_BLOCK (current_function_decl);
12416
12417   while (b != current_function_decl)
12418     {
12419       tree current;
12420
12421       /* Paranoid sanity check. To be removed */
12422       if (TREE_CODE (b) != BLOCK)
12423         abort ();
12424
12425       for (current = BLOCK_EXPR_DECLS (b); current; 
12426            current = TREE_CHAIN (current))
12427         if (DECL_NAME (current) == name)
12428           return current;
12429       b = BLOCK_SUPERCONTEXT (b);
12430     }
12431   return NULL_TREE;
12432 }
12433
12434 static void
12435 maybe_absorb_scoping_blocks ()
12436 {
12437   while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12438     {
12439       tree b = exit_block ();
12440       java_method_add_stmt (current_function_decl, b);
12441       SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
12442     }
12443 }
12444
12445 \f
12446 /* This section of the source is reserved to build_* functions that
12447    are building incomplete tree nodes and the patch_* functions that
12448    are completing them.  */
12449
12450 /* Wrap a non WFL node around a WFL.  */
12451
12452 static tree
12453 build_wfl_wrap (node, location)
12454     tree node;
12455     int location;
12456 {
12457   tree wfl, node_to_insert = node;
12458   
12459   /* We want to process THIS . xxx symbolicaly, to keep it consistent
12460      with the way we're processing SUPER. A THIS from a primary as a
12461      different form than a SUPER. Turn THIS into something symbolic */
12462   if (TREE_CODE (node) == THIS_EXPR)
12463     node_to_insert = wfl = build_wfl_node (this_identifier_node);
12464   else
12465     wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12466
12467   EXPR_WFL_LINECOL (wfl) = location;
12468   EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12469   return wfl;
12470 }
12471
12472 /* Build a super() constructor invocation. Returns empty_stmt_node if
12473    we're currently dealing with the class java.lang.Object. */
12474
12475 static tree
12476 build_super_invocation (mdecl)
12477      tree mdecl;
12478 {
12479   if (DECL_CONTEXT (mdecl) == object_type_node)
12480     return empty_stmt_node;
12481   else
12482     {
12483       tree super_wfl = build_wfl_node (super_identifier_node);
12484       tree a = NULL_TREE, t;
12485       /* If we're dealing with an anonymous class, pass the arguments
12486          of the crafted constructor along. */
12487       if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12488         {
12489           SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12490           for (; t != end_params_node; t = TREE_CHAIN (t))
12491             a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12492         }
12493       return build_method_invocation (super_wfl, a);
12494     }
12495 }
12496
12497 /* Build a SUPER/THIS qualified method invocation.  */
12498
12499 static tree
12500 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
12501      int use_this;
12502      tree name, args;
12503      int lloc, rloc;
12504 {
12505   tree invok;
12506   tree wfl = 
12507     build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12508   EXPR_WFL_LINECOL (wfl) = lloc;
12509   invok = build_method_invocation (name, args);
12510   return make_qualified_primary (wfl, invok, rloc);
12511 }
12512
12513 /* Build an incomplete CALL_EXPR node. */
12514
12515 static tree
12516 build_method_invocation (name, args)
12517     tree name;
12518     tree args;
12519 {
12520   tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12521   TREE_SIDE_EFFECTS (call) = 1;
12522   EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12523   return call;
12524 }
12525
12526 /* Build an incomplete new xxx(...) node. */
12527
12528 static tree
12529 build_new_invocation (name, args)
12530     tree name, args;
12531 {
12532   tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12533   TREE_SIDE_EFFECTS (call) = 1;
12534   EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12535   return call;
12536 }
12537
12538 /* Build an incomplete assignment expression. */
12539
12540 static tree
12541 build_assignment (op, op_location, lhs, rhs)
12542      int op, op_location;
12543      tree lhs, rhs;
12544 {
12545   tree assignment;
12546   /* Build the corresponding binop if we deal with a Compound
12547      Assignment operator. Mark the binop sub-tree as part of a
12548      Compound Assignment expression */
12549   if (op != ASSIGN_TK)
12550     {
12551       rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12552       COMPOUND_ASSIGN_P (rhs) = 1;
12553     }
12554   assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12555   TREE_SIDE_EFFECTS (assignment) = 1;
12556   EXPR_WFL_LINECOL (assignment) = op_location;
12557   return assignment;
12558 }
12559
12560 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
12561
12562 char *
12563 print_int_node (node)
12564     tree node;
12565 {
12566   static char buffer [80];
12567   if (TREE_CONSTANT_OVERFLOW (node))
12568     sprintf (buffer, "<overflow>");
12569     
12570   if (TREE_INT_CST_HIGH (node) == 0)
12571     sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12572              TREE_INT_CST_LOW (node));
12573   else if (TREE_INT_CST_HIGH (node) == -1
12574            && TREE_INT_CST_LOW (node) != 0)
12575     {
12576       buffer [0] = '-';
12577       sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12578                -TREE_INT_CST_LOW (node));
12579     }
12580   else
12581     sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12582              TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12583
12584   return buffer;
12585 }
12586
12587 \f
12588 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12589    context.  */
12590
12591 /* 15.25 Assignment operators. */
12592
12593 static tree
12594 patch_assignment (node, wfl_op1)
12595      tree node;
12596      tree wfl_op1;
12597 {
12598   tree rhs = TREE_OPERAND (node, 1);
12599   tree lvalue = TREE_OPERAND (node, 0), llvalue;
12600   tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12601   int error_found = 0;
12602   int lvalue_from_array = 0;
12603   int is_return = 0;
12604
12605   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12606
12607   /* Lhs can be a named variable */
12608   if (JDECL_P (lvalue))
12609     {
12610       lhs_type = TREE_TYPE (lvalue);
12611     }
12612   /* Or Lhs can be an array access. */
12613   else if (TREE_CODE (lvalue) == ARRAY_REF)
12614     {
12615       lhs_type = TREE_TYPE (lvalue);
12616       lvalue_from_array = 1;
12617     }
12618   /* Or a field access */
12619   else if (TREE_CODE (lvalue) == COMPONENT_REF)
12620     lhs_type = TREE_TYPE (lvalue);
12621   /* Or a function return slot */
12622   else if (TREE_CODE (lvalue) == RESULT_DECL)
12623     {
12624       /* If the return type is an integral type, then we create the
12625          RESULT_DECL with a promoted type, but we need to do these
12626          checks against the unpromoted type to ensure type safety.  So
12627          here we look at the real type, not the type of the decl we
12628          are modifying.  */
12629       lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12630       is_return = 1;
12631     }
12632   /* Otherwise, we might want to try to write into an optimized static
12633      final, this is an of a different nature, reported further on. */
12634   else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12635            && resolve_expression_name (wfl_op1, &llvalue))
12636     {
12637       lhs_type = TREE_TYPE (lvalue);
12638     }
12639   else 
12640     {
12641       parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12642       error_found = 1;
12643     }
12644
12645   rhs_type = TREE_TYPE (rhs);
12646
12647   /* 5.1 Try the assignment conversion for builtin type. */
12648   new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12649
12650   /* 5.2 If it failed, try a reference conversion */
12651   if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12652     lhs_type = promote_type (rhs_type);
12653
12654   /* 15.25.2 If we have a compound assignment, convert RHS into the
12655      type of the LHS */
12656   else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12657     new_rhs = convert (lhs_type, rhs);
12658
12659   /* Explicit cast required. This is an error */
12660   if (!new_rhs)
12661     {
12662       char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12663       char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12664       tree wfl;
12665       char operation [32];      /* Max size known */
12666
12667       /* If the assignment is part of a declaration, we use the WFL of
12668          the declared variable to point out the error and call it a
12669          declaration problem. If the assignment is a genuine =
12670          operator, we call is a operator `=' problem, otherwise we
12671          call it an assignment problem. In both of these last cases,
12672          we use the WFL of the operator to indicate the error. */
12673
12674       if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12675         {
12676           wfl = wfl_op1;
12677           strcpy (operation, "declaration");
12678         }
12679       else
12680         {
12681           wfl = wfl_operator;
12682           if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12683             strcpy (operation, "assignment");
12684           else if (is_return)
12685             strcpy (operation, "`return'");
12686           else
12687             strcpy (operation, "`='");
12688         }
12689
12690       if (!valid_cast_to_p (rhs_type, lhs_type))
12691         parse_error_context
12692           (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12693            operation, t1, t2);
12694       else
12695         parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12696                              operation, t1, t2);
12697       free (t1); free (t2);
12698       error_found = 1;
12699     }
12700
12701   if (error_found)
12702     return error_mark_node;
12703
12704   /* If we're processing a `return' statement, promote the actual type
12705      to the promoted type.  */
12706   if (is_return)
12707     new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12708
12709   /* 10.10: Array Store Exception runtime check */
12710   if (!flag_emit_class_files
12711       && !flag_emit_xref
12712       && lvalue_from_array 
12713       && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12714     {
12715       tree array, store_check, base, index_expr;
12716
12717       /* Get the INDIRECT_REF. */
12718       array = TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0);
12719       /* Get the array pointer expr. */
12720       array = TREE_OPERAND (array, 0);
12721       store_check = build_java_arraystore_check (array, new_rhs);
12722       
12723       index_expr = TREE_OPERAND (lvalue, 1);
12724       
12725       if (TREE_CODE (index_expr) == COMPOUND_EXPR)
12726         {
12727           /* A COMPOUND_EXPR here is a bounds check. The bounds check must 
12728              happen before the store check, so prepare to insert the store
12729              check within the second operand of the existing COMPOUND_EXPR. */
12730           base = index_expr;
12731         }
12732       else
12733         base = lvalue;
12734       
12735       index_expr = TREE_OPERAND (base, 1);
12736       TREE_OPERAND (base, 1) = build (COMPOUND_EXPR, TREE_TYPE (index_expr), 
12737                                       store_check, index_expr);
12738     }
12739
12740   /* Final locals can be used as case values in switch
12741      statement. Prepare them for this eventuality. */
12742   if (TREE_CODE (lvalue) == VAR_DECL 
12743       && DECL_FINAL (lvalue)
12744       && TREE_CONSTANT (new_rhs)
12745       && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12746       && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12747       )
12748     {
12749       TREE_CONSTANT (lvalue) = 1;
12750       DECL_INITIAL (lvalue) = new_rhs;
12751     }
12752
12753   TREE_OPERAND (node, 0) = lvalue;
12754   TREE_OPERAND (node, 1) = new_rhs;
12755   TREE_TYPE (node) = lhs_type;
12756   return node;
12757 }
12758
12759 /* Check that type SOURCE can be cast into type DEST. If the cast
12760    can't occur at all, return NULL; otherwise, return a possibly
12761    modified rhs.  */
12762
12763 static tree
12764 try_reference_assignconv (lhs_type, rhs)
12765      tree lhs_type, rhs;
12766 {
12767   tree new_rhs = NULL_TREE;
12768   tree rhs_type = TREE_TYPE (rhs);
12769
12770   if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12771     {
12772       /* `null' may be assigned to any reference type */
12773       if (rhs == null_pointer_node)
12774         new_rhs = null_pointer_node;
12775       /* Try the reference assignment conversion */
12776       else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12777         new_rhs = rhs;
12778       /* This is a magic assignment that we process differently */
12779       else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
12780         new_rhs = rhs;
12781     }
12782   return new_rhs;
12783 }
12784
12785 /* Check that RHS can be converted into LHS_TYPE by the assignment
12786    conversion (5.2), for the cases of RHS being a builtin type. Return
12787    NULL_TREE if the conversion fails or if because RHS isn't of a
12788    builtin type. Return a converted RHS if the conversion is possible.  */
12789
12790 static tree
12791 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12792      tree wfl_op1, lhs_type, rhs;
12793 {
12794   tree new_rhs = NULL_TREE;
12795   tree rhs_type = TREE_TYPE (rhs);
12796
12797   /* Handle boolean specially.  */
12798   if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12799       || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12800     {
12801       if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12802           && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12803         new_rhs = rhs;
12804     }
12805
12806   /* Zero accepted everywhere */
12807   else if (TREE_CODE (rhs) == INTEGER_CST 
12808       && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12809       && JPRIMITIVE_TYPE_P (rhs_type))
12810     new_rhs = convert (lhs_type, rhs);
12811
12812   /* 5.1.1 Try Identity Conversion,
12813      5.1.2 Try Widening Primitive Conversion */
12814   else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12815     new_rhs = convert (lhs_type, rhs);
12816
12817   /* Try a narrowing primitive conversion (5.1.3): 
12818        - expression is a constant expression of type int AND
12819        - variable is byte, short or char AND
12820        - The value of the expression is representable in the type of the 
12821          variable */
12822   else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12823            && (lhs_type == byte_type_node || lhs_type == char_type_node
12824                || lhs_type == short_type_node))
12825     {
12826       if (int_fits_type_p (rhs, lhs_type))
12827         new_rhs = convert (lhs_type, rhs);
12828       else if (wfl_op1)         /* Might be called with a NULL */
12829         parse_warning_context 
12830           (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'", 
12831            print_int_node (rhs), lang_printable_name (lhs_type, 0));
12832       /* Reported a warning that will turn into an error further
12833          down, so we don't return */
12834     }
12835
12836   return new_rhs;
12837 }
12838
12839 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12840    conversion (5.1.1) or widening primitive conversion (5.1.2).  Return
12841    0 is the conversion test fails.  This implements parts the method
12842    invocation convertion (5.3).  */
12843
12844 static int
12845 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12846      tree lhs_type, rhs_type;
12847 {
12848   /* 5.1.1: This is the identity conversion part. */
12849   if (lhs_type == rhs_type)
12850     return 1;
12851
12852   /* Reject non primitive types and boolean conversions.  */
12853   if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
12854     return 0;
12855
12856   /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12857      than a char can't be converted into a char. Short can't too, but
12858      the < test below takes care of that */
12859   if (lhs_type == char_type_node && rhs_type == byte_type_node)
12860     return 0;
12861
12862   /* Accept all promoted type here. Note, we can't use <= in the test
12863      below, because we still need to bounce out assignments of short
12864      to char and the likes */
12865   if (lhs_type == int_type_node
12866       && (rhs_type == promoted_byte_type_node
12867           || rhs_type == promoted_short_type_node
12868           || rhs_type == promoted_char_type_node
12869           || rhs_type == promoted_boolean_type_node))
12870     return 1;
12871
12872   /* From here, an integral is widened if its precision is smaller
12873      than the precision of the LHS or if the LHS is a floating point
12874      type, or the RHS is a float and the RHS a double. */
12875   if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type) 
12876        && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12877       || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12878       || (rhs_type == float_type_node && lhs_type == double_type_node))
12879     return 1;
12880
12881   return 0;
12882 }
12883
12884 /* Check that something of SOURCE type can be assigned or cast to
12885    something of DEST type at runtime. Return 1 if the operation is
12886    valid, 0 otherwise. If CAST is set to 1, we're treating the case
12887    were SOURCE is cast into DEST, which borrows a lot of the
12888    assignment check. */
12889
12890 static int
12891 valid_ref_assignconv_cast_p (source, dest, cast)
12892      tree source;
12893      tree dest;
12894      int cast;
12895 {
12896   /* SOURCE or DEST might be null if not from a declared entity. */
12897   if (!source || !dest)
12898     return 0;
12899   if (JNULLP_TYPE_P (source))
12900     return 1;
12901   if (TREE_CODE (source) == POINTER_TYPE)
12902     source = TREE_TYPE (source);
12903   if (TREE_CODE (dest) == POINTER_TYPE)
12904     dest = TREE_TYPE (dest);
12905
12906   /* If source and dest are being compiled from bytecode, they may need to
12907      be loaded. */
12908   if (CLASS_P (source) && !CLASS_LOADED_P (source))
12909     {
12910       load_class (source, 1);
12911       safe_layout_class (source);
12912     }
12913   if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12914     {
12915       load_class (dest, 1);
12916       safe_layout_class (dest);
12917     }
12918
12919   /* Case where SOURCE is a class type */
12920   if (TYPE_CLASS_P (source))
12921     {
12922       if (TYPE_CLASS_P (dest))
12923         return  (source == dest 
12924                  || inherits_from_p (source, dest)
12925                  || (cast && inherits_from_p (dest, source)));
12926       if (TYPE_INTERFACE_P (dest))
12927         {
12928           /* If doing a cast and SOURCE is final, the operation is
12929              always correct a compile time (because even if SOURCE
12930              does not implement DEST, a subclass of SOURCE might). */
12931           if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12932             return 1;
12933           /* Otherwise, SOURCE must implement DEST */
12934           return interface_of_p (dest, source);
12935         }
12936       /* DEST is an array, cast permited if SOURCE is of Object type */
12937       return (cast && source == object_type_node ? 1 : 0);
12938     }
12939   if (TYPE_INTERFACE_P (source))
12940     {
12941       if (TYPE_CLASS_P (dest))
12942         {
12943           /* If not casting, DEST must be the Object type */
12944           if (!cast)
12945             return dest == object_type_node;
12946           /* We're doing a cast. The cast is always valid is class
12947              DEST is not final, otherwise, DEST must implement SOURCE */
12948           else if (!CLASS_FINAL (TYPE_NAME (dest)))
12949             return 1;
12950           else
12951             return interface_of_p (source, dest);
12952         }
12953       if (TYPE_INTERFACE_P (dest))
12954         {
12955           /* If doing a cast, then if SOURCE and DEST contain method
12956              with the same signature but different return type, then
12957              this is a (compile time) error */
12958           if (cast)
12959             {
12960               tree method_source, method_dest;
12961               tree source_type;
12962               tree source_sig;
12963               tree source_name;
12964               for (method_source = TYPE_METHODS (source); method_source; 
12965                    method_source = TREE_CHAIN (method_source))
12966                 {
12967                   source_sig = 
12968                     build_java_argument_signature (TREE_TYPE (method_source));
12969                   source_type = TREE_TYPE (TREE_TYPE (method_source));
12970                   source_name = DECL_NAME (method_source);
12971                   for (method_dest = TYPE_METHODS (dest);
12972                        method_dest; method_dest = TREE_CHAIN (method_dest))
12973                     if (source_sig == 
12974                         build_java_argument_signature (TREE_TYPE (method_dest))
12975                         && source_name == DECL_NAME (method_dest)
12976                         && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12977                       return 0;
12978                 }
12979               return 1;
12980             }
12981           else
12982             return source == dest || interface_of_p (dest, source);
12983         }
12984       else
12985         {
12986           /* Array */
12987           return (cast
12988                   && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12989                       || (DECL_NAME (TYPE_NAME (source))
12990                           == java_io_serializable)));
12991         }
12992     }
12993   if (TYPE_ARRAY_P (source))
12994     {
12995       if (TYPE_CLASS_P (dest))
12996         return dest == object_type_node;
12997       /* Can't cast an array to an interface unless the interface is
12998          java.lang.Cloneable or java.io.Serializable.  */
12999       if (TYPE_INTERFACE_P (dest))
13000         return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
13001                 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
13002       else                      /* Arrays */
13003         {
13004           tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13005           tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13006           
13007           /* In case of severe errors, they turn out null */
13008           if (!dest_element_type || !source_element_type)
13009             return 0;
13010           if (source_element_type == dest_element_type)
13011             return 1;
13012           return valid_ref_assignconv_cast_p (source_element_type,
13013                                               dest_element_type, cast);
13014         }
13015       return 0;
13016     }
13017   return 0;
13018 }
13019
13020 static int
13021 valid_cast_to_p (source, dest)
13022      tree source;
13023      tree dest;
13024 {
13025   if (TREE_CODE (source) == POINTER_TYPE)
13026     source = TREE_TYPE (source);
13027   if (TREE_CODE (dest) == POINTER_TYPE)
13028     dest = TREE_TYPE (dest);
13029
13030   if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13031     return valid_ref_assignconv_cast_p (source, dest, 1);
13032
13033   else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13034     return 1;
13035
13036   else if (TREE_CODE (source) == BOOLEAN_TYPE
13037            && TREE_CODE (dest) == BOOLEAN_TYPE)
13038     return 1;
13039
13040   return 0;
13041 }
13042
13043 static tree
13044 do_unary_numeric_promotion (arg)
13045      tree arg;
13046 {
13047   tree type = TREE_TYPE (arg);
13048   if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13049       || TREE_CODE (type) == CHAR_TYPE)
13050     arg = convert (int_type_node, arg);
13051   return arg;
13052 }
13053
13054 /* Return a non zero value if SOURCE can be converted into DEST using
13055    the method invocation conversion rule (5.3).  */
13056 static int
13057 valid_method_invocation_conversion_p (dest, source)
13058      tree dest, source;
13059 {
13060   return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13061            && valid_builtin_assignconv_identity_widening_p (dest, source))
13062           || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13063               && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13064               && valid_ref_assignconv_cast_p (source, dest, 0)));
13065 }
13066
13067 /* Build an incomplete binop expression. */
13068
13069 static tree
13070 build_binop (op, op_location, op1, op2)
13071      enum tree_code op;
13072      int op_location;
13073      tree op1, op2;
13074 {
13075   tree binop = build (op, NULL_TREE, op1, op2);
13076   TREE_SIDE_EFFECTS (binop) = 1;
13077   /* Store the location of the operator, for better error report. The
13078      string of the operator will be rebuild based on the OP value. */
13079   EXPR_WFL_LINECOL (binop) = op_location;
13080   return binop;
13081 }
13082
13083 /* Build the string of the operator retained by NODE. If NODE is part
13084    of a compound expression, add an '=' at the end of the string. This
13085    function is called when an error needs to be reported on an
13086    operator. The string is returned as a pointer to a static character
13087    buffer. */
13088
13089 static char *
13090 operator_string (node)
13091      tree node;
13092 {
13093 #define BUILD_OPERATOR_STRING(S)                                        \
13094   {                                                                     \
13095     sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13096     return buffer;                                                      \
13097   }
13098   
13099   static char buffer [10];
13100   switch (TREE_CODE (node))
13101     {
13102     case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13103     case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13104     case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13105     case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13106     case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13107     case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13108     case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13109     case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13110     case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13111     case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13112     case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13113     case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13114     case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13115     case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13116     case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13117     case GT_EXPR: BUILD_OPERATOR_STRING (">");
13118     case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13119     case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13120     case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13121     case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13122     case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13123     case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13124     case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13125     case PREINCREMENT_EXPR:     /* Fall through */
13126     case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13127     case PREDECREMENT_EXPR:     /* Fall through */
13128     case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13129     default:
13130       internal_error ("unregistered operator %s",
13131                       tree_code_name [TREE_CODE (node)]);
13132     }
13133   return NULL;
13134 #undef BUILD_OPERATOR_STRING
13135 }
13136
13137 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2.  */
13138
13139 static int
13140 java_decl_equiv (var_acc1, var_acc2)
13141      tree var_acc1, var_acc2;
13142 {
13143   if (JDECL_P (var_acc1))
13144     return (var_acc1 == var_acc2);
13145   
13146   return (TREE_CODE (var_acc1) == COMPONENT_REF
13147           && TREE_CODE (var_acc2) == COMPONENT_REF
13148           && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13149              == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13150           && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13151 }
13152
13153 /* Return a non zero value if CODE is one of the operators that can be
13154    used in conjunction with the `=' operator in a compound assignment.  */
13155
13156 static int
13157 binop_compound_p (code)
13158     enum tree_code code;
13159 {
13160   int i;
13161   for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13162     if (binop_lookup [i] == code)
13163       break;
13164
13165   return i < BINOP_COMPOUND_CANDIDATES;
13166 }
13167
13168 /* Reorganize after a fold to get SAVE_EXPR to generate what we want.  */
13169
13170 static tree
13171 java_refold (t)
13172      tree t;
13173 {
13174   tree c, b, ns, decl;
13175
13176   if (TREE_CODE (t) != MODIFY_EXPR)
13177     return t;
13178
13179   c = TREE_OPERAND (t, 1);
13180   if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13181          && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13182          && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13183     return t;
13184
13185   /* Now the left branch of the binary operator. */
13186   b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13187   if (! (b && TREE_CODE (b) == NOP_EXPR 
13188          && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13189     return t;
13190
13191   ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13192   if (! (ns && TREE_CODE (ns) == NOP_EXPR
13193          && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13194     return t;
13195
13196   decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13197   if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13198       /* It's got to be the an equivalent decl */
13199       && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13200     {
13201       /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13202       TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13203       /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13204       TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13205       /* Change the right part of the BINOP_EXPR */
13206       TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13207     }
13208
13209   return t;
13210 }
13211
13212 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13213    errors but we modify NODE so that it contains the type computed
13214    according to the expression, when it's fixed. Otherwise, we write
13215    error_mark_node as the type. It allows us to further the analysis
13216    of remaining nodes and detects more errors in certain cases.  */
13217
13218 static tree
13219 patch_binop (node, wfl_op1, wfl_op2)
13220      tree node;
13221      tree wfl_op1;
13222      tree wfl_op2;
13223 {
13224   tree op1 = TREE_OPERAND (node, 0);
13225   tree op2 = TREE_OPERAND (node, 1);
13226   tree op1_type = TREE_TYPE (op1);
13227   tree op2_type = TREE_TYPE (op2);
13228   tree prom_type = NULL_TREE, cn;
13229   enum tree_code code = TREE_CODE (node);
13230
13231   /* If 1, tell the routine that we have to return error_mark_node
13232      after checking for the initialization of the RHS */
13233   int error_found = 0;
13234
13235   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13236
13237   /* If either op<n>_type are NULL, this might be early signs of an
13238      error situation, unless it's too early to tell (in case we're
13239      handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13240      correctly so the error can be later on reported accurately. */
13241   if (! (code == PLUS_EXPR || code == NE_EXPR 
13242          || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13243     {
13244       tree n;
13245       if (! op1_type)
13246         {
13247           n = java_complete_tree (op1);
13248           op1_type = TREE_TYPE (n);
13249         }
13250       if (! op2_type)
13251         {
13252           n = java_complete_tree (op2);
13253           op2_type = TREE_TYPE (n);
13254         }
13255     }
13256
13257   switch (code)
13258     {
13259     /* 15.16 Multiplicative operators */
13260     case MULT_EXPR:             /* 15.16.1 Multiplication Operator * */
13261     case RDIV_EXPR:             /* 15.16.2 Division Operator / */
13262     case TRUNC_DIV_EXPR:        /* 15.16.2 Integral type Division Operator / */
13263     case TRUNC_MOD_EXPR:        /* 15.16.3 Remainder operator % */
13264       if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13265         {
13266           if (!JNUMERIC_TYPE_P (op1_type))
13267             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13268           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13269             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13270           TREE_TYPE (node) = error_mark_node;
13271           error_found = 1;
13272           break;
13273         }
13274       prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13275
13276       /* Detect integral division by zero */
13277       if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13278           && TREE_CODE (prom_type) == INTEGER_TYPE
13279           && (op2 == integer_zero_node || op2 == long_zero_node ||
13280               (TREE_CODE (op2) == INTEGER_CST &&
13281                ! TREE_INT_CST_LOW (op2)  && ! TREE_INT_CST_HIGH (op2))))
13282         {
13283           parse_warning_context (wfl_operator, "Evaluating this expression will result in an arithmetic exception being thrown");
13284           TREE_CONSTANT (node) = 0;
13285         }
13286           
13287       /* Change the division operator if necessary */
13288       if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13289         TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13290
13291       /* Before divisions as is disapear, try to simplify and bail if
13292          applicable, otherwise we won't perform even simple
13293          simplifications like (1-1)/3. We can't do that with floating
13294          point number, folds can't handle them at this stage. */
13295       if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13296           && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13297         {
13298           TREE_TYPE (node) = prom_type;
13299           node = fold (node);
13300           if (TREE_CODE (node) != code)
13301             return node;
13302         }
13303
13304       if (TREE_CODE (prom_type) == INTEGER_TYPE
13305           && flag_use_divide_subroutine
13306           && ! flag_emit_class_files
13307           && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13308         return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13309  
13310       /* This one is more complicated. FLOATs are processed by a
13311          function call to soft_fmod. Duplicate the value of the
13312          COMPOUND_ASSIGN_P flag. */
13313       if (code == TRUNC_MOD_EXPR)
13314         {
13315           tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13316           COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13317           TREE_SIDE_EFFECTS (mod)
13318             = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13319           return mod;
13320         }
13321       break;
13322
13323     /* 15.17 Additive Operators */
13324     case PLUS_EXPR:             /* 15.17.1 String Concatenation Operator + */
13325
13326       /* Operation is valid if either one argument is a string
13327          constant, a String object or a StringBuffer crafted for the
13328          purpose of the a previous usage of the String concatenation
13329          operator */
13330
13331       if (TREE_CODE (op1) == STRING_CST 
13332           || TREE_CODE (op2) == STRING_CST
13333           || JSTRING_TYPE_P (op1_type)
13334           || JSTRING_TYPE_P (op2_type)
13335           || IS_CRAFTED_STRING_BUFFER_P (op1)
13336           || IS_CRAFTED_STRING_BUFFER_P (op2))
13337         return build_string_concatenation (op1, op2);
13338
13339     case MINUS_EXPR:            /* 15.17.2 Additive Operators (+ and -) for
13340                                    Numeric Types */
13341       if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13342         {
13343           if (!JNUMERIC_TYPE_P (op1_type))
13344             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13345           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13346             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13347           TREE_TYPE (node) = error_mark_node;
13348           error_found = 1;
13349           break;
13350         }
13351       prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13352       break;
13353
13354     /* 15.18 Shift Operators */
13355     case LSHIFT_EXPR:
13356     case RSHIFT_EXPR:
13357     case URSHIFT_EXPR:
13358       if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13359         {
13360           if (!JINTEGRAL_TYPE_P (op1_type))
13361             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13362           else
13363             {
13364               if (JNUMERIC_TYPE_P (op2_type))
13365                 parse_error_context (wfl_operator,
13366                                      "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
13367                                      operator_string (node),
13368                                      lang_printable_name (op2_type, 0));
13369               else
13370                 parse_error_context (wfl_operator,
13371                                      "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral", 
13372                                      operator_string (node),
13373                                      lang_printable_name (op2_type, 0));
13374             }
13375           TREE_TYPE (node) = error_mark_node;
13376           error_found = 1;
13377           break;
13378         }
13379
13380       /* Unary numeric promotion (5.6.1) is performed on each operand
13381          separately */
13382       op1 = do_unary_numeric_promotion (op1);
13383       op2 = do_unary_numeric_promotion (op2);
13384
13385       /* The type of the shift expression is the type of the promoted
13386          type of the left-hand operand */
13387       prom_type = TREE_TYPE (op1);
13388
13389       /* Shift int only up to 0x1f and long up to 0x3f */
13390       if (prom_type == int_type_node)
13391         op2 = fold (build (BIT_AND_EXPR, int_type_node, op2, 
13392                            build_int_2 (0x1f, 0)));
13393       else
13394         op2 = fold (build (BIT_AND_EXPR, int_type_node, op2, 
13395                            build_int_2 (0x3f, 0)));
13396
13397       /* The >>> operator is a >> operating on unsigned quantities */
13398       if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13399         {
13400           tree to_return;
13401           tree utype = unsigned_type (prom_type);
13402           op1 = convert (utype, op1);
13403           TREE_SET_CODE (node, RSHIFT_EXPR);
13404           TREE_OPERAND (node, 0) = op1;
13405           TREE_OPERAND (node, 1) = op2;
13406           TREE_TYPE (node) = utype;
13407           to_return = convert (prom_type, node);
13408           /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13409           COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13410           TREE_SIDE_EFFECTS (to_return)
13411             = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13412           return to_return;
13413         }
13414       break;
13415
13416       /* 15.19.1 Type Comparison Operator instaceof */
13417     case INSTANCEOF_EXPR:
13418
13419       TREE_TYPE (node) = boolean_type_node;
13420
13421       if (!(op2_type = resolve_type_during_patch (op2)))
13422         return error_mark_node;
13423
13424       /* The first operand must be a reference type or the null type */
13425       if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13426         error_found = 1;        /* Error reported further below */
13427
13428       /* The second operand must be a reference type */
13429       if (!JREFERENCE_TYPE_P (op2_type))
13430         {
13431           SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13432           parse_error_context
13433             (wfl_operator, "Invalid argument `%s' for `instanceof'",
13434              lang_printable_name (op2_type, 0));
13435           error_found = 1;
13436         }
13437
13438       if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13439         {
13440           /* If the first operand is null, the result is always false */
13441           if (op1 == null_pointer_node)
13442             return boolean_false_node;
13443           else if (flag_emit_class_files)
13444             {
13445               TREE_OPERAND (node, 1) = op2_type;
13446               TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13447               return node;
13448             }
13449           /* Otherwise we have to invoke instance of to figure it out */
13450           else
13451             return build_instanceof (op1, op2_type);
13452         }
13453       /* There is no way the expression operand can be an instance of
13454          the type operand. This is a compile time error. */
13455       else
13456         {
13457           char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13458           SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13459           parse_error_context 
13460             (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13461              t1, lang_printable_name (op2_type, 0));
13462           free (t1);
13463           error_found = 1;
13464         }
13465       
13466       break;
13467
13468       /* 15.21 Bitwise and Logical Operators */
13469     case BIT_AND_EXPR:
13470     case BIT_XOR_EXPR:
13471     case BIT_IOR_EXPR:
13472       if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13473         /* Binary numeric promotion is performed on both operand and the
13474            expression retain that type */
13475         prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13476
13477       else if (TREE_CODE (op1_type) == BOOLEAN_TYPE 
13478                && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13479         /* The type of the bitwise operator expression is BOOLEAN */
13480         prom_type = boolean_type_node;
13481       else
13482         {
13483           if (!JINTEGRAL_TYPE_P (op1_type))
13484             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13485           if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13486             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13487           TREE_TYPE (node) = error_mark_node;
13488           error_found = 1;
13489           /* Insert a break here if adding thing before the switch's
13490              break for this case */
13491         }
13492       break;
13493
13494       /* 15.22 Conditional-And Operator */
13495     case TRUTH_ANDIF_EXPR:
13496       /* 15.23 Conditional-Or Operator */
13497     case TRUTH_ORIF_EXPR:
13498       /* Operands must be of BOOLEAN type */
13499       if (TREE_CODE (op1_type) != BOOLEAN_TYPE || 
13500           TREE_CODE (op2_type) != BOOLEAN_TYPE)
13501         {
13502           if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13503             ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13504           if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13505             ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13506           TREE_TYPE (node) = boolean_type_node;
13507           error_found = 1;
13508           break;
13509         }
13510       else if (integer_zerop (op1))
13511         {
13512           return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13513         }
13514       else if (integer_onep (op1))
13515         {
13516           return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13517         }
13518       /* The type of the conditional operators is BOOLEAN */
13519       prom_type = boolean_type_node;
13520       break;
13521
13522       /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13523     case LT_EXPR:
13524     case GT_EXPR:
13525     case LE_EXPR:
13526     case GE_EXPR:
13527       /* The type of each of the operands must be a primitive numeric
13528          type */
13529       if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13530         {
13531           if (!JNUMERIC_TYPE_P (op1_type))
13532             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13533           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13534             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13535           TREE_TYPE (node) = boolean_type_node;
13536           error_found = 1;
13537           break;
13538         }
13539       /* Binary numeric promotion is performed on the operands */
13540       binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13541       /* The type of the relation expression is always BOOLEAN */
13542       prom_type = boolean_type_node;
13543       break;
13544
13545       /* 15.20 Equality Operator */
13546     case EQ_EXPR:
13547     case NE_EXPR:
13548       /* It's time for us to patch the strings. */
13549       if ((cn = patch_string (op1))) 
13550        {
13551          op1 = cn;
13552          op1_type = TREE_TYPE (op1);
13553        }
13554       if ((cn = patch_string (op2))) 
13555        {
13556          op2 = cn;
13557          op2_type = TREE_TYPE (op2);
13558        }
13559       
13560       /* 15.20.1 Numerical Equality Operators == and != */
13561       /* Binary numeric promotion is performed on the operands */
13562       if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13563         binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13564       
13565       /* 15.20.2 Boolean Equality Operators == and != */
13566       else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13567           TREE_CODE (op2_type) == BOOLEAN_TYPE)
13568         ;                       /* Nothing to do here */
13569       
13570       /* 15.20.3 Reference Equality Operators == and != */
13571       /* Types have to be either references or the null type. If
13572          they're references, it must be possible to convert either
13573          type to the other by casting conversion. */
13574       else if (op1 == null_pointer_node || op2 == null_pointer_node 
13575                || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13576                    && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13577                        || valid_ref_assignconv_cast_p (op2_type, 
13578                                                        op1_type, 1))))
13579         ;                       /* Nothing to do here */
13580           
13581       /* Else we have an error figure what can't be converted into
13582          what and report the error */
13583       else
13584         {
13585           char *t1;
13586           t1 = xstrdup (lang_printable_name (op1_type, 0));
13587           parse_error_context 
13588             (wfl_operator,
13589              "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13590              operator_string (node), t1, 
13591              lang_printable_name (op2_type, 0));
13592           free (t1);
13593           TREE_TYPE (node) = boolean_type_node;
13594           error_found = 1;
13595           break;
13596         }
13597       prom_type = boolean_type_node;
13598       break;
13599     default:
13600       abort ();
13601     }
13602
13603   if (error_found)
13604     return error_mark_node;
13605
13606   TREE_OPERAND (node, 0) = op1;
13607   TREE_OPERAND (node, 1) = op2;
13608   TREE_TYPE (node) = prom_type;
13609   TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13610   
13611   if (flag_emit_xref)
13612     return node;
13613
13614   /* fold does not respect side-effect order as required for Java but not C.
13615    * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13616    * bytecode.
13617    */
13618   if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13619       : ! TREE_SIDE_EFFECTS (node))
13620     node = fold (node);
13621   return node;
13622 }
13623
13624 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13625    zero value, the value of CSTE comes after the valude of STRING */
13626
13627 static tree
13628 do_merge_string_cste (cste, string, string_len, after)
13629      tree cste;
13630      const char *string;
13631      int string_len, after;
13632 {
13633   const char *old = TREE_STRING_POINTER (cste);
13634   int old_len = TREE_STRING_LENGTH (cste);
13635   int len = old_len + string_len;
13636   char *new = alloca (len+1);
13637
13638   if (after)
13639     {
13640       memcpy (new, string, string_len);
13641       memcpy (&new [string_len], old, old_len);
13642     }
13643   else
13644     {
13645       memcpy (new, old, old_len);
13646       memcpy (&new [old_len], string, string_len);
13647     }
13648   new [len] = '\0';
13649   return build_string (len, new);
13650 }
13651
13652 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13653    new STRING_CST on success, NULL_TREE on failure */
13654
13655 static tree
13656 merge_string_cste (op1, op2, after)
13657      tree op1, op2;
13658      int after;
13659 {
13660   /* Handle two string constants right away */
13661   if (TREE_CODE (op2) == STRING_CST)
13662     return do_merge_string_cste (op1, TREE_STRING_POINTER (op2), 
13663                                  TREE_STRING_LENGTH (op2), after);
13664   
13665   /* Reasonable integer constant can be treated right away */
13666   if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13667     {
13668       static const char *const boolean_true = "true";
13669       static const char *const boolean_false = "false";
13670       static const char *const null_pointer = "null";
13671       char ch[3];
13672       const char *string;
13673       
13674       if (op2 == boolean_true_node)
13675         string = boolean_true;
13676       else if (op2 == boolean_false_node)
13677         string = boolean_false;
13678       else if (op2 == null_pointer_node)
13679         string = null_pointer;
13680       else if (TREE_TYPE (op2) == char_type_node)
13681         {
13682           ch[0] = (char )TREE_INT_CST_LOW (op2);
13683           ch[1] = '\0';
13684           string = ch;
13685         }
13686       else
13687           string = print_int_node (op2);
13688       
13689       return do_merge_string_cste (op1, string, strlen (string), after);
13690     }
13691   return NULL_TREE;
13692 }
13693
13694 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13695    has to be a STRING_CST and the other part must be a STRING_CST or a
13696    INTEGRAL constant. Return a new STRING_CST if the operation
13697    succeed, NULL_TREE otherwise.
13698
13699    If the case we want to optimize for space, we might want to return
13700    NULL_TREE for each invocation of this routine. FIXME */
13701
13702 static tree
13703 string_constant_concatenation (op1, op2)
13704      tree op1, op2;
13705 {
13706   if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13707     {
13708       tree string, rest;
13709       int invert;
13710       
13711       string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13712       rest   = (string == op1 ? op2 : op1);
13713       invert = (string == op1 ? 0 : 1 );
13714       
13715       /* Walk REST, only if it looks reasonable */
13716       if (TREE_CODE (rest) != STRING_CST
13717           && !IS_CRAFTED_STRING_BUFFER_P (rest)
13718           && !JSTRING_TYPE_P (TREE_TYPE (rest))
13719           && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13720         {
13721           rest = java_complete_tree (rest);
13722           if (rest == error_mark_node)
13723             return error_mark_node;
13724           rest = fold (rest);
13725         }
13726       return merge_string_cste (string, rest, invert);
13727     }
13728   return NULL_TREE;
13729 }
13730
13731 /* Implement the `+' operator. Does static optimization if possible,
13732    otherwise create (if necessary) and append elements to a
13733    StringBuffer. The StringBuffer will be carried around until it is
13734    used for a function call or an assignment. Then toString() will be
13735    called on it to turn it into a String object. */
13736
13737 static tree
13738 build_string_concatenation (op1, op2)
13739      tree op1, op2;
13740 {
13741   tree result;
13742   int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13743
13744   if (flag_emit_xref)
13745     return build (PLUS_EXPR, string_type_node, op1, op2);
13746   
13747   /* Try to do some static optimization */
13748   if ((result = string_constant_concatenation (op1, op2)))
13749     return result;
13750
13751   /* Discard empty strings on either side of the expression */
13752   if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13753     {
13754       op1 = op2;
13755       op2 = NULL_TREE;
13756     }
13757   else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13758     op2 = NULL_TREE;
13759
13760   /* If operands are string constant, turn then into object references */
13761   if (TREE_CODE (op1) == STRING_CST)
13762     op1 = patch_string_cst (op1);
13763   if (op2 && TREE_CODE (op2) == STRING_CST)
13764     op2 = patch_string_cst (op2);
13765
13766   /* If either one of the constant is null and the other non null
13767      operand is a String object, return it. */
13768   if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13769     return op1;
13770
13771   /* If OP1 isn't already a StringBuffer, create and
13772      initialize a new one */
13773   if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13774     {
13775       /* Two solutions here: 
13776          1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13777          2) OP1 is something else, we call new StringBuffer().append(OP1).  */
13778       if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13779         op1 = BUILD_STRING_BUFFER (op1);
13780       else
13781         {
13782           tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13783           op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13784         }
13785     }
13786
13787   if (op2)
13788     {
13789       /* OP1 is no longer the last node holding a crafted StringBuffer */
13790       IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13791       /* Create a node for `{new...,xxx}.append (op2)' */
13792       if (op2)
13793         op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13794     }
13795
13796   /* Mark the last node holding a crafted StringBuffer */
13797   IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13798
13799   TREE_SIDE_EFFECTS (op1) = side_effects;
13800   return op1;
13801 }
13802
13803 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13804    StringBuffer. If no string were found to be patched, return
13805    NULL. */
13806
13807 static tree
13808 patch_string (node)
13809     tree node;
13810 {
13811   if (node == error_mark_node)
13812     return error_mark_node;
13813   if (TREE_CODE (node) == STRING_CST)
13814     return patch_string_cst (node);
13815   else if (IS_CRAFTED_STRING_BUFFER_P (node))
13816     {
13817       int saved = ctxp->explicit_constructor_p;
13818       tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13819       tree ret;
13820       /* Temporary disable forbid the use of `this'. */
13821       ctxp->explicit_constructor_p = 0;
13822       ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13823       /* String concatenation arguments must be evaluated in order too. */
13824       ret = force_evaluation_order (ret);
13825       /* Restore it at its previous value */
13826       ctxp->explicit_constructor_p = saved;
13827       return ret;
13828     }
13829   return NULL_TREE;
13830 }
13831
13832 /* Build the internal representation of a string constant.  */
13833
13834 static tree
13835 patch_string_cst (node)
13836      tree node;
13837 {
13838   int location;
13839   if (! flag_emit_class_files)
13840     {
13841       node = get_identifier (TREE_STRING_POINTER (node));
13842       location = alloc_name_constant (CONSTANT_String, node);
13843       node = build_ref_from_constant_pool (location);
13844     }
13845   TREE_TYPE (node) = string_ptr_type_node;
13846   TREE_CONSTANT (node) = 1;
13847   return node;
13848 }
13849
13850 /* Build an incomplete unary operator expression. */
13851
13852 static tree
13853 build_unaryop (op_token, op_location, op1)
13854      int op_token, op_location;
13855      tree op1;
13856 {
13857   enum tree_code op;
13858   tree unaryop;
13859   switch (op_token)
13860     {
13861     case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13862     case MINUS_TK: op = NEGATE_EXPR; break;
13863     case NEG_TK: op = TRUTH_NOT_EXPR; break;
13864     case NOT_TK: op = BIT_NOT_EXPR; break;
13865     default: abort ();
13866     }
13867
13868   unaryop = build1 (op, NULL_TREE, op1);
13869   TREE_SIDE_EFFECTS (unaryop) = 1;
13870   /* Store the location of the operator, for better error report. The
13871      string of the operator will be rebuild based on the OP value. */
13872   EXPR_WFL_LINECOL (unaryop) = op_location;
13873   return unaryop;
13874 }
13875
13876 /* Special case for the ++/-- operators, since they require an extra
13877    argument to build, which is set to NULL and patched
13878    later. IS_POST_P is 1 if the operator, 0 otherwise.  */
13879
13880 static tree
13881 build_incdec (op_token, op_location, op1, is_post_p)
13882      int op_token, op_location;
13883      tree op1;
13884      int is_post_p;
13885 {
13886   static const enum tree_code lookup [2][2] = 
13887     {
13888       { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13889       { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13890     };
13891   tree node = build (lookup [is_post_p][(op_token - DECR_TK)], 
13892                      NULL_TREE, op1, NULL_TREE);
13893   TREE_SIDE_EFFECTS (node) = 1;
13894   /* Store the location of the operator, for better error report. The
13895      string of the operator will be rebuild based on the OP value. */
13896   EXPR_WFL_LINECOL (node) = op_location;
13897   return node;
13898 }     
13899
13900 /* Build an incomplete cast operator, based on the use of the
13901    CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13902    set. java_complete_tree is trained to walk a CONVERT_EXPR even
13903    though its type is already set.  */
13904
13905 static tree
13906 build_cast (location, type, exp)
13907      int location;
13908      tree type, exp;
13909 {
13910   tree node = build1 (CONVERT_EXPR, type, exp);
13911   EXPR_WFL_LINECOL (node) = location;
13912   return node;
13913 }
13914
13915 /* Build an incomplete class reference operator.  */
13916 static tree
13917 build_incomplete_class_ref (location, class_name)
13918     int location;
13919     tree class_name;
13920 {
13921   tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13922   EXPR_WFL_LINECOL (node) = location;
13923   return node;
13924 }
13925
13926 /* Complete an incomplete class reference operator.  */
13927 static tree
13928 patch_incomplete_class_ref (node)
13929     tree node;
13930 {
13931   tree type = TREE_OPERAND (node, 0);
13932   tree ref_type;
13933
13934   if (!(ref_type = resolve_type_during_patch (type)))
13935     return error_mark_node;
13936
13937   if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
13938     {
13939       tree dot = build_class_ref (ref_type);
13940       /* A class referenced by `foo.class' is initialized.  */
13941       if (!flag_emit_class_files)
13942        dot = build_class_init (ref_type, dot);
13943       return java_complete_tree (dot);
13944     }
13945
13946   /* If we're emitting class files and we have to deal with non
13947      primitive types, we invoke (and consider generating) the
13948      synthetic static method `class$'. */
13949   if (!TYPE_DOT_CLASS (current_class))
13950       build_dot_class_method (current_class);
13951   ref_type = build_dot_class_method_invocation (ref_type);
13952   return java_complete_tree (ref_type);
13953 }
13954
13955 /* 15.14 Unary operators. We return error_mark_node in case of error,
13956    but preserve the type of NODE if the type is fixed.  */
13957
13958 static tree
13959 patch_unaryop (node, wfl_op)
13960      tree node;
13961      tree wfl_op;
13962 {
13963   tree op = TREE_OPERAND (node, 0);
13964   tree op_type = TREE_TYPE (op);
13965   tree prom_type = NULL_TREE, value, decl;
13966   int outer_field_flag = 0;
13967   int code = TREE_CODE (node);
13968   int error_found = 0;
13969
13970   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13971
13972   switch (code)
13973     {
13974       /* 15.13.2 Postfix Increment Operator ++ */
13975     case POSTINCREMENT_EXPR:
13976       /* 15.13.3 Postfix Increment Operator -- */
13977     case POSTDECREMENT_EXPR:
13978       /* 15.14.1 Prefix Increment Operator ++ */
13979     case PREINCREMENT_EXPR:
13980       /* 15.14.2 Prefix Decrement Operator -- */
13981     case PREDECREMENT_EXPR:
13982       op = decl = strip_out_static_field_access_decl (op);
13983       outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13984       /* We might be trying to change an outer field accessed using
13985          access method. */
13986       if (outer_field_flag)
13987         {
13988           /* Retrieve the decl of the field we're trying to access. We
13989              do that by first retrieving the function we would call to
13990              access the field. It has been already verified that this
13991              field isn't final */
13992           if (flag_emit_class_files)
13993             decl = TREE_OPERAND (op, 0);
13994           else
13995             decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13996           decl = DECL_FUNCTION_ACCESS_DECL (decl);
13997         }
13998       /* We really should have a JAVA_ARRAY_EXPR to avoid this */
13999       else if (!JDECL_P (decl) 
14000           && TREE_CODE (decl) != COMPONENT_REF
14001           && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14002           && TREE_CODE (decl) != INDIRECT_REF
14003           && !(TREE_CODE (decl) == COMPOUND_EXPR
14004                && TREE_OPERAND (decl, 1)
14005                && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14006         {
14007           TREE_TYPE (node) = error_mark_node;
14008           error_found = 1;
14009         }
14010       
14011       /* From now on, we know that op if a variable and that it has a
14012          valid wfl. We use wfl_op to locate errors related to the
14013          ++/-- operand. */
14014       if (!JNUMERIC_TYPE_P (op_type))
14015         {
14016           parse_error_context
14017             (wfl_op, "Invalid argument type `%s' to `%s'",
14018              lang_printable_name (op_type, 0), operator_string (node));
14019           TREE_TYPE (node) = error_mark_node;
14020           error_found = 1;
14021         }
14022       else
14023         {
14024           /* Before the addition, binary numeric promotion is performed on
14025              both operands, if really necessary */
14026           if (JINTEGRAL_TYPE_P (op_type))
14027             {
14028               value = build_int_2 (1, 0);
14029               TREE_TYPE (value) = TREE_TYPE (node) = op_type;
14030             }
14031           else
14032             {
14033               value = build_int_2 (1, 0);
14034               TREE_TYPE (node) = 
14035                 binary_numeric_promotion (op_type, 
14036                                           TREE_TYPE (value), &op, &value);
14037             }
14038
14039           /* We remember we might be accessing an outer field */
14040           if (outer_field_flag)
14041             {
14042               /* We re-generate an access to the field */
14043               value = build (PLUS_EXPR, TREE_TYPE (op), 
14044                              build_outer_field_access (wfl_op, decl), value);
14045                                                     
14046               /* And we patch the original access$() into a write 
14047                  with plus_op as a rhs */
14048               return outer_field_access_fix (node, op, value);
14049             }
14050
14051           /* And write back into the node. */
14052           TREE_OPERAND (node, 0) = op;
14053           TREE_OPERAND (node, 1) = value;
14054           /* Convert the overall back into its original type, if
14055              necessary, and return */
14056           if (JINTEGRAL_TYPE_P (op_type))
14057             return fold (node);
14058           else
14059             return fold (convert (op_type, node));
14060         }
14061       break;
14062
14063       /* 15.14.3 Unary Plus Operator + */
14064     case UNARY_PLUS_EXPR:
14065       /* 15.14.4 Unary Minus Operator - */
14066     case NEGATE_EXPR:
14067       if (!JNUMERIC_TYPE_P (op_type))
14068         {
14069           ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14070           TREE_TYPE (node) = error_mark_node;
14071           error_found = 1;
14072         }
14073       /* Unary numeric promotion is performed on operand */
14074       else
14075         {
14076           op = do_unary_numeric_promotion (op);
14077           prom_type = TREE_TYPE (op);
14078           if (code == UNARY_PLUS_EXPR)
14079             return fold (op);
14080         }
14081       break;
14082
14083       /* 15.14.5 Bitwise Complement Operator ~ */
14084     case BIT_NOT_EXPR:
14085       if (!JINTEGRAL_TYPE_P (op_type))
14086         {
14087           ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14088           TREE_TYPE (node) = error_mark_node;
14089           error_found = 1;
14090         }
14091       else
14092         {
14093           op = do_unary_numeric_promotion (op);
14094           prom_type = TREE_TYPE (op);
14095         }
14096       break;
14097
14098       /* 15.14.6 Logical Complement Operator ! */
14099     case TRUTH_NOT_EXPR:
14100       if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14101         {
14102           ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14103           /* But the type is known. We will report an error if further
14104              attempt of a assignment is made with this rhs */
14105           TREE_TYPE (node) = boolean_type_node;
14106           error_found = 1;
14107         }
14108       else
14109         prom_type = boolean_type_node;
14110       break;
14111
14112       /* 15.15 Cast Expression */
14113     case CONVERT_EXPR:
14114       value = patch_cast (node, wfl_operator);
14115       if (value == error_mark_node)
14116         {
14117           /* If this cast is part of an assignment, we tell the code
14118              that deals with it not to complain about a mismatch,
14119              because things have been cast, anyways */
14120           TREE_TYPE (node) = error_mark_node;
14121           error_found = 1;
14122         }
14123       else
14124         {
14125           value = fold (value);
14126           TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
14127           return value;
14128         }
14129       break;
14130     }
14131   
14132   if (error_found)
14133     return error_mark_node;
14134
14135   /* There are cases where node has been replaced by something else
14136      and we don't end up returning here: UNARY_PLUS_EXPR,
14137      CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14138   TREE_OPERAND (node, 0) = fold (op);
14139   TREE_TYPE (node) = prom_type;
14140   TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14141   return fold (node);
14142 }
14143
14144 /* Generic type resolution that sometimes takes place during node
14145    patching. Returned the resolved type or generate an error
14146    message. Return the resolved type or NULL_TREE.  */
14147
14148 static tree
14149 resolve_type_during_patch (type)
14150      tree type;
14151 {
14152   if (unresolved_type_p (type, NULL))
14153     {
14154       tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14155       if (!type_decl)
14156         {
14157           parse_error_context (type, 
14158                                "Class `%s' not found in type declaration",
14159                                IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14160           return NULL_TREE;
14161         }
14162       return TREE_TYPE (type_decl);
14163     }
14164   return type;
14165 }
14166 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14167    found. Otherwise NODE or something meant to replace it is returned.  */
14168
14169 static tree
14170 patch_cast (node, wfl_op)
14171      tree node;
14172      tree wfl_op;
14173 {
14174   tree op = TREE_OPERAND (node, 0);
14175   tree cast_type = TREE_TYPE (node);
14176   tree patched, op_type;
14177   char *t1;
14178
14179   /* Some string patching might be necessary at this stage */
14180   if ((patched = patch_string (op)))
14181     TREE_OPERAND (node, 0) = op = patched;
14182   op_type = TREE_TYPE (op);
14183
14184   /* First resolve OP_TYPE if unresolved */
14185   if (!(cast_type = resolve_type_during_patch (cast_type)))
14186     return error_mark_node;
14187
14188   /* Check on cast that are proven correct at compile time */
14189   if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14190     {
14191       /* Same type */
14192       if (cast_type == op_type)
14193         return node;
14194
14195       /* float and double type are converted to the original type main
14196          variant and then to the target type. */
14197       if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
14198         op = convert (integer_type_node, op);
14199
14200       /* Try widening/narowwing convertion. Potentially, things need
14201          to be worked out in gcc so we implement the extreme cases
14202          correctly. fold_convert() needs to be fixed. */
14203       return convert (cast_type, op);
14204     }
14205
14206   /* It's also valid to cast a boolean into a boolean */
14207   if (op_type == boolean_type_node && cast_type == boolean_type_node)
14208     return node;
14209
14210   /* null can be casted to references */
14211   if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14212     return build_null_of_type (cast_type);
14213
14214   /* The remaining legal casts involve conversion between reference
14215      types. Check for their compile time correctness. */
14216   if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type) 
14217       && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14218     {
14219       TREE_TYPE (node) = promote_type (cast_type);
14220       /* Now, the case can be determined correct at compile time if
14221          OP_TYPE can be converted into CAST_TYPE by assignment
14222          conversion (5.2) */
14223
14224       if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14225         {
14226           TREE_SET_CODE (node, NOP_EXPR);
14227           return node;
14228         }
14229
14230       if (flag_emit_class_files)
14231         {
14232           TREE_SET_CODE (node, CONVERT_EXPR);
14233           return node;
14234         }
14235
14236       /* The cast requires a run-time check */
14237       return build (CALL_EXPR, promote_type (cast_type),
14238                     build_address_of (soft_checkcast_node),
14239                     tree_cons (NULL_TREE, build_class_ref (cast_type),
14240                                build_tree_list (NULL_TREE, op)),
14241                     NULL_TREE);
14242     }
14243
14244   /* Any other casts are proven incorrect at compile time */
14245   t1 = xstrdup (lang_printable_name (op_type, 0));
14246   parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
14247                        t1, lang_printable_name (cast_type, 0));
14248   free (t1);
14249   return error_mark_node;
14250 }
14251
14252 /* Build a null constant and give it the type TYPE.  */
14253
14254 static tree
14255 build_null_of_type (type)
14256      tree type;
14257 {
14258   tree node = build_int_2 (0, 0);
14259   TREE_TYPE (node) = promote_type (type);
14260   return node;
14261 }
14262
14263 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14264    a list of indices. */
14265 static tree
14266 build_array_ref (location, array, index)
14267      int location;
14268      tree array, index;
14269 {
14270   tree node = build (ARRAY_REF, NULL_TREE, array, index);
14271   EXPR_WFL_LINECOL (node) = location;
14272   return node;
14273 }
14274
14275 /* 15.12 Array Access Expression */
14276
14277 static tree
14278 patch_array_ref (node)
14279      tree node;
14280 {
14281   tree array = TREE_OPERAND (node, 0);
14282   tree array_type  = TREE_TYPE (array);
14283   tree index = TREE_OPERAND (node, 1);
14284   tree index_type = TREE_TYPE (index);
14285   int error_found = 0;
14286
14287   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14288
14289   if (TREE_CODE (array_type) == POINTER_TYPE)
14290     array_type = TREE_TYPE (array_type);
14291
14292   /* The array reference must be an array */
14293   if (!TYPE_ARRAY_P (array_type))
14294     {
14295       parse_error_context 
14296         (wfl_operator,
14297          "`[]' can only be applied to arrays. It can't be applied to `%s'",
14298          lang_printable_name (array_type, 0));
14299       TREE_TYPE (node) = error_mark_node;
14300       error_found = 1;
14301     }
14302
14303   /* The array index undergoes unary numeric promotion. The promoted
14304      type must be int */
14305   index = do_unary_numeric_promotion (index);
14306   if (TREE_TYPE (index) != int_type_node)
14307     {
14308       if (valid_cast_to_p (index_type, int_type_node))
14309         parse_error_context (wfl_operator,
14310    "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
14311                              lang_printable_name (index_type, 0));
14312       else
14313         parse_error_context (wfl_operator,
14314           "Incompatible type for `[]'. Can't convert `%s' to `int'",
14315                              lang_printable_name (index_type, 0));
14316       TREE_TYPE (node) = error_mark_node;
14317       error_found = 1;
14318     }
14319
14320   if (error_found)
14321     return error_mark_node;
14322
14323   array_type = TYPE_ARRAY_ELEMENT (array_type);
14324
14325   if (flag_emit_class_files || flag_emit_xref)
14326     {
14327       TREE_OPERAND (node, 0) = array;
14328       TREE_OPERAND (node, 1) = index;
14329     }
14330   else
14331     node = build_java_arrayaccess (array, array_type, index);
14332   TREE_TYPE (node) = array_type;
14333   return node;
14334 }
14335
14336 /* 15.9 Array Creation Expressions */
14337
14338 static tree
14339 build_newarray_node (type, dims, extra_dims)
14340      tree type;
14341      tree dims;
14342      int extra_dims;
14343 {
14344   tree node =
14345     build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims), 
14346            build_int_2 (extra_dims, 0));
14347   return node;
14348 }
14349
14350 static tree
14351 patch_newarray (node)
14352      tree node;
14353 {
14354   tree type = TREE_OPERAND (node, 0);
14355   tree dims = TREE_OPERAND (node, 1);
14356   tree cdim, array_type;
14357   int error_found = 0;
14358   int ndims = 0;
14359   int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14360
14361   /* Dimension types are verified. It's better for the types to be
14362      verified in order. */
14363   for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14364     {
14365       int dim_error = 0;
14366       tree dim = TREE_VALUE (cdim);
14367
14368       /* Dim might have been saved during its evaluation */
14369       dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14370
14371       /* The type of each specified dimension must be an integral type. */
14372       if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14373         dim_error = 1;
14374
14375       /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14376          promoted type must be int. */
14377       else
14378         {
14379           dim = do_unary_numeric_promotion (dim);
14380           if (TREE_TYPE (dim) != int_type_node)
14381             dim_error = 1;
14382         }
14383
14384       /* Report errors on types here */
14385       if (dim_error)
14386         {
14387           parse_error_context 
14388             (TREE_PURPOSE (cdim), 
14389              "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'", 
14390              (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14391               "Explicit cast needed to" : "Can't"),
14392              lang_printable_name (TREE_TYPE (dim), 0));
14393           error_found = 1;
14394         }
14395
14396       TREE_PURPOSE (cdim) = NULL_TREE;
14397     }
14398
14399   /* Resolve array base type if unresolved */
14400   if (!(type = resolve_type_during_patch (type)))
14401     error_found = 1;
14402
14403   if (error_found)
14404     {
14405       /* We don't want further evaluation of this bogus array creation
14406          operation */
14407       TREE_TYPE (node) = error_mark_node;
14408       return error_mark_node;
14409     }
14410
14411   /* Set array_type to the actual (promoted) array type of the result. */
14412   if (TREE_CODE (type) == RECORD_TYPE)
14413     type = build_pointer_type (type);
14414   while (--xdims >= 0)
14415     {
14416       type = promote_type (build_java_array_type (type, -1));
14417     }
14418   dims = nreverse (dims);
14419   array_type = type;
14420   for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14421     {
14422       type = array_type;
14423       array_type
14424         = build_java_array_type (type,
14425                                  TREE_CODE (cdim) == INTEGER_CST
14426                                  ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14427                                  : -1);
14428       array_type = promote_type (array_type);
14429     }
14430   dims = nreverse (dims);
14431
14432   /* The node is transformed into a function call. Things are done
14433      differently according to the number of dimensions. If the number
14434      of dimension is equal to 1, then the nature of the base type
14435      (primitive or not) matters. */
14436   if (ndims == 1)
14437     return build_new_array (type, TREE_VALUE (dims));
14438   
14439   /* Can't reuse what's already written in expr.c because it uses the
14440      JVM stack representation. Provide a build_multianewarray. FIXME */
14441   return build (CALL_EXPR, array_type,
14442                 build_address_of (soft_multianewarray_node),
14443                 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
14444                            tree_cons (NULL_TREE, 
14445                                       build_int_2 (ndims, 0), dims )),
14446                 NULL_TREE);
14447 }
14448
14449 /* 10.6 Array initializer.  */
14450
14451 /* Build a wfl for array element that don't have one, so we can
14452    pin-point errors.  */
14453
14454 static tree
14455 maybe_build_array_element_wfl (node)
14456      tree node;
14457 {
14458   if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14459     return build_expr_wfl (NULL_TREE, ctxp->filename,
14460                            ctxp->elc.line, ctxp->elc.prev_col);
14461   else
14462     return NULL_TREE;
14463 }
14464
14465 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14466    identification of initialized arrays easier to detect during walk
14467    and expansion.  */
14468
14469 static tree
14470 build_new_array_init (location, values)
14471      int location;
14472      tree values;
14473 {
14474   tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14475   tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14476   EXPR_WFL_LINECOL (to_return) = location;
14477   return to_return;
14478 }
14479
14480 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14481    occurred.  Otherwise return NODE after having set its type
14482    appropriately.  */
14483
14484 static tree
14485 patch_new_array_init (type, node)
14486      tree type, node;
14487 {
14488   int error_seen = 0;
14489   tree current, element_type;
14490   HOST_WIDE_INT length;
14491   int all_constant = 1;
14492   tree init = TREE_OPERAND (node, 0);
14493
14494   if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14495     {
14496       parse_error_context (node,
14497                            "Invalid array initializer for non-array type `%s'",
14498                            lang_printable_name (type, 1));
14499       return error_mark_node;
14500     }
14501   type = TREE_TYPE (type);
14502   element_type = TYPE_ARRAY_ELEMENT (type);
14503
14504   CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14505
14506   for (length = 0, current = CONSTRUCTOR_ELTS (init);
14507        current;  length++, current = TREE_CHAIN (current))
14508     {
14509       tree elt = TREE_VALUE (current);
14510       if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14511         {
14512           error_seen |= array_constructor_check_entry (element_type, current);
14513           elt = TREE_VALUE (current);
14514           /* When compiling to native code, STRING_CST is converted to
14515              INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14516           if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14517             all_constant = 0;
14518         }
14519       else
14520         {
14521           TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14522           TREE_PURPOSE (current) = NULL_TREE;
14523           all_constant = 0;
14524         }
14525       if (elt && TREE_CODE (elt) == TREE_LIST 
14526           && TREE_VALUE (elt) == error_mark_node)
14527         error_seen = 1;
14528     }
14529
14530   if (error_seen)
14531     return error_mark_node;
14532
14533   /* Create a new type. We can't reuse the one we have here by
14534      patching its dimension because it originally is of dimension -1
14535      hence reused by gcc. This would prevent triangular arrays. */
14536   type = build_java_array_type (element_type, length);
14537   TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14538   TREE_TYPE (node) = promote_type (type);
14539   TREE_CONSTANT (init) = all_constant;
14540   TREE_CONSTANT (node) = all_constant;
14541   return node;
14542 }
14543
14544 /* Verify that one entry of the initializer element list can be
14545    assigned to the array base type. Report 1 if an error occurred, 0
14546    otherwise.  */
14547
14548 static int
14549 array_constructor_check_entry (type, entry)
14550      tree type, entry;
14551 {
14552   char *array_type_string = NULL;       /* For error reports */
14553   tree value, type_value, new_value, wfl_value, patched;
14554   int error_seen = 0;
14555
14556   new_value = NULL_TREE;
14557   wfl_value = TREE_VALUE (entry);
14558
14559   value = java_complete_tree (TREE_VALUE (entry));
14560   /* patch_string return error_mark_node if arg is error_mark_node */
14561   if ((patched = patch_string (value)))
14562     value = patched;
14563   if (value == error_mark_node)
14564     return 1;
14565   
14566   type_value = TREE_TYPE (value);
14567   
14568   /* At anytime, try_builtin_assignconv can report a warning on
14569      constant overflow during narrowing. */
14570   SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14571   new_value = try_builtin_assignconv (wfl_operator, type, value);
14572   if (!new_value && (new_value = try_reference_assignconv (type, value)))
14573     type_value = promote_type (type);
14574
14575   /* Check and report errors */
14576   if (!new_value)
14577     {
14578       const char *const msg = (!valid_cast_to_p (type_value, type) ?
14579                    "Can't" : "Explicit cast needed to");
14580       if (!array_type_string)
14581         array_type_string = xstrdup (lang_printable_name (type, 1));
14582       parse_error_context 
14583         (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14584          msg, lang_printable_name (type_value, 1), array_type_string);
14585       error_seen = 1;
14586     }
14587   
14588   if (new_value)
14589     TREE_VALUE (entry) = new_value;
14590
14591   if (array_type_string)
14592     free (array_type_string);
14593
14594   TREE_PURPOSE (entry) = NULL_TREE;
14595   return error_seen;
14596 }
14597
14598 static tree
14599 build_this (location)
14600      int location;
14601 {
14602   tree node = build_wfl_node (this_identifier_node);
14603   TREE_SET_CODE (node, THIS_EXPR);
14604   EXPR_WFL_LINECOL (node) = location;
14605   return node;
14606 }
14607
14608 /* 14.15 The return statement. It builds a modify expression that
14609    assigns the returned value to the RESULT_DECL that hold the value
14610    to be returned. */
14611
14612 static tree
14613 build_return (location, op)
14614      int location;
14615      tree op;
14616 {
14617   tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14618   EXPR_WFL_LINECOL (node) = location;
14619   node = build_debugable_stmt (location, node);
14620   return node;
14621 }
14622
14623 static tree
14624 patch_return (node)
14625      tree node;
14626 {
14627   tree return_exp = TREE_OPERAND (node, 0);
14628   tree meth = current_function_decl;
14629   tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14630   int error_found = 0;
14631
14632   TREE_TYPE (node) = error_mark_node;
14633   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14634
14635   /* It's invalid to have a return value within a function that is
14636      declared with the keyword void or that is a constructor */
14637   if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14638     error_found = 1;
14639
14640   /* It's invalid to use a return statement in a static block */
14641   if (DECL_CLINIT_P (current_function_decl))
14642     error_found = 1;
14643
14644   /* It's invalid to have a no return value within a function that
14645      isn't declared with the keyword `void' */
14646   if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14647     error_found = 2;
14648   
14649   if (DECL_INSTINIT_P (current_function_decl))
14650     error_found = 1;
14651
14652   if (error_found)
14653     {
14654       if (DECL_INSTINIT_P (current_function_decl))
14655         parse_error_context (wfl_operator,
14656                              "`return' inside instance initializer");
14657         
14658       else if (DECL_CLINIT_P (current_function_decl))
14659         parse_error_context (wfl_operator,
14660                              "`return' inside static initializer");
14661
14662       else if (!DECL_CONSTRUCTOR_P (meth))
14663         {
14664           char *t = xstrdup (lang_printable_name (mtype, 0));
14665           parse_error_context (wfl_operator, 
14666                                "`return' with%s value from `%s %s'",
14667                                (error_found == 1 ? "" : "out"), 
14668                                t, lang_printable_name (meth, 0));
14669           free (t);
14670         }
14671       else
14672         parse_error_context (wfl_operator, 
14673                              "`return' with value from constructor `%s'",
14674                              lang_printable_name (meth, 0));
14675       return error_mark_node;
14676     }
14677
14678   /* If we have a return_exp, build a modify expression and expand
14679      it. Note: at that point, the assignment is declared valid, but we
14680      may want to carry some more hacks */
14681   if (return_exp)
14682     {
14683       tree exp = java_complete_tree (return_exp);
14684       tree modify, patched;
14685
14686       if ((patched = patch_string (exp)))
14687         exp = patched;
14688       
14689       modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14690       EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14691       modify = java_complete_tree (modify);
14692
14693       if (modify != error_mark_node)
14694         {
14695           TREE_SIDE_EFFECTS (modify) = 1;
14696           TREE_OPERAND (node, 0) = modify;
14697         }
14698       else
14699         return error_mark_node;
14700     }
14701   TREE_TYPE (node) = void_type_node;
14702   TREE_SIDE_EFFECTS (node) = 1;
14703   return node;
14704 }
14705
14706 /* 14.8 The if Statement */
14707
14708 static tree
14709 build_if_else_statement (location, expression, if_body, else_body)
14710      int location;
14711      tree expression, if_body, else_body;
14712 {
14713   tree node;
14714   if (!else_body)
14715     else_body = empty_stmt_node;
14716   node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14717   EXPR_WFL_LINECOL (node) = location;
14718   node = build_debugable_stmt (location, node);
14719   return node;
14720 }
14721
14722 static tree
14723 patch_if_else_statement (node)
14724      tree node;
14725 {
14726   tree expression = TREE_OPERAND (node, 0);
14727   int can_complete_normally
14728     = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14729        | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
14730
14731   TREE_TYPE (node) = error_mark_node;
14732   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14733
14734   /* The type of expression must be boolean */
14735   if (TREE_TYPE (expression) != boolean_type_node
14736       && TREE_TYPE (expression) != promoted_boolean_type_node)
14737     {
14738       parse_error_context 
14739         (wfl_operator, 
14740          "Incompatible type for `if'. Can't convert `%s' to `boolean'", 
14741          lang_printable_name (TREE_TYPE (expression), 0));
14742       return error_mark_node;
14743     }
14744   
14745   if (TREE_CODE (expression) == INTEGER_CST)
14746     {
14747       if (integer_zerop (expression))
14748         node = TREE_OPERAND (node, 2);
14749       else
14750         node = TREE_OPERAND (node, 1);
14751       if (CAN_COMPLETE_NORMALLY (node) != can_complete_normally)
14752         {
14753           node = build (COMPOUND_EXPR, void_type_node, node, empty_stmt_node);
14754           CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
14755         }
14756       return node;
14757     }
14758   TREE_TYPE (node) = void_type_node;
14759   TREE_SIDE_EFFECTS (node) = 1;
14760   CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
14761   return node;
14762 }
14763
14764 /* 14.6 Labeled Statements */
14765
14766 /* Action taken when a lableled statement is parsed. a new
14767    LABELED_BLOCK_EXPR is created. No statement is attached to the
14768    label, yet.  LABEL can be NULL_TREE for artificially-generated blocks. */
14769
14770 static tree
14771 build_labeled_block (location, label)
14772      int location;
14773      tree label;
14774 {
14775   tree label_name ;
14776   tree label_decl, node;
14777   if (label == NULL_TREE || label == continue_identifier_node)
14778     label_name = label;
14779   else
14780     {
14781       label_name = merge_qualified_name (label_id, label);
14782       /* Issue an error if we try to reuse a label that was previously
14783          declared */
14784       if (IDENTIFIER_LOCAL_VALUE (label_name))
14785         {
14786           EXPR_WFL_LINECOL (wfl_operator) = location;
14787           parse_error_context (wfl_operator,
14788             "Declaration of `%s' shadows a previous label declaration",
14789                                IDENTIFIER_POINTER (label));
14790           EXPR_WFL_LINECOL (wfl_operator) = 
14791             EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14792           parse_error_context (wfl_operator,
14793             "This is the location of the previous declaration of label `%s'",
14794                                IDENTIFIER_POINTER (label));
14795           java_error_count--;
14796         }
14797     }
14798
14799   label_decl = create_label_decl (label_name);
14800   node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14801   EXPR_WFL_LINECOL (node) = location;
14802   TREE_SIDE_EFFECTS (node) = 1;
14803   return node;
14804 }
14805
14806 /* A labeled statement LBE is attached a statement.  */
14807
14808 static tree
14809 finish_labeled_statement (lbe, statement)
14810      tree lbe;                  /* Labeled block expr */
14811      tree statement;
14812 {
14813   /* In anyways, tie the loop to its statement */
14814   LABELED_BLOCK_BODY (lbe) = statement;
14815   pop_labeled_block ();
14816   POP_LABELED_BLOCK ();
14817   return lbe;
14818 }
14819
14820 /* 14.10, 14.11, 14.12 Loop Statements */
14821
14822 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14823    list. */
14824
14825 static tree
14826 build_new_loop (loop_body)
14827      tree loop_body;
14828 {
14829   tree loop =  build (LOOP_EXPR, NULL_TREE, loop_body);
14830   TREE_SIDE_EFFECTS (loop) = 1;
14831   PUSH_LOOP (loop);
14832   return loop;
14833 }
14834
14835 /* Create a loop body according to the following structure:
14836      COMPOUND_EXPR
14837        COMPOUND_EXPR            (loop main body)
14838          EXIT_EXPR              (this order is for while/for loops.
14839          LABELED_BLOCK_EXPR      the order is reversed for do loops)
14840            LABEL_DECL           (a continue occurring here branches at the 
14841            BODY                  end of this labeled block)
14842        INCREMENT                (if any)
14843
14844   REVERSED, if non zero, tells that the loop condition expr comes
14845   after the body, like in the do-while loop.
14846
14847   To obtain a loop, the loop body structure described above is
14848   encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14849
14850    LABELED_BLOCK_EXPR
14851      LABEL_DECL                   (use this label to exit the loop)
14852      LOOP_EXPR
14853        <structure described above> */
14854
14855 static tree
14856 build_loop_body (location, condition, reversed)
14857      int location;
14858      tree condition;
14859      int reversed;
14860 {
14861   tree first, second, body;
14862
14863   condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14864   EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14865   condition = build_debugable_stmt (location, condition);
14866   TREE_SIDE_EFFECTS (condition) = 1;
14867
14868   body = build_labeled_block (0, continue_identifier_node);
14869   first = (reversed ? body : condition);
14870   second = (reversed ? condition : body);
14871   return 
14872     build (COMPOUND_EXPR, NULL_TREE, 
14873            build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14874 }
14875
14876 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14877    their order) on the current loop. Unlink the current loop from the
14878    loop list.  */
14879
14880 static tree
14881 finish_loop_body (location, condition, body, reversed)
14882      int location;
14883      tree condition, body;
14884      int reversed;
14885 {
14886   tree to_return = ctxp->current_loop;
14887   tree loop_body = LOOP_EXPR_BODY (to_return);
14888   if (condition)
14889     {
14890       tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14891       /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14892          The real EXIT_EXPR is one operand further. */
14893       EXPR_WFL_LINECOL (cnode) = location;
14894       /* This one is for accurate error reports */
14895       EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14896       TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14897     }
14898   LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14899   POP_LOOP ();
14900   return to_return;
14901 }
14902
14903 /* Tailored version of finish_loop_body for FOR loops, when FOR
14904    loops feature the condition part */
14905
14906 static tree
14907 finish_for_loop (location, condition, update, body)
14908     int location;
14909     tree condition, update, body;
14910 {
14911   /* Put the condition and the loop body in place */
14912   tree loop = finish_loop_body (location, condition, body, 0);
14913   /* LOOP is the current loop which has been now popped of the loop
14914      stack. Install the update block */
14915   LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14916   return loop;
14917 }
14918
14919 /* Try to find the loop a block might be related to. This comprises
14920    the case where the LOOP_EXPR is found as the second operand of a
14921    COMPOUND_EXPR, because the loop happens to have an initialization
14922    part, then expressed as the first operand of the COMPOUND_EXPR. If
14923    the search finds something, 1 is returned. Otherwise, 0 is
14924    returned. The search is assumed to start from a
14925    LABELED_BLOCK_EXPR's block.  */
14926
14927 static tree
14928 search_loop (statement)
14929     tree statement;
14930 {
14931   if (TREE_CODE (statement) == LOOP_EXPR)
14932     return statement;
14933
14934   if (TREE_CODE (statement) == BLOCK)
14935     statement = BLOCK_SUBBLOCKS (statement);
14936   else
14937     return NULL_TREE;
14938
14939   if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14940     while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14941       statement = TREE_OPERAND (statement, 1);
14942
14943   return (TREE_CODE (statement) == LOOP_EXPR
14944           && FOR_LOOP_P (statement) ? statement : NULL_TREE);
14945 }
14946
14947 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14948    returned otherwise.  */
14949
14950 static int
14951 labeled_block_contains_loop_p (block, loop)
14952     tree block, loop;
14953 {
14954   if (!block)
14955     return 0;
14956
14957   if (LABELED_BLOCK_BODY (block) == loop)
14958     return 1;
14959
14960   if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
14961     return 1;
14962
14963   return 0;
14964 }
14965
14966 /* If the loop isn't surrounded by a labeled statement, create one and
14967    insert LOOP as its body.  */
14968
14969 static tree
14970 patch_loop_statement (loop)
14971      tree loop;
14972 {
14973   tree loop_label;
14974
14975   TREE_TYPE (loop) = void_type_node;
14976   if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14977     return loop;
14978
14979   loop_label = build_labeled_block (0, NULL_TREE);
14980   /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14981      that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
14982   LABELED_BLOCK_BODY (loop_label) = loop;
14983   PUSH_LABELED_BLOCK (loop_label);
14984   return loop_label;
14985 }
14986
14987 /* 14.13, 14.14: break and continue Statements */
14988
14989 /* Build a break or a continue statement. a null NAME indicates an
14990    unlabeled break/continue statement.  */
14991
14992 static tree
14993 build_bc_statement (location, is_break, name)
14994      int location, is_break;
14995      tree name;
14996 {
14997   tree break_continue, label_block_expr = NULL_TREE;
14998
14999   if (name)
15000     {
15001       if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE 
15002             (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15003         /* Null means that we don't have a target for this named
15004            break/continue. In this case, we make the target to be the
15005            label name, so that the error can be reported accuratly in
15006            patch_bc_statement. */
15007         label_block_expr = EXPR_WFL_NODE (name);
15008     }
15009   /* Unlabeled break/continue will be handled during the
15010      break/continue patch operation */
15011   break_continue 
15012     = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
15013
15014   IS_BREAK_STMT_P (break_continue) = is_break;
15015   TREE_SIDE_EFFECTS (break_continue) = 1;
15016   EXPR_WFL_LINECOL (break_continue) = location;
15017   break_continue = build_debugable_stmt (location, break_continue);
15018   return break_continue;
15019 }
15020
15021 /* Verification of a break/continue statement. */
15022
15023 static tree
15024 patch_bc_statement (node)
15025      tree node;
15026 {
15027   tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15028   tree labeled_block = ctxp->current_labeled_block;
15029   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15030  
15031   /* Having an identifier here means that the target is unknown. */
15032   if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15033     {
15034       parse_error_context (wfl_operator, "No label definition found for `%s'",
15035                            IDENTIFIER_POINTER (bc_label));
15036       return error_mark_node;
15037     }
15038   if (! IS_BREAK_STMT_P (node))
15039     {
15040       /* It's a continue statement. */
15041       for (;; labeled_block = TREE_CHAIN (labeled_block))
15042         {
15043           if (labeled_block == NULL_TREE)
15044             {
15045               if (bc_label == NULL_TREE)
15046                 parse_error_context (wfl_operator,
15047                                      "`continue' must be in loop");
15048               else
15049                 parse_error_context 
15050                   (wfl_operator, "continue label `%s' does not name a loop",
15051                    IDENTIFIER_POINTER (bc_label));
15052               return error_mark_node;
15053             }
15054           if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15055                == continue_identifier_node)
15056               && (bc_label == NULL_TREE
15057                   || TREE_CHAIN (labeled_block) == bc_label))
15058             {
15059               bc_label = labeled_block;
15060               break;
15061             }
15062         }
15063     }
15064   else if (!bc_label)
15065     { 
15066       for (;; labeled_block = TREE_CHAIN (labeled_block))
15067         {
15068           if (labeled_block == NULL_TREE)
15069             {
15070               parse_error_context (wfl_operator,
15071                                      "`break' must be in loop or switch");
15072               return error_mark_node;
15073             }
15074           target_stmt = LABELED_BLOCK_BODY (labeled_block);
15075           if (TREE_CODE (target_stmt) == SWITCH_EXPR
15076               || search_loop (target_stmt))
15077             {
15078               bc_label = labeled_block;
15079               break;
15080             }
15081         }
15082     }
15083
15084   EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15085   CAN_COMPLETE_NORMALLY (bc_label) = 1;
15086
15087   /* Our break/continue don't return values. */
15088   TREE_TYPE (node) = void_type_node;
15089   /* Encapsulate the break within a compound statement so that it's
15090      expanded all the times by expand_expr (and not clobbered
15091      sometimes, like after a if statement) */
15092   node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15093   TREE_SIDE_EFFECTS (node) = 1;
15094   return node;
15095 }
15096
15097 /* Process the exit expression belonging to a loop. Its type must be
15098    boolean.  */
15099
15100 static tree
15101 patch_exit_expr (node)
15102      tree node;
15103 {
15104   tree expression = TREE_OPERAND (node, 0);
15105   TREE_TYPE (node) = error_mark_node;
15106   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15107
15108   /* The type of expression must be boolean */
15109   if (TREE_TYPE (expression) != boolean_type_node)
15110     {
15111       parse_error_context 
15112         (wfl_operator, 
15113     "Incompatible type for loop conditional. Can't convert `%s' to `boolean'", 
15114          lang_printable_name (TREE_TYPE (expression), 0));
15115       return error_mark_node;
15116     }
15117   /* Now we know things are allright, invert the condition, fold and
15118      return */
15119   TREE_OPERAND (node, 0) = 
15120     fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15121
15122   if (! integer_zerop (TREE_OPERAND (node, 0))
15123       && ctxp->current_loop != NULL_TREE
15124       && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15125     CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15126   if (! integer_onep (TREE_OPERAND (node, 0)))
15127     CAN_COMPLETE_NORMALLY (node) = 1;
15128
15129
15130   TREE_TYPE (node) = void_type_node;
15131   return node;
15132 }
15133
15134 /* 14.9 Switch statement */
15135
15136 static tree
15137 patch_switch_statement (node)
15138      tree node;
15139 {
15140   tree se = TREE_OPERAND (node, 0), se_type;
15141   tree save, iter;
15142
15143   /* Complete the switch expression */
15144   se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15145   se_type = TREE_TYPE (se);
15146   /* The type of the switch expression must be char, byte, short or
15147      int */
15148   if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15149     {
15150       EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15151       parse_error_context (wfl_operator,
15152           "Incompatible type for `switch'. Can't convert `%s' to `int'",
15153                            lang_printable_name (se_type, 0));
15154       /* This is what java_complete_tree will check */
15155       TREE_OPERAND (node, 0) = error_mark_node;
15156       return error_mark_node;
15157     }
15158
15159   /* Save and restore the outer case label list.  */
15160   save = case_label_list;
15161   case_label_list = NULL_TREE;
15162
15163   TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15164
15165   /* See if we've found a duplicate label.  We can't leave this until
15166      code generation, because in `--syntax-only' and `-C' modes we
15167      don't do ordinary code generation.  */
15168   for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15169     {
15170       HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15171       tree subiter;
15172       for (subiter = TREE_CHAIN (iter);
15173            subiter != NULL_TREE;
15174            subiter = TREE_CHAIN (subiter))
15175         {
15176           HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15177           if (val == subval)
15178             {
15179               EXPR_WFL_LINECOL (wfl_operator)
15180                 = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15181               /* The case_label_list is in reverse order, so print the
15182                  outer label first.  */
15183               parse_error_context (wfl_operator, "duplicate case label: `"
15184                                    HOST_WIDE_INT_PRINT_DEC "'", subval);
15185               EXPR_WFL_LINECOL (wfl_operator)
15186                 = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15187               parse_error_context (wfl_operator, "original label is here");
15188
15189               break;
15190             }
15191         }
15192     }
15193
15194   case_label_list = save;
15195
15196   /* Ready to return */
15197   if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15198     {
15199       TREE_TYPE (node) = error_mark_node;
15200       return error_mark_node;
15201     }
15202   TREE_TYPE (node) = void_type_node;
15203   TREE_SIDE_EFFECTS (node) = 1;
15204   CAN_COMPLETE_NORMALLY (node)
15205     = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)) 
15206       || ! SWITCH_HAS_DEFAULT (node);
15207   return node;
15208 }
15209
15210 /* 14.18 The try/catch statements */
15211
15212 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15213    catches TYPE and executes CATCH_STMTS.  */
15214
15215 static tree
15216 encapsulate_with_try_catch (location, type, try_stmts, catch_stmts)
15217      int location;
15218      tree type, try_stmts, catch_stmts;
15219 {
15220   tree try_block, catch_clause_param, catch_block, catch;
15221
15222   /* First build a try block */
15223   try_block = build_expr_block (try_stmts, NULL_TREE);
15224
15225   /* Build a catch block: we need a catch clause parameter */
15226   catch_clause_param = build_decl (VAR_DECL, 
15227                                    wpv_id, build_pointer_type (type));
15228   /* And a block */
15229   catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15230   
15231   /* Initialize the variable and store in the block */
15232   catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15233                  build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15234   add_stmt_to_block (catch_block, NULL_TREE, catch);
15235
15236   /* Add the catch statements */
15237   add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15238
15239   /* Now we can build a CATCH_EXPR */
15240   catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
15241
15242   return build_try_statement (location, try_block, catch_block);
15243 }
15244
15245 static tree
15246 build_try_statement (location, try_block, catches)
15247      int location;
15248      tree try_block, catches;
15249 {
15250   tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
15251   EXPR_WFL_LINECOL (node) = location;
15252   return node;
15253 }
15254
15255 static tree
15256 build_try_finally_statement (location, try_block, finally)
15257      int location;
15258      tree try_block, finally;
15259 {
15260   tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15261   EXPR_WFL_LINECOL (node) = location;
15262   return node;
15263 }
15264
15265 static tree
15266 patch_try_statement (node)
15267      tree node;
15268 {
15269   int error_found = 0;
15270   tree try = TREE_OPERAND (node, 0);
15271   /* Exception handlers are considered in left to right order */
15272   tree catch = nreverse (TREE_OPERAND (node, 1));
15273   tree current, caught_type_list = NULL_TREE;
15274
15275   /* Check catch clauses, if any. Every time we find an error, we try
15276      to process the next catch clause. We process the catch clause before
15277      the try block so that when processing the try block we can check thrown
15278      exceptions againts the caught type list. */
15279   for (current = catch; current; current = TREE_CHAIN (current))
15280     {
15281       tree carg_decl, carg_type;
15282       tree sub_current, catch_block, catch_clause;
15283       int unreachable;
15284
15285       /* At this point, the structure of the catch clause is
15286            CATCH_EXPR           (catch node)
15287              BLOCK              (with the decl of the parameter)
15288                COMPOUND_EXPR
15289                  MODIFY_EXPR   (assignment of the catch parameter)
15290                  BLOCK          (catch clause block)
15291        */
15292       catch_clause = TREE_OPERAND (current, 0);
15293       carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15294       carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15295
15296       /* Catch clauses can't have more than one parameter declared,
15297          but it's already enforced by the grammar. Make sure that the
15298          only parameter of the clause statement in of class Throwable
15299          or a subclass of Throwable, but that was done earlier. The
15300          catch clause parameter type has also been resolved. */
15301       
15302       /* Just make sure that the catch clause parameter type inherits
15303          from java.lang.Throwable */
15304       if (!inherits_from_p (carg_type, throwable_type_node))
15305         {
15306           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15307           parse_error_context (wfl_operator,
15308                                "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
15309                                lang_printable_name (carg_type, 0));
15310           error_found = 1;
15311           continue;
15312         }
15313       
15314       /* Partial check for unreachable catch statement: The catch
15315          clause is reachable iff is no earlier catch block A in
15316          the try statement such that the type of the catch
15317          clause's parameter is the same as or a subclass of the
15318          type of A's parameter */
15319       unreachable = 0;
15320       for (sub_current = catch;
15321            sub_current != current; sub_current = TREE_CHAIN (sub_current))
15322         {
15323           tree sub_catch_clause, decl;
15324           sub_catch_clause = TREE_OPERAND (sub_current, 0);
15325           decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15326
15327           if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15328             {
15329               EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15330               parse_error_context 
15331                 (wfl_operator,
15332                  "`catch' not reached because of the catch clause at line %d",
15333                  EXPR_WFL_LINENO (sub_current));
15334               unreachable = error_found = 1;
15335               break;
15336             }
15337         }
15338       /* Complete the catch clause block */
15339       catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15340       if (catch_block == error_mark_node)
15341         {
15342           error_found = 1;
15343           continue;
15344         }
15345       if (CAN_COMPLETE_NORMALLY (catch_block))
15346         CAN_COMPLETE_NORMALLY (node) = 1;
15347       TREE_OPERAND (current, 0) = catch_block;
15348
15349       if (unreachable)
15350         continue;
15351
15352       /* Things to do here: the exception must be thrown */
15353
15354       /* Link this type to the caught type list */
15355       caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15356     }
15357
15358   PUSH_EXCEPTIONS (caught_type_list);
15359   if ((try = java_complete_tree (try)) == error_mark_node)
15360     error_found = 1;
15361   if (CAN_COMPLETE_NORMALLY (try))
15362     CAN_COMPLETE_NORMALLY (node) = 1;
15363   POP_EXCEPTIONS ();
15364
15365   /* Verification ends here */
15366   if (error_found) 
15367     return error_mark_node;
15368
15369   TREE_OPERAND (node, 0) = try;
15370   TREE_OPERAND (node, 1) = catch;
15371   TREE_TYPE (node) = void_type_node;
15372   return node;
15373 }
15374
15375 /* 14.17 The synchronized Statement */
15376
15377 static tree
15378 patch_synchronized_statement (node, wfl_op1)
15379     tree node, wfl_op1;
15380 {
15381   tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15382   tree block = TREE_OPERAND (node, 1);
15383
15384   tree tmp, enter, exit, expr_decl, assignment;
15385
15386   if (expr == error_mark_node)
15387     {
15388       block = java_complete_tree (block);
15389       return expr;
15390     }
15391
15392   /* We might be trying to synchronize on a STRING_CST */
15393   if ((tmp = patch_string (expr)))
15394     expr = tmp;
15395
15396   /* The TYPE of expr must be a reference type */
15397   if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15398     {
15399       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15400       parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
15401                            lang_printable_name (TREE_TYPE (expr), 0));
15402       return error_mark_node;
15403     }
15404
15405   if (flag_emit_xref)
15406     {
15407       TREE_OPERAND (node, 0) = expr;
15408       TREE_OPERAND (node, 1) = java_complete_tree (block);
15409       CAN_COMPLETE_NORMALLY (node) = 1;
15410       return node;
15411     }
15412
15413   /* Generate a try-finally for the synchronized statement, except
15414      that the handler that catches all throw exception calls
15415      _Jv_MonitorExit and then rethrow the exception.
15416      The synchronized statement is then implemented as:
15417      TRY 
15418        {
15419          _Jv_MonitorEnter (expression)
15420          synchronized_block
15421          _Jv_MonitorExit (expression)
15422        }
15423      CATCH_ALL
15424        {
15425          e = _Jv_exception_info ();
15426          _Jv_MonitorExit (expression)
15427          Throw (e);
15428        } */
15429
15430   expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15431   BUILD_MONITOR_ENTER (enter, expr_decl);
15432   BUILD_MONITOR_EXIT (exit, expr_decl);
15433   CAN_COMPLETE_NORMALLY (enter) = 1;
15434   CAN_COMPLETE_NORMALLY (exit) = 1;
15435   assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15436   TREE_SIDE_EFFECTS (assignment) = 1;
15437   node = build (COMPOUND_EXPR, NULL_TREE,
15438                 build (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15439                 build (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15440   node = build_expr_block (node, expr_decl);
15441
15442   return java_complete_tree (node);
15443 }
15444
15445 /* 14.16 The throw Statement */
15446
15447 static tree
15448 patch_throw_statement (node, wfl_op1)
15449     tree node, wfl_op1;
15450 {
15451   tree expr = TREE_OPERAND (node, 0);
15452   tree type = TREE_TYPE (expr);
15453   int unchecked_ok = 0, tryblock_throws_ok = 0;
15454
15455   /* Thrown expression must be assignable to java.lang.Throwable */
15456   if (!try_reference_assignconv (throwable_type_node, expr))
15457     {
15458       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15459       parse_error_context (wfl_operator,
15460     "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
15461                            lang_printable_name (type, 0));
15462       /* If the thrown expression was a reference, we further the
15463          compile-time check. */
15464       if (!JREFERENCE_TYPE_P (type))
15465         return error_mark_node;
15466     }
15467
15468   /* At least one of the following must be true */
15469
15470   /* The type of the throw expression is a not checked exception,
15471      i.e. is a unchecked expression. */
15472   unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15473
15474   SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15475   /* An instance can't throw a checked exception unless that exception
15476      is explicitly declared in the `throws' clause of each
15477      constructor. This doesn't apply to anonymous classes, since they
15478      don't have declared constructors. */
15479   if (!unchecked_ok 
15480       && DECL_INSTINIT_P (current_function_decl)
15481       && !ANONYMOUS_CLASS_P (current_class))
15482     {
15483       tree current;
15484       for (current = TYPE_METHODS (current_class); current; 
15485            current = TREE_CHAIN (current))
15486         if (DECL_CONSTRUCTOR_P (current) 
15487             && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15488           {
15489             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)", 
15490                                  lang_printable_name (TREE_TYPE (expr), 0));
15491             return error_mark_node;
15492           }
15493     }
15494
15495   /* Throw is contained in a try statement and at least one catch
15496      clause can receive the thrown expression or the current method is
15497      declared to throw such an exception. Or, the throw statement is
15498      contained in a method or constructor declaration and the type of
15499      the Expression is assignable to at least one type listed in the
15500      throws clause the declaration. */
15501   if (!unchecked_ok)
15502     tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15503   if (!(unchecked_ok || tryblock_throws_ok))
15504     {
15505       /* If there is a surrounding try block that has no matching
15506          clatch clause, report it first. A surrounding try block exits
15507          only if there is something after the list of checked
15508          exception thrown by the current function (if any). */
15509       if (IN_TRY_BLOCK_P ())
15510         parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
15511                              lang_printable_name (type, 0));
15512       /* If we have no surrounding try statement and the method doesn't have
15513          any throws, report it now. FIXME */
15514
15515       /* We report that the exception can't be throw from a try block
15516          in all circumstances but when the `throw' is inside a static
15517          block. */
15518       else if (!EXCEPTIONS_P (currently_caught_type_list) 
15519                && !tryblock_throws_ok)
15520         {
15521           if (DECL_CLINIT_P (current_function_decl))
15522             parse_error_context (wfl_operator,
15523                    "Checked exception `%s' can't be thrown in initializer",
15524                                  lang_printable_name (type, 0));
15525           else
15526             parse_error_context (wfl_operator,
15527                    "Checked exception `%s' isn't thrown from a `try' block", 
15528                                  lang_printable_name (type, 0));
15529         }
15530       /* Otherwise, the current method doesn't have the appropriate
15531          throws declaration */
15532       else
15533         parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)", 
15534                              lang_printable_name (type, 0));
15535       return error_mark_node;
15536     }
15537
15538   if (! flag_emit_class_files && ! flag_emit_xref)
15539     BUILD_THROW (node, expr);
15540
15541   /* If doing xrefs, keep the location where the `throw' was seen. */
15542   if (flag_emit_xref)
15543     EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15544   return node;
15545 }
15546
15547 /* Check that exception said to be thrown by method DECL can be
15548    effectively caught from where DECL is invoked.  */
15549
15550 static void
15551 check_thrown_exceptions (location, decl)
15552      int location;
15553      tree decl;
15554 {
15555   tree throws;
15556   /* For all the unchecked exceptions thrown by DECL */
15557   for (throws = DECL_FUNCTION_THROWS (decl); throws; 
15558        throws = TREE_CHAIN (throws)) 
15559     if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15560       {
15561 #if 1
15562         /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15563         if (DECL_NAME (decl) == get_identifier ("clone"))
15564           continue;
15565 #endif
15566         EXPR_WFL_LINECOL (wfl_operator) = location;
15567         if (DECL_FINIT_P (current_function_decl))
15568           parse_error_context
15569             (wfl_operator, "Exception `%s' can't be thrown in initializer",
15570              lang_printable_name (TREE_VALUE (throws), 0));
15571         else 
15572           {
15573             parse_error_context 
15574               (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'", 
15575                lang_printable_name (TREE_VALUE (throws), 0),
15576                (DECL_INIT_P (current_function_decl) ?
15577                 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15578                 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15579           }
15580       }
15581 }
15582
15583 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15584    try-catch blocks, OR is listed in the `throws' clause of the
15585    current method.  */
15586
15587 static int
15588 check_thrown_exceptions_do (exception)
15589      tree exception;
15590 {
15591   tree list = currently_caught_type_list;
15592   resolve_and_layout (exception, NULL_TREE);
15593   /* First, all the nested try-catch-finally at that stage. The
15594      last element contains `throws' clause exceptions, if any. */
15595   if (IS_UNCHECKED_EXCEPTION_P (exception))
15596     return 1;
15597   while (list)
15598     {
15599       tree caught;
15600       for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15601         if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15602           return 1;
15603       list = TREE_CHAIN (list);
15604     }
15605   return 0;
15606 }
15607
15608 static void
15609 purge_unchecked_exceptions (mdecl)
15610      tree mdecl;
15611 {
15612   tree throws = DECL_FUNCTION_THROWS (mdecl);
15613   tree new = NULL_TREE;
15614
15615   while (throws)
15616     {
15617       tree next = TREE_CHAIN (throws);
15618       if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15619         {
15620           TREE_CHAIN (throws) = new;
15621           new = throws;
15622         }
15623       throws = next;
15624     }
15625   /* List is inverted here, but it doesn't matter */
15626   DECL_FUNCTION_THROWS (mdecl) = new;
15627 }
15628
15629 /* This function goes over all of CLASS_TYPE ctors and checks whether
15630    each of them features at least one unchecked exception in its
15631    `throws' clause. If it's the case, it returns `true', `false'
15632    otherwise.  */
15633
15634 static bool
15635 ctors_unchecked_throws_clause_p (class_type)
15636      tree class_type;
15637 {
15638   tree current;
15639
15640   for (current = TYPE_METHODS (class_type); current;
15641        current = TREE_CHAIN (current))
15642     {
15643       bool ctu = false; /* Ctor Throws Unchecked */
15644       if (DECL_CONSTRUCTOR_P (current))
15645         {
15646           tree throws;
15647           for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
15648                throws = TREE_CHAIN (throws))
15649             if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
15650               ctu = true;
15651         }
15652       /* We return false as we found one ctor that is unfit. */
15653       if (!ctu && DECL_CONSTRUCTOR_P (current))
15654         return false;
15655     }
15656   /* All ctors feature at least one unchecked exception in their
15657      `throws' clause. */
15658   return true;
15659 }
15660
15661 /* 15.24 Conditional Operator ?: */
15662
15663 static tree
15664 patch_conditional_expr (node, wfl_cond, wfl_op1)
15665      tree node, wfl_cond, wfl_op1;
15666 {
15667   tree cond = TREE_OPERAND (node, 0);
15668   tree op1 = TREE_OPERAND (node, 1);
15669   tree op2 = TREE_OPERAND (node, 2);
15670   tree resulting_type = NULL_TREE;
15671   tree t1, t2, patched;
15672   int error_found = 0;
15673
15674   /* Operands of ?: might be StringBuffers crafted as a result of a
15675      string concatenation. Obtain a descent operand here.  */
15676   if ((patched = patch_string (op1)))
15677     TREE_OPERAND (node, 1) = op1 = patched;
15678   if ((patched = patch_string (op2)))
15679     TREE_OPERAND (node, 2) = op2 = patched;
15680
15681   t1 = TREE_TYPE (op1);
15682   t2 = TREE_TYPE (op2);
15683
15684   /* The first expression must be a boolean */
15685   if (TREE_TYPE (cond) != boolean_type_node)
15686     {
15687       SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15688       parse_error_context (wfl_operator,
15689                "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
15690                            lang_printable_name (TREE_TYPE (cond), 0));
15691       error_found = 1;
15692     }
15693
15694   /* Second and third can be numeric, boolean (i.e. primitive),
15695      references or null. Anything else results in an error */
15696   if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15697         || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node) 
15698             && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15699         || (t1 == boolean_type_node && t2 == boolean_type_node)))
15700     error_found = 1;
15701
15702   /* Determine the type of the conditional expression. Same types are
15703      easy to deal with */
15704   else if (t1 == t2)
15705     resulting_type = t1;
15706
15707   /* There are different rules for numeric types */
15708   else if (JNUMERIC_TYPE_P (t1))
15709     {
15710       /* if byte/short found, the resulting type is short */
15711       if ((t1 == byte_type_node && t2 == short_type_node)
15712           || (t1 == short_type_node && t2 == byte_type_node))
15713         resulting_type = short_type_node;
15714
15715       /* If t1 is a constant int and t2 is of type byte, short or char
15716          and t1's value fits in t2, then the resulting type is t2 */
15717       else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15718           && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15719         resulting_type = t2;
15720
15721       /* If t2 is a constant int and t1 is of type byte, short or char
15722          and t2's value fits in t1, then the resulting type is t1 */
15723       else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15724           && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15725         resulting_type = t1;
15726
15727       /* Otherwise, binary numeric promotion is applied and the
15728          resulting type is the promoted type of operand 1 and 2 */
15729       else 
15730         resulting_type = binary_numeric_promotion (t1, t2, 
15731                                                    &TREE_OPERAND (node, 1), 
15732                                                    &TREE_OPERAND (node, 2));
15733     }
15734
15735   /* Cases of a reference and a null type */
15736   else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15737     resulting_type = t1;
15738
15739   else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15740     resulting_type = t2;
15741
15742   /* Last case: different reference types. If a type can be converted
15743      into the other one by assignment conversion, the latter
15744      determines the type of the expression */
15745   else if ((resulting_type = try_reference_assignconv (t1, op2)))
15746     resulting_type = promote_type (t1);
15747
15748   else if ((resulting_type = try_reference_assignconv (t2, op1)))
15749     resulting_type = promote_type (t2);
15750
15751   /* If we don't have any resulting type, we're in trouble */
15752   if (!resulting_type)
15753     {
15754       char *t = xstrdup (lang_printable_name (t1, 0));
15755       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15756       parse_error_context (wfl_operator,
15757                  "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15758                            t, lang_printable_name (t2, 0));
15759       free (t);
15760       error_found = 1;
15761     }
15762
15763   if (error_found)
15764     {
15765       TREE_TYPE (node) = error_mark_node;
15766       return error_mark_node;
15767     }
15768
15769   TREE_TYPE (node) = resulting_type;
15770   TREE_SET_CODE (node, COND_EXPR);
15771   CAN_COMPLETE_NORMALLY (node) = 1;
15772   return node;
15773 }
15774
15775 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
15776
15777 static tree
15778 maybe_build_class_init_for_field (decl, expr)
15779     tree decl, expr;
15780 {
15781   tree clas = DECL_CONTEXT (decl);
15782   if (flag_emit_class_files || flag_emit_xref)
15783     return expr;
15784
15785   if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
15786       && FIELD_FINAL (decl))
15787     {
15788       tree init = DECL_INITIAL (decl);
15789       if (init != NULL_TREE)
15790         init = fold_constant_for_init (init, decl);
15791       if (init != NULL_TREE && CONSTANT_VALUE_P (init))
15792         return expr;
15793     }
15794
15795   return build_class_init (clas, expr);
15796 }
15797
15798 /* Try to constant fold NODE.
15799    If NODE is not a constant expression, return NULL_EXPR.
15800    CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15801
15802 static tree
15803 fold_constant_for_init (node, context)
15804      tree node;
15805      tree context;
15806 {
15807   tree op0, op1, val;
15808   enum tree_code code = TREE_CODE (node);
15809
15810   switch (code)
15811     {
15812     case STRING_CST:
15813     case INTEGER_CST:
15814     case REAL_CST:
15815       return node;
15816
15817     case PLUS_EXPR:
15818     case MINUS_EXPR:
15819     case MULT_EXPR:
15820     case TRUNC_MOD_EXPR:
15821     case RDIV_EXPR:
15822     case LSHIFT_EXPR:
15823     case RSHIFT_EXPR:
15824     case URSHIFT_EXPR:
15825     case BIT_AND_EXPR:
15826     case BIT_XOR_EXPR:
15827     case BIT_IOR_EXPR:
15828     case TRUTH_ANDIF_EXPR:
15829     case TRUTH_ORIF_EXPR:
15830     case EQ_EXPR: 
15831     case NE_EXPR:
15832     case GT_EXPR:
15833     case GE_EXPR:
15834     case LT_EXPR:
15835     case LE_EXPR:
15836       op0 = TREE_OPERAND (node, 0);
15837       op1 = TREE_OPERAND (node, 1);
15838       val = fold_constant_for_init (op0, context);
15839       if (val == NULL_TREE || ! TREE_CONSTANT (val))
15840         return NULL_TREE;
15841       TREE_OPERAND (node, 0) = val;
15842       val = fold_constant_for_init (op1, context);
15843       if (val == NULL_TREE || ! TREE_CONSTANT (val))
15844         return NULL_TREE;
15845       TREE_OPERAND (node, 1) = val;
15846       return patch_binop (node, op0, op1);
15847
15848     case UNARY_PLUS_EXPR:
15849     case NEGATE_EXPR:
15850     case TRUTH_NOT_EXPR:
15851     case BIT_NOT_EXPR:
15852     case CONVERT_EXPR:
15853       op0 = TREE_OPERAND (node, 0);
15854       val = fold_constant_for_init (op0, context);
15855       if (val == NULL_TREE || ! TREE_CONSTANT (val))
15856         return NULL_TREE;
15857       TREE_OPERAND (node, 0) = val;
15858       return patch_unaryop (node, op0);
15859       break;
15860
15861     case COND_EXPR:
15862       val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15863       if (val == NULL_TREE || ! TREE_CONSTANT (val))
15864         return NULL_TREE;
15865       TREE_OPERAND (node, 0) = val;
15866       val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15867       if (val == NULL_TREE || ! TREE_CONSTANT (val))
15868         return NULL_TREE;
15869       TREE_OPERAND (node, 1) = val;
15870       val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15871       if (val == NULL_TREE || ! TREE_CONSTANT (val))
15872         return NULL_TREE;
15873       TREE_OPERAND (node, 2) = val;
15874       return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15875         : TREE_OPERAND (node, 2);
15876
15877     case VAR_DECL:
15878     case FIELD_DECL:
15879       if (! FIELD_FINAL (node)
15880           || DECL_INITIAL (node) == NULL_TREE)
15881         return NULL_TREE;
15882       val = DECL_INITIAL (node);
15883       /* Guard against infinite recursion. */
15884       DECL_INITIAL (node) = NULL_TREE;
15885       val = fold_constant_for_init (val, node);
15886       DECL_INITIAL (node) = val;
15887       return val;
15888
15889     case EXPR_WITH_FILE_LOCATION:
15890       /* Compare java_complete_tree and resolve_expression_name. */
15891       if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15892           || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15893         {
15894           tree name = EXPR_WFL_NODE (node);
15895           tree decl;
15896           if (PRIMARY_P (node))
15897             return NULL_TREE;
15898           else if (! QUALIFIED_P (name))
15899             {
15900               decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
15901               if (decl == NULL_TREE 
15902                   || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
15903                 return NULL_TREE;
15904               return fold_constant_for_init (decl, decl);
15905             }
15906           else
15907             {
15908               /* Install the proper context for the field resolution.
15909                  The prior context is restored once the name is
15910                  properly qualified. */
15911               tree saved_current_class = current_class;
15912               /* Wait until the USE_COMPONENT_REF re-write.  FIXME. */
15913               current_class = DECL_CONTEXT (context);
15914               qualify_ambiguous_name (node);
15915               current_class = saved_current_class;
15916               if (resolve_field_access (node, &decl, NULL)
15917                   && decl != NULL_TREE)
15918                 return fold_constant_for_init (decl, decl);
15919               return NULL_TREE;
15920             }
15921         }
15922       else
15923         {
15924           op0 = TREE_OPERAND (node, 0);
15925           val = fold_constant_for_init (op0, context);
15926           if (val == NULL_TREE || ! TREE_CONSTANT (val))
15927             return NULL_TREE;
15928           TREE_OPERAND (node, 0) = val;
15929           return val;
15930         }
15931
15932 #ifdef USE_COMPONENT_REF
15933     case IDENTIFIER:
15934     case COMPONENT_REF:
15935       ?;
15936 #endif
15937
15938     default:
15939       return NULL_TREE;
15940     }
15941 }
15942
15943 #ifdef USE_COMPONENT_REF
15944 /* Context is 'T' for TypeName, 'P' for PackageName,
15945    'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15946
15947 tree
15948 resolve_simple_name (name, context)
15949      tree name;
15950      int context;
15951 {
15952 }
15953
15954 tree
15955 resolve_qualified_name (name, context)
15956      tree name;
15957      int context;
15958 {
15959 }
15960 #endif
15961
15962 /* Mark P, which is really a `struct parser_ctxt **' for GC.  */
15963
15964 static void
15965 mark_parser_ctxt (p)
15966      void *p;
15967 {
15968   struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15969   int i;
15970
15971   if (!pc)
15972     return;
15973
15974 #ifndef JC1_LITE
15975   for (i = 0; i < 11; ++i)
15976     ggc_mark_tree (pc->modifier_ctx[i]);
15977   ggc_mark_tree (pc->class_type);
15978   ggc_mark_tree (pc->function_decl);
15979   ggc_mark_tree (pc->package);
15980   ggc_mark_tree (pc->class_list);
15981   ggc_mark_tree (pc->current_parsed_class);
15982   ggc_mark_tree (pc->current_parsed_class_un);
15983   ggc_mark_tree (pc->non_static_initialized);
15984   ggc_mark_tree (pc->static_initialized);
15985   ggc_mark_tree (pc->instance_initializers);
15986   ggc_mark_tree (pc->import_list);
15987   ggc_mark_tree (pc->import_demand_list);
15988   ggc_mark_tree (pc->current_loop);
15989   ggc_mark_tree (pc->current_labeled_block);
15990 #endif /* JC1_LITE */
15991
15992   if (pc->next)
15993     mark_parser_ctxt (&pc->next);
15994 }
15995
15996 void
15997 init_src_parse ()
15998 {
15999   /* Register roots with the garbage collector.  */
16000   ggc_add_tree_root (src_parse_roots, ARRAY_SIZE (src_parse_roots));
16001 }
16002
16003 \f
16004
16005 /* This section deals with the functions that are called when tables
16006    recording class initialization information are traversed.  */
16007
16008 /* Attach to PTR (a block) the declaration found in ENTRY. */
16009
16010 static bool
16011 attach_init_test_initialization_flags (entry, ptr)
16012      struct hash_entry *entry;
16013      PTR ptr;
16014 {
16015   tree block = (tree)ptr;
16016   struct init_test_hash_entry *ite = (struct init_test_hash_entry *) entry;
16017   
16018   TREE_CHAIN (ite->init_test_decl) = BLOCK_EXPR_DECLS (block);
16019   BLOCK_EXPR_DECLS (block) = ite->init_test_decl;
16020   return true;
16021 }
16022
16023 /* This function is called for each classes that is known definitely
16024    assigned when a given static method was called. This function
16025    augments a compound expression (INFO) storing all assignment to
16026    initialized static class flags if a flag already existed, otherwise
16027    a new one is created.  */
16028
16029 static bool
16030 emit_test_initialization (entry, info)
16031      struct hash_entry *entry;
16032      PTR info;
16033 {
16034   tree l = (tree) info;
16035   tree decl, init;
16036
16037   struct init_test_hash_entry *ite = (struct init_test_hash_entry *)
16038     hash_lookup (&DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl),
16039                  entry->key,
16040                  current_function_decl != TREE_PURPOSE (l), NULL);
16041
16042   /* If we haven't found a flag and we're dealing with self registered
16043      with current_function_decl, then don't do anything. Self is
16044      always added as definitely initialized but this information is
16045      valid only if used outside the current function. */
16046   if (! ite)
16047     return true;
16048
16049   /* If we don't have a variable, create one and install it. */
16050   if (! ite->init_test_decl)
16051     {
16052       tree block;
16053       
16054       decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16055       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16056       LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16057       DECL_CONTEXT (decl) = current_function_decl;
16058       DECL_INITIAL (decl) = boolean_true_node;
16059
16060       /* The trick is to find the right context for it. */
16061       block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16062       TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16063       BLOCK_EXPR_DECLS (block) = decl;
16064       ite->init_test_decl = decl;
16065     }
16066   else
16067     decl = ite->init_test_decl;
16068
16069   /* Now simply augment the compound that holds all the assignments
16070      pertaining to this method invocation. */
16071   init = build (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16072   TREE_SIDE_EFFECTS (init) = 1;
16073   TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16074   TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16075
16076   return true;
16077 }