OSDN Git Service

2002-07-30 Andrew Haley <aph@cambridge.redhat.com>
[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 "real.h"
55 #include "obstack.h"
56 #include "toplev.h"
57 #include "flags.h"
58 #include "java-tree.h"
59 #include "jcf.h"
60 #include "lex.h"
61 #include "parse.h"
62 #include "zipfile.h"
63 #include "convert.h"
64 #include "buffer.h"
65 #include "xref.h"
66 #include "function.h"
67 #include "except.h"
68 #include "ggc.h"
69 #include "debug.h"
70 #include "tree-inline.h"
71
72 #ifndef DIR_SEPARATOR
73 #define DIR_SEPARATOR '/'
74 #endif
75
76 /* Local function prototypes */
77 static char *java_accstring_lookup PARAMS ((int));
78 static void  classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
79 static void  variable_redefinition_error PARAMS ((tree, tree, tree, int));
80 static tree  create_class PARAMS ((int, tree, tree, tree));
81 static tree  create_interface PARAMS ((int, tree, tree));
82 static void  end_class_declaration PARAMS ((int));
83 static tree  find_field PARAMS ((tree, tree));
84 static tree lookup_field_wrapper PARAMS ((tree, tree));
85 static int   duplicate_declaration_error_p PARAMS ((tree, tree, tree));
86 static void  register_fields PARAMS ((int, tree, tree));
87 static tree parser_qualified_classname PARAMS ((tree));
88 static int  parser_check_super PARAMS ((tree, tree, tree));
89 static int  parser_check_super_interface PARAMS ((tree, tree, tree));
90 static void check_modifiers_consistency PARAMS ((int));
91 static tree lookup_cl PARAMS ((tree));
92 static tree lookup_java_method2 PARAMS ((tree, tree, int));
93 static tree method_header PARAMS ((int, tree, tree, tree));
94 static void fix_method_argument_names PARAMS ((tree ,tree));
95 static tree method_declarator PARAMS ((tree, tree));
96 static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
97   ATTRIBUTE_PRINTF_2;
98 static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list))
99   ATTRIBUTE_PRINTF (2, 0);
100 static void parse_ctor_invocation_error PARAMS ((void));
101 static tree parse_jdk1_1_error PARAMS ((const char *));
102 static void complete_class_report_errors PARAMS ((jdep *));
103 static int process_imports PARAMS ((void));
104 static void read_import_dir PARAMS ((tree));
105 static int find_in_imports_on_demand PARAMS ((tree, tree));
106 static void find_in_imports PARAMS ((tree, tree));
107 static void check_inner_class_access PARAMS ((tree, tree, tree));
108 static int check_pkg_class_access PARAMS ((tree, tree, bool));
109 static void register_package PARAMS ((tree));
110 static tree resolve_package PARAMS ((tree, tree *, tree *));
111 static tree resolve_class PARAMS ((tree, tree, tree, tree));
112 static void declare_local_variables PARAMS ((int, tree, tree));
113 static void dump_java_tree PARAMS ((enum tree_dump_index, tree));
114 static void source_start_java_method PARAMS ((tree));
115 static void source_end_java_method PARAMS ((void));
116 static tree find_name_in_single_imports PARAMS ((tree));
117 static void check_abstract_method_header PARAMS ((tree));
118 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
119 static tree resolve_expression_name PARAMS ((tree, tree *));
120 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
121 static int check_class_interface_creation PARAMS ((int, int, tree,
122                                                   tree, tree, tree));
123 static tree patch_method_invocation PARAMS ((tree, tree, tree, int,
124                                             int *, tree *));
125 static int breakdown_qualified PARAMS ((tree *, tree *, tree));
126 static int in_same_package PARAMS ((tree, tree));
127 static tree resolve_and_layout PARAMS ((tree, tree));
128 static tree qualify_and_find PARAMS ((tree, tree, tree));
129 static tree resolve_no_layout PARAMS ((tree, tree));
130 static int invocation_mode PARAMS ((tree, int));
131 static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
132                                                             tree, tree));
133 static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
134                                                    tree *, tree *));
135 static tree find_most_specific_methods_list PARAMS ((tree));
136 static int argument_types_convertible PARAMS ((tree, tree));
137 static tree patch_invoke PARAMS ((tree, tree, tree));
138 static int maybe_use_access_method PARAMS ((int, tree *, tree *));
139 static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
140 static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
141 static tree check_inner_circular_reference PARAMS ((tree, tree));
142 static tree check_circular_reference PARAMS ((tree));
143 static tree obtain_incomplete_type PARAMS ((tree));
144 static tree java_complete_lhs PARAMS ((tree));
145 static tree java_complete_tree PARAMS ((tree));
146 static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
147 static int analyze_clinit_body PARAMS ((tree, tree));
148 static int maybe_yank_clinit PARAMS ((tree));
149 static void start_complete_expand_method PARAMS ((tree));
150 static void java_complete_expand_method PARAMS ((tree));
151 static void java_expand_method_bodies PARAMS ((tree));
152 static int  unresolved_type_p PARAMS ((tree, tree *));
153 static void create_jdep_list PARAMS ((struct parser_ctxt *));
154 static tree build_expr_block PARAMS ((tree, tree));
155 static tree enter_block PARAMS ((void));
156 static tree exit_block PARAMS ((void));
157 static tree lookup_name_in_blocks PARAMS ((tree));
158 static void maybe_absorb_scoping_blocks PARAMS ((void));
159 static tree build_method_invocation PARAMS ((tree, tree));
160 static tree build_new_invocation PARAMS ((tree, tree));
161 static tree build_assignment PARAMS ((int, int, tree, tree));
162 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
163 static tree patch_assignment PARAMS ((tree, tree));
164 static tree patch_binop PARAMS ((tree, tree, tree));
165 static tree build_unaryop PARAMS ((int, int, tree));
166 static tree build_incdec PARAMS ((int, int, tree, int));
167 static tree patch_unaryop PARAMS ((tree, tree));
168 static tree build_cast PARAMS ((int, tree, tree));
169 static tree build_null_of_type PARAMS ((tree));
170 static tree patch_cast PARAMS ((tree, tree));
171 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
172 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
173 static int valid_cast_to_p PARAMS ((tree, tree));
174 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
175 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
176 static tree try_reference_assignconv PARAMS ((tree, tree));
177 static tree build_unresolved_array_type PARAMS ((tree));
178 static int build_type_name_from_array_name PARAMS ((tree, tree *));
179 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
180 static tree build_array_ref PARAMS ((int, tree, tree));
181 static tree patch_array_ref PARAMS ((tree));
182 static tree make_qualified_name PARAMS ((tree, tree, int));
183 static tree merge_qualified_name PARAMS ((tree, tree));
184 static tree make_qualified_primary PARAMS ((tree, tree, int));
185 static int resolve_qualified_expression_name PARAMS ((tree, tree *,
186                                                      tree *, tree *));
187 static void qualify_ambiguous_name PARAMS ((tree));
188 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
189 static tree build_newarray_node PARAMS ((tree, tree, int));
190 static tree patch_newarray PARAMS ((tree));
191 static tree resolve_type_during_patch PARAMS ((tree));
192 static tree build_this PARAMS ((int));
193 static tree build_wfl_wrap PARAMS ((tree, int));
194 static tree build_return PARAMS ((int, tree));
195 static tree patch_return PARAMS ((tree));
196 static tree maybe_access_field PARAMS ((tree, tree, tree));
197 static int complete_function_arguments PARAMS ((tree));
198 static int check_for_static_method_reference PARAMS ((tree, tree, tree,
199                                                       tree, tree));
200 static int not_accessible_p PARAMS ((tree, tree, tree, int));
201 static void check_deprecation PARAMS ((tree, tree));
202 static int class_in_current_package PARAMS ((tree));
203 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
204 static tree patch_if_else_statement PARAMS ((tree));
205 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
206 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
207 static tree patch_exit_expr PARAMS ((tree));
208 static tree build_labeled_block PARAMS ((int, tree));
209 static tree finish_labeled_statement PARAMS ((tree, tree));
210 static tree build_bc_statement PARAMS ((int, int, tree));
211 static tree patch_bc_statement PARAMS ((tree));
212 static tree patch_loop_statement PARAMS ((tree));
213 static tree build_new_loop PARAMS ((tree));
214 static tree build_loop_body PARAMS ((int, tree, int));
215 static tree finish_loop_body PARAMS ((int, tree, tree, int));
216 static tree build_debugable_stmt PARAMS ((int, tree));
217 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
218 static tree patch_switch_statement PARAMS ((tree));
219 static tree string_constant_concatenation PARAMS ((tree, tree));
220 static tree build_string_concatenation PARAMS ((tree, tree));
221 static tree patch_string_cst PARAMS ((tree));
222 static tree patch_string PARAMS ((tree));
223 static tree encapsulate_with_try_catch PARAMS ((int, tree, tree, tree));
224 static tree build_assertion PARAMS ((int, tree, tree));
225 static tree build_try_statement PARAMS ((int, tree, tree));
226 static tree build_try_finally_statement PARAMS ((int, tree, tree));
227 static tree patch_try_statement PARAMS ((tree));
228 static tree patch_synchronized_statement PARAMS ((tree, tree));
229 static tree patch_throw_statement PARAMS ((tree, tree));
230 static void check_thrown_exceptions PARAMS ((int, tree));
231 static int check_thrown_exceptions_do PARAMS ((tree));
232 static void purge_unchecked_exceptions PARAMS ((tree));
233 static bool ctors_unchecked_throws_clause_p PARAMS ((tree));
234 static void check_throws_clauses PARAMS ((tree, tree, tree));
235 static void finish_method_declaration PARAMS ((tree));
236 static tree build_super_invocation PARAMS ((tree));
237 static int verify_constructor_circularity PARAMS ((tree, tree));
238 static char *constructor_circularity_msg PARAMS ((tree, tree));
239 static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
240                                                           int, int));
241 static const char *get_printable_method_name PARAMS ((tree));
242 static tree patch_conditional_expr PARAMS ((tree, tree, tree));
243 static tree generate_finit PARAMS ((tree));
244 static tree generate_instinit PARAMS ((tree));
245 static tree build_instinit_invocation PARAMS ((tree));
246 static void fix_constructors PARAMS ((tree));
247 static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
248                                                             tree, int *));
249 static tree craft_constructor PARAMS ((tree, tree));
250 static int verify_constructor_super PARAMS ((tree));
251 static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
252 static void start_artificial_method_body PARAMS ((tree));
253 static void end_artificial_method_body PARAMS ((tree));
254 static int check_method_redefinition PARAMS ((tree, tree));
255 static int check_method_types_complete PARAMS ((tree));
256 static void java_check_regular_methods PARAMS ((tree));
257 static void java_check_abstract_methods PARAMS ((tree));
258 static void unreachable_stmt_error PARAMS ((tree));
259 static tree find_expr_with_wfl PARAMS ((tree));
260 static void missing_return_error PARAMS ((tree));
261 static tree build_new_array_init PARAMS ((int, tree));
262 static tree patch_new_array_init PARAMS ((tree, tree));
263 static tree maybe_build_array_element_wfl PARAMS ((tree));
264 static int array_constructor_check_entry PARAMS ((tree, tree));
265 static const char *purify_type_name PARAMS ((const char *));
266 static tree fold_constant_for_init PARAMS ((tree, tree));
267 static tree strip_out_static_field_access_decl PARAMS ((tree));
268 static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
269 static void static_ref_err PARAMS ((tree, tree, tree));
270 static void parser_add_interface PARAMS ((tree, tree, tree));
271 static void add_superinterfaces PARAMS ((tree, tree));
272 static tree jdep_resolve_class PARAMS ((jdep *));
273 static int note_possible_classname PARAMS ((const char *, int));
274 static void java_complete_expand_classes PARAMS ((void));
275 static void java_complete_expand_class PARAMS ((tree));
276 static void java_complete_expand_methods PARAMS ((tree));
277 static tree cut_identifier_in_qualified PARAMS ((tree));
278 static tree java_stabilize_reference PARAMS ((tree));
279 static tree do_unary_numeric_promotion PARAMS ((tree));
280 static char * operator_string PARAMS ((tree));
281 static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
282 static tree merge_string_cste PARAMS ((tree, tree, int));
283 static tree java_refold PARAMS ((tree));
284 static int java_decl_equiv PARAMS ((tree, tree));
285 static int binop_compound_p PARAMS ((enum tree_code));
286 static tree search_loop PARAMS ((tree));
287 static int labeled_block_contains_loop_p PARAMS ((tree, tree));
288 static int check_abstract_method_definitions PARAMS ((int, tree, tree));
289 static void java_check_abstract_method_definitions PARAMS ((tree));
290 static void java_debug_context_do PARAMS ((int));
291 static void java_parser_context_push_initialized_field PARAMS ((void));
292 static void java_parser_context_pop_initialized_field PARAMS ((void));
293 static tree reorder_static_initialized PARAMS ((tree));
294 static void java_parser_context_suspend PARAMS ((void));
295 static void java_parser_context_resume PARAMS ((void));
296 static int pop_current_osb PARAMS ((struct parser_ctxt *));
297
298 /* JDK 1.1 work. FIXME */
299
300 static tree maybe_make_nested_class_name PARAMS ((tree));
301 static int make_nested_class_name PARAMS ((tree));
302 static void set_nested_class_simple_name_value PARAMS ((tree, int));
303 static void link_nested_class_to_enclosing PARAMS ((void));
304 static tree resolve_inner_class PARAMS ((htab_t, tree, tree *, tree *, tree));
305 static tree find_as_inner_class PARAMS ((tree, tree, tree));
306 static tree find_as_inner_class_do PARAMS ((tree, tree));
307 static int check_inner_class_redefinition PARAMS ((tree, tree));
308
309 static tree build_thisn_assign PARAMS ((void));
310 static tree build_current_thisn PARAMS ((tree));
311 static tree build_access_to_thisn PARAMS ((tree, tree, int));
312 static tree maybe_build_thisn_access_method PARAMS ((tree));
313
314 static tree build_outer_field_access PARAMS ((tree, tree));
315 static tree build_outer_field_access_methods PARAMS ((tree));
316 static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
317                                                   tree, tree));
318 static tree build_outer_method_access_method PARAMS ((tree));
319 static tree build_new_access_id PARAMS ((void));
320 static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
321                                                     tree, tree));
322
323 static int outer_field_access_p PARAMS ((tree, tree));
324 static int outer_field_expanded_access_p PARAMS ((tree, tree *,
325                                                  tree *, tree *));
326 static tree outer_field_access_fix PARAMS ((tree, tree, tree));
327 static tree build_incomplete_class_ref PARAMS ((int, tree));
328 static tree patch_incomplete_class_ref PARAMS ((tree));
329 static tree create_anonymous_class PARAMS ((int, tree));
330 static void patch_anonymous_class PARAMS ((tree, tree, tree));
331 static void add_inner_class_fields PARAMS ((tree, tree));
332
333 static tree build_dot_class_method PARAMS ((tree));
334 static tree build_dot_class_method_invocation PARAMS ((tree));
335 static void create_new_parser_context PARAMS ((int));
336 static void mark_parser_ctxt PARAMS ((void *));
337 static tree maybe_build_class_init_for_field PARAMS ((tree, tree));
338
339 static int attach_init_test_initialization_flags PARAMS ((PTR *, PTR));
340 static int emit_test_initialization PARAMS ((PTR *, PTR));
341
342 static char *string_convert_int_cst PARAMS ((tree));
343
344 /* Number of error found so far. */
345 int java_error_count;
346 /* Number of warning found so far. */
347 int java_warning_count;
348 /* Tell when not to fold, when doing xrefs */
349 int do_not_fold;
350 /* Cyclic inheritance report, as it can be set by layout_class */
351 const char *cyclic_inheritance_report;
352
353 /* The current parser context */
354 struct parser_ctxt *ctxp;
355
356 /* List of things that were analyzed for which code will be generated */
357 struct parser_ctxt *ctxp_for_generation = NULL;
358
359 /* binop_lookup maps token to tree_code. It is used where binary
360    operations are involved and required by the parser. RDIV_EXPR
361    covers both integral/floating point division. The code is changed
362    once the type of both operator is worked out.  */
363
364 static const enum tree_code binop_lookup[19] =
365   {
366     PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
367     LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
368     BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
369     TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
370     EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
371    };
372 #define BINOP_LOOKUP(VALUE)                                             \
373   binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
374
375 /* This is the end index for binary operators that can also be used
376    in compound assignements. */
377 #define BINOP_COMPOUND_CANDIDATES 11
378
379 /* The "$L" identifier we use to create labels.  */
380 static GTY(()) tree label_id;
381
382 /* The "StringBuffer" identifier used for the String `+' operator. */
383 static GTY(()) tree wfl_string_buffer;
384
385 /* The "append" identifier used for String `+' operator.  */
386 static GTY(()) tree wfl_append;
387
388 /* The "toString" identifier used for String `+' operator. */
389 static GTY(()) tree wfl_to_string;
390
391 /* The "java.lang" import qualified name.  */
392 static GTY(()) tree java_lang_id;
393
394 /* The generated `inst$' identifier used for generated enclosing
395    instance/field access functions.  */
396 static GTY(()) tree inst_id;
397
398 /* The "java.lang.Cloneable" qualified name.  */
399 static GTY(()) tree java_lang_cloneable;
400
401 /* The "java.io.Serializable" qualified name.  */
402 static GTY(()) tree java_io_serializable;
403
404 /* Context and flag for static blocks */
405 static GTY(()) tree current_static_block;
406
407 /* The generated `write_parm_value$' identifier.  */
408 static GTY(()) tree wpv_id;
409
410 /* The list of all packages we've seen so far */
411 static GTY(()) tree package_list;
412
413 /* Hold THIS for the scope of the current method decl.  */
414 static GTY(()) tree current_this;
415
416 /* Hold a list of catch clauses list. The first element of this list is
417    the list of the catch clauses of the currently analysed try block. */
418 static GTY(()) tree currently_caught_type_list;
419
420 /* This holds a linked list of all the case labels for the current
421    switch statement.  It is only used when checking to see if there
422    are duplicate labels.  FIXME: probably this should just be attached
423    to the switch itself; then it could be referenced via
424    `ctxp->current_loop'.  */
425 static GTY(()) tree case_label_list;
426
427 /* Anonymous class counter. Will be reset to 1 every time a non
428    anonymous class gets created. */
429 static int anonymous_class_counter = 1;
430
431 static GTY(()) tree src_parse_roots[1];
432
433 /* All classes seen from source code */
434 #define gclass_list src_parse_roots[0]
435
436 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
437    line and point it out.  */
438 /* Should point out the one that don't fit. ASCII/unicode, going
439    backward. FIXME */
440
441 #define check_modifiers(__message, __value, __mask) do {        \
442   if ((__value) & ~(__mask))                                    \
443     {                                                           \
444       size_t i, remainder = (__value) & ~(__mask);              \
445       for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)     \
446         if ((1 << i) & remainder)                               \
447           parse_error_context (ctxp->modifier_ctx [i], (__message), \
448                                java_accstring_lookup (1 << i)); \
449     }                                                           \
450 } while (0)
451
452 %}
453
454 %union {
455   tree node;
456   int sub_token;
457   struct {
458     int token;
459     int location;
460   } operator;
461   int value;
462 }
463
464 %{
465 #include "lex.c"
466 %}
467
468 %pure_parser
469
470 /* Things defined here have to match the order of what's in the
471    binop_lookup table.  */
472
473 %token   PLUS_TK         MINUS_TK        MULT_TK         DIV_TK    REM_TK
474 %token   LS_TK           SRS_TK          ZRS_TK
475 %token   AND_TK          XOR_TK          OR_TK
476 %token   BOOL_AND_TK BOOL_OR_TK
477 %token   EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
478
479 /* This maps to the same binop_lookup entry than the token above */
480
481 %token   PLUS_ASSIGN_TK  MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
482 %token   REM_ASSIGN_TK
483 %token   LS_ASSIGN_TK    SRS_ASSIGN_TK   ZRS_ASSIGN_TK
484 %token   AND_ASSIGN_TK   XOR_ASSIGN_TK   OR_ASSIGN_TK
485
486
487 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
488
489 %token   PUBLIC_TK       PRIVATE_TK         PROTECTED_TK
490 %token   STATIC_TK       FINAL_TK           SYNCHRONIZED_TK
491 %token   VOLATILE_TK     TRANSIENT_TK       NATIVE_TK
492 %token   PAD_TK          ABSTRACT_TK        STRICT_TK
493 %token   MODIFIER_TK
494
495 /* Keep those two in order, too */
496 %token   DECR_TK INCR_TK
497
498 /* From now one, things can be in any order */
499
500 %token   DEFAULT_TK      IF_TK              THROW_TK
501 %token   BOOLEAN_TK      DO_TK              IMPLEMENTS_TK
502 %token   THROWS_TK       BREAK_TK           IMPORT_TK
503 %token   ELSE_TK         INSTANCEOF_TK      RETURN_TK
504 %token   VOID_TK         CATCH_TK           INTERFACE_TK
505 %token   CASE_TK         EXTENDS_TK         FINALLY_TK
506 %token   SUPER_TK        WHILE_TK           CLASS_TK
507 %token   SWITCH_TK       CONST_TK           TRY_TK
508 %token   FOR_TK          NEW_TK             CONTINUE_TK
509 %token   GOTO_TK         PACKAGE_TK         THIS_TK
510 %token   ASSERT_TK
511
512 %token   BYTE_TK         SHORT_TK           INT_TK            LONG_TK
513 %token   CHAR_TK         INTEGRAL_TK
514
515 %token   FLOAT_TK        DOUBLE_TK          FP_TK
516
517 %token   ID_TK
518
519 %token   REL_QM_TK         REL_CL_TK NOT_TK  NEG_TK
520
521 %token   ASSIGN_ANY_TK   ASSIGN_TK
522 %token   OP_TK  CP_TK  OCB_TK  CCB_TK  OSB_TK  CSB_TK  SC_TK  C_TK DOT_TK
523
524 %token   STRING_LIT_TK   CHAR_LIT_TK        INT_LIT_TK        FP_LIT_TK
525 %token   TRUE_TK         FALSE_TK           BOOL_LIT_TK       NULL_TK
526
527 %type    <value>        modifiers MODIFIER_TK final synchronized
528
529 %type    <node>         super ID_TK identifier
530 %type    <node>         name simple_name qualified_name
531 %type    <node>         type_declaration compilation_unit
532                         field_declaration method_declaration extends_interfaces
533                         interfaces interface_type_list
534                         import_declarations package_declaration
535                         type_declarations interface_body
536                         interface_member_declaration constant_declaration
537                         interface_member_declarations interface_type
538                         abstract_method_declaration
539 %type    <node>         class_body_declaration class_member_declaration
540                         static_initializer constructor_declaration block
541 %type    <node>         class_body_declarations constructor_header
542 %type    <node>         class_or_interface_type class_type class_type_list
543                         constructor_declarator explicit_constructor_invocation
544 %type    <node>         dim_expr dim_exprs this_or_super throws
545
546 %type    <node>         variable_declarator_id variable_declarator
547                         variable_declarators variable_initializer
548                         variable_initializers constructor_body
549                         array_initializer
550
551 %type    <node>         class_body block_end constructor_block_end
552 %type    <node>         statement statement_without_trailing_substatement
553                         labeled_statement if_then_statement label_decl
554                         if_then_else_statement while_statement for_statement
555                         statement_nsi labeled_statement_nsi do_statement
556                         if_then_else_statement_nsi while_statement_nsi
557                         for_statement_nsi statement_expression_list for_init
558                         for_update statement_expression expression_statement
559                         primary_no_new_array expression primary
560                         array_creation_expression array_type
561                         class_instance_creation_expression field_access
562                         method_invocation array_access something_dot_new
563                         argument_list postfix_expression while_expression
564                         post_increment_expression post_decrement_expression
565                         unary_expression_not_plus_minus unary_expression
566                         pre_increment_expression pre_decrement_expression
567                         cast_expression
568                         multiplicative_expression additive_expression
569                         shift_expression relational_expression
570                         equality_expression and_expression
571                         exclusive_or_expression inclusive_or_expression
572                         conditional_and_expression conditional_or_expression
573                         conditional_expression assignment_expression
574                         left_hand_side assignment for_header for_begin
575                         constant_expression do_statement_begin empty_statement
576                         switch_statement synchronized_statement throw_statement
577                         try_statement assert_statement
578                         switch_expression switch_block
579                         catches catch_clause catch_clause_parameter finally
580                         anonymous_class_creation trap_overflow_corner_case
581 %type    <node>         return_statement break_statement continue_statement
582
583 %type    <operator>     ASSIGN_TK      MULT_ASSIGN_TK  DIV_ASSIGN_TK
584 %type    <operator>     REM_ASSIGN_TK  PLUS_ASSIGN_TK  MINUS_ASSIGN_TK
585 %type    <operator>     LS_ASSIGN_TK   SRS_ASSIGN_TK   ZRS_ASSIGN_TK
586 %type    <operator>     AND_ASSIGN_TK  XOR_ASSIGN_TK   OR_ASSIGN_TK
587 %type    <operator>     ASSIGN_ANY_TK  assignment_operator
588 %token   <operator>     EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
589 %token   <operator>     BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
590 %token   <operator>     DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
591 %token   <operator>     NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
592 %token   <operator>     OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
593 %type    <operator>     THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
594 %type    <operator>     CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
595 %type    <operator>     NEW_TK ASSERT_TK
596
597 %type    <node>         method_body
598
599 %type    <node>         literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
600                         STRING_LIT_TK NULL_TK VOID_TK
601
602 %type    <node>         IF_TK WHILE_TK FOR_TK
603
604 %type    <node>         formal_parameter_list formal_parameter
605                         method_declarator method_header
606
607 %type    <node>         primitive_type reference_type type
608                         BOOLEAN_TK INTEGRAL_TK FP_TK
609
610 /* Added or modified JDK 1.1 rule types  */
611 %type    <node>         type_literals
612
613 %%
614 /* 19.2 Production from 2.3: The Syntactic Grammar  */
615 goal:
616                 {
617                   /* Register static variables with the garbage
618                      collector.  */
619                   ggc_add_root (&ctxp, 1,
620                                 sizeof (struct parser_ctxt *),
621                                 mark_parser_ctxt);
622                   ggc_add_root (&ctxp_for_generation, 1,
623                                 sizeof (struct parser_ctxt *),
624                                 mark_parser_ctxt);
625                 }
626         compilation_unit
627                 {}
628 ;
629
630 /* 19.3 Productions from 3: Lexical structure  */
631 literal:
632         INT_LIT_TK
633 |       FP_LIT_TK
634 |       BOOL_LIT_TK
635 |       CHAR_LIT_TK
636 |       STRING_LIT_TK
637 |       NULL_TK
638 ;
639
640 /* 19.4 Productions from 4: Types, Values and Variables  */
641 type:
642         primitive_type
643 |       reference_type
644 ;
645
646 primitive_type:
647         INTEGRAL_TK
648 |       FP_TK
649 |       BOOLEAN_TK
650 ;
651
652 reference_type:
653         class_or_interface_type
654 |       array_type
655 ;
656
657 class_or_interface_type:
658         name
659 ;
660
661 class_type:
662         class_or_interface_type /* Default rule */
663 ;
664
665 interface_type:
666          class_or_interface_type
667 ;
668
669 array_type:
670         primitive_type dims
671                 {
672                   int osb = pop_current_osb (ctxp);
673                   tree t = build_java_array_type (($1), -1);
674                   while (--osb)
675                     t = build_unresolved_array_type (t);
676                   $$ = t;
677                 }
678 |       name dims
679                 {
680                   int osb = pop_current_osb (ctxp);
681                   tree t = $1;
682                   while (osb--)
683                     t = build_unresolved_array_type (t);
684                   $$ = t;
685                 }
686 ;
687
688 /* 19.5 Productions from 6: Names  */
689 name:
690         simple_name             /* Default rule */
691 |       qualified_name          /* Default rule */
692 ;
693
694 simple_name:
695         identifier              /* Default rule */
696 ;
697
698 qualified_name:
699         name DOT_TK identifier
700                 { $$ = make_qualified_name ($1, $3, $2.location); }
701 ;
702
703 identifier:
704         ID_TK
705 ;
706
707 /* 19.6: Production from 7: Packages  */
708 compilation_unit:
709                 {$$ = NULL;}
710 |       package_declaration
711 |       import_declarations
712 |       type_declarations
713 |       package_declaration import_declarations
714 |       package_declaration type_declarations
715 |       import_declarations type_declarations
716 |       package_declaration import_declarations type_declarations
717 ;
718
719 import_declarations:
720         import_declaration
721                 {
722                   $$ = NULL;
723                 }
724 |       import_declarations import_declaration
725                 {
726                   $$ = NULL;
727                 }
728 ;
729
730 type_declarations:
731         type_declaration
732 |       type_declarations type_declaration
733 ;
734
735 package_declaration:
736         PACKAGE_TK name SC_TK
737                 {
738                   ctxp->package = EXPR_WFL_NODE ($2);
739                   register_package (ctxp->package);
740                 }
741 |       PACKAGE_TK error
742                 {yyerror ("Missing name"); RECOVER;}
743 |       PACKAGE_TK name error
744                 {yyerror ("';' expected"); RECOVER;}
745 ;
746
747 import_declaration:
748         single_type_import_declaration
749 |       type_import_on_demand_declaration
750 ;
751
752 single_type_import_declaration:
753         IMPORT_TK name SC_TK
754                 {
755                   tree name = EXPR_WFL_NODE ($2), last_name;
756                   int   i = IDENTIFIER_LENGTH (name)-1;
757                   const char *last = &IDENTIFIER_POINTER (name)[i];
758                   while (last != IDENTIFIER_POINTER (name))
759                     {
760                       if (last [0] == '.')
761                         break;
762                       last--;
763                     }
764                   last_name = get_identifier (++last);
765                   if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
766                     {
767                       tree err = find_name_in_single_imports (last_name);
768                       if (err && err != name)
769                         parse_error_context
770                           ($2, "Ambiguous class: `%s' and `%s'",
771                            IDENTIFIER_POINTER (name),
772                            IDENTIFIER_POINTER (err));
773                       else
774                         REGISTER_IMPORT ($2, last_name);
775                     }
776                   else
777                     REGISTER_IMPORT ($2, last_name);
778                 }
779 |       IMPORT_TK error
780                 {yyerror ("Missing name"); RECOVER;}
781 |       IMPORT_TK name error
782                 {yyerror ("';' expected"); RECOVER;}
783 ;
784
785 type_import_on_demand_declaration:
786         IMPORT_TK name DOT_TK MULT_TK SC_TK
787                 {
788                   tree name = EXPR_WFL_NODE ($2);
789                   tree it;
790                   /* Search for duplicates. */
791                   for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
792                     if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
793                       break;
794                   /* Don't import the same thing more than once, just ignore
795                      duplicates (7.5.2) */
796                   if (! it)
797                     {
798                       read_import_dir ($2);
799                       ctxp->import_demand_list =
800                         chainon (ctxp->import_demand_list,
801                                  build_tree_list ($2, NULL_TREE));
802                     }
803                 }
804 |       IMPORT_TK name DOT_TK error
805                 {yyerror ("'*' expected"); RECOVER;}
806 |       IMPORT_TK name DOT_TK MULT_TK error
807                 {yyerror ("';' expected"); RECOVER;}
808 ;
809
810 type_declaration:
811         class_declaration
812                 { end_class_declaration (0); }
813 |       interface_declaration
814                 { end_class_declaration (0); }
815 |       empty_statement
816 |       error
817                 {
818                   YYERROR_NOW;
819                   yyerror ("Class or interface declaration expected");
820                 }
821 ;
822
823 /* 19.7 Shortened from the original:
824    modifiers: modifier | modifiers modifier
825    modifier: any of public...  */
826 modifiers:
827         MODIFIER_TK
828                 {
829                   $$ = (1 << $1);
830                 }
831 |       modifiers MODIFIER_TK
832                 {
833                   int acc = (1 << $2);
834                   if ($$ & acc)
835                     parse_error_context
836                       (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
837                        java_accstring_lookup (acc));
838                   else
839                     {
840                       $$ |= acc;
841                     }
842                 }
843 ;
844
845 /* 19.8.1 Production from $8.1: Class Declaration */
846 class_declaration:
847         modifiers CLASS_TK identifier super interfaces
848                 { create_class ($1, $3, $4, $5); }
849         class_body
850                 {;}
851 |       CLASS_TK identifier super interfaces
852                 { create_class (0, $2, $3, $4); }
853         class_body
854                 {;}
855 |       modifiers CLASS_TK error
856                 { yyerror ("Missing class name"); RECOVER; }
857 |       CLASS_TK error
858                 { yyerror ("Missing class name"); RECOVER; }
859 |       CLASS_TK identifier error
860                 {
861                   if (!ctxp->class_err) yyerror ("'{' expected");
862                   DRECOVER(class1);
863                 }
864 |       modifiers CLASS_TK identifier error
865                 { if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER; }
866 ;
867
868 super:
869                 { $$ = NULL; }
870 |       EXTENDS_TK class_type
871                 { $$ = $2; }
872 |       EXTENDS_TK class_type error
873                 {yyerror ("'{' expected"); ctxp->class_err=1;}
874 |       EXTENDS_TK error
875                 {yyerror ("Missing super class name"); ctxp->class_err=1;}
876 ;
877
878 interfaces:
879                 { $$ = NULL_TREE; }
880 |       IMPLEMENTS_TK interface_type_list
881                 { $$ = $2; }
882 |       IMPLEMENTS_TK error
883                 {
884                   ctxp->class_err=1;
885                   yyerror ("Missing interface name");
886                 }
887 ;
888
889 interface_type_list:
890         interface_type
891                 {
892                   ctxp->interface_number = 1;
893                   $$ = build_tree_list ($1, NULL_TREE);
894                 }
895 |       interface_type_list C_TK interface_type
896                 {
897                   ctxp->interface_number++;
898                   $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
899                 }
900 |       interface_type_list C_TK error
901                 {yyerror ("Missing interface name"); RECOVER;}
902 ;
903
904 class_body:
905         OCB_TK CCB_TK
906                 {
907                   /* Store the location of the `}' when doing xrefs */
908                   if (flag_emit_xref)
909                     DECL_END_SOURCE_LINE (GET_CPC ()) =
910                       EXPR_WFL_ADD_COL ($2.location, 1);
911                   $$ = GET_CPC ();
912                 }
913 |       OCB_TK class_body_declarations CCB_TK
914                 {
915                   /* Store the location of the `}' when doing xrefs */
916                   if (flag_emit_xref)
917                     DECL_END_SOURCE_LINE (GET_CPC ()) =
918                       EXPR_WFL_ADD_COL ($3.location, 1);
919                   $$ = GET_CPC ();
920                 }
921 ;
922
923 class_body_declarations:
924         class_body_declaration
925 |       class_body_declarations class_body_declaration
926 ;
927
928 class_body_declaration:
929         class_member_declaration
930 |       static_initializer
931 |       constructor_declaration
932 |       block                   /* Added, JDK1.1, instance initializer */
933                 {
934                   if ($1 != empty_stmt_node)
935                     {
936                       TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
937                       SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
938                     }
939                 }
940 ;
941
942 class_member_declaration:
943         field_declaration
944 |       method_declaration
945 |       class_declaration       /* Added, JDK1.1 inner classes */
946                 { end_class_declaration (1); }
947 |       interface_declaration   /* Added, JDK1.1 inner interfaces */
948                 { end_class_declaration (1); }
949 |       empty_statement
950 ;
951
952 /* 19.8.2 Productions from 8.3: Field Declarations  */
953 field_declaration:
954         type variable_declarators SC_TK
955                 { register_fields (0, $1, $2); }
956 |       modifiers type variable_declarators SC_TK
957                 {
958                   check_modifiers
959                     ("Illegal modifier `%s' for field declaration",
960                      $1, FIELD_MODIFIERS);
961                   check_modifiers_consistency ($1);
962                   register_fields ($1, $2, $3);
963                 }
964 ;
965
966 variable_declarators:
967         /* Should we use build_decl_list () instead ? FIXME */
968         variable_declarator     /* Default rule */
969 |       variable_declarators C_TK variable_declarator
970                 { $$ = chainon ($1, $3); }
971 |       variable_declarators C_TK error
972                 {yyerror ("Missing term"); RECOVER;}
973 ;
974
975 variable_declarator:
976         variable_declarator_id
977                 { $$ = build_tree_list ($1, NULL_TREE); }
978 |       variable_declarator_id ASSIGN_TK variable_initializer
979                 {
980                   if (java_error_count)
981                     $3 = NULL_TREE;
982                   $$ = build_tree_list
983                     ($1, build_assignment ($2.token, $2.location, $1, $3));
984                 }
985 |       variable_declarator_id ASSIGN_TK error
986                 {
987                   yyerror ("Missing variable initializer");
988                   $$ = build_tree_list ($1, NULL_TREE);
989                   RECOVER;
990                 }
991 |       variable_declarator_id ASSIGN_TK variable_initializer error
992                 {
993                   yyerror ("';' expected");
994                   $$ = build_tree_list ($1, NULL_TREE);
995                   RECOVER;
996                 }
997 ;
998
999 variable_declarator_id:
1000         identifier
1001 |       variable_declarator_id OSB_TK CSB_TK
1002                 { $$ = build_unresolved_array_type ($1); }
1003 |       identifier error
1004                 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
1005 |       variable_declarator_id OSB_TK error
1006                 {
1007                   tree node = java_lval.node;
1008                   if (node && (TREE_CODE (node) == INTEGER_CST
1009                                || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
1010                     yyerror ("Can't specify array dimension in a declaration");
1011                   else
1012                     yyerror ("']' expected");
1013                   DRECOVER(vdi);
1014                 }
1015 |       variable_declarator_id CSB_TK error
1016                 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1017 ;
1018
1019 variable_initializer:
1020         expression
1021 |       array_initializer
1022 ;
1023
1024 /* 19.8.3 Productions from 8.4: Method Declarations  */
1025 method_declaration:
1026         method_header
1027                 {
1028                   current_function_decl = $1;
1029                   if (current_function_decl
1030                       && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1031                     source_start_java_method (current_function_decl);
1032                   else
1033                     current_function_decl = NULL_TREE;
1034                 }
1035         method_body
1036                 { finish_method_declaration ($3); }
1037 |       method_header error
1038                 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1039 ;
1040
1041 method_header:
1042         type method_declarator throws
1043                 { $$ = method_header (0, $1, $2, $3); }
1044 |       VOID_TK method_declarator throws
1045                 { $$ = method_header (0, void_type_node, $2, $3); }
1046 |       modifiers type method_declarator throws
1047                 { $$ = method_header ($1, $2, $3, $4); }
1048 |       modifiers VOID_TK method_declarator throws
1049                 { $$ = method_header ($1, void_type_node, $3, $4); }
1050 |       type error
1051                 {
1052                   yyerror ("Invalid method declaration, method name required");
1053                   RECOVER;
1054                 }
1055 |       modifiers type error
1056                 {
1057                   yyerror ("Identifier expected");
1058                   RECOVER;
1059                 }
1060 |       VOID_TK error
1061                 {
1062                   yyerror ("Identifier expected");
1063                   RECOVER;
1064                 }
1065 |       modifiers VOID_TK error
1066                 {
1067                   yyerror ("Identifier expected");
1068                   RECOVER;
1069                 }
1070 |       modifiers error
1071                 {
1072                   yyerror ("Invalid method declaration, return type required");
1073                   RECOVER;
1074                 }
1075 ;
1076
1077 method_declarator:
1078         identifier OP_TK CP_TK
1079                 {
1080                   ctxp->formal_parameter_number = 0;
1081                   $$ = method_declarator ($1, NULL_TREE);
1082                 }
1083 |       identifier OP_TK formal_parameter_list CP_TK
1084                 { $$ = method_declarator ($1, $3); }
1085 |       method_declarator OSB_TK CSB_TK
1086                 {
1087                   EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1088                   TREE_PURPOSE ($1) =
1089                     build_unresolved_array_type (TREE_PURPOSE ($1));
1090                   parse_warning_context
1091                     (wfl_operator,
1092                      "Discouraged form of returned type specification");
1093                 }
1094 |       identifier OP_TK error
1095                 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1096 |       method_declarator OSB_TK error
1097                 {yyerror ("']' expected"); RECOVER;}
1098 ;
1099
1100 formal_parameter_list:
1101         formal_parameter
1102                 {
1103                   ctxp->formal_parameter_number = 1;
1104                 }
1105 |       formal_parameter_list C_TK formal_parameter
1106                 {
1107                   ctxp->formal_parameter_number += 1;
1108                   $$ = chainon ($1, $3);
1109                 }
1110 |       formal_parameter_list C_TK error
1111                 { yyerror ("Missing formal parameter term"); RECOVER; }
1112 ;
1113
1114 formal_parameter:
1115         type variable_declarator_id
1116                 {
1117                   $$ = build_tree_list ($2, $1);
1118                 }
1119 |       final type variable_declarator_id /* Added, JDK1.1 final parms */
1120                 {
1121                   $$ = build_tree_list ($3, $2);
1122                   ARG_FINAL_P ($$) = 1;
1123                 }
1124 |       type error
1125                 {
1126                   yyerror ("Missing identifier"); RECOVER;
1127                   $$ = NULL_TREE;
1128                 }
1129 |       final type error
1130                 {
1131                   yyerror ("Missing identifier"); RECOVER;
1132                   $$ = NULL_TREE;
1133                 }
1134 ;
1135
1136 final:
1137         modifiers
1138                 {
1139                   check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1140                                    $1, ACC_FINAL);
1141                   if ($1 != ACC_FINAL)
1142                     MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1143                 }
1144 ;
1145
1146 throws:
1147                 { $$ = NULL_TREE; }
1148 |       THROWS_TK class_type_list
1149                 { $$ = $2; }
1150 |       THROWS_TK error
1151                 {yyerror ("Missing class type term"); RECOVER;}
1152 ;
1153
1154 class_type_list:
1155         class_type
1156                 { $$ = build_tree_list ($1, $1); }
1157 |       class_type_list C_TK class_type
1158                 { $$ = tree_cons ($3, $3, $1); }
1159 |       class_type_list C_TK error
1160                 {yyerror ("Missing class type term"); RECOVER;}
1161 ;
1162
1163 method_body:
1164         block
1165 |       SC_TK { $$ = NULL_TREE; }
1166 ;
1167
1168 /* 19.8.4 Productions from 8.5: Static Initializers  */
1169 static_initializer:
1170         static block
1171                 {
1172                   TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1173                   SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1174                   current_static_block = NULL_TREE;
1175                 }
1176 ;
1177
1178 static:                         /* Test lval.sub_token here */
1179         modifiers
1180                 {
1181                   check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1182                   /* Can't have a static initializer in an innerclass */
1183                   if ($1 | ACC_STATIC &&
1184                       GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1185                     parse_error_context
1186                       (MODIFIER_WFL (STATIC_TK),
1187                        "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1188                        IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1189                   SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1190                 }
1191 ;
1192
1193 /* 19.8.5 Productions from 8.6: Constructor Declarations  */
1194 constructor_declaration:
1195         constructor_header
1196                 {
1197                   current_function_decl = $1;
1198                   source_start_java_method (current_function_decl);
1199                 }
1200         constructor_body
1201                 { finish_method_declaration ($3); }
1202 ;
1203
1204 constructor_header:
1205         constructor_declarator throws
1206                 { $$ = method_header (0, NULL_TREE, $1, $2); }
1207 |       modifiers constructor_declarator throws
1208                 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1209 ;
1210
1211 constructor_declarator:
1212         simple_name OP_TK CP_TK
1213                 {
1214                   ctxp->formal_parameter_number = 0;
1215                   $$ = method_declarator ($1, NULL_TREE);
1216                 }
1217 |       simple_name OP_TK formal_parameter_list CP_TK
1218                 { $$ = method_declarator ($1, $3); }
1219 ;
1220
1221 constructor_body:
1222         /* Unlike regular method, we always need a complete (empty)
1223            body so we can safely perform all the required code
1224            addition (super invocation and field initialization) */
1225         block_begin constructor_block_end
1226                 {
1227                   BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1228                   $$ = $2;
1229                 }
1230 |       block_begin explicit_constructor_invocation constructor_block_end
1231                 { $$ = $3; }
1232 |       block_begin block_statements constructor_block_end
1233                 { $$ = $3; }
1234 |       block_begin explicit_constructor_invocation block_statements constructor_block_end
1235                 { $$ = $4; }
1236 ;
1237
1238 constructor_block_end:
1239         block_end
1240 ;
1241
1242 /* Error recovery for that rule moved down expression_statement: rule.  */
1243 explicit_constructor_invocation:
1244         this_or_super OP_TK CP_TK SC_TK
1245                 {
1246                   $$ = build_method_invocation ($1, NULL_TREE);
1247                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1248                   $$ = java_method_add_stmt (current_function_decl, $$);
1249                 }
1250 |       this_or_super OP_TK argument_list CP_TK SC_TK
1251                 {
1252                   $$ = build_method_invocation ($1, $3);
1253                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1254                   $$ = java_method_add_stmt (current_function_decl, $$);
1255                 }
1256         /* Added, JDK1.1 inner classes. Modified because the rule
1257            'primary' couldn't work.  */
1258 |       name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1259                 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1260 |       name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1261                 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1262 ;
1263
1264 this_or_super:                  /* Added, simplifies error diagnostics */
1265         THIS_TK
1266                 {
1267                   tree wfl = build_wfl_node (this_identifier_node);
1268                   EXPR_WFL_LINECOL (wfl) = $1.location;
1269                   $$ = wfl;
1270                 }
1271 |       SUPER_TK
1272                 {
1273                   tree wfl = build_wfl_node (super_identifier_node);
1274                   EXPR_WFL_LINECOL (wfl) = $1.location;
1275                   $$ = wfl;
1276                 }
1277 ;
1278
1279 /* 19.9 Productions from 9: Interfaces  */
1280 /* 19.9.1 Productions from 9.1: Interfaces Declarations  */
1281 interface_declaration:
1282         INTERFACE_TK identifier
1283                 { create_interface (0, $2, NULL_TREE); }
1284         interface_body
1285                 { ; }
1286 |       modifiers INTERFACE_TK identifier
1287                 { create_interface ($1, $3, NULL_TREE); }
1288         interface_body
1289                 { ; }
1290 |       INTERFACE_TK identifier extends_interfaces
1291                 { create_interface (0, $2, $3); }
1292         interface_body
1293                 { ; }
1294 |       modifiers INTERFACE_TK identifier extends_interfaces
1295                 { create_interface ($1, $3, $4); }
1296         interface_body
1297                 { ; }
1298 |       INTERFACE_TK identifier error
1299                 { yyerror ("'{' expected"); RECOVER; }
1300 |       modifiers INTERFACE_TK identifier error
1301                 { yyerror ("'{' expected"); RECOVER; }
1302 ;
1303
1304 extends_interfaces:
1305         EXTENDS_TK interface_type
1306                 {
1307                   ctxp->interface_number = 1;
1308                   $$ = build_tree_list ($2, NULL_TREE);
1309                 }
1310 |       extends_interfaces C_TK interface_type
1311                 {
1312                   ctxp->interface_number++;
1313                   $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1314                 }
1315 |       EXTENDS_TK error
1316                 {yyerror ("Invalid interface type"); RECOVER;}
1317 |       extends_interfaces C_TK error
1318                 {yyerror ("Missing term"); RECOVER;}
1319 ;
1320
1321 interface_body:
1322         OCB_TK CCB_TK
1323                 { $$ = NULL_TREE; }
1324 |       OCB_TK interface_member_declarations CCB_TK
1325                 { $$ = NULL_TREE; }
1326 ;
1327
1328 interface_member_declarations:
1329         interface_member_declaration
1330 |       interface_member_declarations interface_member_declaration
1331 ;
1332
1333 interface_member_declaration:
1334         constant_declaration
1335 |       abstract_method_declaration
1336 |       class_declaration       /* Added, JDK1.1 inner classes */
1337                 { end_class_declaration (1); }
1338 |       interface_declaration   /* Added, JDK1.1 inner interfaces */
1339                 { end_class_declaration (1); }
1340 ;
1341
1342 constant_declaration:
1343         field_declaration
1344 ;
1345
1346 abstract_method_declaration:
1347         method_header SC_TK
1348                 {
1349                   check_abstract_method_header ($1);
1350                   current_function_decl = NULL_TREE; /* FIXME ? */
1351                 }
1352 |       method_header error
1353                 {yyerror ("';' expected"); RECOVER;}
1354 ;
1355
1356 /* 19.10 Productions from 10: Arrays  */
1357 array_initializer:
1358         OCB_TK CCB_TK
1359                 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1360 |       OCB_TK C_TK CCB_TK
1361                 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1362 |       OCB_TK variable_initializers CCB_TK
1363                 { $$ = build_new_array_init ($1.location, $2); }
1364 |       OCB_TK variable_initializers C_TK CCB_TK
1365                 { $$ = build_new_array_init ($1.location, $2); }
1366 ;
1367
1368 variable_initializers:
1369         variable_initializer
1370                 {
1371                   $$ = tree_cons (maybe_build_array_element_wfl ($1),
1372                                   $1, NULL_TREE);
1373                 }
1374 |       variable_initializers C_TK variable_initializer
1375                 {
1376                   $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1377                 }
1378 |       variable_initializers C_TK error
1379                 {yyerror ("Missing term"); RECOVER;}
1380 ;
1381
1382 /* 19.11 Production from 14: Blocks and Statements  */
1383 block:
1384         OCB_TK CCB_TK
1385                 {
1386                   /* Store the location of the `}' when doing xrefs */
1387                   if (current_function_decl && flag_emit_xref)
1388                     DECL_END_SOURCE_LINE (current_function_decl) =
1389                       EXPR_WFL_ADD_COL ($2.location, 1);
1390                   $$ = empty_stmt_node;
1391                 }
1392 |       block_begin block_statements block_end
1393                 { $$ = $3; }
1394 ;
1395
1396 block_begin:
1397         OCB_TK
1398                 { enter_block (); }
1399 ;
1400
1401 block_end:
1402         CCB_TK
1403                 {
1404                   maybe_absorb_scoping_blocks ();
1405                   /* Store the location of the `}' when doing xrefs */
1406                   if (current_function_decl && flag_emit_xref)
1407                     DECL_END_SOURCE_LINE (current_function_decl) =
1408                       EXPR_WFL_ADD_COL ($1.location, 1);
1409                   $$ = exit_block ();
1410                   if (!BLOCK_SUBBLOCKS ($$))
1411                     BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1412                 }
1413 ;
1414
1415 block_statements:
1416         block_statement
1417 |       block_statements block_statement
1418 ;
1419
1420 block_statement:
1421         local_variable_declaration_statement
1422 |       statement
1423                 { java_method_add_stmt (current_function_decl, $1); }
1424 |       class_declaration       /* Added, JDK1.1 local classes */
1425                 {
1426                   LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1427                   end_class_declaration (1);
1428                 }
1429 ;
1430
1431 local_variable_declaration_statement:
1432         local_variable_declaration SC_TK /* Can't catch missing ';' here */
1433 ;
1434
1435 local_variable_declaration:
1436         type variable_declarators
1437                 { declare_local_variables (0, $1, $2); }
1438 |       final type variable_declarators /* Added, JDK1.1 final locals */
1439                 { declare_local_variables ($1, $2, $3); }
1440 ;
1441
1442 statement:
1443         statement_without_trailing_substatement
1444 |       labeled_statement
1445 |       if_then_statement
1446 |       if_then_else_statement
1447 |       while_statement
1448 |       for_statement
1449                 { $$ = exit_block (); }
1450 ;
1451
1452 statement_nsi:
1453         statement_without_trailing_substatement
1454 |       labeled_statement_nsi
1455 |       if_then_else_statement_nsi
1456 |       while_statement_nsi
1457 |       for_statement_nsi
1458                 { $$ = exit_block (); }
1459 ;
1460
1461 statement_without_trailing_substatement:
1462         block
1463 |       empty_statement
1464 |       expression_statement
1465 |       switch_statement
1466 |       do_statement
1467 |       break_statement
1468 |       continue_statement
1469 |       return_statement
1470 |       synchronized_statement
1471 |       throw_statement
1472 |       try_statement
1473 |       assert_statement
1474 ;
1475
1476 empty_statement:
1477         SC_TK
1478                 {
1479                   if (flag_extraneous_semicolon
1480                       && ! current_static_block
1481                       && (! current_function_decl ||
1482                           /* Verify we're not in a inner class declaration */
1483                           (GET_CPC () != TYPE_NAME
1484                            (DECL_CONTEXT (current_function_decl)))))
1485
1486                     {
1487                       EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1488                       parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1489                     }
1490                   $$ = empty_stmt_node;
1491                 }
1492 ;
1493
1494 label_decl:
1495         identifier REL_CL_TK
1496                 {
1497                   $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1498                                             EXPR_WFL_NODE ($1));
1499                   pushlevel (2);
1500                   push_labeled_block ($$);
1501                   PUSH_LABELED_BLOCK ($$);
1502                 }
1503 ;
1504
1505 labeled_statement:
1506         label_decl statement
1507                 { $$ = finish_labeled_statement ($1, $2); }
1508 |       identifier error
1509                 {yyerror ("':' expected"); RECOVER;}
1510 ;
1511
1512 labeled_statement_nsi:
1513         label_decl statement_nsi
1514                 { $$ = finish_labeled_statement ($1, $2); }
1515 ;
1516
1517 /* We concentrate here a bunch of error handling rules that we couldn't write
1518    earlier, because expression_statement catches a missing ';'.  */
1519 expression_statement:
1520         statement_expression SC_TK
1521                 {
1522                   /* We have a statement. Generate a WFL around it so
1523                      we can debug it */
1524                   $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1525                   /* We know we have a statement, so set the debug
1526                      info to be eventually generate here. */
1527                   $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1528                 }
1529 |       error SC_TK
1530                 {
1531                   YYNOT_TWICE yyerror ("Invalid expression statement");
1532                   DRECOVER (expr_stmt);
1533                 }
1534 |       error OCB_TK
1535                 {
1536                   YYNOT_TWICE yyerror ("Invalid expression statement");
1537                   DRECOVER (expr_stmt);
1538                 }
1539 |       error CCB_TK
1540                 {
1541                   YYNOT_TWICE yyerror ("Invalid expression statement");
1542                   DRECOVER (expr_stmt);
1543                 }
1544 |       this_or_super OP_TK error
1545                 {yyerror ("')' expected"); RECOVER;}
1546 |       this_or_super OP_TK CP_TK error
1547                 {
1548                   parse_ctor_invocation_error ();
1549                   RECOVER;
1550                 }
1551 |       this_or_super OP_TK argument_list error
1552                 {yyerror ("')' expected"); RECOVER;}
1553 |       this_or_super OP_TK argument_list CP_TK error
1554                 {
1555                   parse_ctor_invocation_error ();
1556                   RECOVER;
1557                 }
1558 |       name DOT_TK SUPER_TK error
1559                 {yyerror ("'(' expected"); RECOVER;}
1560 |       name DOT_TK SUPER_TK OP_TK error
1561                 {yyerror ("')' expected"); RECOVER;}
1562 |       name DOT_TK SUPER_TK OP_TK argument_list error
1563                 {yyerror ("')' expected"); RECOVER;}
1564 |       name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1565                 {yyerror ("';' expected"); RECOVER;}
1566 |       name DOT_TK SUPER_TK OP_TK CP_TK error
1567                 {yyerror ("';' expected"); RECOVER;}
1568 ;
1569
1570 statement_expression:
1571         assignment
1572 |       pre_increment_expression
1573 |       pre_decrement_expression
1574 |       post_increment_expression
1575 |       post_decrement_expression
1576 |       method_invocation
1577 |       class_instance_creation_expression
1578 ;
1579
1580 if_then_statement:
1581         IF_TK OP_TK expression CP_TK statement
1582                 {
1583                   $$ = build_if_else_statement ($2.location, $3,
1584                                                 $5, NULL_TREE);
1585                 }
1586 |       IF_TK error
1587                 {yyerror ("'(' expected"); RECOVER;}
1588 |       IF_TK OP_TK error
1589                 {yyerror ("Missing term"); RECOVER;}
1590 |       IF_TK OP_TK expression error
1591                 {yyerror ("')' expected"); RECOVER;}
1592 ;
1593
1594 if_then_else_statement:
1595         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1596                 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1597 ;
1598
1599 if_then_else_statement_nsi:
1600         IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1601                 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1602 ;
1603
1604 switch_statement:
1605         switch_expression
1606                 {
1607                   enter_block ();
1608                 }
1609         switch_block
1610                 {
1611                   /* Make into "proper list" of COMPOUND_EXPRs.
1612                      I.e. make the last statement also have its own
1613                      COMPOUND_EXPR. */
1614                   maybe_absorb_scoping_blocks ();
1615                   TREE_OPERAND ($1, 1) = exit_block ();
1616                   $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1617                 }
1618 ;
1619
1620 switch_expression:
1621         SWITCH_TK OP_TK expression CP_TK
1622                 {
1623                   $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1624                   EXPR_WFL_LINECOL ($$) = $2.location;
1625                 }
1626 |       SWITCH_TK error
1627                 {yyerror ("'(' expected"); RECOVER;}
1628 |       SWITCH_TK OP_TK error
1629                 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1630 |       SWITCH_TK OP_TK expression CP_TK error
1631                 {yyerror ("'{' expected"); RECOVER;}
1632 ;
1633
1634 /* Default assignment is there to avoid type node on switch_block
1635    node. */
1636
1637 switch_block:
1638         OCB_TK CCB_TK
1639                 { $$ = NULL_TREE; }
1640 |       OCB_TK switch_labels CCB_TK
1641                 { $$ = NULL_TREE; }
1642 |       OCB_TK switch_block_statement_groups CCB_TK
1643                 { $$ = NULL_TREE; }
1644 |       OCB_TK switch_block_statement_groups switch_labels CCB_TK
1645                 { $$ = NULL_TREE; }
1646 ;
1647
1648 switch_block_statement_groups:
1649         switch_block_statement_group
1650 |       switch_block_statement_groups switch_block_statement_group
1651 ;
1652
1653 switch_block_statement_group:
1654         switch_labels block_statements
1655 ;
1656
1657 switch_labels:
1658         switch_label
1659 |       switch_labels switch_label
1660 ;
1661
1662 switch_label:
1663         CASE_TK constant_expression REL_CL_TK
1664                 {
1665                   tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1666                   EXPR_WFL_LINECOL (lab) = $1.location;
1667                   java_method_add_stmt (current_function_decl, lab);
1668                 }
1669 |       DEFAULT_TK REL_CL_TK
1670                 {
1671                   tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1672                   EXPR_WFL_LINECOL (lab) = $1.location;
1673                   java_method_add_stmt (current_function_decl, lab);
1674                 }
1675 |       CASE_TK error
1676                 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1677 |       CASE_TK constant_expression error
1678                 {yyerror ("':' expected"); RECOVER;}
1679 |       DEFAULT_TK error
1680                 {yyerror ("':' expected"); RECOVER;}
1681 ;
1682
1683 while_expression:
1684         WHILE_TK OP_TK expression CP_TK
1685                 {
1686                   tree body = build_loop_body ($2.location, $3, 0);
1687                   $$ = build_new_loop (body);
1688                 }
1689 ;
1690
1691 while_statement:
1692         while_expression statement
1693                 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1694 |       WHILE_TK error
1695                 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1696 |       WHILE_TK OP_TK error
1697                 {yyerror ("Missing term and ')' expected"); RECOVER;}
1698 |       WHILE_TK OP_TK expression error
1699                 {yyerror ("')' expected"); RECOVER;}
1700 ;
1701
1702 while_statement_nsi:
1703         while_expression statement_nsi
1704                 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1705 ;
1706
1707 do_statement_begin:
1708         DO_TK
1709                 {
1710                   tree body = build_loop_body (0, NULL_TREE, 1);
1711                   $$ = build_new_loop (body);
1712                 }
1713         /* Need error handing here. FIXME */
1714 ;
1715
1716 do_statement:
1717         do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1718                 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1719 ;
1720
1721 for_statement:
1722         for_begin SC_TK expression SC_TK for_update CP_TK statement
1723                 {
1724                   if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1725                     $3 = build_wfl_node ($3);
1726                   $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1727                 }
1728 |       for_begin SC_TK SC_TK for_update CP_TK statement
1729                 {
1730                   $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1731                   /* We have not condition, so we get rid of the EXIT_EXPR */
1732                   LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1733                     empty_stmt_node;
1734                 }
1735 |       for_begin SC_TK error
1736                 {yyerror ("Invalid control expression"); RECOVER;}
1737 |       for_begin SC_TK expression SC_TK error
1738                 {yyerror ("Invalid update expression"); RECOVER;}
1739 |       for_begin SC_TK SC_TK error
1740                 {yyerror ("Invalid update expression"); RECOVER;}
1741 ;
1742
1743 for_statement_nsi:
1744         for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1745                 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1746 |       for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1747                 {
1748                   $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1749                   /* We have not condition, so we get rid of the EXIT_EXPR */
1750                   LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1751                     empty_stmt_node;
1752                 }
1753 ;
1754
1755 for_header:
1756         FOR_TK OP_TK
1757                 {
1758                   /* This scope defined for local variable that may be
1759                      defined within the scope of the for loop */
1760                   enter_block ();
1761                 }
1762 |       FOR_TK error
1763                 {yyerror ("'(' expected"); DRECOVER(for_1);}
1764 |       FOR_TK OP_TK error
1765                 {yyerror ("Invalid init statement"); RECOVER;}
1766 ;
1767
1768 for_begin:
1769         for_header for_init
1770                 {
1771                   /* We now declare the loop body. The loop is
1772                      declared as a for loop. */
1773                   tree body = build_loop_body (0, NULL_TREE, 0);
1774                   $$ =  build_new_loop (body);
1775                   FOR_LOOP_P ($$) = 1;
1776                   /* The loop is added to the current block the for
1777                      statement is defined within */
1778                   java_method_add_stmt (current_function_decl, $$);
1779                 }
1780 ;
1781 for_init:                       /* Can be empty */
1782                 { $$ = empty_stmt_node; }
1783 |       statement_expression_list
1784                 {
1785                   /* Init statement recorded within the previously
1786                      defined block scope */
1787                   $$ = java_method_add_stmt (current_function_decl, $1);
1788                 }
1789 |       local_variable_declaration
1790                 {
1791                   /* Local variable are recorded within the previously
1792                      defined block scope */
1793                   $$ = NULL_TREE;
1794                 }
1795 |       statement_expression_list error
1796                 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1797 ;
1798
1799 for_update:                     /* Can be empty */
1800                 {$$ = empty_stmt_node;}
1801 |       statement_expression_list
1802                 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1803 ;
1804
1805 statement_expression_list:
1806         statement_expression
1807                 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1808 |       statement_expression_list C_TK statement_expression
1809                 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1810 |       statement_expression_list C_TK error
1811                 {yyerror ("Missing term"); RECOVER;}
1812 ;
1813
1814 break_statement:
1815         BREAK_TK SC_TK
1816                 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1817 |       BREAK_TK identifier SC_TK
1818                 { $$ = build_bc_statement ($1.location, 1, $2); }
1819 |       BREAK_TK error
1820                 {yyerror ("Missing term"); RECOVER;}
1821 |       BREAK_TK identifier error
1822                 {yyerror ("';' expected"); RECOVER;}
1823 ;
1824
1825 continue_statement:
1826         CONTINUE_TK SC_TK
1827                 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1828 |       CONTINUE_TK identifier SC_TK
1829                 { $$ = build_bc_statement ($1.location, 0, $2); }
1830 |       CONTINUE_TK error
1831                 {yyerror ("Missing term"); RECOVER;}
1832 |       CONTINUE_TK identifier error
1833                 {yyerror ("';' expected"); RECOVER;}
1834 ;
1835
1836 return_statement:
1837         RETURN_TK SC_TK
1838                 { $$ = build_return ($1.location, NULL_TREE); }
1839 |       RETURN_TK expression SC_TK
1840                 { $$ = build_return ($1.location, $2); }
1841 |       RETURN_TK error
1842                 {yyerror ("Missing term"); RECOVER;}
1843 |       RETURN_TK expression error
1844                 {yyerror ("';' expected"); RECOVER;}
1845 ;
1846
1847 throw_statement:
1848         THROW_TK expression SC_TK
1849                 {
1850                   $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1851                   EXPR_WFL_LINECOL ($$) = $1.location;
1852                 }
1853 |       THROW_TK error
1854                 {yyerror ("Missing term"); RECOVER;}
1855 |       THROW_TK expression error
1856                 {yyerror ("';' expected"); RECOVER;}
1857 ;
1858
1859 assert_statement:
1860         ASSERT_TK expression REL_CL_TK expression SC_TK
1861                 {
1862                   $$ = build_assertion ($1.location, $2, $4);
1863                 }
1864 |       ASSERT_TK expression SC_TK
1865                 {
1866                   $$ = build_assertion ($1.location, $2, NULL_TREE);
1867                 }
1868 |       ASSERT_TK error
1869                 {yyerror ("Missing term"); RECOVER;}
1870 |       ASSERT_TK expression error
1871                 {yyerror ("';' expected"); RECOVER;}
1872 ;
1873
1874 synchronized_statement:
1875         synchronized OP_TK expression CP_TK block
1876                 {
1877                   $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1878                   EXPR_WFL_LINECOL ($$) =
1879                     EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1880                 }
1881 |       synchronized OP_TK expression CP_TK error
1882                 {yyerror ("'{' expected"); RECOVER;}
1883 |       synchronized error
1884                 {yyerror ("'(' expected"); RECOVER;}
1885 |       synchronized OP_TK error CP_TK
1886                 {yyerror ("Missing term"); RECOVER;}
1887 |       synchronized OP_TK error
1888                 {yyerror ("Missing term"); RECOVER;}
1889 ;
1890
1891 synchronized:
1892         modifiers
1893                 {
1894                   check_modifiers (
1895              "Illegal modifier `%s'. Only `synchronized' was expected here",
1896                                    $1, ACC_SYNCHRONIZED);
1897                   if ($1 != ACC_SYNCHRONIZED)
1898                     MODIFIER_WFL (SYNCHRONIZED_TK) =
1899                       build_wfl_node (NULL_TREE);
1900                 }
1901 ;
1902
1903 try_statement:
1904         TRY_TK block catches
1905                 { $$ = build_try_statement ($1.location, $2, $3); }
1906 |       TRY_TK block finally
1907                 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1908 |       TRY_TK block catches finally
1909                 { $$ = build_try_finally_statement
1910                     ($1.location, build_try_statement ($1.location,
1911                                                        $2, $3), $4);
1912                 }
1913 |       TRY_TK error
1914                 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1915 ;
1916
1917 catches:
1918         catch_clause
1919 |       catches catch_clause
1920                 {
1921                   TREE_CHAIN ($2) = $1;
1922                   $$ = $2;
1923                 }
1924 ;
1925
1926 catch_clause:
1927         catch_clause_parameter block
1928                 {
1929                   java_method_add_stmt (current_function_decl, $2);
1930                   exit_block ();
1931                   $$ = $1;
1932                 }
1933 ;
1934
1935 catch_clause_parameter:
1936         CATCH_TK OP_TK formal_parameter CP_TK
1937                 {
1938                   /* We add a block to define a scope for
1939                      formal_parameter (CCBP). The formal parameter is
1940                      declared initialized by the appropriate function
1941                      call */
1942                   tree ccpb = enter_block ();
1943                   tree init = build_assignment
1944                     (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1945                      build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1946                   declare_local_variables (0, TREE_VALUE ($3),
1947                                            build_tree_list (TREE_PURPOSE ($3),
1948                                                             init));
1949                   $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1950                   EXPR_WFL_LINECOL ($$) = $1.location;
1951                 }
1952 |       CATCH_TK error
1953                 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1954 |       CATCH_TK OP_TK error
1955                 {
1956                   yyerror ("Missing term or ')' expected");
1957                   RECOVER; $$ = NULL_TREE;
1958                 }
1959 |       CATCH_TK OP_TK error CP_TK /* That's for () */
1960                 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1961 ;
1962
1963 finally:
1964         FINALLY_TK block
1965                 { $$ = $2; }
1966 |       FINALLY_TK error
1967                 {yyerror ("'{' expected"); RECOVER; }
1968 ;
1969
1970 /* 19.12 Production from 15: Expressions  */
1971 primary:
1972         primary_no_new_array
1973 |       array_creation_expression
1974 ;
1975
1976 primary_no_new_array:
1977         literal
1978 |       THIS_TK
1979                 { $$ = build_this ($1.location); }
1980 |       OP_TK expression CP_TK
1981                 {$$ = $2;}
1982 |       class_instance_creation_expression
1983 |       field_access
1984 |       method_invocation
1985 |       array_access
1986 |       type_literals
1987         /* Added, JDK1.1 inner classes. Documentation is wrong
1988            refering to a 'ClassName' (class_name) rule that doesn't
1989            exist. Used name: instead.  */
1990 |       name DOT_TK THIS_TK
1991                 {
1992                   tree wfl = build_wfl_node (this_identifier_node);
1993                   $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1994                 }
1995 |       OP_TK expression error
1996                 {yyerror ("')' expected"); RECOVER;}
1997 |       name DOT_TK error
1998                 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1999 |       primitive_type DOT_TK error
2000                 {yyerror ("'class' expected" ); RECOVER;}
2001 |       VOID_TK DOT_TK error
2002                 {yyerror ("'class' expected" ); RECOVER;}
2003 ;
2004
2005 type_literals:
2006         name DOT_TK CLASS_TK
2007                 { $$ = build_incomplete_class_ref ($2.location, $1); }
2008 |       array_type DOT_TK CLASS_TK
2009                 { $$ = build_incomplete_class_ref ($2.location, $1); }
2010 |       primitive_type DOT_TK CLASS_TK
2011                 { $$ = build_incomplete_class_ref ($2.location, $1); }
2012 |       VOID_TK DOT_TK CLASS_TK
2013                 {
2014                    $$ = build_incomplete_class_ref ($2.location,
2015                                                    void_type_node);
2016                 }
2017 ;
2018
2019 class_instance_creation_expression:
2020         NEW_TK class_type OP_TK argument_list CP_TK
2021                 { $$ = build_new_invocation ($2, $4); }
2022 |       NEW_TK class_type OP_TK CP_TK
2023                 { $$ = build_new_invocation ($2, NULL_TREE); }
2024 |       anonymous_class_creation
2025         /* Added, JDK1.1 inner classes, modified to use name or
2026            primary instead of primary solely which couldn't work in
2027            all situations.  */
2028 |       something_dot_new identifier OP_TK CP_TK
2029                 {
2030                   tree ctor = build_new_invocation ($2, NULL_TREE);
2031                   $$ = make_qualified_primary ($1, ctor,
2032                                                EXPR_WFL_LINECOL ($1));
2033                 }
2034 |       something_dot_new identifier OP_TK CP_TK class_body
2035 |       something_dot_new identifier OP_TK argument_list CP_TK
2036                 {
2037                   tree ctor = build_new_invocation ($2, $4);
2038                   $$ = make_qualified_primary ($1, ctor,
2039                                                EXPR_WFL_LINECOL ($1));
2040                 }
2041 |       something_dot_new identifier OP_TK argument_list CP_TK class_body
2042 |       NEW_TK error SC_TK
2043                 {yyerror ("'(' expected"); DRECOVER(new_1);}
2044 |       NEW_TK class_type error
2045                 {yyerror ("'(' expected"); RECOVER;}
2046 |       NEW_TK class_type OP_TK error
2047                 {yyerror ("')' or term expected"); RECOVER;}
2048 |       NEW_TK class_type OP_TK argument_list error
2049                 {yyerror ("')' expected"); RECOVER;}
2050 |       something_dot_new error
2051                 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
2052 |       something_dot_new identifier error
2053                 {yyerror ("'(' expected"); RECOVER;}
2054 ;
2055
2056 /* Created after JDK1.1 rules originally added to
2057    class_instance_creation_expression, but modified to use
2058    'class_type' instead of 'TypeName' (type_name) which is mentionned
2059    in the documentation but doesn't exist. */
2060
2061 anonymous_class_creation:
2062         NEW_TK class_type OP_TK argument_list CP_TK
2063                 { create_anonymous_class ($1.location, $2); }
2064         class_body
2065                 {
2066                   tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2067                   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2068
2069                   end_class_declaration (1);
2070
2071                   /* Now we can craft the new expression */
2072                   $$ = build_new_invocation (id, $4);
2073
2074                   /* Note that we can't possibly be here if
2075                      `class_type' is an interface (in which case the
2076                      anonymous class extends Object and implements
2077                      `class_type', hence its constructor can't have
2078                      arguments.) */
2079
2080                   /* Otherwise, the innerclass must feature a
2081                      constructor matching `argument_list'. Anonymous
2082                      classes are a bit special: it's impossible to
2083                      define constructor for them, hence constructors
2084                      must be generated following the hints provided by
2085                      the `new' expression. Whether a super constructor
2086                      of that nature exists or not is to be verified
2087                      later on in verify_constructor_super.
2088
2089                      It's during the expansion of a `new' statement
2090                      refering to an anonymous class that a ctor will
2091                      be generated for the anonymous class, with the
2092                      right arguments. */
2093
2094                 }
2095 |       NEW_TK class_type OP_TK CP_TK
2096                 { create_anonymous_class ($1.location, $2); }
2097         class_body
2098                 {
2099                   tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2100                   EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2101
2102                   end_class_declaration (1);
2103
2104                   /* Now we can craft the new expression. The
2105                      statement doesn't need to be remember so that a
2106                      constructor can be generated, since its signature
2107                      is already known. */
2108                   $$ = build_new_invocation (id, NULL_TREE);
2109                 }
2110 ;
2111
2112 something_dot_new:              /* Added, not part of the specs. */
2113         name DOT_TK NEW_TK
2114                 { $$ = $1; }
2115 |       primary DOT_TK NEW_TK
2116                 { $$ = $1; }
2117 ;
2118
2119 argument_list:
2120         expression
2121                 {
2122                   $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2123                   ctxp->formal_parameter_number = 1;
2124                 }
2125 |       argument_list C_TK expression
2126                 {
2127                   ctxp->formal_parameter_number += 1;
2128                   $$ = tree_cons (NULL_TREE, $3, $1);
2129                 }
2130 |       argument_list C_TK error
2131                 {yyerror ("Missing term"); RECOVER;}
2132 ;
2133
2134 array_creation_expression:
2135         NEW_TK primitive_type dim_exprs
2136                 { $$ = build_newarray_node ($2, $3, 0); }
2137 |       NEW_TK class_or_interface_type dim_exprs
2138                 { $$ = build_newarray_node ($2, $3, 0); }
2139 |       NEW_TK primitive_type dim_exprs dims
2140                 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2141 |       NEW_TK class_or_interface_type dim_exprs dims
2142                 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2143         /* Added, JDK1.1 anonymous array. Initial documentation rule
2144            modified */
2145 |       NEW_TK class_or_interface_type dims array_initializer
2146                 {
2147                   char *sig;
2148                   int osb = pop_current_osb (ctxp);
2149                   while (osb--)
2150                     obstack_grow (&temporary_obstack, "[]", 2);
2151                   obstack_1grow (&temporary_obstack, '\0');
2152                   sig = obstack_finish (&temporary_obstack);
2153                   $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2154                               $2, get_identifier (sig), $4);
2155                 }
2156 |       NEW_TK primitive_type dims array_initializer
2157                 {
2158                   int osb = pop_current_osb (ctxp);
2159                   tree type = $2;
2160                   while (osb--)
2161                     type = build_java_array_type (type, -1);
2162                   $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2163                               build_pointer_type (type), NULL_TREE, $4);
2164                 }
2165 |       NEW_TK error CSB_TK
2166                 {yyerror ("'[' expected"); DRECOVER ("]");}
2167 |       NEW_TK error OSB_TK
2168                 {yyerror ("']' expected"); RECOVER;}
2169 ;
2170
2171 dim_exprs:
2172         dim_expr
2173                 { $$ = build_tree_list (NULL_TREE, $1); }
2174 |       dim_exprs dim_expr
2175                 { $$ = tree_cons (NULL_TREE, $2, $$); }
2176 ;
2177
2178 dim_expr:
2179         OSB_TK expression CSB_TK
2180                 {
2181                   if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2182                     {
2183                       $2 = build_wfl_node ($2);
2184                       TREE_TYPE ($2) = NULL_TREE;
2185                     }
2186                   EXPR_WFL_LINECOL ($2) = $1.location;
2187                   $$ = $2;
2188                 }
2189 |       OSB_TK expression error
2190                 {yyerror ("']' expected"); RECOVER;}
2191 |       OSB_TK error
2192                 {
2193                   yyerror ("Missing term");
2194                   yyerror ("']' expected");
2195                   RECOVER;
2196                 }
2197 ;
2198
2199 dims:
2200         OSB_TK CSB_TK
2201                 {
2202                   int allocate = 0;
2203                   /* If not initialized, allocate memory for the osb
2204                      numbers stack */
2205                   if (!ctxp->osb_limit)
2206                     {
2207                       allocate = ctxp->osb_limit = 32;
2208                       ctxp->osb_depth = -1;
2209                     }
2210                   /* If capacity overflown, reallocate a bigger chunk */
2211                   else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2212                     allocate = ctxp->osb_limit << 1;
2213
2214                   if (allocate)
2215                     {
2216                       allocate *= sizeof (int);
2217                       if (ctxp->osb_number)
2218                         ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2219                                                             allocate);
2220                       else
2221                         ctxp->osb_number = (int *)xmalloc (allocate);
2222                     }
2223                   ctxp->osb_depth++;
2224                   CURRENT_OSB (ctxp) = 1;
2225                 }
2226 |       dims OSB_TK CSB_TK
2227                 { CURRENT_OSB (ctxp)++; }
2228 |       dims OSB_TK error
2229                 { yyerror ("']' expected"); RECOVER;}
2230 ;
2231
2232 field_access:
2233         primary DOT_TK identifier
2234                 { $$ = make_qualified_primary ($1, $3, $2.location); }
2235                 /*  FIXME - REWRITE TO:
2236                 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2237 |       SUPER_TK DOT_TK identifier
2238                 {
2239                   tree super_wfl = build_wfl_node (super_identifier_node);
2240                   EXPR_WFL_LINECOL (super_wfl) = $1.location;
2241                   $$ = make_qualified_name (super_wfl, $3, $2.location);
2242                 }
2243 |       SUPER_TK error
2244                 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2245 ;
2246
2247 method_invocation:
2248         name OP_TK CP_TK
2249                 { $$ = build_method_invocation ($1, NULL_TREE); }
2250 |       name OP_TK argument_list CP_TK
2251                 { $$ = build_method_invocation ($1, $3); }
2252 |       primary DOT_TK identifier OP_TK CP_TK
2253                 {
2254                   if (TREE_CODE ($1) == THIS_EXPR)
2255                     $$ = build_this_super_qualified_invocation
2256                       (1, $3, NULL_TREE, 0, $2.location);
2257                   else
2258                     {
2259                       tree invok = build_method_invocation ($3, NULL_TREE);
2260                       $$ = make_qualified_primary ($1, invok, $2.location);
2261                     }
2262                 }
2263 |       primary DOT_TK identifier OP_TK argument_list CP_TK
2264                 {
2265                   if (TREE_CODE ($1) == THIS_EXPR)
2266                     $$ = build_this_super_qualified_invocation
2267                       (1, $3, $5, 0, $2.location);
2268                   else
2269                     {
2270                       tree invok = build_method_invocation ($3, $5);
2271                       $$ = make_qualified_primary ($1, invok, $2.location);
2272                     }
2273                 }
2274 |       SUPER_TK DOT_TK identifier OP_TK CP_TK
2275                 {
2276                   $$ = build_this_super_qualified_invocation
2277                     (0, $3, NULL_TREE, $1.location, $2.location);
2278                 }
2279 |       SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2280                 {
2281                   $$ = build_this_super_qualified_invocation
2282                     (0, $3, $5, $1.location, $2.location);
2283                 }
2284         /* Screws up thing. I let it here until I'm convinced it can
2285            be removed. FIXME
2286 |       primary DOT_TK error
2287                 {yyerror ("'(' expected"); DRECOVER(bad);} */
2288 |       SUPER_TK DOT_TK error CP_TK
2289                 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2290 |       SUPER_TK DOT_TK error DOT_TK
2291                 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2292 ;
2293
2294 array_access:
2295         name OSB_TK expression CSB_TK
2296                 { $$ = build_array_ref ($2.location, $1, $3); }
2297 |       primary_no_new_array OSB_TK expression CSB_TK
2298                 { $$ = build_array_ref ($2.location, $1, $3); }
2299 |       name OSB_TK error
2300                 {
2301                   yyerror ("Missing term and ']' expected");
2302                   DRECOVER(array_access);
2303                 }
2304 |       name OSB_TK expression error
2305                 {
2306                   yyerror ("']' expected");
2307                   DRECOVER(array_access);
2308                 }
2309 |       primary_no_new_array OSB_TK error
2310                 {
2311                   yyerror ("Missing term and ']' expected");
2312                   DRECOVER(array_access);
2313                 }
2314 |       primary_no_new_array OSB_TK expression error
2315                 {
2316                   yyerror ("']' expected");
2317                   DRECOVER(array_access);
2318                 }
2319 ;
2320
2321 postfix_expression:
2322         primary
2323 |       name
2324 |       post_increment_expression
2325 |       post_decrement_expression
2326 ;
2327
2328 post_increment_expression:
2329         postfix_expression INCR_TK
2330                 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2331 ;
2332
2333 post_decrement_expression:
2334         postfix_expression DECR_TK
2335                 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2336 ;
2337
2338 trap_overflow_corner_case:
2339         pre_increment_expression
2340 |       pre_decrement_expression
2341 |       PLUS_TK unary_expression
2342                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2343 |       unary_expression_not_plus_minus
2344 |       PLUS_TK error
2345                 {yyerror ("Missing term"); RECOVER}
2346 ;
2347
2348 unary_expression:
2349         trap_overflow_corner_case
2350                 {
2351                   error_if_numeric_overflow ($1);
2352                   $$ = $1;
2353                 }
2354 |       MINUS_TK trap_overflow_corner_case
2355                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2356 |       MINUS_TK error
2357                 {yyerror ("Missing term"); RECOVER}
2358 ;
2359
2360 pre_increment_expression:
2361         INCR_TK unary_expression
2362                 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2363 |       INCR_TK error
2364                 {yyerror ("Missing term"); RECOVER}
2365 ;
2366
2367 pre_decrement_expression:
2368         DECR_TK unary_expression
2369                 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2370 |       DECR_TK error
2371                 {yyerror ("Missing term"); RECOVER}
2372 ;
2373
2374 unary_expression_not_plus_minus:
2375         postfix_expression
2376 |       NOT_TK unary_expression
2377                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2378 |       NEG_TK unary_expression
2379                 {$$ = build_unaryop ($1.token, $1.location, $2); }
2380 |       cast_expression
2381 |       NOT_TK error
2382                 {yyerror ("Missing term"); RECOVER}
2383 |       NEG_TK error
2384                 {yyerror ("Missing term"); RECOVER}
2385 ;
2386
2387 cast_expression:                /* Error handling here is potentially weak */
2388         OP_TK primitive_type dims CP_TK unary_expression
2389                 {
2390                   tree type = $2;
2391                   int osb = pop_current_osb (ctxp);
2392                   while (osb--)
2393                     type = build_java_array_type (type, -1);
2394                   $$ = build_cast ($1.location, type, $5);
2395                 }
2396 |       OP_TK primitive_type CP_TK unary_expression
2397                 { $$ = build_cast ($1.location, $2, $4); }
2398 |       OP_TK expression CP_TK unary_expression_not_plus_minus
2399                 { $$ = build_cast ($1.location, $2, $4); }
2400 |       OP_TK name dims CP_TK unary_expression_not_plus_minus
2401                 {
2402                   const char *ptr;
2403                   int osb = pop_current_osb (ctxp);
2404                   obstack_grow (&temporary_obstack,
2405                                 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2406                                 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2407                   while (osb--)
2408                     obstack_grow (&temporary_obstack, "[]", 2);
2409                   obstack_1grow (&temporary_obstack, '\0');
2410                   ptr = obstack_finish (&temporary_obstack);
2411                   EXPR_WFL_NODE ($2) = get_identifier (ptr);
2412                   $$ = build_cast ($1.location, $2, $5);
2413                 }
2414 |       OP_TK primitive_type OSB_TK error
2415                 {yyerror ("']' expected, invalid type expression");}
2416 |       OP_TK error
2417                 {
2418                   YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2419                   RECOVER;
2420                 }
2421 |       OP_TK primitive_type dims CP_TK error
2422                 {yyerror ("Missing term"); RECOVER;}
2423 |       OP_TK primitive_type CP_TK error
2424                 {yyerror ("Missing term"); RECOVER;}
2425 |       OP_TK name dims CP_TK error
2426                 {yyerror ("Missing term"); RECOVER;}
2427 ;
2428
2429 multiplicative_expression:
2430         unary_expression
2431 |       multiplicative_expression MULT_TK unary_expression
2432                 {
2433                   $$ = build_binop (BINOP_LOOKUP ($2.token),
2434                                     $2.location, $1, $3);
2435                 }
2436 |       multiplicative_expression DIV_TK unary_expression
2437                 {
2438                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2439                                     $1, $3);
2440                 }
2441 |       multiplicative_expression REM_TK unary_expression
2442                 {
2443                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2444                                     $1, $3);
2445                 }
2446 |       multiplicative_expression MULT_TK error
2447                 {yyerror ("Missing term"); RECOVER;}
2448 |       multiplicative_expression DIV_TK error
2449                 {yyerror ("Missing term"); RECOVER;}
2450 |       multiplicative_expression REM_TK error
2451                 {yyerror ("Missing term"); RECOVER;}
2452 ;
2453
2454 additive_expression:
2455         multiplicative_expression
2456 |       additive_expression PLUS_TK multiplicative_expression
2457                 {
2458                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2459                                     $1, $3);
2460                 }
2461 |       additive_expression MINUS_TK multiplicative_expression
2462                 {
2463                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2464                                     $1, $3);
2465                 }
2466 |       additive_expression PLUS_TK error
2467                 {yyerror ("Missing term"); RECOVER;}
2468 |       additive_expression MINUS_TK error
2469                 {yyerror ("Missing term"); RECOVER;}
2470 ;
2471
2472 shift_expression:
2473         additive_expression
2474 |       shift_expression LS_TK additive_expression
2475                 {
2476                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2477                                     $1, $3);
2478                 }
2479 |       shift_expression SRS_TK additive_expression
2480                 {
2481                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2482                                     $1, $3);
2483                 }
2484 |       shift_expression ZRS_TK additive_expression
2485                 {
2486                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2487                                     $1, $3);
2488                 }
2489 |       shift_expression LS_TK error
2490                 {yyerror ("Missing term"); RECOVER;}
2491 |       shift_expression SRS_TK error
2492                 {yyerror ("Missing term"); RECOVER;}
2493 |       shift_expression ZRS_TK error
2494                 {yyerror ("Missing term"); RECOVER;}
2495 ;
2496
2497 relational_expression:
2498         shift_expression
2499 |       relational_expression LT_TK shift_expression
2500                 {
2501                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2502                                     $1, $3);
2503                 }
2504 |       relational_expression GT_TK shift_expression
2505                 {
2506                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2507                                     $1, $3);
2508                 }
2509 |       relational_expression LTE_TK shift_expression
2510                 {
2511                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2512                                     $1, $3);
2513                 }
2514 |       relational_expression GTE_TK shift_expression
2515                 {
2516                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2517                                     $1, $3);
2518                 }
2519 |       relational_expression INSTANCEOF_TK reference_type
2520                 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2521 |       relational_expression LT_TK error
2522                 {yyerror ("Missing term"); RECOVER;}
2523 |       relational_expression GT_TK error
2524                 {yyerror ("Missing term"); RECOVER;}
2525 |       relational_expression LTE_TK error
2526                 {yyerror ("Missing term"); RECOVER;}
2527 |       relational_expression GTE_TK error
2528                 {yyerror ("Missing term"); RECOVER;}
2529 |       relational_expression INSTANCEOF_TK error
2530                 {yyerror ("Invalid reference type"); RECOVER;}
2531 ;
2532
2533 equality_expression:
2534         relational_expression
2535 |       equality_expression EQ_TK relational_expression
2536                 {
2537                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2538                                     $1, $3);
2539                 }
2540 |       equality_expression NEQ_TK relational_expression
2541                 {
2542                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2543                                     $1, $3);
2544                 }
2545 |       equality_expression EQ_TK error
2546                 {yyerror ("Missing term"); RECOVER;}
2547 |       equality_expression NEQ_TK error
2548                 {yyerror ("Missing term"); RECOVER;}
2549 ;
2550
2551 and_expression:
2552         equality_expression
2553 |       and_expression AND_TK equality_expression
2554                 {
2555                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2556                                     $1, $3);
2557                 }
2558 |       and_expression AND_TK error
2559                 {yyerror ("Missing term"); RECOVER;}
2560 ;
2561
2562 exclusive_or_expression:
2563         and_expression
2564 |       exclusive_or_expression XOR_TK and_expression
2565                 {
2566                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2567                                     $1, $3);
2568                 }
2569 |       exclusive_or_expression XOR_TK error
2570                 {yyerror ("Missing term"); RECOVER;}
2571 ;
2572
2573 inclusive_or_expression:
2574         exclusive_or_expression
2575 |       inclusive_or_expression OR_TK exclusive_or_expression
2576                 {
2577                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2578                                     $1, $3);
2579                 }
2580 |       inclusive_or_expression OR_TK error
2581                 {yyerror ("Missing term"); RECOVER;}
2582 ;
2583
2584 conditional_and_expression:
2585         inclusive_or_expression
2586 |       conditional_and_expression BOOL_AND_TK inclusive_or_expression
2587                 {
2588                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2589                                     $1, $3);
2590                 }
2591 |       conditional_and_expression BOOL_AND_TK error
2592                 {yyerror ("Missing term"); RECOVER;}
2593 ;
2594
2595 conditional_or_expression:
2596         conditional_and_expression
2597 |       conditional_or_expression BOOL_OR_TK conditional_and_expression
2598                 {
2599                   $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2600                                     $1, $3);
2601                 }
2602 |       conditional_or_expression BOOL_OR_TK error
2603                 {yyerror ("Missing term"); RECOVER;}
2604 ;
2605
2606 conditional_expression:         /* Error handling here is weak */
2607         conditional_or_expression
2608 |       conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2609                 {
2610                   $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2611                   EXPR_WFL_LINECOL ($$) = $2.location;
2612                 }
2613 |       conditional_or_expression REL_QM_TK REL_CL_TK error
2614                 {
2615                   YYERROR_NOW;
2616                   yyerror ("Missing term");
2617                   DRECOVER (1);
2618                 }
2619 |       conditional_or_expression REL_QM_TK error
2620                 {yyerror ("Missing term"); DRECOVER (2);}
2621 |       conditional_or_expression REL_QM_TK expression REL_CL_TK error
2622                 {yyerror ("Missing term"); DRECOVER (3);}
2623 ;
2624
2625 assignment_expression:
2626         conditional_expression
2627 |       assignment
2628 ;
2629
2630 assignment:
2631         left_hand_side assignment_operator assignment_expression
2632                 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2633 |       left_hand_side assignment_operator error
2634                 {
2635                   YYNOT_TWICE yyerror ("Missing term");
2636                   DRECOVER (assign);
2637                 }
2638 ;
2639
2640 left_hand_side:
2641         name
2642 |       field_access
2643 |       array_access
2644 ;
2645
2646 assignment_operator:
2647         ASSIGN_ANY_TK
2648 |       ASSIGN_TK
2649 ;
2650
2651 expression:
2652         assignment_expression
2653 ;
2654
2655 constant_expression:
2656         expression
2657 ;
2658
2659 %%
2660
2661 /* Helper function to retrieve an OSB count. Should be used when the
2662    `dims:' rule is being used.  */
2663
2664 static int
2665 pop_current_osb (ctxp)
2666      struct parser_ctxt *ctxp;
2667 {
2668   int to_return;
2669
2670   if (ctxp->osb_depth < 0)
2671     abort ();
2672
2673   to_return = CURRENT_OSB (ctxp);
2674   ctxp->osb_depth--;
2675
2676   return to_return;
2677 }
2678
2679 \f
2680
2681 /* This section of the code deal with save/restoring parser contexts.
2682    Add mode documentation here. FIXME */
2683
2684 /* Helper function. Create a new parser context. With
2685    COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2686    context is copied, otherwise, the new context is zeroed. The newly
2687    created context becomes the current one.  */
2688
2689 static void
2690 create_new_parser_context (copy_from_previous)
2691     int copy_from_previous;
2692 {
2693   struct parser_ctxt *new;
2694
2695   new =  (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2696   if (copy_from_previous)
2697     {
2698       memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2699       new->saved_data_ctx = 1;
2700     }
2701   else
2702     memset ((PTR) new, 0, sizeof (struct parser_ctxt));
2703
2704   new->next = ctxp;
2705   ctxp = new;
2706 }
2707
2708 /* Create a new parser context and make it the current one. */
2709
2710 void
2711 java_push_parser_context ()
2712 {
2713   create_new_parser_context (0);
2714 }
2715
2716 void
2717 java_pop_parser_context (generate)
2718      int generate;
2719 {
2720   tree current;
2721   struct parser_ctxt *toFree, *next;
2722
2723   if (!ctxp)
2724     return;
2725
2726   toFree = ctxp;
2727   next = ctxp->next;
2728   if (next)
2729     {
2730       lineno = ctxp->lineno;
2731       current_class = ctxp->class_type;
2732     }
2733
2734   /* If the old and new lexers differ, then free the old one.  */
2735   if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2736     java_destroy_lexer (ctxp->lexer);
2737
2738   /* Set the single import class file flag to 0 for the current list
2739      of imported things */
2740   for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2741     IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2742
2743   /* And restore those of the previous context */
2744   if ((ctxp = next))            /* Assignment is really meant here */
2745     for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2746       IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2747
2748   /* If we pushed a context to parse a class intended to be generated,
2749      we keep it so we can remember the class. What we could actually
2750      do is to just update a list of class names.  */
2751   if (generate)
2752     {
2753       toFree->next = ctxp_for_generation;
2754       ctxp_for_generation = toFree;
2755     }
2756   else
2757     free (toFree);
2758 }
2759
2760 /* Create a parser context for the use of saving some global
2761    variables.  */
2762
2763 void
2764 java_parser_context_save_global ()
2765 {
2766   if (!ctxp)
2767     {
2768       java_push_parser_context ();
2769       ctxp->saved_data_ctx = 1;
2770     }
2771
2772   /* If this context already stores data, create a new one suitable
2773      for data storage. */
2774   else if (ctxp->saved_data)
2775     create_new_parser_context (1);
2776
2777   ctxp->lineno = lineno;
2778   ctxp->class_type = current_class;
2779   ctxp->filename = input_filename;
2780   ctxp->function_decl = current_function_decl;
2781   ctxp->saved_data = 1;
2782 }
2783
2784 /* Restore some global variables from the previous context. Make the
2785    previous context the current one.  */
2786
2787 void
2788 java_parser_context_restore_global ()
2789 {
2790   lineno = ctxp->lineno;
2791   current_class = ctxp->class_type;
2792   input_filename = ctxp->filename;
2793   if (wfl_operator)
2794     {
2795       tree s;
2796       BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2797       EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2798     }
2799   current_function_decl = ctxp->function_decl;
2800   ctxp->saved_data = 0;
2801   if (ctxp->saved_data_ctx)
2802     java_pop_parser_context (0);
2803 }
2804
2805 /* Suspend vital data for the current class/function being parsed so
2806    that an other class can be parsed. Used to let local/anonymous
2807    classes be parsed.  */
2808
2809 static void
2810 java_parser_context_suspend ()
2811 {
2812   /* This makes debugging through java_debug_context easier */
2813   static const char *const name = "<inner buffer context>";
2814
2815   /* Duplicate the previous context, use it to save the globals we're
2816      interested in */
2817   create_new_parser_context (1);
2818   ctxp->function_decl = current_function_decl;
2819   ctxp->class_type = current_class;
2820
2821   /* Then create a new context which inherits all data from the
2822      previous one. This will be the new current context  */
2823   create_new_parser_context (1);
2824
2825   /* Help debugging */
2826   ctxp->next->filename = name;
2827 }
2828
2829 /* Resume vital data for the current class/function being parsed so
2830    that an other class can be parsed. Used to let local/anonymous
2831    classes be parsed.  The trick is the data storing file position
2832    informations must be restored to their current value, so parsing
2833    can resume as if no context was ever saved. */
2834
2835 static void
2836 java_parser_context_resume ()
2837 {
2838   struct parser_ctxt *old = ctxp;             /* This one is to be discarded */
2839   struct parser_ctxt *saver = old->next;      /* This one contain saved info */
2840   struct parser_ctxt *restored = saver->next; /* This one is the old current */
2841
2842   /* We need to inherit the list of classes to complete/generate */
2843   restored->classd_list = old->classd_list;
2844   restored->class_list = old->class_list;
2845
2846   /* Restore the current class and function from the saver */
2847   current_class = saver->class_type;
2848   current_function_decl = saver->function_decl;
2849
2850   /* Retrive the restored context */
2851   ctxp = restored;
2852
2853   /* Re-installed the data for the parsing to carry on */
2854   memcpy (&ctxp->marker_begining, &old->marker_begining,
2855           (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2856
2857   /* Buffer context can now be discarded */
2858   free (saver);
2859   free (old);
2860 }
2861
2862 /* Add a new anchor node to which all statement(s) initializing static
2863    and non static initialized upon declaration field(s) will be
2864    linked.  */
2865
2866 static void
2867 java_parser_context_push_initialized_field ()
2868 {
2869   tree node;
2870
2871   node = build_tree_list (NULL_TREE, NULL_TREE);
2872   TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2873   CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2874
2875   node = build_tree_list (NULL_TREE, NULL_TREE);
2876   TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2877   CPC_INITIALIZER_LIST (ctxp) = node;
2878
2879   node = build_tree_list (NULL_TREE, NULL_TREE);
2880   TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2881   CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2882 }
2883
2884 /* Pop the lists of initialized field. If this lists aren't empty,
2885    remember them so we can use it to create and populate the finit$
2886    or <clinit> functions. */
2887
2888 static void
2889 java_parser_context_pop_initialized_field ()
2890 {
2891   tree stmts;
2892   tree class_type = TREE_TYPE (GET_CPC ());
2893
2894   if (CPC_INITIALIZER_LIST (ctxp))
2895     {
2896       stmts = CPC_INITIALIZER_STMT (ctxp);
2897       CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2898       if (stmts && !java_error_count)
2899         TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2900     }
2901
2902   if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2903     {
2904       stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2905       CPC_STATIC_INITIALIZER_LIST (ctxp) =
2906         TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2907       /* Keep initialization in order to enforce 8.5 */
2908       if (stmts && !java_error_count)
2909         TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2910     }
2911
2912   /* JDK 1.1 instance initializers */
2913   if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2914     {
2915       stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2916       CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2917         TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2918       if (stmts && !java_error_count)
2919         TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2920     }
2921 }
2922
2923 static tree
2924 reorder_static_initialized (list)
2925      tree list;
2926 {
2927   /* We have to keep things in order. The alias initializer have to
2928      come first, then the initialized regular field, in reverse to
2929      keep them in lexical order. */
2930   tree marker, previous = NULL_TREE;
2931   for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2932     if (TREE_CODE (marker) == TREE_LIST
2933         && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2934       break;
2935
2936   /* No static initialized, the list is fine as is */
2937   if (!previous)
2938     list = TREE_CHAIN (marker);
2939
2940   /* No marker? reverse the whole list */
2941   else if (!marker)
2942     list = nreverse (list);
2943
2944   /* Otherwise, reverse what's after the marker and the new reordered
2945      sublist will replace the marker. */
2946   else
2947     {
2948       TREE_CHAIN (previous) = NULL_TREE;
2949       list = nreverse (list);
2950       list = chainon (TREE_CHAIN (marker), list);
2951     }
2952   return list;
2953 }
2954
2955 /* Helper functions to dump the parser context stack.  */
2956
2957 #define TAB_CONTEXT(C) \
2958   {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2959
2960 static void
2961 java_debug_context_do (tab)
2962      int tab;
2963 {
2964   struct parser_ctxt *copy = ctxp;
2965   while (copy)
2966     {
2967       TAB_CONTEXT (tab);
2968       fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2969       TAB_CONTEXT (tab);
2970       fprintf (stderr, "filename: %s\n", copy->filename);
2971       TAB_CONTEXT (tab);
2972       fprintf (stderr, "lineno: %d\n", copy->lineno);
2973       TAB_CONTEXT (tab);
2974       fprintf (stderr, "package: %s\n",
2975                (copy->package ?
2976                 IDENTIFIER_POINTER (copy->package) : "<none>"));
2977       TAB_CONTEXT (tab);
2978       fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2979       TAB_CONTEXT (tab);
2980       fprintf (stderr, "saved data: %d\n", copy->saved_data);
2981       copy = copy->next;
2982       tab += 2;
2983     }
2984 }
2985
2986 /* Dump the stacked up parser contexts. Intended to be called from a
2987    debugger.  */
2988
2989 void
2990 java_debug_context ()
2991 {
2992   java_debug_context_do (0);
2993 }
2994
2995 \f
2996
2997 /* Flag for the error report routine to issue the error the first time
2998    it's called (overriding the default behavior which is to drop the
2999    first invocation and honor the second one, taking advantage of a
3000    richer context.  */
3001 static int force_error = 0;
3002
3003 /* Reporting an constructor invocation error.  */
3004 static void
3005 parse_ctor_invocation_error ()
3006 {
3007   if (DECL_CONSTRUCTOR_P (current_function_decl))
3008     yyerror ("Constructor invocation must be first thing in a constructor");
3009   else
3010     yyerror ("Only constructors can invoke constructors");
3011 }
3012
3013 /* Reporting JDK1.1 features not implemented.  */
3014
3015 static tree
3016 parse_jdk1_1_error (msg)
3017     const char *msg;
3018 {
3019   sorry (": `%s' JDK1.1(TM) feature", msg);
3020   java_error_count++;
3021   return empty_stmt_node;
3022 }
3023
3024 static int do_warning = 0;
3025
3026 void
3027 yyerror (msg)
3028      const char *msg;
3029 {
3030   static java_lc elc;
3031   static int  prev_lineno;
3032   static const char *prev_msg;
3033
3034   int save_lineno;
3035   char *remainder, *code_from_source;
3036
3037   if (!force_error && prev_lineno == lineno)
3038     return;
3039
3040   /* Save current error location but report latter, when the context is
3041      richer.  */
3042   if (ctxp->java_error_flag == 0)
3043     {
3044       ctxp->java_error_flag = 1;
3045       elc = ctxp->elc;
3046       /* Do something to use the previous line if we're reaching the
3047          end of the file... */
3048 #ifdef VERBOSE_SKELETON
3049       printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
3050 #endif
3051       return;
3052     }
3053
3054   /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3055   if (!force_error && msg == prev_msg && prev_lineno == elc.line)
3056     return;
3057
3058   ctxp->java_error_flag = 0;
3059   if (do_warning)
3060     java_warning_count++;
3061   else
3062     java_error_count++;
3063
3064   if (elc.col == 0 && msg && msg[1] == ';')
3065     {
3066       elc.col  = ctxp->p_line->char_col-1;
3067       elc.line = ctxp->p_line->lineno;
3068     }
3069
3070   save_lineno = lineno;
3071   prev_lineno = lineno = elc.line;
3072   prev_msg = msg;
3073
3074   code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3075   obstack_grow0 (&temporary_obstack,
3076                  code_from_source, strlen (code_from_source));
3077   remainder = obstack_finish (&temporary_obstack);
3078   if (do_warning)
3079     warning ("%s.\n%s", msg, remainder);
3080   else
3081     error ("%s.\n%s", msg, remainder);
3082
3083   /* This allow us to cheaply avoid an extra 'Invalid expression
3084      statement' error report when errors have been already reported on
3085      the same line. This occurs when we report an error but don't have
3086      a synchronization point other than ';', which
3087      expression_statement is the only one to take care of.  */
3088   ctxp->prevent_ese = lineno = save_lineno;
3089 }
3090
3091 static void
3092 issue_warning_error_from_context (cl, msg, ap)
3093      tree cl;
3094      const char *msg;
3095      va_list ap;
3096 {
3097   const char *saved, *saved_input_filename;
3098   char buffer [4096];
3099   vsprintf (buffer, msg, ap);
3100   force_error = 1;
3101
3102   ctxp->elc.line = EXPR_WFL_LINENO (cl);
3103   ctxp->elc.col  = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3104                     (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3105
3106   /* We have a CL, that's a good reason for using it if it contains data */
3107   saved = ctxp->filename;
3108   if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3109     ctxp->filename = EXPR_WFL_FILENAME (cl);
3110   saved_input_filename = input_filename;
3111   input_filename = ctxp->filename;
3112   java_error (NULL);
3113   java_error (buffer);
3114   ctxp->filename = saved;
3115   input_filename = saved_input_filename;
3116   force_error = 0;
3117 }
3118
3119 /* Issue an error message at a current source line CL */
3120
3121 void
3122 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3123 {
3124   VA_OPEN (ap, msg);
3125   VA_FIXEDARG (ap, tree, cl);
3126   VA_FIXEDARG (ap, const char *, msg);
3127   issue_warning_error_from_context (cl, msg, ap);
3128   VA_CLOSE (ap);
3129 }
3130
3131 /* Issue a warning at a current source line CL */
3132
3133 static void
3134 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3135 {
3136   VA_OPEN (ap, msg);
3137   VA_FIXEDARG (ap, tree, cl);
3138   VA_FIXEDARG (ap, const char *, msg);
3139
3140   force_error = do_warning = 1;
3141   issue_warning_error_from_context (cl, msg, ap);
3142   do_warning = force_error = 0;
3143   VA_CLOSE (ap);
3144 }
3145
3146 static tree
3147 find_expr_with_wfl (node)
3148      tree node;
3149 {
3150   while (node)
3151     {
3152       char code;
3153       tree to_return;
3154
3155       switch (TREE_CODE (node))
3156         {
3157         case BLOCK:
3158           node = BLOCK_EXPR_BODY (node);
3159           continue;
3160
3161         case COMPOUND_EXPR:
3162           to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3163           if (to_return)
3164             return to_return;
3165           node = TREE_OPERAND (node, 1);
3166           continue;
3167
3168         case LOOP_EXPR:
3169           node = TREE_OPERAND (node, 0);
3170           continue;
3171
3172         case LABELED_BLOCK_EXPR:
3173           node = TREE_OPERAND (node, 1);
3174           continue;
3175
3176         default:
3177           code = TREE_CODE_CLASS (TREE_CODE (node));
3178           if (((code == '1') || (code == '2') || (code == 'e'))
3179               && EXPR_WFL_LINECOL (node))
3180             return node;
3181           return NULL_TREE;
3182         }
3183     }
3184   return NULL_TREE;
3185 }
3186
3187 /* Issue a missing return statement error. Uses METHOD to figure the
3188    last line of the method the error occurs in.  */
3189
3190 static void
3191 missing_return_error (method)
3192      tree method;
3193 {
3194   EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3195   parse_error_context (wfl_operator, "Missing return statement");
3196 }
3197
3198 /* Issue an unreachable statement error. From NODE, find the next
3199    statement to report appropriately.  */
3200 static void
3201 unreachable_stmt_error (node)
3202      tree node;
3203 {
3204   /* Browse node to find the next expression node that has a WFL. Use
3205      the location to report the error */
3206   if (TREE_CODE (node) == COMPOUND_EXPR)
3207     node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3208   else
3209     node = find_expr_with_wfl (node);
3210
3211   if (node)
3212     {
3213       EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3214       parse_error_context (wfl_operator, "Unreachable statement");
3215     }
3216   else
3217     abort ();
3218 }
3219
3220 int
3221 java_report_errors ()
3222 {
3223   if (java_error_count)
3224     fprintf (stderr, "%d error%s",
3225              java_error_count, (java_error_count == 1 ? "" : "s"));
3226   if (java_warning_count)
3227     fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3228              java_warning_count, (java_warning_count == 1 ? "" : "s"));
3229   if (java_error_count || java_warning_count)
3230     putc ('\n', stderr);
3231   return java_error_count;
3232 }
3233
3234 static char *
3235 java_accstring_lookup (flags)
3236      int flags;
3237 {
3238   static char buffer [80];
3239 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3240
3241   /* Access modifier looked-up first for easier report on forbidden
3242      access. */
3243   if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3244   if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3245   if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3246   if (flags & ACC_STATIC) COPY_RETURN ("static");
3247   if (flags & ACC_FINAL) COPY_RETURN ("final");
3248   if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3249   if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3250   if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3251   if (flags & ACC_NATIVE) COPY_RETURN ("native");
3252   if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3253   if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3254
3255   buffer [0] = '\0';
3256   return buffer;
3257 #undef COPY_RETURN
3258 }
3259
3260 /* Issuing error messages upon redefinition of classes, interfaces or
3261    variables. */
3262
3263 static void
3264 classitf_redefinition_error (context, id, decl, cl)
3265      const char *context;
3266      tree id, decl, cl;
3267 {
3268   parse_error_context (cl, "%s `%s' already defined in %s:%d",
3269                        context, IDENTIFIER_POINTER (id),
3270                        DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3271   /* Here we should point out where its redefined. It's a unicode. FIXME */
3272 }
3273
3274 static void
3275 variable_redefinition_error (context, name, type, line)
3276      tree context, name, type;
3277      int line;
3278 {
3279   const char *type_name;
3280
3281   /* Figure a proper name for type. We might haven't resolved it */
3282   if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3283     type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3284   else
3285     type_name = lang_printable_name (type, 0);
3286
3287   parse_error_context (context,
3288                        "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3289                        IDENTIFIER_POINTER (name),
3290                        type_name, IDENTIFIER_POINTER (name), line);
3291 }
3292
3293 /* If ANAME is terminated with `[]', it indicates an array. This
3294    function returns the number of `[]' found and if this number is
3295    greater than zero, it extracts the array type name and places it in
3296    the node pointed to by TRIMMED unless TRIMMED is null.  */
3297
3298 static int
3299 build_type_name_from_array_name (aname, trimmed)
3300      tree aname;
3301      tree *trimmed;
3302 {
3303   const char *name = IDENTIFIER_POINTER (aname);
3304   int len = IDENTIFIER_LENGTH (aname);
3305   int array_dims;
3306
3307   STRING_STRIP_BRACKETS (name, len, array_dims);
3308
3309   if (array_dims && trimmed)
3310     *trimmed = get_identifier_with_length (name, len);
3311
3312   return array_dims;
3313 }
3314
3315 static tree
3316 build_array_from_name (type, type_wfl, name, ret_name)
3317      tree type, type_wfl, name, *ret_name;
3318 {
3319   int more_dims = 0;
3320
3321   /* Eventually get more dims */
3322   more_dims = build_type_name_from_array_name (name, &name);
3323
3324   /* If we have, then craft a new type for this variable */
3325   if (more_dims)
3326     {
3327       tree save = type;
3328
3329       /* If we have a pointer, use its type */
3330       if (TREE_CODE (type) == POINTER_TYPE)
3331         type = TREE_TYPE (type);
3332
3333       /* Building the first dimension of a primitive type uses this
3334          function */
3335       if (JPRIMITIVE_TYPE_P (type))
3336         {
3337           type = build_java_array_type (type, -1);
3338           more_dims--;
3339         }
3340       /* Otherwise, if we have a WFL for this type, use it (the type
3341          is already an array on an unresolved type, and we just keep
3342          on adding dimensions) */
3343       else if (type_wfl)
3344         {
3345           type = type_wfl;
3346           more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3347                                                         NULL);
3348         }
3349
3350       /* Add all the dimensions */
3351       while (more_dims--)
3352         type = build_unresolved_array_type (type);
3353
3354       /* The type may have been incomplete in the first place */
3355       if (type_wfl)
3356         type = obtain_incomplete_type (type);
3357     }
3358
3359   if (ret_name)
3360     *ret_name = name;
3361   return type;
3362 }
3363
3364 /* Build something that the type identifier resolver will identify as
3365    being an array to an unresolved type. TYPE_WFL is a WFL on a
3366    identifier. */
3367
3368 static tree
3369 build_unresolved_array_type (type_or_wfl)
3370      tree type_or_wfl;
3371 {
3372   const char *ptr;
3373   tree wfl;
3374
3375   /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3376      just create a array type */
3377   if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3378     return build_java_array_type (type_or_wfl, -1);
3379
3380   obstack_grow (&temporary_obstack,
3381                  IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3382                  IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3383   obstack_grow0 (&temporary_obstack, "[]", 2);
3384   ptr = obstack_finish (&temporary_obstack);
3385   wfl = build_expr_wfl (get_identifier (ptr),
3386                         EXPR_WFL_FILENAME (type_or_wfl),
3387                         EXPR_WFL_LINENO (type_or_wfl),
3388                         EXPR_WFL_COLNO (type_or_wfl));
3389   /* Re-install the existing qualifications so that the type can be
3390      resolved properly. */
3391   EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3392   return wfl;
3393 }
3394
3395 static void
3396 parser_add_interface (class_decl, interface_decl, wfl)
3397      tree class_decl, interface_decl, wfl;
3398 {
3399   if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3400     parse_error_context (wfl, "Interface `%s' repeated",
3401                          IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3402 }
3403
3404 /* Bulk of common class/interface checks. Return 1 if an error was
3405    encountered. TAG is 0 for a class, 1 for an interface.  */
3406
3407 static int
3408 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3409      int is_interface, flags;
3410      tree raw_name, qualified_name, decl, cl;
3411 {
3412   tree node;
3413   int sca = 0;                  /* Static class allowed */
3414   int icaf = 0;                 /* Inner class allowed flags */
3415   int uaaf = CLASS_MODIFIERS;   /* Usually allowed access flags */
3416
3417   if (!quiet_flag)
3418     fprintf (stderr, " %s%s %s",
3419              (CPC_INNER_P () ? "inner" : ""),
3420              (is_interface ? "interface" : "class"),
3421              IDENTIFIER_POINTER (qualified_name));
3422
3423   /* Scope of an interface/class type name:
3424        - Can't be imported by a single type import
3425        - Can't already exists in the package */
3426   if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3427       && (node = find_name_in_single_imports (raw_name))
3428       && !CPC_INNER_P ())
3429     {
3430       parse_error_context
3431         (cl, "%s name `%s' clashes with imported type `%s'",
3432          (is_interface ? "Interface" : "Class"),
3433          IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3434       return 1;
3435     }
3436   if (decl && CLASS_COMPLETE_P (decl))
3437     {
3438       classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3439                                    qualified_name, decl, cl);
3440       return 1;
3441     }
3442
3443   if (check_inner_class_redefinition (raw_name, cl))
3444     return 1;
3445
3446   /* If public, file name should match class/interface name, except
3447      when dealing with an inner class */
3448   if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3449     {
3450       const char *f;
3451
3452       /* Contains OS dependent assumption on path separator. FIXME */
3453       for (f = &input_filename [strlen (input_filename)];
3454            f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3455            f--)
3456         ;
3457       if (f[0] == '/' || f[0] == DIR_SEPARATOR)
3458         f++;
3459       if (strncmp (IDENTIFIER_POINTER (raw_name),
3460                    f , IDENTIFIER_LENGTH (raw_name)) ||
3461           f [IDENTIFIER_LENGTH (raw_name)] != '.')
3462         parse_error_context
3463           (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3464                              (is_interface ? "interface" : "class"),
3465                              IDENTIFIER_POINTER (qualified_name),
3466                              IDENTIFIER_POINTER (raw_name));
3467     }
3468
3469   /* Static classes can be declared only in top level classes. Note:
3470      once static, a inner class is a top level class. */
3471   if (flags & ACC_STATIC)
3472     {
3473       /* Catch the specific error of declaring an class inner class
3474          with no toplevel enclosing class. Prevent check_modifiers from
3475          complaining a second time */
3476       if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3477         {
3478           parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3479                                IDENTIFIER_POINTER (qualified_name));
3480           sca = ACC_STATIC;
3481         }
3482       /* Else, in the context of a top-level class declaration, let
3483          `check_modifiers' do its job, otherwise, give it a go */
3484       else
3485         sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3486     }
3487
3488   /* Inner classes can be declared private or protected
3489      within their enclosing classes. */
3490   if (CPC_INNER_P ())
3491     {
3492       /* A class which is local to a block can't be public, private,
3493          protected or static. But it is created final, so allow this
3494          one. */
3495       if (current_function_decl)
3496         icaf = sca = uaaf = ACC_FINAL;
3497       else
3498         {
3499           check_modifiers_consistency (flags);
3500           icaf = ACC_PROTECTED;
3501           if (! CLASS_INTERFACE (GET_CPC ()))
3502             icaf |= ACC_PRIVATE;
3503         }
3504     }
3505
3506   if (is_interface)
3507     {
3508       if (CPC_INNER_P ())
3509         uaaf = INTERFACE_INNER_MODIFIERS;
3510       else
3511         uaaf = INTERFACE_MODIFIERS;
3512
3513       check_modifiers ("Illegal modifier `%s' for interface declaration",
3514                        flags, uaaf);
3515     }
3516   else
3517     check_modifiers ((current_function_decl ?
3518                       "Illegal modifier `%s' for local class declaration" :
3519                       "Illegal modifier `%s' for class declaration"),
3520                      flags, uaaf|sca|icaf);
3521   return 0;
3522 }
3523
3524 /* Construct a nested class name.  If the final component starts with
3525    a digit, return true.  Otherwise return false.  */
3526 static int
3527 make_nested_class_name (cpc_list)
3528      tree cpc_list;
3529 {
3530   tree name;
3531
3532   if (!cpc_list)
3533     return 0;
3534
3535   make_nested_class_name (TREE_CHAIN (cpc_list));
3536
3537   /* Pick the qualified name when dealing with the first upmost
3538      enclosing class */
3539   name = (TREE_CHAIN (cpc_list)
3540           ? TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3541   obstack_grow (&temporary_obstack,
3542                 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3543   obstack_1grow (&temporary_obstack, '$');
3544
3545   return ISDIGIT (IDENTIFIER_POINTER (name)[0]);
3546 }
3547
3548 /* Can't redefine a class already defined in an earlier scope. */
3549
3550 static int
3551 check_inner_class_redefinition (raw_name, cl)
3552      tree raw_name, cl;
3553 {
3554   tree scope_list;
3555
3556   for (scope_list = GET_CPC_LIST (); scope_list;
3557        scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3558     if (raw_name == GET_CPC_UN_NODE (scope_list))
3559       {
3560         parse_error_context
3561           (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",
3562            IDENTIFIER_POINTER (raw_name));
3563         return 1;
3564       }
3565   return 0;
3566 }
3567
3568 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. If we fail,
3569    we remember ENCLOSING and SUPER.  */
3570
3571 static tree
3572 resolve_inner_class (circularity_hash, cl, enclosing, super, class_type)
3573      htab_t circularity_hash;
3574      tree cl, *enclosing, *super, class_type;
3575 {
3576   tree local_enclosing = *enclosing;
3577   tree local_super = NULL_TREE;
3578
3579   while (local_enclosing)
3580     {
3581       tree intermediate, decl;
3582
3583       *htab_find_slot (circularity_hash, local_enclosing, INSERT) =
3584         local_enclosing;
3585
3586       if ((decl = find_as_inner_class (local_enclosing, class_type, cl)))
3587         return decl;
3588
3589       intermediate = local_enclosing;
3590       /* Explore enclosing contexts. */
3591       while (INNER_CLASS_DECL_P (intermediate))
3592         {
3593           intermediate = DECL_CONTEXT (intermediate);
3594           if ((decl = find_as_inner_class (intermediate, class_type, cl)))
3595             return decl;
3596         }
3597
3598       /* Now go to the upper classes, bail out if necessary. We will
3599          analyze the returned SUPER and act accordingly (see
3600          do_resolve_class.) */
3601       local_super = CLASSTYPE_SUPER (TREE_TYPE (local_enclosing));
3602       if (!local_super || local_super == object_type_node)
3603         break;
3604
3605       if (TREE_CODE (local_super) == POINTER_TYPE)
3606         local_super = do_resolve_class (NULL, local_super, NULL, NULL);
3607       else
3608         local_super = TYPE_NAME (local_super);
3609
3610       /* We may not have checked for circular inheritance yet, so do so
3611          here to prevent an infinite loop. */
3612       if (htab_find (circularity_hash, local_super) != NULL)
3613         {
3614           if (!cl)
3615             cl = lookup_cl (local_enclosing);
3616
3617           parse_error_context
3618             (cl, "Cyclic inheritance involving %s",
3619              IDENTIFIER_POINTER (DECL_NAME (local_enclosing)));
3620           local_enclosing = NULL_TREE;
3621         }
3622       else
3623         local_enclosing = local_super;
3624     }
3625
3626   /* We failed. Return LOCAL_SUPER and LOCAL_ENCLOSING. */
3627   *super = local_super;
3628   *enclosing = local_enclosing;
3629
3630   return NULL_TREE;
3631 }
3632
3633 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3634    qualified. */
3635
3636 static tree
3637 find_as_inner_class (enclosing, name, cl)
3638      tree enclosing, name, cl;
3639 {
3640   tree qual, to_return;
3641   if (!enclosing)
3642     return NULL_TREE;
3643
3644   name = TYPE_NAME (name);
3645
3646   /* First search: within the scope of `enclosing', search for name */
3647   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3648     qual = EXPR_WFL_QUALIFICATION (cl);
3649   else if (cl)
3650     qual = build_tree_list (cl, NULL_TREE);
3651   else
3652     qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3653
3654   if ((to_return = find_as_inner_class_do (qual, enclosing)))
3655     return to_return;
3656
3657   /* We're dealing with a qualified name. Try to resolve thing until
3658      we get something that is an enclosing class. */
3659   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3660     {
3661       tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3662
3663       for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3664            qual = TREE_CHAIN (qual))
3665         {
3666           acc = merge_qualified_name (acc,
3667                                       EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3668           BUILD_PTR_FROM_NAME (ptr, acc);
3669           decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3670         }
3671
3672       /* A NULL qual and a decl means that the search ended
3673          successfully?!? We have to do something then. FIXME */
3674
3675       if (decl)
3676         enclosing = decl;
3677       else
3678         qual = EXPR_WFL_QUALIFICATION (cl);
3679     }
3680   /* Otherwise, create a qual for the other part of the resolution. */
3681   else
3682     qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3683
3684   return find_as_inner_class_do (qual, enclosing);
3685 }
3686
3687 /* We go inside the list of sub classes and try to find a way
3688    through. */
3689
3690 static tree
3691 find_as_inner_class_do (qual, enclosing)
3692      tree qual, enclosing;
3693 {
3694   if (!qual)
3695     return NULL_TREE;
3696
3697   for (; qual && enclosing; qual = TREE_CHAIN (qual))
3698     {
3699       tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3700       tree next_enclosing = NULL_TREE;
3701       tree inner_list;
3702
3703       for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3704            inner_list; inner_list = TREE_CHAIN (inner_list))
3705         {
3706           if (TREE_VALUE (inner_list) == name_to_match)
3707             {
3708               next_enclosing = TREE_PURPOSE (inner_list);
3709               break;
3710             }
3711         }
3712       enclosing = next_enclosing;
3713     }
3714
3715   return (!qual && enclosing ? enclosing : NULL_TREE);
3716 }
3717
3718 /* Reach all inner classes and tie their unqualified name to a
3719    DECL. */
3720
3721 static void
3722 set_nested_class_simple_name_value (outer, set)
3723      tree outer;
3724      int set;
3725 {
3726   tree l;
3727
3728   for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3729     IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3730                                                 TREE_PURPOSE (l) : NULL_TREE);
3731 }
3732
3733 static void
3734 link_nested_class_to_enclosing ()
3735 {
3736   if (GET_ENCLOSING_CPC ())
3737     {
3738       tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3739       DECL_INNER_CLASS_LIST (enclosing) =
3740         tree_cons (GET_CPC (), GET_CPC_UN (),
3741                    DECL_INNER_CLASS_LIST (enclosing));
3742     }
3743 }
3744
3745 static tree
3746 maybe_make_nested_class_name (name)
3747      tree name;
3748 {
3749   tree id = NULL_TREE;
3750
3751   if (CPC_INNER_P ())
3752     {
3753       /* If we're in a function, we must append a number to create the
3754          nested class name.  However, we don't do this if the class we
3755          are constructing is anonymous, because in that case we'll
3756          already have a number as the class name.  */
3757       if (! make_nested_class_name (GET_CPC_LIST ())
3758           && current_function_decl != NULL_TREE
3759           && ! ISDIGIT (IDENTIFIER_POINTER (name)[0]))
3760         {
3761           char buf[10];
3762           sprintf (buf, "%d", anonymous_class_counter);
3763           ++anonymous_class_counter;
3764           obstack_grow (&temporary_obstack, buf, strlen (buf));
3765           obstack_1grow (&temporary_obstack, '$');
3766         }
3767       obstack_grow0 (&temporary_obstack,
3768                      IDENTIFIER_POINTER (name),
3769                      IDENTIFIER_LENGTH (name));
3770       id = get_identifier (obstack_finish (&temporary_obstack));
3771       if (ctxp->package)
3772         QUALIFIED_P (id) = 1;
3773     }
3774   return id;
3775 }
3776
3777 /* If DECL is NULL, create and push a new DECL, record the current
3778    line CL and do other maintenance things.  */
3779
3780 static tree
3781 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3782      tree decl, raw_name, qualified_name, cl;
3783 {
3784   if (!decl)
3785     decl = push_class (make_class (), qualified_name);
3786
3787   /* Take care of the file and line business */
3788   DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3789   /* If we're emiting xrefs, store the line/col number information */
3790   if (flag_emit_xref)
3791     DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3792   else
3793     DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3794   CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3795   CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3796   CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3797     IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3798
3799   PUSH_CPC (decl, raw_name);
3800   DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3801
3802   /* Link the declaration to the already seen ones */
3803   TREE_CHAIN (decl) = ctxp->class_list;
3804   ctxp->class_list = decl;
3805
3806   /* Create a new nodes in the global lists */
3807   gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3808   all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3809
3810   /* Install a new dependency list element */
3811   create_jdep_list (ctxp);
3812
3813   SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3814                           IDENTIFIER_POINTER (qualified_name)));
3815   return decl;
3816 }
3817
3818 static void
3819 add_superinterfaces (decl, interface_list)
3820      tree decl, interface_list;
3821 {
3822   tree node;
3823   /* Superinterface(s): if present and defined, parser_check_super_interface ()
3824      takes care of ensuring that:
3825        - This is an accessible interface type,
3826        - Circularity detection.
3827    parser_add_interface is then called. If present but not defined,
3828    the check operation is delayed until the super interface gets
3829    defined.  */
3830   for (node = interface_list; node; node = TREE_CHAIN (node))
3831     {
3832       tree current = TREE_PURPOSE (node);
3833       tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3834       if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3835         {
3836           if (!parser_check_super_interface (idecl, decl, current))
3837             parser_add_interface (decl, idecl, current);
3838         }
3839       else
3840         register_incomplete_type (JDEP_INTERFACE,
3841                                   current, decl, NULL_TREE);
3842     }
3843 }
3844
3845 /* Create an interface in pass1 and return its decl. Return the
3846    interface's decl in pass 2.  */
3847
3848 static tree
3849 create_interface (flags, id, super)
3850      int flags;
3851      tree id, super;
3852 {
3853   tree raw_name = EXPR_WFL_NODE (id);
3854   tree q_name = parser_qualified_classname (raw_name);
3855   tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3856
3857   /* Certain syntax errors are making SUPER be like ID. Avoid this
3858      case. */
3859   if (ctxp->class_err && id == super)
3860     super = NULL;
3861
3862   EXPR_WFL_NODE (id) = q_name;  /* Keep source location, even if refined. */
3863
3864   /* Basic checks: scope, redefinition, modifiers */
3865   if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3866     {
3867       PUSH_ERROR ();
3868       return NULL_TREE;
3869     }
3870
3871   /* Suspend the current parsing context if we're parsing an inner
3872      interface */
3873   if (CPC_INNER_P ())
3874     {
3875       java_parser_context_suspend ();
3876       /* Interface members are public. */
3877       if (CLASS_INTERFACE (GET_CPC ()))
3878         flags |= ACC_PUBLIC;
3879     }
3880
3881   /* Push a new context for (static) initialized upon declaration fields */
3882   java_parser_context_push_initialized_field ();
3883
3884   /* Interface modifiers check
3885        - public/abstract allowed (already done at that point)
3886        - abstract is obsolete (comes first, it's a warning, or should be)
3887        - Can't use twice the same (checked in the modifier rule) */
3888   if ((flags & ACC_ABSTRACT) && flag_redundant)
3889     parse_warning_context
3890       (MODIFIER_WFL (ABSTRACT_TK),
3891        "Redundant use of `abstract' modifier. Interface `%s' is implicitly abstract", IDENTIFIER_POINTER (raw_name));
3892
3893   /* Create a new decl if DECL is NULL, otherwise fix it */
3894   decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3895
3896   /* Set super info and mark the class a complete */
3897   set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3898                   object_type_node, ctxp->interface_number);
3899   ctxp->interface_number = 0;
3900   CLASS_COMPLETE_P (decl) = 1;
3901   add_superinterfaces (decl, super);
3902
3903   return decl;
3904 }
3905
3906 /* Patch anonymous class CLASS, by either extending or implementing
3907    DEP.  */
3908
3909 static void
3910 patch_anonymous_class (type_decl, class_decl, wfl)
3911     tree type_decl, class_decl, wfl;
3912 {
3913   tree class = TREE_TYPE (class_decl);
3914   tree type =  TREE_TYPE (type_decl);
3915   tree binfo = TYPE_BINFO (class);
3916
3917   /* If it's an interface, implement it */
3918   if (CLASS_INTERFACE (type_decl))
3919     {
3920       tree s_binfo;
3921       int length;
3922
3923       if (parser_check_super_interface (type_decl, class_decl, wfl))
3924         return;
3925
3926       s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3927       length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3928       TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3929       TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3930       /* And add the interface */
3931       parser_add_interface (class_decl, type_decl, wfl);
3932     }
3933   /* Otherwise, it's a type we want to extend */
3934   else
3935     {
3936       if (parser_check_super (type_decl, class_decl, wfl))
3937         return;
3938       BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3939     }
3940 }
3941
3942 static tree
3943 create_anonymous_class (location, type_name)
3944     int location;
3945     tree type_name;
3946 {
3947   char buffer [80];
3948   tree super = NULL_TREE, itf = NULL_TREE;
3949   tree id, type_decl, class;
3950
3951   /* The unqualified name of the anonymous class. It's just a number. */
3952   sprintf (buffer, "%d", anonymous_class_counter++);
3953   id = build_wfl_node (get_identifier (buffer));
3954   EXPR_WFL_LINECOL (id) = location;
3955
3956   /* We know about the type to extend/implement. We go ahead */
3957   if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3958     {
3959       /* Create a class which either implements on extends the designated
3960          class. The class bears an innacessible name. */
3961       if (CLASS_INTERFACE (type_decl))
3962         {
3963           /* It's OK to modify it here. It's been already used and
3964              shouldn't be reused */
3965           ctxp->interface_number = 1;
3966           /* Interfaces should presented as a list of WFLs */
3967           itf = build_tree_list (type_name, NULL_TREE);
3968         }
3969       else
3970         super = type_name;
3971     }
3972
3973   class = create_class (ACC_FINAL, id, super, itf);
3974
3975   /* We didn't know anything about the stuff. We register a dependence. */
3976   if (!type_decl)
3977     register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3978
3979   ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3980   return class;
3981 }
3982
3983 /* Create a class in pass1 and return its decl. Return class
3984    interface's decl in pass 2.  */
3985
3986 static tree
3987 create_class (flags, id, super, interfaces)
3988      int flags;
3989      tree id, super, interfaces;
3990 {
3991   tree raw_name = EXPR_WFL_NODE (id);
3992   tree class_id, decl;
3993   tree super_decl_type;
3994
3995   /* Certain syntax errors are making SUPER be like ID. Avoid this
3996      case. */
3997   if (ctxp->class_err && id == super)
3998     super = NULL;
3999
4000   class_id = parser_qualified_classname (raw_name);
4001   decl = IDENTIFIER_CLASS_VALUE (class_id);
4002   EXPR_WFL_NODE (id) = class_id;
4003
4004   /* Basic check: scope, redefinition, modifiers */
4005   if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
4006     {
4007       PUSH_ERROR ();
4008       return NULL_TREE;
4009     }
4010
4011   /* Suspend the current parsing context if we're parsing an inner
4012      class or an anonymous class. */
4013   if (CPC_INNER_P ())
4014     {
4015       java_parser_context_suspend ();
4016       /* Interface members are public. */
4017       if (CLASS_INTERFACE (GET_CPC ()))
4018         flags |= ACC_PUBLIC;
4019     }
4020
4021   /* Push a new context for (static) initialized upon declaration fields */
4022   java_parser_context_push_initialized_field ();
4023
4024   /* Class modifier check:
4025        - Allowed modifier (already done at that point)
4026        - abstract AND final forbidden
4027        - Public classes defined in the correct file */
4028   if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
4029     parse_error_context
4030       (id, "Class `%s' can't be declared both abstract and final",
4031        IDENTIFIER_POINTER (raw_name));
4032
4033   /* Create a new decl if DECL is NULL, otherwise fix it */
4034   decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
4035
4036   /* If SUPER exists, use it, otherwise use Object */
4037   if (super)
4038     {
4039       /* Can't extend java.lang.Object */
4040       if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4041         {
4042           parse_error_context (id, "Can't extend `java.lang.Object'");
4043           return NULL_TREE;
4044         }
4045
4046       super_decl_type =
4047         register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4048     }
4049   else if (TREE_TYPE (decl) != object_type_node)
4050     super_decl_type = object_type_node;
4051   /* We're defining java.lang.Object */
4052   else
4053     super_decl_type = NULL_TREE;
4054
4055   /* A class nested in an interface is implicitly static. */
4056   if (INNER_CLASS_DECL_P (decl)
4057       && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4058     {
4059       flags |= ACC_STATIC;
4060     }
4061
4062   /* Set super info and mark the class as complete. */
4063   set_super_info (flags, TREE_TYPE (decl), super_decl_type,
4064                   ctxp->interface_number);
4065   ctxp->interface_number = 0;
4066   CLASS_COMPLETE_P (decl) = 1;
4067   add_superinterfaces (decl, interfaces);
4068
4069   /* Add the private this$<n> field, Replicate final locals still in
4070      scope as private final fields mangled like val$<local_name>.
4071      This doesn't not occur for top level (static) inner classes. */
4072   if (PURE_INNER_CLASS_DECL_P (decl))
4073     add_inner_class_fields (decl, current_function_decl);
4074
4075   /* If doing xref, store the location at which the inherited class
4076      (if any) was seen. */
4077   if (flag_emit_xref && super)
4078     DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
4079
4080   /* Eventually sets the @deprecated tag flag */
4081   CHECK_DEPRECATED (decl);
4082
4083   /* Reset the anonymous class counter when declaring non inner classes */
4084   if (!INNER_CLASS_DECL_P (decl))
4085     anonymous_class_counter = 1;
4086
4087   return decl;
4088 }
4089
4090 /* End a class declaration: register the statements used to create
4091    finit$ and <clinit>, pop the current class and resume the prior
4092    parser context if necessary.  */
4093
4094 static void
4095 end_class_declaration (resume)
4096      int resume;
4097 {
4098   /* If an error occurred, context weren't pushed and won't need to be
4099      popped by a resume. */
4100   int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4101
4102   if (GET_CPC () != error_mark_node)
4103     dump_java_tree (TDI_class, GET_CPC ());
4104
4105   java_parser_context_pop_initialized_field ();
4106   POP_CPC ();
4107   if (resume && no_error_occurred)
4108     java_parser_context_resume ();
4109
4110   /* We're ending a class declaration, this is a good time to reset
4111      the interface cout. Note that might have been already done in
4112      create_interface, but if at that time an inner class was being
4113      dealt with, the interface count was reset in a context created
4114      for the sake of handling inner classes declaration. */
4115   ctxp->interface_number = 0;
4116 }
4117
4118 static void
4119 add_inner_class_fields (class_decl, fct_decl)
4120      tree class_decl;
4121      tree fct_decl;
4122 {
4123   tree block, marker, f;
4124
4125   f = add_field (TREE_TYPE (class_decl),
4126                  build_current_thisn (TREE_TYPE (class_decl)),
4127                  build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4128                  ACC_PRIVATE);
4129   FIELD_THISN (f) = 1;
4130
4131   if (!fct_decl)
4132     return;
4133
4134   for (block = GET_CURRENT_BLOCK (fct_decl);
4135        block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4136     {
4137       tree decl;
4138       for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4139         {
4140           tree name, pname;
4141           tree wfl, init, list;
4142
4143           /* Avoid non final arguments. */
4144           if (!LOCAL_FINAL_P (decl))
4145             continue;
4146
4147           MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4148           MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4149           wfl = build_wfl_node (name);
4150           init = build_wfl_node (pname);
4151           /* Build an initialization for the field: it will be
4152              initialized by a parameter added to finit$, bearing a
4153              mangled name of the field itself (param$<n>.) The
4154              parameter is provided to finit$ by the constructor
4155              invoking it (hence the constructor will also feature a
4156              hidden parameter, set to the value of the outer context
4157              local at the time the inner class is created.)
4158
4159              Note: we take into account all possible locals that can
4160              be accessed by the inner class. It's actually not trivial
4161              to minimize these aliases down to the ones really
4162              used. One way to do that would be to expand all regular
4163              methods first, then finit$ to get a picture of what's
4164              used.  It works with the exception that we would have to
4165              go back on all constructor invoked in regular methods to
4166              have their invokation reworked (to include the right amount
4167              of alias initializer parameters.)
4168
4169              The only real way around, I think, is a first pass to
4170              identify locals really used in the inner class. We leave
4171              the flag FIELD_LOCAL_ALIAS_USED around for that future
4172              use.
4173
4174              On the other hand, it only affect local inner classes,
4175              whose constructors (and finit$ call) will be featuring
4176              unecessary arguments. It's easy for a developper to keep
4177              this number of parameter down by using the `final'
4178              keyword only when necessary. For the time being, we can
4179              issue a warning on unecessary finals. FIXME */
4180           init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4181                                    wfl, init);
4182
4183           /* Register the field. The TREE_LIST holding the part
4184              initialized/initializer will be marked ARG_FINAL_P so
4185              that the created field can be marked
4186              FIELD_LOCAL_ALIAS. */
4187           list = build_tree_list (wfl, init);
4188           ARG_FINAL_P (list) = 1;
4189           register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4190         }
4191     }
4192
4193   if (!CPC_INITIALIZER_STMT (ctxp))
4194     return;
4195
4196   /* If we ever registered an alias field, insert and marker to
4197      remeber where the list ends. The second part of the list (the one
4198      featuring initialized fields) so it can be later reversed to
4199      enforce 8.5. The marker will be removed during that operation. */
4200   marker = build_tree_list (NULL_TREE, NULL_TREE);
4201   TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4202   SET_CPC_INITIALIZER_STMT (ctxp, marker);
4203 }
4204
4205 /* Can't use lookup_field () since we don't want to load the class and
4206    can't set the CLASS_LOADED_P flag */
4207
4208 static tree
4209 find_field (class, name)
4210      tree class;
4211      tree name;
4212 {
4213   tree decl;
4214   for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4215     {
4216       if (DECL_NAME (decl) == name)
4217         return decl;
4218     }
4219   return NULL_TREE;
4220 }
4221
4222 /* Wrap around lookup_field that doesn't potentially upset the value
4223    of CLASS */
4224
4225 static tree
4226 lookup_field_wrapper (class, name)
4227      tree class, name;
4228 {
4229   tree type = class;
4230   tree decl = NULL_TREE;
4231   java_parser_context_save_global ();
4232
4233   /* Last chance: if we're within the context of an inner class, we
4234      might be trying to access a local variable defined in an outer
4235      context. We try to look for it now. */
4236   if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4237     {
4238       tree new_name;
4239       MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4240       decl = lookup_field (&type, new_name);
4241       if (decl && decl != error_mark_node)
4242         FIELD_LOCAL_ALIAS_USED (decl) = 1;
4243     }
4244   if (!decl || decl == error_mark_node)
4245     {
4246       type = class;
4247       decl = lookup_field (&type, name);
4248     }
4249
4250   /* If the field still hasn't been found, try the next enclosing context. */
4251   if (!decl && INNER_CLASS_TYPE_P (class))
4252     {
4253       tree outer_type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
4254       decl = lookup_field_wrapper (outer_type, name);
4255     }
4256
4257   java_parser_context_restore_global ();
4258   return decl == error_mark_node ? NULL : decl;
4259 }
4260
4261 /* Find duplicate field within the same class declarations and report
4262    the error. Returns 1 if a duplicated field was found, 0
4263    otherwise.  */
4264
4265 static int
4266 duplicate_declaration_error_p (new_field_name, new_type, cl)
4267      tree new_field_name, new_type, cl;
4268 {
4269   /* This might be modified to work with method decl as well */
4270   tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4271   if (decl)
4272     {
4273       char *t1 = xstrdup (purify_type_name
4274                          ((TREE_CODE (new_type) == POINTER_TYPE
4275                            && TREE_TYPE (new_type) == NULL_TREE) ?
4276                           IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4277                           lang_printable_name (new_type, 1)));
4278       /* The type may not have been completed by the time we report
4279          the error */
4280       char *t2 = xstrdup (purify_type_name
4281                          ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4282                            && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4283                           IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4284                           lang_printable_name (TREE_TYPE (decl), 1)));
4285       parse_error_context
4286         (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4287          t1, IDENTIFIER_POINTER (new_field_name),
4288          t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4289          DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4290       free (t1);
4291       free (t2);
4292       return 1;
4293     }
4294   return 0;
4295 }
4296
4297 /* Field registration routine. If TYPE doesn't exist, field
4298    declarations are linked to the undefined TYPE dependency list, to
4299    be later resolved in java_complete_class () */
4300
4301 static void
4302 register_fields (flags, type, variable_list)
4303      int flags;
4304      tree type, variable_list;
4305 {
4306   tree current, saved_type;
4307   tree class_type = NULL_TREE;
4308   int saved_lineno = lineno;
4309   int must_chain = 0;
4310   tree wfl = NULL_TREE;
4311
4312   if (GET_CPC ())
4313     class_type = TREE_TYPE (GET_CPC ());
4314
4315   if (!class_type || class_type == error_mark_node)
4316     return;
4317
4318   /* If we're adding fields to interfaces, those fields are public,
4319      static, final */
4320   if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4321     {
4322       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4323                                  flags, ACC_PUBLIC, "interface field(s)");
4324       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4325                                  flags, ACC_STATIC, "interface field(s)");
4326       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4327                                  flags, ACC_FINAL, "interface field(s)");
4328       check_modifiers ("Illegal interface member modifier `%s'", flags,
4329                        INTERFACE_FIELD_MODIFIERS);
4330       flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4331     }
4332
4333   /* Obtain a suitable type for resolution, if necessary */
4334   SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4335
4336   /* If TYPE is fully resolved and we don't have a reference, make one */
4337   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4338
4339   for (current = variable_list, saved_type = type; current;
4340        current = TREE_CHAIN (current), type = saved_type)
4341     {
4342       tree real_type;
4343       tree field_decl;
4344       tree cl = TREE_PURPOSE (current);
4345       tree init = TREE_VALUE (current);
4346       tree current_name = EXPR_WFL_NODE (cl);
4347
4348       /* Can't declare non-final static fields in inner classes */
4349       if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4350           && !(flags & ACC_FINAL))
4351         parse_error_context
4352           (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4353            IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4354            lang_printable_name (class_type, 0));
4355
4356       /* Process NAME, as it may specify extra dimension(s) for it */
4357       type = build_array_from_name (type, wfl, current_name, &current_name);
4358
4359       /* Type adjustment. We may have just readjusted TYPE because
4360          the variable specified more dimensions. Make sure we have
4361          a reference if we can and don't have one already. Also
4362          change the name if we have an init. */
4363       if (type != saved_type)
4364         {
4365           PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4366           if (init)
4367             EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4368         }
4369
4370       real_type = GET_REAL_TYPE (type);
4371       /* Check for redeclarations */
4372       if (duplicate_declaration_error_p (current_name, real_type, cl))
4373         continue;
4374
4375       /* Set lineno to the line the field was found and create a
4376          declaration for it. Eventually sets the @deprecated tag flag. */
4377       if (flag_emit_xref)
4378         lineno = EXPR_WFL_LINECOL (cl);
4379       else
4380         lineno = EXPR_WFL_LINENO (cl);
4381       field_decl = add_field (class_type, current_name, real_type, flags);
4382       CHECK_DEPRECATED (field_decl);
4383
4384       /* If the field denotes a final instance variable, then we
4385          allocate a LANG_DECL_SPECIFIC part to keep track of its
4386          initialization. We also mark whether the field was
4387          initialized upon its declaration. We don't do that if the
4388          created field is an alias to a final local. */
4389       if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4390         {
4391           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4392           DECL_FIELD_FINAL_WFL (field_decl) = cl;
4393         }
4394
4395       /* If the couple initializer/initialized is marked ARG_FINAL_P,
4396          we mark the created field FIELD_LOCAL_ALIAS, so that we can
4397          hide parameters to this inner class finit$ and
4398          constructors. It also means that the field isn't final per
4399          say. */
4400       if (ARG_FINAL_P (current))
4401         {
4402           FIELD_LOCAL_ALIAS (field_decl) = 1;
4403           FIELD_FINAL (field_decl) = 0;
4404         }
4405
4406       /* Check if we must chain. */
4407       if (must_chain)
4408         register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4409
4410       /* If we have an initialization value tied to the field */
4411       if (init)
4412         {
4413           /* The field is declared static */
4414           if (flags & ACC_STATIC)
4415             {
4416               /* We include the field and its initialization part into
4417                  a list used to generate <clinit>. After <clinit> is
4418                  walked, field initializations will be processed and
4419                  fields initialized with known constants will be taken
4420                  out of <clinit> and have their DECL_INITIAL set
4421                  appropriately. */
4422               TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4423               SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4424               if (TREE_OPERAND (init, 1)
4425                   && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4426                 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4427             }
4428           /* A non-static field declared with an immediate initialization is
4429              to be initialized in <init>, if any.  This field is remembered
4430              to be processed at the time of the generation of <init>. */
4431           else
4432             {
4433               TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4434               SET_CPC_INITIALIZER_STMT (ctxp, init);
4435             }
4436           MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4437           DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4438         }
4439     }
4440   lineno = saved_lineno;
4441 }
4442
4443 /* Generate finit$, using the list of initialized fields to populate
4444    its body. finit$'s parameter(s) list is adjusted to include the
4445    one(s) used to initialized the field(s) caching outer context
4446    local(s).  */
4447
4448 static tree
4449 generate_finit (class_type)
4450      tree class_type;
4451 {
4452   int count = 0;
4453   tree list = TYPE_FINIT_STMT_LIST (class_type);
4454   tree mdecl, current, parms;
4455
4456   parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4457                                                   class_type, NULL_TREE,
4458                                                   &count);
4459   CRAFTED_PARAM_LIST_FIXUP (parms);
4460   mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4461                                     finit_identifier_node, parms);
4462   fix_method_argument_names (parms, mdecl);
4463   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4464                        mdecl, NULL_TREE);
4465   DECL_FUNCTION_NAP (mdecl) = count;
4466   start_artificial_method_body (mdecl);
4467
4468   for (current = list; current; current = TREE_CHAIN (current))
4469     java_method_add_stmt (mdecl,
4470                           build_debugable_stmt (EXPR_WFL_LINECOL (current),
4471                                                 current));
4472   end_artificial_method_body (mdecl);
4473   return mdecl;
4474 }
4475
4476 /* Generate a function to run the instance initialization code. The
4477    private method is called `instinit$'. Unless we're dealing with an
4478    anonymous class, we determine whether all ctors of CLASS_TYPE
4479    declare a checked exception in their `throws' clause in order to
4480    see whether it's necessary to encapsulate the instance initializer
4481    statements in a try/catch/rethrow sequence.  */
4482
4483 static tree
4484 generate_instinit (class_type)
4485      tree class_type;
4486 {
4487   tree current;
4488   tree compound = NULL_TREE;
4489   tree parms = tree_cons (this_identifier_node,
4490                           build_pointer_type (class_type), end_params_node);
4491   tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4492                                          void_type_node,
4493                                          instinit_identifier_node, parms);
4494
4495   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4496                        mdecl, NULL_TREE);
4497
4498   /* Gather all the statements in a compound */
4499   for (current = TYPE_II_STMT_LIST (class_type);
4500        current; current = TREE_CHAIN (current))
4501     compound = add_stmt_to_compound (compound, NULL_TREE, current);
4502
4503   /* We need to encapsulate COMPOUND by a try/catch statement to
4504      rethrow exceptions that might occur in the instance initializer.
4505      We do that only if all ctors of CLASS_TYPE are set to catch a
4506      checked exception. This doesn't apply to anonymous classes (since
4507      they don't have declared ctors.) */
4508   if (!ANONYMOUS_CLASS_P (class_type) &&
4509       ctors_unchecked_throws_clause_p (class_type))
4510     {
4511       compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4512                                              build1 (THROW_EXPR, NULL_TREE,
4513                                                      build_wfl_node (wpv_id)));
4514       DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4515                                                       exception_type_node);
4516     }
4517
4518   start_artificial_method_body (mdecl);
4519   java_method_add_stmt (mdecl, compound);
4520   end_artificial_method_body (mdecl);
4521
4522   return mdecl;
4523 }
4524
4525 /* FIXME */
4526 static tree
4527 build_instinit_invocation (class_type)
4528      tree class_type;
4529 {
4530   tree to_return = NULL_TREE;
4531
4532   if (TYPE_II_STMT_LIST (class_type))
4533     {
4534       tree parm = build_tree_list (NULL_TREE,
4535                                    build_wfl_node (this_identifier_node));
4536       to_return =
4537         build_method_invocation (build_wfl_node (instinit_identifier_node),
4538                                  parm);
4539     }
4540   return to_return;
4541 }
4542
4543 /* Shared accros method_declarator and method_header to remember the
4544    patch stage that was reached during the declaration of the method.
4545    A method DECL is built differently is there is no patch
4546    (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4547    pending on the currently defined method.  */
4548
4549 static int patch_stage;
4550
4551 /* Check the method declaration and add the method to its current
4552    class.  If the argument list is known to contain incomplete types,
4553    the method is partially added and the registration will be resume
4554    once the method arguments resolved. If TYPE is NULL, we're dealing
4555    with a constructor.  */
4556
4557 static tree
4558 method_header (flags, type, mdecl, throws)
4559      int flags;
4560      tree type, mdecl, throws;
4561 {
4562   tree type_wfl = NULL_TREE;
4563   tree meth_name = NULL_TREE;
4564   tree current, orig_arg, this_class = NULL;
4565   tree id, meth;
4566   int saved_lineno;
4567   int constructor_ok = 0, must_chain;
4568   int count;
4569
4570   if (mdecl == error_mark_node)
4571     return error_mark_node;
4572   meth = TREE_VALUE (mdecl);
4573   id = TREE_PURPOSE (mdecl);
4574
4575   check_modifiers_consistency (flags);
4576
4577   if (GET_CPC ())
4578     this_class = TREE_TYPE (GET_CPC ());
4579
4580   if (!this_class || this_class == error_mark_node)
4581     return NULL_TREE;
4582
4583   /* There are some forbidden modifiers for an abstract method and its
4584      class must be abstract as well.  */
4585   if (type && (flags & ACC_ABSTRACT))
4586     {
4587       ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4588       ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4589       ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4590       ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4591       ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED, id, "Synchronized");
4592       ABSTRACT_CHECK (flags, ACC_STRICT, id, "Strictfp");
4593       if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4594           && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4595         parse_error_context
4596           (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4597            IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4598            IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4599     }
4600
4601   /* Things to be checked when declaring a constructor */
4602   if (!type)
4603     {
4604       int ec = java_error_count;
4605       /* 8.6: Constructor declarations: we might be trying to define a
4606          method without specifying a return type. */
4607       if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4608         parse_error_context
4609           (id, "Invalid method declaration, return type required");
4610       /* 8.6.3: Constructor modifiers */
4611       else
4612         {
4613           JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4614           JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4615           JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4616           JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4617           JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4618           JCONSTRUCTOR_CHECK (flags, ACC_STRICT, id, "strictfp");
4619         }
4620       /* If we found error here, we don't consider it's OK to tread
4621          the method definition as a constructor, for the rest of this
4622          function */
4623       if (ec == java_error_count)
4624         constructor_ok = 1;
4625     }
4626
4627   /* Method declared within the scope of an interface are implicitly
4628      abstract and public. Conflicts with other erroneously provided
4629      modifiers are checked right after. */
4630
4631   if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4632     {
4633       /* If FLAGS isn't set because of a modifier, turn the
4634          corresponding modifier WFL to NULL so we issue a warning on
4635          the obsolete use of the modifier */
4636       if (!(flags & ACC_PUBLIC))
4637         MODIFIER_WFL (PUBLIC_TK) = NULL;
4638       if (!(flags & ACC_ABSTRACT))
4639         MODIFIER_WFL (ABSTRACT_TK) = NULL;
4640       flags |= ACC_PUBLIC;
4641       flags |= ACC_ABSTRACT;
4642     }
4643
4644   /* Inner class can't declare static methods */
4645   if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4646     {
4647       parse_error_context
4648         (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4649          IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4650          lang_printable_name (this_class, 0));
4651     }
4652
4653   /* Modifiers context reset moved up, so abstract method declaration
4654      modifiers can be later checked.  */
4655
4656   /* Set constructor returned type to void and method name to <init>,
4657      unless we found an error identifier the constructor (in which
4658      case we retain the original name) */
4659   if (!type)
4660     {
4661       type = void_type_node;
4662       if (constructor_ok)
4663         meth_name = init_identifier_node;
4664     }
4665   else
4666     meth_name = EXPR_WFL_NODE (id);
4667
4668   /* Do the returned type resolution and registration if necessary */
4669   SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4670
4671   if (meth_name)
4672     type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4673   EXPR_WFL_NODE (id) = meth_name;
4674   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4675
4676   if (must_chain)
4677     {
4678       patch_stage = JDEP_METHOD_RETURN;
4679       register_incomplete_type (patch_stage, type_wfl, id, type);
4680       TREE_TYPE (meth) = GET_REAL_TYPE (type);
4681     }
4682   else
4683     TREE_TYPE (meth) = type;
4684
4685   saved_lineno = lineno;
4686   /* When defining an abstract or interface method, the curly
4687      bracket at level 1 doesn't exist because there is no function
4688      body */
4689   lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4690             EXPR_WFL_LINENO (id));
4691
4692   /* Remember the original argument list */
4693   orig_arg = TYPE_ARG_TYPES (meth);
4694
4695   if (patch_stage)              /* includes ret type and/or all args */
4696     {
4697       jdep *jdep;
4698       meth = add_method_1 (this_class, flags, meth_name, meth);
4699       /* Patch for the return type */
4700       if (patch_stage == JDEP_METHOD_RETURN)
4701         {
4702           jdep = CLASSD_LAST (ctxp->classd_list);
4703           JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4704         }
4705       /* This is the stop JDEP. METH allows the function's signature
4706          to be computed. */
4707       register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4708     }
4709   else
4710     meth = add_method (this_class, flags, meth_name,
4711                        build_java_signature (meth));
4712
4713   /* Remember final parameters */
4714   MARK_FINAL_PARMS (meth, orig_arg);
4715
4716   /* Fix the method argument list so we have the argument name
4717      information */
4718   fix_method_argument_names (orig_arg, meth);
4719
4720   /* Register the parameter number and re-install the current line
4721      number */
4722   DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4723   lineno = saved_lineno;
4724
4725   /* Register exception specified by the `throws' keyword for
4726      resolution and set the method decl appropriate field to the list.
4727      Note: the grammar ensures that what we get here are class
4728      types. */
4729   if (throws)
4730     {
4731       throws = nreverse (throws);
4732       for (current = throws; current; current = TREE_CHAIN (current))
4733         {
4734           register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4735                                     NULL_TREE, NULL_TREE);
4736           JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4737             &TREE_VALUE (current);
4738         }
4739       DECL_FUNCTION_THROWS (meth) = throws;
4740     }
4741
4742   if (TREE_TYPE (GET_CPC ()) != object_type_node)
4743     DECL_FUNCTION_WFL (meth) = id;
4744
4745   /* Set the flag if we correctly processed a constructor */
4746   if (constructor_ok)
4747     {
4748       DECL_CONSTRUCTOR_P (meth) = 1;
4749       /* Compute and store the number of artificial parameters declared
4750          for this constructor */
4751       for (count = 0, current = TYPE_FIELDS (this_class); current;
4752            current = TREE_CHAIN (current))
4753         if (FIELD_LOCAL_ALIAS (current))
4754           count++;
4755       DECL_FUNCTION_NAP (meth) = count;
4756     }
4757
4758   /* Eventually set the @deprecated tag flag */
4759   CHECK_DEPRECATED (meth);
4760
4761   /* If doing xref, store column and line number information instead
4762      of the line number only. */
4763   if (flag_emit_xref)
4764     DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4765
4766   return meth;
4767 }
4768
4769 static void
4770 fix_method_argument_names (orig_arg, meth)
4771     tree orig_arg, meth;
4772 {
4773   tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4774   if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4775     {
4776       TREE_PURPOSE (arg) = this_identifier_node;
4777       arg = TREE_CHAIN (arg);
4778     }
4779   while (orig_arg != end_params_node)
4780     {
4781       TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4782       orig_arg = TREE_CHAIN (orig_arg);
4783       arg = TREE_CHAIN (arg);
4784     }
4785 }
4786
4787 /* Complete the method declaration with METHOD_BODY.  */
4788
4789 static void
4790 finish_method_declaration (method_body)
4791      tree method_body;
4792 {
4793   int flags;
4794
4795   if (!current_function_decl)
4796     return;
4797
4798   flags = get_access_flags_from_decl (current_function_decl);
4799
4800   /* 8.4.5 Method Body */
4801   if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4802     {
4803       tree name = DECL_NAME (current_function_decl);
4804       parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4805                            "%s method `%s' can't have a body defined",
4806                            (METHOD_NATIVE (current_function_decl) ?
4807                             "Native" : "Abstract"),
4808                            IDENTIFIER_POINTER (name));
4809       method_body = NULL_TREE;
4810     }
4811   else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4812     {
4813       tree name = DECL_NAME (current_function_decl);
4814       parse_error_context
4815         (DECL_FUNCTION_WFL (current_function_decl),
4816          "Non native and non abstract method `%s' must have a body defined",
4817          IDENTIFIER_POINTER (name));
4818       method_body = NULL_TREE;
4819     }
4820
4821   if (flag_emit_class_files && method_body
4822       && TREE_CODE (method_body) == NOP_EXPR
4823       && TREE_TYPE (current_function_decl)
4824       && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4825     method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4826
4827   BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4828   maybe_absorb_scoping_blocks ();
4829   /* Exit function's body */
4830   exit_block ();
4831   /* Merge last line of the function with first line, directly in the
4832      function decl. It will be used to emit correct debug info. */
4833   if (!flag_emit_xref)
4834     DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4835
4836   /* Since function's argument's list are shared, reset the
4837      ARG_FINAL_P parameter that might have been set on some of this
4838      function parameters. */
4839   UNMARK_FINAL_PARMS (current_function_decl);
4840
4841   /* So we don't have an irrelevant function declaration context for
4842      the next static block we'll see. */
4843   current_function_decl = NULL_TREE;
4844 }
4845
4846 /* Build a an error message for constructor circularity errors.  */
4847
4848 static char *
4849 constructor_circularity_msg (from, to)
4850      tree from, to;
4851 {
4852   static char string [4096];
4853   char *t = xstrdup (lang_printable_name (from, 0));
4854   sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4855   free (t);
4856   return string;
4857 }
4858
4859 /* Verify a circular call to METH. Return 1 if an error is found, 0
4860    otherwise.  */
4861
4862 static GTY(()) tree vcc_list;
4863 static int
4864 verify_constructor_circularity (meth, current)
4865      tree meth, current;
4866 {
4867   tree c;
4868
4869   for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4870     {
4871       if (TREE_VALUE (c) == meth)
4872         {
4873           char *t;
4874           if (vcc_list)
4875             {
4876               tree liste;
4877               vcc_list = nreverse (vcc_list);
4878               for (liste = vcc_list; liste; liste = TREE_CHAIN (liste))
4879                 {
4880                   parse_error_context
4881                     (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4882                      constructor_circularity_msg
4883                       (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4884                   java_error_count--;
4885                 }
4886             }
4887           t = xstrdup (lang_printable_name (meth, 0));
4888           parse_error_context (TREE_PURPOSE (c),
4889                                "%s: recursive invocation of constructor `%s'",
4890                                constructor_circularity_msg (current, meth), t);
4891           free (t);
4892           vcc_list = NULL_TREE;
4893           return 1;
4894         }
4895     }
4896   for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4897     {
4898       vcc_list = tree_cons (c, current, vcc_list);
4899       if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4900         return 1;
4901       vcc_list = TREE_CHAIN (vcc_list);
4902     }
4903   return 0;
4904 }
4905
4906 /* Check modifiers that can be declared but exclusively */
4907
4908 static void
4909 check_modifiers_consistency (flags)
4910      int flags;
4911 {
4912   int acc_count = 0;
4913   tree cl = NULL_TREE;
4914
4915   THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4916   THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4917   THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4918   if (acc_count > 1)
4919     parse_error_context
4920       (cl, "Inconsistent member declaration.  At most one of `public', `private', or `protected' may be specified");
4921
4922   acc_count = 0;
4923   cl = NULL_TREE;
4924   THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4925   THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4926   if (acc_count > 1)
4927     parse_error_context (cl,
4928                          "Inconsistent member declaration.  At most one of `final' or `volatile' may be specified");
4929 }
4930
4931 /* Check the methode header METH for abstract specifics features */
4932
4933 static void
4934 check_abstract_method_header (meth)
4935      tree meth;
4936 {
4937   int flags = get_access_flags_from_decl (meth);
4938
4939   OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4940                               ACC_ABSTRACT, "abstract method",
4941                               IDENTIFIER_POINTER (DECL_NAME (meth)));
4942   OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4943                               ACC_PUBLIC, "abstract method",
4944                               IDENTIFIER_POINTER (DECL_NAME (meth)));
4945
4946   check_modifiers ("Illegal modifier `%s' for interface method",
4947                   flags, INTERFACE_METHOD_MODIFIERS);
4948 }
4949
4950 /* Create a FUNCTION_TYPE node and start augmenting it with the
4951    declared function arguments. Arguments type that can't be resolved
4952    are left as they are, but the returned node is marked as containing
4953    incomplete types.  */
4954
4955 static tree
4956 method_declarator (id, list)
4957      tree id, list;
4958 {
4959   tree arg_types = NULL_TREE, current, node;
4960   tree meth = make_node (FUNCTION_TYPE);
4961   jdep *jdep;
4962
4963   patch_stage = JDEP_NO_PATCH;
4964
4965   if (GET_CPC () == error_mark_node)
4966     return error_mark_node;
4967
4968   /* If we're dealing with an inner class constructor, we hide the
4969      this$<n> decl in the name field of its parameter declaration.  We
4970      also might have to hide the outer context local alias
4971      initializers. Not done when the class is a toplevel class. */
4972   if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4973       && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4974     {
4975       tree aliases_list, type, thisn;
4976       /* First the aliases, linked to the regular parameters */
4977       aliases_list =
4978         build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4979                                                 TREE_TYPE (GET_CPC ()),
4980                                                 NULL_TREE, NULL);
4981       list = chainon (nreverse (aliases_list), list);
4982
4983       /* Then this$<n> */
4984       type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4985       thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4986       list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4987                         list);
4988     }
4989
4990   for (current = list; current; current = TREE_CHAIN (current))
4991     {
4992       int must_chain = 0;
4993       tree wfl_name = TREE_PURPOSE (current);
4994       tree type = TREE_VALUE (current);
4995       tree name = EXPR_WFL_NODE (wfl_name);
4996       tree already, arg_node;
4997       tree type_wfl = NULL_TREE;
4998       tree real_type;
4999
5000       /* Obtain a suitable type for resolution, if necessary */
5001       SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
5002
5003       /* Process NAME, as it may specify extra dimension(s) for it */
5004       type = build_array_from_name (type, type_wfl, name, &name);
5005       EXPR_WFL_NODE (wfl_name) = name;
5006
5007       real_type = GET_REAL_TYPE (type);
5008       if (TREE_CODE (real_type) == RECORD_TYPE)
5009         {
5010           real_type = promote_type (real_type);
5011           if (TREE_CODE (type) == TREE_LIST)
5012             TREE_PURPOSE (type) = real_type;
5013         }
5014
5015       /* Check redefinition */
5016       for (already = arg_types; already; already = TREE_CHAIN (already))
5017         if (TREE_PURPOSE (already) == name)
5018           {
5019             parse_error_context
5020               (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
5021                IDENTIFIER_POINTER (name),
5022                IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
5023             break;
5024           }
5025
5026       /* If we've an incomplete argument type, we know there is a location
5027          to patch when the type get resolved, later.  */
5028       jdep = NULL;
5029       if (must_chain)
5030         {
5031           patch_stage = JDEP_METHOD;
5032           type = register_incomplete_type (patch_stage,
5033                                            type_wfl, wfl_name, type);
5034           jdep = CLASSD_LAST (ctxp->classd_list);
5035           JDEP_MISC (jdep) = id;
5036         }
5037
5038       /* The argument node: a name and a (possibly) incomplete type.  */
5039       arg_node = build_tree_list (name, real_type);
5040       /* Remeber arguments declared final. */
5041       ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5042
5043       if (jdep)
5044         JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5045       TREE_CHAIN (arg_node) = arg_types;
5046       arg_types = arg_node;
5047     }
5048   TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5049   node = build_tree_list (id, meth);
5050   return node;
5051 }
5052
5053 static int
5054 unresolved_type_p (wfl, returned)
5055      tree wfl;
5056      tree *returned;
5057
5058 {
5059   if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5060     {
5061       if (returned)
5062         {
5063           tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5064           if (decl && current_class && (decl == TYPE_NAME (current_class)))
5065             *returned = TREE_TYPE (decl);
5066           else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5067             *returned = TREE_TYPE (GET_CPC ());
5068           else
5069             *returned = NULL_TREE;
5070         }
5071       return 1;
5072     }
5073   if (returned)
5074     *returned = wfl;
5075   return 0;
5076 }
5077
5078 /* From NAME, build a qualified identifier node using the
5079    qualification from the current package definition. */
5080
5081 static tree
5082 parser_qualified_classname (name)
5083      tree name;
5084 {
5085   tree nested_class_name;
5086
5087   if ((nested_class_name = maybe_make_nested_class_name (name)))
5088     return nested_class_name;
5089
5090   if (ctxp->package)
5091     return merge_qualified_name (ctxp->package, name);
5092   else
5093     return name;
5094 }
5095
5096 /* Called once the type a interface extends is resolved. Returns 0 if
5097    everything is OK.  */
5098
5099 static int
5100 parser_check_super_interface (super_decl, this_decl, this_wfl)
5101      tree super_decl, this_decl, this_wfl;
5102 {
5103   tree super_type = TREE_TYPE (super_decl);
5104
5105   /* Has to be an interface */
5106   if (!CLASS_INTERFACE (super_decl))
5107     {
5108       parse_error_context
5109         (this_wfl, "%s `%s' can't implement/extend %s `%s'",
5110          (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5111           "Interface" : "Class"),
5112          IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5113          (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5114          IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5115       return 1;
5116     }
5117
5118   /* Check top-level interface access. Inner classes are subject to member
5119      access rules (6.6.1). */
5120   if (! INNER_CLASS_P (super_type)
5121       && check_pkg_class_access (DECL_NAME (super_decl),
5122                                  lookup_cl (this_decl), true))
5123     return 1;
5124
5125   SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5126                           IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5127                           IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5128   return 0;
5129 }
5130
5131 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5132    0 if everthing is OK.  */
5133
5134 static int
5135 parser_check_super (super_decl, this_decl, wfl)
5136      tree super_decl, this_decl, wfl;
5137 {
5138   tree super_type = TREE_TYPE (super_decl);
5139
5140   /* SUPER should be a CLASS (neither an array nor an interface) */
5141   if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5142     {
5143       parse_error_context
5144         (wfl, "Class `%s' can't subclass %s `%s'",
5145          IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5146          (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5147          IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5148       return 1;
5149     }
5150
5151   if (CLASS_FINAL (TYPE_NAME (super_type)))
5152     {
5153       parse_error_context (wfl, "Can't subclass final classes: %s",
5154                            IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5155       return 1;
5156     }
5157
5158   /* Check top-level class scope. Inner classes are subject to member access
5159      rules (6.6.1). */
5160   if (! INNER_CLASS_P (super_type)
5161       && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true)))
5162     return 1;
5163
5164   SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5165                           IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5166                           IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5167   return 0;
5168 }
5169
5170 /* Create a new dependency list and link it (in a LIFO manner) to the
5171    CTXP list of type dependency list.  */
5172
5173 static void
5174 create_jdep_list (ctxp)
5175      struct parser_ctxt *ctxp;
5176 {
5177   jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
5178   new->first = new->last = NULL;
5179   new->next = ctxp->classd_list;
5180   ctxp->classd_list = new;
5181 }
5182
5183 static jdeplist *
5184 reverse_jdep_list (ctxp)
5185      struct parser_ctxt *ctxp;
5186 {
5187   register jdeplist *prev = NULL, *current, *next;
5188   for (current = ctxp->classd_list; current; current = next)
5189     {
5190       next = current->next;
5191       current->next = prev;
5192       prev = current;
5193     }
5194   return prev;
5195 }
5196
5197 /* Create a fake pointer based on the ID stored in
5198    TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5199    registered again. */
5200
5201 static tree
5202 obtain_incomplete_type (type_name)
5203      tree type_name;
5204 {
5205   tree ptr = NULL_TREE, name;
5206
5207   if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5208     name = EXPR_WFL_NODE (type_name);
5209   else if (INCOMPLETE_TYPE_P (type_name))
5210     name = TYPE_NAME (type_name);
5211   else
5212     abort ();
5213
5214   BUILD_PTR_FROM_NAME (ptr, name);
5215   layout_type (ptr);
5216
5217   return ptr;
5218 }
5219
5220 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5221    non NULL instead of computing a new fake type based on WFL. The new
5222    dependency is inserted in the current type dependency list, in FIFO
5223    manner.  */
5224
5225 static tree
5226 register_incomplete_type (kind, wfl, decl, ptr)
5227      int kind;
5228      tree wfl, decl, ptr;
5229 {
5230   jdep *new = (jdep *)xmalloc (sizeof (jdep));
5231
5232   if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5233     ptr = obtain_incomplete_type (wfl);
5234
5235   JDEP_KIND (new) = kind;
5236   JDEP_DECL (new) = decl;
5237   JDEP_TO_RESOLVE (new) = ptr;
5238   JDEP_WFL (new) = wfl;
5239   JDEP_CHAIN (new) = NULL;
5240   JDEP_MISC (new) = NULL_TREE;
5241   /* For some dependencies, set the enclosing class of the current
5242      class to be the enclosing context */
5243   if ((kind == JDEP_INTERFACE  || kind == JDEP_ANONYMOUS)
5244       && GET_ENCLOSING_CPC ())
5245     JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5246   else if (kind == JDEP_SUPER)
5247     JDEP_ENCLOSING (new) = (GET_ENCLOSING_CPC () ?
5248                             TREE_VALUE (GET_ENCLOSING_CPC ()) : NULL_TREE);
5249   else
5250     JDEP_ENCLOSING (new) = GET_CPC ();
5251   JDEP_GET_PATCH (new) = (tree *)NULL;
5252
5253   JDEP_INSERT (ctxp->classd_list, new);
5254
5255   return ptr;
5256 }
5257
5258 /* This checks for circular references with innerclasses. We start
5259    from SOURCE and should never reach TARGET. Extended/implemented
5260    types in SOURCE have their enclosing context checked not to reach
5261    TARGET. When the last enclosing context of SOURCE is reached, its
5262    extended/implemented types are also checked not to reach TARGET.
5263    In case of error, WFL of the offending type is returned; NULL_TREE
5264    otherwise.  */
5265
5266 static tree
5267 check_inner_circular_reference (source, target)
5268      tree source;
5269      tree target;
5270 {
5271   tree basetype_vec = TYPE_BINFO_BASETYPES (source);
5272   tree ctx, cl;
5273   int i;
5274
5275   if (!basetype_vec)
5276     return NULL_TREE;
5277
5278   for (i = 0; i < TREE_VEC_LENGTH (basetype_vec); i++)
5279     {
5280       tree su;
5281
5282       /* We can end up with a NULL_TREE or an incomplete type here if
5283          we encountered previous type resolution errors. It's safe to
5284          simply ignore these cases.  */
5285       if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
5286         continue;
5287       su = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
5288       if (INCOMPLETE_TYPE_P (su))
5289         continue;
5290
5291       if (inherits_from_p (su, target))
5292         return lookup_cl (TYPE_NAME (su));
5293
5294       for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5295         {
5296           /* An enclosing context shouldn't be TARGET */
5297           if (ctx == TYPE_NAME (target))
5298             return lookup_cl (TYPE_NAME (su));
5299
5300           /* When we reach the enclosing last context, start a check
5301              on it, with the same target */
5302           if (! DECL_CONTEXT (ctx) &&
5303               (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5304             return cl;
5305         }
5306     }
5307   return NULL_TREE;
5308 }
5309
5310 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5311    offending type if a circularity is detected. NULL_TREE is returned
5312    otherwise. TYPE can be an interface or a class.   */
5313
5314 static tree
5315 check_circular_reference (type)
5316      tree type;
5317 {
5318   tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5319   int i;
5320
5321   if (!basetype_vec)
5322     return NULL_TREE;
5323
5324   if (! CLASS_INTERFACE (TYPE_NAME (type)))
5325     {
5326       if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5327         return lookup_cl (TYPE_NAME (type));
5328       return NULL_TREE;
5329     }
5330
5331   for (i = 0; i < TREE_VEC_LENGTH (basetype_vec); i++)
5332     {
5333       tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5334       if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5335           && interface_of_p (type, BINFO_TYPE (vec_elt)))
5336         return lookup_cl (TYPE_NAME (BINFO_TYPE (vec_elt)));
5337     }
5338   return NULL_TREE;
5339 }
5340
5341 void
5342 java_check_circular_reference ()
5343 {
5344   tree current;
5345   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5346     {
5347       tree type = TREE_TYPE (current);
5348       tree cl;
5349
5350       cl = check_circular_reference (type);
5351       if (! cl)
5352         cl = check_inner_circular_reference (type, type);
5353       if (cl)
5354         parse_error_context (cl, "Cyclic class inheritance%s",
5355                              (cyclic_inheritance_report ?
5356                               cyclic_inheritance_report : ""));
5357     }
5358 }
5359
5360 /* Augment the parameter list PARM with parameters crafted to
5361    initialize outer context locals aliases. Through ARTIFICIAL, a
5362    count is kept of the number of crafted parameters. MODE governs
5363    what eventually gets created: something suitable for a function
5364    creation or a function invocation, either the constructor or
5365    finit$.  */
5366
5367 static tree
5368 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5369     int mode;
5370     tree class_type, parm;
5371     int *artificial;
5372 {
5373   tree field;
5374   tree additional_parms = NULL_TREE;
5375
5376   for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5377     if (FIELD_LOCAL_ALIAS (field))
5378       {
5379         const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5380         tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5381         tree mangled_id;
5382
5383         switch (mode)
5384           {
5385           case AIPL_FUNCTION_DECLARATION:
5386             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5387                                                          &buffer [4]);
5388             purpose = build_wfl_node (mangled_id);
5389             if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5390               value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5391             else
5392               value = TREE_TYPE (field);
5393             break;
5394
5395           case AIPL_FUNCTION_CREATION:
5396             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5397                                                          &buffer [4]);
5398             value = TREE_TYPE (field);
5399             break;
5400
5401           case AIPL_FUNCTION_FINIT_INVOCATION:
5402             MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5403                                                          &buffer [4]);
5404             /* Now, this is wrong. purpose should always be the NAME
5405                of something and value its matching value (decl, type,
5406                etc...) FIXME -- but there is a lot to fix. */
5407
5408             /* When invoked for this kind of operation, we already
5409                know whether a field is used or not. */
5410             purpose = TREE_TYPE (field);
5411             value = build_wfl_node (mangled_id);
5412             break;
5413
5414           case AIPL_FUNCTION_CTOR_INVOCATION:
5415             /* There are two case: the constructor invokation happends
5416                outside the local inner, in which case, locales from the outer
5417                context are directly used.
5418
5419                Otherwise, we fold to using the alias directly. */
5420             if (class_type == current_class)
5421               value = field;
5422             else
5423               {
5424                 name = get_identifier (&buffer[4]);
5425                 value = IDENTIFIER_LOCAL_VALUE (name);
5426               }
5427             break;
5428           }
5429         additional_parms = tree_cons (purpose, value, additional_parms);
5430         if (artificial)
5431           *artificial +=1;
5432       }
5433   if (additional_parms)
5434     {
5435       if (ANONYMOUS_CLASS_P (class_type)
5436           && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5437         additional_parms = nreverse (additional_parms);
5438       parm = chainon (additional_parms, parm);
5439     }
5440
5441    return parm;
5442 }
5443
5444 /* Craft a constructor for CLASS_DECL -- what we should do when none
5445    where found. ARGS is non NULL when a special signature must be
5446    enforced. This is the case for anonymous classes.  */
5447
5448 static tree
5449 craft_constructor (class_decl, args)
5450      tree class_decl, args;
5451 {
5452   tree class_type = TREE_TYPE (class_decl);
5453   tree parm = NULL_TREE;
5454   int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5455                ACC_PUBLIC : 0);
5456   int i = 0, artificial = 0;
5457   tree decl, ctor_name;
5458   char buffer [80];
5459
5460   /* The constructor name is <init> unless we're dealing with an
5461      anonymous class, in which case the name will be fixed after having
5462      be expanded. */
5463   if (ANONYMOUS_CLASS_P (class_type))
5464     ctor_name = DECL_NAME (class_decl);
5465   else
5466     ctor_name = init_identifier_node;
5467
5468   /* If we're dealing with an inner class constructor, we hide the
5469      this$<n> decl in the name field of its parameter declaration. */
5470   if (PURE_INNER_CLASS_TYPE_P (class_type))
5471     {
5472       tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5473       parm = tree_cons (build_current_thisn (class_type),
5474                         build_pointer_type (type), parm);
5475
5476       /* Some more arguments to be hidden here. The values of the local
5477          variables of the outer context that the inner class needs to see. */
5478       parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5479                                                      class_type, parm,
5480                                                      &artificial);
5481     }
5482
5483   /* Then if there are any args to be enforced, enforce them now */
5484   for (; args && args != end_params_node; args = TREE_CHAIN (args))
5485     {
5486       sprintf (buffer, "parm%d", i++);
5487       parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5488     }
5489
5490   CRAFTED_PARAM_LIST_FIXUP (parm);
5491   decl = create_artificial_method (class_type, flags, void_type_node,
5492                                    ctor_name, parm);
5493   fix_method_argument_names (parm, decl);
5494   /* Now, mark the artificial parameters. */
5495   DECL_FUNCTION_NAP (decl) = artificial;
5496   DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5497   return decl;
5498 }
5499
5500
5501 /* Fix the constructors. This will be called right after circular
5502    references have been checked. It is necessary to fix constructors
5503    early even if no code generation will take place for that class:
5504    some generated constructor might be required by the class whose
5505    compilation triggered this one to be simply loaded.  */
5506
5507 void
5508 java_fix_constructors ()
5509 {
5510   tree current;
5511
5512   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5513     {
5514       tree class_type = TREE_TYPE (current);
5515       int saw_ctor = 0;
5516       tree decl;
5517
5518       if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5519         continue;
5520
5521       current_class = class_type;
5522       for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5523         {
5524           if (DECL_CONSTRUCTOR_P (decl))
5525             {
5526               fix_constructors (decl);
5527               saw_ctor = 1;
5528             }
5529         }
5530
5531       /* Anonymous class constructor can't be generated that early. */
5532       if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5533         craft_constructor (current, NULL_TREE);
5534     }
5535 }
5536
5537 /* safe_layout_class just makes sure that we can load a class without
5538    disrupting the current_class, input_file, lineno, etc, information
5539    about the class processed currently.  */
5540
5541 void
5542 safe_layout_class (class)
5543      tree class;
5544 {
5545   tree save_current_class = current_class;
5546   const char *save_input_filename = input_filename;
5547   int save_lineno = lineno;
5548
5549   layout_class (class);
5550
5551   current_class = save_current_class;
5552   input_filename = save_input_filename;
5553   lineno = save_lineno;
5554 }
5555
5556 static tree
5557 jdep_resolve_class (dep)
5558      jdep *dep;
5559 {
5560   tree decl;
5561
5562   if (JDEP_RESOLVED_P (dep))
5563     decl = JDEP_RESOLVED_DECL (dep);
5564   else
5565     {
5566       decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5567                             JDEP_DECL (dep), JDEP_WFL (dep));
5568       JDEP_RESOLVED (dep, decl);
5569     }
5570
5571   if (!decl)
5572     complete_class_report_errors (dep);
5573   else if (PURE_INNER_CLASS_DECL_P (decl))
5574     {
5575       tree inner = TREE_TYPE (decl);
5576       if (! CLASS_LOADED_P (inner))
5577         {
5578           safe_layout_class (inner);
5579           if (TYPE_SIZE (inner) == error_mark_node)
5580             TYPE_SIZE (inner) = NULL_TREE;
5581         }
5582       check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5583     }
5584   return decl;
5585 }
5586
5587 /* Complete unsatisfied class declaration and their dependencies */
5588
5589 void
5590 java_complete_class ()
5591 {
5592   tree cclass;
5593   jdeplist *cclassd;
5594   int error_found;
5595   tree type;
5596
5597   /* Process imports */
5598   process_imports ();
5599
5600   /* Rever things so we have the right order */
5601   ctxp->class_list = nreverse (ctxp->class_list);
5602   ctxp->classd_list = reverse_jdep_list (ctxp);
5603
5604   for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5605        cclass && cclassd;
5606        cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5607     {
5608       jdep *dep;
5609
5610       /* We keep the compilation unit imports in the class so that
5611          they can be used later to resolve type dependencies that
5612          aren't necessary to solve now. */
5613       TYPE_IMPORT_LIST (TREE_TYPE (cclass)) = ctxp->import_list;
5614       TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (cclass)) = ctxp->import_demand_list;
5615
5616       for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5617         {
5618           tree decl;
5619           if (!(decl = jdep_resolve_class (dep)))
5620             continue;
5621
5622           /* Now it's time to patch */
5623           switch (JDEP_KIND (dep))
5624             {
5625             case JDEP_SUPER:
5626               /* Simply patch super */
5627               if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5628                 continue;
5629               BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5630                 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5631               break;
5632
5633             case JDEP_FIELD:
5634               {
5635                 /* We do part of the job done in add_field */
5636                 tree field_decl = JDEP_DECL (dep);
5637                 tree field_type = TREE_TYPE (decl);
5638                 if (TREE_CODE (field_type) == RECORD_TYPE)
5639                   field_type = promote_type (field_type);
5640                 TREE_TYPE (field_decl) = field_type;
5641                 DECL_ALIGN (field_decl) = 0;
5642                 DECL_USER_ALIGN (field_decl) = 0;
5643                 layout_decl (field_decl, 0);
5644                 SOURCE_FRONTEND_DEBUG
5645                   (("Completed field/var decl `%s' with `%s'",
5646                     IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5647                     IDENTIFIER_POINTER (DECL_NAME (decl))));
5648                 break;
5649               }
5650             case JDEP_METHOD:   /* We start patching a method */
5651             case JDEP_METHOD_RETURN:
5652               error_found = 0;
5653               while (1)
5654                 {
5655                   if (decl)
5656                     {
5657                       type = TREE_TYPE(decl);
5658                       if (TREE_CODE (type) == RECORD_TYPE)
5659                         type = promote_type (type);
5660                       JDEP_APPLY_PATCH (dep, type);
5661                       SOURCE_FRONTEND_DEBUG
5662                         (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5663                            "Completing fct `%s' with ret type `%s'":
5664                            "Completing arg `%s' with type `%s'"),
5665                           IDENTIFIER_POINTER (EXPR_WFL_NODE
5666                                               (JDEP_DECL_WFL (dep))),
5667                           IDENTIFIER_POINTER (DECL_NAME (decl))));
5668                     }
5669                   else
5670                     error_found = 1;
5671                   dep = JDEP_CHAIN (dep);
5672                   if (JDEP_KIND (dep) == JDEP_METHOD_END)
5673                     break;
5674                   else
5675                     decl = jdep_resolve_class (dep);
5676                 }
5677               if (!error_found)
5678                 {
5679                   tree mdecl = JDEP_DECL (dep), signature;
5680                   /* Recompute and reset the signature, check first that
5681                      all types are now defined. If they're not,
5682                      don't build the signature. */
5683                   if (check_method_types_complete (mdecl))
5684                     {
5685                       signature = build_java_signature (TREE_TYPE (mdecl));
5686                       set_java_signature (TREE_TYPE (mdecl), signature);
5687                     }
5688                 }
5689               else
5690                 continue;
5691               break;
5692
5693             case JDEP_INTERFACE:
5694               if (parser_check_super_interface (decl, JDEP_DECL (dep),
5695                                                 JDEP_WFL (dep)))
5696                 continue;
5697               parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5698               break;
5699
5700             case JDEP_PARM:
5701             case JDEP_VARIABLE:
5702               type = TREE_TYPE(decl);
5703               if (TREE_CODE (type) == RECORD_TYPE)
5704                 type = promote_type (type);
5705               JDEP_APPLY_PATCH (dep, type);
5706               break;
5707
5708             case JDEP_TYPE:
5709               JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5710               SOURCE_FRONTEND_DEBUG
5711                 (("Completing a random type dependency on a '%s' node",
5712                   tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5713               break;
5714
5715             case JDEP_EXCEPTION:
5716               JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5717               SOURCE_FRONTEND_DEBUG
5718                 (("Completing `%s' `throws' argument node",
5719                   IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5720               break;
5721
5722             case JDEP_ANONYMOUS:
5723               patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5724               break;
5725
5726             default:
5727               abort ();
5728             }
5729         }
5730     }
5731   return;
5732 }
5733
5734 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5735    array.  */
5736
5737 static tree
5738 resolve_class (enclosing, class_type, decl, cl)
5739      tree enclosing, class_type, decl, cl;
5740 {
5741   tree tname = TYPE_NAME (class_type);
5742   tree resolved_type = TREE_TYPE (class_type);
5743   int array_dims = 0;
5744   tree resolved_type_decl;
5745
5746   if (resolved_type != NULL_TREE)
5747     {
5748       tree resolved_type_decl = TYPE_NAME (resolved_type);
5749       if (resolved_type_decl == NULL_TREE
5750           || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5751         {
5752           resolved_type_decl = build_decl (TYPE_DECL,
5753                                            TYPE_NAME (class_type),
5754                                            resolved_type);
5755         }
5756       return resolved_type_decl;
5757     }
5758
5759   /* 1- Check to see if we have an array. If true, find what we really
5760      want to resolve  */
5761   if ((array_dims = build_type_name_from_array_name (tname,
5762                                                      &TYPE_NAME (class_type))))
5763     WFL_STRIP_BRACKET (cl, cl);
5764
5765   /* 2- Resolve the bare type */
5766   if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5767                                                decl, cl)))
5768     return NULL_TREE;
5769   resolved_type = TREE_TYPE (resolved_type_decl);
5770
5771   /* 3- If we have and array, reconstruct the array down to its nesting */
5772   if (array_dims)
5773     {
5774       for (; array_dims; array_dims--)
5775         resolved_type = build_java_array_type (resolved_type, -1);
5776       resolved_type_decl = TYPE_NAME (resolved_type);
5777     }
5778   TREE_TYPE (class_type) = resolved_type;
5779   return resolved_type_decl;
5780 }
5781
5782 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5783    are used to report error messages. Do not try to replace TYPE_NAME
5784    (class_type) by a variable, since it is changed by
5785    find_in_imports{_on_demand} and (but it doesn't really matter)
5786    qualify_and_find.  */
5787
5788 tree
5789 do_resolve_class (enclosing, class_type, decl, cl)
5790      tree enclosing, class_type, decl, cl;
5791 {
5792   tree new_class_decl = NULL_TREE, super = NULL_TREE;
5793   tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5794   tree decl_result;
5795   htab_t circularity_hash;
5796
5797   /* This hash table is used to register the classes we're going
5798      through when searching the current class as an inner class, in
5799      order to detect circular references. Remember to free it before
5800      returning the section 0- of this function. */
5801   circularity_hash = htab_create (20, htab_hash_pointer, htab_eq_pointer,
5802                                   NULL);
5803
5804   /* 0- Search in the current class as an inner class.
5805      Maybe some code here should be added to load the class or
5806      something, at least if the class isn't an inner class and ended
5807      being loaded from class file. FIXME. */
5808   while (enclosing)
5809     {
5810       new_class_decl = resolve_inner_class (circularity_hash, cl, &enclosing,
5811                                             &super, class_type);
5812       if (new_class_decl)
5813         break;
5814
5815       /* If we haven't found anything because SUPER reached Object and
5816          ENCLOSING happens to be an innerclass, try the enclosing context. */
5817       if ((!super || super == object_type_node) &&
5818           enclosing && INNER_CLASS_DECL_P (enclosing))
5819         enclosing = DECL_CONTEXT (enclosing);
5820       else
5821         enclosing = NULL_TREE;
5822     }
5823
5824   htab_delete (circularity_hash);
5825
5826   if (new_class_decl)
5827     return new_class_decl;
5828
5829   /* 1- Check for the type in single imports. This will change
5830      TYPE_NAME() if something relevant is found */
5831   find_in_imports (saved_enclosing_type, class_type);
5832
5833   /* 2- And check for the type in the current compilation unit */
5834   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5835     {
5836       if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5837           !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5838         load_class (TYPE_NAME (class_type), 0);
5839       return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5840     }
5841
5842   /* 3- Search according to the current package definition */
5843   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5844     {
5845       if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5846                                              TYPE_NAME (class_type))))
5847         return new_class_decl;
5848     }
5849
5850   /* 4- Check the import on demands. Don't allow bar.baz to be
5851      imported from foo.* */
5852   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5853     if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5854       return NULL_TREE;
5855
5856   /* If found in find_in_imports_on_demant, the type has already been
5857      loaded. */
5858   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5859     return new_class_decl;
5860
5861   /* 5- Try with a name qualified with the package name we've seen so far */
5862   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5863     {
5864       tree package;
5865
5866       /* If there is a current package (ctxp->package), it's the first
5867          element of package_list and we can skip it. */
5868       for (package = (ctxp->package ?
5869                       TREE_CHAIN (package_list) : package_list);
5870            package; package = TREE_CHAIN (package))
5871         if ((new_class_decl = qualify_and_find (class_type,
5872                                                TREE_PURPOSE (package),
5873                                                TYPE_NAME (class_type))))
5874           return new_class_decl;
5875     }
5876
5877   /* 5- Check an other compilation unit that bears the name of type */
5878   load_class (TYPE_NAME (class_type), 0);
5879
5880   if (!cl)
5881     cl = lookup_cl (decl);
5882
5883   /* If we don't have a value for CL, then we're being called recursively.
5884      We can't check package access just yet, but it will be taken care of
5885      by the caller. */
5886   if (cl)
5887     {
5888       if (check_pkg_class_access (TYPE_NAME (class_type), cl, true))
5889         return NULL_TREE;
5890     }
5891
5892   /* 6- Last call for a resolution */
5893   decl_result = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5894
5895   /* The final lookup might have registered a.b.c into a.b$c If we
5896      failed at the first lookup, progressively change the name if
5897      applicable and use the matching DECL instead. */
5898   if (!decl_result && QUALIFIED_P (TYPE_NAME (class_type)))
5899     {
5900       char *separator;
5901       tree name = TYPE_NAME (class_type);
5902       char *namebuffer = alloca (IDENTIFIER_LENGTH (name) + 1);
5903
5904       strcpy (namebuffer, IDENTIFIER_POINTER (name));
5905
5906       do {
5907
5908        /* Reach the last '.', and if applicable, replace it by a `$' and
5909           see if this exists as a type. */
5910        if ((separator = strrchr (namebuffer, '.')))
5911          {
5912            *separator = '$';
5913            name = get_identifier (namebuffer);
5914            decl_result = IDENTIFIER_CLASS_VALUE (name);
5915          }
5916       } while (!decl_result && separator);
5917     }
5918   return decl_result;
5919 }
5920
5921 static tree
5922 qualify_and_find (class_type, package, name)
5923      tree class_type, package, name;
5924 {
5925   tree new_qualified = merge_qualified_name (package, name);
5926   tree new_class_decl;
5927
5928   if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5929     load_class (new_qualified, 0);
5930   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5931     {
5932       if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5933           !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5934         load_class (new_qualified, 0);
5935       TYPE_NAME (class_type) = new_qualified;
5936       return IDENTIFIER_CLASS_VALUE (new_qualified);
5937     }
5938   return NULL_TREE;
5939 }
5940
5941 /* Resolve NAME and lay it out (if not done and if not the current
5942    parsed class). Return a decl node. This function is meant to be
5943    called when type resolution is necessary during the walk pass.  */
5944
5945 static tree
5946 resolve_and_layout (something, cl)
5947      tree something;
5948      tree cl;
5949 {
5950   tree decl, decl_type;
5951
5952   /* Don't do that on the current class */
5953   if (something == current_class)
5954     return TYPE_NAME (current_class);
5955
5956   /* Don't do anything for void and other primitive types */
5957   if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5958     return NULL_TREE;
5959
5960   /* Pointer types can be reall pointer types or fake pointers. When
5961      finding a real pointer, recheck for primitive types */
5962   if (TREE_CODE (something) == POINTER_TYPE)
5963     {
5964       if (TREE_TYPE (something))
5965         {
5966           something = TREE_TYPE (something);
5967           if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5968             return NULL_TREE;
5969         }
5970       else
5971         something = TYPE_NAME (something);
5972     }
5973
5974   /* Don't do anything for arrays of primitive types */
5975   if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5976       && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5977     return NULL_TREE;
5978
5979   /* Something might be a WFL */
5980   if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5981     something = EXPR_WFL_NODE (something);
5982
5983   /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5984      TYPE_DECL or a real TYPE */
5985   else if (TREE_CODE (something) != IDENTIFIER_NODE)
5986     something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5987             DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5988
5989   if (!(decl = resolve_no_layout (something, cl)))
5990     return NULL_TREE;
5991
5992   /* Resolve and layout if necessary */
5993   decl_type = TREE_TYPE (decl);
5994   layout_class_methods (decl_type);
5995   /* Check methods */
5996   if (CLASS_FROM_SOURCE_P (decl_type))
5997     java_check_methods (decl);
5998   /* Layout the type if necessary */
5999   if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
6000     safe_layout_class (decl_type);
6001
6002   return decl;
6003 }
6004
6005 /* Resolve a class, returns its decl but doesn't perform any
6006    layout. The current parsing context is saved and restored */
6007
6008 static tree
6009 resolve_no_layout (name, cl)
6010      tree name, cl;
6011 {
6012   tree ptr, decl;
6013   BUILD_PTR_FROM_NAME (ptr, name);
6014   java_parser_context_save_global ();
6015   decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
6016   java_parser_context_restore_global ();
6017
6018   return decl;
6019 }
6020
6021 /* Called when reporting errors. Skip the '[]'s in a complex array
6022    type description that failed to be resolved. purify_type_name can't
6023    use an identifier tree.  */
6024
6025 static const char *
6026 purify_type_name (name)
6027      const char *name;
6028 {
6029   int len = strlen (name);
6030   int bracket_found;
6031
6032   STRING_STRIP_BRACKETS (name, len, bracket_found);
6033   if (bracket_found)
6034     {
6035       char *stripped_name = xmemdup (name, len, len+1);
6036       stripped_name [len] = '\0';
6037       return stripped_name;
6038     }
6039   return name;
6040 }
6041
6042 /* The type CURRENT refers to can't be found. We print error messages.  */
6043
6044 static void
6045 complete_class_report_errors (dep)
6046      jdep *dep;
6047 {
6048   const char *name;
6049
6050   if (!JDEP_WFL (dep))
6051     return;
6052
6053   name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
6054   switch (JDEP_KIND (dep))
6055     {
6056     case JDEP_SUPER:
6057       parse_error_context
6058         (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
6059          purify_type_name (name),
6060          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6061       break;
6062     case JDEP_FIELD:
6063       parse_error_context
6064         (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
6065          purify_type_name (name),
6066          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6067       break;
6068     case JDEP_METHOD:           /* Covers arguments */
6069       parse_error_context
6070         (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
6071          purify_type_name (name),
6072          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
6073          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
6074       break;
6075     case JDEP_METHOD_RETURN:    /* Covers return type */
6076       parse_error_context
6077         (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
6078          purify_type_name (name),
6079          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
6080       break;
6081     case JDEP_INTERFACE:
6082       parse_error_context
6083         (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
6084          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
6085          (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
6086          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6087       break;
6088     case JDEP_VARIABLE:
6089       parse_error_context
6090         (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
6091          purify_type_name (IDENTIFIER_POINTER
6092                            (EXPR_WFL_NODE (JDEP_WFL (dep)))),
6093          IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6094       break;
6095     case JDEP_EXCEPTION:        /* As specified by `throws' */
6096       parse_error_context
6097           (JDEP_WFL (dep), "Class `%s' not found in `throws'",
6098          IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
6099       break;
6100     default:
6101       /* Fix for -Wall. Just break doing nothing. The error will be
6102          caught later */
6103       break;
6104     }
6105 }
6106
6107 /* Return a static string containing the DECL prototype string. If
6108    DECL is a constructor, use the class name instead of the form
6109    <init> */
6110
6111 static const char *
6112 get_printable_method_name (decl)
6113      tree decl;
6114 {
6115   const char *to_return;
6116   tree name = NULL_TREE;
6117
6118   if (DECL_CONSTRUCTOR_P (decl))
6119     {
6120       name = DECL_NAME (decl);
6121       DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6122     }
6123
6124   to_return = lang_printable_name (decl, 0);
6125   if (DECL_CONSTRUCTOR_P (decl))
6126     DECL_NAME (decl) = name;
6127
6128   return to_return;
6129 }
6130
6131 /* Track method being redefined inside the same class. As a side
6132    effect, set DECL_NAME to an IDENTIFIER (prior entering this
6133    function it's a FWL, so we can track errors more accurately.)  */
6134
6135 static int
6136 check_method_redefinition (class, method)
6137      tree class, method;
6138 {
6139   tree redef, sig;
6140
6141   /* There's no need to verify <clinit> and finit$ and instinit$ */
6142   if (DECL_CLINIT_P (method)
6143       || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6144     return 0;
6145
6146   sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6147   for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6148     {
6149       if (redef == method)
6150         break;
6151       if (DECL_NAME (redef) == DECL_NAME (method)
6152           && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6153           && !DECL_ARTIFICIAL (method))
6154         {
6155           parse_error_context
6156             (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
6157              (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6158              get_printable_method_name (redef));
6159           return 1;
6160         }
6161     }
6162   return 0;
6163 }
6164
6165 /* Return 1 if check went ok, 0 otherwise.  */
6166 static int
6167 check_abstract_method_definitions (do_interface, class_decl, type)
6168      int do_interface;
6169      tree class_decl, type;
6170 {
6171   tree class = TREE_TYPE (class_decl);
6172   tree method, end_type;
6173   int ok = 1;
6174
6175   end_type = (do_interface ? object_type_node : type);
6176   for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6177     {
6178       tree other_super, other_method, method_sig, method_name;
6179       int found = 0;
6180       int end_type_reached = 0;
6181
6182       if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6183         continue;
6184
6185       /* Now verify that somewhere in between TYPE and CLASS,
6186          abstract method METHOD gets a non abstract definition
6187          that is inherited by CLASS.  */
6188
6189       method_sig = build_java_signature (TREE_TYPE (method));
6190       method_name = DECL_NAME (method);
6191       if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6192         method_name = EXPR_WFL_NODE (method_name);
6193
6194       other_super = class;
6195       do {
6196         if (other_super == end_type)
6197           end_type_reached = 1;
6198
6199         /* Method search */
6200         for (other_method = TYPE_METHODS (other_super); other_method;
6201             other_method = TREE_CHAIN (other_method))
6202           {
6203             tree s = build_java_signature (TREE_TYPE (other_method));
6204             tree other_name = DECL_NAME (other_method);
6205
6206             if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6207               other_name = EXPR_WFL_NODE (other_name);
6208             if (!DECL_CLINIT_P (other_method)
6209                 && !DECL_CONSTRUCTOR_P (other_method)
6210                 && method_name == other_name
6211                 && method_sig == s
6212                 && !METHOD_ABSTRACT (other_method))
6213              {
6214                found = 1;
6215                break;
6216              }
6217           }
6218         other_super = CLASSTYPE_SUPER (other_super);
6219       } while (!end_type_reached);
6220
6221       /* Report that abstract METHOD didn't find an implementation
6222          that CLASS can use. */
6223       if (!found)
6224         {
6225           char *t = xstrdup (lang_printable_name
6226                             (TREE_TYPE (TREE_TYPE (method)), 0));
6227           tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6228
6229           parse_error_context
6230             (lookup_cl (class_decl),
6231              "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",
6232              IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6233              t, lang_printable_name (method, 0),
6234              (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6235               "interface" : "class"),
6236              IDENTIFIER_POINTER (ccn),
6237              (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6238              IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6239           ok = 0;
6240           free (t);
6241         }
6242     }
6243
6244   if (ok && do_interface)
6245     {
6246       /* Check for implemented interfaces. */
6247       int i;
6248       tree vector = TYPE_BINFO_BASETYPES (type);
6249       for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
6250         {
6251           tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6252           ok = check_abstract_method_definitions (1, class_decl, super);
6253         }
6254     }
6255
6256   return ok;
6257 }
6258
6259 /* Check that CLASS_DECL somehow implements all inherited abstract
6260    methods.  */
6261
6262 static void
6263 java_check_abstract_method_definitions (class_decl)
6264      tree class_decl;
6265 {
6266   tree class = TREE_TYPE (class_decl);
6267   tree super, vector;
6268   int i;
6269
6270   if (CLASS_ABSTRACT (class_decl))
6271     return;
6272
6273   /* Check for inherited types */
6274   super = class;
6275   do {
6276     super = CLASSTYPE_SUPER (super);
6277     check_abstract_method_definitions (0, class_decl, super);
6278   } while (super != object_type_node);
6279
6280   /* Check for implemented interfaces. */
6281   vector = TYPE_BINFO_BASETYPES (class);
6282   for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
6283     {
6284       super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6285       check_abstract_method_definitions (1, class_decl, super);
6286     }
6287 }
6288
6289 /* Check all the types method DECL uses and return 1 if all of them
6290    are now complete, 0 otherwise. This is used to check whether its
6291    safe to build a method signature or not.  */
6292
6293 static int
6294 check_method_types_complete (decl)
6295      tree decl;
6296 {
6297   tree type = TREE_TYPE (decl);
6298   tree args;
6299
6300   if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6301     return 0;
6302
6303   args = TYPE_ARG_TYPES (type);
6304   if (TREE_CODE (type) == METHOD_TYPE)
6305     args = TREE_CHAIN (args);
6306   for (; args != end_params_node; args = TREE_CHAIN (args))
6307     if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6308       return 0;
6309
6310   return 1;
6311 }
6312
6313 /* Visible interface to check methods contained in CLASS_DECL */
6314
6315 void
6316 java_check_methods (class_decl)
6317      tree class_decl;
6318 {
6319   if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6320     return;
6321
6322   if (CLASS_INTERFACE (class_decl))
6323     java_check_abstract_methods (class_decl);
6324   else
6325     java_check_regular_methods (class_decl);
6326
6327   CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6328 }
6329
6330 /* Check all the methods of CLASS_DECL. Methods are first completed
6331    then checked according to regular method existence rules.  If no
6332    constructor for CLASS_DECL were encountered, then build its
6333    declaration.  */
6334
6335 static void
6336 java_check_regular_methods (class_decl)
6337      tree class_decl;
6338 {
6339   int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6340   tree method;
6341   tree class = TREE_TYPE (class_decl);
6342   tree found = NULL_TREE;
6343   tree mthrows;
6344
6345   /* It is not necessary to check methods defined in java.lang.Object */
6346   if (class == object_type_node)
6347     return;
6348
6349   if (!TYPE_NVIRTUALS (class))
6350     TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6351
6352   /* Should take interfaces into account. FIXME */
6353   for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6354     {
6355       tree sig;
6356       tree method_wfl = DECL_FUNCTION_WFL (method);
6357       int aflags;
6358
6359       /* Check for redefinitions */
6360       if (check_method_redefinition (class, method))
6361         continue;
6362
6363       /* If we see one constructor a mark so we don't generate the
6364          default one. Also skip other verifications: constructors
6365          can't be inherited hence hiden or overriden */
6366      if (DECL_CONSTRUCTOR_P (method))
6367        {
6368          saw_constructor = 1;
6369          continue;
6370        }
6371
6372       /* We verify things thrown by the method. They must inherits from
6373          java.lang.Throwable */
6374       for (mthrows = DECL_FUNCTION_THROWS (method);
6375            mthrows; mthrows = TREE_CHAIN (mthrows))
6376         {
6377           if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6378             parse_error_context
6379               (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6380                IDENTIFIER_POINTER
6381                  (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6382         }
6383
6384       sig = build_java_argument_signature (TREE_TYPE (method));
6385       found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6386
6387       /* Inner class can't declare static methods */
6388       if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6389         {
6390           char *t = xstrdup (lang_printable_name (class, 0));
6391           parse_error_context
6392             (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6393              lang_printable_name (method, 0), t);
6394           free (t);
6395         }
6396
6397       /* Nothing overrides or it's a private method. */
6398       if (!found)
6399         continue;
6400       if (METHOD_PRIVATE (found))
6401         {
6402           found = NULL_TREE;
6403           continue;
6404         }
6405
6406       /* If `found' is declared in an interface, make sure the
6407          modifier matches. */
6408       if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6409           && clinit_identifier_node != DECL_NAME (found)
6410           && !METHOD_PUBLIC (method))
6411         {
6412           tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6413           parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6414                                IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6415                                lang_printable_name (method, 0),
6416                                IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6417         }
6418
6419       /* Can't override a method with the same name and different return
6420          types. */
6421       if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6422         {
6423           char *t = xstrdup
6424             (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6425           parse_error_context
6426             (method_wfl,
6427              "Method `%s' was defined with return type `%s' in class `%s'",
6428              lang_printable_name (found, 0), t,
6429              IDENTIFIER_POINTER
6430                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6431           free (t);
6432         }
6433
6434       aflags = get_access_flags_from_decl (found);
6435
6436       /* Can't override final. Can't override static. */
6437       if (METHOD_FINAL (found) || METHOD_STATIC (found))
6438         {
6439           /* Static *can* override static */
6440           if (METHOD_STATIC (found) && METHOD_STATIC (method))
6441             continue;
6442           parse_error_context
6443             (method_wfl,
6444              "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6445              (METHOD_FINAL (found) ? "Final" : "Static"),
6446              lang_printable_name (found, 0),
6447              (METHOD_FINAL (found) ? "final" : "static"),
6448              IDENTIFIER_POINTER
6449                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6450           continue;
6451         }
6452
6453       /* Static method can't override instance method. */
6454       if (METHOD_STATIC (method))
6455         {
6456           parse_error_context
6457             (method_wfl,
6458              "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6459              lang_printable_name (found, 0),
6460              IDENTIFIER_POINTER
6461                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6462           continue;
6463         }
6464
6465       /* - Overriding/hiding public must be public
6466          - Overriding/hiding protected must be protected or public
6467          - If the overriden or hidden method has default (package)
6468            access, then the overriding or hiding method must not be
6469            private; otherwise, a compile-time error occurs.  If
6470            `found' belongs to an interface, things have been already
6471            taken care of.  */
6472       if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6473           && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6474               || (METHOD_PROTECTED (found)
6475                   && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6476               || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6477                   && METHOD_PRIVATE (method))))
6478         {
6479           parse_error_context
6480             (method_wfl,
6481              "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6482              (METHOD_PUBLIC (method) ? "public" :
6483               (METHOD_PRIVATE (method) ? "private" : "protected")),
6484              IDENTIFIER_POINTER (DECL_NAME
6485                                  (TYPE_NAME (DECL_CONTEXT (found)))));
6486           continue;
6487         }
6488
6489       /* Overriding methods must have compatible `throws' clauses on checked
6490          exceptions, if any */
6491       check_throws_clauses (method, method_wfl, found);
6492
6493       /* Inheriting multiple methods with the same signature. FIXME */
6494     }
6495
6496   if (!TYPE_NVIRTUALS (class))
6497     TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6498
6499   /* Search for inherited abstract method not yet implemented in this
6500      class.  */
6501   java_check_abstract_method_definitions (class_decl);
6502
6503   if (!saw_constructor)
6504     abort ();
6505 }
6506
6507 /* Return a non zero value if the `throws' clause of METHOD (if any)
6508    is incompatible with the `throws' clause of FOUND (if any).  */
6509
6510 static void
6511 check_throws_clauses (method, method_wfl, found)
6512      tree method, method_wfl, found;
6513 {
6514   tree mthrows, fthrows;
6515
6516   /* Can't check these things with class loaded from bytecode. FIXME */
6517   if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6518     return;
6519
6520   for (mthrows = DECL_FUNCTION_THROWS (method);
6521        mthrows; mthrows = TREE_CHAIN (mthrows))
6522     {
6523       /* We don't verify unchecked expressions */
6524       if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6525         continue;
6526       /* Checked expression must be compatible */
6527       for (fthrows = DECL_FUNCTION_THROWS (found);
6528            fthrows; fthrows = TREE_CHAIN (fthrows))
6529         if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6530           break;
6531       if (!fthrows)
6532         {
6533           parse_error_context
6534             (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'",
6535              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6536              lang_printable_name (found, 0),
6537              IDENTIFIER_POINTER
6538                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6539         }
6540     }
6541 }
6542
6543 /* Check abstract method of interface INTERFACE */
6544
6545 static void
6546 java_check_abstract_methods (interface_decl)
6547      tree interface_decl;
6548 {
6549   int i, n;
6550   tree method, basetype_vec, found;
6551   tree interface = TREE_TYPE (interface_decl);
6552
6553   for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6554     {
6555       /* 2- Check for double definition inside the defining interface */
6556       if (check_method_redefinition (interface, method))
6557         continue;
6558
6559       /* 3- Overriding is OK as far as we preserve the return type and
6560          the thrown exceptions (FIXME) */
6561       found = lookup_java_interface_method2 (interface, method);
6562       if (found)
6563         {
6564           char *t;
6565           t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6566           parse_error_context
6567             (DECL_FUNCTION_WFL (found),
6568              "Method `%s' was defined with return type `%s' in class `%s'",
6569              lang_printable_name (found, 0), t,
6570              IDENTIFIER_POINTER
6571                (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6572           free (t);
6573           continue;
6574         }
6575     }
6576
6577   /* 4- Inherited methods can't differ by their returned types */
6578   if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6579     return;
6580   n = TREE_VEC_LENGTH (basetype_vec);
6581   for (i = 0; i < n; i++)
6582     {
6583       tree sub_interface_method, sub_interface;
6584       tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6585       if (!vec_elt)
6586         continue;
6587       sub_interface = BINFO_TYPE (vec_elt);
6588       for (sub_interface_method = TYPE_METHODS (sub_interface);
6589            sub_interface_method;
6590            sub_interface_method = TREE_CHAIN (sub_interface_method))
6591         {
6592           found = lookup_java_interface_method2 (interface,
6593                                                  sub_interface_method);
6594           if (found && (found != sub_interface_method))
6595             {
6596               parse_error_context
6597                 (lookup_cl (sub_interface_method),
6598                  "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6599                  IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6600                  lang_printable_name (found, 0),
6601                  IDENTIFIER_POINTER
6602                    (DECL_NAME (TYPE_NAME
6603                                (DECL_CONTEXT (sub_interface_method)))),
6604                  IDENTIFIER_POINTER
6605                    (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6606             }
6607         }
6608     }
6609 }
6610
6611 /* Lookup methods in interfaces using their name and partial
6612    signature. Return a matching method only if their types differ.  */
6613
6614 static tree
6615 lookup_java_interface_method2 (class, method_decl)
6616      tree class, method_decl;
6617 {
6618   int i, n;
6619   tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6620
6621   if (!basetype_vec)
6622     return NULL_TREE;
6623
6624   n = TREE_VEC_LENGTH (basetype_vec);
6625   for (i = 0; i < n; i++)
6626     {
6627       tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6628       if ((BINFO_TYPE (vec_elt) != object_type_node)
6629           && (to_return =
6630               lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6631         return to_return;
6632     }
6633   for (i = 0; i < n; i++)
6634     {
6635       to_return = lookup_java_interface_method2
6636         (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6637       if (to_return)
6638         return to_return;
6639     }
6640
6641   return NULL_TREE;
6642 }
6643
6644 /* Lookup method using their name and partial signature. Return a
6645    matching method only if their types differ.  */
6646
6647 static tree
6648 lookup_java_method2 (clas, method_decl, do_interface)
6649      tree clas, method_decl;
6650      int do_interface;
6651 {
6652   tree method, method_signature, method_name, method_type, name;
6653
6654   method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6655   name = DECL_NAME (method_decl);
6656   method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6657                  EXPR_WFL_NODE (name) : name);
6658   method_type = TREE_TYPE (TREE_TYPE (method_decl));
6659
6660   while (clas != NULL_TREE)
6661     {
6662       for (method = TYPE_METHODS (clas);
6663            method != NULL_TREE;  method = TREE_CHAIN (method))
6664         {
6665           tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6666           tree name = DECL_NAME (method);
6667           if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6668                EXPR_WFL_NODE (name) : name) == method_name
6669               && method_sig == method_signature
6670               && TREE_TYPE (TREE_TYPE (method)) != method_type)
6671             return method;
6672         }
6673       clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6674     }
6675   return NULL_TREE;
6676 }
6677
6678 /* Return the line that matches DECL line number, and try its best to
6679    position the column number. Used during error reports.  */
6680
6681 static GTY(()) tree cl_v;
6682 static tree
6683 lookup_cl (decl)
6684      tree decl;
6685 {
6686   char *line, *found;
6687
6688   if (!decl)
6689     return NULL_TREE;
6690
6691   if (cl_v == NULL_TREE)
6692     {
6693       cl_v = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6694     }
6695
6696   EXPR_WFL_FILENAME_NODE (cl_v) = get_identifier (DECL_SOURCE_FILE (decl));
6697   EXPR_WFL_SET_LINECOL (cl_v, DECL_SOURCE_LINE_FIRST (decl), -1);
6698
6699   line = java_get_line_col (EXPR_WFL_FILENAME (cl_v),
6700                             EXPR_WFL_LINENO (cl_v), EXPR_WFL_COLNO (cl_v));
6701
6702   found = strstr ((const char *)line,
6703                   (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6704   if (found)
6705     EXPR_WFL_SET_LINECOL (cl_v, EXPR_WFL_LINENO (cl_v), found - line);
6706
6707   return cl_v;
6708 }
6709
6710 /* Look for a simple name in the single-type import list */
6711
6712 static tree
6713 find_name_in_single_imports (name)
6714      tree name;
6715 {
6716   tree node;
6717
6718   for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6719     if (TREE_VALUE (node) == name)
6720       return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6721
6722   return NULL_TREE;
6723 }
6724
6725 /* Process all single-type import. */
6726
6727 static int
6728 process_imports ()
6729 {
6730   tree import;
6731   int error_found;
6732
6733   for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6734     {
6735       tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6736       char *original_name;
6737
6738       obstack_grow0 (&temporary_obstack,
6739                      IDENTIFIER_POINTER (to_be_found),
6740                      IDENTIFIER_LENGTH (to_be_found));
6741       original_name = obstack_finish (&temporary_obstack);
6742
6743       /* Don't load twice something already defined. */
6744       if (IDENTIFIER_CLASS_VALUE (to_be_found))
6745         continue;
6746
6747       while (1)
6748         {
6749           tree left;
6750
6751           QUALIFIED_P (to_be_found) = 1;
6752           load_class (to_be_found, 0);
6753           error_found =
6754             check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true);
6755
6756           /* We found it, we can bail out */
6757           if (IDENTIFIER_CLASS_VALUE (to_be_found))
6758             break;
6759
6760           /* We haven't found it. Maybe we're trying to access an
6761              inner class.  The only way for us to know is to try again
6762              after having dropped a qualifier. If we can't break it further,
6763              we have an error. */
6764           if (breakdown_qualified (&left, NULL, to_be_found))
6765             break;
6766
6767           to_be_found = left;
6768         }
6769       if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6770         {
6771           parse_error_context (TREE_PURPOSE (import),
6772                                "Class or interface `%s' not found in import",
6773                                original_name);
6774           error_found = 1;
6775         }
6776
6777       obstack_free (&temporary_obstack, original_name);
6778       if (error_found)
6779         return 1;
6780     }
6781   return 0;
6782 }
6783
6784 /* Possibly find and mark a class imported by a single-type import
6785    statement.  */
6786
6787 static void
6788 find_in_imports (enclosing_type, class_type)
6789      tree enclosing_type;
6790      tree class_type;
6791 {
6792   tree import = (enclosing_type ? TYPE_IMPORT_LIST (enclosing_type) :
6793                  ctxp->import_list);
6794   while (import)
6795     {
6796       if (TREE_VALUE (import) == TYPE_NAME (class_type))
6797         {
6798           TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6799           QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6800           return;
6801         }
6802       import = TREE_CHAIN (import);
6803     }
6804 }
6805
6806 static int
6807 note_possible_classname (name, len)
6808      const char *name;
6809      int len;
6810 {
6811   tree node;
6812   if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6813     len = len - 5;
6814   else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6815     len = len - 6;
6816   else
6817     return 0;
6818   node = ident_subst (name, len, "", '/', '.', "");
6819   IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6820   QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6821   return 1;
6822 }
6823
6824 /* Read a import directory, gathering potential match for further type
6825    references. Indifferently reads a filesystem or a ZIP archive
6826    directory.  */
6827
6828 static void
6829 read_import_dir (wfl)
6830      tree wfl;
6831 {
6832   tree package_id = EXPR_WFL_NODE (wfl);
6833   const char *package_name = IDENTIFIER_POINTER (package_id);
6834   int package_length = IDENTIFIER_LENGTH (package_id);
6835   DIR *dirp = NULL;
6836   JCF *saved_jcf = current_jcf;
6837
6838   int found = 0;
6839   int k;
6840   void *entry;
6841   struct buffer filename[1];
6842
6843   if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6844     return;
6845   IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6846
6847   BUFFER_INIT (filename);
6848   buffer_grow (filename, package_length + 100);
6849
6850   for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6851     {
6852       const char *entry_name = jcf_path_name (entry);
6853       int entry_length = strlen (entry_name);
6854       if (jcf_path_is_zipfile (entry))
6855         {
6856           ZipFile *zipf;
6857           buffer_grow (filename, entry_length);
6858           memcpy (filename->data, entry_name, entry_length - 1);
6859           filename->data[entry_length-1] = '\0';
6860           zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6861           if (zipf == NULL)
6862             error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6863           else
6864             {
6865               ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6866               BUFFER_RESET (filename);
6867               for (k = 0; k < package_length; k++)
6868                 {
6869                   char ch = package_name[k];
6870                   *filename->ptr++ = ch == '.' ? '/' : ch;
6871                 }
6872               *filename->ptr++ = '/';
6873
6874               for (k = 0; k < zipf->count;  k++, zipd = ZIPDIR_NEXT (zipd))
6875                 {
6876                   const char *current_entry = ZIPDIR_FILENAME (zipd);
6877                   int current_entry_len = zipd->filename_length;
6878
6879                   if (current_entry_len >= BUFFER_LENGTH (filename)
6880                       && strncmp (filename->data, current_entry,
6881                                   BUFFER_LENGTH (filename)) != 0)
6882                     continue;
6883                   found |= note_possible_classname (current_entry,
6884                                                     current_entry_len);
6885                 }
6886             }
6887         }
6888       else
6889         {
6890           BUFFER_RESET (filename);
6891           buffer_grow (filename, entry_length + package_length + 4);
6892           strcpy (filename->data, entry_name);
6893           filename->ptr = filename->data + entry_length;
6894           for (k = 0; k < package_length; k++)
6895             {
6896               char ch = package_name[k];
6897               *filename->ptr++ = ch == '.' ? '/' : ch;
6898             }
6899           *filename->ptr = '\0';
6900
6901           dirp = opendir (filename->data);
6902           if (dirp == NULL)
6903             continue;
6904           *filename->ptr++ = '/';
6905           for (;;)
6906             {
6907               int len;
6908               const char *d_name;
6909               struct dirent *direntp = readdir (dirp);
6910               if (!direntp)
6911                 break;
6912               d_name = direntp->d_name;
6913               len = strlen (direntp->d_name);
6914               buffer_grow (filename, len+1);
6915               strcpy (filename->ptr, d_name);
6916               found |= note_possible_classname (filename->data + entry_length,
6917                                                 package_length+len+1);
6918             }
6919           if (dirp)
6920             closedir (dirp);
6921         }
6922     }
6923
6924   free (filename->data);
6925
6926   /* Here we should have a unified way of retrieving an entry, to be
6927      indexed. */
6928   if (!found)
6929     {
6930       static int first = 1;
6931       if (first)
6932         {
6933           error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives", package_name);
6934           java_error_count++;
6935           first = 0;
6936         }
6937       else
6938         parse_error_context (wfl, "Package `%s' not found in import",
6939                              package_name);
6940       current_jcf = saved_jcf;
6941       return;
6942     }
6943   current_jcf = saved_jcf;
6944 }
6945
6946 /* Possibly find a type in the import on demands specified
6947    types. Returns 1 if an error occurred, 0 otherwise. Run through the
6948    entire list, to detected potential double definitions.  */
6949
6950 static int
6951 find_in_imports_on_demand (enclosing_type, class_type)
6952      tree enclosing_type;
6953      tree class_type;
6954 {
6955   tree class_type_name = TYPE_NAME (class_type);
6956   tree import = (enclosing_type ? TYPE_IMPORT_DEMAND_LIST (enclosing_type) :
6957                   ctxp->import_demand_list);
6958   tree cl = NULL_TREE;
6959   int seen_once = -1;   /* -1 when not set, 1 if seen once, >1 otherwise. */
6960   int to_return = -1;   /* -1 when not set, 0 or 1 otherwise */
6961   tree node;
6962
6963   for (; import; import = TREE_CHAIN (import))
6964     {
6965       int saved_lineno = lineno;
6966       int access_check;
6967       const char *id_name;
6968       tree decl, type_name_copy;
6969
6970       obstack_grow (&temporary_obstack,
6971                     IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6972                     IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6973       obstack_1grow (&temporary_obstack, '.');
6974       obstack_grow0 (&temporary_obstack,
6975                      IDENTIFIER_POINTER (class_type_name),
6976                      IDENTIFIER_LENGTH (class_type_name));
6977       id_name = obstack_finish (&temporary_obstack);
6978
6979       if (! (node = maybe_get_identifier (id_name)))
6980         continue;
6981
6982       /* Setup lineno so that it refers to the line of the import (in
6983          case we parse a class file and encounter errors */
6984       lineno = EXPR_WFL_LINENO (TREE_PURPOSE (import));
6985
6986       type_name_copy = TYPE_NAME (class_type);
6987       TYPE_NAME (class_type) = node;
6988       QUALIFIED_P (node) = 1;
6989       decl = IDENTIFIER_CLASS_VALUE (node);
6990       access_check = -1;
6991       /* If there is no DECL set for the class or if the class isn't
6992          loaded and not seen in source yet, then load */
6993       if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6994                     && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6995         {
6996           load_class (node, 0);
6997           decl = IDENTIFIER_CLASS_VALUE (node);
6998         }
6999       if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
7000         access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
7001                                                false);
7002       else
7003         /* 6.6.1: Inner classes are subject to member access rules. */
7004         access_check = 0;
7005
7006       lineno = saved_lineno;
7007
7008       /* If the loaded class is not accessible or couldn't be loaded,
7009          we restore the original TYPE_NAME and process the next
7010          import. */
7011       if (access_check || !decl)
7012         {
7013           TYPE_NAME (class_type) = type_name_copy;
7014           continue;
7015         }
7016
7017       /* If the loaded class is accessible, we keep a tab on it to
7018          detect and report multiple inclusions. */
7019       if (IS_A_CLASSFILE_NAME (node))
7020         {
7021           if (seen_once < 0)
7022             {
7023               cl = TREE_PURPOSE (import);
7024               seen_once = 1;
7025             }
7026           else if (seen_once >= 0)
7027             {
7028               tree location = (cl ? cl : TREE_PURPOSE (import));
7029               tree package = (cl ? EXPR_WFL_NODE (cl) :
7030                               EXPR_WFL_NODE (TREE_PURPOSE (import)));
7031               seen_once++;
7032               parse_error_context
7033                 (location,
7034                  "Type `%s' also potentially defined in package `%s'",
7035                  IDENTIFIER_POINTER (TYPE_NAME (class_type)),
7036                  IDENTIFIER_POINTER (package));
7037             }
7038         }
7039       to_return = access_check;
7040     }
7041
7042   if (seen_once == 1)
7043     return to_return;
7044   else
7045     return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
7046 }
7047
7048 /* Add package NAME to the list of package encountered so far. To
7049    speed up class lookup in do_resolve_class, we make sure a
7050    particular package is added only once.  */
7051
7052 static void
7053 register_package (name)
7054      tree name;
7055 {
7056   static htab_t pht;
7057   PTR *e;
7058
7059   if (pht == NULL)
7060     pht = htab_create (50, htab_hash_pointer, htab_eq_pointer, NULL);
7061
7062   e = htab_find_slot (pht, name, INSERT);
7063   if (*e == NULL)
7064     {
7065       package_list = chainon (package_list, build_tree_list (name, NULL));
7066       *e = name;
7067     }
7068 }
7069
7070 static tree
7071 resolve_package (pkg, next, type_name)
7072      tree pkg, *next, *type_name;
7073 {
7074   tree current;
7075   tree decl = NULL_TREE;
7076   *type_name = NULL_TREE;
7077
7078   /* The trick is to determine when the package name stops and were
7079      the name of something contained in the package starts. Then we
7080      return a fully qualified name of what we want to get. */
7081
7082   *next = EXPR_WFL_QUALIFICATION (pkg);
7083
7084   /* Try to progressively construct a type name */
7085   if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
7086     for (current = EXPR_WFL_QUALIFICATION (pkg);
7087          current; current = TREE_CHAIN (current))
7088       {
7089         /* If we don't have what we're expecting, exit now. TYPE_NAME
7090            will be null and the error caught later. */
7091         if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
7092           break;
7093         *type_name =
7094           merge_qualified_name (*type_name, EXPR_WFL_NODE (QUAL_WFL (current)));
7095         if ((decl = resolve_no_layout (*type_name, NULL_TREE)))
7096           {
7097             /* resolve_package should be used in a loop, hence we
7098                point at this one to naturally process the next one at
7099                the next iteration. */
7100             *next = current;
7101             break;
7102           }
7103       }
7104   return decl;
7105 }
7106
7107
7108 /* Check accessibility of inner classes according to member access rules.
7109    DECL is the inner class, ENCLOSING_DECL is the class from which the
7110    access is being attempted. */
7111
7112 static void
7113 check_inner_class_access (decl, enclosing_decl, cl)
7114      tree decl, enclosing_decl, cl;
7115 {
7116   const char *access;
7117   tree enclosing_decl_type;
7118
7119   /* We don't issue an error message when CL is null. CL can be null
7120      as a result of processing a JDEP crafted by source_start_java_method
7121      for the purpose of patching its parm decl. But the error would
7122      have been already trapped when fixing the method's signature.
7123      DECL can also be NULL in case of earlier errors. */
7124   if (!decl || !cl)
7125     return;
7126
7127   enclosing_decl_type = TREE_TYPE (enclosing_decl);
7128
7129   if (CLASS_PRIVATE (decl))
7130     {
7131       /* Access is permitted only within the body of the top-level
7132          class in which DECL is declared. */
7133       tree top_level = decl;
7134       while (DECL_CONTEXT (top_level))
7135         top_level = DECL_CONTEXT (top_level);
7136       while (DECL_CONTEXT (enclosing_decl))
7137         enclosing_decl = DECL_CONTEXT (enclosing_decl);
7138       if (top_level == enclosing_decl)
7139         return;
7140       access = "private";
7141     }
7142   else if (CLASS_PROTECTED (decl))
7143     {
7144       tree decl_context;
7145       /* Access is permitted from within the same package... */
7146       if (in_same_package (decl, enclosing_decl))
7147         return;
7148
7149       /* ... or from within the body of a subtype of the context in which
7150          DECL is declared. */
7151       decl_context = DECL_CONTEXT (decl);
7152       while (enclosing_decl)
7153         {
7154           if (CLASS_INTERFACE (decl))
7155             {
7156               if (interface_of_p (TREE_TYPE (decl_context),
7157                                   enclosing_decl_type))
7158                 return;
7159             }
7160           else
7161             {
7162               /* Eww. The order of the arguments is different!! */
7163               if (inherits_from_p (enclosing_decl_type,
7164                                    TREE_TYPE (decl_context)))
7165                 return;
7166             }
7167           enclosing_decl = DECL_CONTEXT (enclosing_decl);
7168         }
7169       access = "protected";
7170     }
7171   else if (! CLASS_PUBLIC (decl))
7172     {
7173       /* Access is permitted only from within the same package as DECL. */
7174       if (in_same_package (decl, enclosing_decl))
7175         return;
7176       access = "non-public";
7177     }
7178   else
7179     /* Class is public. */
7180     return;
7181
7182   parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7183                        (CLASS_INTERFACE (decl) ? "interface" : "class"),
7184                        lang_printable_name (decl, 0), access);
7185 }
7186
7187 /* Accessibility check for top-level classes. If CLASS_NAME is in a
7188    foreign package, it must be PUBLIC. Return 0 if no access
7189    violations were found, 1 otherwise. If VERBOSE is true and an error
7190    was found, it is reported and accounted for.  */
7191
7192 static int
7193 check_pkg_class_access (class_name, cl, verbose)
7194      tree class_name;
7195      tree cl;
7196      bool verbose;
7197 {
7198   tree type;
7199
7200   if (!IDENTIFIER_CLASS_VALUE (class_name))
7201     return 0;
7202
7203   if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7204     return 0;
7205
7206   if (!CLASS_PUBLIC (TYPE_NAME (type)))
7207     {
7208       /* Access to a private class within the same package is
7209          allowed. */
7210       tree l, r;
7211       breakdown_qualified (&l, &r, class_name);
7212       if (!QUALIFIED_P (class_name) && !ctxp->package)
7213         /* Both in the empty package. */
7214         return 0;
7215       if (l == ctxp->package)
7216         /* Both in the same package. */
7217         return 0;
7218
7219       if (verbose)
7220         parse_error_context
7221           (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
7222            (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7223            IDENTIFIER_POINTER (class_name));
7224       return 1;
7225     }
7226   return 0;
7227 }
7228
7229 /* Local variable declaration. */
7230
7231 static void
7232 declare_local_variables (modifier, type, vlist)
7233      int modifier;
7234      tree type;
7235      tree vlist;
7236 {
7237   tree decl, current, saved_type;
7238   tree type_wfl = NULL_TREE;
7239   int must_chain = 0;
7240   int final_p = 0;
7241
7242   /* Push a new block if statements were seen between the last time we
7243      pushed a block and now. Keep a count of blocks to close */
7244   if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7245     {
7246       tree b = enter_block ();
7247       BLOCK_IS_IMPLICIT (b) = 1;
7248     }
7249
7250   if (modifier)
7251     {
7252       size_t i;
7253       for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)
7254         if (1 << i & modifier)
7255           break;
7256       if (modifier == ACC_FINAL)
7257         final_p = 1;
7258       else
7259         {
7260           parse_error_context
7261             (ctxp->modifier_ctx [i],
7262              "Only `final' is allowed as a local variables modifier");
7263           return;
7264         }
7265     }
7266
7267   /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7268      hold the TYPE value if a new incomplete has to be created (as
7269      opposed to being found already existing and reused). */
7270   SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7271
7272   /* If TYPE is fully resolved and we don't have a reference, make one */
7273   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7274
7275   /* Go through all the declared variables */
7276   for (current = vlist, saved_type = type; current;
7277        current = TREE_CHAIN (current), type = saved_type)
7278     {
7279       tree other, real_type;
7280       tree wfl  = TREE_PURPOSE (current);
7281       tree name = EXPR_WFL_NODE (wfl);
7282       tree init = TREE_VALUE (current);
7283
7284       /* Process NAME, as it may specify extra dimension(s) for it */
7285       type = build_array_from_name (type, type_wfl, name, &name);
7286
7287       /* Variable redefinition check */
7288       if ((other = lookup_name_in_blocks (name)))
7289         {
7290           variable_redefinition_error (wfl, name, TREE_TYPE (other),
7291                                        DECL_SOURCE_LINE (other));
7292           continue;
7293         }
7294
7295       /* Type adjustment. We may have just readjusted TYPE because
7296          the variable specified more dimensions. Make sure we have
7297          a reference if we can and don't have one already. */
7298       PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7299
7300       real_type = GET_REAL_TYPE (type);
7301       /* Never layout this decl. This will be done when its scope
7302          will be entered */
7303       decl = build_decl (VAR_DECL, name, real_type);
7304       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7305       DECL_FINAL (decl) = final_p;
7306       BLOCK_CHAIN_DECL (decl);
7307
7308       /* If doing xreferencing, replace the line number with the WFL
7309          compound value */
7310       if (flag_emit_xref)
7311         DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7312
7313       /* Don't try to use an INIT statement when an error was found */
7314       if (init && java_error_count)
7315         init = NULL_TREE;
7316
7317       /* Add the initialization function to the current function's code */
7318       if (init)
7319         {
7320           /* Name might have been readjusted */
7321           EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7322           MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7323           java_method_add_stmt (current_function_decl,
7324                                 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7325                                                       init));
7326         }
7327
7328       /* Setup dependency the type of the decl */
7329       if (must_chain)
7330         {
7331           jdep *dep;
7332           register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7333           dep = CLASSD_LAST (ctxp->classd_list);
7334           JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7335         }
7336     }
7337   SOURCE_FRONTEND_DEBUG (("Defined locals"));
7338 }
7339
7340 /* Called during parsing. Build decls from argument list.  */
7341
7342 static void
7343 source_start_java_method (fndecl)
7344      tree fndecl;
7345 {
7346   tree tem;
7347   tree parm_decl;
7348   int i;
7349
7350   if (!fndecl)
7351     return;
7352
7353   current_function_decl = fndecl;
7354
7355   /* New scope for the function */
7356   enter_block ();
7357   for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7358        tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7359     {
7360       tree type = TREE_VALUE (tem);
7361       tree name = TREE_PURPOSE (tem);
7362
7363       /* If type is incomplete. Create an incomplete decl and ask for
7364          the decl to be patched later */
7365       if (INCOMPLETE_TYPE_P (type))
7366         {
7367           jdep *jdep;
7368           tree real_type = GET_REAL_TYPE (type);
7369           parm_decl = build_decl (PARM_DECL, name, real_type);
7370           type = obtain_incomplete_type (type);
7371           register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7372           jdep = CLASSD_LAST (ctxp->classd_list);
7373           JDEP_MISC (jdep) = name;
7374           JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7375         }
7376       else
7377         parm_decl = build_decl (PARM_DECL, name, type);
7378
7379       /* Remember if a local variable was declared final (via its
7380          TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7381       if (ARG_FINAL_P (tem))
7382         {
7383           MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7384           DECL_FINAL (parm_decl) = 1;
7385         }
7386
7387       BLOCK_CHAIN_DECL (parm_decl);
7388     }
7389   tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7390   BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7391     nreverse (tem);
7392   DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7393   DECL_MAX_LOCALS (current_function_decl) = i;
7394 }
7395
7396 /* Called during parsing. Creates an artificial method declaration.  */
7397
7398 static tree
7399 create_artificial_method (class, flags, type, name, args)
7400      tree class;
7401      int flags;
7402      tree type, name, args;
7403 {
7404   tree mdecl;
7405
7406   java_parser_context_save_global ();
7407   lineno = 0;
7408   mdecl = make_node (FUNCTION_TYPE);
7409   TREE_TYPE (mdecl) = type;
7410   TYPE_ARG_TYPES (mdecl) = args;
7411   mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7412   java_parser_context_restore_global ();
7413   DECL_ARTIFICIAL (mdecl) = 1;
7414   return mdecl;
7415 }
7416
7417 /* Starts the body if an artificial method.  */
7418
7419 static void
7420 start_artificial_method_body (mdecl)
7421      tree mdecl;
7422 {
7423   DECL_SOURCE_LINE (mdecl) = 1;
7424   DECL_SOURCE_LINE_MERGE (mdecl, 1);
7425   source_start_java_method (mdecl);
7426   enter_block ();
7427 }
7428
7429 static void
7430 end_artificial_method_body (mdecl)
7431      tree mdecl;
7432 {
7433   /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7434      It has to be evaluated first. (if mdecl is current_function_decl,
7435      we have an undefined behavior if no temporary variable is used.) */
7436   tree b = exit_block ();
7437   BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7438   exit_block ();
7439 }
7440
7441 /* Dump a tree of some kind.  This is a convenience wrapper for the
7442    dump_* functions in tree-dump.c.  */
7443 static void
7444 dump_java_tree (phase, t)
7445      enum tree_dump_index phase;
7446      tree t;
7447 {
7448   FILE *stream;
7449   int flags;
7450
7451   stream = dump_begin (phase, &flags);
7452   if (stream)
7453     {
7454       dump_node (t, flags, stream);
7455       dump_end (phase, stream);
7456     }
7457 }
7458
7459 /* Terminate a function and expand its body.  */
7460
7461 static void
7462 source_end_java_method ()
7463 {
7464   tree fndecl = current_function_decl;
7465
7466   if (!fndecl)
7467     return;
7468
7469   java_parser_context_save_global ();
7470   lineno = ctxp->last_ccb_indent1;
7471
7472   /* Turn function bodies with only a NOP expr null, so they don't get
7473      generated at all and we won't get warnings when using the -W
7474      -Wall flags. */
7475   if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7476     BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7477
7478   /* We've generated all the trees for this function, and it has been
7479      patched.  Dump it to a file if the user requested it.  */
7480   dump_java_tree (TDI_original, fndecl);
7481
7482   java_optimize_inline (fndecl); 
7483
7484   /* Generate function's code */
7485   if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7486       && ! flag_emit_class_files
7487       && ! flag_emit_xref)
7488     expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7489
7490   /* pop out of its parameters */
7491   pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7492   poplevel (1, 0, 1);
7493   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7494
7495   /* Generate rtl for function exit.  */
7496   if (! flag_emit_class_files && ! flag_emit_xref)
7497     {
7498       lineno = DECL_SOURCE_LINE_LAST (fndecl);
7499       expand_function_end (input_filename, lineno, 0);
7500
7501       /* Run the optimizers and output assembler code for this function. */
7502       rest_of_compilation (fndecl);
7503     }
7504
7505   current_function_decl = NULL_TREE;
7506   java_parser_context_restore_global ();
7507 }
7508
7509 /* Record EXPR in the current function block. Complements compound
7510    expression second operand if necessary.  */
7511
7512 tree
7513 java_method_add_stmt (fndecl, expr)
7514      tree fndecl, expr;
7515 {
7516   if (!GET_CURRENT_BLOCK (fndecl))
7517     return NULL_TREE;
7518   return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7519 }
7520
7521 static tree
7522 add_stmt_to_block (b, type, stmt)
7523      tree b, type, stmt;
7524 {
7525   tree body = BLOCK_EXPR_BODY (b), c;
7526
7527   if (java_error_count)
7528     return body;
7529
7530   if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7531     return body;
7532
7533   BLOCK_EXPR_BODY (b) = c;
7534   TREE_SIDE_EFFECTS (c) = 1;
7535   return c;
7536 }
7537
7538 /* Add STMT to EXISTING if possible, otherwise create a new
7539    COMPOUND_EXPR and add STMT to it. */
7540
7541 static tree
7542 add_stmt_to_compound (existing, type, stmt)
7543      tree existing, type, stmt;
7544 {
7545   /* Keep track of this for inlining.  */
7546   if (current_function_decl)
7547     ++DECL_NUM_STMTS (current_function_decl);
7548
7549   if (existing)
7550     return build (COMPOUND_EXPR, type, existing, stmt);
7551   else
7552     return stmt;
7553 }
7554
7555 void java_layout_seen_class_methods ()
7556 {
7557   tree previous_list = all_class_list;
7558   tree end = NULL_TREE;
7559   tree current;
7560
7561   while (1)
7562     {
7563       for (current = previous_list;
7564            current != end; current = TREE_CHAIN (current))
7565         layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7566
7567       if (previous_list != all_class_list)
7568         {
7569           end = previous_list;
7570           previous_list = all_class_list;
7571         }
7572       else
7573         break;
7574     }
7575 }
7576
7577 static GTY(()) tree stop_reordering;
7578 void
7579 java_reorder_fields ()
7580 {
7581   tree current;
7582
7583   for (current = gclass_list; current; current = TREE_CHAIN (current))
7584     {
7585       current_class = TREE_TYPE (TREE_VALUE (current));
7586
7587       if (current_class == stop_reordering)
7588         break;
7589
7590       /* Reverse the fields, but leave the dummy field in front.
7591          Fields are already ordered for Object and Class */
7592       if (TYPE_FIELDS (current_class) && current_class != object_type_node
7593           && current_class != class_type_node)
7594       {
7595         /* If the dummy field is there, reverse the right fields and
7596            just layout the type for proper fields offset */
7597         if (!DECL_NAME (TYPE_FIELDS (current_class)))
7598           {
7599             tree fields = TYPE_FIELDS (current_class);
7600             TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7601             TYPE_SIZE (current_class) = NULL_TREE;
7602           }
7603         /* We don't have a dummy field, we need to layout the class,
7604            after having reversed the fields */
7605         else
7606           {
7607             TYPE_FIELDS (current_class) =
7608               nreverse (TYPE_FIELDS (current_class));
7609             TYPE_SIZE (current_class) = NULL_TREE;
7610           }
7611       }
7612     }
7613   /* There are cases were gclass_list will be empty. */
7614   if (gclass_list)
7615     stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7616 }
7617
7618 /* Layout the methods of all classes loaded in one way or another.
7619    Check methods of source parsed classes. Then reorder the
7620    fields and layout the classes or the type of all source parsed
7621    classes */
7622
7623 void
7624 java_layout_classes ()
7625 {
7626   tree current;
7627   int save_error_count = java_error_count;
7628
7629   /* Layout the methods of all classes seen so far */
7630   java_layout_seen_class_methods ();
7631   java_parse_abort_on_error ();
7632   all_class_list = NULL_TREE;
7633
7634   /* Then check the methods of all parsed classes */
7635   for (current = gclass_list; current; current = TREE_CHAIN (current))
7636     if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7637       java_check_methods (TREE_VALUE (current));
7638   java_parse_abort_on_error ();
7639
7640   for (current = gclass_list; current; current = TREE_CHAIN (current))
7641     {
7642       current_class = TREE_TYPE (TREE_VALUE (current));
7643       layout_class (current_class);
7644
7645       /* Error reported by the caller */
7646       if (java_error_count)
7647         return;
7648     }
7649
7650   /* We might have reloaded classes durign the process of laying out
7651      classes for code generation. We must layout the methods of those
7652      late additions, as constructor checks might use them */
7653   java_layout_seen_class_methods ();
7654   java_parse_abort_on_error ();
7655 }
7656
7657 /* Expand methods in the current set of classes rememebered for
7658    generation.  */
7659
7660 static void
7661 java_complete_expand_classes ()
7662 {
7663   tree current;
7664
7665   do_not_fold = flag_emit_xref;
7666
7667   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7668     if (!INNER_CLASS_DECL_P (current))
7669       java_complete_expand_class (current);
7670 }
7671
7672 /* Expand the methods found in OUTER, starting first by OUTER's inner
7673    classes, if any.  */
7674
7675 static void
7676 java_complete_expand_class (outer)
7677      tree outer;
7678 {
7679   tree inner_list;
7680
7681   set_nested_class_simple_name_value (outer, 1); /* Set */
7682
7683   /* We need to go after all inner classes and start expanding them,
7684      starting with most nested ones. We have to do that because nested
7685      classes might add functions to outer classes */
7686
7687   for (inner_list = DECL_INNER_CLASS_LIST (outer);
7688        inner_list; inner_list = TREE_CHAIN (inner_list))
7689     java_complete_expand_class (TREE_PURPOSE (inner_list));
7690
7691   java_complete_expand_methods (outer);
7692   set_nested_class_simple_name_value (outer, 0); /* Reset */
7693 }
7694
7695 /* Expand methods registered in CLASS_DECL. The general idea is that
7696    we expand regular methods first. This allows us get an estimate on
7697    how outer context local alias fields are really used so we can add
7698    to the constructor just enough code to initialize them properly (it
7699    also lets us generate finit$ correctly.) Then we expand the
7700    constructors and then <clinit>.  */
7701
7702 static void
7703 java_complete_expand_methods (class_decl)
7704      tree class_decl;
7705 {
7706   tree clinit, decl, first_decl;
7707
7708   current_class = TREE_TYPE (class_decl);
7709
7710   /* Initialize a new constant pool */
7711   init_outgoing_cpool ();
7712
7713   /* Pre-expand <clinit> to figure whether we really need it or
7714      not. If we do need it, we pre-expand the static fields so they're
7715      ready to be used somewhere else. <clinit> will be fully expanded
7716      after we processed the constructors. */
7717   first_decl = TYPE_METHODS (current_class);
7718   clinit = maybe_generate_pre_expand_clinit (current_class);
7719
7720   /* Then generate finit$ (if we need to) because constructors will
7721    try to use it.*/
7722   if (TYPE_FINIT_STMT_LIST (current_class))
7723     java_complete_expand_method (generate_finit (current_class));
7724
7725   /* Then generate instinit$ (if we need to) because constructors will
7726      try to use it. */
7727   if (TYPE_II_STMT_LIST (current_class))
7728     java_complete_expand_method (generate_instinit (current_class));
7729
7730   /* Now do the constructors */
7731   for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7732     {
7733       int no_body;
7734
7735       if (!DECL_CONSTRUCTOR_P (decl))
7736         continue;
7737
7738       no_body = !DECL_FUNCTION_BODY (decl);
7739       /* Don't generate debug info on line zero when expanding a
7740          generated constructor. */
7741       if (no_body)
7742         restore_line_number_status (1);
7743
7744       java_complete_expand_method (decl);
7745
7746       if (no_body)
7747         restore_line_number_status (0);
7748     }
7749
7750   /* First, do the ordinary methods. */
7751   for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7752     {
7753       /* Ctors aren't part of this batch. */
7754       if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7755         continue;
7756
7757       /* Skip abstract or native methods -- but do handle native
7758          methods when generating JNI stubs.  */
7759       if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7760         {
7761           DECL_FUNCTION_BODY (decl) = NULL_TREE;
7762           continue;
7763         }
7764
7765       if (METHOD_NATIVE (decl))
7766         {
7767           tree body;
7768           current_function_decl = decl;
7769           body = build_jni_stub (decl);
7770           BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7771         }
7772
7773       java_complete_expand_method (decl);
7774     }
7775
7776   /* If there is indeed a <clinit>, fully expand it now */
7777   if (clinit)
7778     {
7779       /* Prevent the use of `this' inside <clinit> */
7780       ctxp->explicit_constructor_p = 1;
7781       java_complete_expand_method (clinit);
7782       ctxp->explicit_constructor_p = 0;
7783     }
7784
7785   /* We might have generated a class$ that we now want to expand */
7786   if (TYPE_DOT_CLASS (current_class))
7787     java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7788
7789   /* Now verify constructor circularity (stop after the first one we
7790      prove wrong.) */
7791   if (!CLASS_INTERFACE (class_decl))
7792     for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7793       if (DECL_CONSTRUCTOR_P (decl)
7794           && verify_constructor_circularity (decl, decl))
7795         break;
7796
7797   /* Save the constant pool. We'll need to restore it later. */
7798   TYPE_CPOOL (current_class) = outgoing_cpool;
7799 }
7800
7801 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7802    safely used in some other methods/constructors.  */
7803
7804 static tree
7805 maybe_generate_pre_expand_clinit (class_type)
7806      tree class_type;
7807 {
7808   tree current, mdecl;
7809
7810   if (!TYPE_CLINIT_STMT_LIST (class_type))
7811     return NULL_TREE;
7812
7813   /* Go through all static fields and pre expand them */
7814   for (current = TYPE_FIELDS (class_type); current;
7815        current = TREE_CHAIN (current))
7816     if (FIELD_STATIC (current))
7817       build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7818
7819   /* Then build the <clinit> method */
7820   mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7821                                     clinit_identifier_node, end_params_node);
7822   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7823                        mdecl, NULL_TREE);
7824   start_artificial_method_body (mdecl);
7825
7826   /* We process the list of assignment we produced as the result of
7827      the declaration of initialized static field and add them as
7828      statement to the <clinit> method. */
7829   for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7830        current = TREE_CHAIN (current))
7831     {
7832       tree stmt = current;
7833       /* We build the assignment expression that will initialize the
7834          field to its value. There are strict rules on static
7835          initializers (8.5). FIXME */
7836       if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7837         stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7838       java_method_add_stmt (mdecl, stmt);
7839     }
7840
7841   end_artificial_method_body (mdecl);
7842
7843   /* Now we want to place <clinit> as the last method (because we need
7844      it at least for interface so that it doesn't interfere with the
7845      dispatch table based lookup. */
7846   if (TREE_CHAIN (TYPE_METHODS (class_type)))
7847     {
7848       current = TREE_CHAIN (TYPE_METHODS (class_type));
7849       TYPE_METHODS (class_type) = current;
7850
7851       while (TREE_CHAIN (current))
7852         current = TREE_CHAIN (current);
7853
7854       TREE_CHAIN (current) = mdecl;
7855       TREE_CHAIN (mdecl) = NULL_TREE;
7856     }
7857
7858   return mdecl;
7859 }
7860
7861 /* Analyzes a method body and look for something that isn't a
7862    MODIFY_EXPR with a constant value.  */
7863
7864 static int
7865 analyze_clinit_body (this_class, bbody)
7866      tree this_class, bbody;
7867 {
7868   while (bbody)
7869     switch (TREE_CODE (bbody))
7870       {
7871       case BLOCK:
7872         bbody = BLOCK_EXPR_BODY (bbody);
7873         break;
7874
7875       case EXPR_WITH_FILE_LOCATION:
7876         bbody = EXPR_WFL_NODE (bbody);
7877         break;
7878
7879       case COMPOUND_EXPR:
7880         if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
7881           return 1;
7882         bbody = TREE_OPERAND (bbody, 1);
7883         break;
7884
7885       case MODIFY_EXPR:
7886         /* If we're generating to class file and we're dealing with an
7887            array initialization, we return 1 to keep <clinit> */
7888         if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
7889             && flag_emit_class_files)
7890           return 1;
7891
7892         /* There are a few cases where we're required to keep
7893            <clinit>:
7894            - If this is an assignment whose operand is not constant,
7895            - If this is an assignment to a non-initialized field,
7896            - If this field is not a member of the current class.
7897         */
7898         return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
7899                 || ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
7900                 || DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
7901
7902       default:
7903         return 1;
7904       }
7905   return 0;
7906 }
7907
7908
7909 /* See whether we could get rid of <clinit>. Criteria are: all static
7910    final fields have constant initial values and the body of <clinit>
7911    is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7912
7913 static int
7914 maybe_yank_clinit (mdecl)
7915      tree mdecl;
7916 {
7917   tree type, current;
7918   tree fbody, bbody;
7919
7920   if (!DECL_CLINIT_P (mdecl))
7921     return 0;
7922
7923   /* If the body isn't empty, then we keep <clinit>. Note that if
7924      we're emitting classfiles, this isn't enough not to rule it
7925      out. */
7926   fbody = DECL_FUNCTION_BODY (mdecl);
7927   bbody = BLOCK_EXPR_BODY (fbody);
7928   if (bbody && bbody != error_mark_node)
7929     bbody = BLOCK_EXPR_BODY (bbody);
7930   else
7931     return 0;
7932   if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7933     return 0;
7934
7935   type = DECL_CONTEXT (mdecl);
7936   current = TYPE_FIELDS (type);
7937
7938   for (current = (current ? TREE_CHAIN (current) : current);
7939        current; current = TREE_CHAIN (current))
7940     {
7941       tree f_init;
7942
7943       /* We're not interested in non-static fields.  */
7944       if (!FIELD_STATIC (current))
7945         continue;
7946
7947       /* Nor in fields without initializers. */
7948       f_init = DECL_INITIAL (current);
7949       if (f_init == NULL_TREE)
7950         continue;
7951
7952       /* Anything that isn't String or a basic type is ruled out -- or
7953          if we know how to deal with it (when doing things natively) we
7954          should generated an empty <clinit> so that SUID are computed
7955          correctly. */
7956       if (! JSTRING_TYPE_P (TREE_TYPE (current))
7957           && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7958         return 0;
7959
7960       if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
7961         return 0;
7962     }
7963
7964   /* Now we analyze the method body and look for something that
7965      isn't a MODIFY_EXPR */
7966   if (bbody != empty_stmt_node && analyze_clinit_body (type, bbody))
7967     return 0;
7968
7969   /* Get rid of <clinit> in the class' list of methods */
7970   if (TYPE_METHODS (type) == mdecl)
7971     TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7972   else
7973     for (current = TYPE_METHODS (type); current;
7974          current = TREE_CHAIN (current))
7975       if (TREE_CHAIN (current) == mdecl)
7976         {
7977           TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7978           break;
7979         }
7980
7981   return 1;
7982 }
7983
7984 /* Install the argument from MDECL. Suitable to completion and
7985    expansion of mdecl's body.  */
7986
7987 static void
7988 start_complete_expand_method (mdecl)
7989      tree mdecl;
7990 {
7991   tree tem;
7992
7993   pushlevel (1);                /* Prepare for a parameter push */
7994   tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7995   DECL_ARGUMENTS (mdecl) = tem;
7996
7997   for (; tem; tem = TREE_CHAIN (tem))
7998     {
7999       /* TREE_CHAIN (tem) will change after pushdecl. */
8000       tree next = TREE_CHAIN (tem);
8001       tree type = TREE_TYPE (tem);
8002       if (PROMOTE_PROTOTYPES
8003           && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8004           && INTEGRAL_TYPE_P (type))
8005         type = integer_type_node;
8006       DECL_ARG_TYPE (tem) = type;
8007       layout_decl (tem, 0);
8008       pushdecl (tem);
8009       /* Re-install the next so that the list is kept and the loop
8010          advances. */
8011       TREE_CHAIN (tem) = next;
8012     }
8013   pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8014   lineno = DECL_SOURCE_LINE_FIRST (mdecl);
8015   build_result_decl (mdecl);
8016 }
8017
8018
8019 /* Complete and expand a method.  */
8020
8021 static void
8022 java_complete_expand_method (mdecl)
8023      tree mdecl;
8024 {
8025   tree fbody, block_body, exception_copy;
8026
8027   current_function_decl = mdecl;
8028   /* Fix constructors before expanding them */
8029   if (DECL_CONSTRUCTOR_P (mdecl))
8030     fix_constructors (mdecl);
8031
8032   /* Expand functions that have a body */
8033   if (!DECL_FUNCTION_BODY (mdecl))
8034     return;
8035
8036   fbody = DECL_FUNCTION_BODY (mdecl);
8037   block_body = BLOCK_EXPR_BODY (fbody);
8038   exception_copy = NULL_TREE;
8039
8040   current_function_decl = mdecl;
8041
8042   if (! quiet_flag)
8043     fprintf (stderr, " [%s.",
8044              lang_printable_name (DECL_CONTEXT (mdecl), 0));
8045   announce_function (mdecl);
8046   if (! quiet_flag)
8047     fprintf (stderr, "]");
8048
8049   /* Prepare the function for tree completion */
8050   start_complete_expand_method (mdecl);
8051
8052   /* Install the current this */
8053   current_this = (!METHOD_STATIC (mdecl) ?
8054                   BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
8055
8056   /* Purge the `throws' list of unchecked exceptions (we save a copy
8057      of the list and re-install it later.) */
8058   exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
8059   purge_unchecked_exceptions (mdecl);
8060
8061   /* Install exceptions thrown with `throws' */
8062   PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
8063
8064   if (block_body != NULL_TREE)
8065     {
8066       block_body = java_complete_tree (block_body);
8067
8068       /* Before we check initialization, attached all class initialization
8069          variable to the block_body */
8070       htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8071                      attach_init_test_initialization_flags, block_body);
8072
8073       if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
8074         {
8075           check_for_initialization (block_body, mdecl);
8076
8077           /* Go through all the flags marking the initialization of
8078              static variables and see whether they're definitively
8079              assigned, in which case the type is remembered as
8080              definitively initialized in MDECL. */
8081           if (STATIC_CLASS_INIT_OPT_P ())
8082             {
8083               /* Always register the context as properly initialized in
8084                  MDECL. This used with caution helps removing extra
8085                  initialization of self. */
8086               if (METHOD_STATIC (mdecl))
8087                 {
8088                   *(htab_find_slot
8089                     (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
8090                      DECL_CONTEXT (mdecl), INSERT)) = DECL_CONTEXT (mdecl);
8091                 }
8092             }
8093         }
8094       ctxp->explicit_constructor_p = 0;
8095     }
8096
8097   BLOCK_EXPR_BODY (fbody) = block_body;
8098
8099   /* If we saw a return but couldn't evaluate it properly, we'll have
8100      an error_mark_node here. */
8101   if (block_body != error_mark_node
8102       && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
8103       && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
8104       && !flag_emit_xref)
8105     missing_return_error (current_function_decl);
8106
8107   /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
8108   maybe_yank_clinit (mdecl);
8109
8110   /* Pop the current level, with special measures if we found errors. */
8111   if (java_error_count)
8112     pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8113   poplevel (1, 0, 1);
8114
8115   /* Pop the exceptions and sanity check */
8116   POP_EXCEPTIONS();
8117   if (currently_caught_type_list)
8118     abort ();
8119
8120   /* Restore the copy of the list of exceptions if emitting xrefs. */
8121   DECL_FUNCTION_THROWS (mdecl) = exception_copy;
8122 }
8123
8124 /* For with each class for which there's code to generate. */
8125
8126 static void
8127 java_expand_method_bodies (class)
8128      tree class;
8129 {
8130   tree decl;
8131   for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8132     {
8133       if (!DECL_FUNCTION_BODY (decl))
8134         continue;
8135
8136       current_function_decl = decl;
8137
8138       /* Save the function for inlining.  */
8139       if (flag_inline_trees)
8140         DECL_SAVED_TREE (decl) = 
8141           BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
8142       
8143       /* It's time to assign the variable flagging static class
8144          initialization based on which classes invoked static methods
8145          are definitely initializing. This should be flagged. */
8146       if (STATIC_CLASS_INIT_OPT_P ())
8147         {
8148           tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
8149           for (; list != NULL_TREE;  list = TREE_CHAIN (list))
8150             {
8151               /* Executed for each statement calling a static function.
8152                  LIST is a TREE_LIST whose PURPOSE is the called function
8153                  and VALUE is a compound whose second operand can be patched
8154                  with static class initialization flag assignments.  */
8155
8156               tree called_method = TREE_PURPOSE (list);
8157               tree compound = TREE_VALUE (list);
8158               tree assignment_compound_list
8159                 = build_tree_list (called_method, NULL);
8160
8161               /* For each class definitely initialized in
8162                  CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8163                  assignment to the class initialization flag. */
8164               htab_traverse (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8165                              emit_test_initialization,
8166                              assignment_compound_list);
8167
8168               if (TREE_VALUE (assignment_compound_list))
8169                 TREE_OPERAND (compound, 1)
8170                   = TREE_VALUE (assignment_compound_list);
8171             }
8172         }
8173
8174       /* Prepare the function for RTL expansion */
8175       start_complete_expand_method (decl);
8176
8177       /* Expand function start, generate initialization flag
8178          assignment, and handle synchronized methods. */
8179       complete_start_java_method (decl);
8180
8181       /* Expand the rest of the function body and terminate
8182          expansion. */
8183       source_end_java_method ();
8184     }
8185 }
8186
8187 \f
8188
8189 /* This section of the code deals with accessing enclosing context
8190    fields either directly by using the relevant access to this$<n> or
8191    by invoking an access method crafted for that purpose.  */
8192
8193 /* Build the necessary access from an inner class to an outer
8194    class. This routine could be optimized to cache previous result
8195    (decl, current_class and returned access).  When an access method
8196    needs to be generated, it always takes the form of a read. It might
8197    be later turned into a write by calling outer_field_access_fix.  */
8198
8199 static tree
8200 build_outer_field_access (id, decl)
8201      tree id, decl;
8202 {
8203   tree access = NULL_TREE;
8204   tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8205   tree decl_ctx = DECL_CONTEXT (decl);
8206
8207   /* If the immediate enclosing context of the current class is the
8208      field decl's class or inherits from it; build the access as
8209      `this$<n>.<field>'. Note that we will break the `private' barrier
8210      if we're not emitting bytecodes. */
8211   if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8212       && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
8213     {
8214       tree thisn = build_current_thisn (current_class);
8215       access = make_qualified_primary (build_wfl_node (thisn),
8216                                        id, EXPR_WFL_LINECOL (id));
8217     }
8218   /* Otherwise, generate access methods to outer this and access the
8219      field (either using an access method or by direct access.) */
8220   else
8221     {
8222       int lc = EXPR_WFL_LINECOL (id);
8223
8224       /* Now we chain the required number of calls to the access$0 to
8225          get a hold to the enclosing instance we need, and then we
8226          build the field access. */
8227       access = build_access_to_thisn (current_class, decl_ctx, lc);
8228
8229       /* If the field is private and we're generating bytecode, then
8230          we generate an access method */
8231       if (FIELD_PRIVATE (decl) && flag_emit_class_files )
8232         {
8233           tree name = build_outer_field_access_methods (decl);
8234           access = build_outer_field_access_expr (lc, decl_ctx,
8235                                                   name, access, NULL_TREE);
8236         }
8237       /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
8238          Once again we break the `private' access rule from a foreign
8239          class. */
8240       else
8241         access = make_qualified_primary (access, id, lc);
8242     }
8243   return resolve_expression_name (access, NULL);
8244 }
8245
8246 /* Return a non zero value if NODE describes an outer field inner
8247    access.  */
8248
8249 static int
8250 outer_field_access_p (type, decl)
8251     tree type, decl;
8252 {
8253   if (!INNER_CLASS_TYPE_P (type)
8254       || TREE_CODE (decl) != FIELD_DECL
8255       || DECL_CONTEXT (decl) == type)
8256     return 0;
8257
8258   /* If the inner class extends the declaration context of the field
8259      we're try to acces, then this isn't an outer field access */
8260   if (inherits_from_p (type, DECL_CONTEXT (decl)))
8261     return 0;
8262
8263   for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
8264        type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
8265     {
8266       if (type == DECL_CONTEXT (decl))
8267         return 1;
8268
8269       if (!DECL_CONTEXT (TYPE_NAME (type)))
8270         {
8271           /* Before we give up, see whether the field is inherited from
8272              the enclosing context we're considering. */
8273           if (inherits_from_p (type, DECL_CONTEXT (decl)))
8274             return 1;
8275           break;
8276         }
8277     }
8278
8279   return 0;
8280 }
8281
8282 /* Return a non zero value if NODE represents an outer field inner
8283    access that was been already expanded. As a side effect, it returns
8284    the name of the field being accessed and the argument passed to the
8285    access function, suitable for a regeneration of the access method
8286    call if necessary. */
8287
8288 static int
8289 outer_field_expanded_access_p (node, name, arg_type, arg)
8290     tree node, *name, *arg_type, *arg;
8291 {
8292   int identified = 0;
8293
8294   if (TREE_CODE (node) != CALL_EXPR)
8295     return 0;
8296
8297   /* Well, gcj generates slightly different tree nodes when compiling
8298      to native or bytecodes. It's the case for function calls. */
8299
8300   if (flag_emit_class_files
8301       && TREE_CODE (node) == CALL_EXPR
8302       && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8303     identified = 1;
8304   else if (!flag_emit_class_files)
8305     {
8306       node = TREE_OPERAND (node, 0);
8307
8308       if (node && TREE_OPERAND (node, 0)
8309           && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8310         {
8311           node = TREE_OPERAND (node, 0);
8312           if (TREE_OPERAND (node, 0)
8313               && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8314               && (OUTER_FIELD_ACCESS_IDENTIFIER_P
8315                   (DECL_NAME (TREE_OPERAND (node, 0)))))
8316             identified = 1;
8317         }
8318     }
8319
8320   if (identified && name && arg_type && arg)
8321     {
8322       tree argument = TREE_OPERAND (node, 1);
8323       *name = DECL_NAME (TREE_OPERAND (node, 0));
8324       *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8325       *arg = TREE_VALUE (argument);
8326     }
8327   return identified;
8328 }
8329
8330 /* Detect in NODE an outer field read access from an inner class and
8331    transform it into a write with RHS as an argument. This function is
8332    called from the java_complete_lhs when an assignment to a LHS can
8333    be identified. */
8334
8335 static tree
8336 outer_field_access_fix (wfl, node, rhs)
8337     tree wfl, node, rhs;
8338 {
8339   tree name, arg_type, arg;
8340
8341   if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
8342     {
8343       node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
8344                                             arg_type, name, arg, rhs);
8345       return java_complete_tree (node);
8346     }
8347   return NULL_TREE;
8348 }
8349
8350 /* Construct the expression that calls an access method:
8351      <type>.access$<n>(<arg1> [, <arg2>]);
8352
8353    ARG2 can be NULL and will be omitted in that case. It will denote a
8354    read access.  */
8355
8356 static tree
8357 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
8358     int lc;
8359     tree type, access_method_name, arg1, arg2;
8360 {
8361   tree args, cn, access;
8362
8363   args = arg1 ? arg1 :
8364     build_wfl_node (build_current_thisn (current_class));
8365   args = build_tree_list (NULL_TREE, args);
8366
8367   if (arg2)
8368     args = tree_cons (NULL_TREE, arg2, args);
8369
8370   access = build_method_invocation (build_wfl_node (access_method_name), args);
8371   cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8372   return make_qualified_primary (cn, access, lc);
8373 }
8374
8375 static tree
8376 build_new_access_id ()
8377 {
8378   static int access_n_counter = 1;
8379   char buffer [128];
8380
8381   sprintf (buffer, "access$%d", access_n_counter++);
8382   return get_identifier (buffer);
8383 }
8384
8385 /* Create the static access functions for the outer field DECL. We define a
8386    read:
8387      TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8388        return inst$.field;
8389      }
8390    and a write access:
8391      TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8392                                      TREE_TYPE (<field>) value$) {
8393        return inst$.field = value$;
8394      }
8395    We should have a usage flags on the DECL so we can lazily turn the ones
8396    we're using for code generation. FIXME.
8397 */
8398
8399 static tree
8400 build_outer_field_access_methods (decl)
8401     tree decl;
8402 {
8403   tree id, args, stmt, mdecl;
8404
8405   if (FIELD_INNER_ACCESS_P (decl))
8406     return FIELD_INNER_ACCESS (decl);
8407
8408   MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8409
8410   /* Create the identifier and a function named after it. */
8411   id = build_new_access_id ();
8412
8413   /* The identifier is marked as bearing the name of a generated write
8414      access function for outer field accessed from inner classes. */
8415   OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8416
8417   /* Create the read access */
8418   args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8419   TREE_CHAIN (args) = end_params_node;
8420   stmt = make_qualified_primary (build_wfl_node (inst_id),
8421                                  build_wfl_node (DECL_NAME (decl)), 0);
8422   stmt = build_return (0, stmt);
8423   mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8424                                            TREE_TYPE (decl), id, args, stmt);
8425   DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8426
8427   /* Create the write access method. No write access for final variable */
8428   if (!FIELD_FINAL (decl))
8429     {
8430       args = build_tree_list (inst_id,
8431                               build_pointer_type (DECL_CONTEXT (decl)));
8432       TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8433       TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8434       stmt = make_qualified_primary (build_wfl_node (inst_id),
8435                                      build_wfl_node (DECL_NAME (decl)), 0);
8436       stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8437                                                 build_wfl_node (wpv_id)));
8438       mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8439                                                TREE_TYPE (decl), id,
8440                                                args, stmt);
8441     }
8442   DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8443
8444   /* Return the access name */
8445   return FIELD_INNER_ACCESS (decl) = id;
8446 }
8447
8448 /* Build an field access method NAME.  */
8449
8450 static tree
8451 build_outer_field_access_method (class, type, name, args, body)
8452     tree class, type, name, args, body;
8453 {
8454   tree saved_current_function_decl, mdecl;
8455
8456   /* Create the method */
8457   mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8458   fix_method_argument_names (args, mdecl);
8459   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8460
8461   /* Attach the method body. */
8462   saved_current_function_decl = current_function_decl;
8463   start_artificial_method_body (mdecl);
8464   java_method_add_stmt (mdecl, body);
8465   end_artificial_method_body (mdecl);
8466   current_function_decl = saved_current_function_decl;
8467
8468   return mdecl;
8469 }
8470
8471 \f
8472 /* This section deals with building access function necessary for
8473    certain kinds of method invocation from inner classes.  */
8474
8475 static tree
8476 build_outer_method_access_method (decl)
8477     tree decl;
8478 {
8479   tree saved_current_function_decl, mdecl;
8480   tree args = NULL_TREE, call_args = NULL_TREE;
8481   tree carg, id, body, class;
8482   char buffer [80];
8483   int parm_id_count = 0;
8484
8485   /* Test this abort with an access to a private field */
8486   if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8487     abort ();
8488
8489   /* Check the cache first */
8490   if (DECL_FUNCTION_INNER_ACCESS (decl))
8491     return DECL_FUNCTION_INNER_ACCESS (decl);
8492
8493   class = DECL_CONTEXT (decl);
8494
8495   /* Obtain an access identifier and mark it */
8496   id = build_new_access_id ();
8497   OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8498
8499   carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8500   /* Create the arguments, as much as the original */
8501   for (; carg && carg != end_params_node;
8502        carg = TREE_CHAIN (carg))
8503     {
8504       sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8505       args = chainon (args, build_tree_list (get_identifier (buffer),
8506                                              TREE_VALUE (carg)));
8507     }
8508   args = chainon (args, end_params_node);
8509
8510   /* Create the method */
8511   mdecl = create_artificial_method (class, ACC_STATIC,
8512                                     TREE_TYPE (TREE_TYPE (decl)), id, args);
8513   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8514   /* There is a potential bug here. We should be able to use
8515      fix_method_argument_names, but then arg names get mixed up and
8516      eventually a constructor will have its this$0 altered and the
8517      outer context won't be assignment properly. The test case is
8518      stub.java FIXME */
8519   TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8520
8521   /* Attach the method body. */
8522   saved_current_function_decl = current_function_decl;
8523   start_artificial_method_body (mdecl);
8524
8525   /* The actual method invocation uses the same args. When invoking a
8526      static methods that way, we don't want to skip the first
8527      argument. */
8528   carg = args;
8529   if (!METHOD_STATIC (decl))
8530     carg = TREE_CHAIN (carg);
8531   for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8532     call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8533                            call_args);
8534
8535   body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8536                                   call_args);
8537   if (!METHOD_STATIC (decl))
8538     body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8539                                    body, 0);
8540   if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8541     body = build_return (0, body);
8542   java_method_add_stmt (mdecl,body);
8543   end_artificial_method_body (mdecl);
8544   current_function_decl = saved_current_function_decl;
8545
8546   /* Back tag the access function so it know what it accesses */
8547   DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8548
8549   /* Tag the current method so it knows it has an access generated */
8550   return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8551 }
8552
8553 \f
8554 /* This section of the code deals with building expressions to access
8555    the enclosing instance of an inner class. The enclosing instance is
8556    kept in a generated field called this$<n>, with <n> being the
8557    inner class nesting level (starting from 0.)  */
8558
8559 /* Build an access to a given this$<n>, always chaining access call to
8560    others. Access methods to this$<n> are build on the fly if
8561    necessary. This CAN'T be used to solely access this$<n-1> from
8562    this$<n> (which alway yield to special cases and optimization, see
8563    for example build_outer_field_access).  */
8564
8565 static tree
8566 build_access_to_thisn (from, to, lc)
8567      tree from, to;
8568      int lc;
8569 {
8570   tree access = NULL_TREE;
8571
8572   while (from != to && PURE_INNER_CLASS_TYPE_P (from))
8573     {
8574       if (!access)
8575         {
8576           access = build_current_thisn (from);
8577           access = build_wfl_node (access);
8578         }
8579       else
8580         {
8581           tree access0_wfl, cn;
8582
8583           maybe_build_thisn_access_method (from);
8584           access0_wfl = build_wfl_node (access0_identifier_node);
8585           cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8586           EXPR_WFL_LINECOL (access0_wfl) = lc;
8587           access = build_tree_list (NULL_TREE, access);
8588           access = build_method_invocation (access0_wfl, access);
8589           access = make_qualified_primary (cn, access, lc);
8590         }
8591
8592       /* If FROM isn't an inner class, that's fine, we've done enough.
8593          What we're looking for can be accessed from there.  */
8594       from = DECL_CONTEXT (TYPE_NAME (from));
8595       if (!from)
8596         break;
8597       from = TREE_TYPE (from);
8598     }
8599   return access;
8600 }
8601
8602 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8603    is returned if nothing needs to be generated. Otherwise, the method
8604    generated and a method decl is returned.
8605
8606    NOTE: These generated methods should be declared in a class file
8607    attribute so that they can't be referred to directly.  */
8608
8609 static tree
8610 maybe_build_thisn_access_method (type)
8611     tree type;
8612 {
8613   tree mdecl, args, stmt, rtype;
8614   tree saved_current_function_decl;
8615
8616   /* If TYPE is a top-level class, no access method is required.
8617      If there already is such an access method, bail out. */
8618   if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8619     return NULL_TREE;
8620
8621   /* We generate the method. The method looks like:
8622      static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8623   */
8624   args = build_tree_list (inst_id, build_pointer_type (type));
8625   TREE_CHAIN (args) = end_params_node;
8626   rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8627   mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8628                                     access0_identifier_node, args);
8629   fix_method_argument_names (args, mdecl);
8630   layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8631   stmt = build_current_thisn (type);
8632   stmt = make_qualified_primary (build_wfl_node (inst_id),
8633                                  build_wfl_node (stmt), 0);
8634   stmt = build_return (0, stmt);
8635
8636   saved_current_function_decl = current_function_decl;
8637   start_artificial_method_body (mdecl);
8638   java_method_add_stmt (mdecl, stmt);
8639   end_artificial_method_body (mdecl);
8640   current_function_decl = saved_current_function_decl;
8641
8642   CLASS_ACCESS0_GENERATED_P (type) = 1;
8643
8644   return mdecl;
8645 }
8646
8647 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8648    the first level of innerclassing. this$1 for the next one, etc...
8649    This function can be invoked with TYPE to NULL, available and then
8650    has to count the parser context.  */
8651
8652 static GTY(()) tree saved_thisn;
8653 static GTY(()) tree saved_type;
8654
8655 static tree
8656 build_current_thisn (type)
8657     tree type;
8658 {
8659   static int saved_i = -1;
8660   static int saved_type_i = 0;
8661   tree decl;
8662   char buffer [24];
8663   int i = 0;
8664
8665   if (type)
8666     {
8667       if (type == saved_type)
8668         i = saved_type_i;
8669       else
8670         {
8671           for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8672                decl; decl = DECL_CONTEXT (decl), i++)
8673             ;
8674
8675           saved_type = type;
8676           saved_type_i = i;
8677         }
8678     }
8679   else
8680     i = list_length (GET_CPC_LIST ())-2;
8681
8682   if (i == saved_i)
8683     return saved_thisn;
8684
8685   sprintf (buffer, "this$%d", i);
8686   saved_i = i;
8687   saved_thisn = get_identifier (buffer);
8688   return saved_thisn;
8689 }
8690
8691 /* Return the assignement to the hidden enclosing context `this$<n>'
8692    by the second incoming parameter to the innerclass constructor. The
8693    form used is `this.this$<n> = this$<n>;'.  */
8694
8695 static tree
8696 build_thisn_assign ()
8697 {
8698   if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8699     {
8700       tree thisn = build_current_thisn (current_class);
8701       tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8702                                          build_wfl_node (thisn), 0);
8703       tree rhs = build_wfl_node (thisn);
8704       EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8705       return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8706     }
8707   return NULL_TREE;
8708 }
8709
8710 \f
8711 /* Building the synthetic `class$' used to implement the `.class' 1.1
8712    extension for non primitive types. This method looks like:
8713
8714     static Class class$(String type) throws NoClassDefFoundError
8715     {
8716       try {return (java.lang.Class.forName (String));}
8717       catch (ClassNotFoundException e) {
8718         throw new NoClassDefFoundError(e.getMessage());}
8719     } */
8720
8721 static GTY(()) tree get_message_wfl;
8722 static GTY(()) tree type_parm_wfl;
8723
8724 static tree
8725 build_dot_class_method (class)
8726      tree class;
8727 {
8728 #define BWF(S) build_wfl_node (get_identifier ((S)))
8729 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8730   tree args, tmp, saved_current_function_decl, mdecl;
8731   tree stmt, throw_stmt;
8732
8733   if (!get_message_wfl)
8734     {
8735       get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8736       type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8737     }
8738
8739   /* Build the arguments */
8740   args = build_tree_list (get_identifier ("type$"),
8741                           build_pointer_type (string_type_node));
8742   TREE_CHAIN (args) = end_params_node;
8743
8744   /* Build the qualified name java.lang.Class.forName */
8745   tmp = MQN (MQN (MQN (BWF ("java"),
8746                        BWF ("lang")), BWF ("Class")), BWF ("forName"));
8747   load_class (class_not_found_type_node, 1);
8748   load_class (no_class_def_found_type_node, 1);
8749
8750   /* Create the "class$" function */
8751   mdecl = create_artificial_method (class, ACC_STATIC,
8752                                     build_pointer_type (class_type_node),
8753                                     classdollar_identifier_node, args);
8754   DECL_FUNCTION_THROWS (mdecl) =
8755     build_tree_list (NULL_TREE, no_class_def_found_type_node);
8756
8757   /* We start by building the try block. We need to build:
8758        return (java.lang.Class.forName (type)); */
8759   stmt = build_method_invocation (tmp,
8760                                   build_tree_list (NULL_TREE, type_parm_wfl));
8761   stmt = build_return (0, stmt);
8762
8763   /* Now onto the catch block. We start by building the expression
8764      throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8765   throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8766                                     get_message_wfl, 0);
8767   throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8768
8769   /* Build new NoClassDefFoundError (_.getMessage) */
8770   throw_stmt = build_new_invocation
8771     (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8772      build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8773
8774   /* Build the throw, (it's too early to use BUILD_THROW) */
8775   throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8776
8777   /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8778   stmt = encapsulate_with_try_catch (0, class_not_found_type_node,
8779                                      stmt, throw_stmt);
8780
8781   fix_method_argument_names (args, mdecl);
8782   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8783   saved_current_function_decl = current_function_decl;
8784   start_artificial_method_body (mdecl);
8785   java_method_add_stmt (mdecl, stmt);
8786   end_artificial_method_body (mdecl);
8787   current_function_decl = saved_current_function_decl;
8788   TYPE_DOT_CLASS (class) = mdecl;
8789
8790   return mdecl;
8791 }
8792
8793 static tree
8794 build_dot_class_method_invocation (type)
8795      tree type;
8796 {
8797   tree sig_id, s;
8798
8799   if (TYPE_ARRAY_P (type))
8800     sig_id = build_java_signature (type);
8801   else
8802     sig_id = DECL_NAME (TYPE_NAME (type));
8803
8804   /* Ensure that the proper name separator is used */
8805   sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
8806                                IDENTIFIER_LENGTH (sig_id));
8807
8808   s = build_string (IDENTIFIER_LENGTH (sig_id),
8809                     IDENTIFIER_POINTER (sig_id));
8810   return build_method_invocation (build_wfl_node (classdollar_identifier_node),
8811                                   build_tree_list (NULL_TREE, s));
8812 }
8813
8814 /* This section of the code deals with constructor.  */
8815
8816 /* Craft a body for default constructor. Patch existing constructor
8817    bodies with call to super() and field initialization statements if
8818    necessary.  */
8819
8820 static void
8821 fix_constructors (mdecl)
8822      tree mdecl;
8823 {
8824   tree iii;                     /* Instance Initializer Invocation */
8825   tree body = DECL_FUNCTION_BODY (mdecl);
8826   tree thisn_assign, compound = NULL_TREE;
8827   tree class_type = DECL_CONTEXT (mdecl);
8828
8829   if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
8830     return;
8831   DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
8832
8833   if (!body)
8834     {
8835       /* It is an error for the compiler to generate a default
8836          constructor if the superclass doesn't have a constructor that
8837          takes no argument, or the same args for an anonymous class */
8838       if (verify_constructor_super (mdecl))
8839         {
8840           tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8841           tree save = DECL_NAME (mdecl);
8842           const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8843           DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8844           parse_error_context
8845             (lookup_cl (TYPE_NAME (class_type)),
8846              "No constructor matching `%s' found in class `%s'",
8847              lang_printable_name (mdecl, 0), n);
8848           DECL_NAME (mdecl) = save;
8849         }
8850
8851       /* The constructor body must be crafted by hand. It's the
8852          constructor we defined when we realize we didn't have the
8853          CLASSNAME() constructor */
8854       start_artificial_method_body (mdecl);
8855
8856       /* Insert an assignment to the this$<n> hidden field, if
8857          necessary */
8858       if ((thisn_assign = build_thisn_assign ()))
8859         java_method_add_stmt (mdecl, thisn_assign);
8860
8861       /* We don't generate a super constructor invocation if we're
8862          compiling java.lang.Object. build_super_invocation takes care
8863          of that. */
8864       java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8865
8866       /* FIXME */
8867       if ((iii = build_instinit_invocation (class_type)))
8868         java_method_add_stmt (mdecl, iii);
8869
8870       end_artificial_method_body (mdecl);
8871     }
8872   /* Search for an explicit constructor invocation */
8873   else
8874     {
8875       int found = 0;
8876       int invokes_this = 0;
8877       tree found_call = NULL_TREE;
8878       tree main_block = BLOCK_EXPR_BODY (body);
8879
8880       while (body)
8881         switch (TREE_CODE (body))
8882           {
8883           case CALL_EXPR:
8884             found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8885             if (CALL_THIS_CONSTRUCTOR_P (body))
8886               invokes_this = 1;
8887             body = NULL_TREE;
8888             break;
8889           case COMPOUND_EXPR:
8890           case EXPR_WITH_FILE_LOCATION:
8891             found_call = body;
8892             body = TREE_OPERAND (body, 0);
8893             break;
8894           case BLOCK:
8895             found_call = body;
8896             body = BLOCK_EXPR_BODY (body);
8897             break;
8898           default:
8899             found = 0;
8900             body = NULL_TREE;
8901           }
8902
8903       /* Generate the assignment to this$<n>, if necessary */
8904       if ((thisn_assign = build_thisn_assign ()))
8905         compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8906
8907       /* The constructor is missing an invocation of super() */
8908       if (!found)
8909         compound = add_stmt_to_compound (compound, NULL_TREE,
8910                                          build_super_invocation (mdecl));
8911       /* Explicit super() invokation should take place before the
8912          instance initializer blocks. */
8913       else
8914         {
8915           compound = add_stmt_to_compound (compound, NULL_TREE,
8916                                            TREE_OPERAND (found_call, 0));
8917           TREE_OPERAND (found_call, 0) = empty_stmt_node;
8918         }
8919
8920       DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
8921
8922       /* Insert the instance initializer block right after. */
8923       if (!invokes_this && (iii = build_instinit_invocation (class_type)))
8924         compound = add_stmt_to_compound (compound, NULL_TREE, iii);
8925
8926       /* Fix the constructor main block if we're adding extra stmts */
8927       if (compound)
8928         {
8929           compound = add_stmt_to_compound (compound, NULL_TREE,
8930                                            BLOCK_EXPR_BODY (main_block));
8931           BLOCK_EXPR_BODY (main_block) = compound;
8932         }
8933     }
8934 }
8935
8936 /* Browse constructors in the super class, searching for a constructor
8937    that doesn't take any argument. Return 0 if one is found, 1
8938    otherwise.  If the current class is an anonymous inner class, look
8939    for something that has the same signature. */
8940
8941 static int
8942 verify_constructor_super (mdecl)
8943      tree mdecl;
8944 {
8945   tree class = CLASSTYPE_SUPER (current_class);
8946   int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8947   tree sdecl;
8948
8949   if (!class)
8950     return 0;
8951
8952   if (ANONYMOUS_CLASS_P (current_class))
8953     {
8954       tree mdecl_arg_type;
8955       SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8956       for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8957         if (DECL_CONSTRUCTOR_P (sdecl))
8958           {
8959             tree m_arg_type;
8960             tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8961             if (super_inner)
8962               arg_type = TREE_CHAIN (arg_type);
8963             for (m_arg_type = mdecl_arg_type;
8964                  (arg_type != end_params_node
8965                   && m_arg_type != end_params_node);
8966                  arg_type = TREE_CHAIN (arg_type),
8967                    m_arg_type = TREE_CHAIN (m_arg_type))
8968               if (!valid_method_invocation_conversion_p
8969                      (TREE_VALUE (arg_type),
8970                       TREE_VALUE (m_arg_type)))
8971                 break;
8972
8973             if (arg_type == end_params_node && m_arg_type == end_params_node)
8974               return 0;
8975           }
8976     }
8977   else
8978     {
8979       for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8980         {
8981           tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8982           if (super_inner)
8983             arg = TREE_CHAIN (arg);
8984           if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8985             return 0;
8986         }
8987     }
8988   return 1;
8989 }
8990
8991 /* Generate code for all context remembered for code generation.  */
8992
8993 static GTY(()) tree reversed_class_list;
8994 void
8995 java_expand_classes ()
8996 {
8997   int save_error_count = 0;
8998   static struct parser_ctxt *cur_ctxp = NULL;
8999
9000   java_parse_abort_on_error ();
9001   if (!(ctxp = ctxp_for_generation))
9002     return;
9003   java_layout_classes ();
9004   java_parse_abort_on_error ();
9005
9006   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9007     {
9008       ctxp = cur_ctxp;
9009       input_filename = ctxp->filename;
9010       lang_init_source (2);            /* Error msgs have method prototypes */
9011       java_complete_expand_classes (); /* Complete and expand classes */
9012       java_parse_abort_on_error ();
9013     }
9014   input_filename = main_input_filename;
9015
9016
9017   /* Find anonymous classes and expand their constructor. This extra pass is
9018      neccessary because the constructor itself is only generated when the
9019      method in which it is defined is expanded. */
9020   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9021     {
9022       tree current;
9023       ctxp = cur_ctxp;
9024       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9025         {
9026           current_class = TREE_TYPE (current);
9027           if (ANONYMOUS_CLASS_P (current_class))
9028             {
9029               tree d;
9030               for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9031                 {
9032                   if (DECL_CONSTRUCTOR_P (d))
9033                     {
9034                       restore_line_number_status (1);
9035                       java_complete_expand_method (d);
9036                       restore_line_number_status (0);
9037                       break;    /* There is only one constructor. */
9038                     }
9039                 }
9040             }
9041         }
9042     }
9043
9044   /* If we've found error at that stage, don't try to generate
9045      anything, unless we're emitting xrefs or checking the syntax only
9046      (but not using -fsyntax-only for the purpose of generating
9047      bytecode. */
9048   if (java_error_count && !flag_emit_xref
9049       && (!flag_syntax_only && !flag_emit_class_files))
9050     return;
9051
9052   /* Now things are stable, go for generation of the class data. */
9053
9054   /* We pessimistically marked all fields external until we knew
9055      what set of classes we were planning to compile.  Now mark
9056      those that will be generated locally as not external.  */
9057   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9058     {
9059       tree current;
9060       ctxp = cur_ctxp;
9061       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9062         {
9063           tree class = TREE_TYPE (current);
9064           tree field;
9065           for (field = TYPE_FIELDS (class); field ; field = TREE_CHAIN (field))
9066             if (FIELD_STATIC (field))
9067               DECL_EXTERNAL (field) = 0;
9068         }
9069     }
9070
9071   /* Compile the classes.  */
9072   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9073     {
9074       tree current;
9075       reversed_class_list = NULL;
9076
9077       ctxp = cur_ctxp;
9078
9079       /* We write out the classes in reverse order.  This ensures that
9080          inner classes are written before their containing classes,
9081          which is important for parallel builds.  Otherwise, the
9082          class file for the outer class may be found, but the class
9083          file for the inner class may not be present.  In that
9084          situation, the compiler cannot fall back to the original
9085          source, having already read the outer class, so we must
9086          prevent that situation.  */
9087       for (current = ctxp->class_list;
9088            current;
9089            current = TREE_CHAIN (current))
9090         reversed_class_list
9091           = tree_cons (NULL_TREE, current, reversed_class_list);
9092
9093       for (current = reversed_class_list;
9094            current;
9095            current = TREE_CHAIN (current))
9096         {
9097           current_class = TREE_TYPE (TREE_VALUE (current));
9098           outgoing_cpool = TYPE_CPOOL (current_class);
9099           if (flag_emit_class_files)
9100             write_classfile (current_class);
9101           if (flag_emit_xref)
9102             expand_xref (current_class);
9103           else if (! flag_syntax_only)
9104             {
9105               java_expand_method_bodies (current_class);
9106               finish_class ();
9107             }
9108         }
9109     }
9110 }
9111
9112 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9113    a tree list node containing RIGHT. Fore coming RIGHTs will be
9114    chained to this hook. LOCATION contains the location of the
9115    separating `.' operator.  */
9116
9117 static tree
9118 make_qualified_primary (primary, right, location)
9119      tree primary, right;
9120      int location;
9121 {
9122   tree wfl;
9123
9124   if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9125     wfl = build_wfl_wrap (primary, location);
9126   else
9127     {
9128       wfl = primary;
9129       /* If wfl wasn't qualified, we build a first anchor */
9130       if (!EXPR_WFL_QUALIFICATION (wfl))
9131         EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9132     }
9133
9134   /* And chain them */
9135   EXPR_WFL_LINECOL (right) = location;
9136   chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9137   PRIMARY_P (wfl) =  1;
9138   return wfl;
9139 }
9140
9141 /* Simple merge of two name separated by a `.' */
9142
9143 static tree
9144 merge_qualified_name (left, right)
9145      tree left, right;
9146 {
9147   tree node;
9148   if (!left && !right)
9149     return NULL_TREE;
9150
9151   if (!left)
9152     return right;
9153
9154   if (!right)
9155     return left;
9156
9157   obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9158                 IDENTIFIER_LENGTH (left));
9159   obstack_1grow (&temporary_obstack, '.');
9160   obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9161                  IDENTIFIER_LENGTH (right));
9162   node =  get_identifier (obstack_base (&temporary_obstack));
9163   obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9164   QUALIFIED_P (node) = 1;
9165   return node;
9166 }
9167
9168 /* Merge the two parts of a qualified name into LEFT.  Set the
9169    location information of the resulting node to LOCATION, usually
9170    inherited from the location information of the `.' operator. */
9171
9172 static tree
9173 make_qualified_name (left, right, location)
9174      tree left, right;
9175      int location;
9176 {
9177 #ifdef USE_COMPONENT_REF
9178   tree node = build (COMPONENT_REF, NULL_TREE, left, right);
9179   EXPR_WFL_LINECOL (node) = location;
9180   return node;
9181 #else
9182   tree left_id = EXPR_WFL_NODE (left);
9183   tree right_id = EXPR_WFL_NODE (right);
9184   tree wfl, merge;
9185
9186   merge = merge_qualified_name (left_id, right_id);
9187
9188   /* Left wasn't qualified and is now qualified */
9189   if (!QUALIFIED_P (left_id))
9190     {
9191       tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9192       EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9193       EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9194     }
9195
9196   wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9197   EXPR_WFL_LINECOL (wfl) = location;
9198   chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9199
9200   EXPR_WFL_NODE (left) = merge;
9201   return left;
9202 #endif
9203 }
9204
9205 /* Extract the last identifier component of the qualified in WFL. The
9206    last identifier is removed from the linked list */
9207
9208 static tree
9209 cut_identifier_in_qualified (wfl)
9210      tree wfl;
9211 {
9212   tree q;
9213   tree previous = NULL_TREE;
9214   for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9215     if (!TREE_CHAIN (q))
9216       {
9217         if (!previous)
9218           /* Operating on a non qualified qualified WFL.  */
9219           abort ();
9220
9221         TREE_CHAIN (previous) = NULL_TREE;
9222         return TREE_PURPOSE (q);
9223       }
9224 }
9225
9226 /* Resolve the expression name NAME. Return its decl.  */
9227
9228 static tree
9229 resolve_expression_name (id, orig)
9230      tree id;
9231      tree *orig;
9232 {
9233   tree name = EXPR_WFL_NODE (id);
9234   tree decl;
9235
9236   /* 6.5.5.1: Simple expression names */
9237   if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9238     {
9239       /* 15.13.1: NAME can appear within the scope of a local variable
9240          declaration */
9241       if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9242         return decl;
9243
9244       /* 15.13.1: NAME can appear within a class declaration */
9245       else
9246         {
9247           decl = lookup_field_wrapper (current_class, name);
9248           if (decl)
9249             {
9250               tree access = NULL_TREE;
9251               int fs = FIELD_STATIC (decl);
9252
9253               /* If we're accessing an outer scope local alias, make
9254                  sure we change the name of the field we're going to
9255                  build access to. */
9256               if (FIELD_LOCAL_ALIAS_USED (decl))
9257                 name = DECL_NAME (decl);
9258
9259               /* Instance variable (8.3.1.1) can't appear within
9260                  static method, static initializer or initializer for
9261                  a static variable. */
9262               if (!fs && METHOD_STATIC (current_function_decl))
9263                 {
9264                   static_ref_err (id, name, current_class);
9265                   return error_mark_node;
9266                 }
9267               /* Instance variables can't appear as an argument of
9268                  an explicit constructor invocation */
9269               if (!fs && ctxp->explicit_constructor_p
9270                   && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9271                 {
9272                   parse_error_context
9273                     (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9274                   return error_mark_node;
9275                 }
9276
9277               /* If we're processing an inner class and we're trying
9278                  to access a field belonging to an outer class, build
9279                  the access to the field */
9280               if (!fs && outer_field_access_p (current_class, decl))
9281                 {
9282                   if (CLASS_STATIC (TYPE_NAME (current_class)))
9283                     {
9284                       static_ref_err (id, DECL_NAME (decl), current_class);
9285                       return error_mark_node;
9286                     }
9287                   access = build_outer_field_access (id, decl);
9288                   if (orig)
9289                     *orig = access;
9290                   return access;
9291                 }
9292
9293               /* Otherwise build what it takes to access the field */
9294               access = build_field_ref ((fs ? NULL_TREE : current_this),
9295                                         DECL_CONTEXT (decl), name);
9296               if (fs)
9297                 access = maybe_build_class_init_for_field (decl, access);
9298               /* We may be asked to save the real field access node */
9299               if (orig)
9300                 *orig = access;
9301               /* And we return what we got */
9302               return access;
9303             }
9304           /* Fall down to error report on undefined variable */
9305         }
9306     }
9307   /* 6.5.5.2 Qualified Expression Names */
9308   else
9309     {
9310       if (orig)
9311         *orig = NULL_TREE;
9312       qualify_ambiguous_name (id);
9313       /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9314       /* 15.10.2: Accessing Superclass Members using super */
9315       return resolve_field_access (id, orig, NULL);
9316     }
9317
9318   /* We've got an error here */
9319   if (INNER_CLASS_TYPE_P (current_class))
9320     parse_error_context (id,
9321                          "Local variable `%s' can't be accessed from within the inner class `%s' unless it is declared final",
9322                          IDENTIFIER_POINTER (name),
9323                          IDENTIFIER_POINTER (DECL_NAME
9324                                              (TYPE_NAME (current_class))));
9325   else
9326     parse_error_context (id, "Undefined variable `%s'",
9327                          IDENTIFIER_POINTER (name));
9328
9329   return error_mark_node;
9330 }
9331
9332 static void
9333 static_ref_err (wfl, field_id, class_type)
9334     tree wfl, field_id, class_type;
9335 {
9336   parse_error_context
9337     (wfl,
9338      "Can't make a static reference to nonstatic variable `%s' in class `%s'",
9339      IDENTIFIER_POINTER (field_id),
9340      IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9341 }
9342
9343 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9344    We return something suitable to generate the field access. We also
9345    return the field decl in FIELD_DECL and its type in FIELD_TYPE.  If
9346    recipient's address can be null. */
9347
9348 static tree
9349 resolve_field_access (qual_wfl, field_decl, field_type)
9350      tree qual_wfl;
9351      tree *field_decl, *field_type;
9352 {
9353   int is_static = 0;
9354   tree field_ref;
9355   tree decl, where_found, type_found;
9356
9357   if (resolve_qualified_expression_name (qual_wfl, &decl,
9358                                          &where_found, &type_found))
9359     return error_mark_node;
9360
9361   /* Resolve the LENGTH field of an array here */
9362   if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9363       && type_found && TYPE_ARRAY_P (type_found)
9364       && ! flag_emit_class_files && ! flag_emit_xref)
9365     {
9366       tree length = build_java_array_length_access (where_found);
9367       field_ref = length;
9368
9369       /* In case we're dealing with a static array, we need to
9370          initialize its class before the array length can be fetched.
9371          It's also a good time to create a DECL_RTL for the field if
9372          none already exists, otherwise if the field was declared in a
9373          class found in an external file and hasn't been (and won't
9374          be) accessed for its value, none will be created. */
9375       if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9376         {
9377           build_static_field_ref (where_found);
9378           field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9379         }
9380     }
9381   /* We might have been trying to resolve field.method(). In which
9382      case, the resolution is over and decl is the answer */
9383   else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9384     field_ref = decl;
9385   else if (JDECL_P (decl))
9386     {
9387       if (!type_found)
9388         type_found = DECL_CONTEXT (decl);
9389       is_static = FIELD_STATIC (decl);
9390       field_ref = build_field_ref ((is_static && !flag_emit_xref?
9391                                     NULL_TREE : where_found),
9392                                    type_found, DECL_NAME (decl));
9393       if (field_ref == error_mark_node)
9394         return error_mark_node;
9395       if (is_static)
9396         field_ref = maybe_build_class_init_for_field (decl, field_ref);
9397     }
9398   else
9399     field_ref = decl;
9400
9401   if (field_decl)
9402     *field_decl = decl;
9403   if (field_type)
9404     *field_type = (QUAL_DECL_TYPE (decl) ?
9405                    QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9406   return field_ref;
9407 }
9408
9409 /* If NODE is an access to f static field, strip out the class
9410    initialization part and return the field decl, otherwise, return
9411    NODE. */
9412
9413 static tree
9414 strip_out_static_field_access_decl (node)
9415     tree node;
9416 {
9417   if (TREE_CODE (node) == COMPOUND_EXPR)
9418     {
9419       tree op1 = TREE_OPERAND (node, 1);
9420       if (TREE_CODE (op1) == COMPOUND_EXPR)
9421          {
9422            tree call = TREE_OPERAND (op1, 0);
9423            if (TREE_CODE (call) == CALL_EXPR
9424                && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9425                && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9426                == soft_initclass_node)
9427              return TREE_OPERAND (op1, 1);
9428          }
9429       else if (JDECL_P (op1))
9430         return op1;
9431     }
9432   return node;
9433 }
9434
9435 /* 6.5.5.2: Qualified Expression Names */
9436
9437 static int
9438 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9439      tree wfl;
9440      tree *found_decl, *type_found, *where_found;
9441 {
9442   int from_type = 0;            /* Field search initiated from a type */
9443   int from_super = 0, from_cast = 0, from_qualified_this = 0;
9444   int previous_call_static = 0;
9445   int is_static;
9446   tree decl = NULL_TREE, type = NULL_TREE, q;
9447   /* For certain for of inner class instantiation */
9448   tree saved_current, saved_this;
9449 #define RESTORE_THIS_AND_CURRENT_CLASS                          \
9450   { current_class = saved_current; current_this = saved_this;}
9451
9452   *type_found = *where_found = NULL_TREE;
9453
9454   for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9455     {
9456       tree qual_wfl = QUAL_WFL (q);
9457       tree ret_decl;            /* for EH checking */
9458       int location;             /* for EH checking */
9459
9460       /* 15.10.1 Field Access Using a Primary */
9461       switch (TREE_CODE (qual_wfl))
9462         {
9463         case CALL_EXPR:
9464         case NEW_CLASS_EXPR:
9465           /* If the access to the function call is a non static field,
9466              build the code to access it. */
9467           if (JDECL_P (decl) && !FIELD_STATIC (decl))
9468             {
9469               decl = maybe_access_field (decl, *where_found,
9470                                          DECL_CONTEXT (decl));
9471               if (decl == error_mark_node)
9472                 return 1;
9473             }
9474
9475           /* And code for the function call */
9476           if (complete_function_arguments (qual_wfl))
9477             return 1;
9478
9479           /* We might have to setup a new current class and a new this
9480              for the search of an inner class, relative to the type of
9481              a expression resolved as `decl'. The current values are
9482              saved and restored shortly after */
9483           saved_current = current_class;
9484           saved_this = current_this;
9485           if (decl
9486               && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9487                   || from_qualified_this))
9488             {
9489               /* If we still have `from_qualified_this', we have the form
9490                  <T>.this.f() and we need to build <T>.this */
9491               if (from_qualified_this)
9492                 {
9493                   decl = build_access_to_thisn (current_class, type, 0);
9494                   decl = java_complete_tree (decl);
9495                   type = TREE_TYPE (TREE_TYPE (decl));
9496                 }
9497               current_class = type;
9498               current_this = decl;
9499               from_qualified_this = 0;
9500             }
9501
9502           if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9503             CALL_USING_SUPER (qual_wfl) = 1;
9504           location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9505                       EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9506           *where_found = patch_method_invocation (qual_wfl, decl, type,
9507                                                   from_super,
9508                                                   &is_static, &ret_decl);
9509           from_super = 0;
9510           if (*where_found == error_mark_node)
9511             {
9512               RESTORE_THIS_AND_CURRENT_CLASS;
9513               return 1;
9514             }
9515           *type_found = type = QUAL_DECL_TYPE (*where_found);
9516
9517           /* If we're creating an inner class instance, check for that
9518              an enclosing instance is in scope */
9519           if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9520               && INNER_ENCLOSING_SCOPE_CHECK (type))
9521             {
9522               parse_error_context
9523                 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9524                  lang_printable_name (type, 0),
9525                  (!current_this ? "" :
9526                   "; an explicit one must be provided when creating this inner class"));
9527               RESTORE_THIS_AND_CURRENT_CLASS;
9528               return 1;
9529             }
9530
9531           /* In case we had to change then to resolve a inner class
9532              instantiation using a primary qualified by a `new' */
9533           RESTORE_THIS_AND_CURRENT_CLASS;
9534
9535           /* EH check. No check on access$<n> functions */
9536           if (location
9537               && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9538                     (DECL_NAME (current_function_decl)))
9539             check_thrown_exceptions (location, ret_decl);
9540
9541           /* If the previous call was static and this one is too,
9542              build a compound expression to hold the two (because in
9543              that case, previous function calls aren't transported as
9544              forcoming function's argument. */
9545           if (previous_call_static && is_static)
9546             {
9547               decl = build (COMPOUND_EXPR, TREE_TYPE (*where_found),
9548                             decl, *where_found);
9549               TREE_SIDE_EFFECTS (decl) = 1;
9550             }
9551           else
9552             {
9553               previous_call_static = is_static;
9554               decl = *where_found;
9555             }
9556           from_type = 0;
9557           continue;
9558
9559         case NEW_ARRAY_EXPR:
9560         case NEW_ANONYMOUS_ARRAY_EXPR:
9561           *where_found = decl = java_complete_tree (qual_wfl);
9562           if (decl == error_mark_node)
9563             return 1;
9564           *type_found = type = QUAL_DECL_TYPE (decl);
9565           continue;
9566
9567         case CONVERT_EXPR:
9568           *where_found = decl = java_complete_tree (qual_wfl);
9569           if (decl == error_mark_node)
9570             return 1;
9571           *type_found = type = QUAL_DECL_TYPE (decl);
9572           from_cast = 1;
9573           continue;
9574
9575         case CONDITIONAL_EXPR:
9576         case STRING_CST:
9577         case MODIFY_EXPR:
9578           *where_found = decl = java_complete_tree (qual_wfl);
9579           if (decl == error_mark_node)
9580             return 1;
9581           *type_found = type = QUAL_DECL_TYPE (decl);
9582           continue;
9583
9584         case ARRAY_REF:
9585           /* If the access to the function call is a non static field,
9586              build the code to access it. */
9587           if (JDECL_P (decl) && !FIELD_STATIC (decl))
9588             {
9589               decl = maybe_access_field (decl, *where_found, type);
9590               if (decl == error_mark_node)
9591                 return 1;
9592             }
9593           /* And code for the array reference expression */
9594           decl = java_complete_tree (qual_wfl);
9595           if (decl == error_mark_node)
9596             return 1;
9597           type = QUAL_DECL_TYPE (decl);
9598           continue;
9599
9600         case PLUS_EXPR:
9601           if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9602             return 1;
9603           if ((type = patch_string (decl)))
9604             decl = type;
9605           *where_found = QUAL_RESOLUTION (q) = decl;
9606           *type_found = type = TREE_TYPE (decl);
9607           break;
9608
9609         case CLASS_LITERAL:
9610           if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9611             return 1;
9612           *where_found = QUAL_RESOLUTION (q) = decl;
9613           *type_found = type = TREE_TYPE (decl);
9614           break;
9615
9616         default:
9617           /* Fix for -Wall Just go to the next statement. Don't
9618              continue */
9619           break;
9620         }
9621
9622       /* If we fall here, we weren't processing a (static) function call. */
9623       previous_call_static = 0;
9624
9625       /* It can be the keyword THIS */
9626       if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9627           && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9628         {
9629           if (!current_this)
9630             {
9631               parse_error_context
9632                 (wfl, "Keyword `this' used outside allowed context");
9633               return 1;
9634             }
9635           if (ctxp->explicit_constructor_p
9636               && type == current_class)
9637             {
9638               parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9639               return 1;
9640             }
9641           /* We have to generate code for intermediate access */
9642           if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9643             {
9644               *where_found = decl = current_this;
9645               *type_found = type = QUAL_DECL_TYPE (decl);
9646             }
9647           /* We're trying to access the this from somewhere else. Make sure
9648              it's allowed before doing so. */
9649           else
9650             {
9651               if (!enclosing_context_p (type, current_class))
9652                 {
9653                   char *p  = xstrdup (lang_printable_name (type, 0));
9654                   parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9655                                        p, p,
9656                                        lang_printable_name (current_class, 0));
9657                   free (p);
9658                   return 1;
9659                 }
9660               from_qualified_this = 1;
9661               /* If there's nothing else after that, we need to
9662                  produce something now, otherwise, the section of the
9663                  code that needs to produce <T>.this will generate
9664                  what is necessary. */
9665               if (!TREE_CHAIN (q))
9666                 {
9667                   decl = build_access_to_thisn (current_class, type, 0);
9668                   *where_found = decl = java_complete_tree (decl);
9669                   *type_found = type = TREE_TYPE (decl);
9670                 }
9671             }
9672
9673           from_type = 0;
9674           continue;
9675         }
9676
9677       /* 15.10.2 Accessing Superclass Members using SUPER */
9678       if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9679           && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9680         {
9681           tree node;
9682           /* Check on the restricted use of SUPER */
9683           if (METHOD_STATIC (current_function_decl)
9684               || current_class == object_type_node)
9685             {
9686               parse_error_context
9687                 (wfl, "Keyword `super' used outside allowed context");
9688               return 1;
9689             }
9690           /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9691           node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9692                              CLASSTYPE_SUPER (current_class),
9693                              build_this (EXPR_WFL_LINECOL (qual_wfl)));
9694           *where_found = decl = java_complete_tree (node);
9695           if (decl == error_mark_node)
9696             return 1;
9697           *type_found = type = QUAL_DECL_TYPE (decl);
9698           from_super = from_type = 1;
9699           continue;
9700         }
9701
9702       /* 15.13.1: Can't search for field name in packages, so we
9703          assume a variable/class name was meant. */
9704       if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9705         {
9706           tree name;
9707           if ((decl = resolve_package (wfl, &q, &name)))
9708             {
9709               tree list;
9710               *where_found = decl;
9711
9712               /* We want to be absolutely sure that the class is laid
9713                  out. We're going to search something inside it. */
9714               *type_found = type = TREE_TYPE (decl);
9715               layout_class (type);
9716               from_type = 1;
9717
9718               /* Fix them all the way down, if any are left. */
9719               if (q)
9720                 {
9721                   list = TREE_CHAIN (q);
9722                   while (list)
9723                     {
9724                       RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9725                       RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9726                       list = TREE_CHAIN (list);
9727                     }
9728                 }
9729             }
9730           else
9731             {
9732               if (from_super || from_cast)
9733                 parse_error_context
9734                   ((from_cast ? qual_wfl : wfl),
9735                    "No variable `%s' defined in class `%s'",
9736                    IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9737                    lang_printable_name (type, 0));
9738               else
9739                 parse_error_context
9740                   (qual_wfl, "Undefined variable or class name: `%s'",
9741                    IDENTIFIER_POINTER (name));
9742               return 1;
9743             }
9744         }
9745
9746       /* We have a type name. It's been already resolved when the
9747          expression was qualified. */
9748       else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
9749         {
9750           decl = QUAL_RESOLUTION (q);
9751
9752           /* Sneak preview. If next we see a `new', we're facing a
9753              qualification with resulted in a type being selected
9754              instead of a field.  Report the error */
9755           if(TREE_CHAIN (q)
9756              && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9757             {
9758               parse_error_context (qual_wfl, "Undefined variable `%s'",
9759                                    IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9760               return 1;
9761             }
9762
9763           if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
9764             {
9765               parse_error_context
9766                 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9767                  java_accstring_lookup (get_access_flags_from_decl (decl)),
9768                  GET_TYPE_NAME (type),
9769                  IDENTIFIER_POINTER (DECL_NAME (decl)),
9770                  IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9771               return 1;
9772             }
9773           check_deprecation (qual_wfl, decl);
9774
9775           type = TREE_TYPE (decl);
9776           from_type = 1;
9777         }
9778       /* We resolve an expression name */
9779       else
9780         {
9781           tree field_decl = NULL_TREE;
9782
9783           /* If there exists an early resolution, use it. That occurs
9784              only once and we know that there are more things to
9785              come. Don't do that when processing something after SUPER
9786              (we need more thing to be put in place below */
9787           if (!from_super && QUAL_RESOLUTION (q))
9788             {
9789               decl = QUAL_RESOLUTION (q);
9790               if (!type)
9791                 {
9792                   if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9793                     {
9794                       if (current_this)
9795                         *where_found = current_this;
9796                       else
9797                         {
9798                           static_ref_err (qual_wfl, DECL_NAME (decl),
9799                                           current_class);
9800                           return 1;
9801                         }
9802                       if (outer_field_access_p (current_class, decl))
9803                         decl = build_outer_field_access (qual_wfl, decl);
9804                     }
9805                   else
9806                     {
9807                       *where_found = TREE_TYPE (decl);
9808                       if (TREE_CODE (*where_found) == POINTER_TYPE)
9809                         *where_found = TREE_TYPE (*where_found);
9810                     }
9811                 }
9812             }
9813
9814           /* Report and error if we're using a numerical litteral as a
9815              qualifier. It can only be an INTEGER_CST. */
9816           else if (TREE_CODE (qual_wfl) == INTEGER_CST)
9817             {
9818               parse_error_context
9819                 (wfl, "Can't use type `%s' as a qualifier",
9820                  lang_printable_name (TREE_TYPE (qual_wfl), 0));
9821               return 1;
9822             }
9823
9824           /* We have to search for a field, knowing the type of its
9825              container. The flag FROM_TYPE indicates that we resolved
9826              the last member of the expression as a type name, which
9827              means that for the resolution of this field, we'll look
9828              for other errors than if it was resolved as a member of
9829              an other field. */
9830           else
9831             {
9832               int is_static;
9833               tree field_decl_type; /* For layout */
9834
9835               if (!from_type && !JREFERENCE_TYPE_P (type))
9836                 {
9837                   parse_error_context
9838                     (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9839                      IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9840                      lang_printable_name (type, 0),
9841                      IDENTIFIER_POINTER (DECL_NAME (decl)));
9842                   return 1;
9843                 }
9844
9845               field_decl = lookup_field_wrapper (type,
9846                                                  EXPR_WFL_NODE (qual_wfl));
9847
9848               /* Maybe what we're trying to access to is an inner
9849                  class, only if decl is a TYPE_DECL. */
9850               if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
9851                 {
9852                   tree ptr, inner_decl;
9853
9854                   BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9855                   inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9856                   if (inner_decl)
9857                     {
9858                       check_inner_class_access (inner_decl, decl, qual_wfl);
9859                       type = TREE_TYPE (inner_decl);
9860                       decl = inner_decl;
9861                       from_type = 1;
9862                       continue;
9863                     }
9864                 }
9865
9866               if (field_decl == NULL_TREE)
9867                 {
9868                   parse_error_context
9869                     (qual_wfl, "No variable `%s' defined in type `%s'",
9870                      IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9871                      GET_TYPE_NAME (type));
9872                   return 1;
9873                 }
9874               if (field_decl == error_mark_node)
9875                 return 1;
9876
9877               /* Layout the type of field_decl, since we may need
9878                  it. Don't do primitive types or loaded classes. The
9879                  situation of non primitive arrays may not handled
9880                  properly here. FIXME */
9881               if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9882                 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9883               else
9884                 field_decl_type = TREE_TYPE (field_decl);
9885               if (!JPRIMITIVE_TYPE_P (field_decl_type)
9886                   && !CLASS_LOADED_P (field_decl_type)
9887                   && !TYPE_ARRAY_P (field_decl_type))
9888                 resolve_and_layout (field_decl_type, NULL_TREE);
9889
9890               /* Check on accessibility here */
9891               if (not_accessible_p (current_class, field_decl,
9892                                     DECL_CONTEXT (field_decl), from_super))
9893                 {
9894                   parse_error_context
9895                     (qual_wfl,
9896                      "Can't access %s field `%s.%s' from `%s'",
9897                      java_accstring_lookup
9898                        (get_access_flags_from_decl (field_decl)),
9899                      GET_TYPE_NAME (type),
9900                      IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9901                      IDENTIFIER_POINTER
9902                        (DECL_NAME (TYPE_NAME (current_class))));
9903                   return 1;
9904                 }
9905               check_deprecation (qual_wfl, field_decl);
9906
9907               /* There are things to check when fields are accessed
9908                  from type. There are no restrictions on a static
9909                  declaration of the field when it is accessed from an
9910                  interface */
9911               is_static = FIELD_STATIC (field_decl);
9912               if (!from_super && from_type
9913                   && !TYPE_INTERFACE_P (type)
9914                   && !is_static
9915                   && (current_function_decl
9916                       && METHOD_STATIC (current_function_decl)))
9917                 {
9918                   static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9919                   return 1;
9920                 }
9921               from_cast = from_super = 0;
9922
9923               /* It's an access from a type but it isn't static, we
9924                  make it relative to `this'. */
9925               if (!is_static && from_type)
9926                 decl = current_this;
9927
9928               /* If we need to generate something to get a proper
9929                  handle on what this field is accessed from, do it
9930                  now. */
9931               if (!is_static)
9932                 {
9933                   decl = maybe_access_field (decl, *where_found, *type_found);
9934                   if (decl == error_mark_node)
9935                     return 1;
9936                 }
9937
9938               /* We want to keep the location were found it, and the type
9939                  we found. */
9940               *where_found = decl;
9941               *type_found = type;
9942
9943               /* Generate the correct expression for field access from
9944                  qualified this */
9945               if (from_qualified_this)
9946                 {
9947                   field_decl = build_outer_field_access (qual_wfl, field_decl);
9948                   from_qualified_this = 0;
9949                 }
9950
9951               /* This is the decl found and eventually the next one to
9952                  search from */
9953               decl = field_decl;
9954             }
9955           from_type = 0;
9956           type = QUAL_DECL_TYPE (decl);
9957
9958           /* Sneak preview. If decl is qualified by a `new', report
9959              the error here to be accurate on the peculiar construct */
9960           if (TREE_CHAIN (q)
9961               && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9962               && !JREFERENCE_TYPE_P (type))
9963             {
9964               parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9965                                    lang_printable_name (type, 0));
9966               return 1;
9967             }
9968         }
9969       /* `q' might have changed due to a after package resolution
9970          re-qualification */
9971       if (!q)
9972         break;
9973     }
9974   *found_decl = decl;
9975   return 0;
9976 }
9977
9978 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9979    can't be accessed from REFERENCE (a record type). If MEMBER
9980    features a protected access, we then use WHERE which, if non null,
9981    holds the type of MEMBER's access that is checked against
9982    6.6.2.1. This function should be used when decl is a field or a
9983    method.  */
9984
9985 static int
9986 not_accessible_p (reference, member, where, from_super)
9987      tree reference, member;
9988      tree where;
9989      int from_super;
9990 {
9991   int access_flag = get_access_flags_from_decl (member);
9992
9993   /* Inner classes are processed by check_inner_class_access */
9994   if (INNER_CLASS_TYPE_P (reference))
9995     return 0;
9996
9997   /* Access always granted for members declared public */
9998   if (access_flag & ACC_PUBLIC)
9999     return 0;
10000
10001   /* Check access on protected members */
10002   if (access_flag & ACC_PROTECTED)
10003     {
10004       /* Access granted if it occurs from within the package
10005          containing the class in which the protected member is
10006          declared */
10007       if (class_in_current_package (DECL_CONTEXT (member)))
10008         return 0;
10009
10010       /* If accessed with the form `super.member', then access is granted */
10011       if (from_super)
10012         return 0;
10013
10014       /* If where is active, access was made through a
10015          qualifier. Access is granted if the type of the qualifier is
10016          or is a sublass of the type the access made from (6.6.2.1.)  */
10017       if (where && !inherits_from_p (reference, where))
10018         return 1;
10019
10020       /* Otherwise, access is granted if occurring from the class where
10021          member is declared or a subclass of it. Find the right
10022          context to perform the check */
10023       if (PURE_INNER_CLASS_TYPE_P (reference))
10024         {
10025           while (INNER_CLASS_TYPE_P (reference))
10026             {
10027               if (inherits_from_p (reference, DECL_CONTEXT (member)))
10028                 return 0;
10029               reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10030             }
10031         }
10032       if (inherits_from_p (reference, DECL_CONTEXT (member)))
10033         return 0;
10034       return 1;
10035     }
10036
10037   /* Check access on private members. Access is granted only if it
10038      occurs from within the class in which it is declared -- that does
10039      it for innerclasses too. */
10040   if (access_flag & ACC_PRIVATE)
10041     {
10042       if (reference == DECL_CONTEXT (member))
10043         return 0;
10044       if (enclosing_context_p (reference, DECL_CONTEXT (member)))
10045         return 0;
10046       return 1;
10047     }
10048
10049   /* Default access are permitted only when occurring within the
10050      package in which the type (REFERENCE) is declared. In other words,
10051      REFERENCE is defined in the current package */
10052   if (ctxp->package)
10053     return !class_in_current_package (reference);
10054
10055   /* Otherwise, access is granted */
10056   return 0;
10057 }
10058
10059 /* Test deprecated decl access.  */
10060 static void
10061 check_deprecation (wfl, decl)
10062      tree wfl, decl;
10063 {
10064   const char *file = DECL_SOURCE_FILE (decl);
10065   /* Complain if the field is deprecated and the file it was defined
10066      in isn't compiled at the same time the file which contains its
10067      use is */
10068   if (DECL_DEPRECATED (decl)
10069       && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
10070     {
10071       char the [20];
10072       switch (TREE_CODE (decl))
10073         {
10074         case FUNCTION_DECL:
10075           strcpy (the, "method");
10076           break;
10077         case FIELD_DECL:
10078         case VAR_DECL:
10079           strcpy (the, "field");
10080           break;
10081         case TYPE_DECL:
10082           parse_warning_context (wfl, "The class `%s' has been deprecated",
10083                                  IDENTIFIER_POINTER (DECL_NAME (decl)));
10084           return;
10085         default:
10086           abort ();
10087         }
10088       /* Don't issue a message if the context as been deprecated as a
10089          whole. */
10090       if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
10091         parse_warning_context
10092           (wfl, "The %s `%s' in class `%s' has been deprecated",
10093            the, lang_printable_name (decl, 0),
10094            IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
10095     }
10096 }
10097
10098 /* Returns 1 if class was declared in the current package, 0 otherwise */
10099
10100 static GTY(()) tree cicp_cache;
10101 static int
10102 class_in_current_package (class)
10103      tree class;
10104 {
10105   int qualified_flag;
10106   tree left;
10107
10108   if (cicp_cache == class)
10109     return 1;
10110
10111   qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
10112
10113   /* If the current package is empty and the name of CLASS is
10114      qualified, class isn't in the current package.  If there is a
10115      current package and the name of the CLASS is not qualified, class
10116      isn't in the current package */
10117   if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
10118     return 0;
10119
10120   /* If there is not package and the name of CLASS isn't qualified,
10121      they belong to the same unnamed package */
10122   if (!ctxp->package && !qualified_flag)
10123     return 1;
10124
10125   /* Compare the left part of the name of CLASS with the package name */
10126   breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
10127   if (ctxp->package == left)
10128     {
10129       cicp_cache = class;
10130       return 1;
10131     }
10132   return 0;
10133 }
10134
10135 /* This function may generate code to access DECL from WHERE. This is
10136    done only if certain conditions meet.  */
10137
10138 static tree
10139 maybe_access_field (decl, where, type)
10140   tree decl, where, type;
10141 {
10142   if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10143       && !FIELD_STATIC (decl))
10144     decl = build_field_ref (where ? where : current_this,
10145                             (type ? type : DECL_CONTEXT (decl)),
10146                             DECL_NAME (decl));
10147   return decl;
10148 }
10149
10150 /* Build a method invocation, by patching PATCH. If non NULL
10151    and according to the situation, PRIMARY and WHERE may be
10152    used. IS_STATIC is set to 1 if the invoked function is static. */
10153
10154 static tree
10155 patch_method_invocation (patch, primary, where, from_super,
10156                         is_static, ret_decl)
10157      tree patch, primary, where;
10158      int from_super;
10159      int *is_static;
10160      tree *ret_decl;
10161 {
10162   tree wfl = TREE_OPERAND (patch, 0);
10163   tree args = TREE_OPERAND (patch, 1);
10164   tree name = EXPR_WFL_NODE (wfl);
10165   tree list;
10166   int is_static_flag = 0;
10167   int is_super_init = 0;
10168   tree this_arg = NULL_TREE;
10169   int is_array_clone_call = 0;
10170
10171   /* Should be overriden if everything goes well. Otherwise, if
10172      something fails, it should keep this value. It stop the
10173      evaluation of a bogus assignment. See java_complete_tree,
10174      MODIFY_EXPR: for the reasons why we sometimes want to keep on
10175      evaluating an assignment */
10176   TREE_TYPE (patch) = error_mark_node;
10177
10178   /* Since lookup functions are messing with line numbers, save the
10179      context now.  */
10180   java_parser_context_save_global ();
10181
10182   /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10183
10184   /* Resolution of qualified name, excluding constructors */
10185   if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10186     {
10187       tree identifier, identifier_wfl, type, resolved;
10188       /* Extract the last IDENTIFIER of the qualified
10189          expression. This is a wfl and we will use it's location
10190          data during error report. */
10191       identifier_wfl = cut_identifier_in_qualified (wfl);
10192       identifier = EXPR_WFL_NODE (identifier_wfl);
10193
10194       /* Given the context, IDENTIFIER is syntactically qualified
10195          as a MethodName. We need to qualify what's before */
10196       qualify_ambiguous_name (wfl);
10197       resolved = resolve_field_access (wfl, NULL, NULL);
10198
10199       if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10200          && FIELD_FINAL (resolved)
10201          && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10202          && !flag_emit_class_files && !flag_emit_xref)
10203        resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10204
10205       if (resolved == error_mark_node)
10206         PATCH_METHOD_RETURN_ERROR ();
10207
10208       type = GET_SKIP_TYPE (resolved);
10209       resolve_and_layout (type, NULL_TREE);
10210
10211       if (JPRIMITIVE_TYPE_P (type))
10212         {
10213           parse_error_context
10214             (identifier_wfl,
10215              "Can't invoke a method on primitive type `%s'",
10216              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10217           PATCH_METHOD_RETURN_ERROR ();
10218         }
10219
10220       list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10221       args = nreverse (args);
10222
10223       /* We're resolving a call from a type */
10224       if (TREE_CODE (resolved) == TYPE_DECL)
10225         {
10226           if (CLASS_INTERFACE (resolved))
10227             {
10228               parse_error_context
10229                 (identifier_wfl,
10230                 "Can't make static reference to method `%s' in interface `%s'",
10231                  IDENTIFIER_POINTER (identifier),
10232                  IDENTIFIER_POINTER (name));
10233               PATCH_METHOD_RETURN_ERROR ();
10234             }
10235           if (list && !METHOD_STATIC (list))
10236             {
10237               char *fct_name = xstrdup (lang_printable_name (list, 0));
10238               parse_error_context
10239                 (identifier_wfl,
10240                  "Can't make static reference to method `%s %s' in class `%s'",
10241                  lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10242                  fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10243               free (fct_name);
10244               PATCH_METHOD_RETURN_ERROR ();
10245             }
10246         }
10247       else
10248         this_arg = primary = resolved;
10249
10250       if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10251         is_array_clone_call = 1;
10252
10253       /* IDENTIFIER_WFL will be used to report any problem further */
10254       wfl = identifier_wfl;
10255     }
10256   /* Resolution of simple names, names generated after a primary: or
10257      constructors */
10258   else
10259     {
10260       tree class_to_search = NULL_TREE;
10261       int lc;                   /* Looking for Constructor */
10262
10263       /* We search constructor in their target class */
10264       if (CALL_CONSTRUCTOR_P (patch))
10265         {
10266           if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10267             class_to_search = EXPR_WFL_NODE (wfl);
10268           else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10269                    this_identifier_node)
10270             class_to_search = NULL_TREE;
10271           else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10272                    super_identifier_node)
10273             {
10274               is_super_init = 1;
10275               if (CLASSTYPE_SUPER (current_class))
10276                 class_to_search =
10277                   DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10278               else
10279                 {
10280                   parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10281                   PATCH_METHOD_RETURN_ERROR ();
10282                 }
10283             }
10284
10285           /* Class to search is NULL if we're searching the current one */
10286           if (class_to_search)
10287             {
10288               class_to_search = resolve_and_layout (class_to_search, wfl);
10289
10290               if (!class_to_search)
10291                 {
10292                   parse_error_context
10293                     (wfl, "Class `%s' not found in type declaration",
10294                      IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10295                   PATCH_METHOD_RETURN_ERROR ();
10296                 }
10297
10298               /* Can't instantiate an abstract class, but we can
10299                  invoke it's constructor. It's use within the `new'
10300                  context is denied here. */
10301               if (CLASS_ABSTRACT (class_to_search)
10302                   && TREE_CODE (patch) == NEW_CLASS_EXPR)
10303                 {
10304                   parse_error_context
10305                     (wfl, "Class `%s' is an abstract class. It can't be instantiated",
10306                      IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10307                   PATCH_METHOD_RETURN_ERROR ();
10308                 }
10309
10310               class_to_search = TREE_TYPE (class_to_search);
10311             }
10312           else
10313             class_to_search = current_class;
10314           lc = 1;
10315         }
10316       /* This is a regular search in the local class, unless an
10317          alternate class is specified. */
10318       else
10319         {
10320           if (where != NULL_TREE)
10321             class_to_search = where;
10322           else if (QUALIFIED_P (name))
10323             class_to_search = current_class;
10324           else
10325             {
10326               class_to_search = current_class;
10327
10328               for (;;)
10329                 {
10330                   if (has_method (class_to_search, name))
10331                     break;
10332                   if (! INNER_CLASS_TYPE_P (class_to_search))
10333                     {
10334                       parse_error_context (wfl,
10335                                            "No method named `%s' in scope",
10336                                            IDENTIFIER_POINTER (name));
10337                       PATCH_METHOD_RETURN_ERROR ();
10338                     }
10339                   class_to_search
10340                     = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10341                 }
10342             }
10343           lc = 0;
10344         }
10345
10346       /* NAME is a simple identifier or comes from a primary. Search
10347          in the class whose declaration contain the method being
10348          invoked. */
10349       resolve_and_layout (class_to_search, NULL_TREE);
10350
10351       list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10352       /* Don't continue if no method were found, as the next statement
10353          can't be executed then. */
10354       if (!list)
10355         PATCH_METHOD_RETURN_ERROR ();
10356
10357       if (TYPE_ARRAY_P (class_to_search)
10358           && DECL_NAME (list) == get_identifier ("clone"))
10359         is_array_clone_call = 1;
10360
10361       /* Check for static reference if non static methods */
10362       if (check_for_static_method_reference (wfl, patch, list,
10363                                              class_to_search, primary))
10364         PATCH_METHOD_RETURN_ERROR ();
10365
10366       /* Check for inner classes creation from illegal contexts */
10367       if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10368                  && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10369           && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10370           && !DECL_INIT_P (current_function_decl))
10371         {
10372           parse_error_context
10373             (wfl, "No enclosing instance for inner class `%s' is in scope%s",
10374              lang_printable_name (class_to_search, 0),
10375              (!current_this ? "" :
10376               "; an explicit one must be provided when creating this inner class"));
10377           PATCH_METHOD_RETURN_ERROR ();
10378         }
10379
10380       /* Non static methods are called with the current object extra
10381          argument. If patch a `new TYPE()', the argument is the value
10382          returned by the object allocator. If method is resolved as a
10383          primary, use the primary otherwise use the current THIS. */
10384       args = nreverse (args);
10385       if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10386         {
10387           this_arg = primary ? primary : current_this;
10388
10389           /* If we're using an access method, things are different.
10390              There are two familly of cases:
10391
10392              1) We're not generating bytecodes:
10393
10394              - LIST is non static. It's invocation is transformed from
10395                x(a1,...,an) into this$<n>.x(a1,....an).
10396              - LIST is static. It's invocation is transformed from
10397                x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10398
10399              2) We're generating bytecodes:
10400
10401              - LIST is non static. It's invocation is transformed from
10402                x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10403              - LIST is static. It's invocation is transformed from
10404                x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10405
10406              Of course, this$<n> can be abitrary complex, ranging from
10407              this$0 (the immediate outer context) to
10408              access$0(access$0(...(this$0))).
10409
10410              maybe_use_access_method returns a non zero value if the
10411              this_arg has to be moved into the (then generated) stub
10412              argument list. In the meantime, the selected function
10413              might have be replaced by a generated stub. */
10414           if (!primary &&
10415               maybe_use_access_method (is_super_init, &list, &this_arg))
10416             {
10417               args = tree_cons (NULL_TREE, this_arg, args);
10418               this_arg = NULL_TREE; /* So it doesn't get chained twice */
10419             }
10420         }
10421     }
10422
10423   /* Merge point of all resolution schemes. If we have nothing, this
10424      is an error, already signaled */
10425   if (!list)
10426     PATCH_METHOD_RETURN_ERROR ();
10427
10428   /* Check accessibility, position the is_static flag, build and
10429      return the call */
10430   if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10431                         (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10432                          NULL_TREE), from_super)
10433       /* Calls to clone() on array types are permitted as a special-case. */
10434       && !is_array_clone_call)
10435     {
10436       const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10437       const char *const access =
10438         java_accstring_lookup (get_access_flags_from_decl (list));
10439       const char *const klass =
10440         IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10441       const char *const refklass =
10442         IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10443       const char *const what = (DECL_CONSTRUCTOR_P (list)
10444                                 ? "constructor" : "method");
10445       /* FIXME: WFL yields the wrong message here but I don't know
10446          what else to use.  */
10447       parse_error_context (wfl,
10448                            "Can't access %s %s `%s.%s' from `%s'",
10449                            access, what, klass, fct_name, refklass);
10450       PATCH_METHOD_RETURN_ERROR ();
10451     }
10452
10453   /* Deprecation check: check whether the method being invoked or the
10454      instance-being-created's type are deprecated. */
10455   if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10456     check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10457   else
10458     check_deprecation (wfl, list);
10459
10460   /* If invoking a innerclass constructor, there are hidden parameters
10461      to pass */
10462   if (TREE_CODE (patch) == NEW_CLASS_EXPR
10463       && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10464     {
10465       /* And make sure we add the accessed local variables to be saved
10466          in field aliases. */
10467       args = build_alias_initializer_parameter_list
10468         (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10469
10470       /* Secretly pass the current_this/primary as a second argument */
10471       if (primary || current_this)
10472         {
10473           tree extra_arg;
10474           tree this_type = (current_this ?
10475                             TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10476           /* Method's (list) enclosing context */
10477           tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10478           /* If we have a primary, use it. */
10479           if (primary)
10480             extra_arg = primary;
10481           /* The current `this' is an inner class but isn't a direct
10482              enclosing context for the inner class we're trying to
10483              create. Build an access to the proper enclosing context
10484              and use it. */
10485           else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10486                    && this_type != TREE_TYPE (mec))
10487             {
10488
10489               extra_arg = build_access_to_thisn (current_class,
10490                                                  TREE_TYPE (mec), 0);
10491               extra_arg = java_complete_tree (extra_arg);
10492             }
10493           /* Otherwise, just use the current `this' as an enclosing
10494              context. */
10495           else
10496             extra_arg = current_this;
10497           args = tree_cons (NULL_TREE, extra_arg, args);
10498         }
10499       else
10500         args = tree_cons (NULL_TREE, integer_zero_node, args);
10501     }
10502
10503   /* This handles the situation where a constructor invocation needs
10504      to have an enclosing context passed as a second parameter (the
10505      constructor is one of an inner class). */
10506   if ((is_super_init ||
10507        (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10508       && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10509     {
10510       tree dest = TYPE_NAME (DECL_CONTEXT (list));
10511       tree extra_arg =
10512         build_access_to_thisn (current_class, DECL_CONTEXT (dest), 0);
10513       extra_arg = java_complete_tree (extra_arg);
10514       args = tree_cons (NULL_TREE, extra_arg, args);
10515     }
10516
10517   is_static_flag = METHOD_STATIC (list);
10518   if (! is_static_flag && this_arg != NULL_TREE)
10519     args = tree_cons (NULL_TREE, this_arg, args);
10520
10521   /* In the context of an explicit constructor invocation, we can't
10522      invoke any method relying on `this'. Exceptions are: we're
10523      invoking a static function, primary exists and is not the current
10524      this, we're creating a new object. */
10525   if (ctxp->explicit_constructor_p
10526       && !is_static_flag
10527       && (!primary || primary == current_this)
10528       && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10529     {
10530       parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
10531       PATCH_METHOD_RETURN_ERROR ();
10532     }
10533   java_parser_context_restore_global ();
10534   if (is_static)
10535     *is_static = is_static_flag;
10536   /* Sometimes, we want the decl of the selected method. Such as for
10537      EH checking */
10538   if (ret_decl)
10539     *ret_decl = list;
10540   patch = patch_invoke (patch, list, args);
10541
10542   /* Now is a good time to insert the call to finit$ */
10543   if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10544     {
10545       tree finit_parms, finit_call;
10546
10547       /* Prepare to pass hidden parameters to finit$, if any. */
10548       finit_parms = build_alias_initializer_parameter_list
10549         (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10550
10551       finit_call =
10552         build_method_invocation (build_wfl_node (finit_identifier_node),
10553                                  finit_parms);
10554
10555       /* Generate the code used to initialize fields declared with an
10556          initialization statement and build a compound statement along
10557          with the super constructor invocation. */
10558       CAN_COMPLETE_NORMALLY (patch) = 1;
10559       patch = build (COMPOUND_EXPR, void_type_node, patch,
10560                      java_complete_tree (finit_call));
10561     }
10562   return patch;
10563 }
10564
10565 /* Check that we're not trying to do a static reference to a method in
10566    non static method. Return 1 if it's the case, 0 otherwise. */
10567
10568 static int
10569 check_for_static_method_reference (wfl, node, method, where, primary)
10570      tree wfl, node, method, where, primary;
10571 {
10572   if (METHOD_STATIC (current_function_decl)
10573       && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10574     {
10575       char *fct_name = xstrdup (lang_printable_name (method, 0));
10576       parse_error_context
10577         (wfl, "Can't make static reference to method `%s %s' in class `%s'",
10578          lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10579          IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10580       free (fct_name);
10581       return 1;
10582     }
10583   return 0;
10584 }
10585
10586 /* Fix the invocation of *MDECL if necessary in the case of a
10587    invocation from an inner class. *THIS_ARG might be modified
10588    appropriately and an alternative access to *MDECL might be
10589    returned.  */
10590
10591 static int
10592 maybe_use_access_method (is_super_init, mdecl, this_arg)
10593      int is_super_init;
10594      tree *mdecl, *this_arg;
10595 {
10596   tree ctx;
10597   tree md = *mdecl, ta = *this_arg;
10598   int to_return = 0;
10599   int non_static_context = !METHOD_STATIC (md);
10600
10601   if (is_super_init
10602       || DECL_CONTEXT (md) == current_class
10603       || !PURE_INNER_CLASS_TYPE_P (current_class)
10604       || DECL_FINIT_P (md)
10605       || DECL_INSTINIT_P (md))
10606     return 0;
10607
10608   /* If we're calling a method found in an enclosing class, generate
10609      what it takes to retrieve the right this. Don't do that if we're
10610      invoking a static method. Note that if MD's type is unrelated to
10611      CURRENT_CLASS, then the current this can be used. */
10612
10613   if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10614     {
10615       ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10616       if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10617         {
10618           ta = build_current_thisn (current_class);
10619           ta = build_wfl_node (ta);
10620         }
10621       else
10622         {
10623           tree type = ctx;
10624           while (type)
10625             {
10626               maybe_build_thisn_access_method (type);
10627               if (inherits_from_p (type, DECL_CONTEXT (md)))
10628                 {
10629                   ta = build_access_to_thisn (ctx, type, 0);
10630                   break;
10631                 }
10632               type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10633                       TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10634             }
10635         }
10636       ta = java_complete_tree (ta);
10637     }
10638
10639   /* We might have to use an access method to get to MD. We can
10640      break the method access rule as far as we're not generating
10641      bytecode */
10642   if (METHOD_PRIVATE (md) && flag_emit_class_files)
10643     {
10644       md = build_outer_method_access_method (md);
10645       to_return = 1;
10646     }
10647
10648   *mdecl = md;
10649   *this_arg = ta;
10650
10651   /* Returnin a non zero value indicates we were doing a non static
10652      method invokation that is now a static invocation. It will have
10653      callee displace `this' to insert it in the regular argument
10654      list. */
10655   return (non_static_context && to_return);
10656 }
10657
10658 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10659    mode.  */
10660
10661 static tree
10662 patch_invoke (patch, method, args)
10663      tree patch, method, args;
10664 {
10665   tree dtable, func;
10666   tree original_call, t, ta;
10667   tree check = NULL_TREE;
10668
10669   /* Last step for args: convert build-in types. If we're dealing with
10670      a new TYPE() type call, the first argument to the constructor
10671      isn't found in the incoming argument list, but delivered by
10672      `new' */
10673   t = TYPE_ARG_TYPES (TREE_TYPE (method));
10674   if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10675     t = TREE_CHAIN (t);
10676   for (ta = args; t != end_params_node && ta;
10677        t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10678     if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10679         TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10680       TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10681
10682   /* Resolve unresolved returned type isses */
10683   t = TREE_TYPE (TREE_TYPE (method));
10684   if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10685     resolve_and_layout (TREE_TYPE (t), NULL);
10686
10687   if (flag_emit_class_files || flag_emit_xref)
10688     func = method;
10689   else
10690     {
10691       switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10692         {
10693         case INVOKE_VIRTUAL:
10694           dtable = invoke_build_dtable (0, args);
10695           func = build_invokevirtual (dtable, method);
10696           break;
10697
10698         case INVOKE_NONVIRTUAL:
10699           /* If the object for the method call is null, we throw an
10700              exception.  We don't do this if the object is the current
10701              method's `this'.  In other cases we just rely on an
10702              optimization pass to eliminate redundant checks.  */
10703           if (TREE_VALUE (args) != current_this)
10704             {
10705               /* We use a save_expr here to make sure we only evaluate
10706                  the new `self' expression once.  */
10707               tree save_arg = save_expr (TREE_VALUE (args));
10708               TREE_VALUE (args) = save_arg;
10709               check = java_check_reference (save_arg, 1);
10710             }
10711           /* Fall through.  */
10712
10713         case INVOKE_SUPER:
10714         case INVOKE_STATIC:
10715           {
10716             tree signature = build_java_signature (TREE_TYPE (method));
10717             func = build_known_method_ref (method, TREE_TYPE (method),
10718                                            DECL_CONTEXT (method),
10719                                            signature, args);
10720           }
10721           break;
10722
10723         case INVOKE_INTERFACE:
10724           dtable = invoke_build_dtable (1, args);
10725           func = build_invokeinterface (dtable, method);
10726           break;
10727
10728         default:
10729           abort ();
10730         }
10731
10732       /* Ensure self_type is initialized, (invokestatic). FIXME */
10733       func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10734     }
10735
10736   TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10737   TREE_OPERAND (patch, 0) = func;
10738   TREE_OPERAND (patch, 1) = args;
10739   patch = check_for_builtin (method, patch);
10740   original_call = patch;
10741
10742   /* We're processing a `new TYPE ()' form. New is called and its
10743      returned value is the first argument to the constructor. We build
10744      a COMPOUND_EXPR and use saved expression so that the overall NEW
10745      expression value is a pointer to a newly created and initialized
10746      class. */
10747   if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10748     {
10749       tree class = DECL_CONTEXT (method);
10750       tree c1, saved_new, size, new;
10751       tree alloc_node;
10752
10753       if (flag_emit_class_files || flag_emit_xref)
10754         {
10755           TREE_TYPE (patch) = build_pointer_type (class);
10756           return patch;
10757         }
10758       if (!TYPE_SIZE (class))
10759         safe_layout_class (class);
10760       size = size_in_bytes (class);
10761       alloc_node =
10762         (class_has_finalize_method (class) ? alloc_object_node
10763                                            : alloc_no_finalizer_node);
10764       new = build (CALL_EXPR, promote_type (class),
10765                    build_address_of (alloc_node),
10766                    tree_cons (NULL_TREE, build_class_ref (class),
10767                               build_tree_list (NULL_TREE,
10768                                                size_in_bytes (class))),
10769                    NULL_TREE);
10770       saved_new = save_expr (new);
10771       c1 = build_tree_list (NULL_TREE, saved_new);
10772       TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10773       TREE_OPERAND (original_call, 1) = c1;
10774       TREE_SET_CODE (original_call, CALL_EXPR);
10775       patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10776     }
10777
10778   /* If CHECK is set, then we are building a check to see if the object
10779      is NULL.  */
10780   if (check != NULL_TREE)
10781     {
10782       patch = build (COMPOUND_EXPR, TREE_TYPE (patch), check, patch);
10783       TREE_SIDE_EFFECTS (patch) = 1;
10784     }
10785
10786   /* In order to be able to modify PATCH later, we SAVE_EXPR it and
10787      put it as the first expression of a COMPOUND_EXPR. The second
10788      expression being an empty statement to be later patched if
10789      necessary. We remember a TREE_LIST (the PURPOSE is the method,
10790      the VALUE is the compound) in a hashtable and return a
10791      COMPOUND_EXPR built so that the result of the evaluation of the
10792      original PATCH node is returned. */
10793   if (STATIC_CLASS_INIT_OPT_P ()
10794       && current_function_decl && METHOD_STATIC (method))
10795     {
10796       tree list;
10797       tree fndecl = current_function_decl;
10798       tree save = save_expr (patch);
10799       tree type = TREE_TYPE (patch);
10800
10801       patch = build (COMPOUND_EXPR, type, save, empty_stmt_node);
10802       list = tree_cons (method, patch,
10803                         DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
10804
10805       DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
10806
10807       patch = build (COMPOUND_EXPR, type, patch, save);
10808     }
10809
10810   return patch;
10811 }
10812
10813 static int
10814 invocation_mode (method, super)
10815      tree method;
10816      int super;
10817 {
10818   int access = get_access_flags_from_decl (method);
10819
10820   if (super)
10821     return INVOKE_SUPER;
10822
10823   if (access & ACC_STATIC)
10824     return INVOKE_STATIC;
10825
10826   /* We have to look for a constructor before we handle nonvirtual
10827      calls; otherwise the constructor will look nonvirtual.  */
10828   if (DECL_CONSTRUCTOR_P (method))
10829     return INVOKE_STATIC;
10830
10831   if (access & ACC_FINAL || access & ACC_PRIVATE)
10832     return INVOKE_NONVIRTUAL;
10833
10834   if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10835     return INVOKE_NONVIRTUAL;
10836
10837   if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10838     return INVOKE_INTERFACE;
10839
10840   return INVOKE_VIRTUAL;
10841 }
10842
10843 /* Retrieve a refined list of matching methods. It covers the step
10844    15.11.2 (Compile-Time Step 2) */
10845
10846 static tree
10847 lookup_method_invoke (lc, cl, class, name, arg_list)
10848      int lc;
10849      tree cl;
10850      tree class, name, arg_list;
10851 {
10852   tree atl = end_params_node;           /* Arg Type List */
10853   tree method, signature, list, node;
10854   const char *candidates;               /* Used for error report */
10855   char *dup;
10856
10857   /* Fix the arguments */
10858   for (node = arg_list; node; node = TREE_CHAIN (node))
10859     {
10860       tree current_arg = TREE_TYPE (TREE_VALUE (node));
10861       /* Non primitive type may have to be resolved */
10862       if (!JPRIMITIVE_TYPE_P (current_arg))
10863         resolve_and_layout (current_arg, NULL_TREE);
10864       /* And promoted */
10865       if (TREE_CODE (current_arg) == RECORD_TYPE)
10866         current_arg = promote_type (current_arg);
10867       atl = tree_cons (NULL_TREE, current_arg, atl);
10868     }
10869
10870   /* Presto. If we're dealing with an anonymous class and a
10871      constructor call, generate the right constructor now, since we
10872      know the arguments' types. */
10873
10874   if (lc && ANONYMOUS_CLASS_P (class))
10875     {
10876       tree saved_current_class;
10877       tree mdecl = craft_constructor (TYPE_NAME (class), atl);
10878       saved_current_class = current_class;
10879       current_class = class;
10880       fix_constructors (mdecl);
10881       current_class = saved_current_class;
10882     }
10883
10884   /* Find all candidates and then refine the list, searching for the
10885      most specific method. */
10886   list = find_applicable_accessible_methods_list (lc, class, name, atl);
10887   list = find_most_specific_methods_list (list);
10888   if (list && !TREE_CHAIN (list))
10889     return TREE_VALUE (list);
10890
10891   /* Issue an error. List candidates if any. Candidates are listed
10892      only if accessible (non accessible methods may end-up here for
10893      the sake of a better error report). */
10894   candidates = NULL;
10895   if (list)
10896     {
10897       tree current;
10898       obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10899       for (current = list; current; current = TREE_CHAIN (current))
10900         {
10901           tree cm = TREE_VALUE (current);
10902           char string [4096];
10903           if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
10904             continue;
10905           sprintf
10906             (string, "  `%s' in `%s'%s",
10907              get_printable_method_name (cm),
10908              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10909              (TREE_CHAIN (current) ? "\n" : ""));
10910           obstack_grow (&temporary_obstack, string, strlen (string));
10911         }
10912       obstack_1grow (&temporary_obstack, '\0');
10913       candidates = obstack_finish (&temporary_obstack);
10914     }
10915   /* Issue the error message */
10916   method = make_node (FUNCTION_TYPE);
10917   TYPE_ARG_TYPES (method) = atl;
10918   signature = build_java_argument_signature (method);
10919   dup = xstrdup (lang_printable_name (class, 0));
10920   parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10921                        (lc ? "constructor" : "method"),
10922                        (lc ? dup : IDENTIFIER_POINTER (name)),
10923                        IDENTIFIER_POINTER (signature), dup,
10924                        (candidates ? candidates : ""));
10925   free (dup);
10926   return NULL_TREE;
10927 }
10928
10929 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10930    when we're looking for a constructor. */
10931
10932 static tree
10933 find_applicable_accessible_methods_list (lc, class, name, arglist)
10934      int lc;
10935      tree class, name, arglist;
10936 {
10937   static htab_t searched_classes;
10938   static int search_not_done = 0;
10939   tree list = NULL_TREE, all_list = NULL_TREE;
10940
10941   /* Check the hash table to determine if this class has been searched
10942      already. */
10943   if (searched_classes)
10944     {
10945       if (htab_find (searched_classes, class) != NULL)
10946         return NULL;
10947     }
10948   else
10949     {
10950       searched_classes = htab_create (10, htab_hash_pointer,
10951                                       htab_eq_pointer, NULL);
10952     }
10953
10954   search_not_done++;
10955   *htab_find_slot (searched_classes, class, INSERT) = class;
10956
10957   if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10958     {
10959       load_class (class, 1);
10960       safe_layout_class (class);
10961     }
10962
10963   /* Search interfaces */
10964   if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10965       && CLASS_INTERFACE (TYPE_NAME (class)))
10966     {
10967       int i, n;
10968       tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10969       search_applicable_methods_list (lc, TYPE_METHODS (class),
10970                                       name, arglist, &list, &all_list);
10971       n = TREE_VEC_LENGTH (basetype_vec);
10972       for (i = 1; i < n; i++)
10973         {
10974           tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10975           tree rlist;
10976
10977           rlist = find_applicable_accessible_methods_list (lc,  t, name,
10978                                                            arglist);
10979           list = chainon (rlist, list);
10980         }
10981     }
10982   /* Search classes */
10983   else
10984     {
10985       search_applicable_methods_list (lc, TYPE_METHODS (class),
10986                                       name, arglist, &list, &all_list);
10987
10988       /* When looking finit$, class$ or instinit$, we turn LC to 1 so
10989          that we only search in class. Note that we should have found
10990          something at this point. */
10991       if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
10992         {
10993           lc = 1;
10994           if (!list)
10995             abort ();
10996         }
10997
10998       /* We must search all interfaces of this class */
10999       if (!lc)
11000       {
11001         tree basetype_vec = TYPE_BINFO_BASETYPES (class);
11002         int n = TREE_VEC_LENGTH (basetype_vec), i;
11003         for (i = 1; i < n; i++)
11004           {
11005             tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
11006             if (t != object_type_node)
11007               {
11008                 tree rlist
11009                   = find_applicable_accessible_methods_list (lc, t,
11010                                                              name, arglist);
11011                 list = chainon (rlist, list);
11012               }
11013           }
11014       }
11015
11016       /* Search superclass */
11017       if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
11018         {
11019           tree rlist;
11020           class = CLASSTYPE_SUPER (class);
11021           rlist = find_applicable_accessible_methods_list (lc, class,
11022                                                            name, arglist);
11023           list = chainon (rlist, list);
11024         }
11025     }
11026
11027   search_not_done--;
11028
11029   /* We're done. Reset the searched classes list and finally search
11030      java.lang.Object if it wasn't searched already. */
11031   if (!search_not_done)
11032     {
11033       if (!lc
11034           && TYPE_METHODS (object_type_node)
11035           && htab_find (searched_classes, object_type_node) == NULL)
11036         {
11037           search_applicable_methods_list (lc,
11038                                           TYPE_METHODS (object_type_node),
11039                                           name, arglist, &list, &all_list);
11040         }
11041       htab_delete (searched_classes);
11042       searched_classes = NULL;
11043     }
11044
11045   /* Either return the list obtained or all selected (but
11046      inaccessible) methods for better error report. */
11047   return (!list ? all_list : list);
11048 }
11049
11050 /* Effectively search for the appropriate method in method */
11051
11052 static void
11053 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
11054      int lc;
11055      tree method, name, arglist;
11056      tree *list, *all_list;
11057 {
11058   for (; method; method = TREE_CHAIN (method))
11059     {
11060       /* When dealing with constructor, stop here, otherwise search
11061          other classes */
11062       if (lc && !DECL_CONSTRUCTOR_P (method))
11063         continue;
11064       else if (!lc && (DECL_CONSTRUCTOR_P (method)
11065                        || (DECL_NAME (method) != name)))
11066         continue;
11067
11068       if (argument_types_convertible (method, arglist))
11069         {
11070           /* Retain accessible methods only */
11071           if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
11072                                  method, NULL_TREE, 0))
11073             *list = tree_cons (NULL_TREE, method, *list);
11074           else
11075             /* Also retain all selected method here */
11076             *all_list = tree_cons (NULL_TREE, method, *list);
11077         }
11078     }
11079 }
11080
11081 /* 15.11.2.2 Choose the Most Specific Method */
11082
11083 static tree
11084 find_most_specific_methods_list (list)
11085      tree list;
11086 {
11087   int max = 0;
11088   int abstract, candidates;
11089   tree current, new_list = NULL_TREE;
11090   for (current = list; current; current = TREE_CHAIN (current))
11091     {
11092       tree method;
11093       DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
11094
11095       for (method = list; method; method = TREE_CHAIN (method))
11096         {
11097           tree method_v, current_v;
11098           /* Don't test a method against itself */
11099           if (method == current)
11100             continue;
11101
11102           method_v = TREE_VALUE (method);
11103           current_v = TREE_VALUE (current);
11104
11105           /* Compare arguments and location where methods where declared */
11106           if (argument_types_convertible (method_v, current_v))
11107             {
11108               if (valid_method_invocation_conversion_p
11109                   (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
11110                   || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
11111                       && enclosing_context_p (DECL_CONTEXT (method_v),
11112                                               DECL_CONTEXT (current_v))))
11113                 {
11114                   int v = (DECL_SPECIFIC_COUNT (current_v) +=
11115                     (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
11116                   max = (v > max ? v : max);
11117                 }
11118             }
11119         }
11120     }
11121
11122   /* Review the list and select the maximally specific methods */
11123   for (current = list, abstract = -1, candidates = -1;
11124        current; current = TREE_CHAIN (current))
11125     if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11126       {
11127         new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11128         abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11129         candidates++;
11130       }
11131
11132   /* If we have several and they're all abstract, just pick the
11133      closest one. */
11134   if (candidates > 0 && (candidates == abstract))
11135     {
11136       new_list = nreverse (new_list);
11137       TREE_CHAIN (new_list) = NULL_TREE;
11138     }
11139
11140   /* We have several (we couldn't find a most specific), all but one
11141      are abstract, we pick the only non abstract one. */
11142   if (candidates > 0 && (candidates == abstract+1))
11143     {
11144       for (current = new_list; current; current = TREE_CHAIN (current))
11145         if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11146           {
11147             TREE_CHAIN (current) = NULL_TREE;
11148             new_list = current;
11149           }
11150     }
11151
11152   /* If we can't find one, lower expectations and try to gather multiple
11153      maximally specific methods */
11154   while (!new_list && max)
11155     {
11156       while (--max > 0)
11157         {
11158           if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11159             new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11160         }
11161     }
11162
11163   return new_list;
11164 }
11165
11166 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
11167    converted by method invocation conversion (5.3) to the type of the
11168    corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11169    to change less often than M1. */
11170
11171 static GTY(()) tree m2_arg_value;
11172 static GTY(()) tree m2_arg_cache;
11173
11174 static int
11175 argument_types_convertible (m1, m2_or_arglist)
11176     tree m1, m2_or_arglist;
11177 {
11178   register tree m1_arg, m2_arg;
11179
11180   SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11181
11182   if (m2_arg_value == m2_or_arglist)
11183     m2_arg = m2_arg_cache;
11184   else
11185     {
11186       /* M2_OR_ARGLIST can be a function DECL or a raw list of
11187          argument types */
11188       if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11189         {
11190           m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11191           if (!METHOD_STATIC (m2_or_arglist))
11192             m2_arg = TREE_CHAIN (m2_arg);
11193         }
11194       else
11195         m2_arg = m2_or_arglist;
11196
11197       m2_arg_value = m2_or_arglist;
11198       m2_arg_cache = m2_arg;
11199     }
11200
11201   while (m1_arg != end_params_node && m2_arg != end_params_node)
11202     {
11203       resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11204       if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11205                                                  TREE_VALUE (m2_arg)))
11206         break;
11207       m1_arg = TREE_CHAIN (m1_arg);
11208       m2_arg = TREE_CHAIN (m2_arg);
11209     }
11210   return m1_arg == end_params_node && m2_arg == end_params_node;
11211 }
11212
11213 /* Qualification routines */
11214
11215 static void
11216 qualify_ambiguous_name (id)
11217      tree id;
11218 {
11219   tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
11220     saved_current_class;
11221   int again, super_found = 0, this_found = 0, new_array_found = 0;
11222   int code;
11223
11224   /* We first qualify the first element, then derive qualification of
11225      others based on the first one. If the first element is qualified
11226      by a resolution (field or type), this resolution is stored in the
11227      QUAL_RESOLUTION of the qual element being examined. We need to
11228      save the current_class since the use of SUPER might change the
11229      its value. */
11230   saved_current_class = current_class;
11231   qual = EXPR_WFL_QUALIFICATION (id);
11232   do {
11233
11234     /* Simple qualified expression feature a qual_wfl that is a
11235        WFL. Expression derived from a primary feature more complicated
11236        things like a CALL_EXPR. Expression from primary need to be
11237        worked out to extract the part on which the qualification will
11238        take place. */
11239     qual_wfl = QUAL_WFL (qual);
11240     switch (TREE_CODE (qual_wfl))
11241       {
11242       case CALL_EXPR:
11243         qual_wfl = TREE_OPERAND (qual_wfl, 0);
11244         if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION
11245             || (EXPR_WFL_QUALIFICATION (qual_wfl)
11246                 && TREE_CODE (EXPR_WFL_QUALIFICATION (qual_wfl)) == TREE_LIST))
11247           {
11248             qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11249             qual_wfl = QUAL_WFL (qual);
11250           }
11251         break;
11252       case NEW_ARRAY_EXPR:
11253       case NEW_ANONYMOUS_ARRAY_EXPR:
11254         qual = TREE_CHAIN (qual);
11255         again = new_array_found = 1;
11256         continue;
11257       case CONVERT_EXPR:
11258         break;
11259       case NEW_CLASS_EXPR:
11260         qual_wfl = TREE_OPERAND (qual_wfl, 0);
11261         break;
11262       case ARRAY_REF:
11263         while (TREE_CODE (qual_wfl) == ARRAY_REF)
11264           qual_wfl = TREE_OPERAND (qual_wfl, 0);
11265         break;
11266       case STRING_CST:
11267         qual = TREE_CHAIN (qual);
11268         qual_wfl = QUAL_WFL (qual);
11269         break;
11270       case CLASS_LITERAL:
11271         qual = TREE_CHAIN (qual);
11272         qual_wfl = QUAL_WFL (qual);
11273       break;
11274       default:
11275         /* Fix for -Wall. Just break doing nothing */
11276         break;
11277       }
11278
11279     ptr_type = current_class;
11280     again = 0;
11281     code = TREE_CODE (qual_wfl);
11282
11283     /* Pos evaluation: non WFL leading expression nodes */
11284     if (code == CONVERT_EXPR
11285         && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
11286       name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
11287
11288     else if (code == INTEGER_CST)
11289       name = qual_wfl;
11290
11291     else if (code == CONVERT_EXPR &&
11292              TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11293       name = TREE_OPERAND (qual_wfl, 0);
11294
11295     else if (code == CONVERT_EXPR
11296              && TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == CALL_EXPR
11297              && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (qual_wfl, 0), 0))
11298                  == EXPR_WITH_FILE_LOCATION))
11299       name = TREE_OPERAND (TREE_OPERAND (qual_wfl, 0), 0);
11300
11301     else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
11302              TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11303       name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
11304
11305     else if (code == TREE_LIST)
11306       name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
11307
11308     else if (code == STRING_CST || code == CONDITIONAL_EXPR
11309              || code == PLUS_EXPR)
11310       {
11311         qual = TREE_CHAIN (qual);
11312         qual_wfl = QUAL_WFL (qual);
11313         again = 1;
11314       }
11315     else
11316       {
11317         name = EXPR_WFL_NODE (qual_wfl);
11318         if (!name)
11319           {
11320             qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11321             again = 1;
11322           }
11323       }
11324
11325     /* If we have a THIS (from a primary), we set the context accordingly */
11326     if (name == this_identifier_node)
11327       {
11328         /* This isn't really elegant. One more added irregularity
11329            before I start using COMPONENT_REF (hopefully very soon.)  */
11330         if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
11331             && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
11332                EXPR_WITH_FILE_LOCATION
11333             && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
11334                this_identifier_node)
11335             {
11336               qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
11337               qual = EXPR_WFL_QUALIFICATION (qual);
11338             }
11339         qual = TREE_CHAIN (qual);
11340         qual_wfl = QUAL_WFL (qual);
11341         if (TREE_CODE (qual_wfl) == CALL_EXPR)
11342           again = 1;
11343         else if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION)
11344           name = EXPR_WFL_NODE (qual_wfl);
11345         else if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
11346           name = TREE_OPERAND (qual_wfl, 0);
11347         this_found = 1;
11348       }
11349     /* If we have a SUPER, we set the context accordingly */
11350     if (name == super_identifier_node)
11351       {
11352         current_class = CLASSTYPE_SUPER (ptr_type);
11353         /* Check that there is such a thing as a super class. If not,
11354            return.  The error will be caught later on, during the
11355            resolution */
11356         if (!current_class)
11357           {
11358             current_class = saved_current_class;
11359             return;
11360           }
11361         qual = TREE_CHAIN (qual);
11362         /* Do one more interation to set things up */
11363         super_found = again = 1;
11364       }
11365   } while (again);
11366
11367   /* If name appears within the scope of a local variable declaration
11368      or parameter declaration, then it is an expression name. We don't
11369      carry this test out if we're in the context of the use of SUPER
11370      or THIS */
11371   if (!this_found && !super_found
11372       && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
11373       && (decl = IDENTIFIER_LOCAL_VALUE (name)))
11374     {
11375       RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11376       QUAL_RESOLUTION (qual) = decl;
11377     }
11378
11379   /* If within the class/interface NAME was found to be used there
11380      exists a (possibly inherited) field named NAME, then this is an
11381      expression name. If we saw a NEW_ARRAY_EXPR before and want to
11382      address length, it is OK. */
11383   else if ((decl = lookup_field_wrapper (ptr_type, name))
11384            || name == length_identifier_node)
11385     {
11386       RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11387       QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
11388     }
11389
11390   /* We reclassify NAME as yielding to a type name resolution if:
11391      - NAME is a class/interface declared within the compilation
11392        unit containing NAME,
11393      - NAME is imported via a single-type-import declaration,
11394      - NAME is declared in an another compilation unit of the package
11395        of the compilation unit containing NAME,
11396      - NAME is declared by exactly on type-import-on-demand declaration
11397      of the compilation unit containing NAME.
11398      - NAME is actually a STRING_CST.
11399      This can't happen if the expression was qualified by `this.' */
11400   else if (! this_found &&
11401            (TREE_CODE (name) == STRING_CST ||
11402             TREE_CODE (name) == INTEGER_CST ||
11403             (decl = resolve_and_layout (name, NULL_TREE))))
11404     {
11405       RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11406       QUAL_RESOLUTION (qual) = decl;
11407     }
11408
11409   /* Method call, array references and cast are expression name */
11410   else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
11411            || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
11412            || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR
11413            || TREE_CODE (QUAL_WFL (qual)) == MODIFY_EXPR)
11414     RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11415
11416   /* Check here that NAME isn't declared by more than one
11417      type-import-on-demand declaration of the compilation unit
11418      containing NAME. FIXME */
11419
11420   /* Otherwise, NAME is reclassified as a package name */
11421   else
11422     RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11423
11424   /* Propagate the qualification accross other components of the
11425      qualified name */
11426   for (qual = TREE_CHAIN (qual); qual;
11427        qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11428     {
11429       if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11430         RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11431       else
11432         RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
11433     }
11434
11435   /* Store the global qualification for the ambiguous part of ID back
11436      into ID fields */
11437   if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
11438     RESOLVE_EXPRESSION_NAME_P (id) = 1;
11439   else if (RESOLVE_TYPE_NAME_P (qual_wfl))
11440     RESOLVE_TYPE_NAME_P (id) = 1;
11441   else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11442     RESOLVE_PACKAGE_NAME_P (id) = 1;
11443
11444   /* Restore the current class */
11445   current_class = saved_current_class;
11446 }
11447
11448 static int
11449 breakdown_qualified (left, right, source)
11450     tree *left, *right, source;
11451 {
11452   char *p, *base;
11453   int   l = IDENTIFIER_LENGTH (source);
11454
11455   base = alloca (l + 1);
11456   memcpy (base, IDENTIFIER_POINTER (source), l + 1);
11457
11458   /* Breakdown NAME into REMAINDER . IDENTIFIER */
11459   p = base + l - 1;
11460   while (*p != '.' && p != base)
11461     p--;
11462
11463   /* We didn't find a '.'. Return an error */
11464   if (p == base)
11465     return 1;
11466
11467   *p = '\0';
11468   if (right)
11469     *right = get_identifier (p+1);
11470   *left = get_identifier (base);
11471
11472   return 0;
11473 }
11474
11475 /* Return TRUE if two classes are from the same package. */
11476
11477 static int
11478 in_same_package (name1, name2)
11479   tree name1, name2;
11480 {
11481   tree tmp;
11482   tree pkg1;
11483   tree pkg2;
11484
11485   if (TREE_CODE (name1) == TYPE_DECL)
11486     name1 = DECL_NAME (name1);
11487   if (TREE_CODE (name2) == TYPE_DECL)
11488     name2 = DECL_NAME (name2);
11489
11490   if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
11491     /* One in empty package. */
11492     return 0;
11493
11494   if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
11495     /* Both in empty package. */
11496     return 1;
11497
11498   breakdown_qualified (&pkg1, &tmp, name1);
11499   breakdown_qualified (&pkg2, &tmp, name2);
11500
11501   return (pkg1 == pkg2);
11502 }
11503
11504 /* Patch tree nodes in a function body. When a BLOCK is found, push
11505    local variable decls if present.
11506    Same as java_complete_lhs, but does resolve static finals to values. */
11507
11508 static tree
11509 java_complete_tree (node)
11510      tree node;
11511 {
11512   node = java_complete_lhs (node);
11513   if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11514       && DECL_INITIAL (node) != NULL_TREE
11515       && !flag_emit_xref)
11516     {
11517       tree value = DECL_INITIAL (node);
11518       DECL_INITIAL (node) = NULL_TREE;
11519       value = fold_constant_for_init (value, node);
11520       DECL_INITIAL (node) = value;
11521       if (value != NULL_TREE)
11522         {
11523           /* fold_constant_for_init sometimes widens the original type
11524              of the constant (i.e. byte to int). It's not desirable,
11525              especially if NODE is a function argument. */
11526           if ((TREE_CODE (value) == INTEGER_CST
11527                || TREE_CODE (value) == REAL_CST)
11528               && TREE_TYPE (node) != TREE_TYPE (value))
11529             return convert (TREE_TYPE (node), value);
11530           else
11531             return value;
11532         }
11533     }
11534   return node;
11535 }
11536
11537 static tree
11538 java_stabilize_reference (node)
11539      tree node;
11540 {
11541   if (TREE_CODE (node) == COMPOUND_EXPR)
11542     {
11543       tree op0 = TREE_OPERAND (node, 0);
11544       tree op1 = TREE_OPERAND (node, 1);
11545       TREE_OPERAND (node, 0) = save_expr (op0);
11546       TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11547       return node;
11548     }
11549   return stabilize_reference (node);
11550 }
11551
11552 /* Patch tree nodes in a function body. When a BLOCK is found, push
11553    local variable decls if present.
11554    Same as java_complete_tree, but does not resolve static finals to values. */
11555
11556 static tree
11557 java_complete_lhs (node)
11558      tree node;
11559 {
11560   tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11561   int flag;
11562
11563   /* CONVERT_EXPR always has its type set, even though it needs to be
11564      worked out. */
11565   if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11566     return node;
11567
11568   /* The switch block implements cases processing container nodes
11569      first.  Contained nodes are always written back. Leaves come
11570      next and return a value. */
11571   switch (TREE_CODE (node))
11572     {
11573     case BLOCK:
11574
11575       /* 1- Block section.
11576          Set the local values on decl names so we can identify them
11577          faster when they're referenced. At that stage, identifiers
11578          are legal so we don't check for declaration errors. */
11579       for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11580         {
11581           DECL_CONTEXT (cn) = current_function_decl;
11582           IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11583         }
11584       if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11585           CAN_COMPLETE_NORMALLY (node) = 1;
11586       else
11587         {
11588           tree stmt = BLOCK_EXPR_BODY (node);
11589           tree *ptr;
11590           int error_seen = 0;
11591           if (TREE_CODE (stmt) == COMPOUND_EXPR)
11592             {
11593               /* Re-order from (((A; B); C); ...; Z) to
11594                  (A; (B; (C ; (...; Z)))).
11595                  This makes it easier to scan the statements left-to-right
11596                  without using recursion (which might overflow the stack
11597                  if the block has many statements. */
11598               for (;;)
11599                 {
11600                   tree left = TREE_OPERAND (stmt, 0);
11601                   if (TREE_CODE (left) != COMPOUND_EXPR)
11602                     break;
11603                   TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11604                   TREE_OPERAND (left, 1) = stmt;
11605                   stmt = left;
11606                 }
11607               BLOCK_EXPR_BODY (node) = stmt;
11608             }
11609
11610           /* Now do the actual complete, without deep recursion for
11611              long blocks. */
11612           ptr = &BLOCK_EXPR_BODY (node);
11613           while (TREE_CODE (*ptr) == COMPOUND_EXPR
11614                  && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
11615             {
11616               tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11617               tree *next = &TREE_OPERAND (*ptr, 1);
11618               TREE_OPERAND (*ptr, 0) = cur;
11619               if (cur == empty_stmt_node)
11620                 {
11621                   /* Optimization;  makes it easier to detect empty bodies.
11622                      Most useful for <clinit> with all-constant initializer. */
11623                   *ptr = *next;
11624                   continue;
11625                 }
11626               if (TREE_CODE (cur) == ERROR_MARK)
11627                 error_seen++;
11628               else if (! CAN_COMPLETE_NORMALLY (cur))
11629                 {
11630                   wfl_op2 = *next;
11631                   for (;;)
11632                     {
11633                       if (TREE_CODE (wfl_op2) == BLOCK)
11634                         wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11635                       else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11636                         wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11637                       else
11638                         break;
11639                     }
11640                   if (TREE_CODE (wfl_op2) != CASE_EXPR
11641                       && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11642                     unreachable_stmt_error (*ptr);
11643                 }
11644               ptr = next;
11645             }
11646           *ptr = java_complete_tree (*ptr);
11647
11648           if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11649             return error_mark_node;
11650           CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11651         }
11652       /* Turn local bindings to null */
11653       for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11654         IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11655
11656       TREE_TYPE (node) = void_type_node;
11657       break;
11658
11659       /* 2- They are expressions but ultimately deal with statements */
11660
11661     case THROW_EXPR:
11662       wfl_op1 = TREE_OPERAND (node, 0);
11663       COMPLETE_CHECK_OP_0 (node);
11664       /* 14.19 A throw statement cannot complete normally. */
11665       CAN_COMPLETE_NORMALLY (node) = 0;
11666       return patch_throw_statement (node, wfl_op1);
11667
11668     case SYNCHRONIZED_EXPR:
11669       wfl_op1 = TREE_OPERAND (node, 0);
11670       return patch_synchronized_statement (node, wfl_op1);
11671
11672     case TRY_EXPR:
11673       return patch_try_statement (node);
11674
11675     case TRY_FINALLY_EXPR:
11676       COMPLETE_CHECK_OP_0 (node);
11677       COMPLETE_CHECK_OP_1 (node);
11678       if (TREE_OPERAND (node, 0) == empty_stmt_node)
11679         return TREE_OPERAND (node, 1);
11680       if (TREE_OPERAND (node, 1) == empty_stmt_node)
11681         return TREE_OPERAND (node, 0);
11682       CAN_COMPLETE_NORMALLY (node)
11683         = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11684            && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11685       TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11686       return node;
11687
11688     case LABELED_BLOCK_EXPR:
11689       PUSH_LABELED_BLOCK (node);
11690       if (LABELED_BLOCK_BODY (node))
11691         COMPLETE_CHECK_OP_1 (node);
11692       TREE_TYPE (node) = void_type_node;
11693       POP_LABELED_BLOCK ();
11694
11695       if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11696         {
11697           LABELED_BLOCK_BODY (node) = NULL_TREE;
11698           CAN_COMPLETE_NORMALLY (node) = 1;
11699         }
11700       else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11701         CAN_COMPLETE_NORMALLY (node) = 1;
11702       return node;
11703
11704     case EXIT_BLOCK_EXPR:
11705       /* We don't complete operand 1, because it's the return value of
11706          the EXIT_BLOCK_EXPR which doesn't exist it Java */
11707       return patch_bc_statement (node);
11708
11709     case CASE_EXPR:
11710       cn = java_complete_tree (TREE_OPERAND (node, 0));
11711       if (cn == error_mark_node)
11712         return cn;
11713
11714       /* First, the case expression must be constant. Values of final
11715          fields are accepted. */
11716       cn = fold (cn);
11717       if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11718           && JDECL_P (TREE_OPERAND (cn, 1))
11719           && FIELD_FINAL (TREE_OPERAND (cn, 1))
11720           && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11721         {
11722           cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11723                                        TREE_OPERAND (cn, 1));
11724         }
11725       /* Accept final locals too. */
11726       else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn))
11727         cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11728
11729       if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11730         {
11731           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11732           parse_error_context (node, "Constant expression required");
11733           return error_mark_node;
11734         }
11735
11736       nn = ctxp->current_loop;
11737
11738       /* It must be assignable to the type of the switch expression. */
11739       if (!try_builtin_assignconv (NULL_TREE,
11740                                    TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11741         {
11742           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11743           parse_error_context
11744             (wfl_operator,
11745              "Incompatible type for case. Can't convert `%s' to `int'",
11746              lang_printable_name (TREE_TYPE (cn), 0));
11747           return error_mark_node;
11748         }
11749
11750       cn = fold (convert (int_type_node, cn));
11751       TREE_CONSTANT_OVERFLOW (cn) = 0;
11752       CAN_COMPLETE_NORMALLY (cn) = 1;
11753
11754       /* Save the label on a list so that we can later check for
11755          duplicates.  */
11756       case_label_list = tree_cons (node, cn, case_label_list);
11757
11758       /* Multiple instance of a case label bearing the same value is
11759          checked later. The case expression is all right so far. */
11760       if (TREE_CODE (cn) == VAR_DECL)
11761         cn = DECL_INITIAL (cn);
11762       TREE_OPERAND (node, 0) = cn;
11763       TREE_TYPE (node) = void_type_node;
11764       CAN_COMPLETE_NORMALLY (node) = 1;
11765       TREE_SIDE_EFFECTS (node) = 1;
11766       break;
11767
11768     case DEFAULT_EXPR:
11769       nn = ctxp->current_loop;
11770       /* Only one default label is allowed per switch statement */
11771       if (SWITCH_HAS_DEFAULT (nn))
11772         {
11773           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11774           parse_error_context (wfl_operator,
11775                                "Duplicate case label: `default'");
11776           return error_mark_node;
11777         }
11778       else
11779         SWITCH_HAS_DEFAULT (nn) = 1;
11780       TREE_TYPE (node) = void_type_node;
11781       TREE_SIDE_EFFECTS (node) = 1;
11782       CAN_COMPLETE_NORMALLY (node) = 1;
11783       break;
11784
11785     case SWITCH_EXPR:
11786     case LOOP_EXPR:
11787       PUSH_LOOP (node);
11788       /* Check whether the loop was enclosed in a labeled
11789          statement. If not, create one, insert the loop in it and
11790          return the node */
11791       nn = patch_loop_statement (node);
11792
11793       /* Anyways, walk the body of the loop */
11794       if (TREE_CODE (node) == LOOP_EXPR)
11795         TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11796       /* Switch statement: walk the switch expression and the cases */
11797       else
11798         node = patch_switch_statement (node);
11799
11800       if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11801         nn = error_mark_node;
11802       else
11803         {
11804           TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11805           /* If we returned something different, that's because we
11806              inserted a label. Pop the label too. */
11807           if (nn != node)
11808             {
11809               if (CAN_COMPLETE_NORMALLY (node))
11810                 CAN_COMPLETE_NORMALLY (nn) = 1;
11811               POP_LABELED_BLOCK ();
11812             }
11813         }
11814       POP_LOOP ();
11815       return nn;
11816
11817     case EXIT_EXPR:
11818       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11819       return patch_exit_expr (node);
11820
11821     case COND_EXPR:
11822       /* Condition */
11823       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11824       if (TREE_OPERAND (node, 0) == error_mark_node)
11825         return error_mark_node;
11826       /* then-else branches */
11827       TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11828       if (TREE_OPERAND (node, 1) == error_mark_node)
11829         return error_mark_node;
11830       TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11831       if (TREE_OPERAND (node, 2) == error_mark_node)
11832         return error_mark_node;
11833       return patch_if_else_statement (node);
11834       break;
11835
11836     case CONDITIONAL_EXPR:
11837       /* Condition */
11838       wfl_op1 = TREE_OPERAND (node, 0);
11839       COMPLETE_CHECK_OP_0 (node);
11840       wfl_op2 = TREE_OPERAND (node, 1);
11841       COMPLETE_CHECK_OP_1 (node);
11842       wfl_op3 = TREE_OPERAND (node, 2);
11843       COMPLETE_CHECK_OP_2 (node);
11844       return patch_conditional_expr (node, wfl_op1, wfl_op2);
11845
11846       /* 3- Expression section */
11847     case COMPOUND_EXPR:
11848       wfl_op2 = TREE_OPERAND (node, 1);
11849       TREE_OPERAND (node, 0) = nn =
11850         java_complete_tree (TREE_OPERAND (node, 0));
11851       if (wfl_op2 == empty_stmt_node)
11852         CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11853       else
11854         {
11855           if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11856             {
11857               /* An unreachable condition in a do-while statement
11858                  is *not* (technically) an unreachable statement. */
11859               nn = wfl_op2;
11860               if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11861                 nn = EXPR_WFL_NODE (nn);
11862               if (TREE_CODE (nn) != EXIT_EXPR)
11863                 {
11864                   SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11865                   if (SUPPRESS_UNREACHABLE_ERROR (nn))
11866                     {
11867                       /* Perhaps this warning should have an
11868                          associated flag.  The code being compiled is
11869                          pedantically correct, but useless.  */
11870                       parse_warning_context (wfl_operator,
11871                                              "Unreachable statement");
11872                     }
11873                   else
11874                     parse_error_context (wfl_operator,
11875                                          "Unreachable statement");
11876                 }
11877             }
11878           TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11879           if (TREE_OPERAND (node, 1) == error_mark_node)
11880             return error_mark_node;
11881           /* Even though we might allow the case where the first
11882              operand doesn't return normally, we still should compute
11883              CAN_COMPLETE_NORMALLY correctly.  */
11884           CAN_COMPLETE_NORMALLY (node)
11885             = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11886                && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11887         }
11888       TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11889       break;
11890
11891     case RETURN_EXPR:
11892       /* CAN_COMPLETE_NORMALLY (node) = 0; */
11893       return patch_return (node);
11894
11895     case EXPR_WITH_FILE_LOCATION:
11896       if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11897           || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11898         {
11899           tree wfl = node;
11900           node = resolve_expression_name (node, NULL);
11901           if (node == error_mark_node)
11902             return node;
11903           /* Keep line number information somewhere were it doesn't
11904              disrupt the completion process. */
11905           if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11906             {
11907               EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11908               TREE_OPERAND (node, 1) = wfl;
11909             }
11910           CAN_COMPLETE_NORMALLY (node) = 1;
11911         }
11912       else
11913         {
11914           tree body;
11915           int save_lineno = lineno;
11916           lineno = EXPR_WFL_LINENO (node);
11917           body = java_complete_tree (EXPR_WFL_NODE (node));
11918           lineno = save_lineno;
11919           EXPR_WFL_NODE (node) = body;
11920           TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11921           CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11922           if (body == empty_stmt_node || TREE_CONSTANT (body))
11923             {
11924               /* Makes it easier to constant fold, detect empty bodies. */
11925               return body;
11926             }
11927           if (body == error_mark_node)
11928             {
11929               /* Its important for the evaluation of assignment that
11930                  this mark on the TREE_TYPE is propagated. */
11931               TREE_TYPE (node) = error_mark_node;
11932               return error_mark_node;
11933             }
11934           else
11935             TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11936
11937         }
11938       break;
11939
11940     case NEW_ARRAY_EXPR:
11941       /* Patch all the dimensions */
11942       flag = 0;
11943       for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11944         {
11945           int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11946           tree dim = convert (int_type_node,
11947                               java_complete_tree (TREE_VALUE (cn)));
11948           if (dim == error_mark_node)
11949             {
11950               flag = 1;
11951               continue;
11952             }
11953           else
11954             {
11955               TREE_VALUE (cn) = dim;
11956               /* Setup the location of the current dimension, for
11957                  later error report. */
11958               TREE_PURPOSE (cn) =
11959                 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11960               EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11961             }
11962         }
11963       /* They complete the array creation expression, if no errors
11964          were found. */
11965       CAN_COMPLETE_NORMALLY (node) = 1;
11966       return (flag ? error_mark_node
11967               : force_evaluation_order (patch_newarray (node)));
11968
11969     case NEW_ANONYMOUS_ARRAY_EXPR:
11970       /* Create the array type if necessary. */
11971       if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11972         {
11973           tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11974           if (!(type = resolve_type_during_patch (type)))
11975             return error_mark_node;
11976           type = build_array_from_name (type, NULL_TREE,
11977                                         ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11978           ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11979         }
11980       node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11981                                    ANONYMOUS_ARRAY_INITIALIZER (node));
11982       if (node == error_mark_node)
11983         return error_mark_node;
11984       CAN_COMPLETE_NORMALLY (node) = 1;
11985       return node;
11986
11987     case NEW_CLASS_EXPR:
11988     case CALL_EXPR:
11989       /* Complete function's argument(s) first */
11990       if (complete_function_arguments (node))
11991         return error_mark_node;
11992       else
11993         {
11994           tree decl, wfl = TREE_OPERAND (node, 0);
11995           int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11996           int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
11997                            super_identifier_node);
11998
11999           node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
12000                                           from_super, 0, &decl);
12001           if (node == error_mark_node)
12002             return error_mark_node;
12003
12004           check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
12005           /* If we call this(...), register signature and positions */
12006           if (in_this)
12007             DECL_CONSTRUCTOR_CALLS (current_function_decl) =
12008               tree_cons (wfl, decl,
12009                          DECL_CONSTRUCTOR_CALLS (current_function_decl));
12010           CAN_COMPLETE_NORMALLY (node) = 1;
12011           return force_evaluation_order (node);
12012         }
12013
12014     case MODIFY_EXPR:
12015       /* Save potential wfls */
12016       wfl_op1 = TREE_OPERAND (node, 0);
12017       TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
12018
12019       if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
12020           && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
12021           && DECL_INITIAL (nn) != NULL_TREE)
12022         {
12023           tree value;
12024
12025           value = fold_constant_for_init (nn, nn);
12026
12027           /* When we have a primitype type, or a string and we're not
12028              emitting a class file, we actually don't want to generate
12029              anything for the assignment. */
12030           if (value != NULL_TREE &&
12031               (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) ||
12032                (TREE_TYPE (value) == string_ptr_type_node &&
12033                 ! flag_emit_class_files)))
12034             {
12035               /* Prepare node for patch_assignment */
12036               TREE_OPERAND (node, 1) = value;
12037               /* Call patch assignment to verify the assignment */
12038               if (patch_assignment (node, wfl_op1) == error_mark_node)
12039                 return error_mark_node;
12040               /* Set DECL_INITIAL properly (a conversion might have
12041                  been decided by patch_assignment) and return the
12042                  empty statement. */
12043               else
12044                 {
12045                   tree patched = patch_string (TREE_OPERAND (node, 1));
12046                   if (patched)
12047                     DECL_INITIAL (nn) = patched;
12048                   else
12049                     DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12050                   DECL_FIELD_FINAL_IUD (nn) = 1;
12051                   return empty_stmt_node;
12052                 }
12053             }
12054           if (! flag_emit_class_files)
12055             DECL_INITIAL (nn) = NULL_TREE;
12056         }
12057       wfl_op2 = TREE_OPERAND (node, 1);
12058
12059       if (TREE_OPERAND (node, 0) == error_mark_node)
12060         return error_mark_node;
12061
12062       flag = COMPOUND_ASSIGN_P (wfl_op2);
12063       if (flag)
12064         {
12065           /* This might break when accessing outer field from inner
12066              class. TESTME, FIXME */
12067           tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
12068
12069           /* Hand stabilize the lhs on both places */
12070           TREE_OPERAND (node, 0) = lvalue;
12071           TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
12072             (flag_emit_class_files ? lvalue : save_expr (lvalue));
12073
12074           /* 15.25.2.a: Left hand is not an array access. FIXME */
12075           /* Now complete the RHS. We write it back later on. */
12076           nn = java_complete_tree (TREE_OPERAND (node, 1));
12077
12078           if ((cn = patch_string (nn)))
12079             nn = cn;
12080
12081           /* The last part of the rewrite for E1 op= E2 is to have
12082              E1 = (T)(E1 op E2), with T being the type of E1. */
12083           nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
12084                                                TREE_TYPE (lvalue), nn));
12085
12086           /* If the assignment is compound and has reference type,
12087              then ensure the LHS has type String and nothing else.  */
12088           if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
12089               && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
12090             parse_error_context (wfl_op2,
12091                                  "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
12092                                  lang_printable_name (TREE_TYPE (lvalue), 0));
12093
12094           /* 15.25.2.b: Left hand is an array access. FIXME */
12095         }
12096
12097       /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12098          function to complete this RHS. Note that a NEW_ARRAY_INIT
12099          might have been already fully expanded if created as a result
12100          of processing an anonymous array initializer. We avoid doing
12101          the operation twice by testing whether the node already bears
12102          a type. */
12103       else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12104         nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12105                                    TREE_OPERAND (node, 1));
12106       /* Otherwise we simply complete the RHS */
12107       else
12108         nn = java_complete_tree (TREE_OPERAND (node, 1));
12109
12110       if (nn == error_mark_node)
12111         return error_mark_node;
12112
12113       /* Write back the RHS as we evaluated it. */
12114       TREE_OPERAND (node, 1) = nn;
12115
12116       /* In case we're handling = with a String as a RHS, we need to
12117          produce a String out of the RHS (it might still be a
12118          STRING_CST or a StringBuffer at this stage */
12119       if ((nn = patch_string (TREE_OPERAND (node, 1))))
12120         TREE_OPERAND (node, 1) = nn;
12121
12122       if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12123                                         TREE_OPERAND (node, 1))))
12124         {
12125           /* We return error_mark_node if outer_field_access_fix
12126              detects we write into a final. */
12127           if (nn == error_mark_node)
12128             return error_mark_node;
12129           node = nn;
12130         }
12131       else
12132         {
12133           node = patch_assignment (node, wfl_op1);
12134           if (node == error_mark_node)
12135             return error_mark_node;
12136           /* Reorganize the tree if necessary. */
12137           if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
12138                        || JSTRING_P (TREE_TYPE (node))))
12139             node = java_refold (node);
12140         }
12141
12142       /* Seek to set DECL_INITIAL to a proper value, since it might have
12143          undergone a conversion in patch_assignment. We do that only when
12144          it's necessary to have DECL_INITIAL properly set. */
12145       nn = TREE_OPERAND (node, 0);
12146       if (TREE_CODE (nn) == VAR_DECL
12147           && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12148           && FIELD_STATIC (nn) && FIELD_FINAL (nn)
12149           && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12150               || TREE_TYPE (nn) == string_ptr_type_node))
12151         DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12152
12153       CAN_COMPLETE_NORMALLY (node) = 1;
12154       return node;
12155
12156     case MULT_EXPR:
12157     case PLUS_EXPR:
12158     case MINUS_EXPR:
12159     case LSHIFT_EXPR:
12160     case RSHIFT_EXPR:
12161     case URSHIFT_EXPR:
12162     case BIT_AND_EXPR:
12163     case BIT_XOR_EXPR:
12164     case BIT_IOR_EXPR:
12165     case TRUNC_MOD_EXPR:
12166     case TRUNC_DIV_EXPR:
12167     case RDIV_EXPR:
12168     case TRUTH_ANDIF_EXPR:
12169     case TRUTH_ORIF_EXPR:
12170     case EQ_EXPR:
12171     case NE_EXPR:
12172     case GT_EXPR:
12173     case GE_EXPR:
12174     case LT_EXPR:
12175     case LE_EXPR:
12176       /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12177          knows how to handle those cases. */
12178       wfl_op1 = TREE_OPERAND (node, 0);
12179       wfl_op2 = TREE_OPERAND (node, 1);
12180
12181       CAN_COMPLETE_NORMALLY (node) = 1;
12182       /* Don't complete string nodes if dealing with the PLUS operand. */
12183       if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12184         {
12185           nn = java_complete_tree (wfl_op1);
12186           if (nn == error_mark_node)
12187             return error_mark_node;
12188
12189           TREE_OPERAND (node, 0) = nn;
12190         }
12191       if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12192         {
12193           nn = java_complete_tree (wfl_op2);
12194           if (nn == error_mark_node)
12195             return error_mark_node;
12196
12197           TREE_OPERAND (node, 1) = nn;
12198         }
12199       return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
12200
12201     case INSTANCEOF_EXPR:
12202       wfl_op1 = TREE_OPERAND (node, 0);
12203       COMPLETE_CHECK_OP_0 (node);
12204       if (flag_emit_xref)
12205         {
12206           TREE_TYPE (node) = boolean_type_node;
12207           return node;
12208         }
12209       return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
12210
12211     case UNARY_PLUS_EXPR:
12212     case NEGATE_EXPR:
12213     case TRUTH_NOT_EXPR:
12214     case BIT_NOT_EXPR:
12215     case PREDECREMENT_EXPR:
12216     case PREINCREMENT_EXPR:
12217     case POSTDECREMENT_EXPR:
12218     case POSTINCREMENT_EXPR:
12219     case CONVERT_EXPR:
12220       /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12221          how to handle those cases. */
12222       wfl_op1 = TREE_OPERAND (node, 0);
12223       CAN_COMPLETE_NORMALLY (node) = 1;
12224       TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12225       if (TREE_OPERAND (node, 0) == error_mark_node)
12226         return error_mark_node;
12227       node = patch_unaryop (node, wfl_op1);
12228       CAN_COMPLETE_NORMALLY (node) = 1;
12229       break;
12230
12231     case ARRAY_REF:
12232       /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12233          how to handle those cases. */
12234       wfl_op1 = TREE_OPERAND (node, 0);
12235       TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12236       if (TREE_OPERAND (node, 0) == error_mark_node)
12237         return error_mark_node;
12238       if (!flag_emit_class_files && !flag_emit_xref)
12239         TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12240       /* The same applies to wfl_op2 */
12241       wfl_op2 = TREE_OPERAND (node, 1);
12242       TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12243       if (TREE_OPERAND (node, 1) == error_mark_node)
12244         return error_mark_node;
12245       if (!flag_emit_class_files && !flag_emit_xref)
12246         TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12247       return patch_array_ref (node);
12248
12249     case RECORD_TYPE:
12250       return node;;
12251
12252     case COMPONENT_REF:
12253       /* The first step in the re-write of qualified name handling.  FIXME.
12254          So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
12255       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
12256       if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
12257         {
12258           tree name = TREE_OPERAND (node, 1);
12259           tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
12260           if (field == NULL_TREE)
12261             {
12262               error ("missing static field `%s'", IDENTIFIER_POINTER (name));
12263               return error_mark_node;
12264             }
12265           if (! FIELD_STATIC (field))
12266             {
12267               error ("not a static field `%s'", IDENTIFIER_POINTER (name));
12268               return error_mark_node;
12269             }
12270           return field;
12271         }
12272       else
12273         abort ();
12274       break;
12275
12276     case THIS_EXPR:
12277       /* Can't use THIS in a static environment */
12278       if (!current_this)
12279         {
12280           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12281           parse_error_context (wfl_operator,
12282                                "Keyword `this' used outside allowed context");
12283           TREE_TYPE (node) = error_mark_node;
12284           return error_mark_node;
12285         }
12286       if (ctxp->explicit_constructor_p)
12287         {
12288           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12289           parse_error_context
12290             (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
12291           TREE_TYPE (node) = error_mark_node;
12292           return error_mark_node;
12293         }
12294       return current_this;
12295
12296     case CLASS_LITERAL:
12297       CAN_COMPLETE_NORMALLY (node) = 1;
12298       node = patch_incomplete_class_ref (node);
12299       if (node == error_mark_node)
12300         return error_mark_node;
12301       break;
12302
12303     default:
12304       CAN_COMPLETE_NORMALLY (node) = 1;
12305       /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12306          and it's time to turn it into the appropriate String object */
12307       if ((nn = patch_string (node)))
12308         node = nn;
12309       else
12310         internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12311     }
12312   return node;
12313 }
12314
12315 /* Complete function call's argument. Return a non zero value is an
12316    error was found.  */
12317
12318 static int
12319 complete_function_arguments (node)
12320      tree node;
12321 {
12322   int flag = 0;
12323   tree cn;
12324
12325   ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12326   for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12327     {
12328       tree wfl = TREE_VALUE (cn), parm, temp;
12329       parm = java_complete_tree (wfl);
12330
12331       if (parm == error_mark_node)
12332         {
12333           flag = 1;
12334           continue;
12335         }
12336       /* If have a string literal that we haven't transformed yet or a
12337          crafted string buffer, as a result of use of the the String
12338          `+' operator. Build `parm.toString()' and expand it. */
12339       if ((temp = patch_string (parm)))
12340         parm = temp;
12341
12342       TREE_VALUE (cn) = parm;
12343     }
12344   ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12345   return flag;
12346 }
12347
12348 /* Sometimes (for loops and variable initialized during their
12349    declaration), we want to wrap a statement around a WFL and turn it
12350    debugable.  */
12351
12352 static tree
12353 build_debugable_stmt (location, stmt)
12354     int location;
12355     tree stmt;
12356 {
12357   if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12358     {
12359       stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12360       EXPR_WFL_LINECOL (stmt) = location;
12361     }
12362   JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12363   return stmt;
12364 }
12365
12366 static tree
12367 build_expr_block (body, decls)
12368      tree body, decls;
12369 {
12370   tree node = make_node (BLOCK);
12371   BLOCK_EXPR_DECLS (node) = decls;
12372   BLOCK_EXPR_BODY (node) = body;
12373   if (body)
12374     TREE_TYPE (node) = TREE_TYPE (body);
12375   TREE_SIDE_EFFECTS (node) = 1;
12376   return node;
12377 }
12378
12379 /* Create a new function block and link it appropriately to current
12380    function block chain */
12381
12382 static tree
12383 enter_block ()
12384 {
12385   tree b = build_expr_block (NULL_TREE, NULL_TREE);
12386
12387   /* Link block B supercontext to the previous block. The current
12388      function DECL is used as supercontext when enter_a_block is called
12389      for the first time for a given function. The current function body
12390      (DECL_FUNCTION_BODY) is set to be block B.  */
12391
12392   tree fndecl = current_function_decl;
12393
12394   if (!fndecl) {
12395     BLOCK_SUPERCONTEXT (b) = current_static_block;
12396     current_static_block = b;
12397   }
12398
12399   else if (!DECL_FUNCTION_BODY (fndecl))
12400     {
12401       BLOCK_SUPERCONTEXT (b) = fndecl;
12402       DECL_FUNCTION_BODY (fndecl) = b;
12403     }
12404   else
12405     {
12406       BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12407       DECL_FUNCTION_BODY (fndecl) = b;
12408     }
12409   return b;
12410 }
12411
12412 /* Exit a block by changing the current function body
12413    (DECL_FUNCTION_BODY) to the current block super context, only if
12414    the block being exited isn't the method's top level one.  */
12415
12416 static tree
12417 exit_block ()
12418 {
12419   tree b;
12420   if (current_function_decl)
12421     {
12422       b = DECL_FUNCTION_BODY (current_function_decl);
12423       if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12424         DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12425     }
12426   else
12427     {
12428       b = current_static_block;
12429
12430       if (BLOCK_SUPERCONTEXT (b))
12431         current_static_block = BLOCK_SUPERCONTEXT (b);
12432     }
12433   return b;
12434 }
12435
12436 /* Lookup for NAME in the nested function's blocks, all the way up to
12437    the current toplevel one. It complies with Java's local variable
12438    scoping rules.  */
12439
12440 static tree
12441 lookup_name_in_blocks (name)
12442      tree name;
12443 {
12444   tree b = GET_CURRENT_BLOCK (current_function_decl);
12445
12446   while (b != current_function_decl)
12447     {
12448       tree current;
12449
12450       /* Paranoid sanity check. To be removed */
12451       if (TREE_CODE (b) != BLOCK)
12452         abort ();
12453
12454       for (current = BLOCK_EXPR_DECLS (b); current;
12455            current = TREE_CHAIN (current))
12456         if (DECL_NAME (current) == name)
12457           return current;
12458       b = BLOCK_SUPERCONTEXT (b);
12459     }
12460   return NULL_TREE;
12461 }
12462
12463 static void
12464 maybe_absorb_scoping_blocks ()
12465 {
12466   while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12467     {
12468       tree b = exit_block ();
12469       java_method_add_stmt (current_function_decl, b);
12470       SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
12471     }
12472 }
12473
12474 \f
12475 /* This section of the source is reserved to build_* functions that
12476    are building incomplete tree nodes and the patch_* functions that
12477    are completing them.  */
12478
12479 /* Wrap a non WFL node around a WFL.  */
12480
12481 static tree
12482 build_wfl_wrap (node, location)
12483     tree node;
12484     int location;
12485 {
12486   tree wfl, node_to_insert = node;
12487
12488   /* We want to process THIS . xxx symbolicaly, to keep it consistent
12489      with the way we're processing SUPER. A THIS from a primary as a
12490      different form than a SUPER. Turn THIS into something symbolic */
12491   if (TREE_CODE (node) == THIS_EXPR)
12492     node_to_insert = wfl = build_wfl_node (this_identifier_node);
12493   else
12494     wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12495
12496   EXPR_WFL_LINECOL (wfl) = location;
12497   EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12498   return wfl;
12499 }
12500
12501 /* Build a super() constructor invocation. Returns empty_stmt_node if
12502    we're currently dealing with the class java.lang.Object. */
12503
12504 static tree
12505 build_super_invocation (mdecl)
12506      tree mdecl;
12507 {
12508   if (DECL_CONTEXT (mdecl) == object_type_node)
12509     return empty_stmt_node;
12510   else
12511     {
12512       tree super_wfl = build_wfl_node (super_identifier_node);
12513       tree a = NULL_TREE, t;
12514       /* If we're dealing with an anonymous class, pass the arguments
12515          of the crafted constructor along. */
12516       if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12517         {
12518           SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12519           for (; t != end_params_node; t = TREE_CHAIN (t))
12520             a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12521         }
12522       return build_method_invocation (super_wfl, a);
12523     }
12524 }
12525
12526 /* Build a SUPER/THIS qualified method invocation.  */
12527
12528 static tree
12529 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
12530      int use_this;
12531      tree name, args;
12532      int lloc, rloc;
12533 {
12534   tree invok;
12535   tree wfl =
12536     build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12537   EXPR_WFL_LINECOL (wfl) = lloc;
12538   invok = build_method_invocation (name, args);
12539   return make_qualified_primary (wfl, invok, rloc);
12540 }
12541
12542 /* Build an incomplete CALL_EXPR node. */
12543
12544 static tree
12545 build_method_invocation (name, args)
12546     tree name;
12547     tree args;
12548 {
12549   tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12550   TREE_SIDE_EFFECTS (call) = 1;
12551   EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12552   return call;
12553 }
12554
12555 /* Build an incomplete new xxx(...) node. */
12556
12557 static tree
12558 build_new_invocation (name, args)
12559     tree name, args;
12560 {
12561   tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12562   TREE_SIDE_EFFECTS (call) = 1;
12563   EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12564   return call;
12565 }
12566
12567 /* Build an incomplete assignment expression. */
12568
12569 static tree
12570 build_assignment (op, op_location, lhs, rhs)
12571      int op, op_location;
12572      tree lhs, rhs;
12573 {
12574   tree assignment;
12575   /* Build the corresponding binop if we deal with a Compound
12576      Assignment operator. Mark the binop sub-tree as part of a
12577      Compound Assignment expression */
12578   if (op != ASSIGN_TK)
12579     {
12580       rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12581       COMPOUND_ASSIGN_P (rhs) = 1;
12582     }
12583   assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12584   TREE_SIDE_EFFECTS (assignment) = 1;
12585   EXPR_WFL_LINECOL (assignment) = op_location;
12586   return assignment;
12587 }
12588
12589 /* Print an INTEGER_CST node as decimal in a static buffer, and return
12590    the buffer.  This is used only for string conversion.  */
12591 static char *
12592 string_convert_int_cst (node)
12593      tree node;
12594 {
12595   static char buffer[80];
12596
12597   unsigned HOST_WIDE_INT lo = TREE_INT_CST_LOW (node);
12598   unsigned HOST_WIDE_INT hi = TREE_INT_CST_HIGH (node);
12599   char *p = buffer + sizeof (buffer) - 1;
12600   int neg = 0;
12601
12602   unsigned HOST_WIDE_INT hibit = (((unsigned HOST_WIDE_INT) 1)
12603                                   << (HOST_BITS_PER_WIDE_INT - 1));
12604
12605   *p-- = '\0';
12606
12607   /* If negative, note the fact and negate the value.  */
12608   if ((hi & hibit))
12609     {
12610       lo = ~lo;
12611       hi = ~hi;
12612       if (++lo == 0)
12613         ++hi;
12614       neg = 1;
12615     }
12616
12617   /* Divide by 10 until there are no bits left.  */
12618   while (hi || lo)
12619     {
12620       unsigned HOST_WIDE_INT acc = 0;
12621       unsigned HOST_WIDE_INT outhi = 0, outlo = 0;
12622       unsigned int i;
12623
12624       /* Use long division to compute the result and the remainder.  */
12625       for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; ++i)
12626         {
12627           /* Shift a bit into accumulator.  */
12628           acc <<= 1;
12629           if ((hi & hibit))
12630             acc |= 1;
12631
12632           /* Shift the value.  */
12633           hi <<= 1;
12634           if ((lo & hibit))
12635             hi |= 1;
12636           lo <<= 1;
12637
12638           /* Shift the correct bit into the result.  */
12639           outhi <<= 1;
12640           if ((outlo & hibit))
12641             outhi |= 1;
12642           outlo <<= 1;
12643           if (acc >= 10)
12644             {
12645               acc -= 10;
12646               outlo |= 1;
12647             }
12648         }
12649
12650       /* FIXME: ASCII assumption.  */
12651       *p-- = '0' + acc;
12652
12653       hi = outhi;
12654       lo = outlo;
12655     }
12656
12657   if (neg)
12658     *p-- = '-';
12659
12660   return p + 1;
12661 }
12662
12663 /* Print an INTEGER_CST node in a static buffer, and return the
12664    buffer.  This is used only for error handling.  */
12665 char *
12666 print_int_node (node)
12667     tree node;
12668 {
12669   static char buffer [80];
12670   if (TREE_CONSTANT_OVERFLOW (node))
12671     sprintf (buffer, "<overflow>");
12672
12673   if (TREE_INT_CST_HIGH (node) == 0)
12674     sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12675              TREE_INT_CST_LOW (node));
12676   else if (TREE_INT_CST_HIGH (node) == -1
12677            && TREE_INT_CST_LOW (node) != 0)
12678     {
12679       buffer [0] = '-';
12680       sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12681                -TREE_INT_CST_LOW (node));
12682     }
12683   else
12684     sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12685              TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12686
12687   return buffer;
12688 }
12689
12690 \f
12691 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12692    context.  */
12693
12694 /* 15.25 Assignment operators. */
12695
12696 static tree
12697 patch_assignment (node, wfl_op1)
12698      tree node;
12699      tree wfl_op1;
12700 {
12701   tree rhs = TREE_OPERAND (node, 1);
12702   tree lvalue = TREE_OPERAND (node, 0), llvalue;
12703   tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12704   int error_found = 0;
12705   int lvalue_from_array = 0;
12706   int is_return = 0;
12707
12708   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12709
12710   /* Lhs can be a named variable */
12711   if (JDECL_P (lvalue))
12712     {
12713       lhs_type = TREE_TYPE (lvalue);
12714     }
12715   /* Or Lhs can be an array access. */
12716   else if (TREE_CODE (lvalue) == ARRAY_REF)
12717     {
12718       lhs_type = TREE_TYPE (lvalue);
12719       lvalue_from_array = 1;
12720     }
12721   /* Or a field access */
12722   else if (TREE_CODE (lvalue) == COMPONENT_REF)
12723     lhs_type = TREE_TYPE (lvalue);
12724   /* Or a function return slot */
12725   else if (TREE_CODE (lvalue) == RESULT_DECL)
12726     {
12727       /* If the return type is an integral type, then we create the
12728          RESULT_DECL with a promoted type, but we need to do these
12729          checks against the unpromoted type to ensure type safety.  So
12730          here we look at the real type, not the type of the decl we
12731          are modifying.  */
12732       lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12733       is_return = 1;
12734     }
12735   /* Otherwise, we might want to try to write into an optimized static
12736      final, this is an of a different nature, reported further on. */
12737   else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12738            && resolve_expression_name (wfl_op1, &llvalue))
12739     {
12740       lhs_type = TREE_TYPE (lvalue);
12741     }
12742   else
12743     {
12744       parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12745       error_found = 1;
12746     }
12747
12748   rhs_type = TREE_TYPE (rhs);
12749
12750   /* 5.1 Try the assignment conversion for builtin type. */
12751   new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12752
12753   /* 5.2 If it failed, try a reference conversion */
12754   if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12755     lhs_type = promote_type (rhs_type);
12756
12757   /* 15.25.2 If we have a compound assignment, convert RHS into the
12758      type of the LHS */
12759   else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12760     new_rhs = convert (lhs_type, rhs);
12761
12762   /* Explicit cast required. This is an error */
12763   if (!new_rhs)
12764     {
12765       char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12766       char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12767       tree wfl;
12768       char operation [32];      /* Max size known */
12769
12770       /* If the assignment is part of a declaration, we use the WFL of
12771          the declared variable to point out the error and call it a
12772          declaration problem. If the assignment is a genuine =
12773          operator, we call is a operator `=' problem, otherwise we
12774          call it an assignment problem. In both of these last cases,
12775          we use the WFL of the operator to indicate the error. */
12776
12777       if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12778         {
12779           wfl = wfl_op1;
12780           strcpy (operation, "declaration");
12781         }
12782       else
12783         {
12784           wfl = wfl_operator;
12785           if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12786             strcpy (operation, "assignment");
12787           else if (is_return)
12788             strcpy (operation, "`return'");
12789           else
12790             strcpy (operation, "`='");
12791         }
12792
12793       if (!valid_cast_to_p (rhs_type, lhs_type))
12794         parse_error_context
12795           (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12796            operation, t1, t2);
12797       else
12798         parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12799                              operation, t1, t2);
12800       free (t1); free (t2);
12801       error_found = 1;
12802     }
12803
12804   if (error_found)
12805     return error_mark_node;
12806
12807   /* If we're processing a `return' statement, promote the actual type
12808      to the promoted type.  */
12809   if (is_return)
12810     new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12811
12812   /* 10.10: Array Store Exception runtime check */
12813   if (!flag_emit_class_files
12814       && !flag_emit_xref
12815       && lvalue_from_array
12816       && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12817     {
12818       tree array, store_check, base, index_expr;
12819
12820       /* Save RHS so that it doesn't get re-evaluated by the store check. */
12821       new_rhs = save_expr (new_rhs);
12822
12823       /* Get the INDIRECT_REF. */
12824       array = TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0);
12825       /* Get the array pointer expr. */
12826       array = TREE_OPERAND (array, 0);
12827       store_check = build_java_arraystore_check (array, new_rhs);
12828
12829       index_expr = TREE_OPERAND (lvalue, 1);
12830
12831       if (TREE_CODE (index_expr) == COMPOUND_EXPR)
12832         {
12833           /* A COMPOUND_EXPR here is a bounds check. The bounds check must
12834              happen before the store check, so prepare to insert the store
12835              check within the second operand of the existing COMPOUND_EXPR. */
12836           base = index_expr;
12837         }
12838       else
12839         base = lvalue;
12840
12841       index_expr = TREE_OPERAND (base, 1);
12842       TREE_OPERAND (base, 1) = build (COMPOUND_EXPR, TREE_TYPE (index_expr),
12843                                       store_check, index_expr);
12844     }
12845
12846   /* Final locals can be used as case values in switch
12847      statement. Prepare them for this eventuality. */
12848   if (TREE_CODE (lvalue) == VAR_DECL
12849       && DECL_FINAL (lvalue)
12850       && TREE_CONSTANT (new_rhs)
12851       && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12852       && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12853       )
12854     {
12855       TREE_CONSTANT (lvalue) = 1;
12856       DECL_INITIAL (lvalue) = new_rhs;
12857     }
12858
12859   TREE_OPERAND (node, 0) = lvalue;
12860   TREE_OPERAND (node, 1) = new_rhs;
12861   TREE_TYPE (node) = lhs_type;
12862   return node;
12863 }
12864
12865 /* Check that type SOURCE can be cast into type DEST. If the cast
12866    can't occur at all, return NULL; otherwise, return a possibly
12867    modified rhs.  */
12868
12869 static tree
12870 try_reference_assignconv (lhs_type, rhs)
12871      tree lhs_type, rhs;
12872 {
12873   tree new_rhs = NULL_TREE;
12874   tree rhs_type = TREE_TYPE (rhs);
12875
12876   if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12877     {
12878       /* `null' may be assigned to any reference type */
12879       if (rhs == null_pointer_node)
12880         new_rhs = null_pointer_node;
12881       /* Try the reference assignment conversion */
12882       else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12883         new_rhs = rhs;
12884       /* This is a magic assignment that we process differently */
12885       else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
12886         new_rhs = rhs;
12887     }
12888   return new_rhs;
12889 }
12890
12891 /* Check that RHS can be converted into LHS_TYPE by the assignment
12892    conversion (5.2), for the cases of RHS being a builtin type. Return
12893    NULL_TREE if the conversion fails or if because RHS isn't of a
12894    builtin type. Return a converted RHS if the conversion is possible.  */
12895
12896 static tree
12897 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12898      tree wfl_op1, lhs_type, rhs;
12899 {
12900   tree new_rhs = NULL_TREE;
12901   tree rhs_type = TREE_TYPE (rhs);
12902
12903   /* Handle boolean specially.  */
12904   if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12905       || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12906     {
12907       if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12908           && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12909         new_rhs = rhs;
12910     }
12911
12912   /* 5.1.1 Try Identity Conversion,
12913      5.1.2 Try Widening Primitive Conversion */
12914   else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12915     new_rhs = convert (lhs_type, rhs);
12916
12917   /* Try a narrowing primitive conversion (5.1.3):
12918        - expression is a constant expression of type int AND
12919        - variable is byte, short or char AND
12920        - The value of the expression is representable in the type of the
12921          variable */
12922   else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12923            && (lhs_type == byte_type_node || lhs_type == char_type_node
12924                || lhs_type == short_type_node))
12925     {
12926       if (int_fits_type_p (rhs, lhs_type))
12927         new_rhs = convert (lhs_type, rhs);
12928       else if (wfl_op1)         /* Might be called with a NULL */
12929         parse_warning_context
12930           (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
12931            print_int_node (rhs), lang_printable_name (lhs_type, 0));
12932       /* Reported a warning that will turn into an error further
12933          down, so we don't return */
12934     }
12935
12936   return new_rhs;
12937 }
12938
12939 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12940    conversion (5.1.1) or widening primitive conversion (5.1.2).  Return
12941    0 is the conversion test fails.  This implements parts the method
12942    invocation convertion (5.3).  */
12943
12944 static int
12945 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12946      tree lhs_type, rhs_type;
12947 {
12948   /* 5.1.1: This is the identity conversion part. */
12949   if (lhs_type == rhs_type)
12950     return 1;
12951
12952   /* Reject non primitive types and boolean conversions.  */
12953   if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
12954     return 0;
12955
12956   /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12957      than a char can't be converted into a char. Short can't too, but
12958      the < test below takes care of that */
12959   if (lhs_type == char_type_node && rhs_type == byte_type_node)
12960     return 0;
12961
12962   /* Accept all promoted type here. Note, we can't use <= in the test
12963      below, because we still need to bounce out assignments of short
12964      to char and the likes */
12965   if (lhs_type == int_type_node
12966       && (rhs_type == promoted_byte_type_node
12967           || rhs_type == promoted_short_type_node
12968           || rhs_type == promoted_char_type_node
12969           || rhs_type == promoted_boolean_type_node))
12970     return 1;
12971
12972   /* From here, an integral is widened if its precision is smaller
12973      than the precision of the LHS or if the LHS is a floating point
12974      type, or the RHS is a float and the RHS a double. */
12975   if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12976        && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12977       || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12978       || (rhs_type == float_type_node && lhs_type == double_type_node))
12979     return 1;
12980
12981   return 0;
12982 }
12983
12984 /* Check that something of SOURCE type can be assigned or cast to
12985    something of DEST type at runtime. Return 1 if the operation is
12986    valid, 0 otherwise. If CAST is set to 1, we're treating the case
12987    were SOURCE is cast into DEST, which borrows a lot of the
12988    assignment check. */
12989
12990 static int
12991 valid_ref_assignconv_cast_p (source, dest, cast)
12992      tree source;
12993      tree dest;
12994      int cast;
12995 {
12996   /* SOURCE or DEST might be null if not from a declared entity. */
12997   if (!source || !dest)
12998     return 0;
12999   if (JNULLP_TYPE_P (source))
13000     return 1;
13001   if (TREE_CODE (source) == POINTER_TYPE)
13002     source = TREE_TYPE (source);
13003   if (TREE_CODE (dest) == POINTER_TYPE)
13004     dest = TREE_TYPE (dest);
13005
13006   /* If source and dest are being compiled from bytecode, they may need to
13007      be loaded. */
13008   if (CLASS_P (source) && !CLASS_LOADED_P (source))
13009     {
13010       load_class (source, 1);
13011       safe_layout_class (source);
13012     }
13013   if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
13014     {
13015       load_class (dest, 1);
13016       safe_layout_class (dest);
13017     }
13018
13019   /* Case where SOURCE is a class type */
13020   if (TYPE_CLASS_P (source))
13021     {
13022       if (TYPE_CLASS_P (dest))
13023         return  (source == dest
13024                  || inherits_from_p (source, dest)
13025                  || (cast && inherits_from_p (dest, source)));
13026       if (TYPE_INTERFACE_P (dest))
13027         {
13028           /* If doing a cast and SOURCE is final, the operation is
13029              always correct a compile time (because even if SOURCE
13030              does not implement DEST, a subclass of SOURCE might). */
13031           if (cast && !CLASS_FINAL (TYPE_NAME (source)))
13032             return 1;
13033           /* Otherwise, SOURCE must implement DEST */
13034           return interface_of_p (dest, source);
13035         }
13036       /* DEST is an array, cast permited if SOURCE is of Object type */
13037       return (cast && source == object_type_node ? 1 : 0);
13038     }
13039   if (TYPE_INTERFACE_P (source))
13040     {
13041       if (TYPE_CLASS_P (dest))
13042         {
13043           /* If not casting, DEST must be the Object type */
13044           if (!cast)
13045             return dest == object_type_node;
13046           /* We're doing a cast. The cast is always valid is class
13047              DEST is not final, otherwise, DEST must implement SOURCE */
13048           else if (!CLASS_FINAL (TYPE_NAME (dest)))
13049             return 1;
13050           else
13051             return interface_of_p (source, dest);
13052         }
13053       if (TYPE_INTERFACE_P (dest))
13054         {
13055           /* If doing a cast, then if SOURCE and DEST contain method
13056              with the same signature but different return type, then
13057              this is a (compile time) error */
13058           if (cast)
13059             {
13060               tree method_source, method_dest;
13061               tree source_type;
13062               tree source_sig;
13063               tree source_name;
13064               for (method_source = TYPE_METHODS (source); method_source;
13065                    method_source = TREE_CHAIN (method_source))
13066                 {
13067                   source_sig =
13068                     build_java_argument_signature (TREE_TYPE (method_source));
13069                   source_type = TREE_TYPE (TREE_TYPE (method_source));
13070                   source_name = DECL_NAME (method_source);
13071                   for (method_dest = TYPE_METHODS (dest);
13072                        method_dest; method_dest = TREE_CHAIN (method_dest))
13073                     if (source_sig ==
13074                         build_java_argument_signature (TREE_TYPE (method_dest))
13075                         && source_name == DECL_NAME (method_dest)
13076                         && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
13077                       return 0;
13078                 }
13079               return 1;
13080             }
13081           else
13082             return source == dest || interface_of_p (dest, source);
13083         }
13084       else
13085         {
13086           /* Array */
13087           return (cast
13088                   && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
13089                       || (DECL_NAME (TYPE_NAME (source))
13090                           == java_io_serializable)));
13091         }
13092     }
13093   if (TYPE_ARRAY_P (source))
13094     {
13095       if (TYPE_CLASS_P (dest))
13096         return dest == object_type_node;
13097       /* Can't cast an array to an interface unless the interface is
13098          java.lang.Cloneable or java.io.Serializable.  */
13099       if (TYPE_INTERFACE_P (dest))
13100         return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
13101                 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
13102       else                      /* Arrays */
13103         {
13104           tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13105           tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13106
13107           /* In case of severe errors, they turn out null */
13108           if (!dest_element_type || !source_element_type)
13109             return 0;
13110           if (source_element_type == dest_element_type)
13111             return 1;
13112           return valid_ref_assignconv_cast_p (source_element_type,
13113                                               dest_element_type, cast);
13114         }
13115       return 0;
13116     }
13117   return 0;
13118 }
13119
13120 static int
13121 valid_cast_to_p (source, dest)
13122      tree source;
13123      tree dest;
13124 {
13125   if (TREE_CODE (source) == POINTER_TYPE)
13126     source = TREE_TYPE (source);
13127   if (TREE_CODE (dest) == POINTER_TYPE)
13128     dest = TREE_TYPE (dest);
13129
13130   if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13131     return valid_ref_assignconv_cast_p (source, dest, 1);
13132
13133   else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13134     return 1;
13135
13136   else if (TREE_CODE (source) == BOOLEAN_TYPE
13137            && TREE_CODE (dest) == BOOLEAN_TYPE)
13138     return 1;
13139
13140   return 0;
13141 }
13142
13143 static tree
13144 do_unary_numeric_promotion (arg)
13145      tree arg;
13146 {
13147   tree type = TREE_TYPE (arg);
13148   if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13149       || TREE_CODE (type) == CHAR_TYPE)
13150     arg = convert (int_type_node, arg);
13151   return arg;
13152 }
13153
13154 /* Return a non zero value if SOURCE can be converted into DEST using
13155    the method invocation conversion rule (5.3).  */
13156 static int
13157 valid_method_invocation_conversion_p (dest, source)
13158      tree dest, source;
13159 {
13160   return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13161            && valid_builtin_assignconv_identity_widening_p (dest, source))
13162           || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13163               && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13164               && valid_ref_assignconv_cast_p (source, dest, 0)));
13165 }
13166
13167 /* Build an incomplete binop expression. */
13168
13169 static tree
13170 build_binop (op, op_location, op1, op2)
13171      enum tree_code op;
13172      int op_location;
13173      tree op1, op2;
13174 {
13175   tree binop = build (op, NULL_TREE, op1, op2);
13176   TREE_SIDE_EFFECTS (binop) = 1;
13177   /* Store the location of the operator, for better error report. The
13178      string of the operator will be rebuild based on the OP value. */
13179   EXPR_WFL_LINECOL (binop) = op_location;
13180   return binop;
13181 }
13182
13183 /* Build the string of the operator retained by NODE. If NODE is part
13184    of a compound expression, add an '=' at the end of the string. This
13185    function is called when an error needs to be reported on an
13186    operator. The string is returned as a pointer to a static character
13187    buffer. */
13188
13189 static char *
13190 operator_string (node)
13191      tree node;
13192 {
13193 #define BUILD_OPERATOR_STRING(S)                                        \
13194   {                                                                     \
13195     sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13196     return buffer;                                                      \
13197   }
13198
13199   static char buffer [10];
13200   switch (TREE_CODE (node))
13201     {
13202     case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13203     case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13204     case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13205     case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13206     case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13207     case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13208     case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13209     case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13210     case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13211     case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13212     case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13213     case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13214     case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13215     case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13216     case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13217     case GT_EXPR: BUILD_OPERATOR_STRING (">");
13218     case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13219     case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13220     case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13221     case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13222     case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13223     case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13224     case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13225     case PREINCREMENT_EXPR:     /* Fall through */
13226     case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13227     case PREDECREMENT_EXPR:     /* Fall through */
13228     case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13229     default:
13230       internal_error ("unregistered operator %s",
13231                       tree_code_name [TREE_CODE (node)]);
13232     }
13233   return NULL;
13234 #undef BUILD_OPERATOR_STRING
13235 }
13236
13237 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2.  */
13238
13239 static int
13240 java_decl_equiv (var_acc1, var_acc2)
13241      tree var_acc1, var_acc2;
13242 {
13243   if (JDECL_P (var_acc1))
13244     return (var_acc1 == var_acc2);
13245
13246   return (TREE_CODE (var_acc1) == COMPONENT_REF
13247           && TREE_CODE (var_acc2) == COMPONENT_REF
13248           && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13249              == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13250           && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13251 }
13252
13253 /* Return a non zero value if CODE is one of the operators that can be
13254    used in conjunction with the `=' operator in a compound assignment.  */
13255
13256 static int
13257 binop_compound_p (code)
13258     enum tree_code code;
13259 {
13260   int i;
13261   for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13262     if (binop_lookup [i] == code)
13263       break;
13264
13265   return i < BINOP_COMPOUND_CANDIDATES;
13266 }
13267
13268 /* Reorganize after a fold to get SAVE_EXPR to generate what we want.  */
13269
13270 static tree
13271 java_refold (t)
13272      tree t;
13273 {
13274   tree c, b, ns, decl;
13275
13276   if (TREE_CODE (t) != MODIFY_EXPR)
13277     return t;
13278
13279   c = TREE_OPERAND (t, 1);
13280   if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13281          && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13282          && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13283     return t;
13284
13285   /* Now the left branch of the binary operator. */
13286   b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13287   if (! (b && TREE_CODE (b) == NOP_EXPR
13288          && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13289     return t;
13290
13291   ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13292   if (! (ns && TREE_CODE (ns) == NOP_EXPR
13293          && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13294     return t;
13295
13296   decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13297   if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13298       /* It's got to be the an equivalent decl */
13299       && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13300     {
13301       /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13302       TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13303       /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13304       TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13305       /* Change the right part of the BINOP_EXPR */
13306       TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13307     }
13308
13309   return t;
13310 }
13311
13312 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13313    errors but we modify NODE so that it contains the type computed
13314    according to the expression, when it's fixed. Otherwise, we write
13315    error_mark_node as the type. It allows us to further the analysis
13316    of remaining nodes and detects more errors in certain cases.  */
13317
13318 static tree
13319 patch_binop (node, wfl_op1, wfl_op2)
13320      tree node;
13321      tree wfl_op1;
13322      tree wfl_op2;
13323 {
13324   tree op1 = TREE_OPERAND (node, 0);
13325   tree op2 = TREE_OPERAND (node, 1);
13326   tree op1_type = TREE_TYPE (op1);
13327   tree op2_type = TREE_TYPE (op2);
13328   tree prom_type = NULL_TREE, cn;
13329   enum tree_code code = TREE_CODE (node);
13330
13331   /* If 1, tell the routine that we have to return error_mark_node
13332      after checking for the initialization of the RHS */
13333   int error_found = 0;
13334
13335   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13336
13337   /* If either op<n>_type are NULL, this might be early signs of an
13338      error situation, unless it's too early to tell (in case we're
13339      handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13340      correctly so the error can be later on reported accurately. */
13341   if (! (code == PLUS_EXPR || code == NE_EXPR
13342          || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13343     {
13344       tree n;
13345       if (! op1_type)
13346         {
13347           n = java_complete_tree (op1);
13348           op1_type = TREE_TYPE (n);
13349         }
13350       if (! op2_type)
13351         {
13352           n = java_complete_tree (op2);
13353           op2_type = TREE_TYPE (n);
13354         }
13355     }
13356
13357   switch (code)
13358     {
13359     /* 15.16 Multiplicative operators */
13360     case MULT_EXPR:             /* 15.16.1 Multiplication Operator * */
13361     case RDIV_EXPR:             /* 15.16.2 Division Operator / */
13362     case TRUNC_DIV_EXPR:        /* 15.16.2 Integral type Division Operator / */
13363     case TRUNC_MOD_EXPR:        /* 15.16.3 Remainder operator % */
13364       if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13365         {
13366           if (!JNUMERIC_TYPE_P (op1_type))
13367             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13368           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13369             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13370           TREE_TYPE (node) = error_mark_node;
13371           error_found = 1;
13372           break;
13373         }
13374       prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13375
13376       /* Detect integral division by zero */
13377       if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13378           && TREE_CODE (prom_type) == INTEGER_TYPE
13379           && (op2 == integer_zero_node || op2 == long_zero_node ||
13380               (TREE_CODE (op2) == INTEGER_CST &&
13381                ! TREE_INT_CST_LOW (op2)  && ! TREE_INT_CST_HIGH (op2))))
13382         {
13383           parse_warning_context (wfl_operator, "Evaluating this expression will result in an arithmetic exception being thrown");
13384           TREE_CONSTANT (node) = 0;
13385         }
13386
13387       /* Change the division operator if necessary */
13388       if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13389         TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13390
13391       /* Before divisions as is disapear, try to simplify and bail if
13392          applicable, otherwise we won't perform even simple
13393          simplifications like (1-1)/3. We can't do that with floating
13394          point number, folds can't handle them at this stage. */
13395       if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13396           && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13397         {
13398           TREE_TYPE (node) = prom_type;
13399           node = fold (node);
13400           if (TREE_CODE (node) != code)
13401             return node;
13402         }
13403
13404       if (TREE_CODE (prom_type) == INTEGER_TYPE
13405           && flag_use_divide_subroutine
13406           && ! flag_emit_class_files
13407           && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13408         return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13409
13410       /* This one is more complicated. FLOATs are processed by a
13411          function call to soft_fmod. Duplicate the value of the
13412          COMPOUND_ASSIGN_P flag. */
13413       if (code == TRUNC_MOD_EXPR)
13414         {
13415           tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13416           COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13417           TREE_SIDE_EFFECTS (mod)
13418             = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13419           return mod;
13420         }
13421       break;
13422
13423     /* 15.17 Additive Operators */
13424     case PLUS_EXPR:             /* 15.17.1 String Concatenation Operator + */
13425
13426       /* Operation is valid if either one argument is a string
13427          constant, a String object or a StringBuffer crafted for the
13428          purpose of the a previous usage of the String concatenation
13429          operator */
13430
13431       if (TREE_CODE (op1) == STRING_CST
13432           || TREE_CODE (op2) == STRING_CST
13433           || JSTRING_TYPE_P (op1_type)
13434           || JSTRING_TYPE_P (op2_type)
13435           || IS_CRAFTED_STRING_BUFFER_P (op1)
13436           || IS_CRAFTED_STRING_BUFFER_P (op2))
13437         return build_string_concatenation (op1, op2);
13438
13439     case MINUS_EXPR:            /* 15.17.2 Additive Operators (+ and -) for
13440                                    Numeric Types */
13441       if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13442         {
13443           if (!JNUMERIC_TYPE_P (op1_type))
13444             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13445           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13446             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13447           TREE_TYPE (node) = error_mark_node;
13448           error_found = 1;
13449           break;
13450         }
13451       prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13452       break;
13453
13454     /* 15.18 Shift Operators */
13455     case LSHIFT_EXPR:
13456     case RSHIFT_EXPR:
13457     case URSHIFT_EXPR:
13458       if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13459         {
13460           if (!JINTEGRAL_TYPE_P (op1_type))
13461             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13462           else
13463             {
13464               if (JNUMERIC_TYPE_P (op2_type))
13465                 parse_error_context (wfl_operator,
13466                                      "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
13467                                      operator_string (node),
13468                                      lang_printable_name (op2_type, 0));
13469               else
13470                 parse_error_context (wfl_operator,
13471                                      "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
13472                                      operator_string (node),
13473                                      lang_printable_name (op2_type, 0));
13474             }
13475           TREE_TYPE (node) = error_mark_node;
13476           error_found = 1;
13477           break;
13478         }
13479
13480       /* Unary numeric promotion (5.6.1) is performed on each operand
13481          separately */
13482       op1 = do_unary_numeric_promotion (op1);
13483       op2 = do_unary_numeric_promotion (op2);
13484
13485       /* The type of the shift expression is the type of the promoted
13486          type of the left-hand operand */
13487       prom_type = TREE_TYPE (op1);
13488
13489       /* Shift int only up to 0x1f and long up to 0x3f */
13490       if (prom_type == int_type_node)
13491         op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13492                            build_int_2 (0x1f, 0)));
13493       else
13494         op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13495                            build_int_2 (0x3f, 0)));
13496
13497       /* The >>> operator is a >> operating on unsigned quantities */
13498       if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13499         {
13500           tree to_return;
13501           tree utype = java_unsigned_type (prom_type);
13502           op1 = convert (utype, op1);
13503           TREE_SET_CODE (node, RSHIFT_EXPR);
13504           TREE_OPERAND (node, 0) = op1;
13505           TREE_OPERAND (node, 1) = op2;
13506           TREE_TYPE (node) = utype;
13507           to_return = convert (prom_type, node);
13508           /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13509           COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13510           TREE_SIDE_EFFECTS (to_return)
13511             = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13512           return to_return;
13513         }
13514       break;
13515
13516       /* 15.19.1 Type Comparison Operator instaceof */
13517     case INSTANCEOF_EXPR:
13518
13519       TREE_TYPE (node) = boolean_type_node;
13520
13521       /* OP1_TYPE might be NULL when OP1 is a string constant.  */
13522       if ((cn = patch_string (op1)))
13523         {
13524           op1 = cn;
13525           op1_type = TREE_TYPE (op1);
13526         }
13527       if (op1_type == NULL_TREE)
13528         abort ();
13529
13530       if (!(op2_type = resolve_type_during_patch (op2)))
13531         return error_mark_node;
13532
13533       /* The first operand must be a reference type or the null type */
13534       if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13535         error_found = 1;        /* Error reported further below */
13536
13537       /* The second operand must be a reference type */
13538       if (!JREFERENCE_TYPE_P (op2_type))
13539         {
13540           SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13541           parse_error_context
13542             (wfl_operator, "Invalid argument `%s' for `instanceof'",
13543              lang_printable_name (op2_type, 0));
13544           error_found = 1;
13545         }
13546
13547       if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13548         {
13549           /* If the first operand is null, the result is always false */
13550           if (op1 == null_pointer_node)
13551             return boolean_false_node;
13552           else if (flag_emit_class_files)
13553             {
13554               TREE_OPERAND (node, 1) = op2_type;
13555               TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13556               return node;
13557             }
13558           /* Otherwise we have to invoke instance of to figure it out */
13559           else
13560             return build_instanceof (op1, op2_type);
13561         }
13562       /* There is no way the expression operand can be an instance of
13563          the type operand. This is a compile time error. */
13564       else
13565         {
13566           char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13567           SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13568           parse_error_context
13569             (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13570              t1, lang_printable_name (op2_type, 0));
13571           free (t1);
13572           error_found = 1;
13573         }
13574
13575       break;
13576
13577       /* 15.21 Bitwise and Logical Operators */
13578     case BIT_AND_EXPR:
13579     case BIT_XOR_EXPR:
13580     case BIT_IOR_EXPR:
13581       if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13582         /* Binary numeric promotion is performed on both operand and the
13583            expression retain that type */
13584         prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13585
13586       else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13587                && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13588         /* The type of the bitwise operator expression is BOOLEAN */
13589         prom_type = boolean_type_node;
13590       else
13591         {
13592           if (!JINTEGRAL_TYPE_P (op1_type))
13593             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13594           if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13595             ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13596           TREE_TYPE (node) = error_mark_node;
13597           error_found = 1;
13598           /* Insert a break here if adding thing before the switch's
13599              break for this case */
13600         }
13601       break;
13602
13603       /* 15.22 Conditional-And Operator */
13604     case TRUTH_ANDIF_EXPR:
13605       /* 15.23 Conditional-Or Operator */
13606     case TRUTH_ORIF_EXPR:
13607       /* Operands must be of BOOLEAN type */
13608       if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13609           TREE_CODE (op2_type) != BOOLEAN_TYPE)
13610         {
13611           if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13612             ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13613           if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13614             ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13615           TREE_TYPE (node) = boolean_type_node;
13616           error_found = 1;
13617           break;
13618         }
13619       else if (integer_zerop (op1))
13620         {
13621           return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13622         }
13623       else if (integer_onep (op1))
13624         {
13625           return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13626         }
13627       /* The type of the conditional operators is BOOLEAN */
13628       prom_type = boolean_type_node;
13629       break;
13630
13631       /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13632     case LT_EXPR:
13633     case GT_EXPR:
13634     case LE_EXPR:
13635     case GE_EXPR:
13636       /* The type of each of the operands must be a primitive numeric
13637          type */
13638       if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13639         {
13640           if (!JNUMERIC_TYPE_P (op1_type))
13641             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13642           if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13643             ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13644           TREE_TYPE (node) = boolean_type_node;
13645           error_found = 1;
13646           break;
13647         }
13648       /* Binary numeric promotion is performed on the operands */
13649       binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13650       /* The type of the relation expression is always BOOLEAN */
13651       prom_type = boolean_type_node;
13652       break;
13653
13654       /* 15.20 Equality Operator */
13655     case EQ_EXPR:
13656     case NE_EXPR:
13657       /* It's time for us to patch the strings. */
13658       if ((cn = patch_string (op1)))
13659        {
13660          op1 = cn;
13661          op1_type = TREE_TYPE (op1);
13662        }
13663       if ((cn = patch_string (op2)))
13664        {
13665          op2 = cn;
13666          op2_type = TREE_TYPE (op2);
13667        }
13668
13669       /* 15.20.1 Numerical Equality Operators == and != */
13670       /* Binary numeric promotion is performed on the operands */
13671       if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13672         binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13673
13674       /* 15.20.2 Boolean Equality Operators == and != */
13675       else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13676           TREE_CODE (op2_type) == BOOLEAN_TYPE)
13677         ;                       /* Nothing to do here */
13678
13679       /* 15.20.3 Reference Equality Operators == and != */
13680       /* Types have to be either references or the null type. If
13681          they're references, it must be possible to convert either
13682          type to the other by casting conversion. */
13683       else if (op1 == null_pointer_node || op2 == null_pointer_node
13684                || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13685                    && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13686                        || valid_ref_assignconv_cast_p (op2_type,
13687                                                        op1_type, 1))))
13688         ;                       /* Nothing to do here */
13689
13690       /* Else we have an error figure what can't be converted into
13691          what and report the error */
13692       else
13693         {
13694           char *t1;
13695           t1 = xstrdup (lang_printable_name (op1_type, 0));
13696           parse_error_context
13697             (wfl_operator,
13698              "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13699              operator_string (node), t1,
13700              lang_printable_name (op2_type, 0));
13701           free (t1);
13702           TREE_TYPE (node) = boolean_type_node;
13703           error_found = 1;
13704           break;
13705         }
13706       prom_type = boolean_type_node;
13707       break;
13708     default:
13709       abort ();
13710     }
13711
13712   if (error_found)
13713     return error_mark_node;
13714
13715   TREE_OPERAND (node, 0) = op1;
13716   TREE_OPERAND (node, 1) = op2;
13717   TREE_TYPE (node) = prom_type;
13718   TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13719
13720   if (flag_emit_xref)
13721     return node;
13722
13723   /* fold does not respect side-effect order as required for Java but not C.
13724    * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13725    * bytecode.
13726    */
13727   if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13728       : ! TREE_SIDE_EFFECTS (node))
13729     node = fold (node);
13730   return node;
13731 }
13732
13733 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13734    zero value, the value of CSTE comes after the valude of STRING */
13735
13736 static tree
13737 do_merge_string_cste (cste, string, string_len, after)
13738      tree cste;
13739      const char *string;
13740      int string_len, after;
13741 {
13742   const char *old = TREE_STRING_POINTER (cste);
13743   int old_len = TREE_STRING_LENGTH (cste);
13744   int len = old_len + string_len;
13745   char *new = alloca (len+1);
13746
13747   if (after)
13748     {
13749       memcpy (new, string, string_len);
13750       memcpy (&new [string_len], old, old_len);
13751     }
13752   else
13753     {
13754       memcpy (new, old, old_len);
13755       memcpy (&new [old_len], string, string_len);
13756     }
13757   new [len] = '\0';
13758   return build_string (len, new);
13759 }
13760
13761 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13762    new STRING_CST on success, NULL_TREE on failure */
13763
13764 static tree
13765 merge_string_cste (op1, op2, after)
13766      tree op1, op2;
13767      int after;
13768 {
13769   /* Handle two string constants right away */
13770   if (TREE_CODE (op2) == STRING_CST)
13771     return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13772                                  TREE_STRING_LENGTH (op2), after);
13773
13774   /* Reasonable integer constant can be treated right away */
13775   if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13776     {
13777       static const char *const boolean_true = "true";
13778       static const char *const boolean_false = "false";
13779       static const char *const null_pointer = "null";
13780       char ch[3];
13781       const char *string;
13782
13783       if (op2 == boolean_true_node)
13784         string = boolean_true;
13785       else if (op2 == boolean_false_node)
13786         string = boolean_false;
13787       else if (op2 == null_pointer_node)
13788         string = null_pointer;
13789       else if (TREE_TYPE (op2) == char_type_node)
13790         {
13791           ch[0] = (char )TREE_INT_CST_LOW (op2);
13792           ch[1] = '\0';
13793           string = ch;
13794         }
13795       else
13796         string = string_convert_int_cst (op2);
13797
13798       return do_merge_string_cste (op1, string, strlen (string), after);
13799     }
13800   return NULL_TREE;
13801 }
13802
13803 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13804    has to be a STRING_CST and the other part must be a STRING_CST or a
13805    INTEGRAL constant. Return a new STRING_CST if the operation
13806    succeed, NULL_TREE otherwise.
13807
13808    If the case we want to optimize for space, we might want to return
13809    NULL_TREE for each invocation of this routine. FIXME */
13810
13811 static tree
13812 string_constant_concatenation (op1, op2)
13813      tree op1, op2;
13814 {
13815   if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13816     {
13817       tree string, rest;
13818       int invert;
13819
13820       string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13821       rest   = (string == op1 ? op2 : op1);
13822       invert = (string == op1 ? 0 : 1 );
13823
13824       /* Walk REST, only if it looks reasonable */
13825       if (TREE_CODE (rest) != STRING_CST
13826           && !IS_CRAFTED_STRING_BUFFER_P (rest)
13827           && !JSTRING_TYPE_P (TREE_TYPE (rest))
13828           && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13829         {
13830           rest = java_complete_tree (rest);
13831           if (rest == error_mark_node)
13832             return error_mark_node;
13833           rest = fold (rest);
13834         }
13835       return merge_string_cste (string, rest, invert);
13836     }
13837   return NULL_TREE;
13838 }
13839
13840 /* Implement the `+' operator. Does static optimization if possible,
13841    otherwise create (if necessary) and append elements to a
13842    StringBuffer. The StringBuffer will be carried around until it is
13843    used for a function call or an assignment. Then toString() will be
13844    called on it to turn it into a String object. */
13845
13846 static tree
13847 build_string_concatenation (op1, op2)
13848      tree op1, op2;
13849 {
13850   tree result;
13851   int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13852
13853   if (flag_emit_xref)
13854     return build (PLUS_EXPR, string_type_node, op1, op2);
13855
13856   /* Try to do some static optimization */
13857   if ((result = string_constant_concatenation (op1, op2)))
13858     return result;
13859
13860   /* Discard empty strings on either side of the expression */
13861   if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13862     {
13863       op1 = op2;
13864       op2 = NULL_TREE;
13865     }
13866   else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13867     op2 = NULL_TREE;
13868
13869   /* If operands are string constant, turn then into object references */
13870   if (TREE_CODE (op1) == STRING_CST)
13871     op1 = patch_string_cst (op1);
13872   if (op2 && TREE_CODE (op2) == STRING_CST)
13873     op2 = patch_string_cst (op2);
13874
13875   /* If either one of the constant is null and the other non null
13876      operand is a String constant, return it. */
13877   if ((TREE_CODE (op1) == STRING_CST) && !op2)
13878     return op1;
13879
13880   /* If OP1 isn't already a StringBuffer, create and
13881      initialize a new one */
13882   if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13883     {
13884       /* Two solutions here:
13885          1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13886          2) OP1 is something else, we call new StringBuffer().append(OP1).  */
13887       if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13888         op1 = BUILD_STRING_BUFFER (op1);
13889       else
13890         {
13891           tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13892           op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13893         }
13894     }
13895
13896   if (op2)
13897     {
13898       /* OP1 is no longer the last node holding a crafted StringBuffer */
13899       IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13900       /* Create a node for `{new...,xxx}.append (op2)' */
13901       if (op2)
13902         op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13903     }
13904
13905   /* Mark the last node holding a crafted StringBuffer */
13906   IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13907
13908   TREE_SIDE_EFFECTS (op1) = side_effects;
13909   return op1;
13910 }
13911
13912 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13913    StringBuffer. If no string were found to be patched, return
13914    NULL. */
13915
13916 static tree
13917 patch_string (node)
13918     tree node;
13919 {
13920   if (node == error_mark_node)
13921     return error_mark_node;
13922   if (TREE_CODE (node) == STRING_CST)
13923     return patch_string_cst (node);
13924   else if (IS_CRAFTED_STRING_BUFFER_P (node))
13925     {
13926       int saved = ctxp->explicit_constructor_p;
13927       tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13928       tree ret;
13929       /* Temporary disable forbid the use of `this'. */
13930       ctxp->explicit_constructor_p = 0;
13931       ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13932       /* String concatenation arguments must be evaluated in order too. */
13933       ret = force_evaluation_order (ret);
13934       /* Restore it at its previous value */
13935       ctxp->explicit_constructor_p = saved;
13936       return ret;
13937     }
13938   return NULL_TREE;
13939 }
13940
13941 /* Build the internal representation of a string constant.  */
13942
13943 static tree
13944 patch_string_cst (node)
13945      tree node;
13946 {
13947   int location;
13948   if (! flag_emit_class_files)
13949     {
13950       node = get_identifier (TREE_STRING_POINTER (node));
13951       location = alloc_name_constant (CONSTANT_String, node);
13952       node = build_ref_from_constant_pool (location);
13953     }
13954   TREE_TYPE (node) = string_ptr_type_node;
13955   TREE_CONSTANT (node) = 1;
13956   return node;
13957 }
13958
13959 /* Build an incomplete unary operator expression. */
13960
13961 static tree
13962 build_unaryop (op_token, op_location, op1)
13963      int op_token, op_location;
13964      tree op1;
13965 {
13966   enum tree_code op;
13967   tree unaryop;
13968   switch (op_token)
13969     {
13970     case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13971     case MINUS_TK: op = NEGATE_EXPR; break;
13972     case NEG_TK: op = TRUTH_NOT_EXPR; break;
13973     case NOT_TK: op = BIT_NOT_EXPR; break;
13974     default: abort ();
13975     }
13976
13977   unaryop = build1 (op, NULL_TREE, op1);
13978   TREE_SIDE_EFFECTS (unaryop) = 1;
13979   /* Store the location of the operator, for better error report. The
13980      string of the operator will be rebuild based on the OP value. */
13981   EXPR_WFL_LINECOL (unaryop) = op_location;
13982   return unaryop;
13983 }
13984
13985 /* Special case for the ++/-- operators, since they require an extra
13986    argument to build, which is set to NULL and patched
13987    later. IS_POST_P is 1 if the operator, 0 otherwise.  */
13988
13989 static tree
13990 build_incdec (op_token, op_location, op1, is_post_p)
13991      int op_token, op_location;
13992      tree op1;
13993      int is_post_p;
13994 {
13995   static const enum tree_code lookup [2][2] =
13996     {
13997       { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13998       { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13999     };
14000   tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
14001                      NULL_TREE, op1, NULL_TREE);
14002   TREE_SIDE_EFFECTS (node) = 1;
14003   /* Store the location of the operator, for better error report. The
14004      string of the operator will be rebuild based on the OP value. */
14005   EXPR_WFL_LINECOL (node) = op_location;
14006   return node;
14007 }
14008
14009 /* Build an incomplete cast operator, based on the use of the
14010    CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
14011    set. java_complete_tree is trained to walk a CONVERT_EXPR even
14012    though its type is already set.  */
14013
14014 static tree
14015 build_cast (location, type, exp)
14016      int location;
14017      tree type, exp;
14018 {
14019   tree node = build1 (CONVERT_EXPR, type, exp);
14020   EXPR_WFL_LINECOL (node) = location;
14021   return node;
14022 }
14023
14024 /* Build an incomplete class reference operator.  */
14025 static tree
14026 build_incomplete_class_ref (location, class_name)
14027     int location;
14028     tree class_name;
14029 {
14030   tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
14031   EXPR_WFL_LINECOL (node) = location;
14032   return node;
14033 }
14034
14035 /* Complete an incomplete class reference operator.  */
14036 static tree
14037 patch_incomplete_class_ref (node)
14038     tree node;
14039 {
14040   tree type = TREE_OPERAND (node, 0);
14041   tree ref_type;
14042
14043   if (!(ref_type = resolve_type_during_patch (type)))
14044     return error_mark_node;
14045
14046   if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type)
14047       || TREE_CODE (ref_type) == VOID_TYPE)
14048     {
14049       tree dot = build_class_ref (ref_type);
14050       /* A class referenced by `foo.class' is initialized.  */
14051       if (!flag_emit_class_files)
14052        dot = build_class_init (ref_type, dot);
14053       return java_complete_tree (dot);
14054     }
14055
14056   /* If we're emitting class files and we have to deal with non
14057      primitive types, we invoke (and consider generating) the
14058      synthetic static method `class$'. */
14059   if (!TYPE_DOT_CLASS (current_class))
14060       build_dot_class_method (current_class);
14061   ref_type = build_dot_class_method_invocation (ref_type);
14062   return java_complete_tree (ref_type);
14063 }
14064
14065 /* 15.14 Unary operators. We return error_mark_node in case of error,
14066    but preserve the type of NODE if the type is fixed.  */
14067
14068 static tree
14069 patch_unaryop (node, wfl_op)
14070      tree node;
14071      tree wfl_op;
14072 {
14073   tree op = TREE_OPERAND (node, 0);
14074   tree op_type = TREE_TYPE (op);
14075   tree prom_type = NULL_TREE, value, decl;
14076   int outer_field_flag = 0;
14077   int code = TREE_CODE (node);
14078   int error_found = 0;
14079
14080   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14081
14082   switch (code)
14083     {
14084       /* 15.13.2 Postfix Increment Operator ++ */
14085     case POSTINCREMENT_EXPR:
14086       /* 15.13.3 Postfix Increment Operator -- */
14087     case POSTDECREMENT_EXPR:
14088       /* 15.14.1 Prefix Increment Operator ++ */
14089     case PREINCREMENT_EXPR:
14090       /* 15.14.2 Prefix Decrement Operator -- */
14091     case PREDECREMENT_EXPR:
14092       op = decl = strip_out_static_field_access_decl (op);
14093       outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
14094       /* We might be trying to change an outer field accessed using
14095          access method. */
14096       if (outer_field_flag)
14097         {
14098           /* Retrieve the decl of the field we're trying to access. We
14099              do that by first retrieving the function we would call to
14100              access the field. It has been already verified that this
14101              field isn't final */
14102           if (flag_emit_class_files)
14103             decl = TREE_OPERAND (op, 0);
14104           else
14105             decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
14106           decl = DECL_FUNCTION_ACCESS_DECL (decl);
14107         }
14108       /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14109       else if (!JDECL_P (decl)
14110           && TREE_CODE (decl) != COMPONENT_REF
14111           && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14112           && TREE_CODE (decl) != INDIRECT_REF
14113           && !(TREE_CODE (decl) == COMPOUND_EXPR
14114                && TREE_OPERAND (decl, 1)
14115                && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14116         {
14117           TREE_TYPE (node) = error_mark_node;
14118           error_found = 1;
14119         }
14120
14121       /* From now on, we know that op if a variable and that it has a
14122          valid wfl. We use wfl_op to locate errors related to the
14123          ++/-- operand. */
14124       if (!JNUMERIC_TYPE_P (op_type))
14125         {
14126           parse_error_context
14127             (wfl_op, "Invalid argument type `%s' to `%s'",
14128              lang_printable_name (op_type, 0), operator_string (node));
14129           TREE_TYPE (node) = error_mark_node;
14130           error_found = 1;
14131         }
14132       else
14133         {
14134           /* Before the addition, binary numeric promotion is performed on
14135              both operands, if really necessary */
14136           if (JINTEGRAL_TYPE_P (op_type))
14137             {
14138               value = build_int_2 (1, 0);
14139               TREE_TYPE (value) = TREE_TYPE (node) = op_type;
14140             }
14141           else
14142             {
14143               value = build_int_2 (1, 0);
14144               TREE_TYPE (node) =
14145                 binary_numeric_promotion (op_type,
14146                                           TREE_TYPE (value), &op, &value);
14147             }
14148
14149           /* We remember we might be accessing an outer field */
14150           if (outer_field_flag)
14151             {
14152               /* We re-generate an access to the field */
14153               value = build (PLUS_EXPR, TREE_TYPE (op),
14154                              build_outer_field_access (wfl_op, decl), value);
14155
14156               /* And we patch the original access$() into a write
14157                  with plus_op as a rhs */
14158               return outer_field_access_fix (node, op, value);
14159             }
14160
14161           /* And write back into the node. */
14162           TREE_OPERAND (node, 0) = op;
14163           TREE_OPERAND (node, 1) = value;
14164           /* Convert the overall back into its original type, if
14165              necessary, and return */
14166           if (JINTEGRAL_TYPE_P (op_type))
14167             return fold (node);
14168           else
14169             return fold (convert (op_type, node));
14170         }
14171       break;
14172
14173       /* 15.14.3 Unary Plus Operator + */
14174     case UNARY_PLUS_EXPR:
14175       /* 15.14.4 Unary Minus Operator - */
14176     case NEGATE_EXPR:
14177       if (!JNUMERIC_TYPE_P (op_type))
14178         {
14179           ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14180           TREE_TYPE (node) = error_mark_node;
14181           error_found = 1;
14182         }
14183       /* Unary numeric promotion is performed on operand */
14184       else
14185         {
14186           op = do_unary_numeric_promotion (op);
14187           prom_type = TREE_TYPE (op);
14188           if (code == UNARY_PLUS_EXPR)
14189             return fold (op);
14190         }
14191       break;
14192
14193       /* 15.14.5 Bitwise Complement Operator ~ */
14194     case BIT_NOT_EXPR:
14195       if (!JINTEGRAL_TYPE_P (op_type))
14196         {
14197           ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14198           TREE_TYPE (node) = error_mark_node;
14199           error_found = 1;
14200         }
14201       else
14202         {
14203           op = do_unary_numeric_promotion (op);
14204           prom_type = TREE_TYPE (op);
14205         }
14206       break;
14207
14208       /* 15.14.6 Logical Complement Operator ! */
14209     case TRUTH_NOT_EXPR:
14210       if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14211         {
14212           ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14213           /* But the type is known. We will report an error if further
14214              attempt of a assignment is made with this rhs */
14215           TREE_TYPE (node) = boolean_type_node;
14216           error_found = 1;
14217         }
14218       else
14219         prom_type = boolean_type_node;
14220       break;
14221
14222       /* 15.15 Cast Expression */
14223     case CONVERT_EXPR:
14224       value = patch_cast (node, wfl_operator);
14225       if (value == error_mark_node)
14226         {
14227           /* If this cast is part of an assignment, we tell the code
14228              that deals with it not to complain about a mismatch,
14229              because things have been cast, anyways */
14230           TREE_TYPE (node) = error_mark_node;
14231           error_found = 1;
14232         }
14233       else
14234         {
14235           value = fold (value);
14236           TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
14237           return value;
14238         }
14239       break;
14240     }
14241
14242   if (error_found)
14243     return error_mark_node;
14244
14245   /* There are cases where node has been replaced by something else
14246      and we don't end up returning here: UNARY_PLUS_EXPR,
14247      CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14248   TREE_OPERAND (node, 0) = fold (op);
14249   TREE_TYPE (node) = prom_type;
14250   TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14251   return fold (node);
14252 }
14253
14254 /* Generic type resolution that sometimes takes place during node
14255    patching. Returned the resolved type or generate an error
14256    message. Return the resolved type or NULL_TREE.  */
14257
14258 static tree
14259 resolve_type_during_patch (type)
14260      tree type;
14261 {
14262   if (unresolved_type_p (type, NULL))
14263     {
14264       tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14265       if (!type_decl)
14266         {
14267           parse_error_context (type,
14268                                "Class `%s' not found in type declaration",
14269                                IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14270           return NULL_TREE;
14271         }
14272       return TREE_TYPE (type_decl);
14273     }
14274   return type;
14275 }
14276 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14277    found. Otherwise NODE or something meant to replace it is returned.  */
14278
14279 static tree
14280 patch_cast (node, wfl_op)
14281      tree node;
14282      tree wfl_op;
14283 {
14284   tree op = TREE_OPERAND (node, 0);
14285   tree cast_type = TREE_TYPE (node);
14286   tree patched, op_type;
14287   char *t1;
14288
14289   /* Some string patching might be necessary at this stage */
14290   if ((patched = patch_string (op)))
14291     TREE_OPERAND (node, 0) = op = patched;
14292   op_type = TREE_TYPE (op);
14293
14294   /* First resolve OP_TYPE if unresolved */
14295   if (!(cast_type = resolve_type_during_patch (cast_type)))
14296     return error_mark_node;
14297
14298   /* Check on cast that are proven correct at compile time */
14299   if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14300     {
14301       /* Same type */
14302       if (cast_type == op_type)
14303         return node;
14304
14305       /* float and double type are converted to the original type main
14306          variant and then to the target type. */
14307       if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
14308         op = convert (integer_type_node, op);
14309
14310       /* Try widening/narowwing convertion. Potentially, things need
14311          to be worked out in gcc so we implement the extreme cases
14312          correctly. fold_convert() needs to be fixed. */
14313       return convert (cast_type, op);
14314     }
14315
14316   /* It's also valid to cast a boolean into a boolean */
14317   if (op_type == boolean_type_node && cast_type == boolean_type_node)
14318     return node;
14319
14320   /* null can be casted to references */
14321   if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14322     return build_null_of_type (cast_type);
14323
14324   /* The remaining legal casts involve conversion between reference
14325      types. Check for their compile time correctness. */
14326   if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14327       && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14328     {
14329       TREE_TYPE (node) = promote_type (cast_type);
14330       /* Now, the case can be determined correct at compile time if
14331          OP_TYPE can be converted into CAST_TYPE by assignment
14332          conversion (5.2) */
14333
14334       if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14335         {
14336           TREE_SET_CODE (node, NOP_EXPR);
14337           return node;
14338         }
14339
14340       if (flag_emit_class_files)
14341         {
14342           TREE_SET_CODE (node, CONVERT_EXPR);
14343           return node;
14344         }
14345
14346       /* The cast requires a run-time check */
14347       return build (CALL_EXPR, promote_type (cast_type),
14348                     build_address_of (soft_checkcast_node),
14349                     tree_cons (NULL_TREE, build_class_ref (cast_type),
14350                                build_tree_list (NULL_TREE, op)),
14351                     NULL_TREE);
14352     }
14353
14354   /* Any other casts are proven incorrect at compile time */
14355   t1 = xstrdup (lang_printable_name (op_type, 0));
14356   parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
14357                        t1, lang_printable_name (cast_type, 0));
14358   free (t1);
14359   return error_mark_node;
14360 }
14361
14362 /* Build a null constant and give it the type TYPE.  */
14363
14364 static tree
14365 build_null_of_type (type)
14366      tree type;
14367 {
14368   tree node = build_int_2 (0, 0);
14369   TREE_TYPE (node) = promote_type (type);
14370   return node;
14371 }
14372
14373 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14374    a list of indices. */
14375 static tree
14376 build_array_ref (location, array, index)
14377      int location;
14378      tree array, index;
14379 {
14380   tree node = build (ARRAY_REF, NULL_TREE, array, index);
14381   EXPR_WFL_LINECOL (node) = location;
14382   return node;
14383 }
14384
14385 /* 15.12 Array Access Expression */
14386
14387 static tree
14388 patch_array_ref (node)
14389      tree node;
14390 {
14391   tree array = TREE_OPERAND (node, 0);
14392   tree array_type  = TREE_TYPE (array);
14393   tree index = TREE_OPERAND (node, 1);
14394   tree index_type = TREE_TYPE (index);
14395   int error_found = 0;
14396
14397   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14398
14399   if (TREE_CODE (array_type) == POINTER_TYPE)
14400     array_type = TREE_TYPE (array_type);
14401
14402   /* The array reference must be an array */
14403   if (!TYPE_ARRAY_P (array_type))
14404     {
14405       parse_error_context
14406         (wfl_operator,
14407          "`[]' can only be applied to arrays. It can't be applied to `%s'",
14408          lang_printable_name (array_type, 0));
14409       TREE_TYPE (node) = error_mark_node;
14410       error_found = 1;
14411     }
14412
14413   /* The array index undergoes unary numeric promotion. The promoted
14414      type must be int */
14415   index = do_unary_numeric_promotion (index);
14416   if (TREE_TYPE (index) != int_type_node)
14417     {
14418       if (valid_cast_to_p (index_type, int_type_node))
14419         parse_error_context (wfl_operator,
14420    "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
14421                              lang_printable_name (index_type, 0));
14422       else
14423         parse_error_context (wfl_operator,
14424           "Incompatible type for `[]'. Can't convert `%s' to `int'",
14425                              lang_printable_name (index_type, 0));
14426       TREE_TYPE (node) = error_mark_node;
14427       error_found = 1;
14428     }
14429
14430   if (error_found)
14431     return error_mark_node;
14432
14433   array_type = TYPE_ARRAY_ELEMENT (array_type);
14434
14435   if (flag_emit_class_files || flag_emit_xref)
14436     {
14437       TREE_OPERAND (node, 0) = array;
14438       TREE_OPERAND (node, 1) = index;
14439     }
14440   else
14441     node = build_java_arrayaccess (array, array_type, index);
14442   TREE_TYPE (node) = array_type;
14443   return node;
14444 }
14445
14446 /* 15.9 Array Creation Expressions */
14447
14448 static tree
14449 build_newarray_node (type, dims, extra_dims)
14450      tree type;
14451      tree dims;
14452      int extra_dims;
14453 {
14454   tree node =
14455     build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
14456            build_int_2 (extra_dims, 0));
14457   return node;
14458 }
14459
14460 static tree
14461 patch_newarray (node)
14462      tree node;
14463 {
14464   tree type = TREE_OPERAND (node, 0);
14465   tree dims = TREE_OPERAND (node, 1);
14466   tree cdim, array_type;
14467   int error_found = 0;
14468   int ndims = 0;
14469   int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14470
14471   /* Dimension types are verified. It's better for the types to be
14472      verified in order. */
14473   for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14474     {
14475       int dim_error = 0;
14476       tree dim = TREE_VALUE (cdim);
14477
14478       /* Dim might have been saved during its evaluation */
14479       dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14480
14481       /* The type of each specified dimension must be an integral type. */
14482       if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14483         dim_error = 1;
14484
14485       /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14486          promoted type must be int. */
14487       else
14488         {
14489           dim = do_unary_numeric_promotion (dim);
14490           if (TREE_TYPE (dim) != int_type_node)
14491             dim_error = 1;
14492         }
14493
14494       /* Report errors on types here */
14495       if (dim_error)
14496         {
14497           parse_error_context
14498             (TREE_PURPOSE (cdim),
14499              "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
14500              (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14501               "Explicit cast needed to" : "Can't"),
14502              lang_printable_name (TREE_TYPE (dim), 0));
14503           error_found = 1;
14504         }
14505
14506       TREE_PURPOSE (cdim) = NULL_TREE;
14507     }
14508
14509   /* Resolve array base type if unresolved */
14510   if (!(type = resolve_type_during_patch (type)))
14511     error_found = 1;
14512
14513   if (error_found)
14514     {
14515       /* We don't want further evaluation of this bogus array creation
14516          operation */
14517       TREE_TYPE (node) = error_mark_node;
14518       return error_mark_node;
14519     }
14520
14521   /* Set array_type to the actual (promoted) array type of the result. */
14522   if (TREE_CODE (type) == RECORD_TYPE)
14523     type = build_pointer_type (type);
14524   while (--xdims >= 0)
14525     {
14526       type = promote_type (build_java_array_type (type, -1));
14527     }
14528   dims = nreverse (dims);
14529   array_type = type;
14530   for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14531     {
14532       type = array_type;
14533       array_type
14534         = build_java_array_type (type,
14535                                  TREE_CODE (cdim) == INTEGER_CST
14536                                  ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14537                                  : -1);
14538       array_type = promote_type (array_type);
14539     }
14540   dims = nreverse (dims);
14541
14542   /* The node is transformed into a function call. Things are done
14543      differently according to the number of dimensions. If the number
14544      of dimension is equal to 1, then the nature of the base type
14545      (primitive or not) matters. */
14546   if (ndims == 1)
14547     return build_new_array (type, TREE_VALUE (dims));
14548
14549   /* Can't reuse what's already written in expr.c because it uses the
14550      JVM stack representation. Provide a build_multianewarray. FIXME */
14551   return build (CALL_EXPR, array_type,
14552                 build_address_of (soft_multianewarray_node),
14553                 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
14554                            tree_cons (NULL_TREE,
14555                                       build_int_2 (ndims, 0), dims )),
14556                 NULL_TREE);
14557 }
14558
14559 /* 10.6 Array initializer.  */
14560
14561 /* Build a wfl for array element that don't have one, so we can
14562    pin-point errors.  */
14563
14564 static tree
14565 maybe_build_array_element_wfl (node)
14566      tree node;
14567 {
14568   if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14569     return build_expr_wfl (NULL_TREE, ctxp->filename,
14570                            ctxp->elc.line, ctxp->elc.prev_col);
14571   else
14572     return NULL_TREE;
14573 }
14574
14575 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14576    identification of initialized arrays easier to detect during walk
14577    and expansion.  */
14578
14579 static tree
14580 build_new_array_init (location, values)
14581      int location;
14582      tree values;
14583 {
14584   tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14585   tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14586   EXPR_WFL_LINECOL (to_return) = location;
14587   return to_return;
14588 }
14589
14590 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14591    occurred.  Otherwise return NODE after having set its type
14592    appropriately.  */
14593
14594 static tree
14595 patch_new_array_init (type, node)
14596      tree type, node;
14597 {
14598   int error_seen = 0;
14599   tree current, element_type;
14600   HOST_WIDE_INT length;
14601   int all_constant = 1;
14602   tree init = TREE_OPERAND (node, 0);
14603
14604   if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14605     {
14606       parse_error_context (node,
14607                            "Invalid array initializer for non-array type `%s'",
14608                            lang_printable_name (type, 1));
14609       return error_mark_node;
14610     }
14611   type = TREE_TYPE (type);
14612   element_type = TYPE_ARRAY_ELEMENT (type);
14613
14614   CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14615
14616   for (length = 0, current = CONSTRUCTOR_ELTS (init);
14617        current;  length++, current = TREE_CHAIN (current))
14618     {
14619       tree elt = TREE_VALUE (current);
14620       if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14621         {
14622           error_seen |= array_constructor_check_entry (element_type, current);
14623           elt = TREE_VALUE (current);
14624           /* When compiling to native code, STRING_CST is converted to
14625              INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14626           if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14627             all_constant = 0;
14628         }
14629       else
14630         {
14631           TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14632           TREE_PURPOSE (current) = NULL_TREE;
14633           all_constant = 0;
14634         }
14635       if (elt && TREE_CODE (elt) == TREE_LIST
14636           && TREE_VALUE (elt) == error_mark_node)
14637         error_seen = 1;
14638     }
14639
14640   if (error_seen)
14641     return error_mark_node;
14642
14643   /* Create a new type. We can't reuse the one we have here by
14644      patching its dimension because it originally is of dimension -1
14645      hence reused by gcc. This would prevent triangular arrays. */
14646   type = build_java_array_type (element_type, length);
14647   TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14648   TREE_TYPE (node) = promote_type (type);
14649   TREE_CONSTANT (init) = all_constant;
14650   TREE_CONSTANT (node) = all_constant;
14651   return node;
14652 }
14653
14654 /* Verify that one entry of the initializer element list can be
14655    assigned to the array base type. Report 1 if an error occurred, 0
14656    otherwise.  */
14657
14658 static int
14659 array_constructor_check_entry (type, entry)
14660      tree type, entry;
14661 {
14662   char *array_type_string = NULL;       /* For error reports */
14663   tree value, type_value, new_value, wfl_value, patched;
14664   int error_seen = 0;
14665
14666   new_value = NULL_TREE;
14667   wfl_value = TREE_VALUE (entry);
14668
14669   value = java_complete_tree (TREE_VALUE (entry));
14670   /* patch_string return error_mark_node if arg is error_mark_node */
14671   if ((patched = patch_string (value)))
14672     value = patched;
14673   if (value == error_mark_node)
14674     return 1;
14675
14676   type_value = TREE_TYPE (value);
14677
14678   /* At anytime, try_builtin_assignconv can report a warning on
14679      constant overflow during narrowing. */
14680   SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14681   new_value = try_builtin_assignconv (wfl_operator, type, value);
14682   if (!new_value && (new_value = try_reference_assignconv (type, value)))
14683     type_value = promote_type (type);
14684
14685   /* Check and report errors */
14686   if (!new_value)
14687     {
14688       const char *const msg = (!valid_cast_to_p (type_value, type) ?
14689                    "Can't" : "Explicit cast needed to");
14690       if (!array_type_string)
14691         array_type_string = xstrdup (lang_printable_name (type, 1));
14692       parse_error_context
14693         (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14694          msg, lang_printable_name (type_value, 1), array_type_string);
14695       error_seen = 1;
14696     }
14697
14698   if (new_value)
14699     TREE_VALUE (entry) = new_value;
14700
14701   if (array_type_string)
14702     free (array_type_string);
14703
14704   TREE_PURPOSE (entry) = NULL_TREE;
14705   return error_seen;
14706 }
14707
14708 static tree
14709 build_this (location)
14710      int location;
14711 {
14712   tree node = build_wfl_node (this_identifier_node);
14713   TREE_SET_CODE (node, THIS_EXPR);
14714   EXPR_WFL_LINECOL (node) = location;
14715   return node;
14716 }
14717
14718 /* 14.15 The return statement. It builds a modify expression that
14719    assigns the returned value to the RESULT_DECL that hold the value
14720    to be returned. */
14721
14722 static tree
14723 build_return (location, op)
14724      int location;
14725      tree op;
14726 {
14727   tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14728   EXPR_WFL_LINECOL (node) = location;
14729   node = build_debugable_stmt (location, node);
14730   return node;
14731 }
14732
14733 static tree
14734 patch_return (node)
14735      tree node;
14736 {
14737   tree return_exp = TREE_OPERAND (node, 0);
14738   tree meth = current_function_decl;
14739   tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14740   int error_found = 0;
14741
14742   TREE_TYPE (node) = error_mark_node;
14743   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14744
14745   /* It's invalid to have a return value within a function that is
14746      declared with the keyword void or that is a constructor */
14747   if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14748     error_found = 1;
14749
14750   /* It's invalid to use a return statement in a static block */
14751   if (DECL_CLINIT_P (current_function_decl))
14752     error_found = 1;
14753
14754   /* It's invalid to have a no return value within a function that
14755      isn't declared with the keyword `void' */
14756   if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14757     error_found = 2;
14758
14759   if (DECL_INSTINIT_P (current_function_decl))
14760     error_found = 1;
14761
14762   if (error_found)
14763     {
14764       if (DECL_INSTINIT_P (current_function_decl))
14765         parse_error_context (wfl_operator,
14766                              "`return' inside instance initializer");
14767
14768       else if (DECL_CLINIT_P (current_function_decl))
14769         parse_error_context (wfl_operator,
14770                              "`return' inside static initializer");
14771
14772       else if (!DECL_CONSTRUCTOR_P (meth))
14773         {
14774           char *t = xstrdup (lang_printable_name (mtype, 0));
14775           parse_error_context (wfl_operator,
14776                                "`return' with%s value from `%s %s'",
14777                                (error_found == 1 ? "" : "out"),
14778                                t, lang_printable_name (meth, 0));
14779           free (t);
14780         }
14781       else
14782         parse_error_context (wfl_operator,
14783                              "`return' with value from constructor `%s'",
14784                              lang_printable_name (meth, 0));
14785       return error_mark_node;
14786     }
14787
14788   /* If we have a return_exp, build a modify expression and expand
14789      it. Note: at that point, the assignment is declared valid, but we
14790      may want to carry some more hacks */
14791   if (return_exp)
14792     {
14793       tree exp = java_complete_tree (return_exp);
14794       tree modify, patched;
14795
14796       if ((patched = patch_string (exp)))
14797         exp = patched;
14798
14799       modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14800       EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14801       modify = java_complete_tree (modify);
14802
14803       if (modify != error_mark_node)
14804         {
14805           TREE_SIDE_EFFECTS (modify) = 1;
14806           TREE_OPERAND (node, 0) = modify;
14807         }
14808       else
14809         return error_mark_node;
14810     }
14811   TREE_TYPE (node) = void_type_node;
14812   TREE_SIDE_EFFECTS (node) = 1;
14813   return node;
14814 }
14815
14816 /* 14.8 The if Statement */
14817
14818 static tree
14819 build_if_else_statement (location, expression, if_body, else_body)
14820      int location;
14821      tree expression, if_body, else_body;
14822 {
14823   tree node;
14824   if (!else_body)
14825     else_body = empty_stmt_node;
14826   node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14827   EXPR_WFL_LINECOL (node) = location;
14828   node = build_debugable_stmt (location, node);
14829   return node;
14830 }
14831
14832 static tree
14833 patch_if_else_statement (node)
14834      tree node;
14835 {
14836   tree expression = TREE_OPERAND (node, 0);
14837   int can_complete_normally
14838     = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14839        | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
14840
14841   TREE_TYPE (node) = error_mark_node;
14842   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14843
14844   /* The type of expression must be boolean */
14845   if (TREE_TYPE (expression) != boolean_type_node
14846       && TREE_TYPE (expression) != promoted_boolean_type_node)
14847     {
14848       parse_error_context
14849         (wfl_operator,
14850          "Incompatible type for `if'. Can't convert `%s' to `boolean'",
14851          lang_printable_name (TREE_TYPE (expression), 0));
14852       return error_mark_node;
14853     }
14854
14855   if (TREE_CODE (expression) == INTEGER_CST)
14856     {
14857       if (integer_zerop (expression))
14858         node = TREE_OPERAND (node, 2);
14859       else
14860         node = TREE_OPERAND (node, 1);
14861       if (CAN_COMPLETE_NORMALLY (node) != can_complete_normally)
14862         {
14863           node = build (COMPOUND_EXPR, void_type_node, node, empty_stmt_node);
14864           CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
14865         }
14866       return node;
14867     }
14868   TREE_TYPE (node) = void_type_node;
14869   TREE_SIDE_EFFECTS (node) = 1;
14870   CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
14871   return node;
14872 }
14873
14874 /* 14.6 Labeled Statements */
14875
14876 /* Action taken when a lableled statement is parsed. a new
14877    LABELED_BLOCK_EXPR is created. No statement is attached to the
14878    label, yet.  LABEL can be NULL_TREE for artificially-generated blocks. */
14879
14880 static tree
14881 build_labeled_block (location, label)
14882      int location;
14883      tree label;
14884 {
14885   tree label_name ;
14886   tree label_decl, node;
14887   if (label == NULL_TREE || label == continue_identifier_node)
14888     label_name = label;
14889   else
14890     {
14891       label_name = merge_qualified_name (label_id, label);
14892       /* Issue an error if we try to reuse a label that was previously
14893          declared */
14894       if (IDENTIFIER_LOCAL_VALUE (label_name))
14895         {
14896           EXPR_WFL_LINECOL (wfl_operator) = location;
14897           parse_error_context (wfl_operator,
14898             "Declaration of `%s' shadows a previous label declaration",
14899                                IDENTIFIER_POINTER (label));
14900           EXPR_WFL_LINECOL (wfl_operator) =
14901             EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14902           parse_error_context (wfl_operator,
14903             "This is the location of the previous declaration of label `%s'",
14904                                IDENTIFIER_POINTER (label));
14905           java_error_count--;
14906         }
14907     }
14908
14909   label_decl = create_label_decl (label_name);
14910   node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14911   EXPR_WFL_LINECOL (node) = location;
14912   TREE_SIDE_EFFECTS (node) = 1;
14913   return node;
14914 }
14915
14916 /* A labeled statement LBE is attached a statement.  */
14917
14918 static tree
14919 finish_labeled_statement (lbe, statement)
14920      tree lbe;                  /* Labeled block expr */
14921      tree statement;
14922 {
14923   /* In anyways, tie the loop to its statement */
14924   LABELED_BLOCK_BODY (lbe) = statement;
14925   pop_labeled_block ();
14926   POP_LABELED_BLOCK ();
14927   return lbe;
14928 }
14929
14930 /* 14.10, 14.11, 14.12 Loop Statements */
14931
14932 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14933    list. */
14934
14935 static tree
14936 build_new_loop (loop_body)
14937      tree loop_body;
14938 {
14939   tree loop =  build (LOOP_EXPR, NULL_TREE, loop_body);
14940   TREE_SIDE_EFFECTS (loop) = 1;
14941   PUSH_LOOP (loop);
14942   return loop;
14943 }
14944
14945 /* Create a loop body according to the following structure:
14946      COMPOUND_EXPR
14947        COMPOUND_EXPR            (loop main body)
14948          EXIT_EXPR              (this order is for while/for loops.
14949          LABELED_BLOCK_EXPR      the order is reversed for do loops)
14950            LABEL_DECL           (a continue occurring here branches at the
14951            BODY                  end of this labeled block)
14952        INCREMENT                (if any)
14953
14954   REVERSED, if non zero, tells that the loop condition expr comes
14955   after the body, like in the do-while loop.
14956
14957   To obtain a loop, the loop body structure described above is
14958   encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14959
14960    LABELED_BLOCK_EXPR
14961      LABEL_DECL                   (use this label to exit the loop)
14962      LOOP_EXPR
14963        <structure described above> */
14964
14965 static tree
14966 build_loop_body (location, condition, reversed)
14967      int location;
14968      tree condition;
14969      int reversed;
14970 {
14971   tree first, second, body;
14972
14973   condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14974   EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14975   condition = build_debugable_stmt (location, condition);
14976   TREE_SIDE_EFFECTS (condition) = 1;
14977
14978   body = build_labeled_block (0, continue_identifier_node);
14979   first = (reversed ? body : condition);
14980   second = (reversed ? condition : body);
14981   return
14982     build (COMPOUND_EXPR, NULL_TREE,
14983            build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14984 }
14985
14986 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14987    their order) on the current loop. Unlink the current loop from the
14988    loop list.  */
14989
14990 static tree
14991 finish_loop_body (location, condition, body, reversed)
14992      int location;
14993      tree condition, body;
14994      int reversed;
14995 {
14996   tree to_return = ctxp->current_loop;
14997   tree loop_body = LOOP_EXPR_BODY (to_return);
14998   if (condition)
14999     {
15000       tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
15001       /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
15002          The real EXIT_EXPR is one operand further. */
15003       EXPR_WFL_LINECOL (cnode) = location;
15004       /* This one is for accurate error reports */
15005       EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
15006       TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
15007     }
15008   LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
15009   POP_LOOP ();
15010   return to_return;
15011 }
15012
15013 /* Tailored version of finish_loop_body for FOR loops, when FOR
15014    loops feature the condition part */
15015
15016 static tree
15017 finish_for_loop (location, condition, update, body)
15018     int location;
15019     tree condition, update, body;
15020 {
15021   /* Put the condition and the loop body in place */
15022   tree loop = finish_loop_body (location, condition, body, 0);
15023   /* LOOP is the current loop which has been now popped of the loop
15024      stack.  Mark the update block as reachable and install it.  We do
15025      this because the (current interpretation of the) JLS requires
15026      that the update expression be considered reachable even if the
15027      for loop's body doesn't complete normally.  */
15028   if (update != NULL_TREE && update != empty_stmt_node)
15029     {
15030       tree up2 = update;
15031       if (TREE_CODE (up2) == EXPR_WITH_FILE_LOCATION)
15032         up2 = EXPR_WFL_NODE (up2);
15033       /* Try to detect constraint violations.  These would be
15034          programming errors somewhere.  */
15035       if (! IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (up2)))
15036           || TREE_CODE (up2) == LOOP_EXPR)
15037         abort ();
15038       SUPPRESS_UNREACHABLE_ERROR (up2) = 1;
15039     }
15040   LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
15041   return loop;
15042 }
15043
15044 /* Try to find the loop a block might be related to. This comprises
15045    the case where the LOOP_EXPR is found as the second operand of a
15046    COMPOUND_EXPR, because the loop happens to have an initialization
15047    part, then expressed as the first operand of the COMPOUND_EXPR. If
15048    the search finds something, 1 is returned. Otherwise, 0 is
15049    returned. The search is assumed to start from a
15050    LABELED_BLOCK_EXPR's block.  */
15051
15052 static tree
15053 search_loop (statement)
15054     tree statement;
15055 {
15056   if (TREE_CODE (statement) == LOOP_EXPR)
15057     return statement;
15058
15059   if (TREE_CODE (statement) == BLOCK)
15060     statement = BLOCK_SUBBLOCKS (statement);
15061   else
15062     return NULL_TREE;
15063
15064   if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15065     while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15066       statement = TREE_OPERAND (statement, 1);
15067
15068   return (TREE_CODE (statement) == LOOP_EXPR
15069           && FOR_LOOP_P (statement) ? statement : NULL_TREE);
15070 }
15071
15072 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
15073    returned otherwise.  */
15074
15075 static int
15076 labeled_block_contains_loop_p (block, loop)
15077     tree block, loop;
15078 {
15079   if (!block)
15080     return 0;
15081
15082   if (LABELED_BLOCK_BODY (block) == loop)
15083     return 1;
15084
15085   if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
15086     return 1;
15087
15088   return 0;
15089 }
15090
15091 /* If the loop isn't surrounded by a labeled statement, create one and
15092    insert LOOP as its body.  */
15093
15094 static tree
15095 patch_loop_statement (loop)
15096      tree loop;
15097 {
15098   tree loop_label;
15099
15100   TREE_TYPE (loop) = void_type_node;
15101   if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
15102     return loop;
15103
15104   loop_label = build_labeled_block (0, NULL_TREE);
15105   /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
15106      that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
15107   LABELED_BLOCK_BODY (loop_label) = loop;
15108   PUSH_LABELED_BLOCK (loop_label);
15109   return loop_label;
15110 }
15111
15112 /* 14.13, 14.14: break and continue Statements */
15113
15114 /* Build a break or a continue statement. a null NAME indicates an
15115    unlabeled break/continue statement.  */
15116
15117 static tree
15118 build_bc_statement (location, is_break, name)
15119      int location, is_break;
15120      tree name;
15121 {
15122   tree break_continue, label_block_expr = NULL_TREE;
15123
15124   if (name)
15125     {
15126       if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15127             (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15128         /* Null means that we don't have a target for this named
15129            break/continue. In this case, we make the target to be the
15130            label name, so that the error can be reported accuratly in
15131            patch_bc_statement. */
15132         label_block_expr = EXPR_WFL_NODE (name);
15133     }
15134   /* Unlabeled break/continue will be handled during the
15135      break/continue patch operation */
15136   break_continue
15137     = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
15138
15139   IS_BREAK_STMT_P (break_continue) = is_break;
15140   TREE_SIDE_EFFECTS (break_continue) = 1;
15141   EXPR_WFL_LINECOL (break_continue) = location;
15142   break_continue = build_debugable_stmt (location, break_continue);
15143   return break_continue;
15144 }
15145
15146 /* Verification of a break/continue statement. */
15147
15148 static tree
15149 patch_bc_statement (node)
15150      tree node;
15151 {
15152   tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15153   tree labeled_block = ctxp->current_labeled_block;
15154   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15155
15156   /* Having an identifier here means that the target is unknown. */
15157   if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15158     {
15159       parse_error_context (wfl_operator, "No label definition found for `%s'",
15160                            IDENTIFIER_POINTER (bc_label));
15161       return error_mark_node;
15162     }
15163   if (! IS_BREAK_STMT_P (node))
15164     {
15165       /* It's a continue statement. */
15166       for (;; labeled_block = TREE_CHAIN (labeled_block))
15167         {
15168           if (labeled_block == NULL_TREE)
15169             {
15170               if (bc_label == NULL_TREE)
15171                 parse_error_context (wfl_operator,
15172                                      "`continue' must be in loop");
15173               else
15174                 parse_error_context
15175                   (wfl_operator, "continue label `%s' does not name a loop",
15176                    IDENTIFIER_POINTER (bc_label));
15177               return error_mark_node;
15178             }
15179           if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15180                == continue_identifier_node)
15181               && (bc_label == NULL_TREE
15182                   || TREE_CHAIN (labeled_block) == bc_label))
15183             {
15184               bc_label = labeled_block;
15185               break;
15186             }
15187         }
15188     }
15189   else if (!bc_label)
15190     {
15191       for (;; labeled_block = TREE_CHAIN (labeled_block))
15192         {
15193           if (labeled_block == NULL_TREE)
15194             {
15195               parse_error_context (wfl_operator,
15196                                      "`break' must be in loop or switch");
15197               return error_mark_node;
15198             }
15199           target_stmt = LABELED_BLOCK_BODY (labeled_block);
15200           if (TREE_CODE (target_stmt) == SWITCH_EXPR
15201               || search_loop (target_stmt))
15202             {
15203               bc_label = labeled_block;
15204               break;
15205             }
15206         }
15207     }
15208
15209   EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15210   CAN_COMPLETE_NORMALLY (bc_label) = 1;
15211
15212   /* Our break/continue don't return values. */
15213   TREE_TYPE (node) = void_type_node;
15214   /* Encapsulate the break within a compound statement so that it's
15215      expanded all the times by expand_expr (and not clobbered
15216      sometimes, like after a if statement) */
15217   node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15218   TREE_SIDE_EFFECTS (node) = 1;
15219   return node;
15220 }
15221
15222 /* Process the exit expression belonging to a loop. Its type must be
15223    boolean.  */
15224
15225 static tree
15226 patch_exit_expr (node)
15227      tree node;
15228 {
15229   tree expression = TREE_OPERAND (node, 0);
15230   TREE_TYPE (node) = error_mark_node;
15231   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15232
15233   /* The type of expression must be boolean */
15234   if (TREE_TYPE (expression) != boolean_type_node)
15235     {
15236       parse_error_context
15237         (wfl_operator,
15238     "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
15239          lang_printable_name (TREE_TYPE (expression), 0));
15240       return error_mark_node;
15241     }
15242   /* Now we know things are allright, invert the condition, fold and
15243      return */
15244   TREE_OPERAND (node, 0) =
15245     fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15246
15247   if (! integer_zerop (TREE_OPERAND (node, 0))
15248       && ctxp->current_loop != NULL_TREE
15249       && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15250     CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15251   if (! integer_onep (TREE_OPERAND (node, 0)))
15252     CAN_COMPLETE_NORMALLY (node) = 1;
15253
15254
15255   TREE_TYPE (node) = void_type_node;
15256   return node;
15257 }
15258
15259 /* 14.9 Switch statement */
15260
15261 static tree
15262 patch_switch_statement (node)
15263      tree node;
15264 {
15265   tree se = TREE_OPERAND (node, 0), se_type;
15266   tree save, iter;
15267
15268   /* Complete the switch expression */
15269   se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15270   se_type = TREE_TYPE (se);
15271   /* The type of the switch expression must be char, byte, short or
15272      int */
15273   if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15274     {
15275       EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15276       parse_error_context (wfl_operator,
15277           "Incompatible type for `switch'. Can't convert `%s' to `int'",
15278                            lang_printable_name (se_type, 0));
15279       /* This is what java_complete_tree will check */
15280       TREE_OPERAND (node, 0) = error_mark_node;
15281       return error_mark_node;
15282     }
15283
15284   /* Save and restore the outer case label list.  */
15285   save = case_label_list;
15286   case_label_list = NULL_TREE;
15287
15288   TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15289
15290   /* See if we've found a duplicate label.  We can't leave this until
15291      code generation, because in `--syntax-only' and `-C' modes we
15292      don't do ordinary code generation.  */
15293   for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15294     {
15295       HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15296       tree subiter;
15297       for (subiter = TREE_CHAIN (iter);
15298            subiter != NULL_TREE;
15299            subiter = TREE_CHAIN (subiter))
15300         {
15301           HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15302           if (val == subval)
15303             {
15304               EXPR_WFL_LINECOL (wfl_operator)
15305                 = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15306               /* The case_label_list is in reverse order, so print the
15307                  outer label first.  */
15308               parse_error_context (wfl_operator, "duplicate case label: `"
15309                                    HOST_WIDE_INT_PRINT_DEC "'", subval);
15310               EXPR_WFL_LINECOL (wfl_operator)
15311                 = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15312               parse_error_context (wfl_operator, "original label is here");
15313
15314               break;
15315             }
15316         }
15317     }
15318
15319   case_label_list = save;
15320
15321   /* Ready to return */
15322   if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15323     {
15324       TREE_TYPE (node) = error_mark_node;
15325       return error_mark_node;
15326     }
15327   TREE_TYPE (node) = void_type_node;
15328   TREE_SIDE_EFFECTS (node) = 1;
15329   CAN_COMPLETE_NORMALLY (node)
15330     = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15331       || ! SWITCH_HAS_DEFAULT (node);
15332   return node;
15333 }
15334
15335 /* Assertions.  */
15336
15337 /* Build an assertion expression for `assert CONDITION : VALUE'; VALUE
15338    might be NULL_TREE.  */
15339 static tree
15340 build_assertion (location, condition, value)
15341      int location;
15342      tree condition, value;
15343 {
15344   tree node;
15345   tree klass = GET_CPC ();
15346
15347   if (! CLASS_USES_ASSERTIONS (klass))
15348     {
15349       tree field, classdollar, id, call;
15350       tree class_type = TREE_TYPE (klass);
15351
15352       field = add_field (class_type,
15353                          get_identifier ("$assertionsDisabled"),
15354                          boolean_type_node,
15355                          ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
15356       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
15357       FIELD_SYNTHETIC (field) = 1;
15358
15359       if (!TYPE_DOT_CLASS (class_type))
15360         build_dot_class_method (class_type);
15361       classdollar = build_dot_class_method_invocation (class_type);
15362
15363       /* Call CLASS.desiredAssertionStatus().  */
15364       id = build_wfl_node (get_identifier ("desiredAssertionStatus"));
15365       call = build (CALL_EXPR, NULL_TREE, id, NULL_TREE, NULL_TREE);
15366       call = make_qualified_primary (classdollar, call, location);
15367       TREE_SIDE_EFFECTS (call) = 1;
15368
15369       /* Invert to obtain !CLASS.desiredAssertionStatus().  This may
15370          seem odd, but we do it to generate code identical to that of
15371          the JDK.  */
15372       call = build1 (TRUTH_NOT_EXPR, NULL_TREE, call);
15373       TREE_SIDE_EFFECTS (call) = 1;
15374       DECL_INITIAL (field) = call;
15375
15376       /* Record the initializer in the initializer statement list.  */
15377       call = build (MODIFY_EXPR, NULL_TREE, field, call);
15378       TREE_CHAIN (call) = CPC_STATIC_INITIALIZER_STMT (ctxp);
15379       SET_CPC_STATIC_INITIALIZER_STMT (ctxp, call);
15380       MODIFY_EXPR_FROM_INITIALIZATION_P (call) = 1;
15381
15382       CLASS_USES_ASSERTIONS (klass) = 1;
15383     }
15384
15385   if (value != NULL_TREE)
15386     value = tree_cons (NULL_TREE, value, NULL_TREE);
15387
15388   node = build_wfl_node (get_identifier ("java"));
15389   node = make_qualified_name (node, build_wfl_node (get_identifier ("lang")),
15390                               location);
15391   node = make_qualified_name (node, build_wfl_node (get_identifier ("AssertionError")),
15392                               location);
15393
15394   node = build (NEW_CLASS_EXPR, NULL_TREE, node, value, NULL_TREE);
15395   TREE_SIDE_EFFECTS (node) = 1;
15396   /* It is too early to use BUILD_THROW.  */
15397   node = build1 (THROW_EXPR, NULL_TREE, node);
15398   TREE_SIDE_EFFECTS (node) = 1;
15399
15400   /* We invert the condition; if we just put NODE as the `else' part
15401      then we generate weird-looking bytecode.  */
15402   condition = build1 (TRUTH_NOT_EXPR, NULL_TREE, condition);
15403   /* Check $assertionsDisabled.  */
15404   condition
15405     = build (TRUTH_ANDIF_EXPR, NULL_TREE,
15406              build1 (TRUTH_NOT_EXPR, NULL_TREE,
15407                      build_wfl_node (get_identifier ("$assertionsDisabled"))),
15408              condition);
15409   node = build_if_else_statement (location, condition, node, NULL_TREE);
15410   return node;
15411 }
15412
15413 /* 14.18 The try/catch statements */
15414
15415 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15416    catches TYPE and executes CATCH_STMTS.  */
15417
15418 static tree
15419 encapsulate_with_try_catch (location, type, try_stmts, catch_stmts)
15420      int location;
15421      tree type, try_stmts, catch_stmts;
15422 {
15423   tree try_block, catch_clause_param, catch_block, catch;
15424
15425   /* First build a try block */
15426   try_block = build_expr_block (try_stmts, NULL_TREE);
15427
15428   /* Build a catch block: we need a catch clause parameter */
15429   catch_clause_param = build_decl (VAR_DECL,
15430                                    wpv_id, build_pointer_type (type));
15431   /* And a block */
15432   catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15433
15434   /* Initialize the variable and store in the block */
15435   catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15436                  build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15437   add_stmt_to_block (catch_block, NULL_TREE, catch);
15438
15439   /* Add the catch statements */
15440   add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15441
15442   /* Now we can build a CATCH_EXPR */
15443   catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
15444
15445   return build_try_statement (location, try_block, catch_block);
15446 }
15447
15448 static tree
15449 build_try_statement (location, try_block, catches)
15450      int location;
15451      tree try_block, catches;
15452 {
15453   tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
15454   EXPR_WFL_LINECOL (node) = location;
15455   return node;
15456 }
15457
15458 static tree
15459 build_try_finally_statement (location, try_block, finally)
15460      int location;
15461      tree try_block, finally;
15462 {
15463   tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15464   EXPR_WFL_LINECOL (node) = location;
15465   return node;
15466 }
15467
15468 static tree
15469 patch_try_statement (node)
15470      tree node;
15471 {
15472   int error_found = 0;
15473   tree try = TREE_OPERAND (node, 0);
15474   /* Exception handlers are considered in left to right order */
15475   tree catch = nreverse (TREE_OPERAND (node, 1));
15476   tree current, caught_type_list = NULL_TREE;
15477
15478   /* Check catch clauses, if any. Every time we find an error, we try
15479      to process the next catch clause. We process the catch clause before
15480      the try block so that when processing the try block we can check thrown
15481      exceptions againts the caught type list. */
15482   for (current = catch; current; current = TREE_CHAIN (current))
15483     {
15484       tree carg_decl, carg_type;
15485       tree sub_current, catch_block, catch_clause;
15486       int unreachable;
15487
15488       /* At this point, the structure of the catch clause is
15489            CATCH_EXPR           (catch node)
15490              BLOCK              (with the decl of the parameter)
15491                COMPOUND_EXPR
15492                  MODIFY_EXPR   (assignment of the catch parameter)
15493                  BLOCK          (catch clause block)
15494        */
15495       catch_clause = TREE_OPERAND (current, 0);
15496       carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15497       carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15498
15499       /* Catch clauses can't have more than one parameter declared,
15500          but it's already enforced by the grammar. Make sure that the
15501          only parameter of the clause statement in of class Throwable
15502          or a subclass of Throwable, but that was done earlier. The
15503          catch clause parameter type has also been resolved. */
15504
15505       /* Just make sure that the catch clause parameter type inherits
15506          from java.lang.Throwable */
15507       if (!inherits_from_p (carg_type, throwable_type_node))
15508         {
15509           EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15510           parse_error_context (wfl_operator,
15511                                "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
15512                                lang_printable_name (carg_type, 0));
15513           error_found = 1;
15514           continue;
15515         }
15516
15517       /* Partial check for unreachable catch statement: The catch
15518          clause is reachable iff is no earlier catch block A in
15519          the try statement such that the type of the catch
15520          clause's parameter is the same as or a subclass of the
15521          type of A's parameter */
15522       unreachable = 0;
15523       for (sub_current = catch;
15524            sub_current != current; sub_current = TREE_CHAIN (sub_current))
15525         {
15526           tree sub_catch_clause, decl;
15527           sub_catch_clause = TREE_OPERAND (sub_current, 0);
15528           decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15529
15530           if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15531             {
15532               EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15533               parse_error_context
15534                 (wfl_operator,
15535                  "`catch' not reached because of the catch clause at line %d",
15536                  EXPR_WFL_LINENO (sub_current));
15537               unreachable = error_found = 1;
15538               break;
15539             }
15540         }
15541       /* Complete the catch clause block */
15542       catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15543       if (catch_block == error_mark_node)
15544         {
15545           error_found = 1;
15546           continue;
15547         }
15548       if (CAN_COMPLETE_NORMALLY (catch_block))
15549         CAN_COMPLETE_NORMALLY (node) = 1;
15550       TREE_OPERAND (current, 0) = catch_block;
15551
15552       if (unreachable)
15553         continue;
15554
15555       /* Things to do here: the exception must be thrown */
15556
15557       /* Link this type to the caught type list */
15558       caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15559     }
15560
15561   PUSH_EXCEPTIONS (caught_type_list);
15562   if ((try = java_complete_tree (try)) == error_mark_node)
15563     error_found = 1;
15564   if (CAN_COMPLETE_NORMALLY (try))
15565     CAN_COMPLETE_NORMALLY (node) = 1;
15566   POP_EXCEPTIONS ();
15567
15568   /* Verification ends here */
15569   if (error_found)
15570     return error_mark_node;
15571
15572   TREE_OPERAND (node, 0) = try;
15573   TREE_OPERAND (node, 1) = catch;
15574   TREE_TYPE (node) = void_type_node;
15575   return node;
15576 }
15577
15578 /* 14.17 The synchronized Statement */
15579
15580 static tree
15581 patch_synchronized_statement (node, wfl_op1)
15582     tree node, wfl_op1;
15583 {
15584   tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15585   tree block = TREE_OPERAND (node, 1);
15586
15587   tree tmp, enter, exit, expr_decl, assignment;
15588
15589   if (expr == error_mark_node)
15590     {
15591       block = java_complete_tree (block);
15592       return expr;
15593     }
15594
15595   /* We might be trying to synchronize on a STRING_CST */
15596   if ((tmp = patch_string (expr)))
15597     expr = tmp;
15598
15599   /* The TYPE of expr must be a reference type */
15600   if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15601     {
15602       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15603       parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
15604                            lang_printable_name (TREE_TYPE (expr), 0));
15605       return error_mark_node;
15606     }
15607
15608   if (flag_emit_xref)
15609     {
15610       TREE_OPERAND (node, 0) = expr;
15611       TREE_OPERAND (node, 1) = java_complete_tree (block);
15612       CAN_COMPLETE_NORMALLY (node) = 1;
15613       return node;
15614     }
15615
15616   /* Generate a try-finally for the synchronized statement, except
15617      that the handler that catches all throw exception calls
15618      _Jv_MonitorExit and then rethrow the exception.
15619      The synchronized statement is then implemented as:
15620      TRY
15621        {
15622          _Jv_MonitorEnter (expression)
15623          synchronized_block
15624          _Jv_MonitorExit (expression)
15625        }
15626      CATCH_ALL
15627        {
15628          e = _Jv_exception_info ();
15629          _Jv_MonitorExit (expression)
15630          Throw (e);
15631        } */
15632
15633   expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15634   BUILD_MONITOR_ENTER (enter, expr_decl);
15635   BUILD_MONITOR_EXIT (exit, expr_decl);
15636   CAN_COMPLETE_NORMALLY (enter) = 1;
15637   CAN_COMPLETE_NORMALLY (exit) = 1;
15638   assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15639   TREE_SIDE_EFFECTS (assignment) = 1;
15640   node = build (COMPOUND_EXPR, NULL_TREE,
15641                 build (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15642                 build (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15643   node = build_expr_block (node, expr_decl);
15644
15645   return java_complete_tree (node);
15646 }
15647
15648 /* 14.16 The throw Statement */
15649
15650 static tree
15651 patch_throw_statement (node, wfl_op1)
15652     tree node, wfl_op1;
15653 {
15654   tree expr = TREE_OPERAND (node, 0);
15655   tree type = TREE_TYPE (expr);
15656   int unchecked_ok = 0, tryblock_throws_ok = 0;
15657
15658   /* Thrown expression must be assignable to java.lang.Throwable */
15659   if (!try_reference_assignconv (throwable_type_node, expr))
15660     {
15661       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15662       parse_error_context (wfl_operator,
15663     "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
15664                            lang_printable_name (type, 0));
15665       /* If the thrown expression was a reference, we further the
15666          compile-time check. */
15667       if (!JREFERENCE_TYPE_P (type))
15668         return error_mark_node;
15669     }
15670
15671   /* At least one of the following must be true */
15672
15673   /* The type of the throw expression is a not checked exception,
15674      i.e. is a unchecked expression. */
15675   unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15676
15677   SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15678   /* An instance can't throw a checked exception unless that exception
15679      is explicitly declared in the `throws' clause of each
15680      constructor. This doesn't apply to anonymous classes, since they
15681      don't have declared constructors. */
15682   if (!unchecked_ok
15683       && DECL_INSTINIT_P (current_function_decl)
15684       && !ANONYMOUS_CLASS_P (current_class))
15685     {
15686       tree current;
15687       for (current = TYPE_METHODS (current_class); current;
15688            current = TREE_CHAIN (current))
15689         if (DECL_CONSTRUCTOR_P (current)
15690             && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15691           {
15692             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)",
15693                                  lang_printable_name (TREE_TYPE (expr), 0));
15694             return error_mark_node;
15695           }
15696     }
15697
15698   /* Throw is contained in a try statement and at least one catch
15699      clause can receive the thrown expression or the current method is
15700      declared to throw such an exception. Or, the throw statement is
15701      contained in a method or constructor declaration and the type of
15702      the Expression is assignable to at least one type listed in the
15703      throws clause the declaration. */
15704   if (!unchecked_ok)
15705     tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15706   if (!(unchecked_ok || tryblock_throws_ok))
15707     {
15708       /* If there is a surrounding try block that has no matching
15709          clatch clause, report it first. A surrounding try block exits
15710          only if there is something after the list of checked
15711          exception thrown by the current function (if any). */
15712       if (IN_TRY_BLOCK_P ())
15713         parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
15714                              lang_printable_name (type, 0));
15715       /* If we have no surrounding try statement and the method doesn't have
15716          any throws, report it now. FIXME */
15717
15718       /* We report that the exception can't be throw from a try block
15719          in all circumstances but when the `throw' is inside a static
15720          block. */
15721       else if (!EXCEPTIONS_P (currently_caught_type_list)
15722                && !tryblock_throws_ok)
15723         {
15724           if (DECL_CLINIT_P (current_function_decl))
15725             parse_error_context (wfl_operator,
15726                    "Checked exception `%s' can't be thrown in initializer",
15727                                  lang_printable_name (type, 0));
15728           else
15729             parse_error_context (wfl_operator,
15730                    "Checked exception `%s' isn't thrown from a `try' block",
15731                                  lang_printable_name (type, 0));
15732         }
15733       /* Otherwise, the current method doesn't have the appropriate
15734          throws declaration */
15735       else
15736         parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
15737                              lang_printable_name (type, 0));
15738       return error_mark_node;
15739     }
15740
15741   if (! flag_emit_class_files && ! flag_emit_xref)
15742     BUILD_THROW (node, expr);
15743
15744   /* If doing xrefs, keep the location where the `throw' was seen. */
15745   if (flag_emit_xref)
15746     EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15747   return node;
15748 }
15749
15750 /* Check that exception said to be thrown by method DECL can be
15751    effectively caught from where DECL is invoked.  */
15752
15753 static void
15754 check_thrown_exceptions (location, decl)
15755      int location;
15756      tree decl;
15757 {
15758   tree throws;
15759   /* For all the unchecked exceptions thrown by DECL */
15760   for (throws = DECL_FUNCTION_THROWS (decl); throws;
15761        throws = TREE_CHAIN (throws))
15762     if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15763       {
15764 #if 1
15765         /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15766         if (DECL_NAME (decl) == get_identifier ("clone"))
15767           continue;
15768 #endif
15769         EXPR_WFL_LINECOL (wfl_operator) = location;
15770         if (DECL_FINIT_P (current_function_decl))
15771           parse_error_context
15772             (wfl_operator, "Exception `%s' can't be thrown in initializer",
15773              lang_printable_name (TREE_VALUE (throws), 0));
15774         else
15775           {
15776             parse_error_context
15777               (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
15778                lang_printable_name (TREE_VALUE (throws), 0),
15779                (DECL_INIT_P (current_function_decl) ?
15780                 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15781                 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15782           }
15783       }
15784 }
15785
15786 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15787    try-catch blocks, OR is listed in the `throws' clause of the
15788    current method.  */
15789
15790 static int
15791 check_thrown_exceptions_do (exception)
15792      tree exception;
15793 {
15794   tree list = currently_caught_type_list;
15795   resolve_and_layout (exception, NULL_TREE);
15796   /* First, all the nested try-catch-finally at that stage. The
15797      last element contains `throws' clause exceptions, if any. */
15798   if (IS_UNCHECKED_EXCEPTION_P (exception))
15799     return 1;
15800   while (list)
15801     {
15802       tree caught;
15803       for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15804         if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15805           return 1;
15806       list = TREE_CHAIN (list);
15807     }
15808   return 0;
15809 }
15810
15811 static void
15812 purge_unchecked_exceptions (mdecl)
15813      tree mdecl;
15814 {
15815   tree throws = DECL_FUNCTION_THROWS (mdecl);
15816   tree new = NULL_TREE;
15817
15818   while (throws)
15819     {
15820       tree next = TREE_CHAIN (throws);
15821       if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15822         {
15823           TREE_CHAIN (throws) = new;
15824           new = throws;
15825         }
15826       throws = next;
15827     }
15828   /* List is inverted here, but it doesn't matter */
15829   DECL_FUNCTION_THROWS (mdecl) = new;
15830 }
15831
15832 /* This function goes over all of CLASS_TYPE ctors and checks whether
15833    each of them features at least one unchecked exception in its
15834    `throws' clause. If it's the case, it returns `true', `false'
15835    otherwise.  */
15836
15837 static bool
15838 ctors_unchecked_throws_clause_p (class_type)
15839      tree class_type;
15840 {
15841   tree current;
15842
15843   for (current = TYPE_METHODS (class_type); current;
15844        current = TREE_CHAIN (current))
15845     {
15846       bool ctu = false; /* Ctor Throws Unchecked */
15847       if (DECL_CONSTRUCTOR_P (current))
15848         {
15849           tree throws;
15850           for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
15851                throws = TREE_CHAIN (throws))
15852             if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
15853               ctu = true;
15854         }
15855       /* We return false as we found one ctor that is unfit. */
15856       if (!ctu && DECL_CONSTRUCTOR_P (current))
15857         return false;
15858     }
15859   /* All ctors feature at least one unchecked exception in their
15860      `throws' clause. */
15861   return true;
15862 }
15863
15864 /* 15.24 Conditional Operator ?: */
15865
15866 static tree
15867 patch_conditional_expr (node, wfl_cond, wfl_op1)
15868      tree node, wfl_cond, wfl_op1;
15869 {
15870   tree cond = TREE_OPERAND (node, 0);
15871   tree op1 = TREE_OPERAND (node, 1);
15872   tree op2 = TREE_OPERAND (node, 2);
15873   tree resulting_type = NULL_TREE;
15874   tree t1, t2, patched;
15875   int error_found = 0;
15876
15877   /* Operands of ?: might be StringBuffers crafted as a result of a
15878      string concatenation. Obtain a descent operand here.  */
15879   if ((patched = patch_string (op1)))
15880     TREE_OPERAND (node, 1) = op1 = patched;
15881   if ((patched = patch_string (op2)))
15882     TREE_OPERAND (node, 2) = op2 = patched;
15883
15884   t1 = TREE_TYPE (op1);
15885   t2 = TREE_TYPE (op2);
15886
15887   /* The first expression must be a boolean */
15888   if (TREE_TYPE (cond) != boolean_type_node)
15889     {
15890       SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15891       parse_error_context (wfl_operator,
15892                "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
15893                            lang_printable_name (TREE_TYPE (cond), 0));
15894       error_found = 1;
15895     }
15896
15897   /* Second and third can be numeric, boolean (i.e. primitive),
15898      references or null. Anything else results in an error */
15899   if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15900         || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15901             && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15902         || (t1 == boolean_type_node && t2 == boolean_type_node)))
15903     error_found = 1;
15904
15905   /* Determine the type of the conditional expression. Same types are
15906      easy to deal with */
15907   else if (t1 == t2)
15908     resulting_type = t1;
15909
15910   /* There are different rules for numeric types */
15911   else if (JNUMERIC_TYPE_P (t1))
15912     {
15913       /* if byte/short found, the resulting type is short */
15914       if ((t1 == byte_type_node && t2 == short_type_node)
15915           || (t1 == short_type_node && t2 == byte_type_node))
15916         resulting_type = short_type_node;
15917
15918       /* If t1 is a constant int and t2 is of type byte, short or char
15919          and t1's value fits in t2, then the resulting type is t2 */
15920       else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15921           && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15922         resulting_type = t2;
15923
15924       /* If t2 is a constant int and t1 is of type byte, short or char
15925          and t2's value fits in t1, then the resulting type is t1 */
15926       else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15927           && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15928         resulting_type = t1;
15929
15930       /* Otherwise, binary numeric promotion is applied and the
15931          resulting type is the promoted type of operand 1 and 2 */
15932       else
15933         resulting_type = binary_numeric_promotion (t1, t2,
15934                                                    &TREE_OPERAND (node, 1),
15935                                                    &TREE_OPERAND (node, 2));
15936     }
15937
15938   /* Cases of a reference and a null type */
15939   else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15940     resulting_type = t1;
15941
15942   else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15943     resulting_type = t2;
15944
15945   /* Last case: different reference types. If a type can be converted
15946      into the other one by assignment conversion, the latter
15947      determines the type of the expression */
15948   else if ((resulting_type = try_reference_assignconv (t1, op2)))
15949     resulting_type = promote_type (t1);
15950
15951   else if ((resulting_type = try_reference_assignconv (t2, op1)))
15952     resulting_type = promote_type (t2);
15953
15954   /* If we don't have any resulting type, we're in trouble */
15955   if (!resulting_type)
15956     {
15957       char *t = xstrdup (lang_printable_name (t1, 0));
15958       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15959       parse_error_context (wfl_operator,
15960                  "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15961                            t, lang_printable_name (t2, 0));
15962       free (t);
15963       error_found = 1;
15964     }
15965
15966   if (error_found)
15967     {
15968       TREE_TYPE (node) = error_mark_node;
15969       return error_mark_node;
15970     }
15971
15972   TREE_TYPE (node) = resulting_type;
15973   TREE_SET_CODE (node, COND_EXPR);
15974   CAN_COMPLETE_NORMALLY (node) = 1;
15975   return node;
15976 }
15977
15978 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
15979
15980 static tree
15981 maybe_build_class_init_for_field (decl, expr)
15982     tree decl, expr;
15983 {
15984   tree clas = DECL_CONTEXT (decl);
15985   if (flag_emit_class_files || flag_emit_xref)
15986     return expr;
15987
15988   if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
15989       && FIELD_FINAL (decl))
15990     {
15991       tree init = DECL_INITIAL (decl);
15992       if (init != NULL_TREE)
15993         init = fold_constant_for_init (init, decl);
15994       if (init != NULL_TREE && CONSTANT_VALUE_P (init))
15995         return expr;
15996     }
15997
15998   return build_class_init (clas, expr);
15999 }
16000
16001 /* Try to constant fold NODE.
16002    If NODE is not a constant expression, return NULL_EXPR.
16003    CONTEXT is a static final VAR_DECL whose initializer we are folding. */
16004
16005 static tree
16006 fold_constant_for_init (node, context)
16007      tree node;
16008      tree context;
16009 {
16010   tree op0, op1, val;
16011   enum tree_code code = TREE_CODE (node);
16012
16013   switch (code)
16014     {
16015     case STRING_CST:
16016     case INTEGER_CST:
16017     case REAL_CST:
16018       return node;
16019
16020     case PLUS_EXPR:
16021     case MINUS_EXPR:
16022     case MULT_EXPR:
16023     case TRUNC_MOD_EXPR:
16024     case RDIV_EXPR:
16025     case LSHIFT_EXPR:
16026     case RSHIFT_EXPR:
16027     case URSHIFT_EXPR:
16028     case BIT_AND_EXPR:
16029     case BIT_XOR_EXPR:
16030     case BIT_IOR_EXPR:
16031     case TRUTH_ANDIF_EXPR:
16032     case TRUTH_ORIF_EXPR:
16033     case EQ_EXPR:
16034     case NE_EXPR:
16035     case GT_EXPR:
16036     case GE_EXPR:
16037     case LT_EXPR:
16038     case LE_EXPR:
16039       op0 = TREE_OPERAND (node, 0);
16040       op1 = TREE_OPERAND (node, 1);
16041       val = fold_constant_for_init (op0, context);
16042       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16043         return NULL_TREE;
16044       TREE_OPERAND (node, 0) = val;
16045       val = fold_constant_for_init (op1, context);
16046       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16047         return NULL_TREE;
16048       TREE_OPERAND (node, 1) = val;
16049       return patch_binop (node, op0, op1);
16050
16051     case UNARY_PLUS_EXPR:
16052     case NEGATE_EXPR:
16053     case TRUTH_NOT_EXPR:
16054     case BIT_NOT_EXPR:
16055     case CONVERT_EXPR:
16056       op0 = TREE_OPERAND (node, 0);
16057       val = fold_constant_for_init (op0, context);
16058       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16059         return NULL_TREE;
16060       TREE_OPERAND (node, 0) = val;
16061       return patch_unaryop (node, op0);
16062       break;
16063
16064     case COND_EXPR:
16065       val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
16066       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16067         return NULL_TREE;
16068       TREE_OPERAND (node, 0) = val;
16069       val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
16070       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16071         return NULL_TREE;
16072       TREE_OPERAND (node, 1) = val;
16073       val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
16074       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16075         return NULL_TREE;
16076       TREE_OPERAND (node, 2) = val;
16077       return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
16078         : TREE_OPERAND (node, 2);
16079
16080     case VAR_DECL:
16081     case FIELD_DECL:
16082       if (! FIELD_FINAL (node)
16083           || DECL_INITIAL (node) == NULL_TREE)
16084         return NULL_TREE;
16085       val = DECL_INITIAL (node);
16086       /* Guard against infinite recursion. */
16087       DECL_INITIAL (node) = NULL_TREE;
16088       val = fold_constant_for_init (val, node);
16089       DECL_INITIAL (node) = val;
16090       return val;
16091
16092     case EXPR_WITH_FILE_LOCATION:
16093       /* Compare java_complete_tree and resolve_expression_name. */
16094       if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
16095           || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
16096         {
16097           tree name = EXPR_WFL_NODE (node);
16098           tree decl;
16099           if (PRIMARY_P (node))
16100             return NULL_TREE;
16101           else if (! QUALIFIED_P (name))
16102             {
16103               decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
16104               if (decl == NULL_TREE
16105                   || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
16106                 return NULL_TREE;
16107               return fold_constant_for_init (decl, decl);
16108             }
16109           else
16110             {
16111               /* Install the proper context for the field resolution.
16112                  The prior context is restored once the name is
16113                  properly qualified. */
16114               tree saved_current_class = current_class;
16115               /* Wait until the USE_COMPONENT_REF re-write.  FIXME. */
16116               current_class = DECL_CONTEXT (context);
16117               qualify_ambiguous_name (node);
16118               current_class = saved_current_class;
16119               if (resolve_field_access (node, &decl, NULL)
16120                   && decl != NULL_TREE)
16121                 return fold_constant_for_init (decl, decl);
16122               return NULL_TREE;
16123             }
16124         }
16125       else
16126         {
16127           op0 = TREE_OPERAND (node, 0);
16128           val = fold_constant_for_init (op0, context);
16129           if (val == NULL_TREE || ! TREE_CONSTANT (val))
16130             return NULL_TREE;
16131           TREE_OPERAND (node, 0) = val;
16132           return val;
16133         }
16134
16135 #ifdef USE_COMPONENT_REF
16136     case IDENTIFIER:
16137     case COMPONENT_REF:
16138       ?;
16139 #endif
16140
16141     default:
16142       return NULL_TREE;
16143     }
16144 }
16145
16146 #ifdef USE_COMPONENT_REF
16147 /* Context is 'T' for TypeName, 'P' for PackageName,
16148    'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
16149
16150 tree
16151 resolve_simple_name (name, context)
16152      tree name;
16153      int context;
16154 {
16155 }
16156
16157 tree
16158 resolve_qualified_name (name, context)
16159      tree name;
16160      int context;
16161 {
16162 }
16163 #endif
16164
16165 /* Mark P, which is really a `struct parser_ctxt **' for GC.  */
16166
16167 static void
16168 mark_parser_ctxt (p)
16169      void *p;
16170 {
16171   struct parser_ctxt *pc = *((struct parser_ctxt **) p);
16172 #ifndef JC1_LITE
16173   size_t i;
16174 #endif
16175
16176   if (!pc)
16177     return;
16178
16179 #ifndef JC1_LITE
16180   for (i = 0; i < ARRAY_SIZE (pc->modifier_ctx); ++i)
16181     ggc_mark_tree (pc->modifier_ctx[i]);
16182   ggc_mark_tree (pc->class_type);
16183   ggc_mark_tree (pc->function_decl);
16184   ggc_mark_tree (pc->package);
16185   ggc_mark_tree (pc->class_list);
16186   ggc_mark_tree (pc->current_parsed_class);
16187   ggc_mark_tree (pc->current_parsed_class_un);
16188   ggc_mark_tree (pc->non_static_initialized);
16189   ggc_mark_tree (pc->static_initialized);
16190   ggc_mark_tree (pc->instance_initializers);
16191   ggc_mark_tree (pc->import_list);
16192   ggc_mark_tree (pc->import_demand_list);
16193   ggc_mark_tree (pc->current_loop);
16194   ggc_mark_tree (pc->current_labeled_block);
16195 #endif /* JC1_LITE */
16196
16197   if (pc->next)
16198     mark_parser_ctxt (&pc->next);
16199 }
16200
16201 void
16202 init_src_parse ()
16203 {
16204   /* Sanity check; we've been bit by this before.  */
16205   if (ARRAY_SIZE (ctxp->modifier_ctx) != MODIFIER_TK - PUBLIC_TK)
16206     abort ();
16207 }
16208
16209 \f
16210
16211 /* This section deals with the functions that are called when tables
16212    recording class initialization information are traversed.  */
16213
16214 /* Attach to PTR (a block) the declaration found in ENTRY. */
16215
16216 static int
16217 attach_init_test_initialization_flags (entry, ptr)
16218      PTR *entry;
16219      PTR ptr;
16220 {
16221   tree block = (tree)ptr;
16222   struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
16223
16224   TREE_CHAIN (ite->value) = BLOCK_EXPR_DECLS (block);
16225   BLOCK_EXPR_DECLS (block) = ite->value;
16226   return true;
16227 }
16228
16229 /* This function is called for each classes that is known definitely
16230    assigned when a given static method was called. This function
16231    augments a compound expression (INFO) storing all assignment to
16232    initialized static class flags if a flag already existed, otherwise
16233    a new one is created.  */
16234
16235 static int
16236 emit_test_initialization (entry_p, info)
16237      PTR *entry_p;
16238      PTR info;
16239 {
16240   tree l = (tree) info;
16241   tree decl, init;
16242   tree key = (tree) *entry_p;
16243   tree *ite;
16244   htab_t cf_ht = DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl);
16245
16246   /* If we haven't found a flag and we're dealing with self registered
16247      with current_function_decl, then don't do anything. Self is
16248      always added as definitely initialized but this information is
16249      valid only if used outside the current function. */
16250   if (current_function_decl == TREE_PURPOSE (l)
16251       && java_treetreehash_find (cf_ht, key) == NULL)
16252     return true;
16253
16254   ite = java_treetreehash_new (cf_ht, key);
16255
16256   /* If we don't have a variable, create one and install it. */
16257   if (*ite == NULL)
16258     {
16259       tree block;
16260
16261       decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16262       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16263       LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16264       DECL_CONTEXT (decl) = current_function_decl;
16265       DECL_INITIAL (decl) = boolean_true_node;
16266
16267       /* The trick is to find the right context for it. */
16268       block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16269       TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16270       BLOCK_EXPR_DECLS (block) = decl;
16271       *ite = decl;
16272     }
16273   else
16274     decl = *ite;
16275
16276   /* Now simply augment the compound that holds all the assignments
16277      pertaining to this method invocation. */
16278   init = build (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16279   TREE_SIDE_EFFECTS (init) = 1;
16280   TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16281   TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16282
16283   return true;
16284 }
16285
16286 #include "gt-java-parse.h"
16287 #include "gtype-java.h"