xref: /openbsd/gnu/usr.bin/gcc/gcc/java/parse.y (revision 4e43c760)
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, 2003 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 /* Local function prototypes */
73 static char *java_accstring_lookup PARAMS ((int));
74 static void  classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
75 static void  variable_redefinition_error PARAMS ((tree, tree, tree, int));
76 static tree  create_class PARAMS ((int, tree, tree, tree));
77 static tree  create_interface PARAMS ((int, tree, tree));
78 static void  end_class_declaration PARAMS ((int));
79 static tree  find_field PARAMS ((tree, tree));
80 static tree lookup_field_wrapper PARAMS ((tree, tree));
81 static int   duplicate_declaration_error_p PARAMS ((tree, tree, tree));
82 static void  register_fields PARAMS ((int, tree, tree));
83 static tree parser_qualified_classname PARAMS ((tree));
84 static int  parser_check_super PARAMS ((tree, tree, tree));
85 static int  parser_check_super_interface PARAMS ((tree, tree, tree));
86 static void check_modifiers_consistency PARAMS ((int));
87 static tree lookup_cl PARAMS ((tree));
88 static tree lookup_java_method2 PARAMS ((tree, tree, int));
89 static tree method_header PARAMS ((int, tree, tree, tree));
90 static void fix_method_argument_names PARAMS ((tree ,tree));
91 static tree method_declarator PARAMS ((tree, tree));
92 static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
93   ATTRIBUTE_PRINTF_2;
94 static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list))
95   ATTRIBUTE_PRINTF (2, 0);
96 static void parse_ctor_invocation_error PARAMS ((void));
97 static tree parse_jdk1_1_error PARAMS ((const char *));
98 static void complete_class_report_errors PARAMS ((jdep *));
99 static int process_imports PARAMS ((void));
100 static void read_import_dir PARAMS ((tree));
101 static int find_in_imports_on_demand PARAMS ((tree, tree));
102 static void find_in_imports PARAMS ((tree, tree));
103 static void check_inner_class_access PARAMS ((tree, tree, tree));
104 static int check_pkg_class_access PARAMS ((tree, tree, bool));
105 static void register_package PARAMS ((tree));
106 static tree resolve_package PARAMS ((tree, tree *, tree *));
107 static tree resolve_class PARAMS ((tree, tree, tree, tree));
108 static void declare_local_variables PARAMS ((int, tree, tree));
109 static void dump_java_tree PARAMS ((enum tree_dump_index, tree));
110 static void source_start_java_method PARAMS ((tree));
111 static void source_end_java_method PARAMS ((void));
112 static tree find_name_in_single_imports PARAMS ((tree));
113 static void check_abstract_method_header PARAMS ((tree));
114 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
115 static tree resolve_expression_name PARAMS ((tree, tree *));
116 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
117 static int check_class_interface_creation PARAMS ((int, int, tree,
118 						  tree, tree, tree));
119 static tree patch_method_invocation PARAMS ((tree, tree, tree, int,
120 					    int *, tree *));
121 static int breakdown_qualified PARAMS ((tree *, tree *, tree));
122 static int in_same_package PARAMS ((tree, tree));
123 static tree resolve_and_layout PARAMS ((tree, tree));
124 static tree qualify_and_find PARAMS ((tree, tree, tree));
125 static tree resolve_no_layout PARAMS ((tree, tree));
126 static int invocation_mode PARAMS ((tree, int));
127 static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
128 							    tree, tree));
129 static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
130 						   tree *, tree *));
131 static tree find_most_specific_methods_list PARAMS ((tree));
132 static int argument_types_convertible PARAMS ((tree, tree));
133 static tree patch_invoke PARAMS ((tree, tree, tree));
134 static int maybe_use_access_method PARAMS ((int, tree *, tree *));
135 static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
136 static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
137 static tree check_inner_circular_reference PARAMS ((tree, tree));
138 static tree check_circular_reference PARAMS ((tree));
139 static tree obtain_incomplete_type PARAMS ((tree));
140 static tree java_complete_lhs PARAMS ((tree));
141 static tree java_complete_tree PARAMS ((tree));
142 static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
143 static int analyze_clinit_body PARAMS ((tree, tree));
144 static int maybe_yank_clinit PARAMS ((tree));
145 static void start_complete_expand_method PARAMS ((tree));
146 static void java_complete_expand_method PARAMS ((tree));
147 static void java_expand_method_bodies PARAMS ((tree));
148 static int  unresolved_type_p PARAMS ((tree, tree *));
149 static void create_jdep_list PARAMS ((struct parser_ctxt *));
150 static tree build_expr_block PARAMS ((tree, tree));
151 static tree enter_block PARAMS ((void));
152 static tree exit_block PARAMS ((void));
153 static tree lookup_name_in_blocks PARAMS ((tree));
154 static void maybe_absorb_scoping_blocks PARAMS ((void));
155 static tree build_method_invocation PARAMS ((tree, tree));
156 static tree build_new_invocation PARAMS ((tree, tree));
157 static tree build_assignment PARAMS ((int, int, tree, tree));
158 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
159 static tree patch_assignment PARAMS ((tree, tree));
160 static tree patch_binop PARAMS ((tree, tree, tree));
161 static tree build_unaryop PARAMS ((int, int, tree));
162 static tree build_incdec PARAMS ((int, int, tree, int));
163 static tree patch_unaryop PARAMS ((tree, tree));
164 static tree build_cast PARAMS ((int, tree, tree));
165 static tree build_null_of_type PARAMS ((tree));
166 static tree patch_cast PARAMS ((tree, tree));
167 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
168 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
169 static int valid_cast_to_p PARAMS ((tree, tree));
170 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
171 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
172 static tree try_reference_assignconv PARAMS ((tree, tree));
173 static tree build_unresolved_array_type PARAMS ((tree));
174 static int build_type_name_from_array_name PARAMS ((tree, tree *));
175 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
176 static tree build_array_ref PARAMS ((int, tree, tree));
177 static tree patch_array_ref PARAMS ((tree));
178 static tree make_qualified_name PARAMS ((tree, tree, int));
179 static tree merge_qualified_name PARAMS ((tree, tree));
180 static tree make_qualified_primary PARAMS ((tree, tree, int));
181 static int resolve_qualified_expression_name PARAMS ((tree, tree *,
182 						     tree *, tree *));
183 static void qualify_ambiguous_name PARAMS ((tree));
184 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
185 static tree build_newarray_node PARAMS ((tree, tree, int));
186 static tree patch_newarray PARAMS ((tree));
187 static tree resolve_type_during_patch PARAMS ((tree));
188 static tree build_this PARAMS ((int));
189 static tree build_wfl_wrap PARAMS ((tree, int));
190 static tree build_return PARAMS ((int, tree));
191 static tree patch_return PARAMS ((tree));
192 static tree maybe_access_field PARAMS ((tree, tree, tree));
193 static int complete_function_arguments PARAMS ((tree));
194 static int check_for_static_method_reference PARAMS ((tree, tree, tree,
195 						      tree, tree));
196 static int not_accessible_p PARAMS ((tree, tree, tree, int));
197 static void check_deprecation PARAMS ((tree, tree));
198 static int class_in_current_package PARAMS ((tree));
199 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
200 static tree patch_if_else_statement PARAMS ((tree));
201 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
202 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
203 static tree patch_exit_expr PARAMS ((tree));
204 static tree build_labeled_block PARAMS ((int, tree));
205 static tree finish_labeled_statement PARAMS ((tree, tree));
206 static tree build_bc_statement PARAMS ((int, int, tree));
207 static tree patch_bc_statement PARAMS ((tree));
208 static tree patch_loop_statement PARAMS ((tree));
209 static tree build_new_loop PARAMS ((tree));
210 static tree build_loop_body PARAMS ((int, tree, int));
211 static tree finish_loop_body PARAMS ((int, tree, tree, int));
212 static tree build_debugable_stmt PARAMS ((int, tree));
213 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
214 static tree patch_switch_statement PARAMS ((tree));
215 static tree string_constant_concatenation PARAMS ((tree, tree));
216 static tree build_string_concatenation PARAMS ((tree, tree));
217 static tree patch_string_cst PARAMS ((tree));
218 static tree patch_string PARAMS ((tree));
219 static tree encapsulate_with_try_catch PARAMS ((int, tree, tree, tree));
220 static tree build_assertion PARAMS ((int, tree, tree));
221 static tree build_try_statement PARAMS ((int, tree, tree));
222 static tree build_try_finally_statement PARAMS ((int, tree, tree));
223 static tree patch_try_statement PARAMS ((tree));
224 static tree patch_synchronized_statement PARAMS ((tree, tree));
225 static tree patch_throw_statement PARAMS ((tree, tree));
226 static void check_thrown_exceptions PARAMS ((int, tree));
227 static int check_thrown_exceptions_do PARAMS ((tree));
228 static void purge_unchecked_exceptions PARAMS ((tree));
229 static bool ctors_unchecked_throws_clause_p PARAMS ((tree));
230 static void check_throws_clauses PARAMS ((tree, tree, tree));
231 static void finish_method_declaration PARAMS ((tree));
232 static tree build_super_invocation PARAMS ((tree));
233 static int verify_constructor_circularity PARAMS ((tree, tree));
234 static char *constructor_circularity_msg PARAMS ((tree, tree));
235 static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
236 							  int, int));
237 static const char *get_printable_method_name PARAMS ((tree));
238 static tree patch_conditional_expr PARAMS ((tree, tree, tree));
239 static tree generate_finit PARAMS ((tree));
240 static tree generate_instinit PARAMS ((tree));
241 static tree build_instinit_invocation PARAMS ((tree));
242 static void fix_constructors PARAMS ((tree));
243 static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
244 							    tree, int *));
245 static tree craft_constructor PARAMS ((tree, tree));
246 static int verify_constructor_super PARAMS ((tree));
247 static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
248 static void start_artificial_method_body PARAMS ((tree));
249 static void end_artificial_method_body PARAMS ((tree));
250 static int check_method_redefinition PARAMS ((tree, tree));
251 static int check_method_types_complete PARAMS ((tree));
252 static void java_check_regular_methods PARAMS ((tree));
253 static void java_check_abstract_methods PARAMS ((tree));
254 static void unreachable_stmt_error PARAMS ((tree));
255 static tree find_expr_with_wfl PARAMS ((tree));
256 static void missing_return_error PARAMS ((tree));
257 static tree build_new_array_init PARAMS ((int, tree));
258 static tree patch_new_array_init PARAMS ((tree, tree));
259 static tree maybe_build_array_element_wfl PARAMS ((tree));
260 static int array_constructor_check_entry PARAMS ((tree, tree));
261 static const char *purify_type_name PARAMS ((const char *));
262 static tree fold_constant_for_init PARAMS ((tree, tree));
263 static tree strip_out_static_field_access_decl PARAMS ((tree));
264 static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
265 static void static_ref_err PARAMS ((tree, tree, tree));
266 static void parser_add_interface PARAMS ((tree, tree, tree));
267 static void add_superinterfaces PARAMS ((tree, tree));
268 static tree jdep_resolve_class PARAMS ((jdep *));
269 static int note_possible_classname PARAMS ((const char *, int));
270 static void java_complete_expand_classes PARAMS ((void));
271 static void java_complete_expand_class PARAMS ((tree));
272 static void java_complete_expand_methods PARAMS ((tree));
273 static tree cut_identifier_in_qualified PARAMS ((tree));
274 static tree java_stabilize_reference PARAMS ((tree));
275 static tree do_unary_numeric_promotion PARAMS ((tree));
276 static char * operator_string PARAMS ((tree));
277 static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
278 static tree merge_string_cste PARAMS ((tree, tree, int));
279 static tree java_refold PARAMS ((tree));
280 static int java_decl_equiv PARAMS ((tree, tree));
281 static int binop_compound_p PARAMS ((enum tree_code));
282 static tree search_loop PARAMS ((tree));
283 static int labeled_block_contains_loop_p PARAMS ((tree, tree));
284 static int check_abstract_method_definitions PARAMS ((int, tree, tree));
285 static void java_check_abstract_method_definitions PARAMS ((tree));
286 static void java_debug_context_do PARAMS ((int));
287 static void java_parser_context_push_initialized_field PARAMS ((void));
288 static void java_parser_context_pop_initialized_field PARAMS ((void));
289 static tree reorder_static_initialized PARAMS ((tree));
290 static void java_parser_context_suspend PARAMS ((void));
291 static void java_parser_context_resume PARAMS ((void));
292 static int pop_current_osb PARAMS ((struct parser_ctxt *));
293 
294 /* JDK 1.1 work. FIXME */
295 
296 static tree maybe_make_nested_class_name PARAMS ((tree));
297 static int make_nested_class_name PARAMS ((tree));
298 static void set_nested_class_simple_name_value PARAMS ((tree, int));
299 static void link_nested_class_to_enclosing PARAMS ((void));
300 static tree resolve_inner_class PARAMS ((htab_t, tree, tree *, tree *, tree));
301 static tree find_as_inner_class PARAMS ((tree, tree, tree));
302 static tree find_as_inner_class_do PARAMS ((tree, tree));
303 static int check_inner_class_redefinition PARAMS ((tree, tree));
304 
305 static tree build_thisn_assign PARAMS ((void));
306 static tree build_current_thisn PARAMS ((tree));
307 static tree build_access_to_thisn PARAMS ((tree, tree, int));
308 static tree maybe_build_thisn_access_method PARAMS ((tree));
309 
310 static tree build_outer_field_access PARAMS ((tree, tree));
311 static tree build_outer_field_access_methods PARAMS ((tree));
312 static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
313 						  tree, tree));
314 static tree build_outer_method_access_method PARAMS ((tree));
315 static tree build_new_access_id PARAMS ((void));
316 static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
317 						    tree, tree));
318 
319 static int outer_field_access_p PARAMS ((tree, tree));
320 static int outer_field_expanded_access_p PARAMS ((tree, tree *,
321 						 tree *, tree *));
322 static tree outer_field_access_fix PARAMS ((tree, tree, tree));
323 static tree build_incomplete_class_ref PARAMS ((int, tree));
324 static tree patch_incomplete_class_ref PARAMS ((tree));
325 static tree create_anonymous_class PARAMS ((int, tree));
326 static void patch_anonymous_class PARAMS ((tree, tree, tree));
327 static void add_inner_class_fields PARAMS ((tree, tree));
328 
329 static tree build_dot_class_method PARAMS ((tree));
330 static tree build_dot_class_method_invocation PARAMS ((tree));
331 static void create_new_parser_context PARAMS ((int));
332 static void mark_parser_ctxt PARAMS ((void *));
333 static tree maybe_build_class_init_for_field PARAMS ((tree, tree));
334 
335 static int attach_init_test_initialization_flags PARAMS ((PTR *, PTR));
336 static int emit_test_initialization PARAMS ((PTR *, PTR));
337 
338 static char *string_convert_int_cst PARAMS ((tree));
339 
340 /* Number of error found so far. */
341 int java_error_count;
342 /* Number of warning found so far. */
343 int java_warning_count;
344 /* Tell when not to fold, when doing xrefs */
345 int do_not_fold;
346 /* Cyclic inheritance report, as it can be set by layout_class */
347 const char *cyclic_inheritance_report;
348 
349 /* The current parser context */
350 struct parser_ctxt *ctxp;
351 
352 /* List of things that were analyzed for which code will be generated */
353 struct parser_ctxt *ctxp_for_generation = NULL;
354 
355 /* binop_lookup maps token to tree_code. It is used where binary
356    operations are involved and required by the parser. RDIV_EXPR
357    covers both integral/floating point division. The code is changed
358    once the type of both operator is worked out.  */
359 
360 static const enum tree_code binop_lookup[19] =
361   {
362     PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
363     LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
364     BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
365     TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
366     EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
367    };
368 #define BINOP_LOOKUP(VALUE) 						\
369   binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
370 
371 /* This is the end index for binary operators that can also be used
372    in compound assignements. */
373 #define BINOP_COMPOUND_CANDIDATES 11
374 
375 /* The "$L" identifier we use to create labels.  */
376 static GTY(()) tree label_id;
377 
378 /* The "StringBuffer" identifier used for the String `+' operator. */
379 static GTY(()) tree wfl_string_buffer;
380 
381 /* The "append" identifier used for String `+' operator.  */
382 static GTY(()) tree wfl_append;
383 
384 /* The "toString" identifier used for String `+' operator. */
385 static GTY(()) tree wfl_to_string;
386 
387 /* The "java.lang" import qualified name.  */
388 static GTY(()) tree java_lang_id;
389 
390 /* The generated `inst$' identifier used for generated enclosing
391    instance/field access functions.  */
392 static GTY(()) tree inst_id;
393 
394 /* Context and flag for static blocks */
395 static GTY(()) tree current_static_block;
396 
397 /* The generated `write_parm_value$' identifier.  */
398 static GTY(()) tree wpv_id;
399 
400 /* The list of all packages we've seen so far */
401 static GTY(()) tree package_list;
402 
403 /* Hold THIS for the scope of the current method decl.  */
404 static GTY(()) tree current_this;
405 
406 /* Hold a list of catch clauses list. The first element of this list is
407    the list of the catch clauses of the currently analysed try block. */
408 static GTY(()) tree currently_caught_type_list;
409 
410 /* This holds a linked list of all the case labels for the current
411    switch statement.  It is only used when checking to see if there
412    are duplicate labels.  FIXME: probably this should just be attached
413    to the switch itself; then it could be referenced via
414    `ctxp->current_loop'.  */
415 static GTY(()) tree case_label_list;
416 
417 /* Anonymous class counter. Will be reset to 1 every time a non
418    anonymous class gets created. */
419 static int anonymous_class_counter = 1;
420 
421 static GTY(()) tree src_parse_roots[1];
422 
423 /* All classes seen from source code */
424 #define gclass_list src_parse_roots[0]
425 
426 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
427    line and point it out.  */
428 /* Should point out the one that don't fit. ASCII/unicode, going
429    backward. FIXME */
430 
431 #define check_modifiers(__message, __value, __mask) do {	\
432   if ((__value) & ~(__mask))					\
433     {								\
434       size_t i, remainder = (__value) & ~(__mask);	       	\
435       for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)	\
436         if ((1 << i) & remainder)				\
437 	  parse_error_context (ctxp->modifier_ctx [i], (__message), \
438 			       java_accstring_lookup (1 << i)); \
439     }								\
440 } while (0)
441 
442 %}
443 
444 %union {
445   tree node;
446   int sub_token;
447   struct {
448     int token;
449     int location;
450   } operator;
451   int value;
452 }
453 
454 %{
455 #include "lex.c"
456 %}
457 
458 %pure_parser
459 
460 /* Things defined here have to match the order of what's in the
461    binop_lookup table.  */
462 
463 %token   PLUS_TK         MINUS_TK        MULT_TK         DIV_TK    REM_TK
464 %token   LS_TK           SRS_TK          ZRS_TK
465 %token   AND_TK          XOR_TK          OR_TK
466 %token   BOOL_AND_TK BOOL_OR_TK
467 %token   EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
468 
469 /* This maps to the same binop_lookup entry than the token above */
470 
471 %token   PLUS_ASSIGN_TK  MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
472 %token   REM_ASSIGN_TK
473 %token   LS_ASSIGN_TK    SRS_ASSIGN_TK   ZRS_ASSIGN_TK
474 %token   AND_ASSIGN_TK   XOR_ASSIGN_TK   OR_ASSIGN_TK
475 
476 
477 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
478 
479 %token   PUBLIC_TK       PRIVATE_TK         PROTECTED_TK
480 %token   STATIC_TK       FINAL_TK           SYNCHRONIZED_TK
481 %token   VOLATILE_TK     TRANSIENT_TK       NATIVE_TK
482 %token   PAD_TK          ABSTRACT_TK        STRICT_TK
483 %token   MODIFIER_TK
484 
485 /* Keep those two in order, too */
486 %token   DECR_TK INCR_TK
487 
488 /* From now one, things can be in any order */
489 
490 %token   DEFAULT_TK      IF_TK              THROW_TK
491 %token   BOOLEAN_TK      DO_TK              IMPLEMENTS_TK
492 %token   THROWS_TK       BREAK_TK           IMPORT_TK
493 %token   ELSE_TK         INSTANCEOF_TK      RETURN_TK
494 %token   VOID_TK         CATCH_TK           INTERFACE_TK
495 %token   CASE_TK         EXTENDS_TK         FINALLY_TK
496 %token   SUPER_TK        WHILE_TK           CLASS_TK
497 %token   SWITCH_TK       CONST_TK           TRY_TK
498 %token   FOR_TK          NEW_TK             CONTINUE_TK
499 %token   GOTO_TK         PACKAGE_TK         THIS_TK
500 %token   ASSERT_TK
501 
502 %token   BYTE_TK         SHORT_TK           INT_TK            LONG_TK
503 %token   CHAR_TK         INTEGRAL_TK
504 
505 %token   FLOAT_TK        DOUBLE_TK          FP_TK
506 
507 %token   ID_TK
508 
509 %token   REL_QM_TK         REL_CL_TK NOT_TK  NEG_TK
510 
511 %token   ASSIGN_ANY_TK   ASSIGN_TK
512 %token   OP_TK  CP_TK  OCB_TK  CCB_TK  OSB_TK  CSB_TK  SC_TK  C_TK DOT_TK
513 
514 %token   STRING_LIT_TK   CHAR_LIT_TK        INT_LIT_TK        FP_LIT_TK
515 %token   TRUE_TK         FALSE_TK           BOOL_LIT_TK       NULL_TK
516 
517 %type    <value>	modifiers MODIFIER_TK final synchronized
518 
519 %type    <node>		super ID_TK identifier
520 %type    <node>		name simple_name qualified_name
521 %type	 <node>		type_declaration compilation_unit
522 			field_declaration method_declaration extends_interfaces
523                         interfaces interface_type_list
524                         import_declarations package_declaration
525                         type_declarations interface_body
526 			interface_member_declaration constant_declaration
527 			interface_member_declarations interface_type
528 			abstract_method_declaration
529 %type	 <node>		class_body_declaration class_member_declaration
530 			static_initializer constructor_declaration block
531 %type	 <node>		class_body_declarations constructor_header
532 %type    <node>		class_or_interface_type class_type class_type_list
533 			constructor_declarator explicit_constructor_invocation
534 %type    <node>         dim_expr dim_exprs this_or_super throws
535 
536 %type	 <node>		variable_declarator_id variable_declarator
537 			variable_declarators variable_initializer
538 			variable_initializers constructor_body
539 			array_initializer
540 
541 %type	 <node>		class_body block_end constructor_block_end
542 %type	 <node>		statement statement_without_trailing_substatement
543 			labeled_statement if_then_statement label_decl
544 			if_then_else_statement while_statement for_statement
545 			statement_nsi labeled_statement_nsi do_statement
546 			if_then_else_statement_nsi while_statement_nsi
547 			for_statement_nsi statement_expression_list for_init
548 			for_update statement_expression expression_statement
549 			primary_no_new_array expression primary
550 			array_creation_expression array_type
551 			class_instance_creation_expression field_access
552 			method_invocation array_access something_dot_new
553 			argument_list postfix_expression while_expression
554 			post_increment_expression post_decrement_expression
555 			unary_expression_not_plus_minus unary_expression
556 			pre_increment_expression pre_decrement_expression
557 			cast_expression
558 			multiplicative_expression additive_expression
559 			shift_expression relational_expression
560 			equality_expression and_expression
561 			exclusive_or_expression inclusive_or_expression
562 			conditional_and_expression conditional_or_expression
563 			conditional_expression assignment_expression
564 			left_hand_side assignment for_header for_begin
565 			constant_expression do_statement_begin empty_statement
566 			switch_statement synchronized_statement throw_statement
567 			try_statement assert_statement
568 			switch_expression switch_block
569 			catches catch_clause catch_clause_parameter finally
570 			anonymous_class_creation trap_overflow_corner_case
571 %type    <node>         return_statement break_statement continue_statement
572 
573 %type    <operator>     ASSIGN_TK      MULT_ASSIGN_TK  DIV_ASSIGN_TK
574 %type    <operator>     REM_ASSIGN_TK  PLUS_ASSIGN_TK  MINUS_ASSIGN_TK
575 %type    <operator>     LS_ASSIGN_TK   SRS_ASSIGN_TK   ZRS_ASSIGN_TK
576 %type    <operator>     AND_ASSIGN_TK  XOR_ASSIGN_TK   OR_ASSIGN_TK
577 %type    <operator>     ASSIGN_ANY_TK  assignment_operator
578 %token   <operator>     EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
579 %token   <operator>     BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
580 %token   <operator>     DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
581 %token   <operator>     NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
582 %token   <operator>     OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
583 %type    <operator>	THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
584 %type	 <operator>     CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
585 %type	 <operator>     NEW_TK ASSERT_TK
586 
587 %type	 <node>		method_body
588 
589 %type    <node>		literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
590 			STRING_LIT_TK NULL_TK VOID_TK
591 
592 %type	 <node>		IF_TK WHILE_TK FOR_TK
593 
594 %type    <node>         formal_parameter_list formal_parameter
595                         method_declarator method_header
596 
597 %type	 <node>		primitive_type reference_type type
598 			BOOLEAN_TK INTEGRAL_TK FP_TK
599 
600 /* Added or modified JDK 1.1 rule types  */
601 %type	 <node>		type_literals
602 
603 %%
604 /* 19.2 Production from 2.3: The Syntactic Grammar  */
605 goal:
606                 {
607 		  /* Register static variables with the garbage
608 		     collector.  */
609 		  ggc_add_root (&ctxp, 1,
610 				sizeof (struct parser_ctxt *),
611 				mark_parser_ctxt);
612 		  ggc_add_root (&ctxp_for_generation, 1,
613 				sizeof (struct parser_ctxt *),
614 				mark_parser_ctxt);
615 		}
616 	compilation_unit
617 		{}
618 ;
619 
620 /* 19.3 Productions from 3: Lexical structure  */
621 literal:
622 	INT_LIT_TK
623 |	FP_LIT_TK
624 |	BOOL_LIT_TK
625 |	CHAR_LIT_TK
626 |       STRING_LIT_TK
627 |       NULL_TK
628 ;
629 
630 /* 19.4 Productions from 4: Types, Values and Variables  */
631 type:
632 	primitive_type
633 |	reference_type
634 ;
635 
636 primitive_type:
637 	INTEGRAL_TK
638 |	FP_TK
639 |	BOOLEAN_TK
640 ;
641 
642 reference_type:
643 	class_or_interface_type
644 |	array_type
645 ;
646 
647 class_or_interface_type:
648 	name
649 ;
650 
651 class_type:
652 	class_or_interface_type	/* Default rule */
653 ;
654 
655 interface_type:
656 	 class_or_interface_type
657 ;
658 
659 array_type:
660 	primitive_type dims
661 		{
662 		  int osb = pop_current_osb (ctxp);
663 		  tree t = build_java_array_type (($1), -1);
664 		  while (--osb)
665 		    t = build_unresolved_array_type (t);
666 		  $$ = t;
667 		}
668 |	name dims
669 		{
670 		  int osb = pop_current_osb (ctxp);
671 		  tree t = $1;
672 		  while (osb--)
673 		    t = build_unresolved_array_type (t);
674 		  $$ = t;
675 		}
676 ;
677 
678 /* 19.5 Productions from 6: Names  */
679 name:
680 	simple_name		/* Default rule */
681 |	qualified_name		/* Default rule */
682 ;
683 
684 simple_name:
685 	identifier		/* Default rule */
686 ;
687 
688 qualified_name:
689 	name DOT_TK identifier
690 		{ $$ = make_qualified_name ($1, $3, $2.location); }
691 ;
692 
693 identifier:
694 	ID_TK
695 ;
696 
697 /* 19.6: Production from 7: Packages  */
698 compilation_unit:
699 		{$$ = NULL;}
700 |	package_declaration
701 |	import_declarations
702 |	type_declarations
703 |       package_declaration import_declarations
704 |       package_declaration type_declarations
705 |       import_declarations type_declarations
706 |       package_declaration import_declarations type_declarations
707 ;
708 
709 import_declarations:
710 	import_declaration
711 		{
712 		  $$ = NULL;
713 		}
714 |	import_declarations import_declaration
715 		{
716 		  $$ = NULL;
717 		}
718 ;
719 
720 type_declarations:
721 	type_declaration
722 | 	type_declarations type_declaration
723 ;
724 
725 package_declaration:
726 	PACKAGE_TK name SC_TK
727 		{
728 		  ctxp->package = EXPR_WFL_NODE ($2);
729 		  register_package (ctxp->package);
730 		}
731 |	PACKAGE_TK error
732 		{yyerror ("Missing name"); RECOVER;}
733 |	PACKAGE_TK name error
734 		{yyerror ("';' expected"); RECOVER;}
735 ;
736 
737 import_declaration:
738 	single_type_import_declaration
739 |	type_import_on_demand_declaration
740 ;
741 
742 single_type_import_declaration:
743 	IMPORT_TK name SC_TK
744 		{
745 		  tree name = EXPR_WFL_NODE ($2), last_name;
746 		  int   i = IDENTIFIER_LENGTH (name)-1;
747 		  const char *last = &IDENTIFIER_POINTER (name)[i];
748 		  while (last != IDENTIFIER_POINTER (name))
749 		    {
750 		      if (last [0] == '.')
751 			break;
752 		      last--;
753 		    }
754 		  last_name = get_identifier (++last);
755 		  if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
756 		    {
757 		      tree err = find_name_in_single_imports (last_name);
758 		      if (err && err != name)
759 			parse_error_context
760 			  ($2, "Ambiguous class: `%s' and `%s'",
761 			   IDENTIFIER_POINTER (name),
762 			   IDENTIFIER_POINTER (err));
763 		      else
764 			REGISTER_IMPORT ($2, last_name);
765 		    }
766 		  else
767 		    REGISTER_IMPORT ($2, last_name);
768 		}
769 |	IMPORT_TK error
770 		{yyerror ("Missing name"); RECOVER;}
771 |	IMPORT_TK name error
772 		{yyerror ("';' expected"); RECOVER;}
773 ;
774 
775 type_import_on_demand_declaration:
776 	IMPORT_TK name DOT_TK MULT_TK SC_TK
777 		{
778 		  tree name = EXPR_WFL_NODE ($2);
779 		  tree it;
780 		  /* Search for duplicates. */
781 		  for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
782 		    if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
783 		      break;
784 		  /* Don't import the same thing more than once, just ignore
785 		     duplicates (7.5.2) */
786 		  if (! it)
787 		    {
788 		      read_import_dir ($2);
789 		      ctxp->import_demand_list =
790 			chainon (ctxp->import_demand_list,
791 				 build_tree_list ($2, NULL_TREE));
792 		    }
793 		}
794 |	IMPORT_TK name DOT_TK error
795 		{yyerror ("'*' expected"); RECOVER;}
796 |	IMPORT_TK name DOT_TK MULT_TK error
797 		{yyerror ("';' expected"); RECOVER;}
798 ;
799 
800 type_declaration:
801 	class_declaration
802 		{ end_class_declaration (0); }
803 |	interface_declaration
804 		{ end_class_declaration (0); }
805 |	empty_statement
806 |	error
807 		{
808 		  YYERROR_NOW;
809 		  yyerror ("Class or interface declaration expected");
810 		}
811 ;
812 
813 /* 19.7 Shortened from the original:
814    modifiers: modifier | modifiers modifier
815    modifier: any of public...  */
816 modifiers:
817 	MODIFIER_TK
818 		{
819 		  $$ = (1 << $1);
820 		}
821 |	modifiers MODIFIER_TK
822 		{
823 		  int acc = (1 << $2);
824 		  if ($$ & acc)
825 		    parse_error_context
826 		      (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
827 		       java_accstring_lookup (acc));
828 		  else
829 		    {
830 		      $$ |= acc;
831 		    }
832 		}
833 ;
834 
835 /* 19.8.1 Production from $8.1: Class Declaration */
836 class_declaration:
837 	modifiers CLASS_TK identifier super interfaces
838 		{ create_class ($1, $3, $4, $5); }
839 	class_body
840 		{;}
841 |	CLASS_TK identifier super interfaces
842 		{ create_class (0, $2, $3, $4); }
843 	class_body
844 		{;}
845 |	modifiers CLASS_TK error
846 		{ yyerror ("Missing class name"); RECOVER; }
847 |	CLASS_TK error
848 		{ yyerror ("Missing class name"); RECOVER; }
849 |       CLASS_TK identifier error
850 		{
851 		  if (!ctxp->class_err) yyerror ("'{' expected");
852 		  DRECOVER(class1);
853 		}
854 |       modifiers CLASS_TK identifier error
855 		{ if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER; }
856 ;
857 
858 super:
859 		{ $$ = NULL; }
860 |	EXTENDS_TK class_type
861 		{ $$ = $2; }
862 |	EXTENDS_TK class_type error
863 		{yyerror ("'{' expected"); ctxp->class_err=1;}
864 |	EXTENDS_TK error
865 		{yyerror ("Missing super class name"); ctxp->class_err=1;}
866 ;
867 
868 interfaces:
869 		{ $$ = NULL_TREE; }
870 |	IMPLEMENTS_TK interface_type_list
871 		{ $$ = $2; }
872 |	IMPLEMENTS_TK error
873 		{
874 		  ctxp->class_err=1;
875 		  yyerror ("Missing interface name");
876 		}
877 ;
878 
879 interface_type_list:
880 	interface_type
881 		{
882 		  ctxp->interface_number = 1;
883 		  $$ = build_tree_list ($1, NULL_TREE);
884 		}
885 |	interface_type_list C_TK interface_type
886 		{
887 		  ctxp->interface_number++;
888 		  $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
889 		}
890 |	interface_type_list C_TK error
891 		{yyerror ("Missing interface name"); RECOVER;}
892 ;
893 
894 class_body:
895 	OCB_TK CCB_TK
896 		{
897 		  /* Store the location of the `}' when doing xrefs */
898 		  if (flag_emit_xref)
899 		    DECL_END_SOURCE_LINE (GET_CPC ()) =
900 		      EXPR_WFL_ADD_COL ($2.location, 1);
901 		  $$ = GET_CPC ();
902 		}
903 |	OCB_TK class_body_declarations CCB_TK
904 		{
905 		  /* Store the location of the `}' when doing xrefs */
906 		  if (flag_emit_xref)
907 		    DECL_END_SOURCE_LINE (GET_CPC ()) =
908 		      EXPR_WFL_ADD_COL ($3.location, 1);
909 		  $$ = GET_CPC ();
910 		}
911 ;
912 
913 class_body_declarations:
914 	class_body_declaration
915 |	class_body_declarations class_body_declaration
916 ;
917 
918 class_body_declaration:
919 	class_member_declaration
920 |	static_initializer
921 |	constructor_declaration
922 |	block			/* Added, JDK1.1, instance initializer */
923 		{
924 		  if ($1 != empty_stmt_node)
925 		    {
926 		      TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
927 		      SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
928 		    }
929 		}
930 ;
931 
932 class_member_declaration:
933 	field_declaration
934 |	method_declaration
935 |	class_declaration	/* Added, JDK1.1 inner classes */
936 		{ end_class_declaration (1); }
937 |	interface_declaration	/* Added, JDK1.1 inner interfaces */
938 		{ end_class_declaration (1); }
939 |	empty_statement
940 ;
941 
942 /* 19.8.2 Productions from 8.3: Field Declarations  */
943 field_declaration:
944 	type variable_declarators SC_TK
945 		{ register_fields (0, $1, $2); }
946 |	modifiers type variable_declarators SC_TK
947 		{
948 		  check_modifiers
949 		    ("Illegal modifier `%s' for field declaration",
950 		     $1, FIELD_MODIFIERS);
951 		  check_modifiers_consistency ($1);
952 		  register_fields ($1, $2, $3);
953 		}
954 ;
955 
956 variable_declarators:
957 	/* Should we use build_decl_list () instead ? FIXME */
958 	variable_declarator	/* Default rule */
959 |	variable_declarators C_TK variable_declarator
960 		{ $$ = chainon ($1, $3); }
961 |	variable_declarators C_TK error
962 		{yyerror ("Missing term"); RECOVER;}
963 ;
964 
965 variable_declarator:
966 	variable_declarator_id
967 		{ $$ = build_tree_list ($1, NULL_TREE); }
968 |	variable_declarator_id ASSIGN_TK variable_initializer
969 		{
970 		  if (java_error_count)
971 		    $3 = NULL_TREE;
972 		  $$ = build_tree_list
973 		    ($1, build_assignment ($2.token, $2.location, $1, $3));
974 		}
975 |	variable_declarator_id ASSIGN_TK error
976 		{
977 		  yyerror ("Missing variable initializer");
978 		  $$ = build_tree_list ($1, NULL_TREE);
979 		  RECOVER;
980 		}
981 |	variable_declarator_id ASSIGN_TK variable_initializer error
982 		{
983 		  yyerror ("';' expected");
984 		  $$ = build_tree_list ($1, NULL_TREE);
985 		  RECOVER;
986 		}
987 ;
988 
989 variable_declarator_id:
990 	identifier
991 |	variable_declarator_id OSB_TK CSB_TK
992 		{ $$ = build_unresolved_array_type ($1); }
993 |	identifier error
994 		{yyerror ("Invalid declaration"); DRECOVER(vdi);}
995 |	variable_declarator_id OSB_TK error
996 		{
997 		  yyerror ("']' expected");
998 		  DRECOVER(vdi);
999 		}
1000 |	variable_declarator_id CSB_TK error
1001 		{yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1002 ;
1003 
1004 variable_initializer:
1005 	expression
1006 |	array_initializer
1007 ;
1008 
1009 /* 19.8.3 Productions from 8.4: Method Declarations  */
1010 method_declaration:
1011 	method_header
1012 		{
1013 		  current_function_decl = $1;
1014 		  if (current_function_decl
1015 		      && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1016 		    source_start_java_method (current_function_decl);
1017 		  else
1018 		    current_function_decl = NULL_TREE;
1019 		}
1020 	method_body
1021 		{ finish_method_declaration ($3); }
1022 |	method_header error
1023 		{YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1024 ;
1025 
1026 method_header:
1027 	type method_declarator throws
1028 		{ $$ = method_header (0, $1, $2, $3); }
1029 |	VOID_TK method_declarator throws
1030 		{ $$ = method_header (0, void_type_node, $2, $3); }
1031 |	modifiers type method_declarator throws
1032 		{ $$ = method_header ($1, $2, $3, $4); }
1033 |	modifiers VOID_TK method_declarator throws
1034 		{ $$ = method_header ($1, void_type_node, $3, $4); }
1035 |	type error
1036 		{
1037 		  yyerror ("Invalid method declaration, method name required");
1038 		  RECOVER;
1039 		}
1040 |	modifiers type error
1041 		{
1042 		  yyerror ("Identifier expected");
1043 		  RECOVER;
1044 		}
1045 |	VOID_TK error
1046 		{
1047 		  yyerror ("Identifier expected");
1048 		  RECOVER;
1049 		}
1050 |	modifiers VOID_TK error
1051 		{
1052 		  yyerror ("Identifier expected");
1053 		  RECOVER;
1054 		}
1055 |	modifiers error
1056 		{
1057 		  yyerror ("Invalid method declaration, return type required");
1058 		  RECOVER;
1059 		}
1060 ;
1061 
1062 method_declarator:
1063 	identifier OP_TK CP_TK
1064 		{
1065 		  ctxp->formal_parameter_number = 0;
1066 		  $$ = method_declarator ($1, NULL_TREE);
1067 		}
1068 |	identifier OP_TK formal_parameter_list CP_TK
1069 		{ $$ = method_declarator ($1, $3); }
1070 |	method_declarator OSB_TK CSB_TK
1071 		{
1072 		  EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1073 		  TREE_PURPOSE ($1) =
1074 		    build_unresolved_array_type (TREE_PURPOSE ($1));
1075 		  parse_warning_context
1076 		    (wfl_operator,
1077 		     "Discouraged form of returned type specification");
1078 		}
1079 |	identifier OP_TK error
1080 		{yyerror ("')' expected"); DRECOVER(method_declarator);}
1081 |	method_declarator OSB_TK error
1082 		{yyerror ("']' expected"); RECOVER;}
1083 ;
1084 
1085 formal_parameter_list:
1086 	formal_parameter
1087 		{
1088 		  ctxp->formal_parameter_number = 1;
1089 		}
1090 |	formal_parameter_list C_TK formal_parameter
1091 		{
1092 		  ctxp->formal_parameter_number += 1;
1093 		  $$ = chainon ($1, $3);
1094 		}
1095 |	formal_parameter_list C_TK error
1096 		{ yyerror ("Missing formal parameter term"); RECOVER; }
1097 ;
1098 
1099 formal_parameter:
1100 	type variable_declarator_id
1101 		{
1102 		  $$ = build_tree_list ($2, $1);
1103 		}
1104 |	final type variable_declarator_id /* Added, JDK1.1 final parms */
1105 		{
1106 		  $$ = build_tree_list ($3, $2);
1107 		  ARG_FINAL_P ($$) = 1;
1108 		}
1109 |	type error
1110 		{
1111 		  yyerror ("Missing identifier"); RECOVER;
1112 		  $$ = NULL_TREE;
1113 		}
1114 |	final type error
1115 		{
1116 		  yyerror ("Missing identifier"); RECOVER;
1117 		  $$ = NULL_TREE;
1118 		}
1119 ;
1120 
1121 final:
1122 	modifiers
1123 		{
1124 		  check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1125 				   $1, ACC_FINAL);
1126 		  if ($1 != ACC_FINAL)
1127 		    MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1128 		}
1129 ;
1130 
1131 throws:
1132 		{ $$ = NULL_TREE; }
1133 |	THROWS_TK class_type_list
1134 		{ $$ = $2; }
1135 |	THROWS_TK error
1136 		{yyerror ("Missing class type term"); RECOVER;}
1137 ;
1138 
1139 class_type_list:
1140 	class_type
1141 		{ $$ = build_tree_list ($1, $1); }
1142 |	class_type_list C_TK class_type
1143 		{ $$ = tree_cons ($3, $3, $1); }
1144 |	class_type_list C_TK error
1145 		{yyerror ("Missing class type term"); RECOVER;}
1146 ;
1147 
1148 method_body:
1149 	block
1150 |	SC_TK { $$ = NULL_TREE; }
1151 ;
1152 
1153 /* 19.8.4 Productions from 8.5: Static Initializers  */
1154 static_initializer:
1155 	static block
1156 		{
1157 		  TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1158 		  SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1159 		  current_static_block = NULL_TREE;
1160 		}
1161 ;
1162 
1163 static:				/* Test lval.sub_token here */
1164 	modifiers
1165 		{
1166 		  check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1167 		  /* Can't have a static initializer in an innerclass */
1168 		  if ($1 | ACC_STATIC &&
1169 		      GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1170 		    parse_error_context
1171 		      (MODIFIER_WFL (STATIC_TK),
1172 		       "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1173 		       IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1174 		  SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1175 		}
1176 ;
1177 
1178 /* 19.8.5 Productions from 8.6: Constructor Declarations  */
1179 constructor_declaration:
1180 	constructor_header
1181 		{
1182 		  current_function_decl = $1;
1183 		  source_start_java_method (current_function_decl);
1184 		}
1185 	constructor_body
1186 		{ finish_method_declaration ($3); }
1187 ;
1188 
1189 constructor_header:
1190 	constructor_declarator throws
1191 		{ $$ = method_header (0, NULL_TREE, $1, $2); }
1192 |	modifiers constructor_declarator throws
1193 		{ $$ = method_header ($1, NULL_TREE, $2, $3); }
1194 ;
1195 
1196 constructor_declarator:
1197 	simple_name OP_TK CP_TK
1198 		{
1199 		  ctxp->formal_parameter_number = 0;
1200 		  $$ = method_declarator ($1, NULL_TREE);
1201 		}
1202 |	simple_name OP_TK formal_parameter_list CP_TK
1203 		{ $$ = method_declarator ($1, $3); }
1204 ;
1205 
1206 constructor_body:
1207 	/* Unlike regular method, we always need a complete (empty)
1208 	   body so we can safely perform all the required code
1209 	   addition (super invocation and field initialization) */
1210 	block_begin constructor_block_end
1211 		{
1212 		  BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1213 		  $$ = $2;
1214 		}
1215 |	block_begin explicit_constructor_invocation constructor_block_end
1216 		{ $$ = $3; }
1217 |	block_begin block_statements constructor_block_end
1218 		{ $$ = $3; }
1219 |       block_begin explicit_constructor_invocation block_statements constructor_block_end
1220 		{ $$ = $4; }
1221 ;
1222 
1223 constructor_block_end:
1224 	block_end
1225 ;
1226 
1227 /* Error recovery for that rule moved down expression_statement: rule.  */
1228 explicit_constructor_invocation:
1229 	this_or_super OP_TK CP_TK SC_TK
1230 		{
1231 		  $$ = build_method_invocation ($1, NULL_TREE);
1232 		  $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1233 		  $$ = java_method_add_stmt (current_function_decl, $$);
1234 		}
1235 |	this_or_super OP_TK argument_list CP_TK SC_TK
1236 		{
1237 		  $$ = build_method_invocation ($1, $3);
1238 		  $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1239 		  $$ = java_method_add_stmt (current_function_decl, $$);
1240 		}
1241         /* Added, JDK1.1 inner classes. Modified because the rule
1242 	   'primary' couldn't work.  */
1243 |	name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1244 		{$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1245 |	name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1246 		{$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1247 ;
1248 
1249 this_or_super:			/* Added, simplifies error diagnostics */
1250 	THIS_TK
1251 		{
1252 		  tree wfl = build_wfl_node (this_identifier_node);
1253 		  EXPR_WFL_LINECOL (wfl) = $1.location;
1254 		  $$ = wfl;
1255 		}
1256 |	SUPER_TK
1257 		{
1258 		  tree wfl = build_wfl_node (super_identifier_node);
1259 		  EXPR_WFL_LINECOL (wfl) = $1.location;
1260 		  $$ = wfl;
1261 		}
1262 ;
1263 
1264 /* 19.9 Productions from 9: Interfaces  */
1265 /* 19.9.1 Productions from 9.1: Interfaces Declarations  */
1266 interface_declaration:
1267 	INTERFACE_TK identifier
1268 		{ create_interface (0, $2, NULL_TREE); }
1269 	interface_body
1270 		{ ; }
1271 |	modifiers INTERFACE_TK identifier
1272 		{ create_interface ($1, $3, NULL_TREE); }
1273 	interface_body
1274 		{ ; }
1275 |	INTERFACE_TK identifier extends_interfaces
1276 		{ create_interface (0, $2, $3);	}
1277 	interface_body
1278 		{ ; }
1279 |	modifiers INTERFACE_TK identifier extends_interfaces
1280 		{ create_interface ($1, $3, $4); }
1281 	interface_body
1282 		{ ; }
1283 |	INTERFACE_TK identifier error
1284 		{ yyerror ("'{' expected"); RECOVER; }
1285 |	modifiers INTERFACE_TK identifier error
1286 		{ yyerror ("'{' expected"); RECOVER; }
1287 ;
1288 
1289 extends_interfaces:
1290 	EXTENDS_TK interface_type
1291 		{
1292 		  ctxp->interface_number = 1;
1293 		  $$ = build_tree_list ($2, NULL_TREE);
1294 		}
1295 |	extends_interfaces C_TK interface_type
1296 		{
1297 		  ctxp->interface_number++;
1298 		  $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1299 		}
1300 |	EXTENDS_TK error
1301 		{yyerror ("Invalid interface type"); RECOVER;}
1302 |	extends_interfaces C_TK error
1303 		{yyerror ("Missing term"); RECOVER;}
1304 ;
1305 
1306 interface_body:
1307 	OCB_TK CCB_TK
1308 		{ $$ = NULL_TREE; }
1309 |	OCB_TK interface_member_declarations CCB_TK
1310 		{ $$ = NULL_TREE; }
1311 ;
1312 
1313 interface_member_declarations:
1314 	interface_member_declaration
1315 |	interface_member_declarations interface_member_declaration
1316 ;
1317 
1318 interface_member_declaration:
1319 	constant_declaration
1320 |	abstract_method_declaration
1321 |	class_declaration	/* Added, JDK1.1 inner classes */
1322 		{ end_class_declaration (1); }
1323 |	interface_declaration	/* Added, JDK1.1 inner interfaces */
1324 		{ end_class_declaration (1); }
1325 ;
1326 
1327 constant_declaration:
1328 	field_declaration
1329 ;
1330 
1331 abstract_method_declaration:
1332 	method_header SC_TK
1333 		{
1334 		  check_abstract_method_header ($1);
1335 		  current_function_decl = NULL_TREE; /* FIXME ? */
1336 		}
1337 |	method_header error
1338 		{yyerror ("';' expected"); RECOVER;}
1339 ;
1340 
1341 /* 19.10 Productions from 10: Arrays  */
1342 array_initializer:
1343 	OCB_TK CCB_TK
1344 		{ $$ = build_new_array_init ($1.location, NULL_TREE); }
1345 |	OCB_TK C_TK CCB_TK
1346 		{ $$ = build_new_array_init ($1.location, NULL_TREE); }
1347 |	OCB_TK variable_initializers CCB_TK
1348 		{ $$ = build_new_array_init ($1.location, $2); }
1349 |	OCB_TK variable_initializers C_TK CCB_TK
1350 		{ $$ = build_new_array_init ($1.location, $2); }
1351 ;
1352 
1353 variable_initializers:
1354 	variable_initializer
1355 		{
1356 		  $$ = tree_cons (maybe_build_array_element_wfl ($1),
1357 				  $1, NULL_TREE);
1358 		}
1359 |	variable_initializers C_TK variable_initializer
1360 		{
1361 		  $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1362 		}
1363 |	variable_initializers C_TK error
1364 		{yyerror ("Missing term"); RECOVER;}
1365 ;
1366 
1367 /* 19.11 Production from 14: Blocks and Statements  */
1368 block:
1369 	OCB_TK CCB_TK
1370 		{
1371 		  /* Store the location of the `}' when doing xrefs */
1372 		  if (current_function_decl && flag_emit_xref)
1373 		    DECL_END_SOURCE_LINE (current_function_decl) =
1374 		      EXPR_WFL_ADD_COL ($2.location, 1);
1375 		  $$ = empty_stmt_node;
1376 		}
1377 |	block_begin block_statements block_end
1378 		{ $$ = $3; }
1379 ;
1380 
1381 block_begin:
1382 	OCB_TK
1383 		{ enter_block (); }
1384 ;
1385 
1386 block_end:
1387 	CCB_TK
1388 		{
1389 		  maybe_absorb_scoping_blocks ();
1390 		  /* Store the location of the `}' when doing xrefs */
1391 		  if (current_function_decl && flag_emit_xref)
1392 		    DECL_END_SOURCE_LINE (current_function_decl) =
1393 		      EXPR_WFL_ADD_COL ($1.location, 1);
1394 		  $$ = exit_block ();
1395 		  if (!BLOCK_SUBBLOCKS ($$))
1396 		    BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1397 		}
1398 ;
1399 
1400 block_statements:
1401 	block_statement
1402 |	block_statements block_statement
1403 ;
1404 
1405 block_statement:
1406 	local_variable_declaration_statement
1407 |	statement
1408 		{ java_method_add_stmt (current_function_decl, $1); }
1409 |	class_declaration	/* Added, JDK1.1 local classes */
1410 		{
1411 		  LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1412 		  end_class_declaration (1);
1413 		}
1414 ;
1415 
1416 local_variable_declaration_statement:
1417 	local_variable_declaration SC_TK /* Can't catch missing ';' here */
1418 ;
1419 
1420 local_variable_declaration:
1421 	type variable_declarators
1422 		{ declare_local_variables (0, $1, $2); }
1423 |	final type variable_declarators /* Added, JDK1.1 final locals */
1424 		{ declare_local_variables ($1, $2, $3); }
1425 ;
1426 
1427 statement:
1428 	statement_without_trailing_substatement
1429 |	labeled_statement
1430 |	if_then_statement
1431 |	if_then_else_statement
1432 |	while_statement
1433 |	for_statement
1434 		{ $$ = exit_block (); }
1435 ;
1436 
1437 statement_nsi:
1438 	statement_without_trailing_substatement
1439 |	labeled_statement_nsi
1440 |	if_then_else_statement_nsi
1441 |	while_statement_nsi
1442 |	for_statement_nsi
1443 		{ $$ = exit_block (); }
1444 ;
1445 
1446 statement_without_trailing_substatement:
1447 	block
1448 |	empty_statement
1449 |	expression_statement
1450 |	switch_statement
1451 |	do_statement
1452 |	break_statement
1453 |	continue_statement
1454 |	return_statement
1455 |	synchronized_statement
1456 |	throw_statement
1457 |	try_statement
1458 |	assert_statement
1459 ;
1460 
1461 empty_statement:
1462 	SC_TK
1463 		{
1464 		  if (flag_extraneous_semicolon
1465 		      && ! current_static_block
1466 		      && (! current_function_decl ||
1467 			  /* Verify we're not in a inner class declaration */
1468 			  (GET_CPC () != TYPE_NAME
1469 			   (DECL_CONTEXT (current_function_decl)))))
1470 
1471 		    {
1472 		      EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1473 		      parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1474 		    }
1475 		  $$ = empty_stmt_node;
1476 		}
1477 ;
1478 
1479 label_decl:
1480 	identifier REL_CL_TK
1481 		{
1482 		  $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1483 					    EXPR_WFL_NODE ($1));
1484 		  pushlevel (2);
1485 		  push_labeled_block ($$);
1486 		  PUSH_LABELED_BLOCK ($$);
1487 		}
1488 ;
1489 
1490 labeled_statement:
1491 	label_decl statement
1492 		{ $$ = finish_labeled_statement ($1, $2); }
1493 |	identifier error
1494 		{yyerror ("':' expected"); RECOVER;}
1495 ;
1496 
1497 labeled_statement_nsi:
1498 	label_decl statement_nsi
1499 		{ $$ = finish_labeled_statement ($1, $2); }
1500 ;
1501 
1502 /* We concentrate here a bunch of error handling rules that we couldn't write
1503    earlier, because expression_statement catches a missing ';'.  */
1504 expression_statement:
1505 	statement_expression SC_TK
1506 		{
1507 		  /* We have a statement. Generate a WFL around it so
1508 		     we can debug it */
1509 		  $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1510 		  /* We know we have a statement, so set the debug
1511                      info to be eventually generate here. */
1512 		  $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1513 		}
1514 |	error SC_TK
1515 		{
1516 		  YYNOT_TWICE yyerror ("Invalid expression statement");
1517 		  DRECOVER (expr_stmt);
1518 		}
1519 |	error OCB_TK
1520 		{
1521 		  YYNOT_TWICE yyerror ("Invalid expression statement");
1522 		  DRECOVER (expr_stmt);
1523 		}
1524 |	error CCB_TK
1525 		{
1526 		  YYNOT_TWICE yyerror ("Invalid expression statement");
1527 		  DRECOVER (expr_stmt);
1528 		}
1529 |       this_or_super OP_TK error
1530 		{yyerror ("')' expected"); RECOVER;}
1531 |       this_or_super OP_TK CP_TK error
1532 		{
1533 		  parse_ctor_invocation_error ();
1534 		  RECOVER;
1535 		}
1536 |       this_or_super OP_TK argument_list error
1537 		{yyerror ("')' expected"); RECOVER;}
1538 |       this_or_super OP_TK argument_list CP_TK error
1539 		{
1540 		  parse_ctor_invocation_error ();
1541 		  RECOVER;
1542 		}
1543 |	name DOT_TK SUPER_TK error
1544 		{yyerror ("'(' expected"); RECOVER;}
1545 |	name DOT_TK SUPER_TK OP_TK error
1546 		{yyerror ("')' expected"); RECOVER;}
1547 |	name DOT_TK SUPER_TK OP_TK argument_list error
1548 		{yyerror ("')' expected"); RECOVER;}
1549 |	name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1550 		{yyerror ("';' expected"); RECOVER;}
1551 |	name DOT_TK SUPER_TK OP_TK CP_TK error
1552 		{yyerror ("';' expected"); RECOVER;}
1553 ;
1554 
1555 statement_expression:
1556 	assignment
1557 |	pre_increment_expression
1558 |	pre_decrement_expression
1559 |	post_increment_expression
1560 |	post_decrement_expression
1561 |	method_invocation
1562 |	class_instance_creation_expression
1563 ;
1564 
1565 if_then_statement:
1566 	IF_TK OP_TK expression CP_TK statement
1567 		{
1568 		  $$ = build_if_else_statement ($2.location, $3,
1569 						$5, NULL_TREE);
1570 		}
1571 |	IF_TK error
1572 		{yyerror ("'(' expected"); RECOVER;}
1573 |	IF_TK OP_TK error
1574 		{yyerror ("Missing term"); RECOVER;}
1575 |	IF_TK OP_TK expression error
1576 		{yyerror ("')' expected"); RECOVER;}
1577 ;
1578 
1579 if_then_else_statement:
1580 	IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1581 		{ $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1582 ;
1583 
1584 if_then_else_statement_nsi:
1585 	IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1586 		{ $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1587 ;
1588 
1589 switch_statement:
1590 	switch_expression
1591 		{
1592 		  enter_block ();
1593 		}
1594 	switch_block
1595 		{
1596 		  /* Make into "proper list" of COMPOUND_EXPRs.
1597 		     I.e. make the last statement also have its own
1598 		     COMPOUND_EXPR. */
1599 		  maybe_absorb_scoping_blocks ();
1600 		  TREE_OPERAND ($1, 1) = exit_block ();
1601 		  $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1602 		}
1603 ;
1604 
1605 switch_expression:
1606 	SWITCH_TK OP_TK expression CP_TK
1607 		{
1608 		  $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1609 		  EXPR_WFL_LINECOL ($$) = $2.location;
1610 		}
1611 |	SWITCH_TK error
1612 		{yyerror ("'(' expected"); RECOVER;}
1613 |	SWITCH_TK OP_TK error
1614 		{yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1615 |	SWITCH_TK OP_TK expression CP_TK error
1616 		{yyerror ("'{' expected"); RECOVER;}
1617 ;
1618 
1619 /* Default assignment is there to avoid type node on switch_block
1620    node. */
1621 
1622 switch_block:
1623 	OCB_TK CCB_TK
1624 		{ $$ = NULL_TREE; }
1625 |	OCB_TK switch_labels CCB_TK
1626 		{ $$ = NULL_TREE; }
1627 |	OCB_TK switch_block_statement_groups CCB_TK
1628 		{ $$ = NULL_TREE; }
1629 |	OCB_TK switch_block_statement_groups switch_labels CCB_TK
1630 		{ $$ = NULL_TREE; }
1631 ;
1632 
1633 switch_block_statement_groups:
1634 	switch_block_statement_group
1635 |	switch_block_statement_groups switch_block_statement_group
1636 ;
1637 
1638 switch_block_statement_group:
1639 	switch_labels block_statements
1640 ;
1641 
1642 switch_labels:
1643 	switch_label
1644 |	switch_labels switch_label
1645 ;
1646 
1647 switch_label:
1648 	CASE_TK constant_expression REL_CL_TK
1649 		{
1650 		  tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1651 		  EXPR_WFL_LINECOL (lab) = $1.location;
1652 		  java_method_add_stmt (current_function_decl, lab);
1653 		}
1654 |	DEFAULT_TK REL_CL_TK
1655 		{
1656 		  tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1657 		  EXPR_WFL_LINECOL (lab) = $1.location;
1658 		  java_method_add_stmt (current_function_decl, lab);
1659 		}
1660 |	CASE_TK error
1661 		{yyerror ("Missing or invalid constant expression"); RECOVER;}
1662 |	CASE_TK constant_expression error
1663 		{yyerror ("':' expected"); RECOVER;}
1664 |	DEFAULT_TK error
1665 		{yyerror ("':' expected"); RECOVER;}
1666 ;
1667 
1668 while_expression:
1669 	WHILE_TK OP_TK expression CP_TK
1670 		{
1671 		  tree body = build_loop_body ($2.location, $3, 0);
1672 		  $$ = build_new_loop (body);
1673 		}
1674 ;
1675 
1676 while_statement:
1677 	while_expression statement
1678 		{ $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1679 |	WHILE_TK error
1680 		{YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1681 |	WHILE_TK OP_TK error
1682 		{yyerror ("Missing term and ')' expected"); RECOVER;}
1683 |	WHILE_TK OP_TK expression error
1684 		{yyerror ("')' expected"); RECOVER;}
1685 ;
1686 
1687 while_statement_nsi:
1688 	while_expression statement_nsi
1689 		{ $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1690 ;
1691 
1692 do_statement_begin:
1693 	DO_TK
1694 		{
1695 		  tree body = build_loop_body (0, NULL_TREE, 1);
1696 		  $$ = build_new_loop (body);
1697 		}
1698 	/* Need error handing here. FIXME */
1699 ;
1700 
1701 do_statement:
1702 	do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1703 		{ $$ = finish_loop_body ($4.location, $5, $2, 1); }
1704 ;
1705 
1706 for_statement:
1707 	for_begin SC_TK expression SC_TK for_update CP_TK statement
1708 		{
1709 		  if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1710 		    $3 = build_wfl_node ($3);
1711 		  $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1712 		}
1713 |	for_begin SC_TK SC_TK for_update CP_TK statement
1714 		{
1715 		  $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1716 		  /* We have not condition, so we get rid of the EXIT_EXPR */
1717 		  LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1718 		    empty_stmt_node;
1719 		}
1720 |	for_begin SC_TK error
1721 		{yyerror ("Invalid control expression"); RECOVER;}
1722 |	for_begin SC_TK expression SC_TK error
1723 		{yyerror ("Invalid update expression"); RECOVER;}
1724 |	for_begin SC_TK SC_TK error
1725 		{yyerror ("Invalid update expression"); RECOVER;}
1726 ;
1727 
1728 for_statement_nsi:
1729 	for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1730 		{ $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1731 |	for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1732 		{
1733 		  $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1734 		  /* We have not condition, so we get rid of the EXIT_EXPR */
1735 		  LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1736 		    empty_stmt_node;
1737 		}
1738 ;
1739 
1740 for_header:
1741 	FOR_TK OP_TK
1742 		{
1743 		  /* This scope defined for local variable that may be
1744                      defined within the scope of the for loop */
1745 		  enter_block ();
1746 		}
1747 |	FOR_TK error
1748 		{yyerror ("'(' expected"); DRECOVER(for_1);}
1749 |	FOR_TK OP_TK error
1750 		{yyerror ("Invalid init statement"); RECOVER;}
1751 ;
1752 
1753 for_begin:
1754 	for_header for_init
1755 		{
1756 		  /* We now declare the loop body. The loop is
1757                      declared as a for loop. */
1758 		  tree body = build_loop_body (0, NULL_TREE, 0);
1759 		  $$ =  build_new_loop (body);
1760 		  FOR_LOOP_P ($$) = 1;
1761 		  /* The loop is added to the current block the for
1762                      statement is defined within */
1763 		  java_method_add_stmt (current_function_decl, $$);
1764 		}
1765 ;
1766 for_init:			/* Can be empty */
1767 		{ $$ = empty_stmt_node; }
1768 |	statement_expression_list
1769 		{
1770 		  /* Init statement recorded within the previously
1771                      defined block scope */
1772 		  $$ = java_method_add_stmt (current_function_decl, $1);
1773 		}
1774 |	local_variable_declaration
1775 		{
1776 		  /* Local variable are recorded within the previously
1777 		     defined block scope */
1778 		  $$ = NULL_TREE;
1779 		}
1780 |	statement_expression_list error
1781 		{yyerror ("';' expected"); DRECOVER(for_init_1);}
1782 ;
1783 
1784 for_update:			/* Can be empty */
1785 		{$$ = empty_stmt_node;}
1786 |	statement_expression_list
1787 		{ $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1788 ;
1789 
1790 statement_expression_list:
1791 	statement_expression
1792 		{ $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1793 |	statement_expression_list C_TK statement_expression
1794 		{ $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1795 |	statement_expression_list C_TK error
1796 		{yyerror ("Missing term"); RECOVER;}
1797 ;
1798 
1799 break_statement:
1800 	BREAK_TK SC_TK
1801 		{ $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1802 |	BREAK_TK identifier SC_TK
1803 		{ $$ = build_bc_statement ($1.location, 1, $2); }
1804 |	BREAK_TK error
1805 		{yyerror ("Missing term"); RECOVER;}
1806 |	BREAK_TK identifier error
1807 		{yyerror ("';' expected"); RECOVER;}
1808 ;
1809 
1810 continue_statement:
1811 	CONTINUE_TK SC_TK
1812 		{ $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1813 |       CONTINUE_TK identifier SC_TK
1814 		{ $$ = build_bc_statement ($1.location, 0, $2); }
1815 |	CONTINUE_TK error
1816 		{yyerror ("Missing term"); RECOVER;}
1817 |	CONTINUE_TK identifier error
1818 		{yyerror ("';' expected"); RECOVER;}
1819 ;
1820 
1821 return_statement:
1822 	RETURN_TK SC_TK
1823 		{ $$ = build_return ($1.location, NULL_TREE); }
1824 |	RETURN_TK expression SC_TK
1825 		{ $$ = build_return ($1.location, $2); }
1826 |	RETURN_TK error
1827 		{yyerror ("Missing term"); RECOVER;}
1828 |	RETURN_TK expression error
1829 		{yyerror ("';' expected"); RECOVER;}
1830 ;
1831 
1832 throw_statement:
1833 	THROW_TK expression SC_TK
1834 		{
1835 		  $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1836 		  EXPR_WFL_LINECOL ($$) = $1.location;
1837 		}
1838 |	THROW_TK error
1839 		{yyerror ("Missing term"); RECOVER;}
1840 |	THROW_TK expression error
1841 		{yyerror ("';' expected"); RECOVER;}
1842 ;
1843 
1844 assert_statement:
1845 	ASSERT_TK expression REL_CL_TK expression SC_TK
1846 		{
1847 		  $$ = build_assertion ($1.location, $2, $4);
1848 		}
1849 |	ASSERT_TK expression SC_TK
1850 		{
1851 		  $$ = build_assertion ($1.location, $2, NULL_TREE);
1852 		}
1853 |	ASSERT_TK error
1854 		{yyerror ("Missing term"); RECOVER;}
1855 |	ASSERT_TK expression error
1856 		{yyerror ("';' expected"); RECOVER;}
1857 ;
1858 
1859 synchronized_statement:
1860 	synchronized OP_TK expression CP_TK block
1861 		{
1862 		  $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1863 		  EXPR_WFL_LINECOL ($$) =
1864 		    EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1865 		}
1866 |	synchronized OP_TK expression CP_TK error
1867 		{yyerror ("'{' expected"); RECOVER;}
1868 |	synchronized error
1869 		{yyerror ("'(' expected"); RECOVER;}
1870 |	synchronized OP_TK error CP_TK
1871 		{yyerror ("Missing term"); RECOVER;}
1872 |	synchronized OP_TK error
1873 		{yyerror ("Missing term"); RECOVER;}
1874 ;
1875 
1876 synchronized:
1877 	modifiers
1878 		{
1879 		  check_modifiers (
1880              "Illegal modifier `%s'. Only `synchronized' was expected here",
1881 				   $1, ACC_SYNCHRONIZED);
1882 		  if ($1 != ACC_SYNCHRONIZED)
1883 		    MODIFIER_WFL (SYNCHRONIZED_TK) =
1884 		      build_wfl_node (NULL_TREE);
1885 		}
1886 ;
1887 
1888 try_statement:
1889 	TRY_TK block catches
1890 		{ $$ = build_try_statement ($1.location, $2, $3); }
1891 |	TRY_TK block finally
1892 		{ $$ = build_try_finally_statement ($1.location, $2, $3); }
1893 |	TRY_TK block catches finally
1894 		{ $$ = build_try_finally_statement
1895 		    ($1.location, build_try_statement ($1.location,
1896 						       $2, $3), $4);
1897 		}
1898 |	TRY_TK error
1899 		{yyerror ("'{' expected"); DRECOVER (try_statement);}
1900 ;
1901 
1902 catches:
1903 	catch_clause
1904 |	catches catch_clause
1905 		{
1906 		  TREE_CHAIN ($2) = $1;
1907 		  $$ = $2;
1908 		}
1909 ;
1910 
1911 catch_clause:
1912 	catch_clause_parameter block
1913 		{
1914 		  java_method_add_stmt (current_function_decl, $2);
1915 		  exit_block ();
1916 		  $$ = $1;
1917 		}
1918 ;
1919 
1920 catch_clause_parameter:
1921 	CATCH_TK OP_TK formal_parameter CP_TK
1922 		{
1923 		  /* We add a block to define a scope for
1924 		     formal_parameter (CCBP). The formal parameter is
1925 		     declared initialized by the appropriate function
1926 		     call */
1927 		  tree ccpb = enter_block ();
1928 		  tree init = build_assignment
1929 		    (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1930 		     build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1931 		  declare_local_variables (0, TREE_VALUE ($3),
1932 					   build_tree_list (TREE_PURPOSE ($3),
1933 							    init));
1934 		  $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1935 		  EXPR_WFL_LINECOL ($$) = $1.location;
1936 		}
1937 |	CATCH_TK error
1938 		{yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1939 |	CATCH_TK OP_TK error
1940 		{
1941 		  yyerror ("Missing term or ')' expected");
1942 		  RECOVER; $$ = NULL_TREE;
1943 		}
1944 |	CATCH_TK OP_TK error CP_TK /* That's for () */
1945 		{yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1946 ;
1947 
1948 finally:
1949 	FINALLY_TK block
1950 		{ $$ = $2; }
1951 |	FINALLY_TK error
1952 		{yyerror ("'{' expected"); RECOVER; }
1953 ;
1954 
1955 /* 19.12 Production from 15: Expressions  */
1956 primary:
1957 	primary_no_new_array
1958 |	array_creation_expression
1959 ;
1960 
1961 primary_no_new_array:
1962 	literal
1963 |	THIS_TK
1964 		{ $$ = build_this ($1.location); }
1965 |	OP_TK expression CP_TK
1966 		{$$ = $2;}
1967 |	class_instance_creation_expression
1968 |	field_access
1969 |	method_invocation
1970 |	array_access
1971 |	type_literals
1972         /* Added, JDK1.1 inner classes. Documentation is wrong
1973            refering to a 'ClassName' (class_name) rule that doesn't
1974            exist. Used name: instead.  */
1975 |	name DOT_TK THIS_TK
1976 		{
1977 		  tree wfl = build_wfl_node (this_identifier_node);
1978 		  $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1979 		}
1980 |	OP_TK expression error
1981 		{yyerror ("')' expected"); RECOVER;}
1982 |	name DOT_TK error
1983 		{yyerror ("'class' or 'this' expected" ); RECOVER;}
1984 |	primitive_type DOT_TK error
1985 		{yyerror ("'class' expected" ); RECOVER;}
1986 |	VOID_TK DOT_TK error
1987 		{yyerror ("'class' expected" ); RECOVER;}
1988 ;
1989 
1990 type_literals:
1991 	name DOT_TK CLASS_TK
1992 		{ $$ = build_incomplete_class_ref ($2.location, $1); }
1993 |	array_type DOT_TK CLASS_TK
1994 		{ $$ = build_incomplete_class_ref ($2.location, $1); }
1995 |	primitive_type DOT_TK CLASS_TK
1996                 { $$ = build_incomplete_class_ref ($2.location, $1); }
1997 |	VOID_TK DOT_TK CLASS_TK
1998                 {
1999                    $$ = build_incomplete_class_ref ($2.location,
2000                                                    void_type_node);
2001                 }
2002 ;
2003 
2004 class_instance_creation_expression:
2005 	NEW_TK class_type OP_TK argument_list CP_TK
2006 		{ $$ = build_new_invocation ($2, $4); }
2007 |	NEW_TK class_type OP_TK CP_TK
2008 		{ $$ = build_new_invocation ($2, NULL_TREE); }
2009 |	anonymous_class_creation
2010         /* Added, JDK1.1 inner classes, modified to use name or
2011 	   primary instead of primary solely which couldn't work in
2012 	   all situations.  */
2013 |	something_dot_new identifier OP_TK CP_TK
2014 		{
2015 		  tree ctor = build_new_invocation ($2, NULL_TREE);
2016 		  $$ = make_qualified_primary ($1, ctor,
2017 					       EXPR_WFL_LINECOL ($1));
2018 		}
2019 |	something_dot_new identifier OP_TK CP_TK class_body
2020 |	something_dot_new identifier OP_TK argument_list CP_TK
2021 		{
2022 		  tree ctor = build_new_invocation ($2, $4);
2023 		  $$ = make_qualified_primary ($1, ctor,
2024 					       EXPR_WFL_LINECOL ($1));
2025 		}
2026 |	something_dot_new identifier OP_TK argument_list CP_TK class_body
2027 |	NEW_TK error SC_TK
2028 		{yyerror ("'(' expected"); DRECOVER(new_1);}
2029 |	NEW_TK class_type error
2030 		{yyerror ("'(' expected"); RECOVER;}
2031 |	NEW_TK class_type OP_TK error
2032 		{yyerror ("')' or term expected"); RECOVER;}
2033 |	NEW_TK class_type OP_TK argument_list error
2034 		{yyerror ("')' expected"); RECOVER;}
2035 |	something_dot_new error
2036 		{YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
2037 |	something_dot_new identifier error
2038 		{yyerror ("'(' expected"); RECOVER;}
2039 ;
2040 
2041 /* Created after JDK1.1 rules originally added to
2042    class_instance_creation_expression, but modified to use
2043    'class_type' instead of 'TypeName' (type_name) which is mentionned
2044    in the documentation but doesn't exist. */
2045 
2046 anonymous_class_creation:
2047 	NEW_TK class_type OP_TK argument_list CP_TK
2048 		{ create_anonymous_class ($1.location, $2); }
2049         class_body
2050 		{
2051 		  tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2052 		  EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2053 
2054 		  end_class_declaration (1);
2055 
2056 		  /* Now we can craft the new expression */
2057 		  $$ = build_new_invocation (id, $4);
2058 
2059 		  /* Note that we can't possibly be here if
2060 		     `class_type' is an interface (in which case the
2061 		     anonymous class extends Object and implements
2062 		     `class_type', hence its constructor can't have
2063 		     arguments.) */
2064 
2065 		  /* Otherwise, the innerclass must feature a
2066 		     constructor matching `argument_list'. Anonymous
2067 		     classes are a bit special: it's impossible to
2068 		     define constructor for them, hence constructors
2069 		     must be generated following the hints provided by
2070 		     the `new' expression. Whether a super constructor
2071 		     of that nature exists or not is to be verified
2072 		     later on in verify_constructor_super.
2073 
2074 		     It's during the expansion of a `new' statement
2075 		     refering to an anonymous class that a ctor will
2076 		     be generated for the anonymous class, with the
2077 		     right arguments. */
2078 
2079 		}
2080 |	NEW_TK class_type OP_TK CP_TK
2081 		{ create_anonymous_class ($1.location, $2); }
2082         class_body
2083 		{
2084 		  tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2085 		  EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2086 
2087 		  end_class_declaration (1);
2088 
2089 		  /* Now we can craft the new expression. The
2090                      statement doesn't need to be remember so that a
2091                      constructor can be generated, since its signature
2092                      is already known. */
2093 		  $$ = build_new_invocation (id, NULL_TREE);
2094 		}
2095 ;
2096 
2097 something_dot_new:		/* Added, not part of the specs. */
2098 	name DOT_TK NEW_TK
2099 		{ $$ = $1; }
2100 |	primary DOT_TK NEW_TK
2101 		{ $$ = $1; }
2102 ;
2103 
2104 argument_list:
2105 	expression
2106 		{
2107 		  $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2108 		  ctxp->formal_parameter_number = 1;
2109 		}
2110 |	argument_list C_TK expression
2111 		{
2112 		  ctxp->formal_parameter_number += 1;
2113 		  $$ = tree_cons (NULL_TREE, $3, $1);
2114 		}
2115 |	argument_list C_TK error
2116 		{yyerror ("Missing term"); RECOVER;}
2117 ;
2118 
2119 array_creation_expression:
2120 	NEW_TK primitive_type dim_exprs
2121 		{ $$ = build_newarray_node ($2, $3, 0); }
2122 |	NEW_TK class_or_interface_type dim_exprs
2123 		{ $$ = build_newarray_node ($2, $3, 0); }
2124 |	NEW_TK primitive_type dim_exprs dims
2125 		{ $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2126 |	NEW_TK class_or_interface_type dim_exprs dims
2127 		{ $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2128         /* Added, JDK1.1 anonymous array. Initial documentation rule
2129            modified */
2130 |	NEW_TK class_or_interface_type dims array_initializer
2131 		{
2132 		  char *sig;
2133 		  int osb = pop_current_osb (ctxp);
2134 		  while (osb--)
2135 		    obstack_grow (&temporary_obstack, "[]", 2);
2136 		  obstack_1grow (&temporary_obstack, '\0');
2137 		  sig = obstack_finish (&temporary_obstack);
2138 		  $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2139 			      $2, get_identifier (sig), $4);
2140 		}
2141 |	NEW_TK primitive_type dims array_initializer
2142 		{
2143 		  int osb = pop_current_osb (ctxp);
2144 		  tree type = $2;
2145 		  while (osb--)
2146 		    type = build_java_array_type (type, -1);
2147 		  $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2148 			      build_pointer_type (type), NULL_TREE, $4);
2149 		}
2150 |	NEW_TK error CSB_TK
2151 		{yyerror ("'[' expected"); DRECOVER ("]");}
2152 |	NEW_TK error OSB_TK
2153 		{yyerror ("']' expected"); RECOVER;}
2154 ;
2155 
2156 dim_exprs:
2157 	dim_expr
2158 		{ $$ = build_tree_list (NULL_TREE, $1); }
2159 |	dim_exprs dim_expr
2160 		{ $$ = tree_cons (NULL_TREE, $2, $$); }
2161 ;
2162 
2163 dim_expr:
2164 	OSB_TK expression CSB_TK
2165 		{
2166 		  if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2167 		    {
2168 		      $2 = build_wfl_node ($2);
2169 		      TREE_TYPE ($2) = NULL_TREE;
2170 		    }
2171 		  EXPR_WFL_LINECOL ($2) = $1.location;
2172 		  $$ = $2;
2173 		}
2174 |	OSB_TK expression error
2175 		{yyerror ("']' expected"); RECOVER;}
2176 |	OSB_TK error
2177 		{
2178 		  yyerror ("Missing term");
2179 		  yyerror ("']' expected");
2180 		  RECOVER;
2181 		}
2182 ;
2183 
2184 dims:
2185 	OSB_TK CSB_TK
2186 		{
2187 		  int allocate = 0;
2188 		  /* If not initialized, allocate memory for the osb
2189                      numbers stack */
2190 		  if (!ctxp->osb_limit)
2191 		    {
2192 		      allocate = ctxp->osb_limit = 32;
2193 		      ctxp->osb_depth = -1;
2194 		    }
2195 		  /* If capacity overflown, reallocate a bigger chunk */
2196 		  else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2197 		    allocate = ctxp->osb_limit << 1;
2198 
2199 		  if (allocate)
2200 		    {
2201 		      allocate *= sizeof (int);
2202 		      if (ctxp->osb_number)
2203 			ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2204 							    allocate);
2205 		      else
2206 			ctxp->osb_number = (int *)xmalloc (allocate);
2207 		    }
2208 		  ctxp->osb_depth++;
2209 		  CURRENT_OSB (ctxp) = 1;
2210 		}
2211 |	dims OSB_TK CSB_TK
2212 		{ CURRENT_OSB (ctxp)++; }
2213 |	dims OSB_TK error
2214 		{ yyerror ("']' expected"); RECOVER;}
2215 ;
2216 
2217 field_access:
2218 	primary DOT_TK identifier
2219 		{ $$ = make_qualified_primary ($1, $3, $2.location); }
2220 		/*  FIXME - REWRITE TO:
2221 		{ $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2222 |	SUPER_TK DOT_TK identifier
2223 		{
2224 		  tree super_wfl = build_wfl_node (super_identifier_node);
2225 		  EXPR_WFL_LINECOL (super_wfl) = $1.location;
2226 		  $$ = make_qualified_name (super_wfl, $3, $2.location);
2227 		}
2228 |	SUPER_TK error
2229 		{yyerror ("Field expected"); DRECOVER (super_field_acces);}
2230 ;
2231 
2232 method_invocation:
2233 	name OP_TK CP_TK
2234 		{ $$ = build_method_invocation ($1, NULL_TREE); }
2235 |	name OP_TK argument_list CP_TK
2236 		{ $$ = build_method_invocation ($1, $3); }
2237 |	primary DOT_TK identifier OP_TK CP_TK
2238 		{
2239 		  if (TREE_CODE ($1) == THIS_EXPR)
2240 		    $$ = build_this_super_qualified_invocation
2241 		      (1, $3, NULL_TREE, 0, $2.location);
2242 		  else
2243 		    {
2244 		      tree invok = build_method_invocation ($3, NULL_TREE);
2245 		      $$ = make_qualified_primary ($1, invok, $2.location);
2246 		    }
2247 		}
2248 |	primary DOT_TK identifier OP_TK argument_list CP_TK
2249 		{
2250 		  if (TREE_CODE ($1) == THIS_EXPR)
2251 		    $$ = build_this_super_qualified_invocation
2252 		      (1, $3, $5, 0, $2.location);
2253 		  else
2254 		    {
2255 		      tree invok = build_method_invocation ($3, $5);
2256 		      $$ = make_qualified_primary ($1, invok, $2.location);
2257 		    }
2258 		}
2259 |	SUPER_TK DOT_TK identifier OP_TK CP_TK
2260 		{
2261 		  $$ = build_this_super_qualified_invocation
2262 		    (0, $3, NULL_TREE, $1.location, $2.location);
2263 		}
2264 |	SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2265 		{
2266 		  $$ = build_this_super_qualified_invocation
2267 		    (0, $3, $5, $1.location, $2.location);
2268 		}
2269         /* Screws up thing. I let it here until I'm convinced it can
2270            be removed. FIXME
2271 |	primary DOT_TK error
2272 		{yyerror ("'(' expected"); DRECOVER(bad);} */
2273 |	SUPER_TK DOT_TK error CP_TK
2274 		{ yyerror ("'(' expected"); DRECOVER (method_invocation); }
2275 |	SUPER_TK DOT_TK error DOT_TK
2276 		{ yyerror ("'(' expected"); DRECOVER (method_invocation); }
2277 ;
2278 
2279 array_access:
2280 	name OSB_TK expression CSB_TK
2281 		{ $$ = build_array_ref ($2.location, $1, $3); }
2282 |	primary_no_new_array OSB_TK expression CSB_TK
2283 		{ $$ = build_array_ref ($2.location, $1, $3); }
2284 |	name OSB_TK error
2285 		{
2286 		  yyerror ("Missing term and ']' expected");
2287 		  DRECOVER(array_access);
2288 		}
2289 |	name OSB_TK expression error
2290 		{
2291 		  yyerror ("']' expected");
2292 		  DRECOVER(array_access);
2293 		}
2294 |	primary_no_new_array OSB_TK error
2295 		{
2296 		  yyerror ("Missing term and ']' expected");
2297 		  DRECOVER(array_access);
2298 		}
2299 |	primary_no_new_array OSB_TK expression error
2300 		{
2301 		  yyerror ("']' expected");
2302 		  DRECOVER(array_access);
2303 		}
2304 ;
2305 
2306 postfix_expression:
2307 	primary
2308 |	name
2309 |	post_increment_expression
2310 |	post_decrement_expression
2311 ;
2312 
2313 post_increment_expression:
2314 	postfix_expression INCR_TK
2315 		{ $$ = build_incdec ($2.token, $2.location, $1, 1); }
2316 ;
2317 
2318 post_decrement_expression:
2319 	postfix_expression DECR_TK
2320 		{ $$ = build_incdec ($2.token, $2.location, $1, 1); }
2321 ;
2322 
2323 trap_overflow_corner_case:
2324 	pre_increment_expression
2325 |	pre_decrement_expression
2326 |	PLUS_TK unary_expression
2327 		{$$ = build_unaryop ($1.token, $1.location, $2); }
2328 |	unary_expression_not_plus_minus
2329 |	PLUS_TK error
2330 		{yyerror ("Missing term"); RECOVER}
2331 ;
2332 
2333 unary_expression:
2334 	trap_overflow_corner_case
2335 		{
2336 		  error_if_numeric_overflow ($1);
2337 		  $$ = $1;
2338 		}
2339 |	MINUS_TK trap_overflow_corner_case
2340 		{$$ = build_unaryop ($1.token, $1.location, $2); }
2341 |	MINUS_TK error
2342 		{yyerror ("Missing term"); RECOVER}
2343 ;
2344 
2345 pre_increment_expression:
2346 	INCR_TK unary_expression
2347 		{$$ = build_incdec ($1.token, $1.location, $2, 0); }
2348 |	INCR_TK error
2349 		{yyerror ("Missing term"); RECOVER}
2350 ;
2351 
2352 pre_decrement_expression:
2353 	DECR_TK unary_expression
2354 		{$$ = build_incdec ($1.token, $1.location, $2, 0); }
2355 |	DECR_TK error
2356 		{yyerror ("Missing term"); RECOVER}
2357 ;
2358 
2359 unary_expression_not_plus_minus:
2360 	postfix_expression
2361 |	NOT_TK unary_expression
2362 		{$$ = build_unaryop ($1.token, $1.location, $2); }
2363 |	NEG_TK unary_expression
2364  		{$$ = build_unaryop ($1.token, $1.location, $2); }
2365 |	cast_expression
2366 |       NOT_TK error
2367 		{yyerror ("Missing term"); RECOVER}
2368 |       NEG_TK error
2369 		{yyerror ("Missing term"); RECOVER}
2370 ;
2371 
2372 cast_expression:		/* Error handling here is potentially weak */
2373 	OP_TK primitive_type dims CP_TK unary_expression
2374 		{
2375 		  tree type = $2;
2376 		  int osb = pop_current_osb (ctxp);
2377 		  while (osb--)
2378 		    type = build_java_array_type (type, -1);
2379 		  $$ = build_cast ($1.location, type, $5);
2380 		}
2381 |	OP_TK primitive_type CP_TK unary_expression
2382 		{ $$ = build_cast ($1.location, $2, $4); }
2383 |	OP_TK expression CP_TK unary_expression_not_plus_minus
2384 		{ $$ = build_cast ($1.location, $2, $4); }
2385 |	OP_TK name dims CP_TK unary_expression_not_plus_minus
2386 		{
2387 		  const char *ptr;
2388 		  int osb = pop_current_osb (ctxp);
2389 		  obstack_grow (&temporary_obstack,
2390 				IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2391 				IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2392 		  while (osb--)
2393 		    obstack_grow (&temporary_obstack, "[]", 2);
2394 		  obstack_1grow (&temporary_obstack, '\0');
2395 		  ptr = obstack_finish (&temporary_obstack);
2396 		  EXPR_WFL_NODE ($2) = get_identifier (ptr);
2397 		  $$ = build_cast ($1.location, $2, $5);
2398 		}
2399 |	OP_TK primitive_type OSB_TK error
2400 		{yyerror ("']' expected, invalid type expression");}
2401 |       OP_TK error
2402 		{
2403 	          YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2404 		  RECOVER;
2405 		}
2406 |	OP_TK primitive_type dims CP_TK error
2407 		{yyerror ("Missing term"); RECOVER;}
2408 |	OP_TK primitive_type CP_TK error
2409 		{yyerror ("Missing term"); RECOVER;}
2410 |	OP_TK name dims CP_TK error
2411 		{yyerror ("Missing term"); RECOVER;}
2412 ;
2413 
2414 multiplicative_expression:
2415 	unary_expression
2416 |	multiplicative_expression MULT_TK unary_expression
2417 		{
2418 		  $$ = build_binop (BINOP_LOOKUP ($2.token),
2419 				    $2.location, $1, $3);
2420 		}
2421 |	multiplicative_expression DIV_TK unary_expression
2422 		{
2423 		  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2424 				    $1, $3);
2425 		}
2426 |	multiplicative_expression REM_TK unary_expression
2427 		{
2428 		  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2429 				    $1, $3);
2430 		}
2431 |	multiplicative_expression MULT_TK error
2432 		{yyerror ("Missing term"); RECOVER;}
2433 |	multiplicative_expression DIV_TK error
2434 		{yyerror ("Missing term"); RECOVER;}
2435 |	multiplicative_expression REM_TK error
2436 		{yyerror ("Missing term"); RECOVER;}
2437 ;
2438 
2439 additive_expression:
2440 	multiplicative_expression
2441 |	additive_expression PLUS_TK multiplicative_expression
2442 		{
2443 		  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2444 				    $1, $3);
2445 		}
2446 |	additive_expression MINUS_TK multiplicative_expression
2447 		{
2448 		  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2449 				    $1, $3);
2450 		}
2451 |	additive_expression PLUS_TK error
2452 		{yyerror ("Missing term"); RECOVER;}
2453 |	additive_expression MINUS_TK error
2454 		{yyerror ("Missing term"); RECOVER;}
2455 ;
2456 
2457 shift_expression:
2458 	additive_expression
2459 |	shift_expression LS_TK additive_expression
2460 		{
2461 		  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2462 				    $1, $3);
2463 		}
2464 |	shift_expression SRS_TK additive_expression
2465 		{
2466 		  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2467 				    $1, $3);
2468 		}
2469 |	shift_expression ZRS_TK additive_expression
2470 		{
2471 		  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2472 				    $1, $3);
2473 		}
2474 |	shift_expression LS_TK error
2475 		{yyerror ("Missing term"); RECOVER;}
2476 |	shift_expression SRS_TK error
2477 		{yyerror ("Missing term"); RECOVER;}
2478 |	shift_expression ZRS_TK error
2479 		{yyerror ("Missing term"); RECOVER;}
2480 ;
2481 
2482 relational_expression:
2483 	shift_expression
2484 |	relational_expression LT_TK shift_expression
2485 		{
2486 		  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2487 				    $1, $3);
2488 		}
2489 |	relational_expression GT_TK shift_expression
2490 		{
2491 		  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2492 				    $1, $3);
2493 		}
2494 |	relational_expression LTE_TK shift_expression
2495 		{
2496 		  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2497 				    $1, $3);
2498 		}
2499 |	relational_expression GTE_TK shift_expression
2500 		{
2501 		  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2502 				    $1, $3);
2503 		}
2504 |	relational_expression INSTANCEOF_TK reference_type
2505 		{ $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2506 |	relational_expression LT_TK error
2507 		{yyerror ("Missing term"); RECOVER;}
2508 |	relational_expression GT_TK error
2509 		{yyerror ("Missing term"); RECOVER;}
2510 |	relational_expression LTE_TK error
2511 		{yyerror ("Missing term"); RECOVER;}
2512 |	relational_expression GTE_TK error
2513 		{yyerror ("Missing term"); RECOVER;}
2514 |	relational_expression INSTANCEOF_TK error
2515 		{yyerror ("Invalid reference type"); RECOVER;}
2516 ;
2517 
2518 equality_expression:
2519 	relational_expression
2520 |	equality_expression EQ_TK relational_expression
2521 		{
2522 		  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2523 				    $1, $3);
2524 		}
2525 |	equality_expression NEQ_TK relational_expression
2526 		{
2527 		  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2528 				    $1, $3);
2529 		}
2530 |	equality_expression EQ_TK error
2531 		{yyerror ("Missing term"); RECOVER;}
2532 |	equality_expression NEQ_TK error
2533 		{yyerror ("Missing term"); RECOVER;}
2534 ;
2535 
2536 and_expression:
2537 	equality_expression
2538 |	and_expression AND_TK equality_expression
2539 		{
2540 		  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2541 				    $1, $3);
2542 		}
2543 |	and_expression AND_TK error
2544 		{yyerror ("Missing term"); RECOVER;}
2545 ;
2546 
2547 exclusive_or_expression:
2548 	and_expression
2549 |	exclusive_or_expression XOR_TK and_expression
2550 		{
2551 		  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2552 				    $1, $3);
2553 		}
2554 |	exclusive_or_expression XOR_TK error
2555 		{yyerror ("Missing term"); RECOVER;}
2556 ;
2557 
2558 inclusive_or_expression:
2559 	exclusive_or_expression
2560 |	inclusive_or_expression OR_TK exclusive_or_expression
2561 		{
2562 		  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2563 				    $1, $3);
2564 		}
2565 |	inclusive_or_expression OR_TK error
2566 		{yyerror ("Missing term"); RECOVER;}
2567 ;
2568 
2569 conditional_and_expression:
2570 	inclusive_or_expression
2571 |	conditional_and_expression BOOL_AND_TK inclusive_or_expression
2572 		{
2573 		  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2574 				    $1, $3);
2575 		}
2576 |	conditional_and_expression BOOL_AND_TK error
2577 		{yyerror ("Missing term"); RECOVER;}
2578 ;
2579 
2580 conditional_or_expression:
2581 	conditional_and_expression
2582 |	conditional_or_expression BOOL_OR_TK conditional_and_expression
2583 		{
2584 		  $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2585 				    $1, $3);
2586 		}
2587 |	conditional_or_expression BOOL_OR_TK error
2588 		{yyerror ("Missing term"); RECOVER;}
2589 ;
2590 
2591 conditional_expression:		/* Error handling here is weak */
2592 	conditional_or_expression
2593 |	conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2594 		{
2595 		  $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2596 		  EXPR_WFL_LINECOL ($$) = $2.location;
2597 		}
2598 |	conditional_or_expression REL_QM_TK REL_CL_TK error
2599 		{
2600 		  YYERROR_NOW;
2601 		  yyerror ("Missing term");
2602 		  DRECOVER (1);
2603 		}
2604 |	conditional_or_expression REL_QM_TK error
2605 		{yyerror ("Missing term"); DRECOVER (2);}
2606 |	conditional_or_expression REL_QM_TK expression REL_CL_TK error
2607 		{yyerror ("Missing term"); DRECOVER (3);}
2608 ;
2609 
2610 assignment_expression:
2611 	conditional_expression
2612 |	assignment
2613 ;
2614 
2615 assignment:
2616 	left_hand_side assignment_operator assignment_expression
2617 		{ $$ = build_assignment ($2.token, $2.location, $1, $3); }
2618 |	left_hand_side assignment_operator error
2619 		{
2620 		  YYNOT_TWICE yyerror ("Missing term");
2621 		  DRECOVER (assign);
2622 		}
2623 ;
2624 
2625 left_hand_side:
2626 	name
2627 |	field_access
2628 |	array_access
2629 ;
2630 
2631 assignment_operator:
2632 	ASSIGN_ANY_TK
2633 |	ASSIGN_TK
2634 ;
2635 
2636 expression:
2637 	assignment_expression
2638 ;
2639 
2640 constant_expression:
2641 	expression
2642 ;
2643 
2644 %%
2645 
2646 /* Helper function to retrieve an OSB count. Should be used when the
2647    `dims:' rule is being used.  */
2648 
2649 static int
pop_current_osb(ctxp)2650 pop_current_osb (ctxp)
2651      struct parser_ctxt *ctxp;
2652 {
2653   int to_return;
2654 
2655   if (ctxp->osb_depth < 0)
2656     abort ();
2657 
2658   to_return = CURRENT_OSB (ctxp);
2659   ctxp->osb_depth--;
2660 
2661   return to_return;
2662 }
2663 
2664 
2665 
2666 /* This section of the code deal with save/restoring parser contexts.
2667    Add mode documentation here. FIXME */
2668 
2669 /* Helper function. Create a new parser context. With
2670    COPY_FROM_PREVIOUS set to a nonzero value, content of the previous
2671    context is copied, otherwise, the new context is zeroed. The newly
2672    created context becomes the current one.  */
2673 
2674 static void
create_new_parser_context(copy_from_previous)2675 create_new_parser_context (copy_from_previous)
2676     int copy_from_previous;
2677 {
2678   struct parser_ctxt *new;
2679 
2680   new =  (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2681   if (copy_from_previous)
2682     {
2683       memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2684       new->saved_data_ctx = 1;
2685     }
2686   else
2687     memset ((PTR) new, 0, sizeof (struct parser_ctxt));
2688 
2689   new->next = ctxp;
2690   ctxp = new;
2691 }
2692 
2693 /* Create a new parser context and make it the current one. */
2694 
2695 void
java_push_parser_context()2696 java_push_parser_context ()
2697 {
2698   create_new_parser_context (0);
2699 }
2700 
2701 void
java_pop_parser_context(generate)2702 java_pop_parser_context (generate)
2703      int generate;
2704 {
2705   tree current;
2706   struct parser_ctxt *toFree, *next;
2707 
2708   if (!ctxp)
2709     return;
2710 
2711   toFree = ctxp;
2712   next = ctxp->next;
2713   if (next)
2714     {
2715       lineno = ctxp->lineno;
2716       current_class = ctxp->class_type;
2717     }
2718 
2719   /* If the old and new lexers differ, then free the old one.  */
2720   if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2721     java_destroy_lexer (ctxp->lexer);
2722 
2723   /* Set the single import class file flag to 0 for the current list
2724      of imported things */
2725   for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2726     IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2727 
2728   /* And restore those of the previous context */
2729   if ((ctxp = next))		/* Assignment is really meant here */
2730     for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2731       IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2732 
2733   /* If we pushed a context to parse a class intended to be generated,
2734      we keep it so we can remember the class. What we could actually
2735      do is to just update a list of class names.  */
2736   if (generate)
2737     {
2738       toFree->next = ctxp_for_generation;
2739       ctxp_for_generation = toFree;
2740     }
2741   else
2742     free (toFree);
2743 }
2744 
2745 /* Create a parser context for the use of saving some global
2746    variables.  */
2747 
2748 void
java_parser_context_save_global()2749 java_parser_context_save_global ()
2750 {
2751   if (!ctxp)
2752     {
2753       java_push_parser_context ();
2754       ctxp->saved_data_ctx = 1;
2755     }
2756 
2757   /* If this context already stores data, create a new one suitable
2758      for data storage. */
2759   else if (ctxp->saved_data)
2760     create_new_parser_context (1);
2761 
2762   ctxp->lineno = lineno;
2763   ctxp->class_type = current_class;
2764   ctxp->filename = input_filename;
2765   ctxp->function_decl = current_function_decl;
2766   ctxp->saved_data = 1;
2767 }
2768 
2769 /* Restore some global variables from the previous context. Make the
2770    previous context the current one.  */
2771 
2772 void
java_parser_context_restore_global()2773 java_parser_context_restore_global ()
2774 {
2775   lineno = ctxp->lineno;
2776   current_class = ctxp->class_type;
2777   input_filename = ctxp->filename;
2778   if (wfl_operator)
2779     {
2780       tree s;
2781       BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2782       EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2783     }
2784   current_function_decl = ctxp->function_decl;
2785   ctxp->saved_data = 0;
2786   if (ctxp->saved_data_ctx)
2787     java_pop_parser_context (0);
2788 }
2789 
2790 /* Suspend vital data for the current class/function being parsed so
2791    that an other class can be parsed. Used to let local/anonymous
2792    classes be parsed.  */
2793 
2794 static void
java_parser_context_suspend()2795 java_parser_context_suspend ()
2796 {
2797   /* This makes debugging through java_debug_context easier */
2798   static const char *const name = "<inner buffer context>";
2799 
2800   /* Duplicate the previous context, use it to save the globals we're
2801      interested in */
2802   create_new_parser_context (1);
2803   ctxp->function_decl = current_function_decl;
2804   ctxp->class_type = current_class;
2805 
2806   /* Then create a new context which inherits all data from the
2807      previous one. This will be the new current context  */
2808   create_new_parser_context (1);
2809 
2810   /* Help debugging */
2811   ctxp->next->filename = name;
2812 }
2813 
2814 /* Resume vital data for the current class/function being parsed so
2815    that an other class can be parsed. Used to let local/anonymous
2816    classes be parsed.  The trick is the data storing file position
2817    informations must be restored to their current value, so parsing
2818    can resume as if no context was ever saved. */
2819 
2820 static void
java_parser_context_resume()2821 java_parser_context_resume ()
2822 {
2823   struct parser_ctxt *old = ctxp;             /* This one is to be discarded */
2824   struct parser_ctxt *saver = old->next;      /* This one contain saved info */
2825   struct parser_ctxt *restored = saver->next; /* This one is the old current */
2826 
2827   /* We need to inherit the list of classes to complete/generate */
2828   restored->classd_list = old->classd_list;
2829   restored->class_list = old->class_list;
2830 
2831   /* Restore the current class and function from the saver */
2832   current_class = saver->class_type;
2833   current_function_decl = saver->function_decl;
2834 
2835   /* Retrive the restored context */
2836   ctxp = restored;
2837 
2838   /* Re-installed the data for the parsing to carry on */
2839   memcpy (&ctxp->marker_begining, &old->marker_begining,
2840 	  (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2841 
2842   /* Buffer context can now be discarded */
2843   free (saver);
2844   free (old);
2845 }
2846 
2847 /* Add a new anchor node to which all statement(s) initializing static
2848    and non static initialized upon declaration field(s) will be
2849    linked.  */
2850 
2851 static void
java_parser_context_push_initialized_field()2852 java_parser_context_push_initialized_field ()
2853 {
2854   tree node;
2855 
2856   node = build_tree_list (NULL_TREE, NULL_TREE);
2857   TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2858   CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2859 
2860   node = build_tree_list (NULL_TREE, NULL_TREE);
2861   TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2862   CPC_INITIALIZER_LIST (ctxp) = node;
2863 
2864   node = build_tree_list (NULL_TREE, NULL_TREE);
2865   TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2866   CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2867 }
2868 
2869 /* Pop the lists of initialized field. If this lists aren't empty,
2870    remember them so we can use it to create and populate the finit$
2871    or <clinit> functions. */
2872 
2873 static void
java_parser_context_pop_initialized_field()2874 java_parser_context_pop_initialized_field ()
2875 {
2876   tree stmts;
2877   tree class_type = TREE_TYPE (GET_CPC ());
2878 
2879   if (CPC_INITIALIZER_LIST (ctxp))
2880     {
2881       stmts = CPC_INITIALIZER_STMT (ctxp);
2882       CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2883       if (stmts && !java_error_count)
2884 	TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2885     }
2886 
2887   if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2888     {
2889       stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2890       CPC_STATIC_INITIALIZER_LIST (ctxp) =
2891 	TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2892       /* Keep initialization in order to enforce 8.5 */
2893       if (stmts && !java_error_count)
2894 	TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2895     }
2896 
2897   /* JDK 1.1 instance initializers */
2898   if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2899     {
2900       stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2901       CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2902 	TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2903       if (stmts && !java_error_count)
2904 	TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2905     }
2906 }
2907 
2908 static tree
reorder_static_initialized(list)2909 reorder_static_initialized (list)
2910      tree list;
2911 {
2912   /* We have to keep things in order. The alias initializer have to
2913      come first, then the initialized regular field, in reverse to
2914      keep them in lexical order. */
2915   tree marker, previous = NULL_TREE;
2916   for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2917     if (TREE_CODE (marker) == TREE_LIST
2918 	&& !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2919       break;
2920 
2921   /* No static initialized, the list is fine as is */
2922   if (!previous)
2923     list = TREE_CHAIN (marker);
2924 
2925   /* No marker? reverse the whole list */
2926   else if (!marker)
2927     list = nreverse (list);
2928 
2929   /* Otherwise, reverse what's after the marker and the new reordered
2930      sublist will replace the marker. */
2931   else
2932     {
2933       TREE_CHAIN (previous) = NULL_TREE;
2934       list = nreverse (list);
2935       list = chainon (TREE_CHAIN (marker), list);
2936     }
2937   return list;
2938 }
2939 
2940 /* Helper functions to dump the parser context stack.  */
2941 
2942 #define TAB_CONTEXT(C) \
2943   {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2944 
2945 static void
java_debug_context_do(tab)2946 java_debug_context_do (tab)
2947      int tab;
2948 {
2949   struct parser_ctxt *copy = ctxp;
2950   while (copy)
2951     {
2952       TAB_CONTEXT (tab);
2953       fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2954       TAB_CONTEXT (tab);
2955       fprintf (stderr, "filename: %s\n", copy->filename);
2956       TAB_CONTEXT (tab);
2957       fprintf (stderr, "lineno: %d\n", copy->lineno);
2958       TAB_CONTEXT (tab);
2959       fprintf (stderr, "package: %s\n",
2960 	       (copy->package ?
2961 		IDENTIFIER_POINTER (copy->package) : "<none>"));
2962       TAB_CONTEXT (tab);
2963       fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2964       TAB_CONTEXT (tab);
2965       fprintf (stderr, "saved data: %d\n", copy->saved_data);
2966       copy = copy->next;
2967       tab += 2;
2968     }
2969 }
2970 
2971 /* Dump the stacked up parser contexts. Intended to be called from a
2972    debugger.  */
2973 
2974 void
java_debug_context()2975 java_debug_context ()
2976 {
2977   java_debug_context_do (0);
2978 }
2979 
2980 
2981 
2982 /* Flag for the error report routine to issue the error the first time
2983    it's called (overriding the default behavior which is to drop the
2984    first invocation and honor the second one, taking advantage of a
2985    richer context.  */
2986 static int force_error = 0;
2987 
2988 /* Reporting an constructor invocation error.  */
2989 static void
parse_ctor_invocation_error()2990 parse_ctor_invocation_error ()
2991 {
2992   if (DECL_CONSTRUCTOR_P (current_function_decl))
2993     yyerror ("Constructor invocation must be first thing in a constructor");
2994   else
2995     yyerror ("Only constructors can invoke constructors");
2996 }
2997 
2998 /* Reporting JDK1.1 features not implemented.  */
2999 
3000 static tree
parse_jdk1_1_error(msg)3001 parse_jdk1_1_error (msg)
3002     const char *msg;
3003 {
3004   sorry (": `%s' JDK1.1(TM) feature", msg);
3005   java_error_count++;
3006   return empty_stmt_node;
3007 }
3008 
3009 static int do_warning = 0;
3010 
3011 void
yyerror(msg)3012 yyerror (msg)
3013      const char *msg;
3014 {
3015   static java_lc elc;
3016   static int  prev_lineno;
3017   static const char *prev_msg;
3018 
3019   int save_lineno;
3020   char *remainder, *code_from_source;
3021 
3022   if (!force_error && prev_lineno == lineno)
3023     return;
3024 
3025   /* Save current error location but report latter, when the context is
3026      richer.  */
3027   if (ctxp->java_error_flag == 0)
3028     {
3029       ctxp->java_error_flag = 1;
3030       elc = ctxp->elc;
3031       /* Do something to use the previous line if we're reaching the
3032 	 end of the file... */
3033 #ifdef VERBOSE_SKELETON
3034       printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
3035 #endif
3036       return;
3037     }
3038 
3039   /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3040   if (!force_error && msg == prev_msg && prev_lineno == elc.line)
3041     return;
3042 
3043   ctxp->java_error_flag = 0;
3044   if (do_warning)
3045     java_warning_count++;
3046   else
3047     java_error_count++;
3048 
3049   if (elc.col == 0 && msg && msg[1] == ';')
3050     {
3051       elc.col  = ctxp->p_line->char_col-1;
3052       elc.line = ctxp->p_line->lineno;
3053     }
3054 
3055   save_lineno = lineno;
3056   prev_lineno = lineno = elc.line;
3057   prev_msg = msg;
3058 
3059   code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3060   obstack_grow0 (&temporary_obstack,
3061 		 code_from_source, strlen (code_from_source));
3062   remainder = obstack_finish (&temporary_obstack);
3063   if (do_warning)
3064     warning ("%s.\n%s", msg, remainder);
3065   else
3066     error ("%s.\n%s", msg, remainder);
3067 
3068   /* This allow us to cheaply avoid an extra 'Invalid expression
3069      statement' error report when errors have been already reported on
3070      the same line. This occurs when we report an error but don't have
3071      a synchronization point other than ';', which
3072      expression_statement is the only one to take care of.  */
3073   ctxp->prevent_ese = lineno = save_lineno;
3074 }
3075 
3076 static void
issue_warning_error_from_context(cl,msg,ap)3077 issue_warning_error_from_context (cl, msg, ap)
3078      tree cl;
3079      const char *msg;
3080      va_list ap;
3081 {
3082   const char *saved, *saved_input_filename;
3083   char buffer [4096];
3084   vsprintf (buffer, msg, ap);
3085   force_error = 1;
3086 
3087   ctxp->elc.line = EXPR_WFL_LINENO (cl);
3088   ctxp->elc.col  = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3089 		    (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3090 
3091   /* We have a CL, that's a good reason for using it if it contains data */
3092   saved = ctxp->filename;
3093   if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3094     ctxp->filename = EXPR_WFL_FILENAME (cl);
3095   saved_input_filename = input_filename;
3096   input_filename = ctxp->filename;
3097   java_error (NULL);
3098   java_error (buffer);
3099   ctxp->filename = saved;
3100   input_filename = saved_input_filename;
3101   force_error = 0;
3102 }
3103 
3104 /* Issue an error message at a current source line CL */
3105 
3106 void
parse_error_context(tree cl,const char * msg,...)3107 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3108 {
3109   VA_OPEN (ap, msg);
3110   VA_FIXEDARG (ap, tree, cl);
3111   VA_FIXEDARG (ap, const char *, msg);
3112   issue_warning_error_from_context (cl, msg, ap);
3113   VA_CLOSE (ap);
3114 }
3115 
3116 /* Issue a warning at a current source line CL */
3117 
3118 static void
parse_warning_context(tree cl,const char * msg,...)3119 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3120 {
3121   VA_OPEN (ap, msg);
3122   VA_FIXEDARG (ap, tree, cl);
3123   VA_FIXEDARG (ap, const char *, msg);
3124 
3125   force_error = do_warning = 1;
3126   issue_warning_error_from_context (cl, msg, ap);
3127   do_warning = force_error = 0;
3128   VA_CLOSE (ap);
3129 }
3130 
3131 static tree
find_expr_with_wfl(node)3132 find_expr_with_wfl (node)
3133      tree node;
3134 {
3135   while (node)
3136     {
3137       char code;
3138       tree to_return;
3139 
3140       switch (TREE_CODE (node))
3141 	{
3142 	case BLOCK:
3143 	  node = BLOCK_EXPR_BODY (node);
3144 	  continue;
3145 
3146 	case COMPOUND_EXPR:
3147 	  to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3148 	  if (to_return)
3149 	    return to_return;
3150 	  node = TREE_OPERAND (node, 1);
3151 	  continue;
3152 
3153 	case LOOP_EXPR:
3154 	  node = TREE_OPERAND (node, 0);
3155 	  continue;
3156 
3157 	case LABELED_BLOCK_EXPR:
3158 	  node = TREE_OPERAND (node, 1);
3159 	  continue;
3160 
3161 	default:
3162 	  code = TREE_CODE_CLASS (TREE_CODE (node));
3163 	  if (((code == '1') || (code == '2') || (code == 'e'))
3164 	      && EXPR_WFL_LINECOL (node))
3165 	    return node;
3166 	  return NULL_TREE;
3167 	}
3168     }
3169   return NULL_TREE;
3170 }
3171 
3172 /* Issue a missing return statement error. Uses METHOD to figure the
3173    last line of the method the error occurs in.  */
3174 
3175 static void
missing_return_error(method)3176 missing_return_error (method)
3177      tree method;
3178 {
3179   EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3180   parse_error_context (wfl_operator, "Missing return statement");
3181 }
3182 
3183 /* Issue an unreachable statement error. From NODE, find the next
3184    statement to report appropriately.  */
3185 static void
unreachable_stmt_error(node)3186 unreachable_stmt_error (node)
3187      tree node;
3188 {
3189   /* Browse node to find the next expression node that has a WFL. Use
3190      the location to report the error */
3191   if (TREE_CODE (node) == COMPOUND_EXPR)
3192     node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3193   else
3194     node = find_expr_with_wfl (node);
3195 
3196   if (node)
3197     {
3198       EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3199       parse_error_context (wfl_operator, "Unreachable statement");
3200     }
3201   else
3202     abort ();
3203 }
3204 
3205 int
java_report_errors()3206 java_report_errors ()
3207 {
3208   if (java_error_count)
3209     fprintf (stderr, "%d error%s",
3210 	     java_error_count, (java_error_count == 1 ? "" : "s"));
3211   if (java_warning_count)
3212     fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3213 	     java_warning_count, (java_warning_count == 1 ? "" : "s"));
3214   if (java_error_count || java_warning_count)
3215     putc ('\n', stderr);
3216   return java_error_count;
3217 }
3218 
3219 static char *
java_accstring_lookup(flags)3220 java_accstring_lookup (flags)
3221      int flags;
3222 {
3223   static char buffer [80];
3224 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3225 
3226   /* Access modifier looked-up first for easier report on forbidden
3227      access. */
3228   if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3229   if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3230   if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3231   if (flags & ACC_STATIC) COPY_RETURN ("static");
3232   if (flags & ACC_FINAL) COPY_RETURN ("final");
3233   if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3234   if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3235   if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3236   if (flags & ACC_NATIVE) COPY_RETURN ("native");
3237   if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3238   if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3239 
3240   buffer [0] = '\0';
3241   return buffer;
3242 #undef COPY_RETURN
3243 }
3244 
3245 /* Issuing error messages upon redefinition of classes, interfaces or
3246    variables. */
3247 
3248 static void
classitf_redefinition_error(context,id,decl,cl)3249 classitf_redefinition_error (context, id, decl, cl)
3250      const char *context;
3251      tree id, decl, cl;
3252 {
3253   parse_error_context (cl, "%s `%s' already defined in %s:%d",
3254 		       context, IDENTIFIER_POINTER (id),
3255 		       DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3256   /* Here we should point out where its redefined. It's a unicode. FIXME */
3257 }
3258 
3259 static void
variable_redefinition_error(context,name,type,line)3260 variable_redefinition_error (context, name, type, line)
3261      tree context, name, type;
3262      int line;
3263 {
3264   const char *type_name;
3265 
3266   /* Figure a proper name for type. We might haven't resolved it */
3267   if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3268     type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3269   else
3270     type_name = lang_printable_name (type, 0);
3271 
3272   parse_error_context (context,
3273 		       "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3274 		       IDENTIFIER_POINTER (name),
3275 		       type_name, IDENTIFIER_POINTER (name), line);
3276 }
3277 
3278 /* If ANAME is terminated with `[]', it indicates an array. This
3279    function returns the number of `[]' found and if this number is
3280    greater than zero, it extracts the array type name and places it in
3281    the node pointed to by TRIMMED unless TRIMMED is null.  */
3282 
3283 static int
build_type_name_from_array_name(aname,trimmed)3284 build_type_name_from_array_name (aname, trimmed)
3285      tree aname;
3286      tree *trimmed;
3287 {
3288   const char *name = IDENTIFIER_POINTER (aname);
3289   int len = IDENTIFIER_LENGTH (aname);
3290   int array_dims;
3291 
3292   STRING_STRIP_BRACKETS (name, len, array_dims);
3293 
3294   if (array_dims && trimmed)
3295     *trimmed = get_identifier_with_length (name, len);
3296 
3297   return array_dims;
3298 }
3299 
3300 static tree
build_array_from_name(type,type_wfl,name,ret_name)3301 build_array_from_name (type, type_wfl, name, ret_name)
3302      tree type, type_wfl, name, *ret_name;
3303 {
3304   int more_dims = 0;
3305 
3306   /* Eventually get more dims */
3307   more_dims = build_type_name_from_array_name (name, &name);
3308 
3309   /* If we have, then craft a new type for this variable */
3310   if (more_dims)
3311     {
3312       tree save = type;
3313 
3314       /* If we have a pointer, use its type */
3315       if (TREE_CODE (type) == POINTER_TYPE)
3316         type = TREE_TYPE (type);
3317 
3318       /* Building the first dimension of a primitive type uses this
3319          function */
3320       if (JPRIMITIVE_TYPE_P (type))
3321 	{
3322 	  type = build_java_array_type (type, -1);
3323 	  more_dims--;
3324 	}
3325       /* Otherwise, if we have a WFL for this type, use it (the type
3326          is already an array on an unresolved type, and we just keep
3327          on adding dimensions) */
3328       else if (type_wfl)
3329 	{
3330 	  type = type_wfl;
3331 	  more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3332 							NULL);
3333 	}
3334 
3335       /* Add all the dimensions */
3336       while (more_dims--)
3337 	type = build_unresolved_array_type (type);
3338 
3339       /* The type may have been incomplete in the first place */
3340       if (type_wfl)
3341 	type = obtain_incomplete_type (type);
3342     }
3343 
3344   if (ret_name)
3345     *ret_name = name;
3346   return type;
3347 }
3348 
3349 /* Build something that the type identifier resolver will identify as
3350    being an array to an unresolved type. TYPE_WFL is a WFL on a
3351    identifier. */
3352 
3353 static tree
build_unresolved_array_type(type_or_wfl)3354 build_unresolved_array_type (type_or_wfl)
3355      tree type_or_wfl;
3356 {
3357   const char *ptr;
3358   tree wfl;
3359 
3360   /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3361      just create a array type */
3362   if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3363     return build_java_array_type (type_or_wfl, -1);
3364 
3365   obstack_grow (&temporary_obstack,
3366 		 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3367 		 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3368   obstack_grow0 (&temporary_obstack, "[]", 2);
3369   ptr = obstack_finish (&temporary_obstack);
3370   wfl = build_expr_wfl (get_identifier (ptr),
3371 			EXPR_WFL_FILENAME (type_or_wfl),
3372 			EXPR_WFL_LINENO (type_or_wfl),
3373 			EXPR_WFL_COLNO (type_or_wfl));
3374   /* Re-install the existing qualifications so that the type can be
3375      resolved properly. */
3376   EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3377   return wfl;
3378 }
3379 
3380 static void
parser_add_interface(class_decl,interface_decl,wfl)3381 parser_add_interface (class_decl, interface_decl, wfl)
3382      tree class_decl, interface_decl, wfl;
3383 {
3384   if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3385     parse_error_context (wfl, "Interface `%s' repeated",
3386 			 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3387 }
3388 
3389 /* Bulk of common class/interface checks. Return 1 if an error was
3390    encountered. TAG is 0 for a class, 1 for an interface.  */
3391 
3392 static int
check_class_interface_creation(is_interface,flags,raw_name,qualified_name,decl,cl)3393 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3394      int is_interface, flags;
3395      tree raw_name, qualified_name, decl, cl;
3396 {
3397   tree node;
3398   int sca = 0;			/* Static class allowed */
3399   int icaf = 0;			/* Inner class allowed flags */
3400   int uaaf = CLASS_MODIFIERS;	/* Usually allowed access flags */
3401 
3402   if (!quiet_flag)
3403     fprintf (stderr, " %s%s %s",
3404 	     (CPC_INNER_P () ? "inner" : ""),
3405 	     (is_interface ? "interface" : "class"),
3406 	     IDENTIFIER_POINTER (qualified_name));
3407 
3408   /* Scope of an interface/class type name:
3409        - Can't be imported by a single type import
3410        - Can't already exists in the package */
3411   if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3412       && (node = find_name_in_single_imports (raw_name))
3413       && !CPC_INNER_P ())
3414     {
3415       parse_error_context
3416 	(cl, "%s name `%s' clashes with imported type `%s'",
3417 	 (is_interface ? "Interface" : "Class"),
3418 	 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3419       return 1;
3420     }
3421   if (decl && CLASS_COMPLETE_P (decl))
3422     {
3423       classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3424 				   qualified_name, decl, cl);
3425       return 1;
3426     }
3427 
3428   if (check_inner_class_redefinition (raw_name, cl))
3429     return 1;
3430 
3431   /* If public, file name should match class/interface name, except
3432      when dealing with an inner class */
3433   if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3434     {
3435       const char *f;
3436 
3437       for (f = &input_filename [strlen (input_filename)];
3438 	   f != input_filename && ! IS_DIR_SEPARATOR (f[0]);
3439 	   f--)
3440 	;
3441       if (IS_DIR_SEPARATOR (f[0]))
3442 	f++;
3443       if (strncmp (IDENTIFIER_POINTER (raw_name),
3444 		   f , IDENTIFIER_LENGTH (raw_name)) ||
3445 	  f [IDENTIFIER_LENGTH (raw_name)] != '.')
3446 	parse_error_context
3447 	  (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3448 			     (is_interface ? "interface" : "class"),
3449 			     IDENTIFIER_POINTER (qualified_name),
3450 			     IDENTIFIER_POINTER (raw_name));
3451     }
3452 
3453   /* Static classes can be declared only in top level classes. Note:
3454      once static, a inner class is a top level class. */
3455   if (flags & ACC_STATIC)
3456     {
3457       /* Catch the specific error of declaring an class inner class
3458 	 with no toplevel enclosing class. Prevent check_modifiers from
3459 	 complaining a second time */
3460       if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3461 	{
3462 	  parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3463 			       IDENTIFIER_POINTER (qualified_name));
3464 	  sca = ACC_STATIC;
3465 	}
3466       /* Else, in the context of a top-level class declaration, let
3467          `check_modifiers' do its job, otherwise, give it a go */
3468       else
3469 	sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3470     }
3471 
3472   /* Inner classes can be declared private or protected
3473      within their enclosing classes. */
3474   if (CPC_INNER_P ())
3475     {
3476       /* A class which is local to a block can't be public, private,
3477 	 protected or static. But it is created final, so allow this
3478 	 one. */
3479       if (current_function_decl)
3480 	icaf = sca = uaaf = ACC_FINAL;
3481       else
3482 	{
3483 	  check_modifiers_consistency (flags);
3484 	  icaf = ACC_PROTECTED;
3485 	  if (! CLASS_INTERFACE (GET_CPC ()))
3486 	    icaf |= ACC_PRIVATE;
3487 	}
3488     }
3489 
3490   if (is_interface)
3491     {
3492       if (CPC_INNER_P ())
3493 	uaaf = INTERFACE_INNER_MODIFIERS;
3494       else
3495 	uaaf = INTERFACE_MODIFIERS;
3496 
3497       check_modifiers ("Illegal modifier `%s' for interface declaration",
3498 		       flags, uaaf);
3499     }
3500   else
3501     check_modifiers ((current_function_decl ?
3502 		      "Illegal modifier `%s' for local class declaration" :
3503 		      "Illegal modifier `%s' for class declaration"),
3504 		     flags, uaaf|sca|icaf);
3505   return 0;
3506 }
3507 
3508 /* Construct a nested class name.  If the final component starts with
3509    a digit, return true.  Otherwise return false.  */
3510 static int
make_nested_class_name(cpc_list)3511 make_nested_class_name (cpc_list)
3512      tree cpc_list;
3513 {
3514   tree name;
3515 
3516   if (!cpc_list)
3517     return 0;
3518 
3519   make_nested_class_name (TREE_CHAIN (cpc_list));
3520 
3521   /* Pick the qualified name when dealing with the first upmost
3522      enclosing class */
3523   name = (TREE_CHAIN (cpc_list)
3524 	  ? TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3525   obstack_grow (&temporary_obstack,
3526 		IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3527   obstack_1grow (&temporary_obstack, '$');
3528 
3529   return ISDIGIT (IDENTIFIER_POINTER (name)[0]);
3530 }
3531 
3532 /* Can't redefine a class already defined in an earlier scope. */
3533 
3534 static int
check_inner_class_redefinition(raw_name,cl)3535 check_inner_class_redefinition (raw_name, cl)
3536      tree raw_name, cl;
3537 {
3538   tree scope_list;
3539 
3540   for (scope_list = GET_CPC_LIST (); scope_list;
3541        scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3542     if (raw_name == GET_CPC_UN_NODE (scope_list))
3543       {
3544 	parse_error_context
3545 	  (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",
3546 	   IDENTIFIER_POINTER (raw_name));
3547 	return 1;
3548       }
3549   return 0;
3550 }
3551 
3552 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. If we fail,
3553    we remember ENCLOSING and SUPER.  */
3554 
3555 static tree
resolve_inner_class(circularity_hash,cl,enclosing,super,class_type)3556 resolve_inner_class (circularity_hash, cl, enclosing, super, class_type)
3557      htab_t circularity_hash;
3558      tree cl, *enclosing, *super, class_type;
3559 {
3560   tree local_enclosing = *enclosing;
3561   tree local_super = NULL_TREE;
3562 
3563   while (local_enclosing)
3564     {
3565       tree intermediate, decl;
3566 
3567       *htab_find_slot (circularity_hash, local_enclosing, INSERT) =
3568 	local_enclosing;
3569 
3570       if ((decl = find_as_inner_class (local_enclosing, class_type, cl)))
3571 	return decl;
3572 
3573       intermediate = local_enclosing;
3574       /* Explore enclosing contexts. */
3575       while (INNER_CLASS_DECL_P (intermediate))
3576 	{
3577 	  intermediate = DECL_CONTEXT (intermediate);
3578 	  if ((decl = find_as_inner_class (intermediate, class_type, cl)))
3579 	    return decl;
3580 	}
3581 
3582       /* Now go to the upper classes, bail out if necessary. We will
3583 	 analyze the returned SUPER and act accordingly (see
3584 	 do_resolve_class.) */
3585       local_super = CLASSTYPE_SUPER (TREE_TYPE (local_enclosing));
3586       if (!local_super || local_super == object_type_node)
3587         break;
3588 
3589       if (TREE_CODE (local_super) == POINTER_TYPE)
3590         local_super = do_resolve_class (NULL, local_super, NULL, NULL);
3591       else
3592 	local_super = TYPE_NAME (local_super);
3593 
3594       /* We may not have checked for circular inheritance yet, so do so
3595          here to prevent an infinite loop. */
3596       if (htab_find (circularity_hash, local_super) != NULL)
3597         {
3598           if (!cl)
3599             cl = lookup_cl (local_enclosing);
3600 
3601           parse_error_context
3602             (cl, "Cyclic inheritance involving %s",
3603 	     IDENTIFIER_POINTER (DECL_NAME (local_enclosing)));
3604 	  local_enclosing = NULL_TREE;
3605         }
3606       else
3607 	local_enclosing = local_super;
3608     }
3609 
3610   /* We failed. Return LOCAL_SUPER and LOCAL_ENCLOSING. */
3611   *super = local_super;
3612   *enclosing = local_enclosing;
3613 
3614   return NULL_TREE;
3615 }
3616 
3617 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3618    qualified. */
3619 
3620 static tree
find_as_inner_class(enclosing,name,cl)3621 find_as_inner_class (enclosing, name, cl)
3622      tree enclosing, name, cl;
3623 {
3624   tree qual, to_return;
3625   if (!enclosing)
3626     return NULL_TREE;
3627 
3628   name = TYPE_NAME (name);
3629 
3630   /* First search: within the scope of `enclosing', search for name */
3631   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3632     qual = EXPR_WFL_QUALIFICATION (cl);
3633   else if (cl)
3634     qual = build_tree_list (cl, NULL_TREE);
3635   else
3636     qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3637 
3638   if ((to_return = find_as_inner_class_do (qual, enclosing)))
3639     return to_return;
3640 
3641   /* We're dealing with a qualified name. Try to resolve thing until
3642      we get something that is an enclosing class. */
3643   if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3644     {
3645       tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3646 
3647       for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3648 	   qual = TREE_CHAIN (qual))
3649 	{
3650 	  acc = merge_qualified_name (acc,
3651 				      EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3652 	  BUILD_PTR_FROM_NAME (ptr, acc);
3653 	  decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3654 	}
3655 
3656       /* A NULL qual and a decl means that the search ended
3657          successfully?!? We have to do something then. FIXME */
3658 
3659       if (decl)
3660 	enclosing = decl;
3661       else
3662 	qual = EXPR_WFL_QUALIFICATION (cl);
3663     }
3664   /* Otherwise, create a qual for the other part of the resolution. */
3665   else
3666     qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3667 
3668   return find_as_inner_class_do (qual, enclosing);
3669 }
3670 
3671 /* We go inside the list of sub classes and try to find a way
3672    through. */
3673 
3674 static tree
find_as_inner_class_do(qual,enclosing)3675 find_as_inner_class_do (qual, enclosing)
3676      tree qual, enclosing;
3677 {
3678   if (!qual)
3679     return NULL_TREE;
3680 
3681   for (; qual && enclosing; qual = TREE_CHAIN (qual))
3682     {
3683       tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3684       tree next_enclosing = NULL_TREE;
3685       tree inner_list;
3686 
3687       for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3688            inner_list; inner_list = TREE_CHAIN (inner_list))
3689 	{
3690 	  if (TREE_VALUE (inner_list) == name_to_match)
3691 	    {
3692 	      next_enclosing = TREE_PURPOSE (inner_list);
3693 	      break;
3694 	    }
3695 	}
3696       enclosing = next_enclosing;
3697     }
3698 
3699   return (!qual && enclosing ? enclosing : NULL_TREE);
3700 }
3701 
3702 /* Reach all inner classes and tie their unqualified name to a
3703    DECL. */
3704 
3705 static void
set_nested_class_simple_name_value(outer,set)3706 set_nested_class_simple_name_value (outer, set)
3707      tree outer;
3708      int set;
3709 {
3710   tree l;
3711 
3712   for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3713     IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3714 						TREE_PURPOSE (l) : NULL_TREE);
3715 }
3716 
3717 static void
link_nested_class_to_enclosing()3718 link_nested_class_to_enclosing ()
3719 {
3720   if (GET_ENCLOSING_CPC ())
3721     {
3722       tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3723       DECL_INNER_CLASS_LIST (enclosing) =
3724 	tree_cons (GET_CPC (), GET_CPC_UN (),
3725 		   DECL_INNER_CLASS_LIST (enclosing));
3726     }
3727 }
3728 
3729 static tree
maybe_make_nested_class_name(name)3730 maybe_make_nested_class_name (name)
3731      tree name;
3732 {
3733   tree id = NULL_TREE;
3734 
3735   if (CPC_INNER_P ())
3736     {
3737       /* If we're in a function, we must append a number to create the
3738 	 nested class name.  However, we don't do this if the class we
3739 	 are constructing is anonymous, because in that case we'll
3740 	 already have a number as the class name.  */
3741       if (! make_nested_class_name (GET_CPC_LIST ())
3742 	  && current_function_decl != NULL_TREE
3743 	  && ! ISDIGIT (IDENTIFIER_POINTER (name)[0]))
3744 	{
3745 	  char buf[10];
3746 	  sprintf (buf, "%d", anonymous_class_counter);
3747 	  ++anonymous_class_counter;
3748 	  obstack_grow (&temporary_obstack, buf, strlen (buf));
3749 	  obstack_1grow (&temporary_obstack, '$');
3750 	}
3751       obstack_grow0 (&temporary_obstack,
3752 		     IDENTIFIER_POINTER (name),
3753 		     IDENTIFIER_LENGTH (name));
3754       id = get_identifier (obstack_finish (&temporary_obstack));
3755       if (ctxp->package)
3756 	QUALIFIED_P (id) = 1;
3757     }
3758   return id;
3759 }
3760 
3761 /* If DECL is NULL, create and push a new DECL, record the current
3762    line CL and do other maintenance things.  */
3763 
3764 static tree
maybe_create_class_interface_decl(decl,raw_name,qualified_name,cl)3765 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3766      tree decl, raw_name, qualified_name, cl;
3767 {
3768   if (!decl)
3769     decl = push_class (make_class (), qualified_name);
3770 
3771   /* Take care of the file and line business */
3772   DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3773   /* If we're emiting xrefs, store the line/col number information */
3774   if (flag_emit_xref)
3775     DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3776   else
3777     DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3778   CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3779   CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3780   CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3781     IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3782 
3783   PUSH_CPC (decl, raw_name);
3784   DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3785 
3786   /* Link the declaration to the already seen ones */
3787   TREE_CHAIN (decl) = ctxp->class_list;
3788   ctxp->class_list = decl;
3789 
3790   /* Create a new nodes in the global lists */
3791   gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3792   all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3793 
3794   /* Install a new dependency list element */
3795   create_jdep_list (ctxp);
3796 
3797   SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3798 			  IDENTIFIER_POINTER (qualified_name)));
3799   return decl;
3800 }
3801 
3802 static void
add_superinterfaces(decl,interface_list)3803 add_superinterfaces (decl, interface_list)
3804      tree decl, interface_list;
3805 {
3806   tree node;
3807   /* Superinterface(s): if present and defined, parser_check_super_interface ()
3808      takes care of ensuring that:
3809        - This is an accessible interface type,
3810        - Circularity detection.
3811    parser_add_interface is then called. If present but not defined,
3812    the check operation is delayed until the super interface gets
3813    defined.  */
3814   for (node = interface_list; node; node = TREE_CHAIN (node))
3815     {
3816       tree current = TREE_PURPOSE (node);
3817       tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3818       if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3819 	{
3820 	  if (!parser_check_super_interface (idecl, decl, current))
3821 	    parser_add_interface (decl, idecl, current);
3822 	}
3823       else
3824 	register_incomplete_type (JDEP_INTERFACE,
3825 				  current, decl, NULL_TREE);
3826     }
3827 }
3828 
3829 /* Create an interface in pass1 and return its decl. Return the
3830    interface's decl in pass 2.  */
3831 
3832 static tree
create_interface(flags,id,super)3833 create_interface (flags, id, super)
3834      int flags;
3835      tree id, super;
3836 {
3837   tree raw_name = EXPR_WFL_NODE (id);
3838   tree q_name = parser_qualified_classname (raw_name);
3839   tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3840 
3841   /* Certain syntax errors are making SUPER be like ID. Avoid this
3842      case. */
3843   if (ctxp->class_err && id == super)
3844     super = NULL;
3845 
3846   EXPR_WFL_NODE (id) = q_name;	/* Keep source location, even if refined. */
3847 
3848   /* Basic checks: scope, redefinition, modifiers */
3849   if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3850     {
3851       PUSH_ERROR ();
3852       return NULL_TREE;
3853     }
3854 
3855   /* Suspend the current parsing context if we're parsing an inner
3856      interface */
3857   if (CPC_INNER_P ())
3858     {
3859       java_parser_context_suspend ();
3860       /* Interface members are public. */
3861       if (CLASS_INTERFACE (GET_CPC ()))
3862 	flags |= ACC_PUBLIC;
3863     }
3864 
3865   /* Push a new context for (static) initialized upon declaration fields */
3866   java_parser_context_push_initialized_field ();
3867 
3868   /* Interface modifiers check
3869        - public/abstract allowed (already done at that point)
3870        - abstract is obsolete (comes first, it's a warning, or should be)
3871        - Can't use twice the same (checked in the modifier rule) */
3872   if ((flags & ACC_ABSTRACT) && flag_redundant)
3873     parse_warning_context
3874       (MODIFIER_WFL (ABSTRACT_TK),
3875        "Redundant use of `abstract' modifier. Interface `%s' is implicitly abstract", IDENTIFIER_POINTER (raw_name));
3876 
3877   /* Create a new decl if DECL is NULL, otherwise fix it */
3878   decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3879 
3880   /* Set super info and mark the class a complete */
3881   set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3882 		  object_type_node, ctxp->interface_number);
3883   ctxp->interface_number = 0;
3884   CLASS_COMPLETE_P (decl) = 1;
3885   add_superinterfaces (decl, super);
3886 
3887   return decl;
3888 }
3889 
3890 /* Patch anonymous class CLASS, by either extending or implementing
3891    DEP.  */
3892 
3893 static void
patch_anonymous_class(type_decl,class_decl,wfl)3894 patch_anonymous_class (type_decl, class_decl, wfl)
3895     tree type_decl, class_decl, wfl;
3896 {
3897   tree class = TREE_TYPE (class_decl);
3898   tree type =  TREE_TYPE (type_decl);
3899   tree binfo = TYPE_BINFO (class);
3900 
3901   /* If it's an interface, implement it */
3902   if (CLASS_INTERFACE (type_decl))
3903     {
3904       tree s_binfo;
3905       int length;
3906 
3907       if (parser_check_super_interface (type_decl, class_decl, wfl))
3908 	return;
3909 
3910       s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3911       length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3912       TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3913       TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3914       /* And add the interface */
3915       parser_add_interface (class_decl, type_decl, wfl);
3916     }
3917   /* Otherwise, it's a type we want to extend */
3918   else
3919     {
3920       if (parser_check_super (type_decl, class_decl, wfl))
3921 	return;
3922       BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3923     }
3924 }
3925 
3926 static tree
create_anonymous_class(location,type_name)3927 create_anonymous_class (location, type_name)
3928     int location;
3929     tree type_name;
3930 {
3931   char buffer [80];
3932   tree super = NULL_TREE, itf = NULL_TREE;
3933   tree id, type_decl, class;
3934 
3935   /* The unqualified name of the anonymous class. It's just a number. */
3936   sprintf (buffer, "%d", anonymous_class_counter++);
3937   id = build_wfl_node (get_identifier (buffer));
3938   EXPR_WFL_LINECOL (id) = location;
3939 
3940   /* We know about the type to extend/implement. We go ahead */
3941   if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3942     {
3943       /* Create a class which either implements on extends the designated
3944 	 class. The class bears an innacessible name. */
3945       if (CLASS_INTERFACE (type_decl))
3946 	{
3947 	  /* It's OK to modify it here. It's been already used and
3948              shouldn't be reused */
3949 	  ctxp->interface_number = 1;
3950 	  /* Interfaces should presented as a list of WFLs */
3951 	  itf = build_tree_list (type_name, NULL_TREE);
3952 	}
3953       else
3954 	super = type_name;
3955     }
3956 
3957   class = create_class (ACC_FINAL, id, super, itf);
3958 
3959   /* We didn't know anything about the stuff. We register a dependence. */
3960   if (!type_decl)
3961     register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3962 
3963   ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3964   return class;
3965 }
3966 
3967 /* Create a class in pass1 and return its decl. Return class
3968    interface's decl in pass 2.  */
3969 
3970 static tree
create_class(flags,id,super,interfaces)3971 create_class (flags, id, super, interfaces)
3972      int flags;
3973      tree id, super, interfaces;
3974 {
3975   tree raw_name = EXPR_WFL_NODE (id);
3976   tree class_id, decl;
3977   tree super_decl_type;
3978 
3979   /* Certain syntax errors are making SUPER be like ID. Avoid this
3980      case. */
3981   if (ctxp->class_err && id == super)
3982     super = NULL;
3983 
3984   class_id = parser_qualified_classname (raw_name);
3985   decl = IDENTIFIER_CLASS_VALUE (class_id);
3986   EXPR_WFL_NODE (id) = class_id;
3987 
3988   /* Basic check: scope, redefinition, modifiers */
3989   if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3990     {
3991       PUSH_ERROR ();
3992       return NULL_TREE;
3993     }
3994 
3995   /* Suspend the current parsing context if we're parsing an inner
3996      class or an anonymous class. */
3997   if (CPC_INNER_P ())
3998     {
3999       java_parser_context_suspend ();
4000       /* Interface members are public. */
4001       if (CLASS_INTERFACE (GET_CPC ()))
4002 	flags |= ACC_PUBLIC;
4003     }
4004 
4005   /* Push a new context for (static) initialized upon declaration fields */
4006   java_parser_context_push_initialized_field ();
4007 
4008   /* Class modifier check:
4009        - Allowed modifier (already done at that point)
4010        - abstract AND final forbidden
4011        - Public classes defined in the correct file */
4012   if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
4013     parse_error_context
4014       (id, "Class `%s' can't be declared both abstract and final",
4015        IDENTIFIER_POINTER (raw_name));
4016 
4017   /* Create a new decl if DECL is NULL, otherwise fix it */
4018   decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
4019 
4020   /* If SUPER exists, use it, otherwise use Object */
4021   if (super)
4022     {
4023       /* Can't extend java.lang.Object */
4024       if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4025 	{
4026 	  parse_error_context (id, "Can't extend `java.lang.Object'");
4027 	  return NULL_TREE;
4028 	}
4029 
4030       super_decl_type =
4031 	register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4032     }
4033   else if (TREE_TYPE (decl) != object_type_node)
4034     super_decl_type = object_type_node;
4035   /* We're defining java.lang.Object */
4036   else
4037     super_decl_type = NULL_TREE;
4038 
4039   /* A class nested in an interface is implicitly static. */
4040   if (INNER_CLASS_DECL_P (decl)
4041       && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4042     {
4043       flags |= ACC_STATIC;
4044     }
4045 
4046   /* Set super info and mark the class as complete. */
4047   set_super_info (flags, TREE_TYPE (decl), super_decl_type,
4048 		  ctxp->interface_number);
4049   ctxp->interface_number = 0;
4050   CLASS_COMPLETE_P (decl) = 1;
4051   add_superinterfaces (decl, interfaces);
4052 
4053   /* Add the private this$<n> field, Replicate final locals still in
4054      scope as private final fields mangled like val$<local_name>.
4055      This doesn't not occur for top level (static) inner classes. */
4056   if (PURE_INNER_CLASS_DECL_P (decl))
4057     add_inner_class_fields (decl, current_function_decl);
4058 
4059   /* If doing xref, store the location at which the inherited class
4060      (if any) was seen. */
4061   if (flag_emit_xref && super)
4062     DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
4063 
4064   /* Eventually sets the @deprecated tag flag */
4065   CHECK_DEPRECATED (decl);
4066 
4067   /* Reset the anonymous class counter when declaring non inner classes */
4068   if (!INNER_CLASS_DECL_P (decl))
4069     anonymous_class_counter = 1;
4070 
4071   return decl;
4072 }
4073 
4074 /* End a class declaration: register the statements used to create
4075    finit$ and <clinit>, pop the current class and resume the prior
4076    parser context if necessary.  */
4077 
4078 static void
end_class_declaration(resume)4079 end_class_declaration (resume)
4080      int resume;
4081 {
4082   /* If an error occurred, context weren't pushed and won't need to be
4083      popped by a resume. */
4084   int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4085 
4086   if (GET_CPC () != error_mark_node)
4087     dump_java_tree (TDI_class, GET_CPC ());
4088 
4089   java_parser_context_pop_initialized_field ();
4090   POP_CPC ();
4091   if (resume && no_error_occurred)
4092     java_parser_context_resume ();
4093 
4094   /* We're ending a class declaration, this is a good time to reset
4095      the interface cout. Note that might have been already done in
4096      create_interface, but if at that time an inner class was being
4097      dealt with, the interface count was reset in a context created
4098      for the sake of handling inner classes declaration. */
4099   ctxp->interface_number = 0;
4100 }
4101 
4102 static void
add_inner_class_fields(class_decl,fct_decl)4103 add_inner_class_fields (class_decl, fct_decl)
4104      tree class_decl;
4105      tree fct_decl;
4106 {
4107   tree block, marker, f;
4108 
4109   f = add_field (TREE_TYPE (class_decl),
4110 		 build_current_thisn (TREE_TYPE (class_decl)),
4111 		 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4112 		 ACC_PRIVATE);
4113   FIELD_THISN (f) = 1;
4114 
4115   if (!fct_decl)
4116     return;
4117 
4118   for (block = GET_CURRENT_BLOCK (fct_decl);
4119        block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4120     {
4121       tree decl;
4122       for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4123 	{
4124 	  tree name, pname;
4125 	  tree wfl, init, list;
4126 
4127 	  /* Avoid non final arguments. */
4128 	  if (!LOCAL_FINAL_P (decl))
4129 	    continue;
4130 
4131 	  MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4132 	  MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4133 	  wfl = build_wfl_node (name);
4134 	  init = build_wfl_node (pname);
4135 	  /* Build an initialization for the field: it will be
4136 	     initialized by a parameter added to finit$, bearing a
4137 	     mangled name of the field itself (param$<n>.) The
4138 	     parameter is provided to finit$ by the constructor
4139 	     invoking it (hence the constructor will also feature a
4140 	     hidden parameter, set to the value of the outer context
4141 	     local at the time the inner class is created.)
4142 
4143 	     Note: we take into account all possible locals that can
4144 	     be accessed by the inner class. It's actually not trivial
4145 	     to minimize these aliases down to the ones really
4146 	     used. One way to do that would be to expand all regular
4147 	     methods first, then finit$ to get a picture of what's
4148 	     used.  It works with the exception that we would have to
4149 	     go back on all constructor invoked in regular methods to
4150 	     have their invokation reworked (to include the right amount
4151 	     of alias initializer parameters.)
4152 
4153 	     The only real way around, I think, is a first pass to
4154 	     identify locals really used in the inner class. We leave
4155 	     the flag FIELD_LOCAL_ALIAS_USED around for that future
4156 	     use.
4157 
4158 	     On the other hand, it only affect local inner classes,
4159 	     whose constructors (and finit$ call) will be featuring
4160 	     unecessary arguments. It's easy for a developper to keep
4161 	     this number of parameter down by using the `final'
4162 	     keyword only when necessary. For the time being, we can
4163 	     issue a warning on unecessary finals. FIXME */
4164 	  init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4165 				   wfl, init);
4166 
4167 	  /* Register the field. The TREE_LIST holding the part
4168 	     initialized/initializer will be marked ARG_FINAL_P so
4169 	     that the created field can be marked
4170 	     FIELD_LOCAL_ALIAS. */
4171 	  list = build_tree_list (wfl, init);
4172 	  ARG_FINAL_P (list) = 1;
4173 	  register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4174 	}
4175     }
4176 
4177   if (!CPC_INITIALIZER_STMT (ctxp))
4178     return;
4179 
4180   /* If we ever registered an alias field, insert and marker to
4181      remeber where the list ends. The second part of the list (the one
4182      featuring initialized fields) so it can be later reversed to
4183      enforce 8.5. The marker will be removed during that operation. */
4184   marker = build_tree_list (NULL_TREE, NULL_TREE);
4185   TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4186   SET_CPC_INITIALIZER_STMT (ctxp, marker);
4187 }
4188 
4189 /* Can't use lookup_field () since we don't want to load the class and
4190    can't set the CLASS_LOADED_P flag */
4191 
4192 static tree
find_field(class,name)4193 find_field (class, name)
4194      tree class;
4195      tree name;
4196 {
4197   tree decl;
4198   for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4199     {
4200       if (DECL_NAME (decl) == name)
4201 	return decl;
4202     }
4203   return NULL_TREE;
4204 }
4205 
4206 /* Wrap around lookup_field that doesn't potentially upset the value
4207    of CLASS */
4208 
4209 static tree
lookup_field_wrapper(class,name)4210 lookup_field_wrapper (class, name)
4211      tree class, name;
4212 {
4213   tree type = class;
4214   tree decl = NULL_TREE;
4215   java_parser_context_save_global ();
4216 
4217   /* Last chance: if we're within the context of an inner class, we
4218      might be trying to access a local variable defined in an outer
4219      context. We try to look for it now. */
4220   if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4221     {
4222       tree new_name;
4223       MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4224       decl = lookup_field (&type, new_name);
4225       if (decl && decl != error_mark_node)
4226 	FIELD_LOCAL_ALIAS_USED (decl) = 1;
4227     }
4228   if (!decl || decl == error_mark_node)
4229     {
4230       type = class;
4231       decl = lookup_field (&type, name);
4232     }
4233 
4234   /* If the field still hasn't been found, try the next enclosing context. */
4235   if (!decl && INNER_CLASS_TYPE_P (class))
4236     {
4237       tree outer_type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
4238       decl = lookup_field_wrapper (outer_type, name);
4239     }
4240 
4241   java_parser_context_restore_global ();
4242   return decl == error_mark_node ? NULL : decl;
4243 }
4244 
4245 /* Find duplicate field within the same class declarations and report
4246    the error. Returns 1 if a duplicated field was found, 0
4247    otherwise.  */
4248 
4249 static int
duplicate_declaration_error_p(new_field_name,new_type,cl)4250 duplicate_declaration_error_p (new_field_name, new_type, cl)
4251      tree new_field_name, new_type, cl;
4252 {
4253   /* This might be modified to work with method decl as well */
4254   tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4255   if (decl)
4256     {
4257       char *t1 = xstrdup (purify_type_name
4258 			 ((TREE_CODE (new_type) == POINTER_TYPE
4259 			   && TREE_TYPE (new_type) == NULL_TREE) ?
4260 			  IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4261 			  lang_printable_name (new_type, 1)));
4262       /* The type may not have been completed by the time we report
4263 	 the error */
4264       char *t2 = xstrdup (purify_type_name
4265 			 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4266 			   && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4267 			  IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4268 			  lang_printable_name (TREE_TYPE (decl), 1)));
4269       parse_error_context
4270 	(cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4271 	 t1, IDENTIFIER_POINTER (new_field_name),
4272 	 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4273 	 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4274       free (t1);
4275       free (t2);
4276       return 1;
4277     }
4278   return 0;
4279 }
4280 
4281 /* Field registration routine. If TYPE doesn't exist, field
4282    declarations are linked to the undefined TYPE dependency list, to
4283    be later resolved in java_complete_class () */
4284 
4285 static void
register_fields(flags,type,variable_list)4286 register_fields (flags, type, variable_list)
4287      int flags;
4288      tree type, variable_list;
4289 {
4290   tree current, saved_type;
4291   tree class_type = NULL_TREE;
4292   int saved_lineno = lineno;
4293   int must_chain = 0;
4294   tree wfl = NULL_TREE;
4295 
4296   if (GET_CPC ())
4297     class_type = TREE_TYPE (GET_CPC ());
4298 
4299   if (!class_type || class_type == error_mark_node)
4300     return;
4301 
4302   /* If we're adding fields to interfaces, those fields are public,
4303      static, final */
4304   if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4305     {
4306       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4307 				 flags, ACC_PUBLIC, "interface field(s)");
4308       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4309 				 flags, ACC_STATIC, "interface field(s)");
4310       OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4311 				 flags, ACC_FINAL, "interface field(s)");
4312       check_modifiers ("Illegal interface member modifier `%s'", flags,
4313 		       INTERFACE_FIELD_MODIFIERS);
4314       flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4315     }
4316 
4317   /* Obtain a suitable type for resolution, if necessary */
4318   SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4319 
4320   /* If TYPE is fully resolved and we don't have a reference, make one */
4321   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4322 
4323   for (current = variable_list, saved_type = type; current;
4324        current = TREE_CHAIN (current), type = saved_type)
4325     {
4326       tree real_type;
4327       tree field_decl;
4328       tree cl = TREE_PURPOSE (current);
4329       tree init = TREE_VALUE (current);
4330       tree current_name = EXPR_WFL_NODE (cl);
4331 
4332       /* Can't declare non-final static fields in inner classes */
4333       if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4334           && !(flags & ACC_FINAL))
4335 	parse_error_context
4336           (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4337 	   IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4338 	   lang_printable_name (class_type, 0));
4339 
4340       /* Process NAME, as it may specify extra dimension(s) for it */
4341       type = build_array_from_name (type, wfl, current_name, &current_name);
4342 
4343       /* Type adjustment. We may have just readjusted TYPE because
4344 	 the variable specified more dimensions. Make sure we have
4345 	 a reference if we can and don't have one already. Also
4346 	 change the name if we have an init. */
4347       if (type != saved_type)
4348 	{
4349 	  PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4350 	  if (init)
4351 	    EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4352 	}
4353 
4354       real_type = GET_REAL_TYPE (type);
4355       /* Check for redeclarations */
4356       if (duplicate_declaration_error_p (current_name, real_type, cl))
4357 	continue;
4358 
4359       /* Set lineno to the line the field was found and create a
4360          declaration for it. Eventually sets the @deprecated tag flag. */
4361       if (flag_emit_xref)
4362 	lineno = EXPR_WFL_LINECOL (cl);
4363       else
4364 	lineno = EXPR_WFL_LINENO (cl);
4365       field_decl = add_field (class_type, current_name, real_type, flags);
4366       CHECK_DEPRECATED (field_decl);
4367 
4368       /* If the field denotes a final instance variable, then we
4369 	 allocate a LANG_DECL_SPECIFIC part to keep track of its
4370 	 initialization. We also mark whether the field was
4371 	 initialized upon its declaration. We don't do that if the
4372 	 created field is an alias to a final local. */
4373       if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4374 	{
4375 	  MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4376 	  DECL_FIELD_FINAL_WFL (field_decl) = cl;
4377 	}
4378 
4379       /* If the couple initializer/initialized is marked ARG_FINAL_P,
4380 	 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4381 	 hide parameters to this inner class finit$ and
4382 	 constructors. It also means that the field isn't final per
4383 	 say. */
4384       if (ARG_FINAL_P (current))
4385 	{
4386 	  FIELD_LOCAL_ALIAS (field_decl) = 1;
4387 	  FIELD_FINAL (field_decl) = 0;
4388 	}
4389 
4390       /* Check if we must chain. */
4391       if (must_chain)
4392 	register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4393 
4394       /* If we have an initialization value tied to the field */
4395       if (init)
4396 	{
4397 	  /* The field is declared static */
4398 	  if (flags & ACC_STATIC)
4399 	    {
4400 	      /* We include the field and its initialization part into
4401 		 a list used to generate <clinit>. After <clinit> is
4402 		 walked, field initializations will be processed and
4403 		 fields initialized with known constants will be taken
4404 		 out of <clinit> and have their DECL_INITIAL set
4405 		 appropriately. */
4406 	      TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4407 	      SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4408 	      if (TREE_OPERAND (init, 1)
4409 		  && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4410 		TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4411 	    }
4412 	  /* A non-static field declared with an immediate initialization is
4413 	     to be initialized in <init>, if any.  This field is remembered
4414 	     to be processed at the time of the generation of <init>. */
4415 	  else
4416 	    {
4417 	      TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4418 	      SET_CPC_INITIALIZER_STMT (ctxp, init);
4419 	    }
4420 	  MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4421 	  DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4422 	}
4423     }
4424   lineno = saved_lineno;
4425 }
4426 
4427 /* Generate finit$, using the list of initialized fields to populate
4428    its body. finit$'s parameter(s) list is adjusted to include the
4429    one(s) used to initialized the field(s) caching outer context
4430    local(s).  */
4431 
4432 static tree
generate_finit(class_type)4433 generate_finit (class_type)
4434      tree class_type;
4435 {
4436   int count = 0;
4437   tree list = TYPE_FINIT_STMT_LIST (class_type);
4438   tree mdecl, current, parms;
4439 
4440   parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4441 						  class_type, NULL_TREE,
4442 						  &count);
4443   CRAFTED_PARAM_LIST_FIXUP (parms);
4444   mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4445 				    finit_identifier_node, parms);
4446   fix_method_argument_names (parms, mdecl);
4447   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4448 		       mdecl, NULL_TREE);
4449   DECL_FUNCTION_NAP (mdecl) = count;
4450   start_artificial_method_body (mdecl);
4451 
4452   for (current = list; current; current = TREE_CHAIN (current))
4453     java_method_add_stmt (mdecl,
4454 			  build_debugable_stmt (EXPR_WFL_LINECOL (current),
4455 						current));
4456   end_artificial_method_body (mdecl);
4457   return mdecl;
4458 }
4459 
4460 /* Generate a function to run the instance initialization code. The
4461    private method is called `instinit$'. Unless we're dealing with an
4462    anonymous class, we determine whether all ctors of CLASS_TYPE
4463    declare a checked exception in their `throws' clause in order to
4464    see whether it's necessary to encapsulate the instance initializer
4465    statements in a try/catch/rethrow sequence.  */
4466 
4467 static tree
generate_instinit(class_type)4468 generate_instinit (class_type)
4469      tree class_type;
4470 {
4471   tree current;
4472   tree compound = NULL_TREE;
4473   tree parms = tree_cons (this_identifier_node,
4474 			  build_pointer_type (class_type), end_params_node);
4475   tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4476 					 void_type_node,
4477 					 instinit_identifier_node, parms);
4478 
4479   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4480 		       mdecl, NULL_TREE);
4481 
4482   /* Gather all the statements in a compound */
4483   for (current = TYPE_II_STMT_LIST (class_type);
4484        current; current = TREE_CHAIN (current))
4485     compound = add_stmt_to_compound (compound, NULL_TREE, current);
4486 
4487   /* We need to encapsulate COMPOUND by a try/catch statement to
4488      rethrow exceptions that might occur in the instance initializer.
4489      We do that only if all ctors of CLASS_TYPE are set to catch a
4490      checked exception. This doesn't apply to anonymous classes (since
4491      they don't have declared ctors.) */
4492   if (!ANONYMOUS_CLASS_P (class_type) &&
4493       ctors_unchecked_throws_clause_p (class_type))
4494     {
4495       compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4496 					     build1 (THROW_EXPR, NULL_TREE,
4497 						     build_wfl_node (wpv_id)));
4498       DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4499 						      exception_type_node);
4500     }
4501 
4502   start_artificial_method_body (mdecl);
4503   java_method_add_stmt (mdecl, compound);
4504   end_artificial_method_body (mdecl);
4505 
4506   return mdecl;
4507 }
4508 
4509 /* FIXME */
4510 static tree
build_instinit_invocation(class_type)4511 build_instinit_invocation (class_type)
4512      tree class_type;
4513 {
4514   tree to_return = NULL_TREE;
4515 
4516   if (TYPE_II_STMT_LIST (class_type))
4517     {
4518       tree parm = build_tree_list (NULL_TREE,
4519 				   build_wfl_node (this_identifier_node));
4520       to_return =
4521 	build_method_invocation (build_wfl_node (instinit_identifier_node),
4522 				 parm);
4523     }
4524   return to_return;
4525 }
4526 
4527 /* Shared accros method_declarator and method_header to remember the
4528    patch stage that was reached during the declaration of the method.
4529    A method DECL is built differently is there is no patch
4530    (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4531    pending on the currently defined method.  */
4532 
4533 static int patch_stage;
4534 
4535 /* Check the method declaration and add the method to its current
4536    class.  If the argument list is known to contain incomplete types,
4537    the method is partially added and the registration will be resume
4538    once the method arguments resolved. If TYPE is NULL, we're dealing
4539    with a constructor.  */
4540 
4541 static tree
method_header(flags,type,mdecl,throws)4542 method_header (flags, type, mdecl, throws)
4543      int flags;
4544      tree type, mdecl, throws;
4545 {
4546   tree type_wfl = NULL_TREE;
4547   tree meth_name = NULL_TREE;
4548   tree current, orig_arg, this_class = NULL;
4549   tree id, meth;
4550   int saved_lineno;
4551   int constructor_ok = 0, must_chain;
4552   int count;
4553 
4554   if (mdecl == error_mark_node)
4555     return error_mark_node;
4556   meth = TREE_VALUE (mdecl);
4557   id = TREE_PURPOSE (mdecl);
4558 
4559   check_modifiers_consistency (flags);
4560 
4561   if (GET_CPC ())
4562     this_class = TREE_TYPE (GET_CPC ());
4563 
4564   if (!this_class || this_class == error_mark_node)
4565     return NULL_TREE;
4566 
4567   /* There are some forbidden modifiers for an abstract method and its
4568      class must be abstract as well.  */
4569   if (type && (flags & ACC_ABSTRACT))
4570     {
4571       ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4572       ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4573       ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4574       ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4575       ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED, id, "Synchronized");
4576       ABSTRACT_CHECK (flags, ACC_STRICT, id, "Strictfp");
4577       if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4578 	  && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4579 	parse_error_context
4580 	  (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4581 	   IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4582 	   IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4583     }
4584 
4585   /* A native method can't be strictfp.  */
4586   if ((flags & ACC_NATIVE) && (flags & ACC_STRICT))
4587     parse_error_context (id, "native method `%s' can't be strictfp",
4588 			 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4589   /* No such thing as a transient or volatile method.  */
4590   if ((flags & ACC_TRANSIENT))
4591     parse_error_context (id, "method `%s' can't be transient",
4592 			 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4593   if ((flags & ACC_VOLATILE))
4594     parse_error_context (id, "method `%s' can't be volatile",
4595 			 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4596 
4597   /* Things to be checked when declaring a constructor */
4598   if (!type)
4599     {
4600       int ec = java_error_count;
4601       /* 8.6: Constructor declarations: we might be trying to define a
4602          method without specifying a return type. */
4603       if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4604 	parse_error_context
4605 	  (id, "Invalid method declaration, return type required");
4606       /* 8.6.3: Constructor modifiers */
4607       else
4608 	{
4609 	  JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4610 	  JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4611 	  JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4612 	  JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4613 	  JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4614 	  JCONSTRUCTOR_CHECK (flags, ACC_STRICT, id, "strictfp");
4615 	}
4616       /* If we found error here, we don't consider it's OK to tread
4617 	 the method definition as a constructor, for the rest of this
4618 	 function */
4619       if (ec == java_error_count)
4620 	constructor_ok = 1;
4621     }
4622 
4623   /* Method declared within the scope of an interface are implicitly
4624      abstract and public. Conflicts with other erroneously provided
4625      modifiers are checked right after. */
4626 
4627   if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4628     {
4629       /* If FLAGS isn't set because of a modifier, turn the
4630 	 corresponding modifier WFL to NULL so we issue a warning on
4631 	 the obsolete use of the modifier */
4632       if (!(flags & ACC_PUBLIC))
4633         MODIFIER_WFL (PUBLIC_TK) = NULL;
4634       if (!(flags & ACC_ABSTRACT))
4635         MODIFIER_WFL (ABSTRACT_TK) = NULL;
4636       flags |= ACC_PUBLIC;
4637       flags |= ACC_ABSTRACT;
4638     }
4639 
4640   /* Inner class can't declare static methods */
4641   if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4642     {
4643       parse_error_context
4644 	(id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4645 	 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4646 	 lang_printable_name (this_class, 0));
4647     }
4648 
4649   /* Modifiers context reset moved up, so abstract method declaration
4650      modifiers can be later checked.  */
4651 
4652   /* Set constructor returned type to void and method name to <init>,
4653      unless we found an error identifier the constructor (in which
4654      case we retain the original name) */
4655   if (!type)
4656     {
4657       type = void_type_node;
4658       if (constructor_ok)
4659 	meth_name = init_identifier_node;
4660     }
4661   else
4662     meth_name = EXPR_WFL_NODE (id);
4663 
4664   /* Do the returned type resolution and registration if necessary */
4665   SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4666 
4667   if (meth_name)
4668     type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4669   EXPR_WFL_NODE (id) = meth_name;
4670   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4671 
4672   if (must_chain)
4673     {
4674       patch_stage = JDEP_METHOD_RETURN;
4675       register_incomplete_type (patch_stage, type_wfl, id, type);
4676       TREE_TYPE (meth) = GET_REAL_TYPE (type);
4677     }
4678   else
4679     TREE_TYPE (meth) = type;
4680 
4681   saved_lineno = lineno;
4682   /* When defining an abstract or interface method, the curly
4683      bracket at level 1 doesn't exist because there is no function
4684      body */
4685   lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4686 	    EXPR_WFL_LINENO (id));
4687 
4688   /* Remember the original argument list */
4689   orig_arg = TYPE_ARG_TYPES (meth);
4690 
4691   if (patch_stage)		/* includes ret type and/or all args */
4692     {
4693       jdep *jdep;
4694       meth = add_method_1 (this_class, flags, meth_name, meth);
4695       /* Patch for the return type */
4696       if (patch_stage == JDEP_METHOD_RETURN)
4697 	{
4698 	  jdep = CLASSD_LAST (ctxp->classd_list);
4699 	  JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4700 	}
4701       /* This is the stop JDEP. METH allows the function's signature
4702 	 to be computed. */
4703       register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4704     }
4705   else
4706     meth = add_method (this_class, flags, meth_name,
4707 		       build_java_signature (meth));
4708 
4709   /* Remember final parameters */
4710   MARK_FINAL_PARMS (meth, orig_arg);
4711 
4712   /* Fix the method argument list so we have the argument name
4713      information */
4714   fix_method_argument_names (orig_arg, meth);
4715 
4716   /* Register the parameter number and re-install the current line
4717      number */
4718   DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4719   lineno = saved_lineno;
4720 
4721   /* Register exception specified by the `throws' keyword for
4722      resolution and set the method decl appropriate field to the list.
4723      Note: the grammar ensures that what we get here are class
4724      types. */
4725   if (throws)
4726     {
4727       throws = nreverse (throws);
4728       for (current = throws; current; current = TREE_CHAIN (current))
4729 	{
4730 	  register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4731 				    NULL_TREE, NULL_TREE);
4732 	  JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4733 	    &TREE_VALUE (current);
4734 	}
4735       DECL_FUNCTION_THROWS (meth) = throws;
4736     }
4737 
4738   if (TREE_TYPE (GET_CPC ()) != object_type_node)
4739     DECL_FUNCTION_WFL (meth) = id;
4740 
4741   /* Set the flag if we correctly processed a constructor */
4742   if (constructor_ok)
4743     {
4744       DECL_CONSTRUCTOR_P (meth) = 1;
4745       /* Compute and store the number of artificial parameters declared
4746 	 for this constructor */
4747       for (count = 0, current = TYPE_FIELDS (this_class); current;
4748 	   current = TREE_CHAIN (current))
4749 	if (FIELD_LOCAL_ALIAS (current))
4750 	  count++;
4751       DECL_FUNCTION_NAP (meth) = count;
4752     }
4753 
4754   /* Eventually set the @deprecated tag flag */
4755   CHECK_DEPRECATED (meth);
4756 
4757   /* If doing xref, store column and line number information instead
4758      of the line number only. */
4759   if (flag_emit_xref)
4760     DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4761 
4762   return meth;
4763 }
4764 
4765 static void
fix_method_argument_names(orig_arg,meth)4766 fix_method_argument_names (orig_arg, meth)
4767     tree orig_arg, meth;
4768 {
4769   tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4770   if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4771     {
4772       TREE_PURPOSE (arg) = this_identifier_node;
4773       arg = TREE_CHAIN (arg);
4774     }
4775   while (orig_arg != end_params_node)
4776     {
4777       TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4778       orig_arg = TREE_CHAIN (orig_arg);
4779       arg = TREE_CHAIN (arg);
4780     }
4781 }
4782 
4783 /* Complete the method declaration with METHOD_BODY.  */
4784 
4785 static void
finish_method_declaration(method_body)4786 finish_method_declaration (method_body)
4787      tree method_body;
4788 {
4789   int flags;
4790 
4791   if (!current_function_decl)
4792     return;
4793 
4794   flags = get_access_flags_from_decl (current_function_decl);
4795 
4796   /* 8.4.5 Method Body */
4797   if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4798     {
4799       tree name = DECL_NAME (current_function_decl);
4800       parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4801 			   "%s method `%s' can't have a body defined",
4802 			   (METHOD_NATIVE (current_function_decl) ?
4803 			    "Native" : "Abstract"),
4804 			   IDENTIFIER_POINTER (name));
4805       method_body = NULL_TREE;
4806     }
4807   else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4808     {
4809       tree name = DECL_NAME (current_function_decl);
4810       parse_error_context
4811 	(DECL_FUNCTION_WFL (current_function_decl),
4812 	 "Non native and non abstract method `%s' must have a body defined",
4813 	 IDENTIFIER_POINTER (name));
4814       method_body = NULL_TREE;
4815     }
4816 
4817   if (flag_emit_class_files && method_body
4818       && TREE_CODE (method_body) == NOP_EXPR
4819       && TREE_TYPE (current_function_decl)
4820       && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4821     method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4822 
4823   BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4824   maybe_absorb_scoping_blocks ();
4825   /* Exit function's body */
4826   exit_block ();
4827   /* Merge last line of the function with first line, directly in the
4828      function decl. It will be used to emit correct debug info. */
4829   if (!flag_emit_xref)
4830     DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4831 
4832   /* Since function's argument's list are shared, reset the
4833      ARG_FINAL_P parameter that might have been set on some of this
4834      function parameters. */
4835   UNMARK_FINAL_PARMS (current_function_decl);
4836 
4837   /* So we don't have an irrelevant function declaration context for
4838      the next static block we'll see. */
4839   current_function_decl = NULL_TREE;
4840 }
4841 
4842 /* Build a an error message for constructor circularity errors.  */
4843 
4844 static char *
constructor_circularity_msg(from,to)4845 constructor_circularity_msg (from, to)
4846      tree from, to;
4847 {
4848   static char string [4096];
4849   char *t = xstrdup (lang_printable_name (from, 0));
4850   sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4851   free (t);
4852   return string;
4853 }
4854 
4855 /* Verify a circular call to METH. Return 1 if an error is found, 0
4856    otherwise.  */
4857 
4858 static GTY(()) tree vcc_list;
4859 static int
verify_constructor_circularity(meth,current)4860 verify_constructor_circularity (meth, current)
4861      tree meth, current;
4862 {
4863   tree c;
4864 
4865   for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4866     {
4867       if (TREE_VALUE (c) == meth)
4868 	{
4869 	  char *t;
4870 	  if (vcc_list)
4871 	    {
4872 	      tree liste;
4873 	      vcc_list = nreverse (vcc_list);
4874 	      for (liste = vcc_list; liste; liste = TREE_CHAIN (liste))
4875 		{
4876 		  parse_error_context
4877 		    (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4878 		     constructor_circularity_msg
4879 		      (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4880 		  java_error_count--;
4881 		}
4882 	    }
4883 	  t = xstrdup (lang_printable_name (meth, 0));
4884 	  parse_error_context (TREE_PURPOSE (c),
4885 			       "%s: recursive invocation of constructor `%s'",
4886 			       constructor_circularity_msg (current, meth), t);
4887 	  free (t);
4888 	  vcc_list = NULL_TREE;
4889 	  return 1;
4890 	}
4891     }
4892   for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4893     {
4894       vcc_list = tree_cons (c, current, vcc_list);
4895       if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4896 	return 1;
4897       vcc_list = TREE_CHAIN (vcc_list);
4898     }
4899   return 0;
4900 }
4901 
4902 /* Check modifiers that can be declared but exclusively */
4903 
4904 static void
check_modifiers_consistency(flags)4905 check_modifiers_consistency (flags)
4906      int flags;
4907 {
4908   int acc_count = 0;
4909   tree cl = NULL_TREE;
4910 
4911   THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4912   THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4913   THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4914   if (acc_count > 1)
4915     parse_error_context
4916       (cl, "Inconsistent member declaration.  At most one of `public', `private', or `protected' may be specified");
4917 
4918   acc_count = 0;
4919   cl = NULL_TREE;
4920   THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4921   THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4922   if (acc_count > 1)
4923     parse_error_context (cl,
4924 			 "Inconsistent member declaration.  At most one of `final' or `volatile' may be specified");
4925 }
4926 
4927 /* Check the methode header METH for abstract specifics features */
4928 
4929 static void
check_abstract_method_header(meth)4930 check_abstract_method_header (meth)
4931      tree meth;
4932 {
4933   int flags = get_access_flags_from_decl (meth);
4934 
4935   OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4936 			      ACC_ABSTRACT, "abstract method",
4937 			      IDENTIFIER_POINTER (DECL_NAME (meth)));
4938   OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4939 			      ACC_PUBLIC, "abstract method",
4940 			      IDENTIFIER_POINTER (DECL_NAME (meth)));
4941 
4942   check_modifiers ("Illegal modifier `%s' for interface method",
4943 		  flags, INTERFACE_METHOD_MODIFIERS);
4944 }
4945 
4946 /* Create a FUNCTION_TYPE node and start augmenting it with the
4947    declared function arguments. Arguments type that can't be resolved
4948    are left as they are, but the returned node is marked as containing
4949    incomplete types.  */
4950 
4951 static tree
method_declarator(id,list)4952 method_declarator (id, list)
4953      tree id, list;
4954 {
4955   tree arg_types = NULL_TREE, current, node;
4956   tree meth = make_node (FUNCTION_TYPE);
4957   jdep *jdep;
4958 
4959   patch_stage = JDEP_NO_PATCH;
4960 
4961   if (GET_CPC () == error_mark_node)
4962     return error_mark_node;
4963 
4964   /* If we're dealing with an inner class constructor, we hide the
4965      this$<n> decl in the name field of its parameter declaration.  We
4966      also might have to hide the outer context local alias
4967      initializers. Not done when the class is a toplevel class. */
4968   if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4969       && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4970     {
4971       tree aliases_list, type, thisn;
4972       /* First the aliases, linked to the regular parameters */
4973       aliases_list =
4974 	build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4975 						TREE_TYPE (GET_CPC ()),
4976 						NULL_TREE, NULL);
4977       list = chainon (nreverse (aliases_list), list);
4978 
4979       /* Then this$<n> */
4980       type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4981       thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4982       list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4983 			list);
4984     }
4985 
4986   for (current = list; current; current = TREE_CHAIN (current))
4987     {
4988       int must_chain = 0;
4989       tree wfl_name = TREE_PURPOSE (current);
4990       tree type = TREE_VALUE (current);
4991       tree name = EXPR_WFL_NODE (wfl_name);
4992       tree already, arg_node;
4993       tree type_wfl = NULL_TREE;
4994       tree real_type;
4995 
4996       /* Obtain a suitable type for resolution, if necessary */
4997       SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4998 
4999       /* Process NAME, as it may specify extra dimension(s) for it */
5000       type = build_array_from_name (type, type_wfl, name, &name);
5001       EXPR_WFL_NODE (wfl_name) = name;
5002 
5003       real_type = GET_REAL_TYPE (type);
5004       if (TREE_CODE (real_type) == RECORD_TYPE)
5005 	{
5006 	  real_type = promote_type (real_type);
5007 	  if (TREE_CODE (type) == TREE_LIST)
5008 	    TREE_PURPOSE (type) = real_type;
5009 	}
5010 
5011       /* Check redefinition */
5012       for (already = arg_types; already; already = TREE_CHAIN (already))
5013 	if (TREE_PURPOSE (already) == name)
5014 	  {
5015 	    parse_error_context
5016 	      (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
5017 	       IDENTIFIER_POINTER (name),
5018 	       IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
5019 	    break;
5020 	  }
5021 
5022       /* If we've an incomplete argument type, we know there is a location
5023 	 to patch when the type get resolved, later.  */
5024       jdep = NULL;
5025       if (must_chain)
5026 	{
5027 	  patch_stage = JDEP_METHOD;
5028 	  type = register_incomplete_type (patch_stage,
5029 					   type_wfl, wfl_name, type);
5030 	  jdep = CLASSD_LAST (ctxp->classd_list);
5031 	  JDEP_MISC (jdep) = id;
5032 	}
5033 
5034       /* The argument node: a name and a (possibly) incomplete type.  */
5035       arg_node = build_tree_list (name, real_type);
5036       /* Remeber arguments declared final. */
5037       ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5038 
5039       if (jdep)
5040 	JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5041       TREE_CHAIN (arg_node) = arg_types;
5042       arg_types = arg_node;
5043     }
5044   TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5045   node = build_tree_list (id, meth);
5046   return node;
5047 }
5048 
5049 static int
unresolved_type_p(wfl,returned)5050 unresolved_type_p (wfl, returned)
5051      tree wfl;
5052      tree *returned;
5053 
5054 {
5055   if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5056     {
5057       if (returned)
5058 	{
5059 	  tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5060 	  if (decl && current_class && (decl == TYPE_NAME (current_class)))
5061 	    *returned = TREE_TYPE (decl);
5062 	  else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5063 	    *returned = TREE_TYPE (GET_CPC ());
5064 	  else
5065 	    *returned = NULL_TREE;
5066 	}
5067       return 1;
5068     }
5069   if (returned)
5070     *returned = wfl;
5071   return 0;
5072 }
5073 
5074 /* From NAME, build a qualified identifier node using the
5075    qualification from the current package definition. */
5076 
5077 static tree
parser_qualified_classname(name)5078 parser_qualified_classname (name)
5079      tree name;
5080 {
5081   tree nested_class_name;
5082 
5083   if ((nested_class_name = maybe_make_nested_class_name (name)))
5084     return nested_class_name;
5085 
5086   if (ctxp->package)
5087     return merge_qualified_name (ctxp->package, name);
5088   else
5089     return name;
5090 }
5091 
5092 /* Called once the type a interface extends is resolved. Returns 0 if
5093    everything is OK.  */
5094 
5095 static int
parser_check_super_interface(super_decl,this_decl,this_wfl)5096 parser_check_super_interface (super_decl, this_decl, this_wfl)
5097      tree super_decl, this_decl, this_wfl;
5098 {
5099   tree super_type = TREE_TYPE (super_decl);
5100 
5101   /* Has to be an interface */
5102   if (!CLASS_INTERFACE (super_decl))
5103     {
5104       parse_error_context
5105 	(this_wfl, "%s `%s' can't implement/extend %s `%s'",
5106 	 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5107 	  "Interface" : "Class"),
5108 	 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5109 	 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5110 	 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5111       return 1;
5112     }
5113 
5114   /* Check top-level interface access. Inner classes are subject to member
5115      access rules (6.6.1). */
5116   if (! INNER_CLASS_P (super_type)
5117       && check_pkg_class_access (DECL_NAME (super_decl),
5118 				 lookup_cl (this_decl), true))
5119     return 1;
5120 
5121   SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5122 			  IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5123 			  IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5124   return 0;
5125 }
5126 
5127 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5128    0 if everthing is OK.  */
5129 
5130 static int
parser_check_super(super_decl,this_decl,wfl)5131 parser_check_super (super_decl, this_decl, wfl)
5132      tree super_decl, this_decl, wfl;
5133 {
5134   tree super_type = TREE_TYPE (super_decl);
5135 
5136   /* SUPER should be a CLASS (neither an array nor an interface) */
5137   if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5138     {
5139       parse_error_context
5140 	(wfl, "Class `%s' can't subclass %s `%s'",
5141 	 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5142 	 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5143 	 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5144       return 1;
5145     }
5146 
5147   if (CLASS_FINAL (TYPE_NAME (super_type)))
5148     {
5149       parse_error_context (wfl, "Can't subclass final classes: %s",
5150 			   IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5151       return 1;
5152     }
5153 
5154   /* Check top-level class scope. Inner classes are subject to member access
5155      rules (6.6.1). */
5156   if (! INNER_CLASS_P (super_type)
5157       && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true)))
5158     return 1;
5159 
5160   SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5161 			  IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5162 			  IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5163   return 0;
5164 }
5165 
5166 /* Create a new dependency list and link it (in a LIFO manner) to the
5167    CTXP list of type dependency list.  */
5168 
5169 static void
create_jdep_list(ctxp)5170 create_jdep_list (ctxp)
5171      struct parser_ctxt *ctxp;
5172 {
5173   jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
5174   new->first = new->last = NULL;
5175   new->next = ctxp->classd_list;
5176   ctxp->classd_list = new;
5177 }
5178 
5179 static jdeplist *
reverse_jdep_list(ctxp)5180 reverse_jdep_list (ctxp)
5181      struct parser_ctxt *ctxp;
5182 {
5183   register jdeplist *prev = NULL, *current, *next;
5184   for (current = ctxp->classd_list; current; current = next)
5185     {
5186       next = current->next;
5187       current->next = prev;
5188       prev = current;
5189     }
5190   return prev;
5191 }
5192 
5193 /* Create a fake pointer based on the ID stored in
5194    TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5195    registered again. */
5196 
5197 static tree
obtain_incomplete_type(type_name)5198 obtain_incomplete_type (type_name)
5199      tree type_name;
5200 {
5201   tree ptr = NULL_TREE, name;
5202 
5203   if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5204     name = EXPR_WFL_NODE (type_name);
5205   else if (INCOMPLETE_TYPE_P (type_name))
5206     name = TYPE_NAME (type_name);
5207   else
5208     abort ();
5209 
5210   BUILD_PTR_FROM_NAME (ptr, name);
5211   layout_type (ptr);
5212 
5213   return ptr;
5214 }
5215 
5216 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5217    non NULL instead of computing a new fake type based on WFL. The new
5218    dependency is inserted in the current type dependency list, in FIFO
5219    manner.  */
5220 
5221 static tree
register_incomplete_type(kind,wfl,decl,ptr)5222 register_incomplete_type (kind, wfl, decl, ptr)
5223      int kind;
5224      tree wfl, decl, ptr;
5225 {
5226   jdep *new = (jdep *)xmalloc (sizeof (jdep));
5227 
5228   if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5229     ptr = obtain_incomplete_type (wfl);
5230 
5231   JDEP_KIND (new) = kind;
5232   JDEP_DECL (new) = decl;
5233   JDEP_TO_RESOLVE (new) = ptr;
5234   JDEP_WFL (new) = wfl;
5235   JDEP_CHAIN (new) = NULL;
5236   JDEP_MISC (new) = NULL_TREE;
5237   /* For some dependencies, set the enclosing class of the current
5238      class to be the enclosing context */
5239   if ((kind == JDEP_INTERFACE  || kind == JDEP_ANONYMOUS)
5240       && GET_ENCLOSING_CPC ())
5241     JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5242   else if (kind == JDEP_SUPER)
5243     JDEP_ENCLOSING (new) = (GET_ENCLOSING_CPC () ?
5244 			    TREE_VALUE (GET_ENCLOSING_CPC ()) : NULL_TREE);
5245   else
5246     JDEP_ENCLOSING (new) = GET_CPC ();
5247   JDEP_GET_PATCH (new) = (tree *)NULL;
5248 
5249   JDEP_INSERT (ctxp->classd_list, new);
5250 
5251   return ptr;
5252 }
5253 
5254 /* This checks for circular references with innerclasses. We start
5255    from SOURCE and should never reach TARGET. Extended/implemented
5256    types in SOURCE have their enclosing context checked not to reach
5257    TARGET. When the last enclosing context of SOURCE is reached, its
5258    extended/implemented types are also checked not to reach TARGET.
5259    In case of error, WFL of the offending type is returned; NULL_TREE
5260    otherwise.  */
5261 
5262 static tree
check_inner_circular_reference(source,target)5263 check_inner_circular_reference (source, target)
5264      tree source;
5265      tree target;
5266 {
5267   tree basetype_vec = TYPE_BINFO_BASETYPES (source);
5268   tree ctx, cl;
5269   int i;
5270 
5271   if (!basetype_vec)
5272     return NULL_TREE;
5273 
5274   for (i = 0; i < TREE_VEC_LENGTH (basetype_vec); i++)
5275     {
5276       tree su;
5277 
5278       /* We can end up with a NULL_TREE or an incomplete type here if
5279 	 we encountered previous type resolution errors. It's safe to
5280 	 simply ignore these cases.  */
5281       if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
5282 	continue;
5283       su = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
5284       if (INCOMPLETE_TYPE_P (su))
5285 	continue;
5286 
5287       if (inherits_from_p (su, target))
5288 	return lookup_cl (TYPE_NAME (su));
5289 
5290       for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5291 	{
5292 	  /* An enclosing context shouldn't be TARGET */
5293 	  if (ctx == TYPE_NAME (target))
5294 	    return lookup_cl (TYPE_NAME (su));
5295 
5296 	  /* When we reach the enclosing last context, start a check
5297 	     on it, with the same target */
5298 	  if (! DECL_CONTEXT (ctx) &&
5299 	      (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5300 	    return cl;
5301 	}
5302     }
5303   return NULL_TREE;
5304 }
5305 
5306 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5307    offending type if a circularity is detected. NULL_TREE is returned
5308    otherwise. TYPE can be an interface or a class.   */
5309 
5310 static tree
check_circular_reference(type)5311 check_circular_reference (type)
5312      tree type;
5313 {
5314   tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5315   int i;
5316 
5317   if (!basetype_vec)
5318     return NULL_TREE;
5319 
5320   if (! CLASS_INTERFACE (TYPE_NAME (type)))
5321     {
5322       if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5323 	return lookup_cl (TYPE_NAME (type));
5324       return NULL_TREE;
5325     }
5326 
5327   for (i = 0; i < TREE_VEC_LENGTH (basetype_vec); i++)
5328     {
5329       tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5330       if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5331 	  && interface_of_p (type, BINFO_TYPE (vec_elt)))
5332 	return lookup_cl (TYPE_NAME (BINFO_TYPE (vec_elt)));
5333     }
5334   return NULL_TREE;
5335 }
5336 
5337 void
java_check_circular_reference()5338 java_check_circular_reference ()
5339 {
5340   tree current;
5341   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5342     {
5343       tree type = TREE_TYPE (current);
5344       tree cl;
5345 
5346       cl = check_circular_reference (type);
5347       if (! cl)
5348 	cl = check_inner_circular_reference (type, type);
5349       if (cl)
5350 	parse_error_context (cl, "Cyclic class inheritance%s",
5351 			     (cyclic_inheritance_report ?
5352 			      cyclic_inheritance_report : ""));
5353     }
5354 }
5355 
5356 /* Augment the parameter list PARM with parameters crafted to
5357    initialize outer context locals aliases. Through ARTIFICIAL, a
5358    count is kept of the number of crafted parameters. MODE governs
5359    what eventually gets created: something suitable for a function
5360    creation or a function invocation, either the constructor or
5361    finit$.  */
5362 
5363 static tree
build_alias_initializer_parameter_list(mode,class_type,parm,artificial)5364 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5365     int mode;
5366     tree class_type, parm;
5367     int *artificial;
5368 {
5369   tree field;
5370   tree additional_parms = NULL_TREE;
5371 
5372   for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5373     if (FIELD_LOCAL_ALIAS (field))
5374       {
5375 	const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5376 	tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5377 	tree mangled_id;
5378 
5379 	switch (mode)
5380 	  {
5381 	  case AIPL_FUNCTION_DECLARATION:
5382 	    MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5383 							 &buffer [4]);
5384 	    purpose = build_wfl_node (mangled_id);
5385 	    if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5386 	      value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5387 	    else
5388 	      value = TREE_TYPE (field);
5389 	    break;
5390 
5391 	  case AIPL_FUNCTION_CREATION:
5392 	    MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5393 							 &buffer [4]);
5394 	    value = TREE_TYPE (field);
5395 	    break;
5396 
5397 	  case AIPL_FUNCTION_FINIT_INVOCATION:
5398 	    MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5399 							 &buffer [4]);
5400 	    /* Now, this is wrong. purpose should always be the NAME
5401 	       of something and value its matching value (decl, type,
5402 	       etc...) FIXME -- but there is a lot to fix. */
5403 
5404 	    /* When invoked for this kind of operation, we already
5405 	       know whether a field is used or not. */
5406 	    purpose = TREE_TYPE (field);
5407 	    value = build_wfl_node (mangled_id);
5408 	    break;
5409 
5410 	  case AIPL_FUNCTION_CTOR_INVOCATION:
5411 	    /* There are two case: the constructor invokation happends
5412 	       outside the local inner, in which case, locales from the outer
5413 	       context are directly used.
5414 
5415 	       Otherwise, we fold to using the alias directly. */
5416 	    if (class_type == current_class)
5417 	      value = field;
5418 	    else
5419 	      {
5420 		name = get_identifier (&buffer[4]);
5421 		value = IDENTIFIER_LOCAL_VALUE (name);
5422 	      }
5423 	    break;
5424 	  }
5425 	additional_parms = tree_cons (purpose, value, additional_parms);
5426 	if (artificial)
5427 	  *artificial +=1;
5428       }
5429   if (additional_parms)
5430     {
5431       if (ANONYMOUS_CLASS_P (class_type)
5432           && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5433         additional_parms = nreverse (additional_parms);
5434       parm = chainon (additional_parms, parm);
5435     }
5436 
5437    return parm;
5438 }
5439 
5440 /* Craft a constructor for CLASS_DECL -- what we should do when none
5441    where found. ARGS is non NULL when a special signature must be
5442    enforced. This is the case for anonymous classes.  */
5443 
5444 static tree
craft_constructor(class_decl,args)5445 craft_constructor (class_decl, args)
5446      tree class_decl, args;
5447 {
5448   tree class_type = TREE_TYPE (class_decl);
5449   tree parm = NULL_TREE;
5450   int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5451 	       ACC_PUBLIC : 0);
5452   int i = 0, artificial = 0;
5453   tree decl, ctor_name;
5454   char buffer [80];
5455 
5456   /* The constructor name is <init> unless we're dealing with an
5457      anonymous class, in which case the name will be fixed after having
5458      be expanded. */
5459   if (ANONYMOUS_CLASS_P (class_type))
5460     ctor_name = DECL_NAME (class_decl);
5461   else
5462     ctor_name = init_identifier_node;
5463 
5464   /* If we're dealing with an inner class constructor, we hide the
5465      this$<n> decl in the name field of its parameter declaration. */
5466   if (PURE_INNER_CLASS_TYPE_P (class_type))
5467     {
5468       tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5469       parm = tree_cons (build_current_thisn (class_type),
5470 			build_pointer_type (type), parm);
5471 
5472       /* Some more arguments to be hidden here. The values of the local
5473 	 variables of the outer context that the inner class needs to see. */
5474       parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5475 						     class_type, parm,
5476 						     &artificial);
5477     }
5478 
5479   /* Then if there are any args to be enforced, enforce them now */
5480   for (; args && args != end_params_node; args = TREE_CHAIN (args))
5481     {
5482       sprintf (buffer, "parm%d", i++);
5483       parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5484     }
5485 
5486   CRAFTED_PARAM_LIST_FIXUP (parm);
5487   decl = create_artificial_method (class_type, flags, void_type_node,
5488 				   ctor_name, parm);
5489   fix_method_argument_names (parm, decl);
5490   /* Now, mark the artificial parameters. */
5491   DECL_FUNCTION_NAP (decl) = artificial;
5492   DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5493   return decl;
5494 }
5495 
5496 
5497 /* Fix the constructors. This will be called right after circular
5498    references have been checked. It is necessary to fix constructors
5499    early even if no code generation will take place for that class:
5500    some generated constructor might be required by the class whose
5501    compilation triggered this one to be simply loaded.  */
5502 
5503 void
java_fix_constructors()5504 java_fix_constructors ()
5505 {
5506   tree current;
5507 
5508   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5509     {
5510       tree class_type = TREE_TYPE (current);
5511       int saw_ctor = 0;
5512       tree decl;
5513 
5514       if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5515 	continue;
5516 
5517       current_class = class_type;
5518       for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5519 	{
5520 	  if (DECL_CONSTRUCTOR_P (decl))
5521 	    {
5522 	      fix_constructors (decl);
5523 	      saw_ctor = 1;
5524 	    }
5525 	}
5526 
5527       /* Anonymous class constructor can't be generated that early. */
5528       if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5529 	craft_constructor (current, NULL_TREE);
5530     }
5531 }
5532 
5533 /* safe_layout_class just makes sure that we can load a class without
5534    disrupting the current_class, input_file, lineno, etc, information
5535    about the class processed currently.  */
5536 
5537 void
safe_layout_class(class)5538 safe_layout_class (class)
5539      tree class;
5540 {
5541   tree save_current_class = current_class;
5542   const char *save_input_filename = input_filename;
5543   int save_lineno = lineno;
5544 
5545   layout_class (class);
5546 
5547   current_class = save_current_class;
5548   input_filename = save_input_filename;
5549   lineno = save_lineno;
5550 }
5551 
5552 static tree
jdep_resolve_class(dep)5553 jdep_resolve_class (dep)
5554      jdep *dep;
5555 {
5556   tree decl;
5557 
5558   if (JDEP_RESOLVED_P (dep))
5559     decl = JDEP_RESOLVED_DECL (dep);
5560   else
5561     {
5562       decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5563 			    JDEP_DECL (dep), JDEP_WFL (dep));
5564       JDEP_RESOLVED (dep, decl);
5565     }
5566 
5567   if (!decl)
5568     complete_class_report_errors (dep);
5569   else if (PURE_INNER_CLASS_DECL_P (decl))
5570     {
5571       tree inner = TREE_TYPE (decl);
5572       if (! CLASS_LOADED_P (inner))
5573 	{
5574 	  safe_layout_class (inner);
5575 	  if (TYPE_SIZE (inner) == error_mark_node)
5576 	    TYPE_SIZE (inner) = NULL_TREE;
5577 	}
5578       check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5579     }
5580   return decl;
5581 }
5582 
5583 /* Complete unsatisfied class declaration and their dependencies */
5584 
5585 void
java_complete_class()5586 java_complete_class ()
5587 {
5588   tree cclass;
5589   jdeplist *cclassd;
5590   int error_found;
5591   tree type;
5592 
5593   /* Process imports */
5594   process_imports ();
5595 
5596   /* Reverse things so we have the right order */
5597   ctxp->class_list = nreverse (ctxp->class_list);
5598   ctxp->classd_list = reverse_jdep_list (ctxp);
5599 
5600   for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5601        cclass && cclassd;
5602        cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5603     {
5604       jdep *dep;
5605 
5606       /* We keep the compilation unit imports in the class so that
5607 	 they can be used later to resolve type dependencies that
5608 	 aren't necessary to solve now. */
5609       TYPE_IMPORT_LIST (TREE_TYPE (cclass)) = ctxp->import_list;
5610       TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (cclass)) = ctxp->import_demand_list;
5611 
5612       for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5613 	{
5614 	  tree decl;
5615 	  if (!(decl = jdep_resolve_class (dep)))
5616 	    continue;
5617 
5618 	  /* Now it's time to patch */
5619 	  switch (JDEP_KIND (dep))
5620 	    {
5621 	    case JDEP_SUPER:
5622 	      /* Simply patch super */
5623 	      if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5624 		continue;
5625 	      BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5626 	        (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5627 	      break;
5628 
5629 	    case JDEP_FIELD:
5630 	      {
5631 		/* We do part of the job done in add_field */
5632 		tree field_decl = JDEP_DECL (dep);
5633 		tree field_type = TREE_TYPE (decl);
5634 		if (TREE_CODE (field_type) == RECORD_TYPE)
5635 		  field_type = promote_type (field_type);
5636 		TREE_TYPE (field_decl) = field_type;
5637 		DECL_ALIGN (field_decl) = 0;
5638 		DECL_USER_ALIGN (field_decl) = 0;
5639 		layout_decl (field_decl, 0);
5640 		SOURCE_FRONTEND_DEBUG
5641 		  (("Completed field/var decl `%s' with `%s'",
5642 		    IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5643 		    IDENTIFIER_POINTER (DECL_NAME (decl))));
5644 		break;
5645 	      }
5646 	    case JDEP_METHOD:	/* We start patching a method */
5647 	    case JDEP_METHOD_RETURN:
5648 	      error_found = 0;
5649 	      while (1)
5650 		{
5651 		  if (decl)
5652 		    {
5653 		      type = TREE_TYPE(decl);
5654 		      if (TREE_CODE (type) == RECORD_TYPE)
5655 			type = promote_type (type);
5656 		      JDEP_APPLY_PATCH (dep, type);
5657 		      SOURCE_FRONTEND_DEBUG
5658 			(((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5659 			   "Completing fct `%s' with ret type `%s'":
5660 			   "Completing arg `%s' with type `%s'"),
5661 			  IDENTIFIER_POINTER (EXPR_WFL_NODE
5662 					      (JDEP_DECL_WFL (dep))),
5663 			  IDENTIFIER_POINTER (DECL_NAME (decl))));
5664 		    }
5665 		  else
5666 		    error_found = 1;
5667 		  dep = JDEP_CHAIN (dep);
5668 		  if (JDEP_KIND (dep) == JDEP_METHOD_END)
5669 		    break;
5670 		  else
5671 		    decl = jdep_resolve_class (dep);
5672 		}
5673 	      if (!error_found)
5674 		{
5675 		  tree mdecl = JDEP_DECL (dep), signature;
5676 		  /* Recompute and reset the signature, check first that
5677 		     all types are now defined. If they're not,
5678 		     don't build the signature. */
5679 		  if (check_method_types_complete (mdecl))
5680 		    {
5681 		      signature = build_java_signature (TREE_TYPE (mdecl));
5682 		      set_java_signature (TREE_TYPE (mdecl), signature);
5683 		    }
5684 		}
5685 	      else
5686 		continue;
5687 	      break;
5688 
5689 	    case JDEP_INTERFACE:
5690 	      if (parser_check_super_interface (decl, JDEP_DECL (dep),
5691 						JDEP_WFL (dep)))
5692 		continue;
5693 	      parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5694 	      break;
5695 
5696 	    case JDEP_PARM:
5697 	    case JDEP_VARIABLE:
5698 	      type = TREE_TYPE(decl);
5699 	      if (TREE_CODE (type) == RECORD_TYPE)
5700 		type = promote_type (type);
5701 	      JDEP_APPLY_PATCH (dep, type);
5702 	      break;
5703 
5704 	    case JDEP_TYPE:
5705 	      JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5706 	      SOURCE_FRONTEND_DEBUG
5707 		(("Completing a random type dependency on a '%s' node",
5708 		  tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5709 	      break;
5710 
5711 	    case JDEP_EXCEPTION:
5712 	      JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5713 	      SOURCE_FRONTEND_DEBUG
5714 		(("Completing `%s' `throws' argument node",
5715 		  IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5716 	      break;
5717 
5718 	    case JDEP_ANONYMOUS:
5719 	      patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5720 	      break;
5721 
5722 	    default:
5723 	      abort ();
5724 	    }
5725 	}
5726     }
5727   return;
5728 }
5729 
5730 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5731    array.  */
5732 
5733 static tree
resolve_class(enclosing,class_type,decl,cl)5734 resolve_class (enclosing, class_type, decl, cl)
5735      tree enclosing, class_type, decl, cl;
5736 {
5737   tree tname = TYPE_NAME (class_type);
5738   tree resolved_type = TREE_TYPE (class_type);
5739   int array_dims = 0;
5740   tree resolved_type_decl;
5741 
5742   if (resolved_type != NULL_TREE)
5743     {
5744       tree resolved_type_decl = TYPE_NAME (resolved_type);
5745       if (resolved_type_decl == NULL_TREE
5746 	  || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5747 	{
5748 	  resolved_type_decl = build_decl (TYPE_DECL,
5749 					   TYPE_NAME (class_type),
5750 					   resolved_type);
5751 	}
5752       return resolved_type_decl;
5753     }
5754 
5755   /* 1- Check to see if we have an array. If true, find what we really
5756      want to resolve  */
5757   if ((array_dims = build_type_name_from_array_name (tname,
5758 						     &TYPE_NAME (class_type))))
5759     WFL_STRIP_BRACKET (cl, cl);
5760 
5761   /* 2- Resolve the bare type */
5762   if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5763 					       decl, cl)))
5764     return NULL_TREE;
5765   resolved_type = TREE_TYPE (resolved_type_decl);
5766 
5767   /* 3- If we have an array, reconstruct the array down to its nesting */
5768   if (array_dims)
5769     {
5770       for (; array_dims; array_dims--)
5771 	resolved_type = build_java_array_type (resolved_type, -1);
5772       resolved_type_decl = TYPE_NAME (resolved_type);
5773     }
5774   TREE_TYPE (class_type) = resolved_type;
5775   return resolved_type_decl;
5776 }
5777 
5778 /* Effectively perform the resolution of class CLASS_TYPE.  DECL or CL
5779    are used to report error messages; CL must either be NULL_TREE or a
5780    WFL wrapping a class.  Do not try to replace TYPE_NAME (class_type)
5781    by a variable, since it is changed by find_in_imports{_on_demand}
5782    and (but it doesn't really matter) qualify_and_find.  */
5783 
5784 tree
do_resolve_class(enclosing,class_type,decl,cl)5785 do_resolve_class (enclosing, class_type, decl, cl)
5786      tree enclosing, class_type, decl, cl;
5787 {
5788   tree new_class_decl = NULL_TREE, super = NULL_TREE;
5789   tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5790   tree decl_result;
5791   htab_t circularity_hash;
5792 
5793   if (QUALIFIED_P (TYPE_NAME (class_type)))
5794     {
5795       /* If the type name is of the form `Q . Id', then Q is either a
5796 	 package name or a class name.  First we try to find Q as a
5797 	 class and then treat Id as a member type.  If we can't find Q
5798 	 as a class then we fall through.  */
5799       tree q, left, left_type, right;
5800       if (breakdown_qualified (&left, &right, TYPE_NAME (class_type)) == 0)
5801 	{
5802 	  BUILD_PTR_FROM_NAME (left_type, left);
5803 	  q = do_resolve_class (enclosing, left_type, decl, cl);
5804 	  if (q)
5805 	    {
5806 	      enclosing = q;
5807 	      saved_enclosing_type = TREE_TYPE (q);
5808 	      BUILD_PTR_FROM_NAME (class_type, right);
5809 	    }
5810 	}
5811     }
5812 
5813   if (enclosing)
5814     {
5815       /* This hash table is used to register the classes we're going
5816 	 through when searching the current class as an inner class, in
5817 	 order to detect circular references. Remember to free it before
5818 	 returning the section 0- of this function. */
5819       circularity_hash = htab_create (20, htab_hash_pointer, htab_eq_pointer,
5820 				      NULL);
5821 
5822       /* 0- Search in the current class as an inner class.
5823 	 Maybe some code here should be added to load the class or
5824 	 something, at least if the class isn't an inner class and ended
5825 	 being loaded from class file. FIXME. */
5826       while (enclosing)
5827 	{
5828 	  new_class_decl = resolve_inner_class (circularity_hash, cl, &enclosing,
5829 						&super, class_type);
5830 	  if (new_class_decl)
5831 	    break;
5832 
5833 	  /* If we haven't found anything because SUPER reached Object and
5834 	     ENCLOSING happens to be an innerclass, try the enclosing context. */
5835 	  if ((!super || super == object_type_node) &&
5836 	      enclosing && INNER_CLASS_DECL_P (enclosing))
5837 	    enclosing = DECL_CONTEXT (enclosing);
5838 	  else
5839 	    enclosing = NULL_TREE;
5840 	}
5841 
5842       htab_delete (circularity_hash);
5843 
5844       if (new_class_decl)
5845 	return new_class_decl;
5846     }
5847 
5848   /* 1- Check for the type in single imports. This will change
5849      TYPE_NAME() if something relevant is found */
5850   find_in_imports (saved_enclosing_type, class_type);
5851 
5852   /* 2- And check for the type in the current compilation unit */
5853   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5854     {
5855       if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5856 	  !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5857 	load_class (TYPE_NAME (class_type), 0);
5858       return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5859     }
5860 
5861   /* 3- Search according to the current package definition */
5862   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5863     {
5864       if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5865 					     TYPE_NAME (class_type))))
5866 	return new_class_decl;
5867     }
5868 
5869   /* 4- Check the import on demands. Don't allow bar.baz to be
5870      imported from foo.* */
5871   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5872     if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5873       return NULL_TREE;
5874 
5875   /* If found in find_in_imports_on_demand, the type has already been
5876      loaded. */
5877   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5878     return new_class_decl;
5879 
5880   /* 5- Try with a name qualified with the package name we've seen so far */
5881   if (!QUALIFIED_P (TYPE_NAME (class_type)))
5882     {
5883       tree package;
5884 
5885       /* If there is a current package (ctxp->package), it's the first
5886 	 element of package_list and we can skip it. */
5887       for (package = (ctxp->package ?
5888 		      TREE_CHAIN (package_list) : package_list);
5889 	   package; package = TREE_CHAIN (package))
5890 	if ((new_class_decl = qualify_and_find (class_type,
5891 					       TREE_PURPOSE (package),
5892 					       TYPE_NAME (class_type))))
5893 	  return new_class_decl;
5894     }
5895 
5896   /* 5- Check another compilation unit that bears the name of type */
5897   load_class (TYPE_NAME (class_type), 0);
5898 
5899   if (!cl)
5900     cl = lookup_cl (decl);
5901 
5902   /* If we don't have a value for CL, then we're being called recursively.
5903      We can't check package access just yet, but it will be taken care of
5904      by the caller. */
5905   if (cl)
5906     {
5907       if (check_pkg_class_access (TYPE_NAME (class_type), cl, true))
5908         return NULL_TREE;
5909     }
5910 
5911   /* 6- Last call for a resolution */
5912   decl_result = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5913 
5914   /* The final lookup might have registered a.b.c into a.b$c If we
5915      failed at the first lookup, progressively change the name if
5916      applicable and use the matching DECL instead. */
5917   if (!decl_result && QUALIFIED_P (TYPE_NAME (class_type)))
5918     {
5919       char *separator;
5920       tree name = TYPE_NAME (class_type);
5921       char *namebuffer = alloca (IDENTIFIER_LENGTH (name) + 1);
5922 
5923       strcpy (namebuffer, IDENTIFIER_POINTER (name));
5924 
5925       do {
5926 
5927        /* Reach the last '.', and if applicable, replace it by a `$' and
5928           see if this exists as a type. */
5929        if ((separator = strrchr (namebuffer, '.')))
5930          {
5931            *separator = '$';
5932            name = get_identifier (namebuffer);
5933            decl_result = IDENTIFIER_CLASS_VALUE (name);
5934          }
5935       } while (!decl_result && separator);
5936     }
5937   return decl_result;
5938 }
5939 
5940 static tree
qualify_and_find(class_type,package,name)5941 qualify_and_find (class_type, package, name)
5942      tree class_type, package, name;
5943 {
5944   tree new_qualified = merge_qualified_name (package, name);
5945   tree new_class_decl;
5946 
5947   if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5948     load_class (new_qualified, 0);
5949   if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5950     {
5951       if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5952 	  !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5953 	load_class (new_qualified, 0);
5954       TYPE_NAME (class_type) = new_qualified;
5955       return IDENTIFIER_CLASS_VALUE (new_qualified);
5956     }
5957   return NULL_TREE;
5958 }
5959 
5960 /* Resolve NAME and lay it out (if not done and if not the current
5961    parsed class). Return a decl node. This function is meant to be
5962    called when type resolution is necessary during the walk pass.  */
5963 
5964 static tree
resolve_and_layout(something,cl)5965 resolve_and_layout (something, cl)
5966      tree something;
5967      tree cl;
5968 {
5969   tree decl, decl_type;
5970 
5971   /* Don't do that on the current class */
5972   if (something == current_class)
5973     return TYPE_NAME (current_class);
5974 
5975   /* Don't do anything for void and other primitive types */
5976   if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5977     return NULL_TREE;
5978 
5979   /* Pointer types can be reall pointer types or fake pointers. When
5980      finding a real pointer, recheck for primitive types */
5981   if (TREE_CODE (something) == POINTER_TYPE)
5982     {
5983       if (TREE_TYPE (something))
5984 	{
5985 	  something = TREE_TYPE (something);
5986 	  if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5987 	    return NULL_TREE;
5988 	}
5989       else
5990 	something = TYPE_NAME (something);
5991     }
5992 
5993   /* Don't do anything for arrays of primitive types */
5994   if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5995       && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5996     return NULL_TREE;
5997 
5998   /* Something might be a WFL */
5999   if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
6000     something = EXPR_WFL_NODE (something);
6001 
6002   /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
6003      TYPE_DECL or a real TYPE */
6004   else if (TREE_CODE (something) != IDENTIFIER_NODE)
6005     something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
6006 	    DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
6007 
6008   if (!(decl = resolve_no_layout (something, cl)))
6009     return NULL_TREE;
6010 
6011   /* Resolve and layout if necessary */
6012   decl_type = TREE_TYPE (decl);
6013   layout_class_methods (decl_type);
6014   /* Check methods */
6015   if (CLASS_FROM_SOURCE_P (decl_type))
6016     java_check_methods (decl);
6017   /* Layout the type if necessary */
6018   if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
6019     safe_layout_class (decl_type);
6020 
6021   return decl;
6022 }
6023 
6024 /* Resolve a class, returns its decl but doesn't perform any
6025    layout. The current parsing context is saved and restored */
6026 
6027 static tree
resolve_no_layout(name,cl)6028 resolve_no_layout (name, cl)
6029      tree name, cl;
6030 {
6031   tree ptr, decl;
6032   BUILD_PTR_FROM_NAME (ptr, name);
6033   java_parser_context_save_global ();
6034   decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
6035   java_parser_context_restore_global ();
6036 
6037   return decl;
6038 }
6039 
6040 /* Called when reporting errors. Skip the '[]'s in a complex array
6041    type description that failed to be resolved. purify_type_name can't
6042    use an identifier tree.  */
6043 
6044 static const char *
purify_type_name(name)6045 purify_type_name (name)
6046      const char *name;
6047 {
6048   int len = strlen (name);
6049   int bracket_found;
6050 
6051   STRING_STRIP_BRACKETS (name, len, bracket_found);
6052   if (bracket_found)
6053     {
6054       char *stripped_name = xmemdup (name, len, len+1);
6055       stripped_name [len] = '\0';
6056       return stripped_name;
6057     }
6058   return name;
6059 }
6060 
6061 /* The type CURRENT refers to can't be found. We print error messages.  */
6062 
6063 static void
complete_class_report_errors(dep)6064 complete_class_report_errors (dep)
6065      jdep *dep;
6066 {
6067   const char *name;
6068 
6069   if (!JDEP_WFL (dep))
6070     return;
6071 
6072   name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
6073   switch (JDEP_KIND (dep))
6074     {
6075     case JDEP_SUPER:
6076       parse_error_context
6077 	(JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
6078 	 purify_type_name (name),
6079 	 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6080       break;
6081     case JDEP_FIELD:
6082       parse_error_context
6083 	(JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
6084 	 purify_type_name (name),
6085 	 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6086       break;
6087     case JDEP_METHOD:		/* Covers arguments */
6088       parse_error_context
6089 	(JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
6090 	 purify_type_name (name),
6091 	 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
6092 	 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
6093       break;
6094     case JDEP_METHOD_RETURN:	/* Covers return type */
6095       parse_error_context
6096 	(JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
6097 	 purify_type_name (name),
6098 	 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
6099       break;
6100     case JDEP_INTERFACE:
6101       parse_error_context
6102 	(JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
6103 	 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
6104 	 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
6105 	 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6106       break;
6107     case JDEP_VARIABLE:
6108       parse_error_context
6109 	(JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
6110 	 purify_type_name (IDENTIFIER_POINTER
6111 			   (EXPR_WFL_NODE (JDEP_WFL (dep)))),
6112 	 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6113       break;
6114     case JDEP_EXCEPTION:	/* As specified by `throws' */
6115       parse_error_context
6116 	  (JDEP_WFL (dep), "Class `%s' not found in `throws'",
6117 	 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
6118       break;
6119     default:
6120       /* Fix for -Wall. Just break doing nothing. The error will be
6121          caught later */
6122       break;
6123     }
6124 }
6125 
6126 /* Return a static string containing the DECL prototype string. If
6127    DECL is a constructor, use the class name instead of the form
6128    <init> */
6129 
6130 static const char *
get_printable_method_name(decl)6131 get_printable_method_name (decl)
6132      tree decl;
6133 {
6134   const char *to_return;
6135   tree name = NULL_TREE;
6136 
6137   if (DECL_CONSTRUCTOR_P (decl))
6138     {
6139       name = DECL_NAME (decl);
6140       DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6141     }
6142 
6143   to_return = lang_printable_name (decl, 0);
6144   if (DECL_CONSTRUCTOR_P (decl))
6145     DECL_NAME (decl) = name;
6146 
6147   return to_return;
6148 }
6149 
6150 /* Track method being redefined inside the same class. As a side
6151    effect, set DECL_NAME to an IDENTIFIER (prior entering this
6152    function it's a FWL, so we can track errors more accurately.)  */
6153 
6154 static int
check_method_redefinition(class,method)6155 check_method_redefinition (class, method)
6156      tree class, method;
6157 {
6158   tree redef, sig;
6159 
6160   /* There's no need to verify <clinit> and finit$ and instinit$ */
6161   if (DECL_CLINIT_P (method)
6162       || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6163     return 0;
6164 
6165   sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6166   for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6167     {
6168       if (redef == method)
6169 	break;
6170       if (DECL_NAME (redef) == DECL_NAME (method)
6171 	  && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6172 	  && !DECL_ARTIFICIAL (method))
6173 	{
6174 	  parse_error_context
6175 	    (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
6176 	     (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6177 	     get_printable_method_name (redef));
6178 	  return 1;
6179 	}
6180     }
6181   return 0;
6182 }
6183 
6184 /* Return 1 if check went ok, 0 otherwise.  */
6185 static int
check_abstract_method_definitions(do_interface,class_decl,type)6186 check_abstract_method_definitions (do_interface, class_decl, type)
6187      int do_interface;
6188      tree class_decl, type;
6189 {
6190   tree class = TREE_TYPE (class_decl);
6191   tree method, end_type;
6192   int ok = 1;
6193 
6194   end_type = (do_interface ? object_type_node : type);
6195   for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6196     {
6197       tree other_super, other_method, method_sig, method_name;
6198       int found = 0;
6199       int end_type_reached = 0;
6200 
6201       if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6202 	continue;
6203 
6204       /* Now verify that somewhere in between TYPE and CLASS,
6205 	 abstract method METHOD gets a non abstract definition
6206 	 that is inherited by CLASS.  */
6207 
6208       method_sig = build_java_signature (TREE_TYPE (method));
6209       method_name = DECL_NAME (method);
6210       if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6211 	method_name = EXPR_WFL_NODE (method_name);
6212 
6213       other_super = class;
6214       do {
6215 	if (other_super == end_type)
6216 	  end_type_reached = 1;
6217 
6218 	/* Method search */
6219 	for (other_method = TYPE_METHODS (other_super); other_method;
6220             other_method = TREE_CHAIN (other_method))
6221 	  {
6222 	    tree s = build_java_signature (TREE_TYPE (other_method));
6223 	    tree other_name = DECL_NAME (other_method);
6224 
6225 	    if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6226 	      other_name = EXPR_WFL_NODE (other_name);
6227 	    if (!DECL_CLINIT_P (other_method)
6228 		&& !DECL_CONSTRUCTOR_P (other_method)
6229 		&& method_name == other_name
6230 		&& method_sig == s
6231 		&& !METHOD_ABSTRACT (other_method))
6232              {
6233                found = 1;
6234                break;
6235              }
6236 	  }
6237 	other_super = CLASSTYPE_SUPER (other_super);
6238       } while (!end_type_reached);
6239 
6240       /* Report that abstract METHOD didn't find an implementation
6241 	 that CLASS can use. */
6242       if (!found)
6243 	{
6244 	  char *t = xstrdup (lang_printable_name
6245 			    (TREE_TYPE (TREE_TYPE (method)), 0));
6246 	  tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6247 
6248 	  parse_error_context
6249 	    (lookup_cl (class_decl),
6250 	     "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",
6251 	     IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6252 	     t, lang_printable_name (method, 0),
6253 	     (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6254 	      "interface" : "class"),
6255 	     IDENTIFIER_POINTER (ccn),
6256 	     (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6257 	     IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6258 	  ok = 0;
6259 	  free (t);
6260 	}
6261     }
6262 
6263   if (ok && do_interface)
6264     {
6265       /* Check for implemented interfaces. */
6266       int i;
6267       tree vector = TYPE_BINFO_BASETYPES (type);
6268       for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
6269 	{
6270 	  tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6271 	  ok = check_abstract_method_definitions (1, class_decl, super);
6272 	}
6273     }
6274 
6275   return ok;
6276 }
6277 
6278 /* Check that CLASS_DECL somehow implements all inherited abstract
6279    methods.  */
6280 
6281 static void
java_check_abstract_method_definitions(class_decl)6282 java_check_abstract_method_definitions (class_decl)
6283      tree class_decl;
6284 {
6285   tree class = TREE_TYPE (class_decl);
6286   tree super, vector;
6287   int i;
6288 
6289   if (CLASS_ABSTRACT (class_decl))
6290     return;
6291 
6292   /* Check for inherited types */
6293   super = class;
6294   do {
6295     super = CLASSTYPE_SUPER (super);
6296     check_abstract_method_definitions (0, class_decl, super);
6297   } while (super != object_type_node);
6298 
6299   /* Check for implemented interfaces. */
6300   vector = TYPE_BINFO_BASETYPES (class);
6301   for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
6302     {
6303       super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6304       check_abstract_method_definitions (1, class_decl, super);
6305     }
6306 }
6307 
6308 /* Check all the types method DECL uses and return 1 if all of them
6309    are now complete, 0 otherwise. This is used to check whether its
6310    safe to build a method signature or not.  */
6311 
6312 static int
check_method_types_complete(decl)6313 check_method_types_complete (decl)
6314      tree decl;
6315 {
6316   tree type = TREE_TYPE (decl);
6317   tree args;
6318 
6319   if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6320     return 0;
6321 
6322   args = TYPE_ARG_TYPES (type);
6323   if (TREE_CODE (type) == METHOD_TYPE)
6324     args = TREE_CHAIN (args);
6325   for (; args != end_params_node; args = TREE_CHAIN (args))
6326     if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6327       return 0;
6328 
6329   return 1;
6330 }
6331 
6332 /* Visible interface to check methods contained in CLASS_DECL */
6333 
6334 void
java_check_methods(class_decl)6335 java_check_methods (class_decl)
6336      tree class_decl;
6337 {
6338   if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6339     return;
6340 
6341   if (CLASS_INTERFACE (class_decl))
6342     java_check_abstract_methods (class_decl);
6343   else
6344     java_check_regular_methods (class_decl);
6345 
6346   CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6347 }
6348 
6349 /* Check all the methods of CLASS_DECL. Methods are first completed
6350    then checked according to regular method existence rules.  If no
6351    constructor for CLASS_DECL were encountered, then build its
6352    declaration.  */
6353 
6354 static void
java_check_regular_methods(class_decl)6355 java_check_regular_methods (class_decl)
6356      tree class_decl;
6357 {
6358   int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6359   tree method;
6360   tree class = TREE_TYPE (class_decl);
6361   tree found = NULL_TREE;
6362   tree mthrows;
6363 
6364   /* It is not necessary to check methods defined in java.lang.Object */
6365   if (class == object_type_node)
6366     return;
6367 
6368   if (!TYPE_NVIRTUALS (class))
6369     TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6370 
6371   /* Should take interfaces into account. FIXME */
6372   for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6373     {
6374       tree sig;
6375       tree method_wfl = DECL_FUNCTION_WFL (method);
6376       int aflags;
6377 
6378       /* Check for redefinitions */
6379       if (check_method_redefinition (class, method))
6380 	continue;
6381 
6382       /* We verify things thrown by the method.  They must inherit from
6383 	 java.lang.Throwable.  */
6384       for (mthrows = DECL_FUNCTION_THROWS (method);
6385 	   mthrows; mthrows = TREE_CHAIN (mthrows))
6386 	{
6387 	  if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6388 	    parse_error_context
6389 	      (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6390 	       IDENTIFIER_POINTER
6391 	         (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6392 	}
6393 
6394       /* If we see one constructor a mark so we don't generate the
6395 	 default one.  Also skip other verifications: constructors
6396 	 can't be inherited hence hidden or overridden.  */
6397       if (DECL_CONSTRUCTOR_P (method))
6398 	{
6399 	  saw_constructor = 1;
6400 	  continue;
6401 	}
6402 
6403       sig = build_java_argument_signature (TREE_TYPE (method));
6404       found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6405 
6406       /* Inner class can't declare static methods */
6407       if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6408 	{
6409 	  char *t = xstrdup (lang_printable_name (class, 0));
6410 	  parse_error_context
6411 	    (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6412 	     lang_printable_name (method, 0), t);
6413 	  free (t);
6414 	}
6415 
6416       /* Nothing overrides or it's a private method. */
6417       if (!found)
6418 	continue;
6419       if (METHOD_PRIVATE (found))
6420 	{
6421 	  found = NULL_TREE;
6422 	  continue;
6423 	}
6424 
6425       /* If `found' is declared in an interface, make sure the
6426 	 modifier matches. */
6427       if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6428 	  && clinit_identifier_node != DECL_NAME (found)
6429 	  && !METHOD_PUBLIC (method))
6430 	{
6431 	  tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6432 	  parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6433 			       IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6434 			       lang_printable_name (method, 0),
6435 			       IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6436 	}
6437 
6438       /* Can't override a method with the same name and different return
6439 	 types. */
6440       if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6441 	{
6442 	  char *t = xstrdup
6443 	    (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6444 	  parse_error_context
6445 	    (method_wfl,
6446 	     "Method `%s' was defined with return type `%s' in class `%s'",
6447 	     lang_printable_name (found, 0), t,
6448 	     IDENTIFIER_POINTER
6449 	       (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6450 	  free (t);
6451 	}
6452 
6453       aflags = get_access_flags_from_decl (found);
6454 
6455       /* Can't override final. Can't override static. */
6456       if (METHOD_FINAL (found) || METHOD_STATIC (found))
6457 	{
6458 	  /* Static *can* override static */
6459 	  if (METHOD_STATIC (found) && METHOD_STATIC (method))
6460 	    continue;
6461 	  parse_error_context
6462 	    (method_wfl,
6463 	     "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6464 	     (METHOD_FINAL (found) ? "Final" : "Static"),
6465 	     lang_printable_name (found, 0),
6466 	     (METHOD_FINAL (found) ? "final" : "static"),
6467 	     IDENTIFIER_POINTER
6468 	       (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6469 	  continue;
6470 	}
6471 
6472       /* Static method can't override instance method. */
6473       if (METHOD_STATIC (method))
6474 	{
6475 	  parse_error_context
6476 	    (method_wfl,
6477 	     "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6478 	     lang_printable_name (found, 0),
6479 	     IDENTIFIER_POINTER
6480 	       (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6481 	  continue;
6482 	}
6483 
6484       /* - Overriding/hiding public must be public
6485 	 - Overriding/hiding protected must be protected or public
6486          - If the overriden or hidden method has default (package)
6487            access, then the overriding or hiding method must not be
6488            private; otherwise, a compile-time error occurs.  If
6489            `found' belongs to an interface, things have been already
6490            taken care of.  */
6491       if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6492 	  && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6493 	      || (METHOD_PROTECTED (found)
6494 		  && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6495 	      || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6496 		  && METHOD_PRIVATE (method))))
6497 	{
6498 	  parse_error_context
6499 	    (method_wfl,
6500 	     "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6501 	     (METHOD_PUBLIC (method) ? "public" :
6502 	      (METHOD_PRIVATE (method) ? "private" : "protected")),
6503 	     IDENTIFIER_POINTER (DECL_NAME
6504 				 (TYPE_NAME (DECL_CONTEXT (found)))));
6505 	  continue;
6506 	}
6507 
6508       /* Overriding methods must have compatible `throws' clauses on checked
6509 	 exceptions, if any */
6510       check_throws_clauses (method, method_wfl, found);
6511 
6512       /* Inheriting multiple methods with the same signature. FIXME */
6513     }
6514 
6515   if (!TYPE_NVIRTUALS (class))
6516     TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6517 
6518   /* Search for inherited abstract method not yet implemented in this
6519      class.  */
6520   java_check_abstract_method_definitions (class_decl);
6521 
6522   if (!saw_constructor)
6523     abort ();
6524 }
6525 
6526 /* Return a nonzero value if the `throws' clause of METHOD (if any)
6527    is incompatible with the `throws' clause of FOUND (if any).  */
6528 
6529 static void
check_throws_clauses(method,method_wfl,found)6530 check_throws_clauses (method, method_wfl, found)
6531      tree method, method_wfl, found;
6532 {
6533   tree mthrows, fthrows;
6534 
6535   /* Can't check these things with class loaded from bytecode. FIXME */
6536   if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6537     return;
6538 
6539   for (mthrows = DECL_FUNCTION_THROWS (method);
6540        mthrows; mthrows = TREE_CHAIN (mthrows))
6541     {
6542       /* We don't verify unchecked expressions */
6543       if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6544 	continue;
6545       /* Checked expression must be compatible */
6546       for (fthrows = DECL_FUNCTION_THROWS (found);
6547 	   fthrows; fthrows = TREE_CHAIN (fthrows))
6548 	if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6549 	  break;
6550       if (!fthrows)
6551 	{
6552 	  parse_error_context
6553 	    (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'",
6554 	     IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6555 	     lang_printable_name (found, 0),
6556 	     IDENTIFIER_POINTER
6557 	       (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6558 	}
6559     }
6560 }
6561 
6562 /* Check abstract method of interface INTERFACE */
6563 
6564 static void
java_check_abstract_methods(interface_decl)6565 java_check_abstract_methods (interface_decl)
6566      tree interface_decl;
6567 {
6568   int i, n;
6569   tree method, basetype_vec, found;
6570   tree interface = TREE_TYPE (interface_decl);
6571 
6572   for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6573     {
6574       /* 2- Check for double definition inside the defining interface */
6575       if (check_method_redefinition (interface, method))
6576 	continue;
6577 
6578       /* 3- Overriding is OK as far as we preserve the return type and
6579 	 the thrown exceptions (FIXME) */
6580       found = lookup_java_interface_method2 (interface, method);
6581       if (found)
6582 	{
6583 	  char *t;
6584 	  t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6585 	  parse_error_context
6586 	    (DECL_FUNCTION_WFL (found),
6587 	     "Method `%s' was defined with return type `%s' in class `%s'",
6588 	     lang_printable_name (found, 0), t,
6589 	     IDENTIFIER_POINTER
6590 	       (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6591 	  free (t);
6592 	  continue;
6593 	}
6594     }
6595 
6596   /* 4- Inherited methods can't differ by their returned types */
6597   if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6598     return;
6599   n = TREE_VEC_LENGTH (basetype_vec);
6600   for (i = 0; i < n; i++)
6601     {
6602       tree sub_interface_method, sub_interface;
6603       tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6604       if (!vec_elt)
6605 	continue;
6606       sub_interface = BINFO_TYPE (vec_elt);
6607       for (sub_interface_method = TYPE_METHODS (sub_interface);
6608 	   sub_interface_method;
6609 	   sub_interface_method = TREE_CHAIN (sub_interface_method))
6610 	{
6611 	  found = lookup_java_interface_method2 (interface,
6612 						 sub_interface_method);
6613 	  if (found && (found != sub_interface_method))
6614 	    {
6615 	      parse_error_context
6616 		(lookup_cl (sub_interface_method),
6617 		 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6618 		 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6619 		 lang_printable_name (found, 0),
6620 		 IDENTIFIER_POINTER
6621 		   (DECL_NAME (TYPE_NAME
6622 			       (DECL_CONTEXT (sub_interface_method)))),
6623 		 IDENTIFIER_POINTER
6624 	           (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6625 	    }
6626 	}
6627     }
6628 }
6629 
6630 /* Lookup methods in interfaces using their name and partial
6631    signature. Return a matching method only if their types differ.  */
6632 
6633 static tree
lookup_java_interface_method2(class,method_decl)6634 lookup_java_interface_method2 (class, method_decl)
6635      tree class, method_decl;
6636 {
6637   int i, n;
6638   tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6639 
6640   if (!basetype_vec)
6641     return NULL_TREE;
6642 
6643   n = TREE_VEC_LENGTH (basetype_vec);
6644   for (i = 0; i < n; i++)
6645     {
6646       tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6647       if ((BINFO_TYPE (vec_elt) != object_type_node)
6648 	  && (to_return =
6649 	      lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6650 	return to_return;
6651     }
6652   for (i = 0; i < n; i++)
6653     {
6654       to_return = lookup_java_interface_method2
6655 	(BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6656       if (to_return)
6657 	return to_return;
6658     }
6659 
6660   return NULL_TREE;
6661 }
6662 
6663 /* Lookup method using their name and partial signature. Return a
6664    matching method only if their types differ.  */
6665 
6666 static tree
lookup_java_method2(clas,method_decl,do_interface)6667 lookup_java_method2 (clas, method_decl, do_interface)
6668      tree clas, method_decl;
6669      int do_interface;
6670 {
6671   tree method, method_signature, method_name, method_type, name;
6672 
6673   method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6674   name = DECL_NAME (method_decl);
6675   method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6676 		 EXPR_WFL_NODE (name) : name);
6677   method_type = TREE_TYPE (TREE_TYPE (method_decl));
6678 
6679   while (clas != NULL_TREE)
6680     {
6681       for (method = TYPE_METHODS (clas);
6682 	   method != NULL_TREE;  method = TREE_CHAIN (method))
6683 	{
6684 	  tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6685 	  tree name = DECL_NAME (method);
6686 	  if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6687 	       EXPR_WFL_NODE (name) : name) == method_name
6688 	      && method_sig == method_signature
6689 	      && TREE_TYPE (TREE_TYPE (method)) != method_type)
6690 	    return method;
6691 	}
6692       clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6693     }
6694   return NULL_TREE;
6695 }
6696 
6697 /* Return the line that matches DECL line number, and try its best to
6698    position the column number. Used during error reports.  */
6699 
6700 static GTY(()) tree cl_v;
6701 static tree
lookup_cl(decl)6702 lookup_cl (decl)
6703      tree decl;
6704 {
6705   char *line, *found;
6706 
6707   if (!decl)
6708     return NULL_TREE;
6709 
6710   if (cl_v == NULL_TREE)
6711     {
6712       cl_v = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6713     }
6714 
6715   EXPR_WFL_FILENAME_NODE (cl_v) = get_identifier (DECL_SOURCE_FILE (decl));
6716   EXPR_WFL_SET_LINECOL (cl_v, DECL_SOURCE_LINE_FIRST (decl), -1);
6717 
6718   line = java_get_line_col (EXPR_WFL_FILENAME (cl_v),
6719 			    EXPR_WFL_LINENO (cl_v), EXPR_WFL_COLNO (cl_v));
6720 
6721   found = strstr ((const char *)line,
6722 		  (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6723   if (found)
6724     EXPR_WFL_SET_LINECOL (cl_v, EXPR_WFL_LINENO (cl_v), found - line);
6725 
6726   return cl_v;
6727 }
6728 
6729 /* Look for a simple name in the single-type import list */
6730 
6731 static tree
find_name_in_single_imports(name)6732 find_name_in_single_imports (name)
6733      tree name;
6734 {
6735   tree node;
6736 
6737   for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6738     if (TREE_VALUE (node) == name)
6739       return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6740 
6741   return NULL_TREE;
6742 }
6743 
6744 /* Process all single-type import. */
6745 
6746 static int
process_imports()6747 process_imports ()
6748 {
6749   tree import;
6750   int error_found;
6751 
6752   for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6753     {
6754       tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6755       char *original_name;
6756 
6757       obstack_grow0 (&temporary_obstack,
6758 		     IDENTIFIER_POINTER (to_be_found),
6759 		     IDENTIFIER_LENGTH (to_be_found));
6760       original_name = obstack_finish (&temporary_obstack);
6761 
6762       /* Don't load twice something already defined. */
6763       if (IDENTIFIER_CLASS_VALUE (to_be_found))
6764 	continue;
6765 
6766       while (1)
6767 	{
6768 	  tree left;
6769 
6770 	  QUALIFIED_P (to_be_found) = 1;
6771 	  load_class (to_be_found, 0);
6772 	  error_found =
6773 	    check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true);
6774 
6775 	  /* We found it, we can bail out */
6776 	  if (IDENTIFIER_CLASS_VALUE (to_be_found))
6777 	    break;
6778 
6779 	  /* We haven't found it. Maybe we're trying to access an
6780 	     inner class.  The only way for us to know is to try again
6781 	     after having dropped a qualifier. If we can't break it further,
6782 	     we have an error. */
6783 	  if (breakdown_qualified (&left, NULL, to_be_found))
6784 	    break;
6785 
6786 	  to_be_found = left;
6787 	}
6788       if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6789 	{
6790 	  parse_error_context (TREE_PURPOSE (import),
6791 			       "Class or interface `%s' not found in import",
6792 			       original_name);
6793 	  error_found = 1;
6794 	}
6795 
6796       obstack_free (&temporary_obstack, original_name);
6797       if (error_found)
6798 	return 1;
6799     }
6800   return 0;
6801 }
6802 
6803 /* Possibly find and mark a class imported by a single-type import
6804    statement.  */
6805 
6806 static void
find_in_imports(enclosing_type,class_type)6807 find_in_imports (enclosing_type, class_type)
6808      tree enclosing_type;
6809      tree class_type;
6810 {
6811   tree import = (enclosing_type ? TYPE_IMPORT_LIST (enclosing_type) :
6812 		 ctxp->import_list);
6813   while (import)
6814     {
6815       if (TREE_VALUE (import) == TYPE_NAME (class_type))
6816 	{
6817 	  TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6818 	  QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6819 	  return;
6820 	}
6821       import = TREE_CHAIN (import);
6822     }
6823 }
6824 
6825 static int
note_possible_classname(name,len)6826 note_possible_classname (name, len)
6827      const char *name;
6828      int len;
6829 {
6830   tree node;
6831   if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6832     len = len - 5;
6833   else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6834     len = len - 6;
6835   else
6836     return 0;
6837   node = ident_subst (name, len, "", '/', '.', "");
6838   IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6839   QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6840   return 1;
6841 }
6842 
6843 /* Read a import directory, gathering potential match for further type
6844    references. Indifferently reads a filesystem or a ZIP archive
6845    directory.  */
6846 
6847 static void
read_import_dir(wfl)6848 read_import_dir (wfl)
6849      tree wfl;
6850 {
6851   tree package_id = EXPR_WFL_NODE (wfl);
6852   const char *package_name = IDENTIFIER_POINTER (package_id);
6853   int package_length = IDENTIFIER_LENGTH (package_id);
6854   DIR *dirp = NULL;
6855   JCF *saved_jcf = current_jcf;
6856 
6857   int found = 0;
6858   int k;
6859   void *entry;
6860   struct buffer filename[1];
6861 
6862   if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6863     return;
6864   IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6865 
6866   BUFFER_INIT (filename);
6867   buffer_grow (filename, package_length + 100);
6868 
6869   for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6870     {
6871       const char *entry_name = jcf_path_name (entry);
6872       int entry_length = strlen (entry_name);
6873       if (jcf_path_is_zipfile (entry))
6874 	{
6875 	  ZipFile *zipf;
6876 	  buffer_grow (filename, entry_length);
6877 	  memcpy (filename->data, entry_name, entry_length - 1);
6878 	  filename->data[entry_length-1] = '\0';
6879 	  zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6880 	  if (zipf == NULL)
6881 	    error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6882 	  else
6883 	    {
6884 	      ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6885 	      BUFFER_RESET (filename);
6886 	      for (k = 0; k < package_length; k++)
6887 		{
6888 		  char ch = package_name[k];
6889 		  *filename->ptr++ = ch == '.' ? '/' : ch;
6890 		}
6891 	      *filename->ptr++ = '/';
6892 
6893 	      for (k = 0; k < zipf->count;  k++, zipd = ZIPDIR_NEXT (zipd))
6894 		{
6895 		  const char *current_entry = ZIPDIR_FILENAME (zipd);
6896 		  int current_entry_len = zipd->filename_length;
6897 
6898 		  if (current_entry_len >= BUFFER_LENGTH (filename)
6899 		      && strncmp (filename->data, current_entry,
6900 				  BUFFER_LENGTH (filename)) != 0)
6901 		    continue;
6902 		  found |= note_possible_classname (current_entry,
6903 						    current_entry_len);
6904 		}
6905 	    }
6906 	}
6907       else
6908 	{
6909 	  BUFFER_RESET (filename);
6910 	  buffer_grow (filename, entry_length + package_length + 4);
6911 	  strcpy (filename->data, entry_name);
6912 	  filename->ptr = filename->data + entry_length;
6913 	  for (k = 0; k < package_length; k++)
6914 	    {
6915 	      char ch = package_name[k];
6916 	      *filename->ptr++ = ch == '.' ? '/' : ch;
6917 	    }
6918 	  *filename->ptr = '\0';
6919 
6920 	  dirp = opendir (filename->data);
6921 	  if (dirp == NULL)
6922 	    continue;
6923 	  *filename->ptr++ = '/';
6924 	  for (;;)
6925 	    {
6926 	      int len;
6927 	      const char *d_name;
6928 	      struct dirent *direntp = readdir (dirp);
6929 	      if (!direntp)
6930 		break;
6931 	      d_name = direntp->d_name;
6932 	      len = strlen (direntp->d_name);
6933 	      buffer_grow (filename, len+1);
6934 	      strcpy (filename->ptr, d_name);
6935 	      found |= note_possible_classname (filename->data + entry_length,
6936 						package_length+len+1);
6937 	    }
6938 	  if (dirp)
6939 	    closedir (dirp);
6940 	}
6941     }
6942 
6943   free (filename->data);
6944 
6945   /* Here we should have a unified way of retrieving an entry, to be
6946      indexed. */
6947   if (!found)
6948     {
6949       static int first = 1;
6950       if (first)
6951 	{
6952 	  error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives", package_name);
6953 	  java_error_count++;
6954 	  first = 0;
6955 	}
6956       else
6957 	parse_error_context (wfl, "Package `%s' not found in import",
6958 			     package_name);
6959       current_jcf = saved_jcf;
6960       return;
6961     }
6962   current_jcf = saved_jcf;
6963 }
6964 
6965 /* Possibly find a type in the import on demands specified
6966    types. Returns 1 if an error occurred, 0 otherwise. Run through the
6967    entire list, to detected potential double definitions.  */
6968 
6969 static int
find_in_imports_on_demand(enclosing_type,class_type)6970 find_in_imports_on_demand (enclosing_type, class_type)
6971      tree enclosing_type;
6972      tree class_type;
6973 {
6974   tree class_type_name = TYPE_NAME (class_type);
6975   tree import = (enclosing_type ? TYPE_IMPORT_DEMAND_LIST (enclosing_type) :
6976 		  ctxp->import_demand_list);
6977   tree cl = NULL_TREE;
6978   int seen_once = -1;	/* -1 when not set, 1 if seen once, >1 otherwise. */
6979   int to_return = -1;	/* -1 when not set, 0 or 1 otherwise */
6980   tree node;
6981 
6982   for (; import; import = TREE_CHAIN (import))
6983     {
6984       int saved_lineno = lineno;
6985       int access_check;
6986       const char *id_name;
6987       tree decl, type_name_copy;
6988 
6989       obstack_grow (&temporary_obstack,
6990 		    IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6991 		    IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6992       obstack_1grow (&temporary_obstack, '.');
6993       obstack_grow0 (&temporary_obstack,
6994 		     IDENTIFIER_POINTER (class_type_name),
6995 		     IDENTIFIER_LENGTH (class_type_name));
6996       id_name = obstack_finish (&temporary_obstack);
6997 
6998       if (! (node = maybe_get_identifier (id_name)))
6999 	continue;
7000 
7001       /* Setup lineno so that it refers to the line of the import (in
7002 	 case we parse a class file and encounter errors */
7003       lineno = EXPR_WFL_LINENO (TREE_PURPOSE (import));
7004 
7005       type_name_copy = TYPE_NAME (class_type);
7006       TYPE_NAME (class_type) = node;
7007       QUALIFIED_P (node) = 1;
7008       decl = IDENTIFIER_CLASS_VALUE (node);
7009       access_check = -1;
7010       /* If there is no DECL set for the class or if the class isn't
7011 	 loaded and not seen in source yet, then load */
7012       if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
7013 		    && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
7014 	{
7015 	  load_class (node, 0);
7016 	  decl = IDENTIFIER_CLASS_VALUE (node);
7017 	}
7018       if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
7019 	access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
7020 					       false);
7021       else
7022 	/* 6.6.1: Inner classes are subject to member access rules. */
7023 	access_check = 0;
7024 
7025       lineno = saved_lineno;
7026 
7027       /* If the loaded class is not accessible or couldn't be loaded,
7028 	 we restore the original TYPE_NAME and process the next
7029 	 import. */
7030       if (access_check || !decl)
7031 	{
7032 	  TYPE_NAME (class_type) = type_name_copy;
7033 	  continue;
7034 	}
7035 
7036       /* If the loaded class is accessible, we keep a tab on it to
7037 	 detect and report multiple inclusions. */
7038       if (IS_A_CLASSFILE_NAME (node))
7039 	{
7040 	  if (seen_once < 0)
7041 	    {
7042 	      cl = TREE_PURPOSE (import);
7043 	      seen_once = 1;
7044 	    }
7045 	  else if (seen_once >= 0)
7046 	    {
7047 	      tree location = (cl ? cl : TREE_PURPOSE (import));
7048 	      tree package = (cl ? EXPR_WFL_NODE (cl) :
7049 			      EXPR_WFL_NODE (TREE_PURPOSE (import)));
7050 	      seen_once++;
7051 	      parse_error_context
7052 		(location,
7053 		 "Type `%s' also potentially defined in package `%s'",
7054 		 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
7055 		 IDENTIFIER_POINTER (package));
7056 	    }
7057 	}
7058       to_return = access_check;
7059     }
7060 
7061   if (seen_once == 1)
7062     return to_return;
7063   else
7064     return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
7065 }
7066 
7067 /* Add package NAME to the list of packages encountered so far. To
7068    speed up class lookup in do_resolve_class, we make sure a
7069    particular package is added only once.  */
7070 
7071 static void
register_package(name)7072 register_package (name)
7073      tree name;
7074 {
7075   static htab_t pht;
7076   PTR *e;
7077 
7078   if (pht == NULL)
7079     pht = htab_create (50, htab_hash_pointer, htab_eq_pointer, NULL);
7080 
7081   e = htab_find_slot (pht, name, INSERT);
7082   if (*e == NULL)
7083     {
7084       package_list = chainon (package_list, build_tree_list (name, NULL));
7085       *e = name;
7086     }
7087 }
7088 
7089 static tree
resolve_package(pkg,next,type_name)7090 resolve_package (pkg, next, type_name)
7091      tree pkg, *next, *type_name;
7092 {
7093   tree current;
7094   tree decl = NULL_TREE;
7095   *type_name = NULL_TREE;
7096 
7097   /* The trick is to determine when the package name stops and were
7098      the name of something contained in the package starts. Then we
7099      return a fully qualified name of what we want to get. */
7100 
7101   *next = EXPR_WFL_QUALIFICATION (pkg);
7102 
7103   /* Try to progressively construct a type name */
7104   if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
7105     for (current = EXPR_WFL_QUALIFICATION (pkg);
7106 	 current; current = TREE_CHAIN (current))
7107       {
7108 	/* If we don't have what we're expecting, exit now. TYPE_NAME
7109 	   will be null and the error caught later. */
7110 	if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
7111 	  break;
7112 	*type_name =
7113 	  merge_qualified_name (*type_name, EXPR_WFL_NODE (QUAL_WFL (current)));
7114 	if ((decl = resolve_no_layout (*type_name, NULL_TREE)))
7115 	  {
7116 	    /* resolve_package should be used in a loop, hence we
7117 	       point at this one to naturally process the next one at
7118 	       the next iteration. */
7119 	    *next = current;
7120 	    break;
7121 	  }
7122       }
7123   return decl;
7124 }
7125 
7126 
7127 /* Check accessibility of inner classes according to member access rules.
7128    DECL is the inner class, ENCLOSING_DECL is the class from which the
7129    access is being attempted. */
7130 
7131 static void
check_inner_class_access(decl,enclosing_decl,cl)7132 check_inner_class_access (decl, enclosing_decl, cl)
7133      tree decl, enclosing_decl, cl;
7134 {
7135   const char *access;
7136   tree enclosing_decl_type;
7137 
7138   /* We don't issue an error message when CL is null. CL can be null
7139      as a result of processing a JDEP crafted by source_start_java_method
7140      for the purpose of patching its parm decl. But the error would
7141      have been already trapped when fixing the method's signature.
7142      DECL can also be NULL in case of earlier errors. */
7143   if (!decl || !cl)
7144     return;
7145 
7146   enclosing_decl_type = TREE_TYPE (enclosing_decl);
7147 
7148   if (CLASS_PRIVATE (decl))
7149     {
7150       /* Access is permitted only within the body of the top-level
7151          class in which DECL is declared. */
7152       tree top_level = decl;
7153       while (DECL_CONTEXT (top_level))
7154         top_level = DECL_CONTEXT (top_level);
7155       while (DECL_CONTEXT (enclosing_decl))
7156         enclosing_decl = DECL_CONTEXT (enclosing_decl);
7157       if (top_level == enclosing_decl)
7158         return;
7159       access = "private";
7160     }
7161   else if (CLASS_PROTECTED (decl))
7162     {
7163       tree decl_context;
7164       /* Access is permitted from within the same package... */
7165       if (in_same_package (decl, enclosing_decl))
7166         return;
7167 
7168       /* ... or from within the body of a subtype of the context in which
7169          DECL is declared. */
7170       decl_context = DECL_CONTEXT (decl);
7171       while (enclosing_decl)
7172         {
7173 	  if (CLASS_INTERFACE (decl))
7174 	    {
7175 	      if (interface_of_p (TREE_TYPE (decl_context),
7176 				  enclosing_decl_type))
7177 		return;
7178 	    }
7179 	  else
7180 	    {
7181 	      /* Eww. The order of the arguments is different!! */
7182 	      if (inherits_from_p (enclosing_decl_type,
7183 				   TREE_TYPE (decl_context)))
7184 		return;
7185 	    }
7186 	  enclosing_decl = DECL_CONTEXT (enclosing_decl);
7187 	}
7188       access = "protected";
7189     }
7190   else if (! CLASS_PUBLIC (decl))
7191     {
7192       /* Access is permitted only from within the same package as DECL. */
7193       if (in_same_package (decl, enclosing_decl))
7194         return;
7195       access = "non-public";
7196     }
7197   else
7198     /* Class is public. */
7199     return;
7200 
7201   parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7202 		       (CLASS_INTERFACE (decl) ? "interface" : "class"),
7203 		       lang_printable_name (decl, 0), access);
7204 }
7205 
7206 /* Accessibility check for top-level classes. If CLASS_NAME is in a
7207    foreign package, it must be PUBLIC. Return 0 if no access
7208    violations were found, 1 otherwise. If VERBOSE is true and an error
7209    was found, it is reported and accounted for.  */
7210 
7211 static int
check_pkg_class_access(class_name,cl,verbose)7212 check_pkg_class_access (class_name, cl, verbose)
7213      tree class_name;
7214      tree cl;
7215      bool verbose;
7216 {
7217   tree type;
7218 
7219   if (!IDENTIFIER_CLASS_VALUE (class_name))
7220     return 0;
7221 
7222   if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7223     return 0;
7224 
7225   if (!CLASS_PUBLIC (TYPE_NAME (type)))
7226     {
7227       /* Access to a private class within the same package is
7228          allowed. */
7229       tree l, r;
7230       breakdown_qualified (&l, &r, class_name);
7231       if (!QUALIFIED_P (class_name) && !ctxp->package)
7232 	/* Both in the empty package. */
7233         return 0;
7234       if (l == ctxp->package)
7235 	/* Both in the same package. */
7236 	return 0;
7237 
7238       if (verbose)
7239 	parse_error_context
7240 	  (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
7241 	   (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7242 	   IDENTIFIER_POINTER (class_name));
7243       return 1;
7244     }
7245   return 0;
7246 }
7247 
7248 /* Local variable declaration. */
7249 
7250 static void
declare_local_variables(modifier,type,vlist)7251 declare_local_variables (modifier, type, vlist)
7252      int modifier;
7253      tree type;
7254      tree vlist;
7255 {
7256   tree decl, current, saved_type;
7257   tree type_wfl = NULL_TREE;
7258   int must_chain = 0;
7259   int final_p = 0;
7260 
7261   /* Push a new block if statements were seen between the last time we
7262      pushed a block and now. Keep a count of blocks to close */
7263   if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7264     {
7265       tree b = enter_block ();
7266       BLOCK_IS_IMPLICIT (b) = 1;
7267     }
7268 
7269   if (modifier)
7270     {
7271       size_t i;
7272       for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)
7273 	if (1 << i & modifier)
7274 	  break;
7275       if (modifier == ACC_FINAL)
7276 	final_p = 1;
7277       else
7278 	{
7279 	  parse_error_context
7280 	    (ctxp->modifier_ctx [i],
7281 	     "Only `final' is allowed as a local variables modifier");
7282 	  return;
7283 	}
7284     }
7285 
7286   /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7287      hold the TYPE value if a new incomplete has to be created (as
7288      opposed to being found already existing and reused). */
7289   SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7290 
7291   /* If TYPE is fully resolved and we don't have a reference, make one */
7292   PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7293 
7294   /* Go through all the declared variables */
7295   for (current = vlist, saved_type = type; current;
7296        current = TREE_CHAIN (current), type = saved_type)
7297     {
7298       tree other, real_type;
7299       tree wfl  = TREE_PURPOSE (current);
7300       tree name = EXPR_WFL_NODE (wfl);
7301       tree init = TREE_VALUE (current);
7302 
7303       /* Process NAME, as it may specify extra dimension(s) for it */
7304       type = build_array_from_name (type, type_wfl, name, &name);
7305 
7306       /* Variable redefinition check */
7307       if ((other = lookup_name_in_blocks (name)))
7308 	{
7309 	  variable_redefinition_error (wfl, name, TREE_TYPE (other),
7310 				       DECL_SOURCE_LINE (other));
7311 	  continue;
7312 	}
7313 
7314       /* Type adjustment. We may have just readjusted TYPE because
7315 	 the variable specified more dimensions. Make sure we have
7316 	 a reference if we can and don't have one already. */
7317       PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7318 
7319       real_type = GET_REAL_TYPE (type);
7320       /* Never layout this decl. This will be done when its scope
7321 	 will be entered */
7322       decl = build_decl (VAR_DECL, name, real_type);
7323       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7324       DECL_FINAL (decl) = final_p;
7325       BLOCK_CHAIN_DECL (decl);
7326 
7327       /* If doing xreferencing, replace the line number with the WFL
7328          compound value */
7329       if (flag_emit_xref)
7330 	DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7331 
7332       /* Don't try to use an INIT statement when an error was found */
7333       if (init && java_error_count)
7334 	init = NULL_TREE;
7335 
7336       /* Add the initialization function to the current function's code */
7337       if (init)
7338 	{
7339 	  /* Name might have been readjusted */
7340 	  EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7341 	  MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7342 	  java_method_add_stmt (current_function_decl,
7343 				build_debugable_stmt (EXPR_WFL_LINECOL (init),
7344 						      init));
7345 	}
7346 
7347       /* Setup dependency the type of the decl */
7348       if (must_chain)
7349 	{
7350 	  jdep *dep;
7351 	  register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7352 	  dep = CLASSD_LAST (ctxp->classd_list);
7353 	  JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7354 	}
7355     }
7356   SOURCE_FRONTEND_DEBUG (("Defined locals"));
7357 }
7358 
7359 /* Called during parsing. Build decls from argument list.  */
7360 
7361 static void
source_start_java_method(fndecl)7362 source_start_java_method (fndecl)
7363      tree fndecl;
7364 {
7365   tree tem;
7366   tree parm_decl;
7367   int i;
7368 
7369   if (!fndecl)
7370     return;
7371 
7372   current_function_decl = fndecl;
7373 
7374   /* New scope for the function */
7375   enter_block ();
7376   for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7377        tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7378     {
7379       tree type = TREE_VALUE (tem);
7380       tree name = TREE_PURPOSE (tem);
7381 
7382       /* If type is incomplete. Create an incomplete decl and ask for
7383 	 the decl to be patched later */
7384       if (INCOMPLETE_TYPE_P (type))
7385 	{
7386 	  jdep *jdep;
7387 	  tree real_type = GET_REAL_TYPE (type);
7388 	  parm_decl = build_decl (PARM_DECL, name, real_type);
7389 	  type = obtain_incomplete_type (type);
7390 	  register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7391 	  jdep = CLASSD_LAST (ctxp->classd_list);
7392 	  JDEP_MISC (jdep) = name;
7393 	  JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7394 	}
7395       else
7396 	parm_decl = build_decl (PARM_DECL, name, type);
7397 
7398       /* Remember if a local variable was declared final (via its
7399          TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7400       if (ARG_FINAL_P (tem))
7401 	{
7402 	  MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7403 	  DECL_FINAL (parm_decl) = 1;
7404 	}
7405 
7406       BLOCK_CHAIN_DECL (parm_decl);
7407     }
7408   tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7409   BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7410     nreverse (tem);
7411   DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7412   DECL_MAX_LOCALS (current_function_decl) = i;
7413 }
7414 
7415 /* Called during parsing. Creates an artificial method declaration.  */
7416 
7417 static tree
create_artificial_method(class,flags,type,name,args)7418 create_artificial_method (class, flags, type, name, args)
7419      tree class;
7420      int flags;
7421      tree type, name, args;
7422 {
7423   tree mdecl;
7424 
7425   java_parser_context_save_global ();
7426   lineno = 0;
7427   mdecl = make_node (FUNCTION_TYPE);
7428   TREE_TYPE (mdecl) = type;
7429   TYPE_ARG_TYPES (mdecl) = args;
7430   mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7431   java_parser_context_restore_global ();
7432   DECL_ARTIFICIAL (mdecl) = 1;
7433   return mdecl;
7434 }
7435 
7436 /* Starts the body if an artificial method.  */
7437 
7438 static void
start_artificial_method_body(mdecl)7439 start_artificial_method_body (mdecl)
7440      tree mdecl;
7441 {
7442   DECL_SOURCE_LINE (mdecl) = 1;
7443   DECL_SOURCE_LINE_MERGE (mdecl, 1);
7444   source_start_java_method (mdecl);
7445   enter_block ();
7446 }
7447 
7448 static void
end_artificial_method_body(mdecl)7449 end_artificial_method_body (mdecl)
7450      tree mdecl;
7451 {
7452   /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7453      It has to be evaluated first. (if mdecl is current_function_decl,
7454      we have an undefined behavior if no temporary variable is used.) */
7455   tree b = exit_block ();
7456   BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7457   exit_block ();
7458 }
7459 
7460 /* Dump a tree of some kind.  This is a convenience wrapper for the
7461    dump_* functions in tree-dump.c.  */
7462 static void
dump_java_tree(phase,t)7463 dump_java_tree (phase, t)
7464      enum tree_dump_index phase;
7465      tree t;
7466 {
7467   FILE *stream;
7468   int flags;
7469 
7470   stream = dump_begin (phase, &flags);
7471   flags |= TDF_SLIM;
7472   if (stream)
7473     {
7474       dump_node (t, flags, stream);
7475       dump_end (phase, stream);
7476     }
7477 }
7478 
7479 /* Terminate a function and expand its body.  */
7480 
7481 static void
source_end_java_method()7482 source_end_java_method ()
7483 {
7484   tree fndecl = current_function_decl;
7485 
7486   if (!fndecl)
7487     return;
7488 
7489   java_parser_context_save_global ();
7490   lineno = ctxp->last_ccb_indent1;
7491 
7492   /* Turn function bodies with only a NOP expr null, so they don't get
7493      generated at all and we won't get warnings when using the -W
7494      -Wall flags. */
7495   if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7496     BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7497 
7498   /* We've generated all the trees for this function, and it has been
7499      patched.  Dump it to a file if the user requested it.  */
7500   dump_java_tree (TDI_original, fndecl);
7501 
7502   java_optimize_inline (fndecl);
7503 
7504   /* Generate function's code */
7505   if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7506       && ! flag_emit_class_files
7507       && ! flag_emit_xref)
7508     expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7509 
7510   /* pop out of its parameters */
7511   pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7512   poplevel (1, 0, 1);
7513   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7514 
7515   /* Generate rtl for function exit.  */
7516   if (! flag_emit_class_files && ! flag_emit_xref)
7517     {
7518       lineno = DECL_SOURCE_LINE_LAST (fndecl);
7519       expand_function_end (input_filename, lineno, 0);
7520 
7521       DECL_SOURCE_LINE (fndecl) = DECL_SOURCE_LINE_FIRST (fndecl);
7522 
7523       /* Run the optimizers and output assembler code for this function. */
7524       rest_of_compilation (fndecl);
7525     }
7526 
7527   current_function_decl = NULL_TREE;
7528   java_parser_context_restore_global ();
7529 }
7530 
7531 /* Record EXPR in the current function block. Complements compound
7532    expression second operand if necessary.  */
7533 
7534 tree
java_method_add_stmt(fndecl,expr)7535 java_method_add_stmt (fndecl, expr)
7536      tree fndecl, expr;
7537 {
7538   if (!GET_CURRENT_BLOCK (fndecl))
7539     return NULL_TREE;
7540   return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7541 }
7542 
7543 static tree
add_stmt_to_block(b,type,stmt)7544 add_stmt_to_block (b, type, stmt)
7545      tree b, type, stmt;
7546 {
7547   tree body = BLOCK_EXPR_BODY (b), c;
7548 
7549   if (java_error_count)
7550     return body;
7551 
7552   if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7553     return body;
7554 
7555   BLOCK_EXPR_BODY (b) = c;
7556   TREE_SIDE_EFFECTS (c) = 1;
7557   return c;
7558 }
7559 
7560 /* Add STMT to EXISTING if possible, otherwise create a new
7561    COMPOUND_EXPR and add STMT to it. */
7562 
7563 static tree
add_stmt_to_compound(existing,type,stmt)7564 add_stmt_to_compound (existing, type, stmt)
7565      tree existing, type, stmt;
7566 {
7567   /* Keep track of this for inlining.  */
7568   if (current_function_decl)
7569     ++DECL_NUM_STMTS (current_function_decl);
7570 
7571   if (existing)
7572     return build (COMPOUND_EXPR, type, existing, stmt);
7573   else
7574     return stmt;
7575 }
7576 
java_layout_seen_class_methods()7577 void java_layout_seen_class_methods ()
7578 {
7579   tree previous_list = all_class_list;
7580   tree end = NULL_TREE;
7581   tree current;
7582 
7583   while (1)
7584     {
7585       for (current = previous_list;
7586 	   current != end; current = TREE_CHAIN (current))
7587 	layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7588 
7589       if (previous_list != all_class_list)
7590 	{
7591 	  end = previous_list;
7592 	  previous_list = all_class_list;
7593 	}
7594       else
7595 	break;
7596     }
7597 }
7598 
7599 static GTY(()) tree stop_reordering;
7600 void
java_reorder_fields()7601 java_reorder_fields ()
7602 {
7603   tree current;
7604 
7605   for (current = gclass_list; current; current = TREE_CHAIN (current))
7606     {
7607       current_class = TREE_TYPE (TREE_VALUE (current));
7608 
7609       if (current_class == stop_reordering)
7610 	break;
7611 
7612       /* Reverse the fields, but leave the dummy field in front.
7613 	 Fields are already ordered for Object and Class */
7614       if (TYPE_FIELDS (current_class) && current_class != object_type_node
7615 	  && current_class != class_type_node)
7616       {
7617 	/* If the dummy field is there, reverse the right fields and
7618 	   just layout the type for proper fields offset */
7619 	if (!DECL_NAME (TYPE_FIELDS (current_class)))
7620 	  {
7621 	    tree fields = TYPE_FIELDS (current_class);
7622 	    TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7623 	    TYPE_SIZE (current_class) = NULL_TREE;
7624 	  }
7625 	/* We don't have a dummy field, we need to layout the class,
7626            after having reversed the fields */
7627 	else
7628 	  {
7629 	    TYPE_FIELDS (current_class) =
7630 	      nreverse (TYPE_FIELDS (current_class));
7631 	    TYPE_SIZE (current_class) = NULL_TREE;
7632 	  }
7633       }
7634     }
7635   /* There are cases were gclass_list will be empty. */
7636   if (gclass_list)
7637     stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7638 }
7639 
7640 /* Layout the methods of all classes loaded in one way or another.
7641    Check methods of source parsed classes. Then reorder the
7642    fields and layout the classes or the type of all source parsed
7643    classes */
7644 
7645 void
java_layout_classes()7646 java_layout_classes ()
7647 {
7648   tree current;
7649   int save_error_count = java_error_count;
7650 
7651   /* Layout the methods of all classes seen so far */
7652   java_layout_seen_class_methods ();
7653   java_parse_abort_on_error ();
7654   all_class_list = NULL_TREE;
7655 
7656   /* Then check the methods of all parsed classes */
7657   for (current = gclass_list; current; current = TREE_CHAIN (current))
7658     if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7659       java_check_methods (TREE_VALUE (current));
7660   java_parse_abort_on_error ();
7661 
7662   for (current = gclass_list; current; current = TREE_CHAIN (current))
7663     {
7664       current_class = TREE_TYPE (TREE_VALUE (current));
7665       layout_class (current_class);
7666 
7667       /* Error reported by the caller */
7668       if (java_error_count)
7669 	return;
7670     }
7671 
7672   /* We might have reloaded classes durign the process of laying out
7673      classes for code generation. We must layout the methods of those
7674      late additions, as constructor checks might use them */
7675   java_layout_seen_class_methods ();
7676   java_parse_abort_on_error ();
7677 }
7678 
7679 /* Expand methods in the current set of classes rememebered for
7680    generation.  */
7681 
7682 static void
java_complete_expand_classes()7683 java_complete_expand_classes ()
7684 {
7685   tree current;
7686 
7687   do_not_fold = flag_emit_xref;
7688 
7689   for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7690     if (!INNER_CLASS_DECL_P (current))
7691       java_complete_expand_class (current);
7692 }
7693 
7694 /* Expand the methods found in OUTER, starting first by OUTER's inner
7695    classes, if any.  */
7696 
7697 static void
java_complete_expand_class(outer)7698 java_complete_expand_class (outer)
7699      tree outer;
7700 {
7701   tree inner_list;
7702 
7703   set_nested_class_simple_name_value (outer, 1); /* Set */
7704 
7705   /* We need to go after all inner classes and start expanding them,
7706      starting with most nested ones. We have to do that because nested
7707      classes might add functions to outer classes */
7708 
7709   for (inner_list = DECL_INNER_CLASS_LIST (outer);
7710        inner_list; inner_list = TREE_CHAIN (inner_list))
7711     java_complete_expand_class (TREE_PURPOSE (inner_list));
7712 
7713   java_complete_expand_methods (outer);
7714   set_nested_class_simple_name_value (outer, 0); /* Reset */
7715 }
7716 
7717 /* Expand methods registered in CLASS_DECL. The general idea is that
7718    we expand regular methods first. This allows us get an estimate on
7719    how outer context local alias fields are really used so we can add
7720    to the constructor just enough code to initialize them properly (it
7721    also lets us generate finit$ correctly.) Then we expand the
7722    constructors and then <clinit>.  */
7723 
7724 static void
java_complete_expand_methods(class_decl)7725 java_complete_expand_methods (class_decl)
7726      tree class_decl;
7727 {
7728   tree clinit, decl, first_decl;
7729 
7730   current_class = TREE_TYPE (class_decl);
7731 
7732   /* Initialize a new constant pool */
7733   init_outgoing_cpool ();
7734 
7735   /* Pre-expand <clinit> to figure whether we really need it or
7736      not. If we do need it, we pre-expand the static fields so they're
7737      ready to be used somewhere else. <clinit> will be fully expanded
7738      after we processed the constructors. */
7739   first_decl = TYPE_METHODS (current_class);
7740   clinit = maybe_generate_pre_expand_clinit (current_class);
7741 
7742   /* Then generate finit$ (if we need to) because constructors will
7743    try to use it.*/
7744   if (TYPE_FINIT_STMT_LIST (current_class))
7745     java_complete_expand_method (generate_finit (current_class));
7746 
7747   /* Then generate instinit$ (if we need to) because constructors will
7748      try to use it. */
7749   if (TYPE_II_STMT_LIST (current_class))
7750     java_complete_expand_method (generate_instinit (current_class));
7751 
7752   /* Now do the constructors */
7753   for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7754     {
7755       int no_body;
7756 
7757       if (!DECL_CONSTRUCTOR_P (decl))
7758 	continue;
7759 
7760       no_body = !DECL_FUNCTION_BODY (decl);
7761       /* Don't generate debug info on line zero when expanding a
7762 	 generated constructor. */
7763       if (no_body)
7764 	restore_line_number_status (1);
7765 
7766       java_complete_expand_method (decl);
7767 
7768       if (no_body)
7769 	restore_line_number_status (0);
7770     }
7771 
7772   /* First, do the ordinary methods. */
7773   for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7774     {
7775       /* Ctors aren't part of this batch. */
7776       if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7777 	continue;
7778 
7779       /* Skip abstract or native methods -- but do handle native
7780  	 methods when generating JNI stubs.  */
7781       if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7782 	{
7783 	  DECL_FUNCTION_BODY (decl) = NULL_TREE;
7784 	  continue;
7785 	}
7786 
7787       if (METHOD_NATIVE (decl))
7788  	{
7789  	  tree body;
7790 	  current_function_decl = decl;
7791 	  body = build_jni_stub (decl);
7792  	  BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7793  	}
7794 
7795       java_complete_expand_method (decl);
7796     }
7797 
7798   /* If there is indeed a <clinit>, fully expand it now */
7799   if (clinit)
7800     {
7801       /* Prevent the use of `this' inside <clinit> */
7802       ctxp->explicit_constructor_p = 1;
7803       java_complete_expand_method (clinit);
7804       ctxp->explicit_constructor_p = 0;
7805     }
7806 
7807   /* We might have generated a class$ that we now want to expand */
7808   if (TYPE_DOT_CLASS (current_class))
7809     java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7810 
7811   /* Now verify constructor circularity (stop after the first one we
7812      prove wrong.) */
7813   if (!CLASS_INTERFACE (class_decl))
7814     for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7815       if (DECL_CONSTRUCTOR_P (decl)
7816 	  && verify_constructor_circularity (decl, decl))
7817 	break;
7818 
7819   /* Save the constant pool. We'll need to restore it later. */
7820   TYPE_CPOOL (current_class) = outgoing_cpool;
7821 }
7822 
7823 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7824    safely used in some other methods/constructors.  */
7825 
7826 static tree
maybe_generate_pre_expand_clinit(class_type)7827 maybe_generate_pre_expand_clinit (class_type)
7828      tree class_type;
7829 {
7830   tree current, mdecl;
7831 
7832   if (!TYPE_CLINIT_STMT_LIST (class_type))
7833     return NULL_TREE;
7834 
7835   /* Go through all static fields and pre expand them */
7836   for (current = TYPE_FIELDS (class_type); current;
7837        current = TREE_CHAIN (current))
7838     if (FIELD_STATIC (current))
7839       build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7840 
7841   /* Then build the <clinit> method */
7842   mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7843 				    clinit_identifier_node, end_params_node);
7844   layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7845 		       mdecl, NULL_TREE);
7846   start_artificial_method_body (mdecl);
7847 
7848   /* We process the list of assignment we produced as the result of
7849      the declaration of initialized static field and add them as
7850      statement to the <clinit> method. */
7851   for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7852        current = TREE_CHAIN (current))
7853     {
7854       tree stmt = current;
7855       /* We build the assignment expression that will initialize the
7856 	 field to its value. There are strict rules on static
7857 	 initializers (8.5). FIXME */
7858       if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7859 	stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7860       java_method_add_stmt (mdecl, stmt);
7861     }
7862 
7863   end_artificial_method_body (mdecl);
7864 
7865   /* Now we want to place <clinit> as the last method (because we need
7866      it at least for interface so that it doesn't interfere with the
7867      dispatch table based lookup. */
7868   if (TREE_CHAIN (TYPE_METHODS (class_type)))
7869     {
7870       current = TREE_CHAIN (TYPE_METHODS (class_type));
7871       TYPE_METHODS (class_type) = current;
7872 
7873       while (TREE_CHAIN (current))
7874 	current = TREE_CHAIN (current);
7875 
7876       TREE_CHAIN (current) = mdecl;
7877       TREE_CHAIN (mdecl) = NULL_TREE;
7878     }
7879 
7880   return mdecl;
7881 }
7882 
7883 /* Analyzes a method body and look for something that isn't a
7884    MODIFY_EXPR with a constant value.  */
7885 
7886 static int
analyze_clinit_body(this_class,bbody)7887 analyze_clinit_body (this_class, bbody)
7888      tree this_class, bbody;
7889 {
7890   while (bbody)
7891     switch (TREE_CODE (bbody))
7892       {
7893       case BLOCK:
7894 	bbody = BLOCK_EXPR_BODY (bbody);
7895 	break;
7896 
7897       case EXPR_WITH_FILE_LOCATION:
7898 	bbody = EXPR_WFL_NODE (bbody);
7899 	break;
7900 
7901       case COMPOUND_EXPR:
7902 	if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
7903 	  return 1;
7904 	bbody = TREE_OPERAND (bbody, 1);
7905 	break;
7906 
7907       case MODIFY_EXPR:
7908 	/* If we're generating to class file and we're dealing with an
7909 	   array initialization, we return 1 to keep <clinit> */
7910 	if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
7911 	    && flag_emit_class_files)
7912 	  return 1;
7913 
7914 	/* There are a few cases where we're required to keep
7915 	   <clinit>:
7916 	   - If this is an assignment whose operand is not constant,
7917 	   - If this is an assignment to a non-initialized field,
7918 	   - If this field is not a member of the current class.
7919 	*/
7920 	return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
7921 		|| ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
7922 		|| DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
7923 
7924       default:
7925 	return 1;
7926       }
7927   return 0;
7928 }
7929 
7930 
7931 /* See whether we could get rid of <clinit>. Criteria are: all static
7932    final fields have constant initial values and the body of <clinit>
7933    is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7934 
7935 static int
maybe_yank_clinit(mdecl)7936 maybe_yank_clinit (mdecl)
7937      tree mdecl;
7938 {
7939   tree type, current;
7940   tree fbody, bbody;
7941 
7942   if (!DECL_CLINIT_P (mdecl))
7943     return 0;
7944 
7945   /* If the body isn't empty, then we keep <clinit>. Note that if
7946      we're emitting classfiles, this isn't enough not to rule it
7947      out. */
7948   fbody = DECL_FUNCTION_BODY (mdecl);
7949   bbody = BLOCK_EXPR_BODY (fbody);
7950   if (bbody && bbody != error_mark_node)
7951     bbody = BLOCK_EXPR_BODY (bbody);
7952   else
7953     return 0;
7954   if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7955     return 0;
7956 
7957   type = DECL_CONTEXT (mdecl);
7958   current = TYPE_FIELDS (type);
7959 
7960   for (current = (current ? TREE_CHAIN (current) : current);
7961        current; current = TREE_CHAIN (current))
7962     {
7963       tree f_init;
7964 
7965       /* We're not interested in non-static fields.  */
7966       if (!FIELD_STATIC (current))
7967 	continue;
7968 
7969       /* Nor in fields without initializers. */
7970       f_init = DECL_INITIAL (current);
7971       if (f_init == NULL_TREE)
7972 	continue;
7973 
7974       /* Anything that isn't String or a basic type is ruled out -- or
7975 	 if we know how to deal with it (when doing things natively) we
7976 	 should generated an empty <clinit> so that SUID are computed
7977 	 correctly. */
7978       if (! JSTRING_TYPE_P (TREE_TYPE (current))
7979 	  && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7980 	return 0;
7981 
7982       if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
7983 	return 0;
7984     }
7985 
7986   /* Now we analyze the method body and look for something that
7987      isn't a MODIFY_EXPR */
7988   if (bbody != empty_stmt_node && analyze_clinit_body (type, bbody))
7989     return 0;
7990 
7991   /* Get rid of <clinit> in the class' list of methods */
7992   if (TYPE_METHODS (type) == mdecl)
7993     TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7994   else
7995     for (current = TYPE_METHODS (type); current;
7996 	 current = TREE_CHAIN (current))
7997       if (TREE_CHAIN (current) == mdecl)
7998 	{
7999 	  TREE_CHAIN (current) = TREE_CHAIN (mdecl);
8000 	  break;
8001 	}
8002 
8003   return 1;
8004 }
8005 
8006 /* Install the argument from MDECL. Suitable to completion and
8007    expansion of mdecl's body.  */
8008 
8009 static void
start_complete_expand_method(mdecl)8010 start_complete_expand_method (mdecl)
8011      tree mdecl;
8012 {
8013   tree tem;
8014 
8015   pushlevel (1);		/* Prepare for a parameter push */
8016   tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
8017   DECL_ARGUMENTS (mdecl) = tem;
8018 
8019   for (; tem; tem = TREE_CHAIN (tem))
8020     {
8021       /* TREE_CHAIN (tem) will change after pushdecl. */
8022       tree next = TREE_CHAIN (tem);
8023       tree type = TREE_TYPE (tem);
8024       if (PROMOTE_PROTOTYPES
8025 	  && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8026 	  && INTEGRAL_TYPE_P (type))
8027 	type = integer_type_node;
8028       DECL_ARG_TYPE (tem) = type;
8029       layout_decl (tem, 0);
8030       pushdecl (tem);
8031       /* Re-install the next so that the list is kept and the loop
8032 	 advances. */
8033       TREE_CHAIN (tem) = next;
8034     }
8035   pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8036   lineno = DECL_SOURCE_LINE_FIRST (mdecl);
8037   build_result_decl (mdecl);
8038 }
8039 
8040 
8041 /* Complete and expand a method.  */
8042 
8043 static void
java_complete_expand_method(mdecl)8044 java_complete_expand_method (mdecl)
8045      tree mdecl;
8046 {
8047   tree fbody, block_body, exception_copy;
8048 
8049   current_function_decl = mdecl;
8050   /* Fix constructors before expanding them */
8051   if (DECL_CONSTRUCTOR_P (mdecl))
8052     fix_constructors (mdecl);
8053 
8054   /* Expand functions that have a body */
8055   if (!DECL_FUNCTION_BODY (mdecl))
8056     return;
8057 
8058   fbody = DECL_FUNCTION_BODY (mdecl);
8059   block_body = BLOCK_EXPR_BODY (fbody);
8060   exception_copy = NULL_TREE;
8061 
8062   current_function_decl = mdecl;
8063 
8064   if (! quiet_flag)
8065     fprintf (stderr, " [%s.",
8066 	     lang_printable_name (DECL_CONTEXT (mdecl), 0));
8067   announce_function (mdecl);
8068   if (! quiet_flag)
8069     fprintf (stderr, "]");
8070 
8071   /* Prepare the function for tree completion */
8072   start_complete_expand_method (mdecl);
8073 
8074   /* Install the current this */
8075   current_this = (!METHOD_STATIC (mdecl) ?
8076 		  BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
8077 
8078   /* Purge the `throws' list of unchecked exceptions (we save a copy
8079      of the list and re-install it later.) */
8080   exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
8081   purge_unchecked_exceptions (mdecl);
8082 
8083   /* Install exceptions thrown with `throws' */
8084   PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
8085 
8086   if (block_body != NULL_TREE)
8087     {
8088       block_body = java_complete_tree (block_body);
8089 
8090       /* Before we check initialization, attached all class initialization
8091 	 variable to the block_body */
8092       htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8093 		     attach_init_test_initialization_flags, block_body);
8094 
8095       if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
8096 	{
8097 	  check_for_initialization (block_body, mdecl);
8098 
8099 	  /* Go through all the flags marking the initialization of
8100 	     static variables and see whether they're definitively
8101 	     assigned, in which case the type is remembered as
8102 	     definitively initialized in MDECL. */
8103 	  if (STATIC_CLASS_INIT_OPT_P ())
8104 	    {
8105 	      /* Always register the context as properly initialized in
8106 		 MDECL. This used with caution helps removing extra
8107 		 initialization of self. */
8108 	      if (METHOD_STATIC (mdecl))
8109 		{
8110 		  *(htab_find_slot
8111 		    (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
8112 		     DECL_CONTEXT (mdecl), INSERT)) = DECL_CONTEXT (mdecl);
8113 		}
8114 	    }
8115 	}
8116       ctxp->explicit_constructor_p = 0;
8117     }
8118 
8119   BLOCK_EXPR_BODY (fbody) = block_body;
8120 
8121   /* If we saw a return but couldn't evaluate it properly, we'll have
8122      an error_mark_node here. */
8123   if (block_body != error_mark_node
8124       && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
8125       && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
8126       && !flag_emit_xref)
8127     missing_return_error (current_function_decl);
8128 
8129   /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
8130   maybe_yank_clinit (mdecl);
8131 
8132   /* Pop the current level, with special measures if we found errors. */
8133   if (java_error_count)
8134     pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8135   poplevel (1, 0, 1);
8136 
8137   /* Pop the exceptions and sanity check */
8138   POP_EXCEPTIONS();
8139   if (currently_caught_type_list)
8140     abort ();
8141 
8142   /* Restore the copy of the list of exceptions if emitting xrefs. */
8143   DECL_FUNCTION_THROWS (mdecl) = exception_copy;
8144 }
8145 
8146 /* For with each class for which there's code to generate. */
8147 
8148 static void
java_expand_method_bodies(class)8149 java_expand_method_bodies (class)
8150      tree class;
8151 {
8152   tree decl;
8153   for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8154     {
8155       if (!DECL_FUNCTION_BODY (decl))
8156 	continue;
8157 
8158       current_function_decl = decl;
8159 
8160       /* Save the function for inlining.  */
8161       if (flag_inline_trees)
8162 	DECL_SAVED_TREE (decl) =
8163 	  BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
8164 
8165       /* It's time to assign the variable flagging static class
8166 	 initialization based on which classes invoked static methods
8167 	 are definitely initializing. This should be flagged. */
8168       if (STATIC_CLASS_INIT_OPT_P ())
8169 	{
8170 	  tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
8171 	  for (; list != NULL_TREE;  list = TREE_CHAIN (list))
8172 	    {
8173 	      /* Executed for each statement calling a static function.
8174 		 LIST is a TREE_LIST whose PURPOSE is the called function
8175 		 and VALUE is a compound whose second operand can be patched
8176 		 with static class initialization flag assignments.  */
8177 
8178 	      tree called_method = TREE_PURPOSE (list);
8179 	      tree compound = TREE_VALUE (list);
8180 	      tree assignment_compound_list
8181 		= build_tree_list (called_method, NULL);
8182 
8183 	      /* For each class definitely initialized in
8184 		 CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8185 		 assignment to the class initialization flag. */
8186 	      htab_traverse (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8187 			     emit_test_initialization,
8188 			     assignment_compound_list);
8189 
8190 	      if (TREE_VALUE (assignment_compound_list))
8191 		TREE_OPERAND (compound, 1)
8192 		  = TREE_VALUE (assignment_compound_list);
8193 	    }
8194 	}
8195 
8196       /* Prepare the function for RTL expansion */
8197       start_complete_expand_method (decl);
8198 
8199       /* Expand function start, generate initialization flag
8200 	 assignment, and handle synchronized methods. */
8201       complete_start_java_method (decl);
8202 
8203       /* Expand the rest of the function body and terminate
8204          expansion. */
8205       source_end_java_method ();
8206     }
8207 }
8208 
8209 
8210 
8211 /* This section of the code deals with accessing enclosing context
8212    fields either directly by using the relevant access to this$<n> or
8213    by invoking an access method crafted for that purpose.  */
8214 
8215 /* Build the necessary access from an inner class to an outer
8216    class. This routine could be optimized to cache previous result
8217    (decl, current_class and returned access).  When an access method
8218    needs to be generated, it always takes the form of a read. It might
8219    be later turned into a write by calling outer_field_access_fix.  */
8220 
8221 static tree
build_outer_field_access(id,decl)8222 build_outer_field_access (id, decl)
8223      tree id, decl;
8224 {
8225   tree access = NULL_TREE;
8226   tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8227   tree decl_ctx = DECL_CONTEXT (decl);
8228 
8229   /* If the immediate enclosing context of the current class is the
8230      field decl's class or inherits from it; build the access as
8231      `this$<n>.<field>'. Note that we will break the `private' barrier
8232      if we're not emitting bytecodes. */
8233   if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8234       && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
8235     {
8236       tree thisn = build_current_thisn (current_class);
8237       access = make_qualified_primary (build_wfl_node (thisn),
8238 				       id, EXPR_WFL_LINECOL (id));
8239     }
8240   /* Otherwise, generate access methods to outer this and access the
8241      field (either using an access method or by direct access.) */
8242   else
8243     {
8244       int lc = EXPR_WFL_LINECOL (id);
8245 
8246       /* Now we chain the required number of calls to the access$0 to
8247 	 get a hold to the enclosing instance we need, and then we
8248 	 build the field access. */
8249       access = build_access_to_thisn (current_class, decl_ctx, lc);
8250 
8251       /* If the field is private and we're generating bytecode, then
8252          we generate an access method */
8253       if (FIELD_PRIVATE (decl) && flag_emit_class_files )
8254 	{
8255 	  tree name = build_outer_field_access_methods (decl);
8256 	  access = build_outer_field_access_expr (lc, decl_ctx,
8257 						  name, access, NULL_TREE);
8258 	}
8259       /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
8260 	 Once again we break the `private' access rule from a foreign
8261 	 class. */
8262       else
8263 	access = make_qualified_primary (access, id, lc);
8264     }
8265   return resolve_expression_name (access, NULL);
8266 }
8267 
8268 /* Return a nonzero value if NODE describes an outer field inner
8269    access.  */
8270 
8271 static int
outer_field_access_p(type,decl)8272 outer_field_access_p (type, decl)
8273     tree type, decl;
8274 {
8275   if (!INNER_CLASS_TYPE_P (type)
8276       || TREE_CODE (decl) != FIELD_DECL
8277       || DECL_CONTEXT (decl) == type)
8278     return 0;
8279 
8280   /* If the inner class extends the declaration context of the field
8281      we're try to acces, then this isn't an outer field access */
8282   if (inherits_from_p (type, DECL_CONTEXT (decl)))
8283     return 0;
8284 
8285   for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
8286        type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
8287     {
8288       if (type == DECL_CONTEXT (decl))
8289 	return 1;
8290 
8291       if (!DECL_CONTEXT (TYPE_NAME (type)))
8292 	{
8293 	  /* Before we give up, see whether the field is inherited from
8294 	     the enclosing context we're considering. */
8295 	  if (inherits_from_p (type, DECL_CONTEXT (decl)))
8296 	    return 1;
8297 	  break;
8298 	}
8299     }
8300 
8301   return 0;
8302 }
8303 
8304 /* Return a nonzero value if NODE represents an outer field inner
8305    access that was been already expanded. As a side effect, it returns
8306    the name of the field being accessed and the argument passed to the
8307    access function, suitable for a regeneration of the access method
8308    call if necessary. */
8309 
8310 static int
outer_field_expanded_access_p(node,name,arg_type,arg)8311 outer_field_expanded_access_p (node, name, arg_type, arg)
8312     tree node, *name, *arg_type, *arg;
8313 {
8314   int identified = 0;
8315 
8316   if (TREE_CODE (node) != CALL_EXPR)
8317     return 0;
8318 
8319   /* Well, gcj generates slightly different tree nodes when compiling
8320      to native or bytecodes. It's the case for function calls. */
8321 
8322   if (flag_emit_class_files
8323       && TREE_CODE (node) == CALL_EXPR
8324       && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8325     identified = 1;
8326   else if (!flag_emit_class_files)
8327     {
8328       node = TREE_OPERAND (node, 0);
8329 
8330       if (node && TREE_OPERAND (node, 0)
8331 	  && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8332 	{
8333 	  node = TREE_OPERAND (node, 0);
8334 	  if (TREE_OPERAND (node, 0)
8335 	      && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8336 	      && (OUTER_FIELD_ACCESS_IDENTIFIER_P
8337 		  (DECL_NAME (TREE_OPERAND (node, 0)))))
8338 	    identified = 1;
8339 	}
8340     }
8341 
8342   if (identified && name && arg_type && arg)
8343     {
8344       tree argument = TREE_OPERAND (node, 1);
8345       *name = DECL_NAME (TREE_OPERAND (node, 0));
8346       *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8347       *arg = TREE_VALUE (argument);
8348     }
8349   return identified;
8350 }
8351 
8352 /* Detect in NODE an outer field read access from an inner class and
8353    transform it into a write with RHS as an argument. This function is
8354    called from the java_complete_lhs when an assignment to a LHS can
8355    be identified. */
8356 
8357 static tree
outer_field_access_fix(wfl,node,rhs)8358 outer_field_access_fix (wfl, node, rhs)
8359     tree wfl, node, rhs;
8360 {
8361   tree name, arg_type, arg;
8362 
8363   if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
8364     {
8365       node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
8366 					    arg_type, name, arg, rhs);
8367       return java_complete_tree (node);
8368     }
8369   return NULL_TREE;
8370 }
8371 
8372 /* Construct the expression that calls an access method:
8373      <type>.access$<n>(<arg1> [, <arg2>]);
8374 
8375    ARG2 can be NULL and will be omitted in that case. It will denote a
8376    read access.  */
8377 
8378 static tree
build_outer_field_access_expr(lc,type,access_method_name,arg1,arg2)8379 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
8380     int lc;
8381     tree type, access_method_name, arg1, arg2;
8382 {
8383   tree args, cn, access;
8384 
8385   args = arg1 ? arg1 :
8386     build_wfl_node (build_current_thisn (current_class));
8387   args = build_tree_list (NULL_TREE, args);
8388 
8389   if (arg2)
8390     args = tree_cons (NULL_TREE, arg2, args);
8391 
8392   access = build_method_invocation (build_wfl_node (access_method_name), args);
8393   cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8394   return make_qualified_primary (cn, access, lc);
8395 }
8396 
8397 static tree
build_new_access_id()8398 build_new_access_id ()
8399 {
8400   static int access_n_counter = 1;
8401   char buffer [128];
8402 
8403   sprintf (buffer, "access$%d", access_n_counter++);
8404   return get_identifier (buffer);
8405 }
8406 
8407 /* Create the static access functions for the outer field DECL. We define a
8408    read:
8409      TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8410        return inst$.field;
8411      }
8412    and a write access:
8413      TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8414                                      TREE_TYPE (<field>) value$) {
8415        return inst$.field = value$;
8416      }
8417    We should have a usage flags on the DECL so we can lazily turn the ones
8418    we're using for code generation. FIXME.
8419 */
8420 
8421 static tree
build_outer_field_access_methods(decl)8422 build_outer_field_access_methods (decl)
8423     tree decl;
8424 {
8425   tree id, args, stmt, mdecl;
8426 
8427   if (FIELD_INNER_ACCESS_P (decl))
8428     return FIELD_INNER_ACCESS (decl);
8429 
8430   MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8431 
8432   /* Create the identifier and a function named after it. */
8433   id = build_new_access_id ();
8434 
8435   /* The identifier is marked as bearing the name of a generated write
8436      access function for outer field accessed from inner classes. */
8437   OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8438 
8439   /* Create the read access */
8440   args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8441   TREE_CHAIN (args) = end_params_node;
8442   stmt = make_qualified_primary (build_wfl_node (inst_id),
8443 				 build_wfl_node (DECL_NAME (decl)), 0);
8444   stmt = build_return (0, stmt);
8445   mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8446 					   TREE_TYPE (decl), id, args, stmt);
8447   DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8448 
8449   /* Create the write access method. No write access for final variable */
8450   if (!FIELD_FINAL (decl))
8451     {
8452       args = build_tree_list (inst_id,
8453 			      build_pointer_type (DECL_CONTEXT (decl)));
8454       TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8455       TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8456       stmt = make_qualified_primary (build_wfl_node (inst_id),
8457 				     build_wfl_node (DECL_NAME (decl)), 0);
8458       stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8459 						build_wfl_node (wpv_id)));
8460       mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8461 					       TREE_TYPE (decl), id,
8462 					       args, stmt);
8463     }
8464   DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8465 
8466   /* Return the access name */
8467   return FIELD_INNER_ACCESS (decl) = id;
8468 }
8469 
8470 /* Build an field access method NAME.  */
8471 
8472 static tree
build_outer_field_access_method(class,type,name,args,body)8473 build_outer_field_access_method (class, type, name, args, body)
8474     tree class, type, name, args, body;
8475 {
8476   tree saved_current_function_decl, mdecl;
8477 
8478   /* Create the method */
8479   mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8480   fix_method_argument_names (args, mdecl);
8481   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8482 
8483   /* Attach the method body. */
8484   saved_current_function_decl = current_function_decl;
8485   start_artificial_method_body (mdecl);
8486   java_method_add_stmt (mdecl, body);
8487   end_artificial_method_body (mdecl);
8488   current_function_decl = saved_current_function_decl;
8489 
8490   return mdecl;
8491 }
8492 
8493 
8494 /* This section deals with building access function necessary for
8495    certain kinds of method invocation from inner classes.  */
8496 
8497 static tree
build_outer_method_access_method(decl)8498 build_outer_method_access_method (decl)
8499     tree decl;
8500 {
8501   tree saved_current_function_decl, mdecl;
8502   tree args = NULL_TREE, call_args = NULL_TREE;
8503   tree carg, id, body, class;
8504   char buffer [80];
8505   int parm_id_count = 0;
8506 
8507   /* Test this abort with an access to a private field */
8508   if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8509     abort ();
8510 
8511   /* Check the cache first */
8512   if (DECL_FUNCTION_INNER_ACCESS (decl))
8513     return DECL_FUNCTION_INNER_ACCESS (decl);
8514 
8515   class = DECL_CONTEXT (decl);
8516 
8517   /* Obtain an access identifier and mark it */
8518   id = build_new_access_id ();
8519   OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8520 
8521   carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8522   /* Create the arguments, as much as the original */
8523   for (; carg && carg != end_params_node;
8524        carg = TREE_CHAIN (carg))
8525     {
8526       sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8527       args = chainon (args, build_tree_list (get_identifier (buffer),
8528 					     TREE_VALUE (carg)));
8529     }
8530   args = chainon (args, end_params_node);
8531 
8532   /* Create the method */
8533   mdecl = create_artificial_method (class, ACC_STATIC,
8534 				    TREE_TYPE (TREE_TYPE (decl)), id, args);
8535   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8536   /* There is a potential bug here. We should be able to use
8537      fix_method_argument_names, but then arg names get mixed up and
8538      eventually a constructor will have its this$0 altered and the
8539      outer context won't be assignment properly. The test case is
8540      stub.java FIXME */
8541   TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8542 
8543   /* Attach the method body. */
8544   saved_current_function_decl = current_function_decl;
8545   start_artificial_method_body (mdecl);
8546 
8547   /* The actual method invocation uses the same args. When invoking a
8548      static methods that way, we don't want to skip the first
8549      argument. */
8550   carg = args;
8551   if (!METHOD_STATIC (decl))
8552     carg = TREE_CHAIN (carg);
8553   for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8554     call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8555 			   call_args);
8556 
8557   body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8558 				  call_args);
8559   if (!METHOD_STATIC (decl))
8560     body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8561 				   body, 0);
8562   if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8563     body = build_return (0, body);
8564   java_method_add_stmt (mdecl,body);
8565   end_artificial_method_body (mdecl);
8566   current_function_decl = saved_current_function_decl;
8567 
8568   /* Back tag the access function so it know what it accesses */
8569   DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8570 
8571   /* Tag the current method so it knows it has an access generated */
8572   return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8573 }
8574 
8575 
8576 /* This section of the code deals with building expressions to access
8577    the enclosing instance of an inner class. The enclosing instance is
8578    kept in a generated field called this$<n>, with <n> being the
8579    inner class nesting level (starting from 0.)  */
8580 
8581 /* Build an access to a given this$<n>, always chaining access call to
8582    others. Access methods to this$<n> are build on the fly if
8583    necessary. This CAN'T be used to solely access this$<n-1> from
8584    this$<n> (which alway yield to special cases and optimization, see
8585    for example build_outer_field_access).  */
8586 
8587 static tree
build_access_to_thisn(from,to,lc)8588 build_access_to_thisn (from, to, lc)
8589      tree from, to;
8590      int lc;
8591 {
8592   tree access = NULL_TREE;
8593 
8594   while (from != to && PURE_INNER_CLASS_TYPE_P (from))
8595     {
8596       if (!access)
8597         {
8598           access = build_current_thisn (from);
8599           access = build_wfl_node (access);
8600         }
8601       else
8602 	{
8603 	  tree access0_wfl, cn;
8604 
8605 	  maybe_build_thisn_access_method (from);
8606 	  access0_wfl = build_wfl_node (access0_identifier_node);
8607 	  cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8608 	  EXPR_WFL_LINECOL (access0_wfl) = lc;
8609 	  access = build_tree_list (NULL_TREE, access);
8610 	  access = build_method_invocation (access0_wfl, access);
8611 	  access = make_qualified_primary (cn, access, lc);
8612 	}
8613 
8614       /* If FROM isn't an inner class, that's fine, we've done enough.
8615          What we're looking for can be accessed from there.  */
8616       from = DECL_CONTEXT (TYPE_NAME (from));
8617       if (!from)
8618 	break;
8619       from = TREE_TYPE (from);
8620     }
8621   return access;
8622 }
8623 
8624 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8625    is returned if nothing needs to be generated. Otherwise, the method
8626    generated and a method decl is returned.
8627 
8628    NOTE: These generated methods should be declared in a class file
8629    attribute so that they can't be referred to directly.  */
8630 
8631 static tree
maybe_build_thisn_access_method(type)8632 maybe_build_thisn_access_method (type)
8633     tree type;
8634 {
8635   tree mdecl, args, stmt, rtype;
8636   tree saved_current_function_decl;
8637 
8638   /* If TYPE is a top-level class, no access method is required.
8639      If there already is such an access method, bail out. */
8640   if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8641     return NULL_TREE;
8642 
8643   /* We generate the method. The method looks like:
8644      static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8645   */
8646   args = build_tree_list (inst_id, build_pointer_type (type));
8647   TREE_CHAIN (args) = end_params_node;
8648   rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8649   mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8650 				    access0_identifier_node, args);
8651   fix_method_argument_names (args, mdecl);
8652   layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8653   stmt = build_current_thisn (type);
8654   stmt = make_qualified_primary (build_wfl_node (inst_id),
8655 				 build_wfl_node (stmt), 0);
8656   stmt = build_return (0, stmt);
8657 
8658   saved_current_function_decl = current_function_decl;
8659   start_artificial_method_body (mdecl);
8660   java_method_add_stmt (mdecl, stmt);
8661   end_artificial_method_body (mdecl);
8662   current_function_decl = saved_current_function_decl;
8663 
8664   CLASS_ACCESS0_GENERATED_P (type) = 1;
8665 
8666   return mdecl;
8667 }
8668 
8669 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8670    the first level of innerclassing. this$1 for the next one, etc...
8671    This function can be invoked with TYPE to NULL, available and then
8672    has to count the parser context.  */
8673 
8674 static GTY(()) tree saved_thisn;
8675 static GTY(()) tree saved_type;
8676 
8677 static tree
build_current_thisn(type)8678 build_current_thisn (type)
8679     tree type;
8680 {
8681   static int saved_i = -1;
8682   static int saved_type_i = 0;
8683   tree decl;
8684   char buffer [24];
8685   int i = 0;
8686 
8687   if (type)
8688     {
8689       if (type == saved_type)
8690 	i = saved_type_i;
8691       else
8692 	{
8693 	  for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8694 	       decl; decl = DECL_CONTEXT (decl), i++)
8695 	    ;
8696 
8697 	  saved_type = type;
8698 	  saved_type_i = i;
8699 	}
8700     }
8701   else
8702     i = list_length (GET_CPC_LIST ())-2;
8703 
8704   if (i == saved_i)
8705     return saved_thisn;
8706 
8707   sprintf (buffer, "this$%d", i);
8708   saved_i = i;
8709   saved_thisn = get_identifier (buffer);
8710   return saved_thisn;
8711 }
8712 
8713 /* Return the assignement to the hidden enclosing context `this$<n>'
8714    by the second incoming parameter to the innerclass constructor. The
8715    form used is `this.this$<n> = this$<n>;'.  */
8716 
8717 static tree
build_thisn_assign()8718 build_thisn_assign ()
8719 {
8720   if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8721     {
8722       tree thisn = build_current_thisn (current_class);
8723       tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8724 					 build_wfl_node (thisn), 0);
8725       tree rhs = build_wfl_node (thisn);
8726       EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8727       return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8728     }
8729   return NULL_TREE;
8730 }
8731 
8732 
8733 /* Building the synthetic `class$' used to implement the `.class' 1.1
8734    extension for non primitive types. This method looks like:
8735 
8736     static Class class$(String type) throws NoClassDefFoundError
8737     {
8738       try {return (java.lang.Class.forName (String));}
8739       catch (ClassNotFoundException e) {
8740         throw new NoClassDefFoundError(e.getMessage());}
8741     } */
8742 
8743 static GTY(()) tree get_message_wfl;
8744 static GTY(()) tree type_parm_wfl;
8745 
8746 static tree
build_dot_class_method(class)8747 build_dot_class_method (class)
8748      tree class;
8749 {
8750 #define BWF(S) build_wfl_node (get_identifier ((S)))
8751 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8752   tree args, tmp, saved_current_function_decl, mdecl;
8753   tree stmt, throw_stmt;
8754 
8755   if (!get_message_wfl)
8756     {
8757       get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8758       type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8759     }
8760 
8761   /* Build the arguments */
8762   args = build_tree_list (get_identifier ("type$"),
8763 			  build_pointer_type (string_type_node));
8764   TREE_CHAIN (args) = end_params_node;
8765 
8766   /* Build the qualified name java.lang.Class.forName */
8767   tmp = MQN (MQN (MQN (BWF ("java"),
8768 		       BWF ("lang")), BWF ("Class")), BWF ("forName"));
8769   load_class (class_not_found_type_node, 1);
8770   load_class (no_class_def_found_type_node, 1);
8771 
8772   /* Create the "class$" function */
8773   mdecl = create_artificial_method (class, ACC_STATIC,
8774 				    build_pointer_type (class_type_node),
8775 				    classdollar_identifier_node, args);
8776   DECL_FUNCTION_THROWS (mdecl) =
8777     build_tree_list (NULL_TREE, no_class_def_found_type_node);
8778 
8779   /* We start by building the try block. We need to build:
8780        return (java.lang.Class.forName (type)); */
8781   stmt = build_method_invocation (tmp,
8782 				  build_tree_list (NULL_TREE, type_parm_wfl));
8783   stmt = build_return (0, stmt);
8784 
8785   /* Now onto the catch block. We start by building the expression
8786      throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8787   throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8788 				    get_message_wfl, 0);
8789   throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8790 
8791   /* Build new NoClassDefFoundError (_.getMessage) */
8792   throw_stmt = build_new_invocation
8793     (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8794      build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8795 
8796   /* Build the throw, (it's too early to use BUILD_THROW) */
8797   throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8798 
8799   /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8800   stmt = encapsulate_with_try_catch (0, class_not_found_type_node,
8801 				     stmt, throw_stmt);
8802 
8803   fix_method_argument_names (args, mdecl);
8804   layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8805   saved_current_function_decl = current_function_decl;
8806   start_artificial_method_body (mdecl);
8807   java_method_add_stmt (mdecl, stmt);
8808   end_artificial_method_body (mdecl);
8809   current_function_decl = saved_current_function_decl;
8810   TYPE_DOT_CLASS (class) = mdecl;
8811 
8812   return mdecl;
8813 }
8814 
8815 static tree
build_dot_class_method_invocation(type)8816 build_dot_class_method_invocation (type)
8817      tree type;
8818 {
8819   tree sig_id, s;
8820 
8821   if (TYPE_ARRAY_P (type))
8822     sig_id = build_java_signature (type);
8823   else
8824     sig_id = DECL_NAME (TYPE_NAME (type));
8825 
8826   /* Ensure that the proper name separator is used */
8827   sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
8828 			       IDENTIFIER_LENGTH (sig_id));
8829 
8830   s = build_string (IDENTIFIER_LENGTH (sig_id),
8831 		    IDENTIFIER_POINTER (sig_id));
8832   return build_method_invocation (build_wfl_node (classdollar_identifier_node),
8833 				  build_tree_list (NULL_TREE, s));
8834 }
8835 
8836 /* This section of the code deals with constructor.  */
8837 
8838 /* Craft a body for default constructor. Patch existing constructor
8839    bodies with call to super() and field initialization statements if
8840    necessary.  */
8841 
8842 static void
fix_constructors(mdecl)8843 fix_constructors (mdecl)
8844      tree mdecl;
8845 {
8846   tree iii;			/* Instance Initializer Invocation */
8847   tree body = DECL_FUNCTION_BODY (mdecl);
8848   tree thisn_assign, compound = NULL_TREE;
8849   tree class_type = DECL_CONTEXT (mdecl);
8850 
8851   if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
8852     return;
8853   DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
8854 
8855   if (!body)
8856     {
8857       /* It is an error for the compiler to generate a default
8858 	 constructor if the superclass doesn't have a constructor that
8859 	 takes no argument, or the same args for an anonymous class */
8860       if (verify_constructor_super (mdecl))
8861 	{
8862 	  tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8863 	  tree save = DECL_NAME (mdecl);
8864 	  const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8865 	  DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8866 	  parse_error_context
8867 	    (lookup_cl (TYPE_NAME (class_type)),
8868 	     "No constructor matching `%s' found in class `%s'",
8869 	     lang_printable_name (mdecl, 0), n);
8870 	  DECL_NAME (mdecl) = save;
8871 	}
8872 
8873       /* The constructor body must be crafted by hand. It's the
8874 	 constructor we defined when we realize we didn't have the
8875 	 CLASSNAME() constructor */
8876       start_artificial_method_body (mdecl);
8877 
8878       /* Insert an assignment to the this$<n> hidden field, if
8879          necessary */
8880       if ((thisn_assign = build_thisn_assign ()))
8881 	java_method_add_stmt (mdecl, thisn_assign);
8882 
8883       /* We don't generate a super constructor invocation if we're
8884 	 compiling java.lang.Object. build_super_invocation takes care
8885 	 of that. */
8886       java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8887 
8888       /* FIXME */
8889       if ((iii = build_instinit_invocation (class_type)))
8890 	java_method_add_stmt (mdecl, iii);
8891 
8892       end_artificial_method_body (mdecl);
8893     }
8894   /* Search for an explicit constructor invocation */
8895   else
8896     {
8897       int found = 0;
8898       int invokes_this = 0;
8899       tree found_call = NULL_TREE;
8900       tree main_block = BLOCK_EXPR_BODY (body);
8901 
8902       while (body)
8903 	switch (TREE_CODE (body))
8904 	  {
8905 	  case CALL_EXPR:
8906 	    found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8907 	    if (CALL_THIS_CONSTRUCTOR_P (body))
8908 	      invokes_this = 1;
8909 	    body = NULL_TREE;
8910 	    break;
8911 	  case COMPOUND_EXPR:
8912 	  case EXPR_WITH_FILE_LOCATION:
8913 	    found_call = body;
8914 	    body = TREE_OPERAND (body, 0);
8915 	    break;
8916 	  case BLOCK:
8917 	    found_call = body;
8918 	    body = BLOCK_EXPR_BODY (body);
8919 	    break;
8920 	  default:
8921 	    found = 0;
8922 	    body = NULL_TREE;
8923 	  }
8924 
8925       /* Generate the assignment to this$<n>, if necessary */
8926       if ((thisn_assign = build_thisn_assign ()))
8927         compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8928 
8929       /* The constructor is missing an invocation of super() */
8930       if (!found)
8931 	compound = add_stmt_to_compound (compound, NULL_TREE,
8932                                          build_super_invocation (mdecl));
8933       /* Explicit super() invokation should take place before the
8934          instance initializer blocks. */
8935       else
8936 	{
8937 	  compound = add_stmt_to_compound (compound, NULL_TREE,
8938 					   TREE_OPERAND (found_call, 0));
8939 	  TREE_OPERAND (found_call, 0) = empty_stmt_node;
8940 	}
8941 
8942       DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
8943 
8944       /* Insert the instance initializer block right after. */
8945       if (!invokes_this && (iii = build_instinit_invocation (class_type)))
8946 	compound = add_stmt_to_compound (compound, NULL_TREE, iii);
8947 
8948       /* Fix the constructor main block if we're adding extra stmts */
8949       if (compound)
8950 	{
8951 	  compound = add_stmt_to_compound (compound, NULL_TREE,
8952 					   BLOCK_EXPR_BODY (main_block));
8953 	  BLOCK_EXPR_BODY (main_block) = compound;
8954 	}
8955     }
8956 }
8957 
8958 /* Browse constructors in the super class, searching for a constructor
8959    that doesn't take any argument. Return 0 if one is found, 1
8960    otherwise.  If the current class is an anonymous inner class, look
8961    for something that has the same signature. */
8962 
8963 static int
verify_constructor_super(mdecl)8964 verify_constructor_super (mdecl)
8965      tree mdecl;
8966 {
8967   tree class = CLASSTYPE_SUPER (current_class);
8968   int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8969   tree sdecl;
8970 
8971   if (!class)
8972     return 0;
8973 
8974   if (ANONYMOUS_CLASS_P (current_class))
8975     {
8976       tree mdecl_arg_type;
8977       SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8978       for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8979 	if (DECL_CONSTRUCTOR_P (sdecl))
8980 	  {
8981 	    tree m_arg_type;
8982 	    tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8983 	    if (super_inner)
8984 	      arg_type = TREE_CHAIN (arg_type);
8985 	    for (m_arg_type = mdecl_arg_type;
8986 		 (arg_type != end_params_node
8987 		  && m_arg_type != end_params_node);
8988 		 arg_type = TREE_CHAIN (arg_type),
8989 		   m_arg_type = TREE_CHAIN (m_arg_type))
8990 	      if (!valid_method_invocation_conversion_p
8991 		     (TREE_VALUE (arg_type),
8992 		      TREE_VALUE (m_arg_type)))
8993 		break;
8994 
8995 	    if (arg_type == end_params_node && m_arg_type == end_params_node)
8996 	      return 0;
8997 	  }
8998     }
8999   else
9000     {
9001       for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
9002 	{
9003 	  tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
9004 	  if (super_inner)
9005 	    arg = TREE_CHAIN (arg);
9006 	  if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
9007 	    return 0;
9008 	}
9009     }
9010   return 1;
9011 }
9012 
9013 /* Generate code for all context remembered for code generation.  */
9014 
9015 static GTY(()) tree reversed_class_list;
9016 void
java_expand_classes()9017 java_expand_classes ()
9018 {
9019   int save_error_count = 0;
9020   static struct parser_ctxt *cur_ctxp = NULL;
9021 
9022   java_parse_abort_on_error ();
9023   if (!(ctxp = ctxp_for_generation))
9024     return;
9025   java_layout_classes ();
9026   java_parse_abort_on_error ();
9027 
9028   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9029     {
9030       ctxp = cur_ctxp;
9031       input_filename = ctxp->filename;
9032       lang_init_source (2);	       /* Error msgs have method prototypes */
9033       java_complete_expand_classes (); /* Complete and expand classes */
9034       java_parse_abort_on_error ();
9035     }
9036   input_filename = main_input_filename;
9037 
9038 
9039   /* Find anonymous classes and expand their constructor. This extra pass is
9040      neccessary because the constructor itself is only generated when the
9041      method in which it is defined is expanded. */
9042   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9043     {
9044       tree current;
9045       ctxp = cur_ctxp;
9046       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9047 	{
9048 	  current_class = TREE_TYPE (current);
9049 	  if (ANONYMOUS_CLASS_P (current_class))
9050 	    {
9051 	      tree d;
9052 	      for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9053 		{
9054 		  if (DECL_CONSTRUCTOR_P (d))
9055 		    {
9056 		      restore_line_number_status (1);
9057 		      java_complete_expand_method (d);
9058 		      restore_line_number_status (0);
9059 		      break;	/* There is only one constructor. */
9060 		    }
9061 		}
9062 	    }
9063 	}
9064     }
9065 
9066   /* Expanding the constructors of anonymous classes generates access
9067      methods.  Scan all the methods looking for null DECL_RESULTs --
9068      this will be the case if a method hasn't been expanded.  */
9069   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9070     {
9071       tree current;
9072       ctxp = cur_ctxp;
9073       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9074 	{
9075 	  tree d;
9076 	  current_class = TREE_TYPE (current);
9077 	  for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9078 	    {
9079 	      if (DECL_RESULT (d) == NULL_TREE)
9080 		{
9081 		  restore_line_number_status (1);
9082 		  java_complete_expand_method (d);
9083 		  restore_line_number_status (0);
9084 		}
9085 	    }
9086 	}
9087     }
9088 
9089   /* ???  Instead of all this we could iterate around the list of
9090      classes until there were no more un-expanded methods.  It would
9091      take a little longer -- one pass over the whole list of methods
9092      -- but it would be simpler.  Like this:  */
9093 #if 0
9094     {
9095       int something_changed;
9096 
9097       do
9098 	{
9099 	  something_changed = 0;
9100 	  for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9101 	    {
9102 	      tree current;
9103 	      ctxp = cur_ctxp;
9104 	      for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9105 		{
9106 		  tree d;
9107 		  current_class = TREE_TYPE (current);
9108 		  for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9109 		    {
9110 		      if (DECL_RESULT (d) == NULL_TREE)
9111 			{
9112 			  something_changed = 1;
9113 			  restore_line_number_status (1);
9114 			  java_complete_expand_method (d);
9115 			  restore_line_number_status (0);
9116 			}
9117 		    }
9118 		}
9119 	    }
9120 	}
9121       while (something_changed);
9122     }
9123 #endif
9124 
9125   /* If we've found error at that stage, don't try to generate
9126      anything, unless we're emitting xrefs or checking the syntax only
9127      (but not using -fsyntax-only for the purpose of generating
9128      bytecode. */
9129   if (java_error_count && !flag_emit_xref
9130       && (!flag_syntax_only && !flag_emit_class_files))
9131     return;
9132 
9133   /* Now things are stable, go for generation of the class data. */
9134 
9135   /* We pessimistically marked all fields external until we knew
9136      what set of classes we were planning to compile.  Now mark
9137      those that will be generated locally as not external.  */
9138   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9139     {
9140       tree current;
9141       ctxp = cur_ctxp;
9142       for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9143 	{
9144 	  tree class = TREE_TYPE (current);
9145 	  tree field;
9146 	  for (field = TYPE_FIELDS (class); field ; field = TREE_CHAIN (field))
9147 	    if (FIELD_STATIC (field))
9148 	      DECL_EXTERNAL (field) = 0;
9149 	}
9150     }
9151 
9152   /* Compile the classes.  */
9153   for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9154     {
9155       tree current;
9156       reversed_class_list = NULL;
9157 
9158       ctxp = cur_ctxp;
9159 
9160       /* We write out the classes in reverse order.  This ensures that
9161 	 inner classes are written before their containing classes,
9162 	 which is important for parallel builds.  Otherwise, the
9163 	 class file for the outer class may be found, but the class
9164 	 file for the inner class may not be present.  In that
9165 	 situation, the compiler cannot fall back to the original
9166 	 source, having already read the outer class, so we must
9167 	 prevent that situation.  */
9168       for (current = ctxp->class_list;
9169 	   current;
9170 	   current = TREE_CHAIN (current))
9171 	reversed_class_list
9172 	  = tree_cons (NULL_TREE, current, reversed_class_list);
9173 
9174       for (current = reversed_class_list;
9175 	   current;
9176 	   current = TREE_CHAIN (current))
9177 	{
9178 	  current_class = TREE_TYPE (TREE_VALUE (current));
9179 	  outgoing_cpool = TYPE_CPOOL (current_class);
9180 	  if (flag_emit_class_files)
9181 	    write_classfile (current_class);
9182 	  if (flag_emit_xref)
9183 	    expand_xref (current_class);
9184 	  else if (! flag_syntax_only)
9185 	    {
9186 	      java_expand_method_bodies (current_class);
9187 	      finish_class ();
9188 	    }
9189 	}
9190     }
9191 }
9192 
9193 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9194    a tree list node containing RIGHT. Fore coming RIGHTs will be
9195    chained to this hook. LOCATION contains the location of the
9196    separating `.' operator.  */
9197 
9198 static tree
make_qualified_primary(primary,right,location)9199 make_qualified_primary (primary, right, location)
9200      tree primary, right;
9201      int location;
9202 {
9203   tree wfl;
9204 
9205   if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9206     wfl = build_wfl_wrap (primary, location);
9207   else
9208     {
9209       wfl = primary;
9210       /* If wfl wasn't qualified, we build a first anchor */
9211       if (!EXPR_WFL_QUALIFICATION (wfl))
9212 	EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9213     }
9214 
9215   /* And chain them */
9216   EXPR_WFL_LINECOL (right) = location;
9217   chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9218   PRIMARY_P (wfl) =  1;
9219   return wfl;
9220 }
9221 
9222 /* Simple merge of two name separated by a `.' */
9223 
9224 static tree
merge_qualified_name(left,right)9225 merge_qualified_name (left, right)
9226      tree left, right;
9227 {
9228   tree node;
9229   if (!left && !right)
9230     return NULL_TREE;
9231 
9232   if (!left)
9233     return right;
9234 
9235   if (!right)
9236     return left;
9237 
9238   obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9239 		IDENTIFIER_LENGTH (left));
9240   obstack_1grow (&temporary_obstack, '.');
9241   obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9242 		 IDENTIFIER_LENGTH (right));
9243   node =  get_identifier (obstack_base (&temporary_obstack));
9244   obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9245   QUALIFIED_P (node) = 1;
9246   return node;
9247 }
9248 
9249 /* Merge the two parts of a qualified name into LEFT.  Set the
9250    location information of the resulting node to LOCATION, usually
9251    inherited from the location information of the `.' operator. */
9252 
9253 static tree
make_qualified_name(left,right,location)9254 make_qualified_name (left, right, location)
9255      tree left, right;
9256      int location;
9257 {
9258 #ifdef USE_COMPONENT_REF
9259   tree node = build (COMPONENT_REF, NULL_TREE, left, right);
9260   EXPR_WFL_LINECOL (node) = location;
9261   return node;
9262 #else
9263   tree left_id = EXPR_WFL_NODE (left);
9264   tree right_id = EXPR_WFL_NODE (right);
9265   tree wfl, merge;
9266 
9267   merge = merge_qualified_name (left_id, right_id);
9268 
9269   /* Left wasn't qualified and is now qualified */
9270   if (!QUALIFIED_P (left_id))
9271     {
9272       tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9273       EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9274       EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9275     }
9276 
9277   wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9278   EXPR_WFL_LINECOL (wfl) = location;
9279   chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9280 
9281   EXPR_WFL_NODE (left) = merge;
9282   return left;
9283 #endif
9284 }
9285 
9286 /* Extract the last identifier component of the qualified in WFL. The
9287    last identifier is removed from the linked list */
9288 
9289 static tree
cut_identifier_in_qualified(wfl)9290 cut_identifier_in_qualified (wfl)
9291      tree wfl;
9292 {
9293   tree q;
9294   tree previous = NULL_TREE;
9295   for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9296     if (!TREE_CHAIN (q))
9297       {
9298 	if (!previous)
9299 	  /* Operating on a non qualified qualified WFL.  */
9300 	  abort ();
9301 
9302 	TREE_CHAIN (previous) = NULL_TREE;
9303 	return TREE_PURPOSE (q);
9304       }
9305 }
9306 
9307 /* Resolve the expression name NAME. Return its decl.  */
9308 
9309 static tree
resolve_expression_name(id,orig)9310 resolve_expression_name (id, orig)
9311      tree id;
9312      tree *orig;
9313 {
9314   tree name = EXPR_WFL_NODE (id);
9315   tree decl;
9316 
9317   /* 6.5.5.1: Simple expression names */
9318   if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9319     {
9320       /* 15.13.1: NAME can appear within the scope of a local variable
9321          declaration */
9322       if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9323         return decl;
9324 
9325       /* 15.13.1: NAME can appear within a class declaration */
9326       else
9327         {
9328 	  decl = lookup_field_wrapper (current_class, name);
9329 	  if (decl)
9330 	    {
9331 	      tree access = NULL_TREE;
9332 	      int fs = FIELD_STATIC (decl);
9333 
9334 	      /* If we're accessing an outer scope local alias, make
9335 		 sure we change the name of the field we're going to
9336 		 build access to. */
9337 	      if (FIELD_LOCAL_ALIAS_USED (decl))
9338 		name = DECL_NAME (decl);
9339 
9340 	      /* Instance variable (8.3.1.1) can't appear within
9341 		 static method, static initializer or initializer for
9342 		 a static variable. */
9343 	      if (!fs && METHOD_STATIC (current_function_decl))
9344 	        {
9345 		  static_ref_err (id, name, current_class);
9346 		  return error_mark_node;
9347 		}
9348 	      /* Instance variables can't appear as an argument of
9349 		 an explicit constructor invocation */
9350 	      if (!fs && ctxp->explicit_constructor_p
9351 		  && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9352 		{
9353 		  parse_error_context
9354 		    (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9355 		  return error_mark_node;
9356 		}
9357 
9358 	      /* If we're processing an inner class and we're trying
9359 		 to access a field belonging to an outer class, build
9360 		 the access to the field */
9361 	      if (!fs && outer_field_access_p (current_class, decl))
9362 		{
9363 		  if (CLASS_STATIC (TYPE_NAME (current_class)))
9364 		    {
9365 		      static_ref_err (id, DECL_NAME (decl), current_class);
9366 		      return error_mark_node;
9367 		    }
9368 		  access = build_outer_field_access (id, decl);
9369 		  if (orig)
9370 		    *orig = access;
9371 		  return access;
9372 		}
9373 
9374 	      /* Otherwise build what it takes to access the field */
9375 	      access = build_field_ref ((fs ? NULL_TREE : current_this),
9376 					DECL_CONTEXT (decl), name);
9377 	      if (fs)
9378 		access = maybe_build_class_init_for_field (decl, access);
9379 	      /* We may be asked to save the real field access node */
9380 	      if (orig)
9381 		*orig = access;
9382 	      /* And we return what we got */
9383 	      return access;
9384 	    }
9385 	  /* Fall down to error report on undefined variable */
9386 	}
9387     }
9388   /* 6.5.5.2 Qualified Expression Names */
9389   else
9390     {
9391       if (orig)
9392 	*orig = NULL_TREE;
9393       qualify_ambiguous_name (id);
9394       /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9395       /* 15.10.2: Accessing Superclass Members using super */
9396       return resolve_field_access (id, orig, NULL);
9397     }
9398 
9399   /* We've got an error here */
9400   if (INNER_CLASS_TYPE_P (current_class))
9401     parse_error_context (id,
9402 			 "Local variable `%s' can't be accessed from within the inner class `%s' unless it is declared final",
9403 			 IDENTIFIER_POINTER (name),
9404 			 IDENTIFIER_POINTER (DECL_NAME
9405 					     (TYPE_NAME (current_class))));
9406   else
9407     parse_error_context (id, "Undefined variable `%s'",
9408 			 IDENTIFIER_POINTER (name));
9409 
9410   return error_mark_node;
9411 }
9412 
9413 static void
static_ref_err(wfl,field_id,class_type)9414 static_ref_err (wfl, field_id, class_type)
9415     tree wfl, field_id, class_type;
9416 {
9417   parse_error_context
9418     (wfl,
9419      "Can't make a static reference to nonstatic variable `%s' in class `%s'",
9420      IDENTIFIER_POINTER (field_id),
9421      IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9422 }
9423 
9424 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9425    We return something suitable to generate the field access. We also
9426    return the field decl in FIELD_DECL and its type in FIELD_TYPE.  If
9427    recipient's address can be null. */
9428 
9429 static tree
resolve_field_access(qual_wfl,field_decl,field_type)9430 resolve_field_access (qual_wfl, field_decl, field_type)
9431      tree qual_wfl;
9432      tree *field_decl, *field_type;
9433 {
9434   int is_static = 0;
9435   tree field_ref;
9436   tree decl, where_found, type_found;
9437 
9438   if (resolve_qualified_expression_name (qual_wfl, &decl,
9439 					 &where_found, &type_found))
9440     return error_mark_node;
9441 
9442   /* Resolve the LENGTH field of an array here */
9443   if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9444       && type_found && TYPE_ARRAY_P (type_found)
9445       && ! flag_emit_class_files && ! flag_emit_xref)
9446     {
9447       tree length = build_java_array_length_access (where_found);
9448       field_ref = length;
9449 
9450       /* In case we're dealing with a static array, we need to
9451 	 initialize its class before the array length can be fetched.
9452 	 It's also a good time to create a DECL_RTL for the field if
9453 	 none already exists, otherwise if the field was declared in a
9454 	 class found in an external file and hasn't been (and won't
9455 	 be) accessed for its value, none will be created. */
9456       if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9457 	{
9458 	  build_static_field_ref (where_found);
9459 	  field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9460 	}
9461     }
9462   /* We might have been trying to resolve field.method(). In which
9463      case, the resolution is over and decl is the answer */
9464   else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9465     field_ref = decl;
9466   else if (JDECL_P (decl))
9467     {
9468       if (!type_found)
9469 	type_found = DECL_CONTEXT (decl);
9470       is_static = FIELD_STATIC (decl);
9471       field_ref = build_field_ref ((is_static && !flag_emit_xref?
9472 				    NULL_TREE : where_found),
9473 				   type_found, DECL_NAME (decl));
9474       if (field_ref == error_mark_node)
9475 	return error_mark_node;
9476       if (is_static)
9477 	field_ref = maybe_build_class_init_for_field (decl, field_ref);
9478 
9479       /* If we're looking at a static field, we may need to generate a
9480 	 class initialization for it.  This can happen when the access
9481 	 looks like `field.ref', where `field' is a static field in an
9482 	 interface we implement.  */
9483       if (!flag_emit_class_files
9484 	  && !flag_emit_xref
9485 	  && TREE_CODE (where_found) == VAR_DECL
9486 	  && FIELD_STATIC (where_found))
9487 	{
9488 	  build_static_field_ref (where_found);
9489 	  field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9490 	}
9491     }
9492   else
9493     field_ref = decl;
9494 
9495   if (field_decl)
9496     *field_decl = decl;
9497   if (field_type)
9498     *field_type = (QUAL_DECL_TYPE (decl) ?
9499 		   QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9500   return field_ref;
9501 }
9502 
9503 /* If NODE is an access to f static field, strip out the class
9504    initialization part and return the field decl, otherwise, return
9505    NODE. */
9506 
9507 static tree
strip_out_static_field_access_decl(node)9508 strip_out_static_field_access_decl (node)
9509     tree node;
9510 {
9511   if (TREE_CODE (node) == COMPOUND_EXPR)
9512     {
9513       tree op1 = TREE_OPERAND (node, 1);
9514       if (TREE_CODE (op1) == COMPOUND_EXPR)
9515 	 {
9516 	   tree call = TREE_OPERAND (op1, 0);
9517 	   if (TREE_CODE (call) == CALL_EXPR
9518 	       && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9519 	       && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9520 	       == soft_initclass_node)
9521 	     return TREE_OPERAND (op1, 1);
9522 	 }
9523       else if (JDECL_P (op1))
9524 	return op1;
9525     }
9526   return node;
9527 }
9528 
9529 /* 6.5.5.2: Qualified Expression Names */
9530 
9531 static int
resolve_qualified_expression_name(wfl,found_decl,where_found,type_found)9532 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9533      tree wfl;
9534      tree *found_decl, *type_found, *where_found;
9535 {
9536   int from_type = 0;		/* Field search initiated from a type */
9537   int from_super = 0, from_cast = 0, from_qualified_this = 0;
9538   int previous_call_static = 0;
9539   int is_static;
9540   tree decl = NULL_TREE, type = NULL_TREE, q;
9541   /* For certain for of inner class instantiation */
9542   tree saved_current, saved_this;
9543 #define RESTORE_THIS_AND_CURRENT_CLASS 				\
9544   { current_class = saved_current; current_this = saved_this;}
9545 
9546   *type_found = *where_found = NULL_TREE;
9547 
9548   for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9549     {
9550       tree qual_wfl = QUAL_WFL (q);
9551       tree ret_decl;		/* for EH checking */
9552       int location;		/* for EH checking */
9553 
9554       /* 15.10.1 Field Access Using a Primary */
9555       switch (TREE_CODE (qual_wfl))
9556 	{
9557 	case CALL_EXPR:
9558 	case NEW_CLASS_EXPR:
9559 	  /* If the access to the function call is a non static field,
9560 	     build the code to access it. */
9561 	  if (JDECL_P (decl) && !FIELD_STATIC (decl))
9562 	    {
9563 	      decl = maybe_access_field (decl, *where_found,
9564 					 DECL_CONTEXT (decl));
9565 	      if (decl == error_mark_node)
9566 		return 1;
9567 	    }
9568 
9569 	  /* And code for the function call */
9570 	  if (complete_function_arguments (qual_wfl))
9571 	    return 1;
9572 
9573 	  /* We might have to setup a new current class and a new this
9574 	     for the search of an inner class, relative to the type of
9575 	     a expression resolved as `decl'. The current values are
9576 	     saved and restored shortly after */
9577 	  saved_current = current_class;
9578 	  saved_this = current_this;
9579 	  if (decl
9580 	      && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9581 		  || from_qualified_this))
9582 	    {
9583 	      /* If we still have `from_qualified_this', we have the form
9584 		 <T>.this.f() and we need to build <T>.this */
9585 	      if (from_qualified_this)
9586 		{
9587 		  decl = build_access_to_thisn (current_class, type, 0);
9588 		  decl = java_complete_tree (decl);
9589 		  type = TREE_TYPE (TREE_TYPE (decl));
9590 		}
9591 	      current_class = type;
9592 	      current_this = decl;
9593 	      from_qualified_this = 0;
9594 	    }
9595 
9596 	  if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9597 	    CALL_USING_SUPER (qual_wfl) = 1;
9598 	  location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9599 		      EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9600 	  *where_found = patch_method_invocation (qual_wfl, decl, type,
9601 						  from_super,
9602 						  &is_static, &ret_decl);
9603 	  from_super = 0;
9604 	  if (*where_found == error_mark_node)
9605 	    {
9606 	      RESTORE_THIS_AND_CURRENT_CLASS;
9607 	      return 1;
9608 	    }
9609 	  *type_found = type = QUAL_DECL_TYPE (*where_found);
9610 
9611 	  *where_found = force_evaluation_order (*where_found);
9612 
9613 	  /* If we're creating an inner class instance, check for that
9614 	     an enclosing instance is in scope */
9615 	  if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9616 	      && INNER_ENCLOSING_SCOPE_CHECK (type))
9617 	    {
9618 	      parse_error_context
9619 		(qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9620 		 lang_printable_name (type, 0),
9621 		 (!current_this ? "" :
9622 		  "; an explicit one must be provided when creating this inner class"));
9623 	      RESTORE_THIS_AND_CURRENT_CLASS;
9624 	      return 1;
9625 	    }
9626 
9627 	  /* In case we had to change then to resolve a inner class
9628 	     instantiation using a primary qualified by a `new' */
9629 	  RESTORE_THIS_AND_CURRENT_CLASS;
9630 
9631 	  /* EH check. No check on access$<n> functions */
9632 	  if (location
9633 	      && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9634 	            (DECL_NAME (current_function_decl)))
9635 	    check_thrown_exceptions (location, ret_decl);
9636 
9637 	  /* If the previous call was static and this one is too,
9638 	     build a compound expression to hold the two (because in
9639 	     that case, previous function calls aren't transported as
9640 	     forcoming function's argument. */
9641 	  if (previous_call_static && is_static)
9642 	    {
9643 	      decl = build (COMPOUND_EXPR, TREE_TYPE (*where_found),
9644 			    decl, *where_found);
9645 	      TREE_SIDE_EFFECTS (decl) = 1;
9646 	    }
9647 	  else
9648 	    {
9649 	      previous_call_static = is_static;
9650 	      decl = *where_found;
9651 	    }
9652 	  from_type = 0;
9653 	  continue;
9654 
9655 	case NEW_ARRAY_EXPR:
9656 	case NEW_ANONYMOUS_ARRAY_EXPR:
9657 	  *where_found = decl = java_complete_tree (qual_wfl);
9658 	  if (decl == error_mark_node)
9659 	    return 1;
9660 	  *type_found = type = QUAL_DECL_TYPE (decl);
9661 	  continue;
9662 
9663 	case CONVERT_EXPR:
9664 	  *where_found = decl = java_complete_tree (qual_wfl);
9665 	  if (decl == error_mark_node)
9666 	    return 1;
9667 	  *type_found = type = QUAL_DECL_TYPE (decl);
9668 	  from_cast = 1;
9669 	  continue;
9670 
9671 	case CONDITIONAL_EXPR:
9672 	case STRING_CST:
9673 	case MODIFY_EXPR:
9674 	  *where_found = decl = java_complete_tree (qual_wfl);
9675 	  if (decl == error_mark_node)
9676 	    return 1;
9677 	  *type_found = type = QUAL_DECL_TYPE (decl);
9678 	  continue;
9679 
9680 	case ARRAY_REF:
9681 	  /* If the access to the function call is a non static field,
9682 	     build the code to access it. */
9683 	  if (JDECL_P (decl) && !FIELD_STATIC (decl))
9684 	    {
9685 	      decl = maybe_access_field (decl, *where_found, type);
9686 	      if (decl == error_mark_node)
9687 		return 1;
9688 	    }
9689 	  /* And code for the array reference expression */
9690 	  decl = java_complete_tree (qual_wfl);
9691 	  if (decl == error_mark_node)
9692 	    return 1;
9693 	  type = QUAL_DECL_TYPE (decl);
9694 	  continue;
9695 
9696 	case PLUS_EXPR:
9697 	  if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9698 	    return 1;
9699 	  if ((type = patch_string (decl)))
9700 	    decl = type;
9701 	  *where_found = QUAL_RESOLUTION (q) = decl;
9702 	  *type_found = type = TREE_TYPE (decl);
9703 	  break;
9704 
9705 	case CLASS_LITERAL:
9706 	  if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9707 	    return 1;
9708 	  *where_found = QUAL_RESOLUTION (q) = decl;
9709 	  *type_found = type = TREE_TYPE (decl);
9710 	  break;
9711 
9712 	default:
9713 	  /* Fix for -Wall Just go to the next statement. Don't
9714              continue */
9715 	  break;
9716 	}
9717 
9718       /* If we fall here, we weren't processing a (static) function call. */
9719       previous_call_static = 0;
9720 
9721       /* It can be the keyword THIS */
9722       if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9723 	  && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9724 	{
9725 	  if (!current_this)
9726 	    {
9727 	      parse_error_context
9728 		(wfl, "Keyword `this' used outside allowed context");
9729 	      return 1;
9730 	    }
9731 	  if (ctxp->explicit_constructor_p
9732 	      && type == current_class)
9733 	    {
9734 	      parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9735 	      return 1;
9736 	    }
9737 	  /* We have to generate code for intermediate access */
9738 	  if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9739 	    {
9740 	      *where_found = decl = current_this;
9741 	      *type_found = type = QUAL_DECL_TYPE (decl);
9742 	    }
9743 	  /* We're trying to access the this from somewhere else. Make sure
9744 	     it's allowed before doing so. */
9745 	  else
9746 	    {
9747 	      if (!enclosing_context_p (type, current_class))
9748 		{
9749 		  char *p  = xstrdup (lang_printable_name (type, 0));
9750 		  parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9751 				       p, p,
9752 				       lang_printable_name (current_class, 0));
9753 		  free (p);
9754 		  return 1;
9755 		}
9756 	      from_qualified_this = 1;
9757 	      /* If there's nothing else after that, we need to
9758                  produce something now, otherwise, the section of the
9759                  code that needs to produce <T>.this will generate
9760                  what is necessary. */
9761 	      if (!TREE_CHAIN (q))
9762 		{
9763 		  decl = build_access_to_thisn (current_class, type, 0);
9764 		  *where_found = decl = java_complete_tree (decl);
9765 		  *type_found = type = TREE_TYPE (decl);
9766 		}
9767 	    }
9768 
9769 	  from_type = 0;
9770 	  continue;
9771 	}
9772 
9773       /* 15.10.2 Accessing Superclass Members using SUPER */
9774       if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9775 	  && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9776 	{
9777 	  tree node;
9778 	  /* Check on the restricted use of SUPER */
9779 	  if (METHOD_STATIC (current_function_decl)
9780 	      || current_class == object_type_node)
9781 	    {
9782 	      parse_error_context
9783 		(wfl, "Keyword `super' used outside allowed context");
9784 	      return 1;
9785 	    }
9786 	  /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9787 	  node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9788 			     CLASSTYPE_SUPER (current_class),
9789 			     build_this (EXPR_WFL_LINECOL (qual_wfl)));
9790 	  *where_found = decl = java_complete_tree (node);
9791 	  if (decl == error_mark_node)
9792 	    return 1;
9793 	  *type_found = type = QUAL_DECL_TYPE (decl);
9794 	  from_super = from_type = 1;
9795 	  continue;
9796 	}
9797 
9798       /* 15.13.1: Can't search for field name in packages, so we
9799 	 assume a variable/class name was meant. */
9800       if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9801 	{
9802 	  tree name;
9803 	  if ((decl = resolve_package (wfl, &q, &name)))
9804 	    {
9805 	      tree list;
9806 	      *where_found = decl;
9807 
9808 	      /* We want to be absolutely sure that the class is laid
9809                  out. We're going to search something inside it. */
9810 	      *type_found = type = TREE_TYPE (decl);
9811 	      layout_class (type);
9812 	      from_type = 1;
9813 
9814 	      /* Fix them all the way down, if any are left. */
9815 	      if (q)
9816 		{
9817 		  list = TREE_CHAIN (q);
9818 		  while (list)
9819 		    {
9820 		      RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9821 		      RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9822 		      list = TREE_CHAIN (list);
9823 		    }
9824 		}
9825 	    }
9826 	  else
9827 	    {
9828 	      if (from_super || from_cast)
9829 		parse_error_context
9830 		  ((from_cast ? qual_wfl : wfl),
9831 		   "No variable `%s' defined in class `%s'",
9832 		   IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9833 		   lang_printable_name (type, 0));
9834 	      else
9835 		parse_error_context
9836 		  (qual_wfl, "Undefined variable or class name: `%s'",
9837 		   IDENTIFIER_POINTER (name));
9838 	      return 1;
9839 	    }
9840 	}
9841 
9842       /* We have a type name. It's been already resolved when the
9843 	 expression was qualified. */
9844       else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
9845 	{
9846 	  decl = QUAL_RESOLUTION (q);
9847 
9848 	  /* Sneak preview. If next we see a `new', we're facing a
9849 	     qualification with resulted in a type being selected
9850 	     instead of a field.  Report the error */
9851 	  if(TREE_CHAIN (q)
9852 	     && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9853 	    {
9854 	      parse_error_context (qual_wfl, "Undefined variable `%s'",
9855 				   IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9856 	      return 1;
9857 	    }
9858 
9859 	  if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
9860 	    {
9861 	      parse_error_context
9862 		(qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9863 		 java_accstring_lookup (get_access_flags_from_decl (decl)),
9864 		 GET_TYPE_NAME (type),
9865 		 IDENTIFIER_POINTER (DECL_NAME (decl)),
9866 		 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9867 	      return 1;
9868 	    }
9869 	  check_deprecation (qual_wfl, decl);
9870 
9871 	  type = TREE_TYPE (decl);
9872 	  from_type = 1;
9873 	}
9874       /* We resolve an expression name */
9875       else
9876 	{
9877 	  tree field_decl = NULL_TREE;
9878 
9879 	  /* If there exists an early resolution, use it. That occurs
9880 	     only once and we know that there are more things to
9881 	     come. Don't do that when processing something after SUPER
9882 	     (we need more thing to be put in place below */
9883 	  if (!from_super && QUAL_RESOLUTION (q))
9884 	    {
9885 	      decl = QUAL_RESOLUTION (q);
9886 	      if (!type)
9887 		{
9888 		  if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9889 		    {
9890 		      if (current_this)
9891 			*where_found = current_this;
9892 		      else
9893 			{
9894 			  static_ref_err (qual_wfl, DECL_NAME (decl),
9895 					  current_class);
9896 			  return 1;
9897 			}
9898                       if (outer_field_access_p (current_class, decl))
9899                         decl = build_outer_field_access (qual_wfl, decl);
9900 		    }
9901 		  else
9902 		    {
9903 		      *where_found = TREE_TYPE (decl);
9904 		      if (TREE_CODE (*where_found) == POINTER_TYPE)
9905 			*where_found = TREE_TYPE (*where_found);
9906 		    }
9907 		}
9908 	    }
9909 
9910 	  /* Report and error if we're using a numerical litteral as a
9911              qualifier. It can only be an INTEGER_CST. */
9912 	  else if (TREE_CODE (qual_wfl) == INTEGER_CST)
9913 	    {
9914 	      parse_error_context
9915 		(wfl, "Can't use type `%s' as a qualifier",
9916 		 lang_printable_name (TREE_TYPE (qual_wfl), 0));
9917 	      return 1;
9918 	    }
9919 
9920 	  /* We have to search for a field, knowing the type of its
9921              container. The flag FROM_TYPE indicates that we resolved
9922              the last member of the expression as a type name, which
9923              means that for the resolution of this field, we'll look
9924              for other errors than if it was resolved as a member of
9925              an other field. */
9926 	  else
9927 	    {
9928 	      int is_static;
9929 	      tree field_decl_type; /* For layout */
9930 
9931 	      if (!from_type && !JREFERENCE_TYPE_P (type))
9932 		{
9933 		  parse_error_context
9934 		    (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9935 		     IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9936 		     lang_printable_name (type, 0),
9937 		     IDENTIFIER_POINTER (DECL_NAME (decl)));
9938 		  return 1;
9939 		}
9940 
9941 	      field_decl = lookup_field_wrapper (type,
9942 						 EXPR_WFL_NODE (qual_wfl));
9943 
9944 	      /* Maybe what we're trying to access to is an inner
9945 		 class, only if decl is a TYPE_DECL. */
9946 	      if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
9947 		{
9948 		  tree ptr, inner_decl;
9949 
9950 		  BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9951 		  inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9952 		  if (inner_decl)
9953 		    {
9954 		      check_inner_class_access (inner_decl, decl, qual_wfl);
9955 		      type = TREE_TYPE (inner_decl);
9956 		      decl = inner_decl;
9957 		      from_type = 1;
9958 		      continue;
9959 		    }
9960 		}
9961 
9962 	      if (field_decl == NULL_TREE)
9963 		{
9964 		  parse_error_context
9965 		    (qual_wfl, "No variable `%s' defined in type `%s'",
9966 		     IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9967 		     GET_TYPE_NAME (type));
9968 		  return 1;
9969 		}
9970 	      if (field_decl == error_mark_node)
9971 		return 1;
9972 
9973 	      /* Layout the type of field_decl, since we may need
9974                  it. Don't do primitive types or loaded classes. The
9975                  situation of non primitive arrays may not handled
9976                  properly here. FIXME */
9977 	      if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9978 		field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9979 	      else
9980 		field_decl_type = TREE_TYPE (field_decl);
9981 	      if (!JPRIMITIVE_TYPE_P (field_decl_type)
9982 		  && !CLASS_LOADED_P (field_decl_type)
9983 		  && !TYPE_ARRAY_P (field_decl_type))
9984 		resolve_and_layout (field_decl_type, NULL_TREE);
9985 
9986 	      /* Check on accessibility here */
9987 	      if (not_accessible_p (current_class, field_decl,
9988 				    DECL_CONTEXT (field_decl), from_super))
9989 		{
9990 		  parse_error_context
9991 		    (qual_wfl,
9992 		     "Can't access %s field `%s.%s' from `%s'",
9993 		     java_accstring_lookup
9994 		       (get_access_flags_from_decl (field_decl)),
9995 		     GET_TYPE_NAME (type),
9996 		     IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9997 		     IDENTIFIER_POINTER
9998 		       (DECL_NAME (TYPE_NAME (current_class))));
9999 		  return 1;
10000 		}
10001 	      check_deprecation (qual_wfl, field_decl);
10002 
10003 	      /* There are things to check when fields are accessed
10004 	         from type. There are no restrictions on a static
10005 	         declaration of the field when it is accessed from an
10006 	         interface */
10007 	      is_static = FIELD_STATIC (field_decl);
10008 	      if (!from_super && from_type
10009 		  && !TYPE_INTERFACE_P (type)
10010 		  && !is_static
10011 		  && (current_function_decl
10012 		      && METHOD_STATIC (current_function_decl)))
10013 		{
10014 		  static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
10015 		  return 1;
10016 		}
10017 	      from_cast = from_super = 0;
10018 
10019 	      /* It's an access from a type but it isn't static, we
10020 		 make it relative to `this'. */
10021 	      if (!is_static && from_type)
10022 		decl = current_this;
10023 
10024 	      /* If we need to generate something to get a proper
10025 		 handle on what this field is accessed from, do it
10026 		 now. */
10027 	      if (!is_static)
10028 		{
10029 		  decl = maybe_access_field (decl, *where_found, *type_found);
10030 		  if (decl == error_mark_node)
10031 		    return 1;
10032 		}
10033 
10034 	      /* We want to keep the location were found it, and the type
10035 		 we found. */
10036 	      *where_found = decl;
10037 	      *type_found = type;
10038 
10039 	      /* Generate the correct expression for field access from
10040 		 qualified this */
10041 	      if (from_qualified_this)
10042 		{
10043 		  field_decl = build_outer_field_access (qual_wfl, field_decl);
10044 		  from_qualified_this = 0;
10045 		}
10046 
10047 	      /* This is the decl found and eventually the next one to
10048 		 search from */
10049 	      decl = field_decl;
10050 	    }
10051 	  from_type = 0;
10052 	  type = QUAL_DECL_TYPE (decl);
10053 
10054 	  /* Sneak preview. If decl is qualified by a `new', report
10055              the error here to be accurate on the peculiar construct */
10056 	  if (TREE_CHAIN (q)
10057 	      && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
10058 	      && !JREFERENCE_TYPE_P (type))
10059 	    {
10060 	      parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
10061 				   lang_printable_name (type, 0));
10062 	      return 1;
10063 	    }
10064 	}
10065       /* `q' might have changed due to a after package resolution
10066          re-qualification */
10067       if (!q)
10068 	break;
10069     }
10070   *found_decl = decl;
10071   return 0;
10072 }
10073 
10074 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
10075    can't be accessed from REFERENCE (a record type). If MEMBER
10076    features a protected access, we then use WHERE which, if non null,
10077    holds the type of MEMBER's access that is checked against
10078    6.6.2.1. This function should be used when decl is a field or a
10079    method.  */
10080 
10081 static int
not_accessible_p(reference,member,where,from_super)10082 not_accessible_p (reference, member, where, from_super)
10083      tree reference, member;
10084      tree where;
10085      int from_super;
10086 {
10087   int access_flag = get_access_flags_from_decl (member);
10088 
10089   /* Inner classes are processed by check_inner_class_access */
10090   if (INNER_CLASS_TYPE_P (reference))
10091     return 0;
10092 
10093   /* Access always granted for members declared public */
10094   if (access_flag & ACC_PUBLIC)
10095     return 0;
10096 
10097   /* Check access on protected members */
10098   if (access_flag & ACC_PROTECTED)
10099     {
10100       /* Access granted if it occurs from within the package
10101          containing the class in which the protected member is
10102          declared */
10103       if (class_in_current_package (DECL_CONTEXT (member)))
10104 	return 0;
10105 
10106       /* If accessed with the form `super.member', then access is granted */
10107       if (from_super)
10108 	return 0;
10109 
10110       /* If where is active, access was made through a
10111 	 qualifier. Access is granted if the type of the qualifier is
10112 	 or is a sublass of the type the access made from (6.6.2.1.)  */
10113       if (where && !inherits_from_p (reference, where))
10114 	return 1;
10115 
10116       /* Otherwise, access is granted if occurring from the class where
10117 	 member is declared or a subclass of it. Find the right
10118 	 context to perform the check */
10119       if (PURE_INNER_CLASS_TYPE_P (reference))
10120         {
10121           while (INNER_CLASS_TYPE_P (reference))
10122             {
10123               if (inherits_from_p (reference, DECL_CONTEXT (member)))
10124                 return 0;
10125               reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10126             }
10127         }
10128       if (inherits_from_p (reference, DECL_CONTEXT (member)))
10129 	return 0;
10130       return 1;
10131     }
10132 
10133   /* Check access on private members. Access is granted only if it
10134      occurs from within the class in which it is declared -- that does
10135      it for innerclasses too. */
10136   if (access_flag & ACC_PRIVATE)
10137     {
10138       if (reference == DECL_CONTEXT (member))
10139 	return 0;
10140       if (enclosing_context_p (reference, DECL_CONTEXT (member)))
10141 	return 0;
10142       return 1;
10143     }
10144 
10145   /* Default access are permitted only when occurring within the
10146      package in which the type (REFERENCE) is declared. In other words,
10147      REFERENCE is defined in the current package */
10148   if (ctxp->package)
10149     return !class_in_current_package (reference);
10150 
10151   /* Otherwise, access is granted */
10152   return 0;
10153 }
10154 
10155 /* Test deprecated decl access.  */
10156 static void
check_deprecation(wfl,decl)10157 check_deprecation (wfl, decl)
10158      tree wfl, decl;
10159 {
10160   const char *file = DECL_SOURCE_FILE (decl);
10161   /* Complain if the field is deprecated and the file it was defined
10162      in isn't compiled at the same time the file which contains its
10163      use is */
10164   if (DECL_DEPRECATED (decl)
10165       && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
10166     {
10167       char the [20];
10168       switch (TREE_CODE (decl))
10169 	{
10170 	case FUNCTION_DECL:
10171 	  strcpy (the, "method");
10172 	  break;
10173 	case FIELD_DECL:
10174 	case VAR_DECL:
10175 	  strcpy (the, "field");
10176 	  break;
10177 	case TYPE_DECL:
10178 	  parse_warning_context (wfl, "The class `%s' has been deprecated",
10179 				 IDENTIFIER_POINTER (DECL_NAME (decl)));
10180 	  return;
10181 	default:
10182 	  abort ();
10183 	}
10184       /* Don't issue a message if the context as been deprecated as a
10185          whole. */
10186       if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
10187 	parse_warning_context
10188 	  (wfl, "The %s `%s' in class `%s' has been deprecated",
10189 	   the, lang_printable_name (decl, 0),
10190 	   IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
10191     }
10192 }
10193 
10194 /* Returns 1 if class was declared in the current package, 0 otherwise */
10195 
10196 static GTY(()) tree cicp_cache;
10197 static int
class_in_current_package(class)10198 class_in_current_package (class)
10199      tree class;
10200 {
10201   int qualified_flag;
10202   tree left;
10203 
10204   if (cicp_cache == class)
10205     return 1;
10206 
10207   qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
10208 
10209   /* If the current package is empty and the name of CLASS is
10210      qualified, class isn't in the current package.  If there is a
10211      current package and the name of the CLASS is not qualified, class
10212      isn't in the current package */
10213   if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
10214     return 0;
10215 
10216   /* If there is not package and the name of CLASS isn't qualified,
10217      they belong to the same unnamed package */
10218   if (!ctxp->package && !qualified_flag)
10219     return 1;
10220 
10221   /* Compare the left part of the name of CLASS with the package name */
10222   breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
10223   if (ctxp->package == left)
10224     {
10225       cicp_cache = class;
10226       return 1;
10227     }
10228   return 0;
10229 }
10230 
10231 /* This function may generate code to access DECL from WHERE. This is
10232    done only if certain conditions meet.  */
10233 
10234 static tree
maybe_access_field(decl,where,type)10235 maybe_access_field (decl, where, type)
10236   tree decl, where, type;
10237 {
10238   if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10239       && !FIELD_STATIC (decl))
10240     decl = build_field_ref (where ? where : current_this,
10241 			    (type ? type : DECL_CONTEXT (decl)),
10242 			    DECL_NAME (decl));
10243   return decl;
10244 }
10245 
10246 /* Build a method invocation, by patching PATCH. If non NULL
10247    and according to the situation, PRIMARY and WHERE may be
10248    used. IS_STATIC is set to 1 if the invoked function is static. */
10249 
10250 static tree
patch_method_invocation(patch,primary,where,from_super,is_static,ret_decl)10251 patch_method_invocation (patch, primary, where, from_super,
10252                         is_static, ret_decl)
10253      tree patch, primary, where;
10254      int from_super;
10255      int *is_static;
10256      tree *ret_decl;
10257 {
10258   tree wfl = TREE_OPERAND (patch, 0);
10259   tree args = TREE_OPERAND (patch, 1);
10260   tree name = EXPR_WFL_NODE (wfl);
10261   tree list;
10262   int is_static_flag = 0;
10263   int is_super_init = 0;
10264   tree this_arg = NULL_TREE;
10265   int is_array_clone_call = 0;
10266 
10267   /* Should be overriden if everything goes well. Otherwise, if
10268      something fails, it should keep this value. It stop the
10269      evaluation of a bogus assignment. See java_complete_tree,
10270      MODIFY_EXPR: for the reasons why we sometimes want to keep on
10271      evaluating an assignment */
10272   TREE_TYPE (patch) = error_mark_node;
10273 
10274   /* Since lookup functions are messing with line numbers, save the
10275      context now.  */
10276   java_parser_context_save_global ();
10277 
10278   /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10279 
10280   /* Resolution of qualified name, excluding constructors */
10281   if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10282     {
10283       tree identifier, identifier_wfl, type, resolved;
10284       /* Extract the last IDENTIFIER of the qualified
10285 	 expression. This is a wfl and we will use it's location
10286 	 data during error report. */
10287       identifier_wfl = cut_identifier_in_qualified (wfl);
10288       identifier = EXPR_WFL_NODE (identifier_wfl);
10289 
10290       /* Given the context, IDENTIFIER is syntactically qualified
10291 	 as a MethodName. We need to qualify what's before */
10292       qualify_ambiguous_name (wfl);
10293       resolved = resolve_field_access (wfl, NULL, NULL);
10294 
10295       if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10296          && FIELD_FINAL (resolved)
10297          && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10298          && !flag_emit_class_files && !flag_emit_xref)
10299        resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10300 
10301       if (resolved == error_mark_node)
10302 	PATCH_METHOD_RETURN_ERROR ();
10303 
10304       type = GET_SKIP_TYPE (resolved);
10305       resolve_and_layout (type, NULL_TREE);
10306 
10307       if (JPRIMITIVE_TYPE_P (type))
10308         {
10309 	  parse_error_context
10310 	    (identifier_wfl,
10311 	     "Can't invoke a method on primitive type `%s'",
10312 	     IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10313 	  PATCH_METHOD_RETURN_ERROR ();
10314 	}
10315 
10316       list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10317       args = nreverse (args);
10318 
10319       /* We're resolving a call from a type */
10320       if (TREE_CODE (resolved) == TYPE_DECL)
10321 	{
10322 	  if (CLASS_INTERFACE (resolved))
10323 	    {
10324 	      parse_error_context
10325 		(identifier_wfl,
10326 		"Can't make static reference to method `%s' in interface `%s'",
10327 		 IDENTIFIER_POINTER (identifier),
10328 		 IDENTIFIER_POINTER (name));
10329 	      PATCH_METHOD_RETURN_ERROR ();
10330 	    }
10331 	  if (list && !METHOD_STATIC (list))
10332 	    {
10333 	      char *fct_name = xstrdup (lang_printable_name (list, 0));
10334 	      parse_error_context
10335 		(identifier_wfl,
10336 		 "Can't make static reference to method `%s %s' in class `%s'",
10337 		 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10338 		 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10339 	      free (fct_name);
10340 	      PATCH_METHOD_RETURN_ERROR ();
10341 	    }
10342 	}
10343       else
10344 	this_arg = primary = resolved;
10345 
10346       if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10347         is_array_clone_call = 1;
10348 
10349       /* IDENTIFIER_WFL will be used to report any problem further */
10350       wfl = identifier_wfl;
10351     }
10352   /* Resolution of simple names, names generated after a primary: or
10353      constructors */
10354   else
10355     {
10356       tree class_to_search = NULL_TREE;
10357       int lc;			/* Looking for Constructor */
10358 
10359       /* We search constructor in their target class */
10360       if (CALL_CONSTRUCTOR_P (patch))
10361 	{
10362 	  if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10363 	    class_to_search = EXPR_WFL_NODE (wfl);
10364 	  else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10365 		   this_identifier_node)
10366 	    class_to_search = NULL_TREE;
10367 	  else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10368 		   super_identifier_node)
10369 	    {
10370 	      is_super_init = 1;
10371 	      if (CLASSTYPE_SUPER (current_class))
10372 		class_to_search =
10373 		  DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10374 	      else
10375 		{
10376 		  parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10377 		  PATCH_METHOD_RETURN_ERROR ();
10378 		}
10379 	    }
10380 
10381 	  /* Class to search is NULL if we're searching the current one */
10382 	  if (class_to_search)
10383 	    {
10384 	      class_to_search = resolve_and_layout (class_to_search, wfl);
10385 
10386 	      if (!class_to_search)
10387 		{
10388 		  parse_error_context
10389 		    (wfl, "Class `%s' not found in type declaration",
10390 		     IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10391 		  PATCH_METHOD_RETURN_ERROR ();
10392 		}
10393 
10394 	      /* Can't instantiate an abstract class, but we can
10395 	         invoke it's constructor. It's use within the `new'
10396 	         context is denied here. */
10397 	      if (CLASS_ABSTRACT (class_to_search)
10398 		  && TREE_CODE (patch) == NEW_CLASS_EXPR)
10399 		{
10400 		  parse_error_context
10401 		    (wfl, "Class `%s' is an abstract class. It can't be instantiated",
10402 		     IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10403 		  PATCH_METHOD_RETURN_ERROR ();
10404 		}
10405 
10406 	      class_to_search = TREE_TYPE (class_to_search);
10407 	    }
10408 	  else
10409 	    class_to_search = current_class;
10410 	  lc = 1;
10411 	}
10412       /* This is a regular search in the local class, unless an
10413          alternate class is specified. */
10414       else
10415 	{
10416 	  if (where != NULL_TREE)
10417 	    class_to_search = where;
10418 	  else if (QUALIFIED_P (name))
10419 	    class_to_search = current_class;
10420 	  else
10421 	    {
10422 	      class_to_search = current_class;
10423 
10424 	      for (;;)
10425 		{
10426 		  if (has_method (class_to_search, name))
10427 		    break;
10428 		  if (! INNER_CLASS_TYPE_P (class_to_search))
10429 		    {
10430 		      parse_error_context (wfl,
10431 					   "No method named `%s' in scope",
10432 					   IDENTIFIER_POINTER (name));
10433 		      PATCH_METHOD_RETURN_ERROR ();
10434 		    }
10435 		  class_to_search
10436 		    = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10437 		}
10438 	    }
10439 	  lc = 0;
10440 	}
10441 
10442       /* NAME is a simple identifier or comes from a primary. Search
10443 	 in the class whose declaration contain the method being
10444 	 invoked. */
10445       resolve_and_layout (class_to_search, NULL_TREE);
10446 
10447       list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10448       /* Don't continue if no method were found, as the next statement
10449          can't be executed then. */
10450       if (!list)
10451 	PATCH_METHOD_RETURN_ERROR ();
10452 
10453       if (TYPE_ARRAY_P (class_to_search)
10454           && DECL_NAME (list) == get_identifier ("clone"))
10455         is_array_clone_call = 1;
10456 
10457       /* Check for static reference if non static methods */
10458       if (check_for_static_method_reference (wfl, patch, list,
10459 					     class_to_search, primary))
10460 	PATCH_METHOD_RETURN_ERROR ();
10461 
10462       /* Check for inner classes creation from illegal contexts */
10463       if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10464 		 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10465 	  && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10466 	  && !DECL_INIT_P (current_function_decl))
10467 	{
10468 	  parse_error_context
10469 	    (wfl, "No enclosing instance for inner class `%s' is in scope%s",
10470 	     lang_printable_name (class_to_search, 0),
10471 	     (!current_this ? "" :
10472 	      "; an explicit one must be provided when creating this inner class"));
10473 	  PATCH_METHOD_RETURN_ERROR ();
10474 	}
10475 
10476       /* Non static methods are called with the current object extra
10477 	 argument. If patch a `new TYPE()', the argument is the value
10478 	 returned by the object allocator. If method is resolved as a
10479 	 primary, use the primary otherwise use the current THIS. */
10480       args = nreverse (args);
10481       if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10482 	{
10483 	  this_arg = primary ? primary : current_this;
10484 
10485 	  /* If we're using an access method, things are different.
10486 	     There are two familly of cases:
10487 
10488 	     1) We're not generating bytecodes:
10489 
10490 	     - LIST is non static. It's invocation is transformed from
10491 	       x(a1,...,an) into this$<n>.x(a1,....an).
10492 	     - LIST is static. It's invocation is transformed from
10493 	       x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10494 
10495 	     2) We're generating bytecodes:
10496 
10497 	     - LIST is non static. It's invocation is transformed from
10498 	       x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10499 	     - LIST is static. It's invocation is transformed from
10500 	       x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10501 
10502 	     Of course, this$<n> can be abitrary complex, ranging from
10503 	     this$0 (the immediate outer context) to
10504 	     access$0(access$0(...(this$0))).
10505 
10506 	     maybe_use_access_method returns a nonzero value if the
10507 	     this_arg has to be moved into the (then generated) stub
10508 	     argument list. In the meantime, the selected function
10509 	     might have be replaced by a generated stub. */
10510 	  if (!primary &&
10511 	      maybe_use_access_method (is_super_init, &list, &this_arg))
10512 	    {
10513 	      args = tree_cons (NULL_TREE, this_arg, args);
10514 	      this_arg = NULL_TREE; /* So it doesn't get chained twice */
10515 	    }
10516 	}
10517     }
10518 
10519   /* Merge point of all resolution schemes. If we have nothing, this
10520      is an error, already signaled */
10521   if (!list)
10522     PATCH_METHOD_RETURN_ERROR ();
10523 
10524   /* Check accessibility, position the is_static flag, build and
10525      return the call */
10526   if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10527 			(primary ? TREE_TYPE (TREE_TYPE (primary)) :
10528 			 NULL_TREE), from_super)
10529       /* Calls to clone() on array types are permitted as a special-case. */
10530       && !is_array_clone_call)
10531     {
10532       const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10533       const char *const access =
10534 	java_accstring_lookup (get_access_flags_from_decl (list));
10535       const char *const klass =
10536 	IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10537       const char *const refklass =
10538 	IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10539       const char *const what = (DECL_CONSTRUCTOR_P (list)
10540 				? "constructor" : "method");
10541       /* FIXME: WFL yields the wrong message here but I don't know
10542 	 what else to use.  */
10543       parse_error_context (wfl,
10544 			   "Can't access %s %s `%s.%s' from `%s'",
10545 			   access, what, klass, fct_name, refklass);
10546       PATCH_METHOD_RETURN_ERROR ();
10547     }
10548 
10549   /* Deprecation check: check whether the method being invoked or the
10550      instance-being-created's type are deprecated. */
10551   if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10552     check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10553   else
10554     check_deprecation (wfl, list);
10555 
10556   /* If invoking a innerclass constructor, there are hidden parameters
10557      to pass */
10558   if (TREE_CODE (patch) == NEW_CLASS_EXPR
10559       && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10560     {
10561       /* And make sure we add the accessed local variables to be saved
10562 	 in field aliases. */
10563       args = build_alias_initializer_parameter_list
10564 	(AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10565 
10566       /* Secretly pass the current_this/primary as a second argument */
10567       if (primary || current_this)
10568 	{
10569 	  tree extra_arg;
10570 	  tree this_type = (current_this ?
10571 			    TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10572 	  /* Method's (list) enclosing context */
10573 	  tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10574 	  /* If we have a primary, use it. */
10575 	  if (primary)
10576 	    extra_arg = primary;
10577 	  /* The current `this' is an inner class but isn't a direct
10578 	     enclosing context for the inner class we're trying to
10579 	     create. Build an access to the proper enclosing context
10580 	     and use it. */
10581 	  else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10582 		   && this_type != TREE_TYPE (mec))
10583 	    {
10584 
10585 	      extra_arg = build_access_to_thisn (current_class,
10586 						 TREE_TYPE (mec), 0);
10587 	      extra_arg = java_complete_tree (extra_arg);
10588 	    }
10589 	  /* Otherwise, just use the current `this' as an enclosing
10590              context. */
10591 	  else
10592 	    extra_arg = current_this;
10593 	  args = tree_cons (NULL_TREE, extra_arg, args);
10594 	}
10595       else
10596 	args = tree_cons (NULL_TREE, integer_zero_node, args);
10597     }
10598 
10599   /* This handles the situation where a constructor invocation needs
10600      to have an enclosing context passed as a second parameter (the
10601      constructor is one of an inner class). */
10602   if ((is_super_init ||
10603        (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10604       && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10605     {
10606       tree dest = TYPE_NAME (DECL_CONTEXT (list));
10607       tree extra_arg =
10608 	build_access_to_thisn (current_class, DECL_CONTEXT (dest), 0);
10609       extra_arg = java_complete_tree (extra_arg);
10610       args = tree_cons (NULL_TREE, extra_arg, args);
10611     }
10612 
10613   is_static_flag = METHOD_STATIC (list);
10614   if (! is_static_flag && this_arg != NULL_TREE)
10615     args = tree_cons (NULL_TREE, this_arg, args);
10616 
10617   /* In the context of an explicit constructor invocation, we can't
10618      invoke any method relying on `this'. Exceptions are: we're
10619      invoking a static function, primary exists and is not the current
10620      this, we're creating a new object. */
10621   if (ctxp->explicit_constructor_p
10622       && !is_static_flag
10623       && (!primary || primary == current_this)
10624       && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10625     {
10626       parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
10627       PATCH_METHOD_RETURN_ERROR ();
10628     }
10629   java_parser_context_restore_global ();
10630   if (is_static)
10631     *is_static = is_static_flag;
10632   /* Sometimes, we want the decl of the selected method. Such as for
10633      EH checking */
10634   if (ret_decl)
10635     *ret_decl = list;
10636   patch = patch_invoke (patch, list, args);
10637 
10638   /* Now is a good time to insert the call to finit$ */
10639   if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10640     {
10641       tree finit_parms, finit_call;
10642 
10643       /* Prepare to pass hidden parameters to finit$, if any. */
10644       finit_parms = build_alias_initializer_parameter_list
10645 	(AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10646 
10647       finit_call =
10648 	build_method_invocation (build_wfl_node (finit_identifier_node),
10649 				 finit_parms);
10650 
10651       /* Generate the code used to initialize fields declared with an
10652 	 initialization statement and build a compound statement along
10653 	 with the super constructor invocation. */
10654       CAN_COMPLETE_NORMALLY (patch) = 1;
10655       patch = build (COMPOUND_EXPR, void_type_node, patch,
10656 		     java_complete_tree (finit_call));
10657     }
10658   return patch;
10659 }
10660 
10661 /* Check that we're not trying to do a static reference to a method in
10662    non static method. Return 1 if it's the case, 0 otherwise. */
10663 
10664 static int
check_for_static_method_reference(wfl,node,method,where,primary)10665 check_for_static_method_reference (wfl, node, method, where, primary)
10666      tree wfl, node, method, where, primary;
10667 {
10668   if (METHOD_STATIC (current_function_decl)
10669       && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10670     {
10671       char *fct_name = xstrdup (lang_printable_name (method, 0));
10672       parse_error_context
10673 	(wfl, "Can't make static reference to method `%s %s' in class `%s'",
10674 	 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10675 	 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10676       free (fct_name);
10677       return 1;
10678     }
10679   return 0;
10680 }
10681 
10682 /* Fix the invocation of *MDECL if necessary in the case of a
10683    invocation from an inner class. *THIS_ARG might be modified
10684    appropriately and an alternative access to *MDECL might be
10685    returned.  */
10686 
10687 static int
maybe_use_access_method(is_super_init,mdecl,this_arg)10688 maybe_use_access_method (is_super_init, mdecl, this_arg)
10689      int is_super_init;
10690      tree *mdecl, *this_arg;
10691 {
10692   tree ctx;
10693   tree md = *mdecl, ta = *this_arg;
10694   int to_return = 0;
10695   int non_static_context = !METHOD_STATIC (md);
10696 
10697   if (is_super_init
10698       || DECL_CONTEXT (md) == current_class
10699       || !PURE_INNER_CLASS_TYPE_P (current_class)
10700       || DECL_FINIT_P (md)
10701       || DECL_INSTINIT_P (md))
10702     return 0;
10703 
10704   /* If we're calling a method found in an enclosing class, generate
10705      what it takes to retrieve the right this. Don't do that if we're
10706      invoking a static method. Note that if MD's type is unrelated to
10707      CURRENT_CLASS, then the current this can be used. */
10708 
10709   if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10710     {
10711       ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10712       if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10713 	{
10714 	  ta = build_current_thisn (current_class);
10715 	  ta = build_wfl_node (ta);
10716 	}
10717       else
10718 	{
10719 	  tree type = ctx;
10720 	  while (type)
10721 	    {
10722 	      maybe_build_thisn_access_method (type);
10723 	      if (inherits_from_p (type, DECL_CONTEXT (md)))
10724 		{
10725 		  ta = build_access_to_thisn (ctx, type, 0);
10726 		  break;
10727 		}
10728 	      type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10729 		      TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10730 	    }
10731 	}
10732       ta = java_complete_tree (ta);
10733     }
10734 
10735   /* We might have to use an access method to get to MD. We can
10736      break the method access rule as far as we're not generating
10737      bytecode */
10738   if (METHOD_PRIVATE (md) && flag_emit_class_files)
10739     {
10740       md = build_outer_method_access_method (md);
10741       to_return = 1;
10742     }
10743 
10744   *mdecl = md;
10745   *this_arg = ta;
10746 
10747   /* Returnin a nonzero value indicates we were doing a non static
10748      method invokation that is now a static invocation. It will have
10749      callee displace `this' to insert it in the regular argument
10750      list. */
10751   return (non_static_context && to_return);
10752 }
10753 
10754 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10755    mode.  */
10756 
10757 static tree
patch_invoke(patch,method,args)10758 patch_invoke (patch, method, args)
10759      tree patch, method, args;
10760 {
10761   tree dtable, func;
10762   tree original_call, t, ta;
10763   tree check = NULL_TREE;
10764 
10765   /* Last step for args: convert build-in types. If we're dealing with
10766      a new TYPE() type call, the first argument to the constructor
10767      isn't found in the incoming argument list, but delivered by
10768      `new' */
10769   t = TYPE_ARG_TYPES (TREE_TYPE (method));
10770   if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10771     t = TREE_CHAIN (t);
10772   for (ta = args; t != end_params_node && ta;
10773        t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10774     if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10775 	TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10776       TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10777 
10778   /* Resolve unresolved returned type isses */
10779   t = TREE_TYPE (TREE_TYPE (method));
10780   if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10781     resolve_and_layout (TREE_TYPE (t), NULL);
10782 
10783   if (flag_emit_class_files || flag_emit_xref)
10784     func = method;
10785   else
10786     {
10787       switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10788 	{
10789 	case INVOKE_VIRTUAL:
10790 	  dtable = invoke_build_dtable (0, args);
10791 	  func = build_invokevirtual (dtable, method);
10792 	  break;
10793 
10794 	case INVOKE_NONVIRTUAL:
10795 	  /* If the object for the method call is null, we throw an
10796 	     exception.  We don't do this if the object is the current
10797 	     method's `this'.  In other cases we just rely on an
10798 	     optimization pass to eliminate redundant checks.  */
10799 	  if (TREE_VALUE (args) != current_this)
10800 	    {
10801 	      /* We use a save_expr here to make sure we only evaluate
10802 		 the new `self' expression once.  */
10803 	      tree save_arg = save_expr (TREE_VALUE (args));
10804 	      TREE_VALUE (args) = save_arg;
10805 	      check = java_check_reference (save_arg, 1);
10806 	    }
10807 	  /* Fall through.  */
10808 
10809 	case INVOKE_SUPER:
10810 	case INVOKE_STATIC:
10811 	  {
10812 	    tree signature = build_java_signature (TREE_TYPE (method));
10813 	    func = build_known_method_ref (method, TREE_TYPE (method),
10814 					   DECL_CONTEXT (method),
10815 					   signature, args);
10816 	  }
10817 	  break;
10818 
10819 	case INVOKE_INTERFACE:
10820 	  dtable = invoke_build_dtable (1, args);
10821 	  func = build_invokeinterface (dtable, method);
10822 	  break;
10823 
10824 	default:
10825 	  abort ();
10826 	}
10827 
10828       /* Ensure self_type is initialized, (invokestatic). FIXME */
10829       func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10830     }
10831 
10832   TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10833   TREE_OPERAND (patch, 0) = func;
10834   TREE_OPERAND (patch, 1) = args;
10835   patch = check_for_builtin (method, patch);
10836   original_call = patch;
10837 
10838   /* We're processing a `new TYPE ()' form. New is called and its
10839      returned value is the first argument to the constructor. We build
10840      a COMPOUND_EXPR and use saved expression so that the overall NEW
10841      expression value is a pointer to a newly created and initialized
10842      class. */
10843   if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10844     {
10845       tree class = DECL_CONTEXT (method);
10846       tree c1, saved_new, size, new;
10847       tree alloc_node;
10848 
10849       if (flag_emit_class_files || flag_emit_xref)
10850 	{
10851 	  TREE_TYPE (patch) = build_pointer_type (class);
10852 	  return patch;
10853 	}
10854       if (!TYPE_SIZE (class))
10855 	safe_layout_class (class);
10856       size = size_in_bytes (class);
10857       alloc_node =
10858 	(class_has_finalize_method (class) ? alloc_object_node
10859 		  			   : alloc_no_finalizer_node);
10860       new = build (CALL_EXPR, promote_type (class),
10861 		   build_address_of (alloc_node),
10862 		   tree_cons (NULL_TREE, build_class_ref (class),
10863 			      build_tree_list (NULL_TREE,
10864 					       size_in_bytes (class))),
10865 		   NULL_TREE);
10866       saved_new = save_expr (new);
10867       c1 = build_tree_list (NULL_TREE, saved_new);
10868       TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10869       TREE_OPERAND (original_call, 1) = c1;
10870       TREE_SET_CODE (original_call, CALL_EXPR);
10871       patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10872     }
10873 
10874   /* If CHECK is set, then we are building a check to see if the object
10875      is NULL.  */
10876   if (check != NULL_TREE)
10877     {
10878       /* We have to call force_evaluation_order now because creating a
10879  	 COMPOUND_EXPR wraps the arg list in a way that makes it
10880  	 unrecognizable by force_evaluation_order later.  Yuk.  */
10881       patch = build (COMPOUND_EXPR, TREE_TYPE (patch), check,
10882  		     force_evaluation_order (patch));
10883       TREE_SIDE_EFFECTS (patch) = 1;
10884     }
10885 
10886   /* In order to be able to modify PATCH later, we SAVE_EXPR it and
10887      put it as the first expression of a COMPOUND_EXPR. The second
10888      expression being an empty statement to be later patched if
10889      necessary. We remember a TREE_LIST (the PURPOSE is the method,
10890      the VALUE is the compound) in a hashtable and return a
10891      COMPOUND_EXPR built so that the result of the evaluation of the
10892      original PATCH node is returned. */
10893   if (STATIC_CLASS_INIT_OPT_P ()
10894       && current_function_decl && METHOD_STATIC (method))
10895     {
10896       tree list;
10897       tree fndecl = current_function_decl;
10898       /* We have to call force_evaluation_order now because creating a
10899 	 COMPOUND_EXPR wraps the arg list in a way that makes it
10900 	 unrecognizable by force_evaluation_order later.  Yuk.  */
10901       tree save = save_expr (force_evaluation_order (patch));
10902       tree type = TREE_TYPE (patch);
10903 
10904       patch = build (COMPOUND_EXPR, type, save, empty_stmt_node);
10905       list = tree_cons (method, patch,
10906 			DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
10907 
10908       DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
10909 
10910       patch = build (COMPOUND_EXPR, type, patch, save);
10911     }
10912 
10913   return patch;
10914 }
10915 
10916 static int
invocation_mode(method,super)10917 invocation_mode (method, super)
10918      tree method;
10919      int super;
10920 {
10921   int access = get_access_flags_from_decl (method);
10922 
10923   if (super)
10924     return INVOKE_SUPER;
10925 
10926   if (access & ACC_STATIC)
10927     return INVOKE_STATIC;
10928 
10929   /* We have to look for a constructor before we handle nonvirtual
10930      calls; otherwise the constructor will look nonvirtual.  */
10931   if (DECL_CONSTRUCTOR_P (method))
10932     return INVOKE_STATIC;
10933 
10934   if (access & ACC_FINAL || access & ACC_PRIVATE)
10935     return INVOKE_NONVIRTUAL;
10936 
10937   if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10938     return INVOKE_NONVIRTUAL;
10939 
10940   if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10941     return INVOKE_INTERFACE;
10942 
10943   return INVOKE_VIRTUAL;
10944 }
10945 
10946 /* Retrieve a refined list of matching methods. It covers the step
10947    15.11.2 (Compile-Time Step 2) */
10948 
10949 static tree
lookup_method_invoke(lc,cl,class,name,arg_list)10950 lookup_method_invoke (lc, cl, class, name, arg_list)
10951      int lc;
10952      tree cl;
10953      tree class, name, arg_list;
10954 {
10955   tree atl = end_params_node;		/* Arg Type List */
10956   tree method, signature, list, node;
10957   const char *candidates;		/* Used for error report */
10958   char *dup;
10959 
10960   /* Fix the arguments */
10961   for (node = arg_list; node; node = TREE_CHAIN (node))
10962     {
10963       tree current_arg = TREE_TYPE (TREE_VALUE (node));
10964       /* Non primitive type may have to be resolved */
10965       if (!JPRIMITIVE_TYPE_P (current_arg))
10966 	resolve_and_layout (current_arg, NULL_TREE);
10967       /* And promoted */
10968       if (TREE_CODE (current_arg) == RECORD_TYPE)
10969         current_arg = promote_type (current_arg);
10970       atl = tree_cons (NULL_TREE, current_arg, atl);
10971     }
10972 
10973   /* Presto. If we're dealing with an anonymous class and a
10974      constructor call, generate the right constructor now, since we
10975      know the arguments' types. */
10976 
10977   if (lc && ANONYMOUS_CLASS_P (class))
10978     {
10979       tree saved_current_class;
10980       tree mdecl = craft_constructor (TYPE_NAME (class), atl);
10981       saved_current_class = current_class;
10982       current_class = class;
10983       fix_constructors (mdecl);
10984       current_class = saved_current_class;
10985     }
10986 
10987   /* Find all candidates and then refine the list, searching for the
10988      most specific method. */
10989   list = find_applicable_accessible_methods_list (lc, class, name, atl);
10990   list = find_most_specific_methods_list (list);
10991   if (list && !TREE_CHAIN (list))
10992     return TREE_VALUE (list);
10993 
10994   /* Issue an error. List candidates if any. Candidates are listed
10995      only if accessible (non accessible methods may end-up here for
10996      the sake of a better error report). */
10997   candidates = NULL;
10998   if (list)
10999     {
11000       tree current;
11001       obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
11002       for (current = list; current; current = TREE_CHAIN (current))
11003 	{
11004 	  tree cm = TREE_VALUE (current);
11005 	  char string [4096];
11006 	  if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
11007 	    continue;
11008 	  sprintf
11009 	    (string, "  `%s' in `%s'%s",
11010 	     get_printable_method_name (cm),
11011 	     IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
11012 	     (TREE_CHAIN (current) ? "\n" : ""));
11013 	  obstack_grow (&temporary_obstack, string, strlen (string));
11014 	}
11015       obstack_1grow (&temporary_obstack, '\0');
11016       candidates = obstack_finish (&temporary_obstack);
11017     }
11018   /* Issue the error message */
11019   method = make_node (FUNCTION_TYPE);
11020   TYPE_ARG_TYPES (method) = atl;
11021   signature = build_java_argument_signature (method);
11022   dup = xstrdup (lang_printable_name (class, 0));
11023   parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
11024 		       (lc ? "constructor" : "method"),
11025 		       (lc ? dup : IDENTIFIER_POINTER (name)),
11026 		       IDENTIFIER_POINTER (signature), dup,
11027 		       (candidates ? candidates : ""));
11028   free (dup);
11029   return NULL_TREE;
11030 }
11031 
11032 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
11033    when we're looking for a constructor. */
11034 
11035 static tree
find_applicable_accessible_methods_list(lc,class,name,arglist)11036 find_applicable_accessible_methods_list (lc, class, name, arglist)
11037      int lc;
11038      tree class, name, arglist;
11039 {
11040   static htab_t searched_classes;
11041   static int search_not_done = 0;
11042   tree list = NULL_TREE, all_list = NULL_TREE;
11043 
11044   /* Check the hash table to determine if this class has been searched
11045      already. */
11046   if (searched_classes)
11047     {
11048       if (htab_find (searched_classes, class) != NULL)
11049 	return NULL;
11050     }
11051   else
11052     {
11053       searched_classes = htab_create (10, htab_hash_pointer,
11054 				      htab_eq_pointer, NULL);
11055     }
11056 
11057   search_not_done++;
11058   *htab_find_slot (searched_classes, class, INSERT) = class;
11059 
11060   if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
11061     {
11062       load_class (class, 1);
11063       safe_layout_class (class);
11064     }
11065 
11066   /* Search interfaces */
11067   if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
11068       && CLASS_INTERFACE (TYPE_NAME (class)))
11069     {
11070       int i, n;
11071       tree basetype_vec = TYPE_BINFO_BASETYPES (class);
11072       search_applicable_methods_list (lc, TYPE_METHODS (class),
11073 				      name, arglist, &list, &all_list);
11074       n = TREE_VEC_LENGTH (basetype_vec);
11075       for (i = 1; i < n; i++)
11076 	{
11077 	  tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
11078 	  tree rlist;
11079 
11080 	  rlist = find_applicable_accessible_methods_list (lc,  t, name,
11081 							   arglist);
11082 	  list = chainon (rlist, list);
11083 	}
11084     }
11085   /* Search classes */
11086   else
11087     {
11088       search_applicable_methods_list (lc, TYPE_METHODS (class),
11089 				      name, arglist, &list, &all_list);
11090 
11091       /* When looking finit$, class$ or instinit$, we turn LC to 1 so
11092 	 that we only search in class. Note that we should have found
11093 	 something at this point. */
11094       if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
11095 	{
11096 	  lc = 1;
11097 	  if (!list)
11098 	    abort ();
11099 	}
11100 
11101       /* We must search all interfaces of this class */
11102       if (!lc)
11103       {
11104 	tree basetype_vec = TYPE_BINFO_BASETYPES (class);
11105 	int n = TREE_VEC_LENGTH (basetype_vec), i;
11106 	for (i = 1; i < n; i++)
11107 	  {
11108 	    tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
11109 	    if (t != object_type_node)
11110 	      {
11111 		tree rlist
11112 		  = find_applicable_accessible_methods_list (lc, t,
11113 							     name, arglist);
11114 		list = chainon (rlist, list);
11115 	      }
11116 	  }
11117       }
11118 
11119       /* Search superclass */
11120       if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
11121 	{
11122           tree rlist;
11123           class = CLASSTYPE_SUPER (class);
11124           rlist = find_applicable_accessible_methods_list (lc, class,
11125                                                            name, arglist);
11126           list = chainon (rlist, list);
11127         }
11128     }
11129 
11130   search_not_done--;
11131 
11132   /* We're done. Reset the searched classes list and finally search
11133      java.lang.Object if it wasn't searched already. */
11134   if (!search_not_done)
11135     {
11136       if (!lc
11137 	  && TYPE_METHODS (object_type_node)
11138 	  && htab_find (searched_classes, object_type_node) == NULL)
11139 	{
11140           search_applicable_methods_list (lc,
11141                                           TYPE_METHODS (object_type_node),
11142                                           name, arglist, &list, &all_list);
11143         }
11144       htab_delete (searched_classes);
11145       searched_classes = NULL;
11146     }
11147 
11148   /* Either return the list obtained or all selected (but
11149      inaccessible) methods for better error report. */
11150   return (!list ? all_list : list);
11151 }
11152 
11153 /* Effectively search for the appropriate method in method */
11154 
11155 static void
search_applicable_methods_list(lc,method,name,arglist,list,all_list)11156 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
11157      int lc;
11158      tree method, name, arglist;
11159      tree *list, *all_list;
11160 {
11161   for (; method; method = TREE_CHAIN (method))
11162     {
11163       /* When dealing with constructor, stop here, otherwise search
11164          other classes */
11165       if (lc && !DECL_CONSTRUCTOR_P (method))
11166 	continue;
11167       else if (!lc && (DECL_CONSTRUCTOR_P (method)
11168 		       || (DECL_NAME (method) != name)))
11169 	continue;
11170 
11171       if (argument_types_convertible (method, arglist))
11172 	{
11173 	  /* Retain accessible methods only */
11174 	  if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
11175 				 method, NULL_TREE, 0))
11176 	    *list = tree_cons (NULL_TREE, method, *list);
11177 	  else
11178 	    /* Also retain all selected method here */
11179 	    *all_list = tree_cons (NULL_TREE, method, *list);
11180 	}
11181     }
11182 }
11183 
11184 /* 15.11.2.2 Choose the Most Specific Method */
11185 
11186 static tree
find_most_specific_methods_list(list)11187 find_most_specific_methods_list (list)
11188      tree list;
11189 {
11190   int max = 0;
11191   int abstract, candidates;
11192   tree current, new_list = NULL_TREE;
11193   for (current = list; current; current = TREE_CHAIN (current))
11194     {
11195       tree method;
11196       DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
11197 
11198       for (method = list; method; method = TREE_CHAIN (method))
11199 	{
11200 	  tree method_v, current_v;
11201 	  /* Don't test a method against itself */
11202 	  if (method == current)
11203 	    continue;
11204 
11205 	  method_v = TREE_VALUE (method);
11206 	  current_v = TREE_VALUE (current);
11207 
11208 	  /* Compare arguments and location where methods where declared */
11209 	  if (argument_types_convertible (method_v, current_v))
11210 	    {
11211 	      if (valid_method_invocation_conversion_p
11212 		  (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
11213 		  || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
11214 		      && enclosing_context_p (DECL_CONTEXT (method_v),
11215 					      DECL_CONTEXT (current_v))))
11216 		{
11217 		  int v = (DECL_SPECIFIC_COUNT (current_v) +=
11218 		    (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
11219 		  max = (v > max ? v : max);
11220 		}
11221 	    }
11222 	}
11223     }
11224 
11225   /* Review the list and select the maximally specific methods */
11226   for (current = list, abstract = -1, candidates = -1;
11227        current; current = TREE_CHAIN (current))
11228     if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11229       {
11230 	new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11231 	abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11232 	candidates++;
11233       }
11234 
11235   /* If we have several and they're all abstract, just pick the
11236      closest one. */
11237   if (candidates > 0 && (candidates == abstract))
11238     {
11239       new_list = nreverse (new_list);
11240       TREE_CHAIN (new_list) = NULL_TREE;
11241     }
11242 
11243   /* We have several (we couldn't find a most specific), all but one
11244      are abstract, we pick the only non abstract one. */
11245   if (candidates > 0 && (candidates == abstract+1))
11246     {
11247       for (current = new_list; current; current = TREE_CHAIN (current))
11248 	if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11249 	  {
11250 	    TREE_CHAIN (current) = NULL_TREE;
11251 	    new_list = current;
11252 	  }
11253     }
11254 
11255   /* If we can't find one, lower expectations and try to gather multiple
11256      maximally specific methods */
11257   while (!new_list && max)
11258     {
11259       while (--max > 0)
11260 	{
11261 	  if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11262 	    new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11263 	}
11264     }
11265 
11266   return new_list;
11267 }
11268 
11269 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
11270    converted by method invocation conversion (5.3) to the type of the
11271    corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11272    to change less often than M1. */
11273 
11274 static GTY(()) tree m2_arg_value;
11275 static GTY(()) tree m2_arg_cache;
11276 
11277 static int
argument_types_convertible(m1,m2_or_arglist)11278 argument_types_convertible (m1, m2_or_arglist)
11279     tree m1, m2_or_arglist;
11280 {
11281   register tree m1_arg, m2_arg;
11282 
11283   SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11284 
11285   if (m2_arg_value == m2_or_arglist)
11286     m2_arg = m2_arg_cache;
11287   else
11288     {
11289       /* M2_OR_ARGLIST can be a function DECL or a raw list of
11290          argument types */
11291       if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11292 	{
11293 	  m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11294 	  if (!METHOD_STATIC (m2_or_arglist))
11295 	    m2_arg = TREE_CHAIN (m2_arg);
11296 	}
11297       else
11298 	m2_arg = m2_or_arglist;
11299 
11300       m2_arg_value = m2_or_arglist;
11301       m2_arg_cache = m2_arg;
11302     }
11303 
11304   while (m1_arg != end_params_node && m2_arg != end_params_node)
11305     {
11306       resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11307       if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11308 						 TREE_VALUE (m2_arg)))
11309 	break;
11310       m1_arg = TREE_CHAIN (m1_arg);
11311       m2_arg = TREE_CHAIN (m2_arg);
11312     }
11313   return m1_arg == end_params_node && m2_arg == end_params_node;
11314 }
11315 
11316 /* Qualification routines */
11317 
11318 static void
qualify_ambiguous_name(id)11319 qualify_ambiguous_name (id)
11320      tree id;
11321 {
11322   tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
11323     saved_current_class;
11324   int again, super_found = 0, this_found = 0, new_array_found = 0;
11325   int code;
11326 
11327   /* We first qualify the first element, then derive qualification of
11328      others based on the first one. If the first element is qualified
11329      by a resolution (field or type), this resolution is stored in the
11330      QUAL_RESOLUTION of the qual element being examined. We need to
11331      save the current_class since the use of SUPER might change the
11332      its value. */
11333   saved_current_class = current_class;
11334   qual = EXPR_WFL_QUALIFICATION (id);
11335   do {
11336 
11337     /* Simple qualified expression feature a qual_wfl that is a
11338        WFL. Expression derived from a primary feature more complicated
11339        things like a CALL_EXPR. Expression from primary need to be
11340        worked out to extract the part on which the qualification will
11341        take place. */
11342     qual_wfl = QUAL_WFL (qual);
11343     switch (TREE_CODE (qual_wfl))
11344       {
11345       case CALL_EXPR:
11346 	qual_wfl = TREE_OPERAND (qual_wfl, 0);
11347 	if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION
11348 	    || (EXPR_WFL_QUALIFICATION (qual_wfl)
11349 		&& TREE_CODE (EXPR_WFL_QUALIFICATION (qual_wfl)) == TREE_LIST))
11350 	  {
11351 	    qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11352 	    qual_wfl = QUAL_WFL (qual);
11353 	  }
11354 	break;
11355       case NEW_ARRAY_EXPR:
11356       case NEW_ANONYMOUS_ARRAY_EXPR:
11357 	qual = TREE_CHAIN (qual);
11358 	again = new_array_found = 1;
11359 	continue;
11360       case CONVERT_EXPR:
11361 	break;
11362       case NEW_CLASS_EXPR:
11363 	qual_wfl = TREE_OPERAND (qual_wfl, 0);
11364 	break;
11365       case ARRAY_REF:
11366 	while (TREE_CODE (qual_wfl) == ARRAY_REF)
11367 	  qual_wfl = TREE_OPERAND (qual_wfl, 0);
11368 	break;
11369       case STRING_CST:
11370 	qual = TREE_CHAIN (qual);
11371 	qual_wfl = QUAL_WFL (qual);
11372 	break;
11373       case CLASS_LITERAL:
11374 	qual = TREE_CHAIN (qual);
11375 	qual_wfl = QUAL_WFL (qual);
11376       break;
11377       default:
11378 	/* Fix for -Wall. Just break doing nothing */
11379 	break;
11380       }
11381 
11382     ptr_type = current_class;
11383     again = 0;
11384     code = TREE_CODE (qual_wfl);
11385 
11386     /* Pos evaluation: non WFL leading expression nodes */
11387     if (code == CONVERT_EXPR
11388 	&& TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
11389       name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
11390 
11391     else if (code == INTEGER_CST)
11392       name = qual_wfl;
11393 
11394     else if (code == CONVERT_EXPR &&
11395 	     TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11396       name = TREE_OPERAND (qual_wfl, 0);
11397 
11398     else if (code == CONVERT_EXPR
11399 	     && TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == CALL_EXPR
11400 	     && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (qual_wfl, 0), 0))
11401 		 == EXPR_WITH_FILE_LOCATION))
11402       name = TREE_OPERAND (TREE_OPERAND (qual_wfl, 0), 0);
11403 
11404     else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
11405 	     TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11406       name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
11407 
11408     else if (code == TREE_LIST)
11409       name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
11410 
11411     else if (code == STRING_CST || code == CONDITIONAL_EXPR
11412 	     || code == PLUS_EXPR)
11413       {
11414 	qual = TREE_CHAIN (qual);
11415 	qual_wfl = QUAL_WFL (qual);
11416 	again = 1;
11417       }
11418     else
11419       {
11420 	name = EXPR_WFL_NODE (qual_wfl);
11421 	if (!name)
11422 	  {
11423 	    qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11424 	    again = 1;
11425 	  }
11426       }
11427 
11428     /* If we have a THIS (from a primary), we set the context accordingly */
11429     if (name == this_identifier_node)
11430       {
11431 	/* This isn't really elegant. One more added irregularity
11432 	   before I start using COMPONENT_REF (hopefully very soon.)  */
11433 	if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
11434 	    && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
11435 	       EXPR_WITH_FILE_LOCATION
11436 	    && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
11437 	       this_identifier_node)
11438 	    {
11439 	      qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
11440 	      qual = EXPR_WFL_QUALIFICATION (qual);
11441 	    }
11442 	qual = TREE_CHAIN (qual);
11443 	qual_wfl = QUAL_WFL (qual);
11444 	if (TREE_CODE (qual_wfl) == CALL_EXPR)
11445 	  again = 1;
11446 	else if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION)
11447 	  name = EXPR_WFL_NODE (qual_wfl);
11448 	else if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
11449 	  name = TREE_OPERAND (qual_wfl, 0);
11450 	this_found = 1;
11451       }
11452     /* If we have a SUPER, we set the context accordingly */
11453     if (name == super_identifier_node)
11454       {
11455 	current_class = CLASSTYPE_SUPER (ptr_type);
11456 	/* Check that there is such a thing as a super class. If not,
11457 	   return.  The error will be caught later on, during the
11458 	   resolution */
11459 	if (!current_class)
11460 	  {
11461 	    current_class = saved_current_class;
11462 	    return;
11463 	  }
11464 	qual = TREE_CHAIN (qual);
11465 	/* Do one more interation to set things up */
11466 	super_found = again = 1;
11467       }
11468   } while (again);
11469 
11470   /* If name appears within the scope of a local variable declaration
11471      or parameter declaration, then it is an expression name. We don't
11472      carry this test out if we're in the context of the use of SUPER
11473      or THIS */
11474   if (!this_found && !super_found
11475       && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
11476       && (decl = IDENTIFIER_LOCAL_VALUE (name)))
11477     {
11478       RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11479       QUAL_RESOLUTION (qual) = decl;
11480     }
11481 
11482   /* If within the class/interface NAME was found to be used there
11483      exists a (possibly inherited) field named NAME, then this is an
11484      expression name. If we saw a NEW_ARRAY_EXPR before and want to
11485      address length, it is OK. */
11486   else if ((decl = lookup_field_wrapper (ptr_type, name))
11487 	   || name == length_identifier_node)
11488     {
11489       RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11490       QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
11491     }
11492 
11493   /* We reclassify NAME as yielding to a type name resolution if:
11494      - NAME is a class/interface declared within the compilation
11495        unit containing NAME,
11496      - NAME is imported via a single-type-import declaration,
11497      - NAME is declared in an another compilation unit of the package
11498        of the compilation unit containing NAME,
11499      - NAME is declared by exactly on type-import-on-demand declaration
11500      of the compilation unit containing NAME.
11501      - NAME is actually a STRING_CST.
11502      This can't happen if the expression was qualified by `this.' */
11503   else if (! this_found &&
11504 	   (TREE_CODE (name) == STRING_CST ||
11505 	    TREE_CODE (name) == INTEGER_CST ||
11506 	    (decl = resolve_and_layout (name, NULL_TREE))))
11507     {
11508       RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11509       QUAL_RESOLUTION (qual) = decl;
11510     }
11511 
11512   /* Method call, array references and cast are expression name */
11513   else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
11514 	   || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
11515 	   || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR
11516 	   || TREE_CODE (QUAL_WFL (qual)) == MODIFY_EXPR)
11517     RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11518 
11519   /* Check here that NAME isn't declared by more than one
11520      type-import-on-demand declaration of the compilation unit
11521      containing NAME. FIXME */
11522 
11523   /* Otherwise, NAME is reclassified as a package name */
11524   else
11525     RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11526 
11527   /* Propagate the qualification accross other components of the
11528      qualified name */
11529   for (qual = TREE_CHAIN (qual); qual;
11530        qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11531     {
11532       if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11533 	RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11534       else
11535 	RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
11536     }
11537 
11538   /* Store the global qualification for the ambiguous part of ID back
11539      into ID fields */
11540   if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
11541     RESOLVE_EXPRESSION_NAME_P (id) = 1;
11542   else if (RESOLVE_TYPE_NAME_P (qual_wfl))
11543     RESOLVE_TYPE_NAME_P (id) = 1;
11544   else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11545     RESOLVE_PACKAGE_NAME_P (id) = 1;
11546 
11547   /* Restore the current class */
11548   current_class = saved_current_class;
11549 }
11550 
11551 static int
breakdown_qualified(left,right,source)11552 breakdown_qualified (left, right, source)
11553     tree *left, *right, source;
11554 {
11555   char *p, *base;
11556   int l = IDENTIFIER_LENGTH (source);
11557 
11558   base = alloca (l + 1);
11559   memcpy (base, IDENTIFIER_POINTER (source), l + 1);
11560 
11561   /* Breakdown NAME into REMAINDER . IDENTIFIER.  */
11562   p = base + l - 1;
11563   while (*p != '.' && p != base)
11564     p--;
11565 
11566   /* We didn't find a '.'. Return an error.  */
11567   if (p == base)
11568     return 1;
11569 
11570   *p = '\0';
11571   if (right)
11572     *right = get_identifier (p+1);
11573   *left = get_identifier (base);
11574 
11575   return 0;
11576 }
11577 
11578 /* Return TRUE if two classes are from the same package. */
11579 
11580 static int
in_same_package(name1,name2)11581 in_same_package (name1, name2)
11582   tree name1, name2;
11583 {
11584   tree tmp;
11585   tree pkg1;
11586   tree pkg2;
11587 
11588   if (TREE_CODE (name1) == TYPE_DECL)
11589     name1 = DECL_NAME (name1);
11590   if (TREE_CODE (name2) == TYPE_DECL)
11591     name2 = DECL_NAME (name2);
11592 
11593   if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
11594     /* One in empty package. */
11595     return 0;
11596 
11597   if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
11598     /* Both in empty package. */
11599     return 1;
11600 
11601   breakdown_qualified (&pkg1, &tmp, name1);
11602   breakdown_qualified (&pkg2, &tmp, name2);
11603 
11604   return (pkg1 == pkg2);
11605 }
11606 
11607 /* Patch tree nodes in a function body. When a BLOCK is found, push
11608    local variable decls if present.
11609    Same as java_complete_lhs, but does resolve static finals to values. */
11610 
11611 static tree
java_complete_tree(node)11612 java_complete_tree (node)
11613      tree node;
11614 {
11615   node = java_complete_lhs (node);
11616   if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11617       && DECL_INITIAL (node) != NULL_TREE
11618       && !flag_emit_xref)
11619     {
11620       tree value = fold_constant_for_init (node, node);
11621       if (value != NULL_TREE)
11622 	return value;
11623     }
11624   return node;
11625 }
11626 
11627 static tree
java_stabilize_reference(node)11628 java_stabilize_reference (node)
11629      tree node;
11630 {
11631   if (TREE_CODE (node) == COMPOUND_EXPR)
11632     {
11633       tree op0 = TREE_OPERAND (node, 0);
11634       tree op1 = TREE_OPERAND (node, 1);
11635       TREE_OPERAND (node, 0) = save_expr (op0);
11636       TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11637       return node;
11638     }
11639   return stabilize_reference (node);
11640 }
11641 
11642 /* Patch tree nodes in a function body. When a BLOCK is found, push
11643    local variable decls if present.
11644    Same as java_complete_tree, but does not resolve static finals to values. */
11645 
11646 static tree
java_complete_lhs(node)11647 java_complete_lhs (node)
11648      tree node;
11649 {
11650   tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11651   int flag;
11652 
11653   /* CONVERT_EXPR always has its type set, even though it needs to be
11654      worked out. */
11655   if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11656     return node;
11657 
11658   /* The switch block implements cases processing container nodes
11659      first.  Contained nodes are always written back. Leaves come
11660      next and return a value. */
11661   switch (TREE_CODE (node))
11662     {
11663     case BLOCK:
11664 
11665       /* 1- Block section.
11666 	 Set the local values on decl names so we can identify them
11667 	 faster when they're referenced. At that stage, identifiers
11668 	 are legal so we don't check for declaration errors. */
11669       for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11670 	{
11671 	  DECL_CONTEXT (cn) = current_function_decl;
11672 	  IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11673 	}
11674       if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11675 	  CAN_COMPLETE_NORMALLY (node) = 1;
11676       else
11677 	{
11678 	  tree stmt = BLOCK_EXPR_BODY (node);
11679 	  tree *ptr;
11680 	  int error_seen = 0;
11681 	  if (TREE_CODE (stmt) == COMPOUND_EXPR)
11682 	    {
11683 	      /* Re-order from (((A; B); C); ...; Z) to
11684 		 (A; (B; (C ; (...; Z)))).
11685 		 This makes it easier to scan the statements left-to-right
11686 		 without using recursion (which might overflow the stack
11687 		 if the block has many statements. */
11688 	      for (;;)
11689 		{
11690 		  tree left = TREE_OPERAND (stmt, 0);
11691 		  if (TREE_CODE (left) != COMPOUND_EXPR)
11692 		    break;
11693 		  TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11694 		  TREE_OPERAND (left, 1) = stmt;
11695 		  stmt = left;
11696 		}
11697 	      BLOCK_EXPR_BODY (node) = stmt;
11698 	    }
11699 
11700 	  /* Now do the actual complete, without deep recursion for
11701              long blocks. */
11702 	  ptr = &BLOCK_EXPR_BODY (node);
11703 	  while (TREE_CODE (*ptr) == COMPOUND_EXPR
11704 		 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
11705 	    {
11706 	      tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11707 	      tree *next = &TREE_OPERAND (*ptr, 1);
11708 	      TREE_OPERAND (*ptr, 0) = cur;
11709 	      if (cur == empty_stmt_node)
11710 		{
11711 		  /* Optimization;  makes it easier to detect empty bodies.
11712 		     Most useful for <clinit> with all-constant initializer. */
11713 		  *ptr = *next;
11714 		  continue;
11715 		}
11716 	      if (TREE_CODE (cur) == ERROR_MARK)
11717 		error_seen++;
11718 	      else if (! CAN_COMPLETE_NORMALLY (cur))
11719 		{
11720 		  wfl_op2 = *next;
11721 		  for (;;)
11722 		    {
11723 		      if (TREE_CODE (wfl_op2) == BLOCK)
11724 			wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11725 		      else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11726 			wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11727 		      else
11728 			break;
11729 		    }
11730 		  if (TREE_CODE (wfl_op2) != CASE_EXPR
11731 		      && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11732 		    unreachable_stmt_error (*ptr);
11733 		}
11734 	      ptr = next;
11735 	    }
11736 	  *ptr = java_complete_tree (*ptr);
11737 
11738 	  if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11739 	    return error_mark_node;
11740 	  CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11741 	}
11742       /* Turn local bindings to null */
11743       for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11744 	IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11745 
11746       TREE_TYPE (node) = void_type_node;
11747       break;
11748 
11749       /* 2- They are expressions but ultimately deal with statements */
11750 
11751     case THROW_EXPR:
11752       wfl_op1 = TREE_OPERAND (node, 0);
11753       COMPLETE_CHECK_OP_0 (node);
11754       /* 14.19 A throw statement cannot complete normally. */
11755       CAN_COMPLETE_NORMALLY (node) = 0;
11756       return patch_throw_statement (node, wfl_op1);
11757 
11758     case SYNCHRONIZED_EXPR:
11759       wfl_op1 = TREE_OPERAND (node, 0);
11760       return patch_synchronized_statement (node, wfl_op1);
11761 
11762     case TRY_EXPR:
11763       return patch_try_statement (node);
11764 
11765     case TRY_FINALLY_EXPR:
11766       COMPLETE_CHECK_OP_0 (node);
11767       COMPLETE_CHECK_OP_1 (node);
11768       if (TREE_OPERAND (node, 0) == empty_stmt_node)
11769 	return TREE_OPERAND (node, 1);
11770       if (TREE_OPERAND (node, 1) == empty_stmt_node)
11771 	return TREE_OPERAND (node, 0);
11772       CAN_COMPLETE_NORMALLY (node)
11773 	= (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11774 	   && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11775       TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11776       return node;
11777 
11778     case LABELED_BLOCK_EXPR:
11779       PUSH_LABELED_BLOCK (node);
11780       if (LABELED_BLOCK_BODY (node))
11781 	COMPLETE_CHECK_OP_1 (node);
11782       TREE_TYPE (node) = void_type_node;
11783       POP_LABELED_BLOCK ();
11784 
11785       if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11786 	{
11787 	  LABELED_BLOCK_BODY (node) = NULL_TREE;
11788 	  CAN_COMPLETE_NORMALLY (node) = 1;
11789 	}
11790       else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11791 	CAN_COMPLETE_NORMALLY (node) = 1;
11792       return node;
11793 
11794     case EXIT_BLOCK_EXPR:
11795       /* We don't complete operand 1, because it's the return value of
11796          the EXIT_BLOCK_EXPR which doesn't exist it Java */
11797       return patch_bc_statement (node);
11798 
11799     case CASE_EXPR:
11800       cn = java_complete_tree (TREE_OPERAND (node, 0));
11801       if (cn == error_mark_node)
11802 	return cn;
11803 
11804       /* First, the case expression must be constant. Values of final
11805          fields are accepted. */
11806       cn = fold (cn);
11807       if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11808 	  && JDECL_P (TREE_OPERAND (cn, 1))
11809 	  && FIELD_FINAL (TREE_OPERAND (cn, 1))
11810 	  && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11811 	{
11812 	  cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11813 				       TREE_OPERAND (cn, 1));
11814 	}
11815       /* Accept final locals too. */
11816       else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn))
11817 	cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11818 
11819       if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11820 	{
11821 	  EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11822 	  parse_error_context (node, "Constant expression required");
11823 	  return error_mark_node;
11824 	}
11825 
11826       nn = ctxp->current_loop;
11827 
11828       /* It must be assignable to the type of the switch expression. */
11829       if (!try_builtin_assignconv (NULL_TREE,
11830 				   TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11831 	{
11832 	  EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11833 	  parse_error_context
11834 	    (wfl_operator,
11835 	     "Incompatible type for case. Can't convert `%s' to `int'",
11836 	     lang_printable_name (TREE_TYPE (cn), 0));
11837 	  return error_mark_node;
11838 	}
11839 
11840       cn = fold (convert (int_type_node, cn));
11841       TREE_CONSTANT_OVERFLOW (cn) = 0;
11842       CAN_COMPLETE_NORMALLY (cn) = 1;
11843 
11844       /* Save the label on a list so that we can later check for
11845 	 duplicates.  */
11846       case_label_list = tree_cons (node, cn, case_label_list);
11847 
11848       /* Multiple instance of a case label bearing the same value is
11849 	 checked later. The case expression is all right so far. */
11850       if (TREE_CODE (cn) == VAR_DECL)
11851 	cn = DECL_INITIAL (cn);
11852       TREE_OPERAND (node, 0) = cn;
11853       TREE_TYPE (node) = void_type_node;
11854       CAN_COMPLETE_NORMALLY (node) = 1;
11855       TREE_SIDE_EFFECTS (node) = 1;
11856       break;
11857 
11858     case DEFAULT_EXPR:
11859       nn = ctxp->current_loop;
11860       /* Only one default label is allowed per switch statement */
11861       if (SWITCH_HAS_DEFAULT (nn))
11862 	{
11863 	  EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11864 	  parse_error_context (wfl_operator,
11865 			       "Duplicate case label: `default'");
11866 	  return error_mark_node;
11867 	}
11868       else
11869 	SWITCH_HAS_DEFAULT (nn) = 1;
11870       TREE_TYPE (node) = void_type_node;
11871       TREE_SIDE_EFFECTS (node) = 1;
11872       CAN_COMPLETE_NORMALLY (node) = 1;
11873       break;
11874 
11875     case SWITCH_EXPR:
11876     case LOOP_EXPR:
11877       PUSH_LOOP (node);
11878       /* Check whether the loop was enclosed in a labeled
11879          statement. If not, create one, insert the loop in it and
11880          return the node */
11881       nn = patch_loop_statement (node);
11882 
11883       /* Anyways, walk the body of the loop */
11884       if (TREE_CODE (node) == LOOP_EXPR)
11885 	TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11886       /* Switch statement: walk the switch expression and the cases */
11887       else
11888 	node = patch_switch_statement (node);
11889 
11890       if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11891 	nn = error_mark_node;
11892       else
11893 	{
11894 	  TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11895 	  /* If we returned something different, that's because we
11896 	     inserted a label. Pop the label too. */
11897 	  if (nn != node)
11898 	    {
11899 	      if (CAN_COMPLETE_NORMALLY (node))
11900 		CAN_COMPLETE_NORMALLY (nn) = 1;
11901 	      POP_LABELED_BLOCK ();
11902 	    }
11903 	}
11904       POP_LOOP ();
11905       return nn;
11906 
11907     case EXIT_EXPR:
11908       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11909       return patch_exit_expr (node);
11910 
11911     case COND_EXPR:
11912       /* Condition */
11913       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11914       if (TREE_OPERAND (node, 0) == error_mark_node)
11915 	return error_mark_node;
11916       /* then-else branches */
11917       TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11918       if (TREE_OPERAND (node, 1) == error_mark_node)
11919 	return error_mark_node;
11920       TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11921       if (TREE_OPERAND (node, 2) == error_mark_node)
11922 	return error_mark_node;
11923       return patch_if_else_statement (node);
11924       break;
11925 
11926     case CONDITIONAL_EXPR:
11927       /* Condition */
11928       wfl_op1 = TREE_OPERAND (node, 0);
11929       COMPLETE_CHECK_OP_0 (node);
11930       wfl_op2 = TREE_OPERAND (node, 1);
11931       COMPLETE_CHECK_OP_1 (node);
11932       wfl_op3 = TREE_OPERAND (node, 2);
11933       COMPLETE_CHECK_OP_2 (node);
11934       return patch_conditional_expr (node, wfl_op1, wfl_op2);
11935 
11936       /* 3- Expression section */
11937     case COMPOUND_EXPR:
11938       wfl_op2 = TREE_OPERAND (node, 1);
11939       TREE_OPERAND (node, 0) = nn =
11940 	java_complete_tree (TREE_OPERAND (node, 0));
11941       if (wfl_op2 == empty_stmt_node)
11942 	CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11943       else
11944 	{
11945 	  if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11946 	    {
11947 	      /* An unreachable condition in a do-while statement
11948 		 is *not* (technically) an unreachable statement. */
11949 	      nn = wfl_op2;
11950 	      if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11951 		nn = EXPR_WFL_NODE (nn);
11952 	      /* NN can be NULL_TREE exactly when UPDATE is, in
11953 		 finish_for_loop.  */
11954 	      if (nn != NULL_TREE && TREE_CODE (nn) != EXIT_EXPR)
11955 		{
11956 		  SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11957 		  if (SUPPRESS_UNREACHABLE_ERROR (nn))
11958 		    {
11959 		      /* Perhaps this warning should have an
11960 			 associated flag.  The code being compiled is
11961 			 pedantically correct, but useless.  */
11962 		      parse_warning_context (wfl_operator,
11963 					     "Unreachable statement");
11964 		    }
11965 		  else
11966 		    parse_error_context (wfl_operator,
11967 					 "Unreachable statement");
11968 		}
11969 	    }
11970 	  TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11971 	  if (TREE_OPERAND (node, 1) == error_mark_node)
11972 	    return error_mark_node;
11973 	  /* Even though we might allow the case where the first
11974 	     operand doesn't return normally, we still should compute
11975 	     CAN_COMPLETE_NORMALLY correctly.  */
11976 	  CAN_COMPLETE_NORMALLY (node)
11977 	    = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11978 	       && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11979 	}
11980       TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11981       break;
11982 
11983     case RETURN_EXPR:
11984       /* CAN_COMPLETE_NORMALLY (node) = 0; */
11985       return patch_return (node);
11986 
11987     case EXPR_WITH_FILE_LOCATION:
11988       if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11989 	  || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11990 	{
11991 	  tree wfl = node;
11992 	  node = resolve_expression_name (node, NULL);
11993 	  if (node == error_mark_node)
11994 	    return node;
11995 	  /* Keep line number information somewhere were it doesn't
11996 	     disrupt the completion process. */
11997 	  if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11998 	    {
11999 	      EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
12000 	      TREE_OPERAND (node, 1) = wfl;
12001 	    }
12002 	  CAN_COMPLETE_NORMALLY (node) = 1;
12003 	}
12004       else
12005 	{
12006 	  tree body;
12007 	  int save_lineno = lineno;
12008 	  lineno = EXPR_WFL_LINENO (node);
12009 	  body = java_complete_tree (EXPR_WFL_NODE (node));
12010 	  lineno = save_lineno;
12011 	  EXPR_WFL_NODE (node) = body;
12012 	  TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
12013 	  CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
12014 	  if (body == empty_stmt_node || TREE_CONSTANT (body))
12015 	    {
12016 	      /* Makes it easier to constant fold, detect empty bodies. */
12017 	      return body;
12018 	    }
12019 	  if (body == error_mark_node)
12020 	    {
12021 	      /* Its important for the evaluation of assignment that
12022 		 this mark on the TREE_TYPE is propagated. */
12023 	      TREE_TYPE (node) = error_mark_node;
12024 	      return error_mark_node;
12025 	    }
12026 	  else
12027 	    TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
12028 
12029 	}
12030       break;
12031 
12032     case NEW_ARRAY_EXPR:
12033       /* Patch all the dimensions */
12034       flag = 0;
12035       for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12036 	{
12037 	  int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
12038 	  tree dim = convert (int_type_node,
12039 			      java_complete_tree (TREE_VALUE (cn)));
12040 	  if (dim == error_mark_node)
12041 	    {
12042 	      flag = 1;
12043 	      continue;
12044 	    }
12045 	  else
12046 	    {
12047 	      TREE_VALUE (cn) = dim;
12048 	      /* Setup the location of the current dimension, for
12049 		 later error report. */
12050 	      TREE_PURPOSE (cn) =
12051 		build_expr_wfl (NULL_TREE, input_filename, 0, 0);
12052 	      EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
12053 	    }
12054 	}
12055       /* They complete the array creation expression, if no errors
12056          were found. */
12057       CAN_COMPLETE_NORMALLY (node) = 1;
12058       return (flag ? error_mark_node
12059 	      : force_evaluation_order (patch_newarray (node)));
12060 
12061     case NEW_ANONYMOUS_ARRAY_EXPR:
12062       /* Create the array type if necessary. */
12063       if (ANONYMOUS_ARRAY_DIMS_SIG (node))
12064 	{
12065 	  tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
12066 	  if (!(type = resolve_type_during_patch (type)))
12067 	    return error_mark_node;
12068 	  type = build_array_from_name (type, NULL_TREE,
12069 					ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
12070 	  ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
12071 	}
12072       node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
12073 				   ANONYMOUS_ARRAY_INITIALIZER (node));
12074       if (node == error_mark_node)
12075 	return error_mark_node;
12076       CAN_COMPLETE_NORMALLY (node) = 1;
12077       return node;
12078 
12079     case NEW_CLASS_EXPR:
12080     case CALL_EXPR:
12081       /* Complete function's argument(s) first */
12082       if (complete_function_arguments (node))
12083 	return error_mark_node;
12084       else
12085 	{
12086 	  tree decl, wfl = TREE_OPERAND (node, 0);
12087 	  int in_this = CALL_THIS_CONSTRUCTOR_P (node);
12088 	  int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
12089                            super_identifier_node);
12090 
12091 	  node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
12092 					  from_super, 0, &decl);
12093 	  if (node == error_mark_node)
12094 	    return error_mark_node;
12095 
12096 	  check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
12097 	  /* If we call this(...), register signature and positions */
12098 	  if (in_this)
12099 	    DECL_CONSTRUCTOR_CALLS (current_function_decl) =
12100 	      tree_cons (wfl, decl,
12101 			 DECL_CONSTRUCTOR_CALLS (current_function_decl));
12102 	  CAN_COMPLETE_NORMALLY (node) = 1;
12103 	  return force_evaluation_order (node);
12104 	}
12105 
12106     case MODIFY_EXPR:
12107       /* Save potential wfls */
12108       wfl_op1 = TREE_OPERAND (node, 0);
12109       TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
12110 
12111       if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
12112 	  && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
12113 	  && DECL_INITIAL (nn) != NULL_TREE)
12114 	{
12115 	  tree value;
12116 
12117 	  value = fold_constant_for_init (nn, nn);
12118 
12119 	  /* When we have a primitype type, or a string and we're not
12120              emitting a class file, we actually don't want to generate
12121              anything for the assignment. */
12122 	  if (value != NULL_TREE &&
12123 	      (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) ||
12124 	       (TREE_TYPE (value) == string_ptr_type_node &&
12125 		! flag_emit_class_files)))
12126 	    {
12127 	      /* Prepare node for patch_assignment */
12128 	      TREE_OPERAND (node, 1) = value;
12129 	      /* Call patch assignment to verify the assignment */
12130 	      if (patch_assignment (node, wfl_op1) == error_mark_node)
12131 		return error_mark_node;
12132 	      /* Set DECL_INITIAL properly (a conversion might have
12133                  been decided by patch_assignment) and return the
12134                  empty statement. */
12135 	      else
12136 		{
12137 		  tree patched = patch_string (TREE_OPERAND (node, 1));
12138 		  if (patched)
12139 		    DECL_INITIAL (nn) = patched;
12140 		  else
12141 		    DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12142 		  DECL_FIELD_FINAL_IUD (nn) = 1;
12143 		  return empty_stmt_node;
12144 		}
12145 	    }
12146 	  if (! flag_emit_class_files)
12147 	    DECL_INITIAL (nn) = NULL_TREE;
12148 	}
12149       wfl_op2 = TREE_OPERAND (node, 1);
12150 
12151       if (TREE_OPERAND (node, 0) == error_mark_node)
12152 	return error_mark_node;
12153 
12154       flag = COMPOUND_ASSIGN_P (wfl_op2);
12155       if (flag)
12156 	{
12157 	  /* This might break when accessing outer field from inner
12158              class. TESTME, FIXME */
12159 	  tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
12160 
12161 	  /* Hand stabilize the lhs on both places */
12162 	  TREE_OPERAND (node, 0) = lvalue;
12163 	  TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
12164 	    (flag_emit_class_files ? lvalue : save_expr (lvalue));
12165 
12166 	  /* 15.25.2.a: Left hand is not an array access. FIXME */
12167 	  /* Now complete the RHS. We write it back later on. */
12168 	  nn = java_complete_tree (TREE_OPERAND (node, 1));
12169 
12170 	  if ((cn = patch_string (nn)))
12171 	    nn = cn;
12172 
12173 	  /* The last part of the rewrite for E1 op= E2 is to have
12174 	     E1 = (T)(E1 op E2), with T being the type of E1. */
12175 	  nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
12176 					       TREE_TYPE (lvalue), nn));
12177 
12178 	  /* If the assignment is compound and has reference type,
12179 	     then ensure the LHS has type String and nothing else.  */
12180 	  if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
12181 	      && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
12182 	    parse_error_context (wfl_op2,
12183 				 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
12184 				 lang_printable_name (TREE_TYPE (lvalue), 0));
12185 
12186 	  /* 15.25.2.b: Left hand is an array access. FIXME */
12187 	}
12188 
12189       /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12190 	 function to complete this RHS. Note that a NEW_ARRAY_INIT
12191 	 might have been already fully expanded if created as a result
12192 	 of processing an anonymous array initializer. We avoid doing
12193 	 the operation twice by testing whether the node already bears
12194 	 a type. */
12195       else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12196 	nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12197 				   TREE_OPERAND (node, 1));
12198       /* Otherwise we simply complete the RHS */
12199       else
12200 	nn = java_complete_tree (TREE_OPERAND (node, 1));
12201 
12202       if (nn == error_mark_node)
12203 	return error_mark_node;
12204 
12205       /* Write back the RHS as we evaluated it. */
12206       TREE_OPERAND (node, 1) = nn;
12207 
12208       /* In case we're handling = with a String as a RHS, we need to
12209 	 produce a String out of the RHS (it might still be a
12210 	 STRING_CST or a StringBuffer at this stage */
12211       if ((nn = patch_string (TREE_OPERAND (node, 1))))
12212 	TREE_OPERAND (node, 1) = nn;
12213 
12214       if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12215 					TREE_OPERAND (node, 1))))
12216 	{
12217 	  /* We return error_mark_node if outer_field_access_fix
12218 	     detects we write into a final. */
12219 	  if (nn == error_mark_node)
12220 	    return error_mark_node;
12221 	  node = nn;
12222 	}
12223       else
12224 	{
12225 	  node = patch_assignment (node, wfl_op1);
12226 	  if (node == error_mark_node)
12227 	    return error_mark_node;
12228 	  /* Reorganize the tree if necessary. */
12229 	  if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
12230 		       || JSTRING_P (TREE_TYPE (node))))
12231 	    node = java_refold (node);
12232 	}
12233 
12234       /* Seek to set DECL_INITIAL to a proper value, since it might have
12235 	 undergone a conversion in patch_assignment. We do that only when
12236 	 it's necessary to have DECL_INITIAL properly set. */
12237       nn = TREE_OPERAND (node, 0);
12238       if (TREE_CODE (nn) == VAR_DECL
12239 	  && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12240 	  && FIELD_STATIC (nn) && FIELD_FINAL (nn)
12241 	  && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12242 	      || TREE_TYPE (nn) == string_ptr_type_node))
12243 	DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12244 
12245       CAN_COMPLETE_NORMALLY (node) = 1;
12246       return node;
12247 
12248     case MULT_EXPR:
12249     case PLUS_EXPR:
12250     case MINUS_EXPR:
12251     case LSHIFT_EXPR:
12252     case RSHIFT_EXPR:
12253     case URSHIFT_EXPR:
12254     case BIT_AND_EXPR:
12255     case BIT_XOR_EXPR:
12256     case BIT_IOR_EXPR:
12257     case TRUNC_MOD_EXPR:
12258     case TRUNC_DIV_EXPR:
12259     case RDIV_EXPR:
12260     case TRUTH_ANDIF_EXPR:
12261     case TRUTH_ORIF_EXPR:
12262     case EQ_EXPR:
12263     case NE_EXPR:
12264     case GT_EXPR:
12265     case GE_EXPR:
12266     case LT_EXPR:
12267     case LE_EXPR:
12268       /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12269 	 knows how to handle those cases. */
12270       wfl_op1 = TREE_OPERAND (node, 0);
12271       wfl_op2 = TREE_OPERAND (node, 1);
12272 
12273       CAN_COMPLETE_NORMALLY (node) = 1;
12274       /* Don't complete string nodes if dealing with the PLUS operand. */
12275       if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12276         {
12277           nn = java_complete_tree (wfl_op1);
12278           if (nn == error_mark_node)
12279             return error_mark_node;
12280 
12281           TREE_OPERAND (node, 0) = nn;
12282         }
12283       if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12284         {
12285           nn = java_complete_tree (wfl_op2);
12286           if (nn == error_mark_node)
12287             return error_mark_node;
12288 
12289           TREE_OPERAND (node, 1) = nn;
12290         }
12291       return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
12292 
12293     case INSTANCEOF_EXPR:
12294       wfl_op1 = TREE_OPERAND (node, 0);
12295       COMPLETE_CHECK_OP_0 (node);
12296       if (flag_emit_xref)
12297 	{
12298 	  TREE_TYPE (node) = boolean_type_node;
12299 	  return node;
12300 	}
12301       return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
12302 
12303     case UNARY_PLUS_EXPR:
12304     case NEGATE_EXPR:
12305     case TRUTH_NOT_EXPR:
12306     case BIT_NOT_EXPR:
12307     case PREDECREMENT_EXPR:
12308     case PREINCREMENT_EXPR:
12309     case POSTDECREMENT_EXPR:
12310     case POSTINCREMENT_EXPR:
12311     case CONVERT_EXPR:
12312       /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12313 	 how to handle those cases. */
12314       wfl_op1 = TREE_OPERAND (node, 0);
12315       CAN_COMPLETE_NORMALLY (node) = 1;
12316       TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12317       if (TREE_OPERAND (node, 0) == error_mark_node)
12318 	return error_mark_node;
12319       node = patch_unaryop (node, wfl_op1);
12320       CAN_COMPLETE_NORMALLY (node) = 1;
12321       break;
12322 
12323     case ARRAY_REF:
12324       /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12325 	 how to handle those cases. */
12326       wfl_op1 = TREE_OPERAND (node, 0);
12327       TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12328       if (TREE_OPERAND (node, 0) == error_mark_node)
12329 	return error_mark_node;
12330       if (!flag_emit_class_files && !flag_emit_xref)
12331 	TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12332       /* The same applies to wfl_op2 */
12333       wfl_op2 = TREE_OPERAND (node, 1);
12334       TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12335       if (TREE_OPERAND (node, 1) == error_mark_node)
12336 	return error_mark_node;
12337       if (!flag_emit_class_files && !flag_emit_xref)
12338 	TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12339       return patch_array_ref (node);
12340 
12341     case RECORD_TYPE:
12342       return node;;
12343 
12344     case COMPONENT_REF:
12345       /* The first step in the re-write of qualified name handling.  FIXME.
12346 	 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
12347       TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
12348       if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
12349 	{
12350 	  tree name = TREE_OPERAND (node, 1);
12351 	  tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
12352 	  if (field == NULL_TREE)
12353 	    {
12354 	      error ("missing static field `%s'", IDENTIFIER_POINTER (name));
12355 	      return error_mark_node;
12356 	    }
12357 	  if (! FIELD_STATIC (field))
12358 	    {
12359 	      error ("not a static field `%s'", IDENTIFIER_POINTER (name));
12360 	      return error_mark_node;
12361 	    }
12362 	  return field;
12363 	}
12364       else
12365 	abort ();
12366       break;
12367 
12368     case THIS_EXPR:
12369       /* Can't use THIS in a static environment */
12370       if (!current_this)
12371 	{
12372 	  EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12373 	  parse_error_context (wfl_operator,
12374 			       "Keyword `this' used outside allowed context");
12375 	  TREE_TYPE (node) = error_mark_node;
12376 	  return error_mark_node;
12377 	}
12378       if (ctxp->explicit_constructor_p)
12379 	{
12380 	  EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12381 	  parse_error_context
12382 	    (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
12383 	  TREE_TYPE (node) = error_mark_node;
12384 	  return error_mark_node;
12385 	}
12386       return current_this;
12387 
12388     case CLASS_LITERAL:
12389       CAN_COMPLETE_NORMALLY (node) = 1;
12390       node = patch_incomplete_class_ref (node);
12391       if (node == error_mark_node)
12392 	return error_mark_node;
12393       break;
12394 
12395     default:
12396       CAN_COMPLETE_NORMALLY (node) = 1;
12397       /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12398 	 and it's time to turn it into the appropriate String object */
12399       if ((nn = patch_string (node)))
12400 	node = nn;
12401       else
12402 	internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12403     }
12404   return node;
12405 }
12406 
12407 /* Complete function call's argument. Return a nonzero value is an
12408    error was found.  */
12409 
12410 static int
complete_function_arguments(node)12411 complete_function_arguments (node)
12412      tree node;
12413 {
12414   int flag = 0;
12415   tree cn;
12416 
12417   ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12418   for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12419     {
12420       tree wfl = TREE_VALUE (cn), parm, temp;
12421       parm = java_complete_tree (wfl);
12422 
12423       if (parm == error_mark_node)
12424 	{
12425 	  flag = 1;
12426 	  continue;
12427 	}
12428       /* If have a string literal that we haven't transformed yet or a
12429 	 crafted string buffer, as a result of use of the the String
12430 	 `+' operator. Build `parm.toString()' and expand it. */
12431       if ((temp = patch_string (parm)))
12432 	parm = temp;
12433 
12434       TREE_VALUE (cn) = parm;
12435     }
12436   ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12437   return flag;
12438 }
12439 
12440 /* Sometimes (for loops and variable initialized during their
12441    declaration), we want to wrap a statement around a WFL and turn it
12442    debugable.  */
12443 
12444 static tree
build_debugable_stmt(location,stmt)12445 build_debugable_stmt (location, stmt)
12446     int location;
12447     tree stmt;
12448 {
12449   if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12450     {
12451       stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12452       EXPR_WFL_LINECOL (stmt) = location;
12453     }
12454   JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12455   return stmt;
12456 }
12457 
12458 static tree
build_expr_block(body,decls)12459 build_expr_block (body, decls)
12460      tree body, decls;
12461 {
12462   tree node = make_node (BLOCK);
12463   BLOCK_EXPR_DECLS (node) = decls;
12464   BLOCK_EXPR_BODY (node) = body;
12465   if (body)
12466     TREE_TYPE (node) = TREE_TYPE (body);
12467   TREE_SIDE_EFFECTS (node) = 1;
12468   return node;
12469 }
12470 
12471 /* Create a new function block and link it appropriately to current
12472    function block chain */
12473 
12474 static tree
enter_block()12475 enter_block ()
12476 {
12477   tree b = build_expr_block (NULL_TREE, NULL_TREE);
12478 
12479   /* Link block B supercontext to the previous block. The current
12480      function DECL is used as supercontext when enter_a_block is called
12481      for the first time for a given function. The current function body
12482      (DECL_FUNCTION_BODY) is set to be block B.  */
12483 
12484   tree fndecl = current_function_decl;
12485 
12486   if (!fndecl) {
12487     BLOCK_SUPERCONTEXT (b) = current_static_block;
12488     current_static_block = b;
12489   }
12490 
12491   else if (!DECL_FUNCTION_BODY (fndecl))
12492     {
12493       BLOCK_SUPERCONTEXT (b) = fndecl;
12494       DECL_FUNCTION_BODY (fndecl) = b;
12495     }
12496   else
12497     {
12498       BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12499       DECL_FUNCTION_BODY (fndecl) = b;
12500     }
12501   return b;
12502 }
12503 
12504 /* Exit a block by changing the current function body
12505    (DECL_FUNCTION_BODY) to the current block super context, only if
12506    the block being exited isn't the method's top level one.  */
12507 
12508 static tree
exit_block()12509 exit_block ()
12510 {
12511   tree b;
12512   if (current_function_decl)
12513     {
12514       b = DECL_FUNCTION_BODY (current_function_decl);
12515       if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12516 	DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12517     }
12518   else
12519     {
12520       b = current_static_block;
12521 
12522       if (BLOCK_SUPERCONTEXT (b))
12523 	current_static_block = BLOCK_SUPERCONTEXT (b);
12524     }
12525   return b;
12526 }
12527 
12528 /* Lookup for NAME in the nested function's blocks, all the way up to
12529    the current toplevel one. It complies with Java's local variable
12530    scoping rules.  */
12531 
12532 static tree
lookup_name_in_blocks(name)12533 lookup_name_in_blocks (name)
12534      tree name;
12535 {
12536   tree b = GET_CURRENT_BLOCK (current_function_decl);
12537 
12538   while (b != current_function_decl)
12539     {
12540       tree current;
12541 
12542       /* Paranoid sanity check. To be removed */
12543       if (TREE_CODE (b) != BLOCK)
12544 	abort ();
12545 
12546       for (current = BLOCK_EXPR_DECLS (b); current;
12547 	   current = TREE_CHAIN (current))
12548 	if (DECL_NAME (current) == name)
12549 	  return current;
12550       b = BLOCK_SUPERCONTEXT (b);
12551     }
12552   return NULL_TREE;
12553 }
12554 
12555 static void
maybe_absorb_scoping_blocks()12556 maybe_absorb_scoping_blocks ()
12557 {
12558   while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12559     {
12560       tree b = exit_block ();
12561       java_method_add_stmt (current_function_decl, b);
12562       SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
12563     }
12564 }
12565 
12566 
12567 /* This section of the source is reserved to build_* functions that
12568    are building incomplete tree nodes and the patch_* functions that
12569    are completing them.  */
12570 
12571 /* Wrap a non WFL node around a WFL.  */
12572 
12573 static tree
build_wfl_wrap(node,location)12574 build_wfl_wrap (node, location)
12575     tree node;
12576     int location;
12577 {
12578   tree wfl, node_to_insert = node;
12579 
12580   /* We want to process THIS . xxx symbolicaly, to keep it consistent
12581      with the way we're processing SUPER. A THIS from a primary as a
12582      different form than a SUPER. Turn THIS into something symbolic */
12583   if (TREE_CODE (node) == THIS_EXPR)
12584     node_to_insert = wfl = build_wfl_node (this_identifier_node);
12585   else
12586     wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12587 
12588   EXPR_WFL_LINECOL (wfl) = location;
12589   EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12590   return wfl;
12591 }
12592 
12593 /* Build a super() constructor invocation. Returns empty_stmt_node if
12594    we're currently dealing with the class java.lang.Object. */
12595 
12596 static tree
build_super_invocation(mdecl)12597 build_super_invocation (mdecl)
12598      tree mdecl;
12599 {
12600   if (DECL_CONTEXT (mdecl) == object_type_node)
12601     return empty_stmt_node;
12602   else
12603     {
12604       tree super_wfl = build_wfl_node (super_identifier_node);
12605       tree a = NULL_TREE, t;
12606       /* If we're dealing with an anonymous class, pass the arguments
12607          of the crafted constructor along. */
12608       if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12609 	{
12610 	  SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12611 	  for (; t != end_params_node; t = TREE_CHAIN (t))
12612 	    a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12613 	}
12614       return build_method_invocation (super_wfl, a);
12615     }
12616 }
12617 
12618 /* Build a SUPER/THIS qualified method invocation.  */
12619 
12620 static tree
build_this_super_qualified_invocation(use_this,name,args,lloc,rloc)12621 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
12622      int use_this;
12623      tree name, args;
12624      int lloc, rloc;
12625 {
12626   tree invok;
12627   tree wfl =
12628     build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12629   EXPR_WFL_LINECOL (wfl) = lloc;
12630   invok = build_method_invocation (name, args);
12631   return make_qualified_primary (wfl, invok, rloc);
12632 }
12633 
12634 /* Build an incomplete CALL_EXPR node. */
12635 
12636 static tree
build_method_invocation(name,args)12637 build_method_invocation (name, args)
12638     tree name;
12639     tree args;
12640 {
12641   tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12642   TREE_SIDE_EFFECTS (call) = 1;
12643   EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12644   return call;
12645 }
12646 
12647 /* Build an incomplete new xxx(...) node. */
12648 
12649 static tree
build_new_invocation(name,args)12650 build_new_invocation (name, args)
12651     tree name, args;
12652 {
12653   tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12654   TREE_SIDE_EFFECTS (call) = 1;
12655   EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12656   return call;
12657 }
12658 
12659 /* Build an incomplete assignment expression. */
12660 
12661 static tree
build_assignment(op,op_location,lhs,rhs)12662 build_assignment (op, op_location, lhs, rhs)
12663      int op, op_location;
12664      tree lhs, rhs;
12665 {
12666   tree assignment;
12667   /* Build the corresponding binop if we deal with a Compound
12668      Assignment operator. Mark the binop sub-tree as part of a
12669      Compound Assignment expression */
12670   if (op != ASSIGN_TK)
12671     {
12672       rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12673       COMPOUND_ASSIGN_P (rhs) = 1;
12674     }
12675   assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12676   TREE_SIDE_EFFECTS (assignment) = 1;
12677   EXPR_WFL_LINECOL (assignment) = op_location;
12678   return assignment;
12679 }
12680 
12681 /* Print an INTEGER_CST node as decimal in a static buffer, and return
12682    the buffer.  This is used only for string conversion.  */
12683 static char *
string_convert_int_cst(node)12684 string_convert_int_cst (node)
12685      tree node;
12686 {
12687   /* Long.MIN_VALUE is -9223372036854775808, 20 characters.  */
12688   static char buffer[21];
12689 
12690   unsigned HOST_WIDE_INT lo = TREE_INT_CST_LOW (node);
12691   unsigned HOST_WIDE_INT hi = TREE_INT_CST_HIGH (node);
12692   char *p = buffer + sizeof (buffer);
12693   int neg = 0;
12694 
12695   unsigned HOST_WIDE_INT hibit = (((unsigned HOST_WIDE_INT) 1)
12696 				  << (HOST_BITS_PER_WIDE_INT - 1));
12697 
12698   *--p = '\0';
12699 
12700   /* If negative, note the fact and negate the value.  */
12701   if ((hi & hibit))
12702     {
12703       lo = ~lo;
12704       hi = ~hi;
12705       if (++lo == 0)
12706 	++hi;
12707       neg = 1;
12708     }
12709 
12710   /* Divide by 10 until there are no bits left.  */
12711   do
12712     {
12713       unsigned HOST_WIDE_INT acc = 0;
12714       unsigned HOST_WIDE_INT outhi = 0, outlo = 0;
12715       unsigned int i;
12716 
12717       /* Use long division to compute the result and the remainder.  */
12718       for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; ++i)
12719 	{
12720 	  /* Shift a bit into accumulator.  */
12721 	  acc <<= 1;
12722 	  if ((hi & hibit))
12723 	    acc |= 1;
12724 
12725 	  /* Shift the value.  */
12726 	  hi <<= 1;
12727 	  if ((lo & hibit))
12728 	    hi |= 1;
12729 	  lo <<= 1;
12730 
12731 	  /* Shift the correct bit into the result.  */
12732 	  outhi <<= 1;
12733 	  if ((outlo & hibit))
12734 	    outhi |= 1;
12735 	  outlo <<= 1;
12736 	  if (acc >= 10)
12737 	    {
12738 	      acc -= 10;
12739 	      outlo |= 1;
12740 	    }
12741 	}
12742 
12743       /* '0' == 060 in Java, but might not be here (think EBCDIC).  */
12744       *--p = '\060' + acc;
12745 
12746       hi = outhi;
12747       lo = outlo;
12748     }
12749   while (hi || lo);
12750 
12751   if (neg)
12752     *--p = '\055'; /* '-' == 055 in Java, but might not be here.  */
12753 
12754   return p;
12755 }
12756 
12757 /* Print an INTEGER_CST node in a static buffer, and return the
12758    buffer.  This is used only for error handling.  */
12759 char *
print_int_node(node)12760 print_int_node (node)
12761     tree node;
12762 {
12763   static char buffer [80];
12764   if (TREE_CONSTANT_OVERFLOW (node))
12765     sprintf (buffer, "<overflow>");
12766 
12767   if (TREE_INT_CST_HIGH (node) == 0)
12768     sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12769 	     TREE_INT_CST_LOW (node));
12770   else if (TREE_INT_CST_HIGH (node) == -1
12771 	   && TREE_INT_CST_LOW (node) != 0)
12772     {
12773       buffer [0] = '-';
12774       sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12775 	       -TREE_INT_CST_LOW (node));
12776     }
12777   else
12778     sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12779 	     TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12780 
12781   return buffer;
12782 }
12783 
12784 
12785 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12786    context.  */
12787 
12788 /* 15.25 Assignment operators. */
12789 
12790 static tree
patch_assignment(node,wfl_op1)12791 patch_assignment (node, wfl_op1)
12792      tree node;
12793      tree wfl_op1;
12794 {
12795   tree rhs = TREE_OPERAND (node, 1);
12796   tree lvalue = TREE_OPERAND (node, 0), llvalue;
12797   tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12798   int error_found = 0;
12799   int lvalue_from_array = 0;
12800   int is_return = 0;
12801 
12802   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12803 
12804   /* Lhs can be a named variable */
12805   if (JDECL_P (lvalue))
12806     {
12807       lhs_type = TREE_TYPE (lvalue);
12808     }
12809   /* Or Lhs can be an array access. */
12810   else if (TREE_CODE (lvalue) == ARRAY_REF)
12811     {
12812       lhs_type = TREE_TYPE (lvalue);
12813       lvalue_from_array = 1;
12814     }
12815   /* Or a field access */
12816   else if (TREE_CODE (lvalue) == COMPONENT_REF)
12817     lhs_type = TREE_TYPE (lvalue);
12818   /* Or a function return slot */
12819   else if (TREE_CODE (lvalue) == RESULT_DECL)
12820     {
12821       /* If the return type is an integral type, then we create the
12822 	 RESULT_DECL with a promoted type, but we need to do these
12823 	 checks against the unpromoted type to ensure type safety.  So
12824 	 here we look at the real type, not the type of the decl we
12825 	 are modifying.  */
12826       lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12827       is_return = 1;
12828     }
12829   /* Otherwise, we might want to try to write into an optimized static
12830      final, this is an of a different nature, reported further on. */
12831   else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12832 	   && resolve_expression_name (wfl_op1, &llvalue))
12833     {
12834       lhs_type = TREE_TYPE (lvalue);
12835     }
12836   else
12837     {
12838       parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12839       error_found = 1;
12840     }
12841 
12842   rhs_type = TREE_TYPE (rhs);
12843 
12844   /* 5.1 Try the assignment conversion for builtin type. */
12845   new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12846 
12847   /* 5.2 If it failed, try a reference conversion */
12848   if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12849     lhs_type = promote_type (rhs_type);
12850 
12851   /* 15.25.2 If we have a compound assignment, convert RHS into the
12852      type of the LHS */
12853   else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12854     new_rhs = convert (lhs_type, rhs);
12855 
12856   /* Explicit cast required. This is an error */
12857   if (!new_rhs)
12858     {
12859       char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12860       char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12861       tree wfl;
12862       char operation [32];	/* Max size known */
12863 
12864       /* If the assignment is part of a declaration, we use the WFL of
12865 	 the declared variable to point out the error and call it a
12866 	 declaration problem. If the assignment is a genuine =
12867 	 operator, we call is a operator `=' problem, otherwise we
12868 	 call it an assignment problem. In both of these last cases,
12869 	 we use the WFL of the operator to indicate the error. */
12870 
12871       if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12872 	{
12873 	  wfl = wfl_op1;
12874 	  strcpy (operation, "declaration");
12875 	}
12876       else
12877 	{
12878 	  wfl = wfl_operator;
12879 	  if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12880 	    strcpy (operation, "assignment");
12881 	  else if (is_return)
12882 	    strcpy (operation, "`return'");
12883 	  else
12884 	    strcpy (operation, "`='");
12885 	}
12886 
12887       if (!valid_cast_to_p (rhs_type, lhs_type))
12888 	parse_error_context
12889 	  (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12890 	   operation, t1, t2);
12891       else
12892 	parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12893 			     operation, t1, t2);
12894       free (t1); free (t2);
12895       error_found = 1;
12896     }
12897 
12898   if (error_found)
12899     return error_mark_node;
12900 
12901   /* If we're processing a `return' statement, promote the actual type
12902      to the promoted type.  */
12903   if (is_return)
12904     new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12905 
12906   /* 10.10: Array Store Exception runtime check */
12907   if (!flag_emit_class_files
12908       && !flag_emit_xref
12909       && lvalue_from_array
12910       && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12911     {
12912       tree array, store_check, base, index_expr;
12913 
12914       /* Save RHS so that it doesn't get re-evaluated by the store check. */
12915       new_rhs = save_expr (new_rhs);
12916 
12917       /* Get the INDIRECT_REF. */
12918       array = TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0);
12919       /* Get the array pointer expr. */
12920       array = TREE_OPERAND (array, 0);
12921       store_check = build_java_arraystore_check (array, new_rhs);
12922 
12923       index_expr = TREE_OPERAND (lvalue, 1);
12924 
12925       if (TREE_CODE (index_expr) == COMPOUND_EXPR)
12926 	{
12927 	  /* A COMPOUND_EXPR here is a bounds check. The bounds check must
12928 	     happen before the store check, so prepare to insert the store
12929 	     check within the second operand of the existing COMPOUND_EXPR. */
12930 	  base = index_expr;
12931 	}
12932       else
12933         base = lvalue;
12934 
12935       index_expr = TREE_OPERAND (base, 1);
12936       TREE_OPERAND (base, 1) = build (COMPOUND_EXPR, TREE_TYPE (index_expr),
12937 	  			      store_check, index_expr);
12938     }
12939 
12940   /* Final locals can be used as case values in switch
12941      statement. Prepare them for this eventuality. */
12942   if (TREE_CODE (lvalue) == VAR_DECL
12943       && DECL_FINAL (lvalue)
12944       && TREE_CONSTANT (new_rhs)
12945       && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12946       && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12947       )
12948     {
12949       TREE_CONSTANT (lvalue) = 1;
12950       DECL_INITIAL (lvalue) = new_rhs;
12951     }
12952 
12953   /* Copy the rhs if it's a reference.  */
12954   if (! flag_check_references && ! flag_emit_class_files && optimize > 0)
12955     {
12956       switch (TREE_CODE (new_rhs))
12957 	{
12958 	case ARRAY_REF:
12959 	case INDIRECT_REF:
12960 	case COMPONENT_REF:
12961 	  /* Transform a = foo.bar
12962 	     into a = { int tmp; tmp = foo.bar; tmp; ).
12963 	     We need to ensure that if a read from memory fails
12964 	     because of a NullPointerException, a destination variable
12965 	     will remain unchanged.  An explicit temporary does what
12966 	     we need.
12967 
12968 	     If flag_check_references is set, this is unnecessary
12969 	     because we'll check each reference before doing any
12970 	     reads.  If optimize is not set the result will never be
12971 	     written to a stack slot that contains the LHS.  */
12972 	  {
12973 	    tree tmp = build_decl (VAR_DECL, get_identifier ("<tmp>"),
12974 				   TREE_TYPE (new_rhs));
12975 	    tree block = build (BLOCK, TREE_TYPE (new_rhs), NULL);
12976 	    tree assignment
12977 	      = build (MODIFY_EXPR, TREE_TYPE (new_rhs), tmp, fold (new_rhs));
12978 	    BLOCK_VARS (block) = tmp;
12979 	    BLOCK_EXPR_BODY (block)
12980 	      = build (COMPOUND_EXPR, TREE_TYPE (new_rhs), assignment, tmp);
12981 	    TREE_SIDE_EFFECTS (block) = 1;
12982 	    new_rhs = block;
12983 	  }
12984 	  break;
12985 	default:
12986 	  break;
12987 	}
12988     }
12989 
12990   TREE_OPERAND (node, 0) = lvalue;
12991   TREE_OPERAND (node, 1) = new_rhs;
12992   TREE_TYPE (node) = lhs_type;
12993   return node;
12994 }
12995 
12996 /* Check that type SOURCE can be cast into type DEST. If the cast
12997    can't occur at all, return NULL; otherwise, return a possibly
12998    modified rhs.  */
12999 
13000 static tree
try_reference_assignconv(lhs_type,rhs)13001 try_reference_assignconv (lhs_type, rhs)
13002      tree lhs_type, rhs;
13003 {
13004   tree new_rhs = NULL_TREE;
13005   tree rhs_type = TREE_TYPE (rhs);
13006 
13007   if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
13008     {
13009       /* `null' may be assigned to any reference type */
13010       if (rhs == null_pointer_node)
13011         new_rhs = null_pointer_node;
13012       /* Try the reference assignment conversion */
13013       else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
13014 	new_rhs = rhs;
13015       /* This is a magic assignment that we process differently */
13016       else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
13017 	new_rhs = rhs;
13018     }
13019   return new_rhs;
13020 }
13021 
13022 /* Check that RHS can be converted into LHS_TYPE by the assignment
13023    conversion (5.2), for the cases of RHS being a builtin type. Return
13024    NULL_TREE if the conversion fails or if because RHS isn't of a
13025    builtin type. Return a converted RHS if the conversion is possible.  */
13026 
13027 static tree
try_builtin_assignconv(wfl_op1,lhs_type,rhs)13028 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
13029      tree wfl_op1, lhs_type, rhs;
13030 {
13031   tree new_rhs = NULL_TREE;
13032   tree rhs_type = TREE_TYPE (rhs);
13033 
13034   /* Handle boolean specially.  */
13035   if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13036       || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13037     {
13038       if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
13039 	  && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
13040 	new_rhs = rhs;
13041     }
13042 
13043   /* 5.1.1 Try Identity Conversion,
13044      5.1.2 Try Widening Primitive Conversion */
13045   else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
13046     new_rhs = convert (lhs_type, rhs);
13047 
13048   /* Try a narrowing primitive conversion (5.1.3):
13049        - expression is a constant expression of type byte, short, char,
13050          or int, AND
13051        - variable is byte, short or char AND
13052        - The value of the expression is representable in the type of the
13053          variable */
13054   else if ((rhs_type == byte_type_node || rhs_type == short_type_node
13055 	    || rhs_type == char_type_node || rhs_type == int_type_node)
13056 	    && TREE_CONSTANT (rhs)
13057 	   && (lhs_type == byte_type_node || lhs_type == char_type_node
13058 	       || lhs_type == short_type_node))
13059     {
13060       if (int_fits_type_p (rhs, lhs_type))
13061         new_rhs = convert (lhs_type, rhs);
13062       else if (wfl_op1)		/* Might be called with a NULL */
13063 	parse_warning_context
13064 	  (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
13065 	   print_int_node (rhs), lang_printable_name (lhs_type, 0));
13066       /* Reported a warning that will turn into an error further
13067 	 down, so we don't return */
13068     }
13069 
13070   return new_rhs;
13071 }
13072 
13073 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
13074    conversion (5.1.1) or widening primitive conversion (5.1.2).  Return
13075    0 is the conversion test fails.  This implements parts the method
13076    invocation convertion (5.3).  */
13077 
13078 static int
valid_builtin_assignconv_identity_widening_p(lhs_type,rhs_type)13079 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
13080      tree lhs_type, rhs_type;
13081 {
13082   /* 5.1.1: This is the identity conversion part. */
13083   if (lhs_type == rhs_type)
13084     return 1;
13085 
13086   /* Reject non primitive types and boolean conversions.  */
13087   if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
13088     return 0;
13089 
13090   /* 5.1.2: widening primitive conversion. byte, even if it's smaller
13091      than a char can't be converted into a char. Short can't too, but
13092      the < test below takes care of that */
13093   if (lhs_type == char_type_node && rhs_type == byte_type_node)
13094     return 0;
13095 
13096   /* Accept all promoted type here. Note, we can't use <= in the test
13097      below, because we still need to bounce out assignments of short
13098      to char and the likes */
13099   if (lhs_type == int_type_node
13100       && (rhs_type == promoted_byte_type_node
13101 	  || rhs_type == promoted_short_type_node
13102 	  || rhs_type == promoted_char_type_node
13103 	  || rhs_type == promoted_boolean_type_node))
13104     return 1;
13105 
13106   /* From here, an integral is widened if its precision is smaller
13107      than the precision of the LHS or if the LHS is a floating point
13108      type, or the RHS is a float and the RHS a double. */
13109   if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
13110        && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
13111       || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
13112       || (rhs_type == float_type_node && lhs_type == double_type_node))
13113     return 1;
13114 
13115   return 0;
13116 }
13117 
13118 /* Check that something of SOURCE type can be assigned or cast to
13119    something of DEST type at runtime. Return 1 if the operation is
13120    valid, 0 otherwise. If CAST is set to 1, we're treating the case
13121    were SOURCE is cast into DEST, which borrows a lot of the
13122    assignment check. */
13123 
13124 static int
valid_ref_assignconv_cast_p(source,dest,cast)13125 valid_ref_assignconv_cast_p (source, dest, cast)
13126      tree source;
13127      tree dest;
13128      int cast;
13129 {
13130   /* SOURCE or DEST might be null if not from a declared entity. */
13131   if (!source || !dest)
13132     return 0;
13133   if (JNULLP_TYPE_P (source))
13134     return 1;
13135   if (TREE_CODE (source) == POINTER_TYPE)
13136     source = TREE_TYPE (source);
13137   if (TREE_CODE (dest) == POINTER_TYPE)
13138     dest = TREE_TYPE (dest);
13139 
13140   /* If source and dest are being compiled from bytecode, they may need to
13141      be loaded. */
13142   if (CLASS_P (source) && !CLASS_LOADED_P (source))
13143     {
13144       load_class (source, 1);
13145       safe_layout_class (source);
13146     }
13147   if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
13148     {
13149       load_class (dest, 1);
13150       safe_layout_class (dest);
13151     }
13152 
13153   /* Case where SOURCE is a class type */
13154   if (TYPE_CLASS_P (source))
13155     {
13156       if (TYPE_CLASS_P (dest))
13157 	return  (source == dest
13158 		 || inherits_from_p (source, dest)
13159 		 || (cast && inherits_from_p (dest, source)));
13160       if (TYPE_INTERFACE_P (dest))
13161 	{
13162 	  /* If doing a cast and SOURCE is final, the operation is
13163              always correct a compile time (because even if SOURCE
13164              does not implement DEST, a subclass of SOURCE might). */
13165 	  if (cast && !CLASS_FINAL (TYPE_NAME (source)))
13166 	    return 1;
13167 	  /* Otherwise, SOURCE must implement DEST */
13168 	  return interface_of_p (dest, source);
13169 	}
13170       /* DEST is an array, cast permited if SOURCE is of Object type */
13171       return (cast && source == object_type_node ? 1 : 0);
13172     }
13173   if (TYPE_INTERFACE_P (source))
13174     {
13175       if (TYPE_CLASS_P (dest))
13176 	{
13177 	  /* If not casting, DEST must be the Object type */
13178 	  if (!cast)
13179 	    return dest == object_type_node;
13180 	  /* We're doing a cast. The cast is always valid is class
13181 	     DEST is not final, otherwise, DEST must implement SOURCE */
13182 	  else if (!CLASS_FINAL (TYPE_NAME (dest)))
13183 	    return 1;
13184 	  else
13185 	    return interface_of_p (source, dest);
13186 	}
13187       if (TYPE_INTERFACE_P (dest))
13188 	{
13189 	  /* If doing a cast, then if SOURCE and DEST contain method
13190              with the same signature but different return type, then
13191              this is a (compile time) error */
13192 	  if (cast)
13193 	    {
13194 	      tree method_source, method_dest;
13195 	      tree source_type;
13196 	      tree source_sig;
13197 	      tree source_name;
13198 	      for (method_source = TYPE_METHODS (source); method_source;
13199 		   method_source = TREE_CHAIN (method_source))
13200 		{
13201 		  source_sig =
13202 		    build_java_argument_signature (TREE_TYPE (method_source));
13203 		  source_type = TREE_TYPE (TREE_TYPE (method_source));
13204 		  source_name = DECL_NAME (method_source);
13205 		  for (method_dest = TYPE_METHODS (dest);
13206 		       method_dest; method_dest = TREE_CHAIN (method_dest))
13207 		    if (source_sig ==
13208 			build_java_argument_signature (TREE_TYPE (method_dest))
13209 			&& source_name == DECL_NAME (method_dest)
13210 			&& source_type != TREE_TYPE (TREE_TYPE (method_dest)))
13211 		      return 0;
13212 		}
13213 	      return 1;
13214 	    }
13215 	  else
13216 	    return source == dest || interface_of_p (dest, source);
13217 	}
13218       else
13219 	{
13220 	  /* Array */
13221 	  return (cast
13222 		  && (DECL_NAME (TYPE_NAME (source))
13223 		      == java_lang_cloneable_identifier_node
13224 		      || (DECL_NAME (TYPE_NAME (source))
13225 			  == java_io_serializable_identifier_node)));
13226 	}
13227     }
13228   if (TYPE_ARRAY_P (source))
13229     {
13230       if (TYPE_CLASS_P (dest))
13231 	return dest == object_type_node;
13232       /* Can't cast an array to an interface unless the interface is
13233 	 java.lang.Cloneable or java.io.Serializable.  */
13234       if (TYPE_INTERFACE_P (dest))
13235 	return (DECL_NAME (TYPE_NAME (dest))
13236 		== java_lang_cloneable_identifier_node
13237 		|| (DECL_NAME (TYPE_NAME (dest))
13238 		    == java_io_serializable_identifier_node));
13239       else			/* Arrays */
13240 	{
13241 	  tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13242 	  tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13243 
13244 	  /* In case of severe errors, they turn out null */
13245 	  if (!dest_element_type || !source_element_type)
13246 	    return 0;
13247 	  if (source_element_type == dest_element_type)
13248 	    return 1;
13249 	  return valid_ref_assignconv_cast_p (source_element_type,
13250 					      dest_element_type, cast);
13251 	}
13252       return 0;
13253     }
13254   return 0;
13255 }
13256 
13257 static int
valid_cast_to_p(source,dest)13258 valid_cast_to_p (source, dest)
13259      tree source;
13260      tree dest;
13261 {
13262   if (TREE_CODE (source) == POINTER_TYPE)
13263     source = TREE_TYPE (source);
13264   if (TREE_CODE (dest) == POINTER_TYPE)
13265     dest = TREE_TYPE (dest);
13266 
13267   if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13268     return valid_ref_assignconv_cast_p (source, dest, 1);
13269 
13270   else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13271     return 1;
13272 
13273   else if (TREE_CODE (source) == BOOLEAN_TYPE
13274 	   && TREE_CODE (dest) == BOOLEAN_TYPE)
13275     return 1;
13276 
13277   return 0;
13278 }
13279 
13280 static tree
do_unary_numeric_promotion(arg)13281 do_unary_numeric_promotion (arg)
13282      tree arg;
13283 {
13284   tree type = TREE_TYPE (arg);
13285   if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13286       || TREE_CODE (type) == CHAR_TYPE)
13287     arg = convert (int_type_node, arg);
13288   return arg;
13289 }
13290 
13291 /* Return a nonzero value if SOURCE can be converted into DEST using
13292    the method invocation conversion rule (5.3).  */
13293 static int
valid_method_invocation_conversion_p(dest,source)13294 valid_method_invocation_conversion_p (dest, source)
13295      tree dest, source;
13296 {
13297   return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13298 	   && valid_builtin_assignconv_identity_widening_p (dest, source))
13299 	  || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13300 	      && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13301 	      && valid_ref_assignconv_cast_p (source, dest, 0)));
13302 }
13303 
13304 /* Build an incomplete binop expression. */
13305 
13306 static tree
build_binop(op,op_location,op1,op2)13307 build_binop (op, op_location, op1, op2)
13308      enum tree_code op;
13309      int op_location;
13310      tree op1, op2;
13311 {
13312   tree binop = build (op, NULL_TREE, op1, op2);
13313   TREE_SIDE_EFFECTS (binop) = 1;
13314   /* Store the location of the operator, for better error report. The
13315      string of the operator will be rebuild based on the OP value. */
13316   EXPR_WFL_LINECOL (binop) = op_location;
13317   return binop;
13318 }
13319 
13320 /* Build the string of the operator retained by NODE. If NODE is part
13321    of a compound expression, add an '=' at the end of the string. This
13322    function is called when an error needs to be reported on an
13323    operator. The string is returned as a pointer to a static character
13324    buffer. */
13325 
13326 static char *
operator_string(node)13327 operator_string (node)
13328      tree node;
13329 {
13330 #define BUILD_OPERATOR_STRING(S)					\
13331   {									\
13332     sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : ""));	\
13333     return buffer;							\
13334   }
13335 
13336   static char buffer [10];
13337   switch (TREE_CODE (node))
13338     {
13339     case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13340     case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13341     case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13342     case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13343     case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13344     case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13345     case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13346     case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13347     case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13348     case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13349     case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13350     case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13351     case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13352     case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13353     case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13354     case GT_EXPR: BUILD_OPERATOR_STRING (">");
13355     case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13356     case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13357     case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13358     case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13359     case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13360     case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13361     case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13362     case PREINCREMENT_EXPR:	/* Fall through */
13363     case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13364     case PREDECREMENT_EXPR:	/* Fall through */
13365     case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13366     default:
13367       internal_error ("unregistered operator %s",
13368 		      tree_code_name [TREE_CODE (node)]);
13369     }
13370   return NULL;
13371 #undef BUILD_OPERATOR_STRING
13372 }
13373 
13374 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2.  */
13375 
13376 static int
java_decl_equiv(var_acc1,var_acc2)13377 java_decl_equiv (var_acc1, var_acc2)
13378      tree var_acc1, var_acc2;
13379 {
13380   if (JDECL_P (var_acc1))
13381     return (var_acc1 == var_acc2);
13382 
13383   return (TREE_CODE (var_acc1) == COMPONENT_REF
13384 	  && TREE_CODE (var_acc2) == COMPONENT_REF
13385 	  && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13386 	     == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13387 	  && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13388 }
13389 
13390 /* Return a nonzero value if CODE is one of the operators that can be
13391    used in conjunction with the `=' operator in a compound assignment.  */
13392 
13393 static int
binop_compound_p(code)13394 binop_compound_p (code)
13395     enum tree_code code;
13396 {
13397   int i;
13398   for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13399     if (binop_lookup [i] == code)
13400       break;
13401 
13402   return i < BINOP_COMPOUND_CANDIDATES;
13403 }
13404 
13405 /* Reorganize after a fold to get SAVE_EXPR to generate what we want.  */
13406 
13407 static tree
java_refold(t)13408 java_refold (t)
13409      tree t;
13410 {
13411   tree c, b, ns, decl;
13412 
13413   if (TREE_CODE (t) != MODIFY_EXPR)
13414     return t;
13415 
13416   c = TREE_OPERAND (t, 1);
13417   if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13418 	 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13419 	 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13420     return t;
13421 
13422   /* Now the left branch of the binary operator. */
13423   b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13424   if (! (b && TREE_CODE (b) == NOP_EXPR
13425 	 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13426     return t;
13427 
13428   ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13429   if (! (ns && TREE_CODE (ns) == NOP_EXPR
13430 	 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13431     return t;
13432 
13433   decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13434   if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13435       /* It's got to be the an equivalent decl */
13436       && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13437     {
13438       /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13439       TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13440       /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13441       TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13442       /* Change the right part of the BINOP_EXPR */
13443       TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13444     }
13445 
13446   return t;
13447 }
13448 
13449 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13450    errors but we modify NODE so that it contains the type computed
13451    according to the expression, when it's fixed. Otherwise, we write
13452    error_mark_node as the type. It allows us to further the analysis
13453    of remaining nodes and detects more errors in certain cases.  */
13454 
13455 static tree
patch_binop(node,wfl_op1,wfl_op2)13456 patch_binop (node, wfl_op1, wfl_op2)
13457      tree node;
13458      tree wfl_op1;
13459      tree wfl_op2;
13460 {
13461   tree op1 = TREE_OPERAND (node, 0);
13462   tree op2 = TREE_OPERAND (node, 1);
13463   tree op1_type = TREE_TYPE (op1);
13464   tree op2_type = TREE_TYPE (op2);
13465   tree prom_type = NULL_TREE, cn;
13466   enum tree_code code = TREE_CODE (node);
13467 
13468   /* If 1, tell the routine that we have to return error_mark_node
13469      after checking for the initialization of the RHS */
13470   int error_found = 0;
13471 
13472   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13473 
13474   /* If either op<n>_type are NULL, this might be early signs of an
13475      error situation, unless it's too early to tell (in case we're
13476      handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13477      correctly so the error can be later on reported accurately. */
13478   if (! (code == PLUS_EXPR || code == NE_EXPR
13479 	 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13480     {
13481       tree n;
13482       if (! op1_type)
13483 	{
13484 	  n = java_complete_tree (op1);
13485 	  op1_type = TREE_TYPE (n);
13486 	}
13487       if (! op2_type)
13488 	{
13489 	  n = java_complete_tree (op2);
13490 	  op2_type = TREE_TYPE (n);
13491 	}
13492     }
13493 
13494   switch (code)
13495     {
13496     /* 15.16 Multiplicative operators */
13497     case MULT_EXPR:		/* 15.16.1 Multiplication Operator * */
13498     case RDIV_EXPR:		/* 15.16.2 Division Operator / */
13499     case TRUNC_DIV_EXPR:	/* 15.16.2 Integral type Division Operator / */
13500     case TRUNC_MOD_EXPR:	/* 15.16.3 Remainder operator % */
13501       if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13502 	{
13503 	  if (!JNUMERIC_TYPE_P (op1_type))
13504 	    ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13505 	  if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13506 	    ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13507 	  TREE_TYPE (node) = error_mark_node;
13508 	  error_found = 1;
13509 	  break;
13510 	}
13511       prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13512 
13513       /* Detect integral division by zero */
13514       if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13515 	  && TREE_CODE (prom_type) == INTEGER_TYPE
13516 	  && (op2 == integer_zero_node || op2 == long_zero_node ||
13517 	      (TREE_CODE (op2) == INTEGER_CST &&
13518 	       ! TREE_INT_CST_LOW (op2)  && ! TREE_INT_CST_HIGH (op2))))
13519 	{
13520 	  parse_warning_context (wfl_operator, "Evaluating this expression will result in an arithmetic exception being thrown");
13521 	  TREE_CONSTANT (node) = 0;
13522 	}
13523 
13524       /* Change the division operator if necessary */
13525       if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13526 	TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13527 
13528       /* Before divisions as is disapear, try to simplify and bail if
13529          applicable, otherwise we won't perform even simple
13530          simplifications like (1-1)/3. We can't do that with floating
13531          point number, folds can't handle them at this stage. */
13532       if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13533 	  && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13534 	{
13535 	  TREE_TYPE (node) = prom_type;
13536 	  node = fold (node);
13537 	  if (TREE_CODE (node) != code)
13538 	    return node;
13539 	}
13540 
13541       if (TREE_CODE (prom_type) == INTEGER_TYPE
13542 	  && flag_use_divide_subroutine
13543 	  && ! flag_emit_class_files
13544 	  && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13545 	return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13546 
13547       /* This one is more complicated. FLOATs are processed by a
13548 	 function call to soft_fmod. Duplicate the value of the
13549 	 COMPOUND_ASSIGN_P flag. */
13550       if (code == TRUNC_MOD_EXPR)
13551 	{
13552 	  tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13553 	  COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13554 	  TREE_SIDE_EFFECTS (mod)
13555 	    = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13556 	  return mod;
13557 	}
13558       break;
13559 
13560     /* 15.17 Additive Operators */
13561     case PLUS_EXPR:		/* 15.17.1 String Concatenation Operator + */
13562 
13563       /* Operation is valid if either one argument is a string
13564 	 constant, a String object or a StringBuffer crafted for the
13565 	 purpose of the a previous usage of the String concatenation
13566 	 operator */
13567 
13568       if (TREE_CODE (op1) == STRING_CST
13569 	  || TREE_CODE (op2) == STRING_CST
13570 	  || JSTRING_TYPE_P (op1_type)
13571 	  || JSTRING_TYPE_P (op2_type)
13572 	  || IS_CRAFTED_STRING_BUFFER_P (op1)
13573 	  || IS_CRAFTED_STRING_BUFFER_P (op2))
13574 	return build_string_concatenation (op1, op2);
13575 
13576     case MINUS_EXPR:		/* 15.17.2 Additive Operators (+ and -) for
13577 				   Numeric Types */
13578       if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13579 	{
13580 	  if (!JNUMERIC_TYPE_P (op1_type))
13581 	    ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13582 	  if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13583 	    ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13584 	  TREE_TYPE (node) = error_mark_node;
13585 	  error_found = 1;
13586 	  break;
13587 	}
13588       prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13589       break;
13590 
13591     /* 15.18 Shift Operators */
13592     case LSHIFT_EXPR:
13593     case RSHIFT_EXPR:
13594     case URSHIFT_EXPR:
13595       if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13596 	{
13597 	  if (!JINTEGRAL_TYPE_P (op1_type))
13598 	    ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13599 	  else
13600 	    {
13601 	      if (JNUMERIC_TYPE_P (op2_type))
13602 		parse_error_context (wfl_operator,
13603 				     "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
13604 				     operator_string (node),
13605 				     lang_printable_name (op2_type, 0));
13606 	      else
13607 		parse_error_context (wfl_operator,
13608 				     "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
13609 				     operator_string (node),
13610 				     lang_printable_name (op2_type, 0));
13611 	    }
13612 	  TREE_TYPE (node) = error_mark_node;
13613 	  error_found = 1;
13614 	  break;
13615 	}
13616 
13617       /* Unary numeric promotion (5.6.1) is performed on each operand
13618          separately */
13619       op1 = do_unary_numeric_promotion (op1);
13620       op2 = do_unary_numeric_promotion (op2);
13621 
13622       /* If the right hand side is of type `long', first cast it to
13623 	 `int'.  */
13624       if (TREE_TYPE (op2) == long_type_node)
13625 	op2 = build1 (CONVERT_EXPR, int_type_node, op2);
13626 
13627       /* The type of the shift expression is the type of the promoted
13628          type of the left-hand operand */
13629       prom_type = TREE_TYPE (op1);
13630 
13631       /* Shift int only up to 0x1f and long up to 0x3f */
13632       if (prom_type == int_type_node)
13633 	op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13634 			   build_int_2 (0x1f, 0)));
13635       else
13636 	op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13637 			   build_int_2 (0x3f, 0)));
13638 
13639       /* The >>> operator is a >> operating on unsigned quantities */
13640       if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13641 	{
13642 	  tree to_return;
13643           tree utype = java_unsigned_type (prom_type);
13644           op1 = convert (utype, op1);
13645 	  TREE_SET_CODE (node, RSHIFT_EXPR);
13646           TREE_OPERAND (node, 0) = op1;
13647           TREE_OPERAND (node, 1) = op2;
13648           TREE_TYPE (node) = utype;
13649 	  to_return = convert (prom_type, node);
13650 	  /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13651 	  COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13652 	  TREE_SIDE_EFFECTS (to_return)
13653 	    = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13654 	  return to_return;
13655 	}
13656       break;
13657 
13658       /* 15.19.1 Type Comparison Operator instaceof */
13659     case INSTANCEOF_EXPR:
13660 
13661       TREE_TYPE (node) = boolean_type_node;
13662 
13663       /* OP1_TYPE might be NULL when OP1 is a string constant.  */
13664       if ((cn = patch_string (op1)))
13665 	{
13666 	  op1 = cn;
13667 	  op1_type = TREE_TYPE (op1);
13668 	}
13669       if (op1_type == NULL_TREE)
13670 	abort ();
13671 
13672       if (!(op2_type = resolve_type_during_patch (op2)))
13673 	return error_mark_node;
13674 
13675       /* The first operand must be a reference type or the null type */
13676       if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13677 	error_found = 1;	/* Error reported further below */
13678 
13679       /* The second operand must be a reference type */
13680       if (!JREFERENCE_TYPE_P (op2_type))
13681 	{
13682 	  SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13683 	  parse_error_context
13684 	    (wfl_operator, "Invalid argument `%s' for `instanceof'",
13685 	     lang_printable_name (op2_type, 0));
13686 	  error_found = 1;
13687 	}
13688 
13689       if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13690 	{
13691 	  /* If the first operand is null, the result is always false */
13692 	  if (op1 == null_pointer_node)
13693 	    return boolean_false_node;
13694 	  else if (flag_emit_class_files)
13695 	    {
13696 	      TREE_OPERAND (node, 1) = op2_type;
13697 	      TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13698 	      return node;
13699 	    }
13700 	  /* Otherwise we have to invoke instance of to figure it out */
13701 	  else
13702 	    return build_instanceof (op1, op2_type);
13703 	}
13704       /* There is no way the expression operand can be an instance of
13705 	 the type operand. This is a compile time error. */
13706       else
13707 	{
13708 	  char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13709 	  SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13710 	  parse_error_context
13711 	    (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13712 	     t1, lang_printable_name (op2_type, 0));
13713 	  free (t1);
13714 	  error_found = 1;
13715 	}
13716 
13717       break;
13718 
13719       /* 15.21 Bitwise and Logical Operators */
13720     case BIT_AND_EXPR:
13721     case BIT_XOR_EXPR:
13722     case BIT_IOR_EXPR:
13723       if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13724 	/* Binary numeric promotion is performed on both operand and the
13725 	   expression retain that type */
13726 	prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13727 
13728       else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13729 	       && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13730 	/* The type of the bitwise operator expression is BOOLEAN */
13731 	prom_type = boolean_type_node;
13732       else
13733 	{
13734 	  if (!JINTEGRAL_TYPE_P (op1_type))
13735 	    ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13736 	  if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13737 	    ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13738 	  TREE_TYPE (node) = error_mark_node;
13739 	  error_found = 1;
13740 	  /* Insert a break here if adding thing before the switch's
13741              break for this case */
13742 	}
13743       break;
13744 
13745       /* 15.22 Conditional-And Operator */
13746     case TRUTH_ANDIF_EXPR:
13747       /* 15.23 Conditional-Or Operator */
13748     case TRUTH_ORIF_EXPR:
13749       /* Operands must be of BOOLEAN type */
13750       if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13751 	  TREE_CODE (op2_type) != BOOLEAN_TYPE)
13752 	{
13753 	  if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13754 	    ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13755 	  if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13756 	    ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13757 	  TREE_TYPE (node) = boolean_type_node;
13758 	  error_found = 1;
13759 	  break;
13760 	}
13761       else if (integer_zerop (op1))
13762 	{
13763 	  return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13764 	}
13765       else if (integer_onep (op1))
13766 	{
13767 	  return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13768 	}
13769       /* The type of the conditional operators is BOOLEAN */
13770       prom_type = boolean_type_node;
13771       break;
13772 
13773       /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13774     case LT_EXPR:
13775     case GT_EXPR:
13776     case LE_EXPR:
13777     case GE_EXPR:
13778       /* The type of each of the operands must be a primitive numeric
13779          type */
13780       if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13781 	{
13782 	  if (!JNUMERIC_TYPE_P (op1_type))
13783 	    ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13784 	  if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13785 	    ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13786 	  TREE_TYPE (node) = boolean_type_node;
13787 	  error_found = 1;
13788 	  break;
13789 	}
13790       /* Binary numeric promotion is performed on the operands */
13791       binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13792       /* The type of the relation expression is always BOOLEAN */
13793       prom_type = boolean_type_node;
13794       break;
13795 
13796       /* 15.20 Equality Operator */
13797     case EQ_EXPR:
13798     case NE_EXPR:
13799       /* It's time for us to patch the strings. */
13800       if ((cn = patch_string (op1)))
13801        {
13802          op1 = cn;
13803          op1_type = TREE_TYPE (op1);
13804        }
13805       if ((cn = patch_string (op2)))
13806        {
13807          op2 = cn;
13808          op2_type = TREE_TYPE (op2);
13809        }
13810 
13811       /* 15.20.1 Numerical Equality Operators == and != */
13812       /* Binary numeric promotion is performed on the operands */
13813       if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13814 	binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13815 
13816       /* 15.20.2 Boolean Equality Operators == and != */
13817       else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13818 	  TREE_CODE (op2_type) == BOOLEAN_TYPE)
13819 	;			/* Nothing to do here */
13820 
13821       /* 15.20.3 Reference Equality Operators == and != */
13822       /* Types have to be either references or the null type. If
13823          they're references, it must be possible to convert either
13824          type to the other by casting conversion. */
13825       else if (op1 == null_pointer_node || op2 == null_pointer_node
13826 	       || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13827 		   && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13828 		       || valid_ref_assignconv_cast_p (op2_type,
13829 						       op1_type, 1))))
13830 	;			/* Nothing to do here */
13831 
13832       /* Else we have an error figure what can't be converted into
13833 	 what and report the error */
13834       else
13835 	{
13836 	  char *t1;
13837 	  t1 = xstrdup (lang_printable_name (op1_type, 0));
13838 	  parse_error_context
13839 	    (wfl_operator,
13840 	     "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13841 	     operator_string (node), t1,
13842 	     lang_printable_name (op2_type, 0));
13843 	  free (t1);
13844 	  TREE_TYPE (node) = boolean_type_node;
13845 	  error_found = 1;
13846 	  break;
13847 	}
13848       prom_type = boolean_type_node;
13849       break;
13850     default:
13851       abort ();
13852     }
13853 
13854   if (error_found)
13855     return error_mark_node;
13856 
13857   TREE_OPERAND (node, 0) = op1;
13858   TREE_OPERAND (node, 1) = op2;
13859   TREE_TYPE (node) = prom_type;
13860   TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13861 
13862   if (flag_emit_xref)
13863     return node;
13864 
13865   /* fold does not respect side-effect order as required for Java but not C.
13866    * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13867    * bytecode.
13868    */
13869   if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13870       : ! TREE_SIDE_EFFECTS (node))
13871     node = fold (node);
13872   return node;
13873 }
13874 
13875 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13876    zero value, the value of CSTE comes after the valude of STRING */
13877 
13878 static tree
do_merge_string_cste(cste,string,string_len,after)13879 do_merge_string_cste (cste, string, string_len, after)
13880      tree cste;
13881      const char *string;
13882      int string_len, after;
13883 {
13884   const char *old = TREE_STRING_POINTER (cste);
13885   int old_len = TREE_STRING_LENGTH (cste);
13886   int len = old_len + string_len;
13887   char *new = alloca (len+1);
13888 
13889   if (after)
13890     {
13891       memcpy (new, string, string_len);
13892       memcpy (&new [string_len], old, old_len);
13893     }
13894   else
13895     {
13896       memcpy (new, old, old_len);
13897       memcpy (&new [old_len], string, string_len);
13898     }
13899   new [len] = '\0';
13900   return build_string (len, new);
13901 }
13902 
13903 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13904    new STRING_CST on success, NULL_TREE on failure.  */
13905 
13906 static tree
merge_string_cste(op1,op2,after)13907 merge_string_cste (op1, op2, after)
13908      tree op1, op2;
13909      int after;
13910 {
13911   /* Handle two string constants right away.  */
13912   if (TREE_CODE (op2) == STRING_CST)
13913     return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13914 				 TREE_STRING_LENGTH (op2), after);
13915 
13916   /* Reasonable integer constant can be treated right away.  */
13917   if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13918     {
13919       static const char *const boolean_true = "true";
13920       static const char *const boolean_false = "false";
13921       static const char *const null_pointer = "null";
13922       char ch[4];
13923       const char *string;
13924 
13925       if (op2 == boolean_true_node)
13926 	string = boolean_true;
13927       else if (op2 == boolean_false_node)
13928 	string = boolean_false;
13929       else if (op2 == null_pointer_node)
13930 	/* FIXME: null is not a compile-time constant, so it is only safe to
13931 	   merge if the overall expression is non-constant. However, this
13932 	   code always merges without checking the overall expression.  */
13933 	string = null_pointer;
13934       else if (TREE_TYPE (op2) == char_type_node)
13935 	{
13936 	  /* Convert the character into UTF-8.	*/
13937 	  unsigned int c = (unsigned int) TREE_INT_CST_LOW (op2);
13938 	  unsigned char *p = (unsigned char *) ch;
13939 	  if (0x01 <= c && c <= 0x7f)
13940 	    *p++ = (unsigned char) c;
13941 	  else if (c < 0x7ff)
13942 	    {
13943 	      *p++ = (unsigned char) (c >> 6 | 0xc0);
13944 	      *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13945 	    }
13946 	  else
13947 	    {
13948 	      *p++ = (unsigned char) (c >> 12 | 0xe0);
13949 	      *p++ = (unsigned char) (((c >> 6) & 0x3f) | 0x80);
13950 	      *p++ = (unsigned char) ((c & 0x3f) | 0x80);
13951 	    }
13952 	  *p = '\0';
13953 
13954 	  string = ch;
13955 	}
13956       else
13957 	string = string_convert_int_cst (op2);
13958 
13959       return do_merge_string_cste (op1, string, strlen (string), after);
13960     }
13961   return NULL_TREE;
13962 }
13963 
13964 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13965    has to be a STRING_CST and the other part must be a STRING_CST or a
13966    INTEGRAL constant. Return a new STRING_CST if the operation
13967    succeed, NULL_TREE otherwise.
13968 
13969    If the case we want to optimize for space, we might want to return
13970    NULL_TREE for each invocation of this routine. FIXME */
13971 
13972 static tree
string_constant_concatenation(op1,op2)13973 string_constant_concatenation (op1, op2)
13974      tree op1, op2;
13975 {
13976   if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13977     {
13978       tree string, rest;
13979       int invert;
13980 
13981       string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13982       rest   = (string == op1 ? op2 : op1);
13983       invert = (string == op1 ? 0 : 1 );
13984 
13985       /* Walk REST, only if it looks reasonable */
13986       if (TREE_CODE (rest) != STRING_CST
13987 	  && !IS_CRAFTED_STRING_BUFFER_P (rest)
13988 	  && !JSTRING_TYPE_P (TREE_TYPE (rest))
13989 	  && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13990 	{
13991 	  rest = java_complete_tree (rest);
13992 	  if (rest == error_mark_node)
13993 	    return error_mark_node;
13994 	  rest = fold (rest);
13995 	}
13996       return merge_string_cste (string, rest, invert);
13997     }
13998   return NULL_TREE;
13999 }
14000 
14001 /* Implement the `+' operator. Does static optimization if possible,
14002    otherwise create (if necessary) and append elements to a
14003    StringBuffer. The StringBuffer will be carried around until it is
14004    used for a function call or an assignment. Then toString() will be
14005    called on it to turn it into a String object. */
14006 
14007 static tree
build_string_concatenation(op1,op2)14008 build_string_concatenation (op1, op2)
14009      tree op1, op2;
14010 {
14011   tree result;
14012   int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
14013 
14014   if (flag_emit_xref)
14015     return build (PLUS_EXPR, string_type_node, op1, op2);
14016 
14017   /* Try to do some static optimization */
14018   if ((result = string_constant_concatenation (op1, op2)))
14019     return result;
14020 
14021   /* Discard empty strings on either side of the expression */
14022   if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
14023     {
14024       op1 = op2;
14025       op2 = NULL_TREE;
14026     }
14027   else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
14028     op2 = NULL_TREE;
14029 
14030   /* If operands are string constant, turn then into object references */
14031   if (TREE_CODE (op1) == STRING_CST)
14032     op1 = patch_string_cst (op1);
14033   if (op2 && TREE_CODE (op2) == STRING_CST)
14034     op2 = patch_string_cst (op2);
14035 
14036   /* If either one of the constant is null and the other non null
14037      operand is a String constant, return it. */
14038   if ((TREE_CODE (op1) == STRING_CST) && !op2)
14039     return op1;
14040 
14041   /* If OP1 isn't already a StringBuffer, create and
14042      initialize a new one */
14043   if (!IS_CRAFTED_STRING_BUFFER_P (op1))
14044     {
14045       /* Two solutions here:
14046 	 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
14047 	 2) OP1 is something else, we call new StringBuffer().append(OP1).  */
14048       if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
14049 	op1 = BUILD_STRING_BUFFER (op1);
14050       else
14051 	{
14052 	  tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
14053 	  op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
14054 	}
14055     }
14056 
14057   if (op2)
14058     {
14059       /* OP1 is no longer the last node holding a crafted StringBuffer */
14060       IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
14061       /* Create a node for `{new...,xxx}.append (op2)' */
14062       if (op2)
14063 	op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
14064     }
14065 
14066   /* Mark the last node holding a crafted StringBuffer */
14067   IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
14068 
14069   TREE_SIDE_EFFECTS (op1) = side_effects;
14070   return op1;
14071 }
14072 
14073 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
14074    StringBuffer. If no string were found to be patched, return
14075    NULL. */
14076 
14077 static tree
patch_string(node)14078 patch_string (node)
14079     tree node;
14080 {
14081   if (node == error_mark_node)
14082     return error_mark_node;
14083   if (TREE_CODE (node) == STRING_CST)
14084     return patch_string_cst (node);
14085   else if (IS_CRAFTED_STRING_BUFFER_P (node))
14086     {
14087       int saved = ctxp->explicit_constructor_p;
14088       tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
14089       tree ret;
14090       /* Temporary disable forbid the use of `this'. */
14091       ctxp->explicit_constructor_p = 0;
14092       ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
14093       /* String concatenation arguments must be evaluated in order too. */
14094       ret = force_evaluation_order (ret);
14095       /* Restore it at its previous value */
14096       ctxp->explicit_constructor_p = saved;
14097       return ret;
14098     }
14099   return NULL_TREE;
14100 }
14101 
14102 /* Build the internal representation of a string constant.  */
14103 
14104 static tree
patch_string_cst(node)14105 patch_string_cst (node)
14106      tree node;
14107 {
14108   int location;
14109   if (! flag_emit_class_files)
14110     {
14111       node = get_identifier (TREE_STRING_POINTER (node));
14112       location = alloc_name_constant (CONSTANT_String, node);
14113       node = build_ref_from_constant_pool (location);
14114     }
14115   TREE_TYPE (node) = string_ptr_type_node;
14116   TREE_CONSTANT (node) = 1;
14117   return node;
14118 }
14119 
14120 /* Build an incomplete unary operator expression. */
14121 
14122 static tree
build_unaryop(op_token,op_location,op1)14123 build_unaryop (op_token, op_location, op1)
14124      int op_token, op_location;
14125      tree op1;
14126 {
14127   enum tree_code op;
14128   tree unaryop;
14129   switch (op_token)
14130     {
14131     case PLUS_TK: op = UNARY_PLUS_EXPR; break;
14132     case MINUS_TK: op = NEGATE_EXPR; break;
14133     case NEG_TK: op = TRUTH_NOT_EXPR; break;
14134     case NOT_TK: op = BIT_NOT_EXPR; break;
14135     default: abort ();
14136     }
14137 
14138   unaryop = build1 (op, NULL_TREE, op1);
14139   TREE_SIDE_EFFECTS (unaryop) = 1;
14140   /* Store the location of the operator, for better error report. The
14141      string of the operator will be rebuild based on the OP value. */
14142   EXPR_WFL_LINECOL (unaryop) = op_location;
14143   return unaryop;
14144 }
14145 
14146 /* Special case for the ++/-- operators, since they require an extra
14147    argument to build, which is set to NULL and patched
14148    later. IS_POST_P is 1 if the operator, 0 otherwise.  */
14149 
14150 static tree
build_incdec(op_token,op_location,op1,is_post_p)14151 build_incdec (op_token, op_location, op1, is_post_p)
14152      int op_token, op_location;
14153      tree op1;
14154      int is_post_p;
14155 {
14156   static const enum tree_code lookup [2][2] =
14157     {
14158       { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
14159       { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
14160     };
14161   tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
14162 		     NULL_TREE, op1, NULL_TREE);
14163   TREE_SIDE_EFFECTS (node) = 1;
14164   /* Store the location of the operator, for better error report. The
14165      string of the operator will be rebuild based on the OP value. */
14166   EXPR_WFL_LINECOL (node) = op_location;
14167   return node;
14168 }
14169 
14170 /* Build an incomplete cast operator, based on the use of the
14171    CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
14172    set. java_complete_tree is trained to walk a CONVERT_EXPR even
14173    though its type is already set.  */
14174 
14175 static tree
build_cast(location,type,exp)14176 build_cast (location, type, exp)
14177      int location;
14178      tree type, exp;
14179 {
14180   tree node = build1 (CONVERT_EXPR, type, exp);
14181   EXPR_WFL_LINECOL (node) = location;
14182   return node;
14183 }
14184 
14185 /* Build an incomplete class reference operator.  */
14186 static tree
build_incomplete_class_ref(location,class_name)14187 build_incomplete_class_ref (location, class_name)
14188     int location;
14189     tree class_name;
14190 {
14191   tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
14192   EXPR_WFL_LINECOL (node) = location;
14193   return node;
14194 }
14195 
14196 /* Complete an incomplete class reference operator.  */
14197 static tree
patch_incomplete_class_ref(node)14198 patch_incomplete_class_ref (node)
14199     tree node;
14200 {
14201   tree type = TREE_OPERAND (node, 0);
14202   tree ref_type;
14203 
14204   if (!(ref_type = resolve_type_during_patch (type)))
14205     return error_mark_node;
14206 
14207   if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type)
14208       || TREE_CODE (ref_type) == VOID_TYPE)
14209     {
14210       tree dot = build_class_ref (ref_type);
14211       /* A class referenced by `foo.class' is initialized.  */
14212       if (!flag_emit_class_files)
14213        dot = build_class_init (ref_type, dot);
14214       return java_complete_tree (dot);
14215     }
14216 
14217   /* If we're emitting class files and we have to deal with non
14218      primitive types, we invoke (and consider generating) the
14219      synthetic static method `class$'. */
14220   if (!TYPE_DOT_CLASS (current_class))
14221       build_dot_class_method (current_class);
14222   ref_type = build_dot_class_method_invocation (ref_type);
14223   return java_complete_tree (ref_type);
14224 }
14225 
14226 /* 15.14 Unary operators. We return error_mark_node in case of error,
14227    but preserve the type of NODE if the type is fixed.  */
14228 
14229 static tree
patch_unaryop(node,wfl_op)14230 patch_unaryop (node, wfl_op)
14231      tree node;
14232      tree wfl_op;
14233 {
14234   tree op = TREE_OPERAND (node, 0);
14235   tree op_type = TREE_TYPE (op);
14236   tree prom_type = NULL_TREE, value, decl;
14237   int outer_field_flag = 0;
14238   int code = TREE_CODE (node);
14239   int error_found = 0;
14240 
14241   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14242 
14243   switch (code)
14244     {
14245       /* 15.13.2 Postfix Increment Operator ++ */
14246     case POSTINCREMENT_EXPR:
14247       /* 15.13.3 Postfix Increment Operator -- */
14248     case POSTDECREMENT_EXPR:
14249       /* 15.14.1 Prefix Increment Operator ++ */
14250     case PREINCREMENT_EXPR:
14251       /* 15.14.2 Prefix Decrement Operator -- */
14252     case PREDECREMENT_EXPR:
14253       op = decl = strip_out_static_field_access_decl (op);
14254       outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
14255       /* We might be trying to change an outer field accessed using
14256          access method. */
14257       if (outer_field_flag)
14258 	{
14259 	  /* Retrieve the decl of the field we're trying to access. We
14260              do that by first retrieving the function we would call to
14261              access the field. It has been already verified that this
14262              field isn't final */
14263 	  if (flag_emit_class_files)
14264 	    decl = TREE_OPERAND (op, 0);
14265 	  else
14266 	    decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
14267 	  decl = DECL_FUNCTION_ACCESS_DECL (decl);
14268 	}
14269       /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14270       else if (!JDECL_P (decl)
14271 	  && TREE_CODE (decl) != COMPONENT_REF
14272 	  && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14273 	  && TREE_CODE (decl) != INDIRECT_REF
14274 	  && !(TREE_CODE (decl) == COMPOUND_EXPR
14275 	       && TREE_OPERAND (decl, 1)
14276 	       && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14277 	{
14278 	  TREE_TYPE (node) = error_mark_node;
14279 	  error_found = 1;
14280 	}
14281 
14282       /* From now on, we know that op if a variable and that it has a
14283          valid wfl. We use wfl_op to locate errors related to the
14284          ++/-- operand. */
14285       if (!JNUMERIC_TYPE_P (op_type))
14286 	{
14287 	  parse_error_context
14288 	    (wfl_op, "Invalid argument type `%s' to `%s'",
14289 	     lang_printable_name (op_type, 0), operator_string (node));
14290 	  TREE_TYPE (node) = error_mark_node;
14291 	  error_found = 1;
14292 	}
14293       else
14294 	{
14295 	  /* Before the addition, binary numeric promotion is performed on
14296 	     both operands, if really necessary */
14297 	  if (JINTEGRAL_TYPE_P (op_type))
14298 	    {
14299 	      value = build_int_2 (1, 0);
14300 	      TREE_TYPE (value) = TREE_TYPE (node) = op_type;
14301 	    }
14302 	  else
14303 	    {
14304 	      value = build_int_2 (1, 0);
14305 	      TREE_TYPE (node) =
14306 		binary_numeric_promotion (op_type,
14307 					  TREE_TYPE (value), &op, &value);
14308 	    }
14309 
14310 	  /* We remember we might be accessing an outer field */
14311 	  if (outer_field_flag)
14312 	    {
14313 	      /* We re-generate an access to the field */
14314 	      value = build (PLUS_EXPR, TREE_TYPE (op),
14315 			     build_outer_field_access (wfl_op, decl), value);
14316 
14317 	      /* And we patch the original access$() into a write
14318                  with plus_op as a rhs */
14319 	      return outer_field_access_fix (node, op, value);
14320 	    }
14321 
14322 	  /* And write back into the node. */
14323 	  TREE_OPERAND (node, 0) = op;
14324 	  TREE_OPERAND (node, 1) = value;
14325 	  /* Convert the overall back into its original type, if
14326              necessary, and return */
14327 	  if (JINTEGRAL_TYPE_P (op_type))
14328 	    return fold (node);
14329 	  else
14330 	    return fold (convert (op_type, node));
14331 	}
14332       break;
14333 
14334       /* 15.14.3 Unary Plus Operator + */
14335     case UNARY_PLUS_EXPR:
14336       /* 15.14.4 Unary Minus Operator - */
14337     case NEGATE_EXPR:
14338       if (!JNUMERIC_TYPE_P (op_type))
14339 	{
14340 	  ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14341 	  TREE_TYPE (node) = error_mark_node;
14342 	  error_found = 1;
14343 	}
14344       /* Unary numeric promotion is performed on operand */
14345       else
14346 	{
14347 	  op = do_unary_numeric_promotion (op);
14348 	  prom_type = TREE_TYPE (op);
14349 	  if (code == UNARY_PLUS_EXPR)
14350 	    return fold (op);
14351 	}
14352       break;
14353 
14354       /* 15.14.5 Bitwise Complement Operator ~ */
14355     case BIT_NOT_EXPR:
14356       if (!JINTEGRAL_TYPE_P (op_type))
14357 	{
14358 	  ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14359 	  TREE_TYPE (node) = error_mark_node;
14360 	  error_found = 1;
14361 	}
14362       else
14363 	{
14364 	  op = do_unary_numeric_promotion (op);
14365 	  prom_type = TREE_TYPE (op);
14366 	}
14367       break;
14368 
14369       /* 15.14.6 Logical Complement Operator ! */
14370     case TRUTH_NOT_EXPR:
14371       if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14372 	{
14373 	  ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14374 	  /* But the type is known. We will report an error if further
14375 	     attempt of a assignment is made with this rhs */
14376 	  TREE_TYPE (node) = boolean_type_node;
14377 	  error_found = 1;
14378 	}
14379       else
14380 	prom_type = boolean_type_node;
14381       break;
14382 
14383       /* 15.15 Cast Expression */
14384     case CONVERT_EXPR:
14385       value = patch_cast (node, wfl_operator);
14386       if (value == error_mark_node)
14387 	{
14388 	  /* If this cast is part of an assignment, we tell the code
14389 	     that deals with it not to complain about a mismatch,
14390 	     because things have been cast, anyways */
14391 	  TREE_TYPE (node) = error_mark_node;
14392 	  error_found = 1;
14393 	}
14394       else
14395 	{
14396 	  value = fold (value);
14397 	  TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
14398 	  return value;
14399 	}
14400       break;
14401     }
14402 
14403   if (error_found)
14404     return error_mark_node;
14405 
14406   /* There are cases where node has been replaced by something else
14407      and we don't end up returning here: UNARY_PLUS_EXPR,
14408      CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14409   TREE_OPERAND (node, 0) = fold (op);
14410   TREE_TYPE (node) = prom_type;
14411   TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14412   return fold (node);
14413 }
14414 
14415 /* Generic type resolution that sometimes takes place during node
14416    patching. Returned the resolved type or generate an error
14417    message. Return the resolved type or NULL_TREE.  */
14418 
14419 static tree
resolve_type_during_patch(type)14420 resolve_type_during_patch (type)
14421      tree type;
14422 {
14423   if (unresolved_type_p (type, NULL))
14424     {
14425       tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14426       if (!type_decl)
14427 	{
14428 	  parse_error_context (type,
14429 			       "Class `%s' not found in type declaration",
14430 			       IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14431 	  return NULL_TREE;
14432 	}
14433       return TREE_TYPE (type_decl);
14434     }
14435   return type;
14436 }
14437 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14438    found. Otherwise NODE or something meant to replace it is returned.  */
14439 
14440 static tree
patch_cast(node,wfl_op)14441 patch_cast (node, wfl_op)
14442      tree node;
14443      tree wfl_op;
14444 {
14445   tree op = TREE_OPERAND (node, 0);
14446   tree cast_type = TREE_TYPE (node);
14447   tree patched, op_type;
14448   char *t1;
14449 
14450   /* Some string patching might be necessary at this stage */
14451   if ((patched = patch_string (op)))
14452     TREE_OPERAND (node, 0) = op = patched;
14453   op_type = TREE_TYPE (op);
14454 
14455   /* First resolve OP_TYPE if unresolved */
14456   if (!(cast_type = resolve_type_during_patch (cast_type)))
14457     return error_mark_node;
14458 
14459   /* Check on cast that are proven correct at compile time */
14460   if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14461     {
14462       /* Same type */
14463       if (cast_type == op_type)
14464 	return node;
14465 
14466       /* float and double type are converted to the original type main
14467 	 variant and then to the target type. */
14468       if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
14469 	op = convert (integer_type_node, op);
14470 
14471       /* Try widening/narowwing convertion. Potentially, things need
14472 	 to be worked out in gcc so we implement the extreme cases
14473 	 correctly. fold_convert() needs to be fixed. */
14474       return convert (cast_type, op);
14475     }
14476 
14477   /* It's also valid to cast a boolean into a boolean */
14478   if (op_type == boolean_type_node && cast_type == boolean_type_node)
14479     return node;
14480 
14481   /* null can be casted to references */
14482   if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14483     return build_null_of_type (cast_type);
14484 
14485   /* The remaining legal casts involve conversion between reference
14486      types. Check for their compile time correctness. */
14487   if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14488       && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14489     {
14490       TREE_TYPE (node) = promote_type (cast_type);
14491       /* Now, the case can be determined correct at compile time if
14492          OP_TYPE can be converted into CAST_TYPE by assignment
14493          conversion (5.2) */
14494 
14495       if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14496 	{
14497 	  TREE_SET_CODE (node, NOP_EXPR);
14498 	  return node;
14499 	}
14500 
14501       if (flag_emit_class_files)
14502 	{
14503 	  TREE_SET_CODE (node, CONVERT_EXPR);
14504 	  return node;
14505 	}
14506 
14507       /* The cast requires a run-time check */
14508       return build (CALL_EXPR, promote_type (cast_type),
14509 		    build_address_of (soft_checkcast_node),
14510 		    tree_cons (NULL_TREE, build_class_ref (cast_type),
14511 			       build_tree_list (NULL_TREE, op)),
14512 		    NULL_TREE);
14513     }
14514 
14515   /* Any other casts are proven incorrect at compile time */
14516   t1 = xstrdup (lang_printable_name (op_type, 0));
14517   parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
14518 		       t1, lang_printable_name (cast_type, 0));
14519   free (t1);
14520   return error_mark_node;
14521 }
14522 
14523 /* Build a null constant and give it the type TYPE.  */
14524 
14525 static tree
build_null_of_type(type)14526 build_null_of_type (type)
14527      tree type;
14528 {
14529   tree node = build_int_2 (0, 0);
14530   TREE_TYPE (node) = promote_type (type);
14531   return node;
14532 }
14533 
14534 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14535    a list of indices. */
14536 static tree
build_array_ref(location,array,index)14537 build_array_ref (location, array, index)
14538      int location;
14539      tree array, index;
14540 {
14541   tree node = build (ARRAY_REF, NULL_TREE, array, index);
14542   EXPR_WFL_LINECOL (node) = location;
14543   return node;
14544 }
14545 
14546 /* 15.12 Array Access Expression */
14547 
14548 static tree
patch_array_ref(node)14549 patch_array_ref (node)
14550      tree node;
14551 {
14552   tree array = TREE_OPERAND (node, 0);
14553   tree array_type  = TREE_TYPE (array);
14554   tree index = TREE_OPERAND (node, 1);
14555   tree index_type = TREE_TYPE (index);
14556   int error_found = 0;
14557 
14558   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14559 
14560   if (TREE_CODE (array_type) == POINTER_TYPE)
14561     array_type = TREE_TYPE (array_type);
14562 
14563   /* The array reference must be an array */
14564   if (!TYPE_ARRAY_P (array_type))
14565     {
14566       parse_error_context
14567 	(wfl_operator,
14568 	 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14569 	 lang_printable_name (array_type, 0));
14570       TREE_TYPE (node) = error_mark_node;
14571       error_found = 1;
14572     }
14573 
14574   /* The array index undergoes unary numeric promotion. The promoted
14575      type must be int */
14576   index = do_unary_numeric_promotion (index);
14577   if (TREE_TYPE (index) != int_type_node)
14578     {
14579       if (valid_cast_to_p (index_type, int_type_node))
14580 	parse_error_context (wfl_operator,
14581    "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
14582 			     lang_printable_name (index_type, 0));
14583       else
14584 	parse_error_context (wfl_operator,
14585           "Incompatible type for `[]'. Can't convert `%s' to `int'",
14586 			     lang_printable_name (index_type, 0));
14587       TREE_TYPE (node) = error_mark_node;
14588       error_found = 1;
14589     }
14590 
14591   if (error_found)
14592     return error_mark_node;
14593 
14594   array_type = TYPE_ARRAY_ELEMENT (array_type);
14595 
14596   if (flag_emit_class_files || flag_emit_xref)
14597     {
14598       TREE_OPERAND (node, 0) = array;
14599       TREE_OPERAND (node, 1) = index;
14600     }
14601   else
14602     node = build_java_arrayaccess (array, array_type, index);
14603   TREE_TYPE (node) = array_type;
14604   return node;
14605 }
14606 
14607 /* 15.9 Array Creation Expressions */
14608 
14609 static tree
build_newarray_node(type,dims,extra_dims)14610 build_newarray_node (type, dims, extra_dims)
14611      tree type;
14612      tree dims;
14613      int extra_dims;
14614 {
14615   tree node =
14616     build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
14617 	   build_int_2 (extra_dims, 0));
14618   return node;
14619 }
14620 
14621 static tree
patch_newarray(node)14622 patch_newarray (node)
14623      tree node;
14624 {
14625   tree type = TREE_OPERAND (node, 0);
14626   tree dims = TREE_OPERAND (node, 1);
14627   tree cdim, array_type;
14628   int error_found = 0;
14629   int ndims = 0;
14630   int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14631 
14632   /* Dimension types are verified. It's better for the types to be
14633      verified in order. */
14634   for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14635     {
14636       int dim_error = 0;
14637       tree dim = TREE_VALUE (cdim);
14638 
14639       /* Dim might have been saved during its evaluation */
14640       dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14641 
14642       /* The type of each specified dimension must be an integral type. */
14643       if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14644 	dim_error = 1;
14645 
14646       /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14647 	 promoted type must be int. */
14648       else
14649 	{
14650 	  dim = do_unary_numeric_promotion (dim);
14651 	  if (TREE_TYPE (dim) != int_type_node)
14652 	    dim_error = 1;
14653 	}
14654 
14655       /* Report errors on types here */
14656       if (dim_error)
14657 	{
14658 	  parse_error_context
14659 	    (TREE_PURPOSE (cdim),
14660 	     "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
14661 	     (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14662 	      "Explicit cast needed to" : "Can't"),
14663 	     lang_printable_name (TREE_TYPE (dim), 0));
14664 	  error_found = 1;
14665 	}
14666 
14667       TREE_PURPOSE (cdim) = NULL_TREE;
14668     }
14669 
14670   /* Resolve array base type if unresolved */
14671   if (!(type = resolve_type_during_patch (type)))
14672     error_found = 1;
14673 
14674   if (error_found)
14675     {
14676       /* We don't want further evaluation of this bogus array creation
14677          operation */
14678       TREE_TYPE (node) = error_mark_node;
14679       return error_mark_node;
14680     }
14681 
14682   /* Set array_type to the actual (promoted) array type of the result. */
14683   if (TREE_CODE (type) == RECORD_TYPE)
14684     type = build_pointer_type (type);
14685   while (--xdims >= 0)
14686     {
14687       type = promote_type (build_java_array_type (type, -1));
14688     }
14689   dims = nreverse (dims);
14690   array_type = type;
14691   for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14692     {
14693       type = array_type;
14694       array_type
14695 	= build_java_array_type (type,
14696 				 TREE_CODE (cdim) == INTEGER_CST
14697 				 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14698 				 : -1);
14699       array_type = promote_type (array_type);
14700     }
14701   dims = nreverse (dims);
14702 
14703   /* The node is transformed into a function call. Things are done
14704      differently according to the number of dimensions. If the number
14705      of dimension is equal to 1, then the nature of the base type
14706      (primitive or not) matters. */
14707   if (ndims == 1)
14708     return build_new_array (type, TREE_VALUE (dims));
14709 
14710   /* Can't reuse what's already written in expr.c because it uses the
14711      JVM stack representation. Provide a build_multianewarray. FIXME */
14712   return build (CALL_EXPR, array_type,
14713 		build_address_of (soft_multianewarray_node),
14714 		tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
14715 			   tree_cons (NULL_TREE,
14716 				      build_int_2 (ndims, 0), dims )),
14717 		NULL_TREE);
14718 }
14719 
14720 /* 10.6 Array initializer.  */
14721 
14722 /* Build a wfl for array element that don't have one, so we can
14723    pin-point errors.  */
14724 
14725 static tree
maybe_build_array_element_wfl(node)14726 maybe_build_array_element_wfl (node)
14727      tree node;
14728 {
14729   if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14730     return build_expr_wfl (NULL_TREE, ctxp->filename,
14731 			   ctxp->elc.line, ctxp->elc.prev_col);
14732   else
14733     return NULL_TREE;
14734 }
14735 
14736 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14737    identification of initialized arrays easier to detect during walk
14738    and expansion.  */
14739 
14740 static tree
build_new_array_init(location,values)14741 build_new_array_init (location, values)
14742      int location;
14743      tree values;
14744 {
14745   tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14746   tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14747   EXPR_WFL_LINECOL (to_return) = location;
14748   return to_return;
14749 }
14750 
14751 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14752    occurred.  Otherwise return NODE after having set its type
14753    appropriately.  */
14754 
14755 static tree
patch_new_array_init(type,node)14756 patch_new_array_init (type, node)
14757      tree type, node;
14758 {
14759   int error_seen = 0;
14760   tree current, element_type;
14761   HOST_WIDE_INT length;
14762   int all_constant = 1;
14763   tree init = TREE_OPERAND (node, 0);
14764 
14765   if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14766     {
14767       parse_error_context (node,
14768 			   "Invalid array initializer for non-array type `%s'",
14769 			   lang_printable_name (type, 1));
14770       return error_mark_node;
14771     }
14772   type = TREE_TYPE (type);
14773   element_type = TYPE_ARRAY_ELEMENT (type);
14774 
14775   CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14776 
14777   for (length = 0, current = CONSTRUCTOR_ELTS (init);
14778        current;  length++, current = TREE_CHAIN (current))
14779     {
14780       tree elt = TREE_VALUE (current);
14781       if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14782 	{
14783 	  error_seen |= array_constructor_check_entry (element_type, current);
14784 	  elt = TREE_VALUE (current);
14785 	  /* When compiling to native code, STRING_CST is converted to
14786 	     INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14787 	  if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14788 	    all_constant = 0;
14789 	}
14790       else
14791 	{
14792 	  TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14793 	  TREE_PURPOSE (current) = NULL_TREE;
14794 	  all_constant = 0;
14795 	}
14796       if (elt && TREE_CODE (elt) == TREE_LIST
14797 	  && TREE_VALUE (elt) == error_mark_node)
14798 	error_seen = 1;
14799     }
14800 
14801   if (error_seen)
14802     return error_mark_node;
14803 
14804   /* Create a new type. We can't reuse the one we have here by
14805      patching its dimension because it originally is of dimension -1
14806      hence reused by gcc. This would prevent triangular arrays. */
14807   type = build_java_array_type (element_type, length);
14808   TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14809   TREE_TYPE (node) = promote_type (type);
14810   TREE_CONSTANT (init) = all_constant;
14811   TREE_CONSTANT (node) = all_constant;
14812   return node;
14813 }
14814 
14815 /* Verify that one entry of the initializer element list can be
14816    assigned to the array base type. Report 1 if an error occurred, 0
14817    otherwise.  */
14818 
14819 static int
array_constructor_check_entry(type,entry)14820 array_constructor_check_entry (type, entry)
14821      tree type, entry;
14822 {
14823   char *array_type_string = NULL;	/* For error reports */
14824   tree value, type_value, new_value, wfl_value, patched;
14825   int error_seen = 0;
14826 
14827   new_value = NULL_TREE;
14828   wfl_value = TREE_VALUE (entry);
14829 
14830   value = java_complete_tree (TREE_VALUE (entry));
14831   /* patch_string return error_mark_node if arg is error_mark_node */
14832   if ((patched = patch_string (value)))
14833     value = patched;
14834   if (value == error_mark_node)
14835     return 1;
14836 
14837   type_value = TREE_TYPE (value);
14838 
14839   /* At anytime, try_builtin_assignconv can report a warning on
14840      constant overflow during narrowing. */
14841   SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14842   new_value = try_builtin_assignconv (wfl_operator, type, value);
14843   if (!new_value && (new_value = try_reference_assignconv (type, value)))
14844     type_value = promote_type (type);
14845 
14846   /* Check and report errors */
14847   if (!new_value)
14848     {
14849       const char *const msg = (!valid_cast_to_p (type_value, type) ?
14850 		   "Can't" : "Explicit cast needed to");
14851       if (!array_type_string)
14852 	array_type_string = xstrdup (lang_printable_name (type, 1));
14853       parse_error_context
14854 	(wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14855 	 msg, lang_printable_name (type_value, 1), array_type_string);
14856       error_seen = 1;
14857     }
14858 
14859   if (new_value)
14860     TREE_VALUE (entry) = new_value;
14861 
14862   if (array_type_string)
14863     free (array_type_string);
14864 
14865   TREE_PURPOSE (entry) = NULL_TREE;
14866   return error_seen;
14867 }
14868 
14869 static tree
build_this(location)14870 build_this (location)
14871      int location;
14872 {
14873   tree node = build_wfl_node (this_identifier_node);
14874   TREE_SET_CODE (node, THIS_EXPR);
14875   EXPR_WFL_LINECOL (node) = location;
14876   return node;
14877 }
14878 
14879 /* 14.15 The return statement. It builds a modify expression that
14880    assigns the returned value to the RESULT_DECL that hold the value
14881    to be returned. */
14882 
14883 static tree
build_return(location,op)14884 build_return (location, op)
14885      int location;
14886      tree op;
14887 {
14888   tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14889   EXPR_WFL_LINECOL (node) = location;
14890   node = build_debugable_stmt (location, node);
14891   return node;
14892 }
14893 
14894 static tree
patch_return(node)14895 patch_return (node)
14896      tree node;
14897 {
14898   tree return_exp = TREE_OPERAND (node, 0);
14899   tree meth = current_function_decl;
14900   tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14901   int error_found = 0;
14902 
14903   TREE_TYPE (node) = error_mark_node;
14904   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14905 
14906   /* It's invalid to have a return value within a function that is
14907      declared with the keyword void or that is a constructor */
14908   if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14909     error_found = 1;
14910 
14911   /* It's invalid to use a return statement in a static block */
14912   if (DECL_CLINIT_P (current_function_decl))
14913     error_found = 1;
14914 
14915   /* It's invalid to have a no return value within a function that
14916      isn't declared with the keyword `void' */
14917   if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14918     error_found = 2;
14919 
14920   if (DECL_INSTINIT_P (current_function_decl))
14921     error_found = 1;
14922 
14923   if (error_found)
14924     {
14925       if (DECL_INSTINIT_P (current_function_decl))
14926 	parse_error_context (wfl_operator,
14927 			     "`return' inside instance initializer");
14928 
14929       else if (DECL_CLINIT_P (current_function_decl))
14930 	parse_error_context (wfl_operator,
14931 			     "`return' inside static initializer");
14932 
14933       else if (!DECL_CONSTRUCTOR_P (meth))
14934 	{
14935 	  char *t = xstrdup (lang_printable_name (mtype, 0));
14936 	  parse_error_context (wfl_operator,
14937 			       "`return' with%s value from `%s %s'",
14938 			       (error_found == 1 ? "" : "out"),
14939 			       t, lang_printable_name (meth, 0));
14940 	  free (t);
14941 	}
14942       else
14943 	parse_error_context (wfl_operator,
14944 			     "`return' with value from constructor `%s'",
14945 			     lang_printable_name (meth, 0));
14946       return error_mark_node;
14947     }
14948 
14949   /* If we have a return_exp, build a modify expression and expand
14950      it. Note: at that point, the assignment is declared valid, but we
14951      may want to carry some more hacks */
14952   if (return_exp)
14953     {
14954       tree exp = java_complete_tree (return_exp);
14955       tree modify, patched;
14956 
14957       if ((patched = patch_string (exp)))
14958 	exp = patched;
14959 
14960       modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14961       EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14962       modify = java_complete_tree (modify);
14963 
14964       if (modify != error_mark_node)
14965 	{
14966 	  TREE_SIDE_EFFECTS (modify) = 1;
14967 	  TREE_OPERAND (node, 0) = modify;
14968 	}
14969       else
14970 	return error_mark_node;
14971     }
14972   TREE_TYPE (node) = void_type_node;
14973   TREE_SIDE_EFFECTS (node) = 1;
14974   return node;
14975 }
14976 
14977 /* 14.8 The if Statement */
14978 
14979 static tree
build_if_else_statement(location,expression,if_body,else_body)14980 build_if_else_statement (location, expression, if_body, else_body)
14981      int location;
14982      tree expression, if_body, else_body;
14983 {
14984   tree node;
14985   if (!else_body)
14986     else_body = empty_stmt_node;
14987   node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14988   EXPR_WFL_LINECOL (node) = location;
14989   node = build_debugable_stmt (location, node);
14990   return node;
14991 }
14992 
14993 static tree
patch_if_else_statement(node)14994 patch_if_else_statement (node)
14995      tree node;
14996 {
14997   tree expression = TREE_OPERAND (node, 0);
14998   int can_complete_normally
14999     = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15000        | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
15001 
15002   TREE_TYPE (node) = error_mark_node;
15003   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15004 
15005   /* The type of expression must be boolean */
15006   if (TREE_TYPE (expression) != boolean_type_node
15007       && TREE_TYPE (expression) != promoted_boolean_type_node)
15008     {
15009       parse_error_context
15010 	(wfl_operator,
15011 	 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
15012 	 lang_printable_name (TREE_TYPE (expression), 0));
15013       return error_mark_node;
15014     }
15015 
15016   if (TREE_CODE (expression) == INTEGER_CST)
15017     {
15018       if (integer_zerop (expression))
15019 	node = TREE_OPERAND (node, 2);
15020       else
15021 	node = TREE_OPERAND (node, 1);
15022       if (CAN_COMPLETE_NORMALLY (node) != can_complete_normally)
15023 	{
15024 	  node = build (COMPOUND_EXPR, void_type_node, node, empty_stmt_node);
15025 	  CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
15026 	}
15027       return node;
15028     }
15029   TREE_TYPE (node) = void_type_node;
15030   TREE_SIDE_EFFECTS (node) = 1;
15031   CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
15032   return node;
15033 }
15034 
15035 /* 14.6 Labeled Statements */
15036 
15037 /* Action taken when a lableled statement is parsed. a new
15038    LABELED_BLOCK_EXPR is created. No statement is attached to the
15039    label, yet.  LABEL can be NULL_TREE for artificially-generated blocks. */
15040 
15041 static tree
build_labeled_block(location,label)15042 build_labeled_block (location, label)
15043      int location;
15044      tree label;
15045 {
15046   tree label_name ;
15047   tree label_decl, node;
15048   if (label == NULL_TREE || label == continue_identifier_node)
15049     label_name = label;
15050   else
15051     {
15052       label_name = merge_qualified_name (label_id, label);
15053       /* Issue an error if we try to reuse a label that was previously
15054 	 declared */
15055       if (IDENTIFIER_LOCAL_VALUE (label_name))
15056 	{
15057 	  EXPR_WFL_LINECOL (wfl_operator) = location;
15058 	  parse_error_context (wfl_operator,
15059             "Declaration of `%s' shadows a previous label declaration",
15060 			       IDENTIFIER_POINTER (label));
15061 	  EXPR_WFL_LINECOL (wfl_operator) =
15062 	    EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
15063 	  parse_error_context (wfl_operator,
15064             "This is the location of the previous declaration of label `%s'",
15065 			       IDENTIFIER_POINTER (label));
15066 	  java_error_count--;
15067 	}
15068     }
15069 
15070   label_decl = create_label_decl (label_name);
15071   node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
15072   EXPR_WFL_LINECOL (node) = location;
15073   TREE_SIDE_EFFECTS (node) = 1;
15074   return node;
15075 }
15076 
15077 /* A labeled statement LBE is attached a statement.  */
15078 
15079 static tree
finish_labeled_statement(lbe,statement)15080 finish_labeled_statement (lbe, statement)
15081      tree lbe;			/* Labeled block expr */
15082      tree statement;
15083 {
15084   /* In anyways, tie the loop to its statement */
15085   LABELED_BLOCK_BODY (lbe) = statement;
15086   pop_labeled_block ();
15087   POP_LABELED_BLOCK ();
15088   return lbe;
15089 }
15090 
15091 /* 14.10, 14.11, 14.12 Loop Statements */
15092 
15093 /* Create an empty LOOP_EXPR and make it the last in the nested loop
15094    list. */
15095 
15096 static tree
build_new_loop(loop_body)15097 build_new_loop (loop_body)
15098      tree loop_body;
15099 {
15100   tree loop =  build (LOOP_EXPR, NULL_TREE, loop_body);
15101   TREE_SIDE_EFFECTS (loop) = 1;
15102   PUSH_LOOP (loop);
15103   return loop;
15104 }
15105 
15106 /* Create a loop body according to the following structure:
15107      COMPOUND_EXPR
15108        COMPOUND_EXPR		(loop main body)
15109          EXIT_EXPR		(this order is for while/for loops.
15110          LABELED_BLOCK_EXPR      the order is reversed for do loops)
15111            LABEL_DECL           (a continue occurring here branches at the
15112            BODY			 end of this labeled block)
15113        INCREMENT		(if any)
15114 
15115   REVERSED, if nonzero, tells that the loop condition expr comes
15116   after the body, like in the do-while loop.
15117 
15118   To obtain a loop, the loop body structure described above is
15119   encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
15120 
15121    LABELED_BLOCK_EXPR
15122      LABEL_DECL                   (use this label to exit the loop)
15123      LOOP_EXPR
15124        <structure described above> */
15125 
15126 static tree
build_loop_body(location,condition,reversed)15127 build_loop_body (location, condition, reversed)
15128      int location;
15129      tree condition;
15130      int reversed;
15131 {
15132   tree first, second, body;
15133 
15134   condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
15135   EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
15136   condition = build_debugable_stmt (location, condition);
15137   TREE_SIDE_EFFECTS (condition) = 1;
15138 
15139   body = build_labeled_block (0, continue_identifier_node);
15140   first = (reversed ? body : condition);
15141   second = (reversed ? condition : body);
15142   return
15143     build (COMPOUND_EXPR, NULL_TREE,
15144 	   build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
15145 }
15146 
15147 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
15148    their order) on the current loop. Unlink the current loop from the
15149    loop list.  */
15150 
15151 static tree
finish_loop_body(location,condition,body,reversed)15152 finish_loop_body (location, condition, body, reversed)
15153      int location;
15154      tree condition, body;
15155      int reversed;
15156 {
15157   tree to_return = ctxp->current_loop;
15158   tree loop_body = LOOP_EXPR_BODY (to_return);
15159   if (condition)
15160     {
15161       tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
15162       /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
15163          The real EXIT_EXPR is one operand further. */
15164       EXPR_WFL_LINECOL (cnode) = location;
15165       /* This one is for accurate error reports */
15166       EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
15167       TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
15168     }
15169   LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
15170   POP_LOOP ();
15171   return to_return;
15172 }
15173 
15174 /* Tailored version of finish_loop_body for FOR loops, when FOR
15175    loops feature the condition part */
15176 
15177 static tree
finish_for_loop(location,condition,update,body)15178 finish_for_loop (location, condition, update, body)
15179     int location;
15180     tree condition, update, body;
15181 {
15182   /* Put the condition and the loop body in place */
15183   tree loop = finish_loop_body (location, condition, body, 0);
15184   /* LOOP is the current loop which has been now popped of the loop
15185      stack.  Mark the update block as reachable and install it.  We do
15186      this because the (current interpretation of the) JLS requires
15187      that the update expression be considered reachable even if the
15188      for loop's body doesn't complete normally.  */
15189   if (update != NULL_TREE && update != empty_stmt_node)
15190     {
15191       tree up2 = update;
15192       if (TREE_CODE (up2) == EXPR_WITH_FILE_LOCATION)
15193 	up2 = EXPR_WFL_NODE (up2);
15194       /* It is possible for the update expression to be an
15195 	 EXPR_WFL_NODE wrapping nothing.  */
15196       if (up2 != NULL_TREE && up2 != empty_stmt_node)
15197 	{
15198 	  /* Try to detect constraint violations.  These would be
15199 	     programming errors somewhere.  */
15200 	  if (! IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (up2)))
15201 	      || TREE_CODE (up2) == LOOP_EXPR)
15202 	    abort ();
15203 	  SUPPRESS_UNREACHABLE_ERROR (up2) = 1;
15204 	}
15205     }
15206   LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
15207   return loop;
15208 }
15209 
15210 /* Try to find the loop a block might be related to. This comprises
15211    the case where the LOOP_EXPR is found as the second operand of a
15212    COMPOUND_EXPR, because the loop happens to have an initialization
15213    part, then expressed as the first operand of the COMPOUND_EXPR. If
15214    the search finds something, 1 is returned. Otherwise, 0 is
15215    returned. The search is assumed to start from a
15216    LABELED_BLOCK_EXPR's block.  */
15217 
15218 static tree
search_loop(statement)15219 search_loop (statement)
15220     tree statement;
15221 {
15222   if (TREE_CODE (statement) == LOOP_EXPR)
15223     return statement;
15224 
15225   if (TREE_CODE (statement) == BLOCK)
15226     statement = BLOCK_SUBBLOCKS (statement);
15227   else
15228     return NULL_TREE;
15229 
15230   if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15231     while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15232       statement = TREE_OPERAND (statement, 1);
15233 
15234   return (TREE_CODE (statement) == LOOP_EXPR
15235 	  && FOR_LOOP_P (statement) ? statement : NULL_TREE);
15236 }
15237 
15238 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
15239    returned otherwise.  */
15240 
15241 static int
labeled_block_contains_loop_p(block,loop)15242 labeled_block_contains_loop_p (block, loop)
15243     tree block, loop;
15244 {
15245   if (!block)
15246     return 0;
15247 
15248   if (LABELED_BLOCK_BODY (block) == loop)
15249     return 1;
15250 
15251   if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
15252     return 1;
15253 
15254   return 0;
15255 }
15256 
15257 /* If the loop isn't surrounded by a labeled statement, create one and
15258    insert LOOP as its body.  */
15259 
15260 static tree
patch_loop_statement(loop)15261 patch_loop_statement (loop)
15262      tree loop;
15263 {
15264   tree loop_label;
15265 
15266   TREE_TYPE (loop) = void_type_node;
15267   if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
15268     return loop;
15269 
15270   loop_label = build_labeled_block (0, NULL_TREE);
15271   /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
15272      that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
15273   LABELED_BLOCK_BODY (loop_label) = loop;
15274   PUSH_LABELED_BLOCK (loop_label);
15275   return loop_label;
15276 }
15277 
15278 /* 14.13, 14.14: break and continue Statements */
15279 
15280 /* Build a break or a continue statement. a null NAME indicates an
15281    unlabeled break/continue statement.  */
15282 
15283 static tree
build_bc_statement(location,is_break,name)15284 build_bc_statement (location, is_break, name)
15285      int location, is_break;
15286      tree name;
15287 {
15288   tree break_continue, label_block_expr = NULL_TREE;
15289 
15290   if (name)
15291     {
15292       if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15293 	    (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15294 	/* Null means that we don't have a target for this named
15295 	   break/continue. In this case, we make the target to be the
15296 	   label name, so that the error can be reported accuratly in
15297 	   patch_bc_statement. */
15298 	label_block_expr = EXPR_WFL_NODE (name);
15299     }
15300   /* Unlabeled break/continue will be handled during the
15301      break/continue patch operation */
15302   break_continue
15303     = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
15304 
15305   IS_BREAK_STMT_P (break_continue) = is_break;
15306   TREE_SIDE_EFFECTS (break_continue) = 1;
15307   EXPR_WFL_LINECOL (break_continue) = location;
15308   break_continue = build_debugable_stmt (location, break_continue);
15309   return break_continue;
15310 }
15311 
15312 /* Verification of a break/continue statement. */
15313 
15314 static tree
patch_bc_statement(node)15315 patch_bc_statement (node)
15316      tree node;
15317 {
15318   tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15319   tree labeled_block = ctxp->current_labeled_block;
15320   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15321 
15322   /* Having an identifier here means that the target is unknown. */
15323   if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15324     {
15325       parse_error_context (wfl_operator, "No label definition found for `%s'",
15326 			   IDENTIFIER_POINTER (bc_label));
15327       return error_mark_node;
15328     }
15329   if (! IS_BREAK_STMT_P (node))
15330     {
15331       /* It's a continue statement. */
15332       for (;; labeled_block = TREE_CHAIN (labeled_block))
15333 	{
15334 	  if (labeled_block == NULL_TREE)
15335 	    {
15336 	      if (bc_label == NULL_TREE)
15337 		parse_error_context (wfl_operator,
15338 				     "`continue' must be in loop");
15339 	      else
15340 		parse_error_context
15341 		  (wfl_operator, "continue label `%s' does not name a loop",
15342 		   IDENTIFIER_POINTER (bc_label));
15343 	      return error_mark_node;
15344 	    }
15345 	  if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15346 	       == continue_identifier_node)
15347 	      && (bc_label == NULL_TREE
15348 		  || TREE_CHAIN (labeled_block) == bc_label))
15349 	    {
15350 	      bc_label = labeled_block;
15351 	      break;
15352 	    }
15353 	}
15354     }
15355   else if (!bc_label)
15356     {
15357       for (;; labeled_block = TREE_CHAIN (labeled_block))
15358 	{
15359 	  if (labeled_block == NULL_TREE)
15360 	    {
15361 	      parse_error_context (wfl_operator,
15362 				     "`break' must be in loop or switch");
15363 	      return error_mark_node;
15364 	    }
15365 	  target_stmt = LABELED_BLOCK_BODY (labeled_block);
15366 	  if (TREE_CODE (target_stmt) == SWITCH_EXPR
15367 	      || search_loop (target_stmt))
15368 	    {
15369 	      bc_label = labeled_block;
15370 	      break;
15371 	    }
15372 	}
15373     }
15374 
15375   EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15376   CAN_COMPLETE_NORMALLY (bc_label) = 1;
15377 
15378   /* Our break/continue don't return values. */
15379   TREE_TYPE (node) = void_type_node;
15380   /* Encapsulate the break within a compound statement so that it's
15381      expanded all the times by expand_expr (and not clobbered
15382      sometimes, like after a if statement) */
15383   node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15384   TREE_SIDE_EFFECTS (node) = 1;
15385   return node;
15386 }
15387 
15388 /* Process the exit expression belonging to a loop. Its type must be
15389    boolean.  */
15390 
15391 static tree
patch_exit_expr(node)15392 patch_exit_expr (node)
15393      tree node;
15394 {
15395   tree expression = TREE_OPERAND (node, 0);
15396   TREE_TYPE (node) = error_mark_node;
15397   EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15398 
15399   /* The type of expression must be boolean */
15400   if (TREE_TYPE (expression) != boolean_type_node)
15401     {
15402       parse_error_context
15403 	(wfl_operator,
15404     "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
15405 	 lang_printable_name (TREE_TYPE (expression), 0));
15406       return error_mark_node;
15407     }
15408   /* Now we know things are allright, invert the condition, fold and
15409      return */
15410   TREE_OPERAND (node, 0) =
15411     fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15412 
15413   if (! integer_zerop (TREE_OPERAND (node, 0))
15414       && ctxp->current_loop != NULL_TREE
15415       && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15416     CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15417   if (! integer_onep (TREE_OPERAND (node, 0)))
15418     CAN_COMPLETE_NORMALLY (node) = 1;
15419 
15420 
15421   TREE_TYPE (node) = void_type_node;
15422   return node;
15423 }
15424 
15425 /* 14.9 Switch statement */
15426 
15427 static tree
patch_switch_statement(node)15428 patch_switch_statement (node)
15429      tree node;
15430 {
15431   tree se = TREE_OPERAND (node, 0), se_type;
15432   tree save, iter;
15433 
15434   /* Complete the switch expression */
15435   se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15436   se_type = TREE_TYPE (se);
15437   /* The type of the switch expression must be char, byte, short or
15438      int */
15439   if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15440     {
15441       EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15442       parse_error_context (wfl_operator,
15443 	  "Incompatible type for `switch'. Can't convert `%s' to `int'",
15444 			   lang_printable_name (se_type, 0));
15445       /* This is what java_complete_tree will check */
15446       TREE_OPERAND (node, 0) = error_mark_node;
15447       return error_mark_node;
15448     }
15449 
15450   /* Save and restore the outer case label list.  */
15451   save = case_label_list;
15452   case_label_list = NULL_TREE;
15453 
15454   TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15455 
15456   /* See if we've found a duplicate label.  We can't leave this until
15457      code generation, because in `--syntax-only' and `-C' modes we
15458      don't do ordinary code generation.  */
15459   for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15460     {
15461       HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15462       tree subiter;
15463       for (subiter = TREE_CHAIN (iter);
15464 	   subiter != NULL_TREE;
15465 	   subiter = TREE_CHAIN (subiter))
15466 	{
15467 	  HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15468 	  if (val == subval)
15469 	    {
15470 	      EXPR_WFL_LINECOL (wfl_operator)
15471 		= EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15472 	      /* The case_label_list is in reverse order, so print the
15473 		 outer label first.  */
15474 	      parse_error_context (wfl_operator, "duplicate case label: `"
15475 				   HOST_WIDE_INT_PRINT_DEC "'", subval);
15476 	      EXPR_WFL_LINECOL (wfl_operator)
15477 		= EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15478 	      parse_error_context (wfl_operator, "original label is here");
15479 
15480 	      break;
15481 	    }
15482 	}
15483     }
15484 
15485   case_label_list = save;
15486 
15487   /* Ready to return */
15488   if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15489     {
15490       TREE_TYPE (node) = error_mark_node;
15491       return error_mark_node;
15492     }
15493   TREE_TYPE (node) = void_type_node;
15494   TREE_SIDE_EFFECTS (node) = 1;
15495   CAN_COMPLETE_NORMALLY (node)
15496     = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15497       || ! SWITCH_HAS_DEFAULT (node);
15498   return node;
15499 }
15500 
15501 /* Assertions.  */
15502 
15503 /* Build an assertion expression for `assert CONDITION : VALUE'; VALUE
15504    might be NULL_TREE.  */
15505 static tree
build_assertion(location,condition,value)15506 build_assertion (location, condition, value)
15507      int location;
15508      tree condition, value;
15509 {
15510   tree node;
15511   tree klass = GET_CPC ();
15512 
15513   if (! CLASS_USES_ASSERTIONS (klass))
15514     {
15515       tree field, classdollar, id, call;
15516       tree class_type = TREE_TYPE (klass);
15517 
15518       field = add_field (class_type,
15519 			 get_identifier ("$assertionsDisabled"),
15520 			 boolean_type_node,
15521 			 ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
15522       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
15523       FIELD_SYNTHETIC (field) = 1;
15524 
15525       if (!TYPE_DOT_CLASS (class_type))
15526 	build_dot_class_method (class_type);
15527       classdollar = build_dot_class_method_invocation (class_type);
15528 
15529       /* Call CLASS.desiredAssertionStatus().  */
15530       id = build_wfl_node (get_identifier ("desiredAssertionStatus"));
15531       call = build (CALL_EXPR, NULL_TREE, id, NULL_TREE, NULL_TREE);
15532       call = make_qualified_primary (classdollar, call, location);
15533       TREE_SIDE_EFFECTS (call) = 1;
15534 
15535       /* Invert to obtain !CLASS.desiredAssertionStatus().  This may
15536 	 seem odd, but we do it to generate code identical to that of
15537 	 the JDK.  */
15538       call = build1 (TRUTH_NOT_EXPR, NULL_TREE, call);
15539       TREE_SIDE_EFFECTS (call) = 1;
15540       DECL_INITIAL (field) = call;
15541 
15542       /* Record the initializer in the initializer statement list.  */
15543       call = build (MODIFY_EXPR, NULL_TREE, field, call);
15544       TREE_CHAIN (call) = CPC_STATIC_INITIALIZER_STMT (ctxp);
15545       SET_CPC_STATIC_INITIALIZER_STMT (ctxp, call);
15546       MODIFY_EXPR_FROM_INITIALIZATION_P (call) = 1;
15547 
15548       CLASS_USES_ASSERTIONS (klass) = 1;
15549     }
15550 
15551   if (value != NULL_TREE)
15552     value = tree_cons (NULL_TREE, value, NULL_TREE);
15553 
15554   node = build_wfl_node (get_identifier ("java"));
15555   node = make_qualified_name (node, build_wfl_node (get_identifier ("lang")),
15556 			      location);
15557   node = make_qualified_name (node, build_wfl_node (get_identifier ("AssertionError")),
15558 			      location);
15559 
15560   node = build (NEW_CLASS_EXPR, NULL_TREE, node, value, NULL_TREE);
15561   TREE_SIDE_EFFECTS (node) = 1;
15562   /* It is too early to use BUILD_THROW.  */
15563   node = build1 (THROW_EXPR, NULL_TREE, node);
15564   TREE_SIDE_EFFECTS (node) = 1;
15565 
15566   /* We invert the condition; if we just put NODE as the `else' part
15567      then we generate weird-looking bytecode.  */
15568   condition = build1 (TRUTH_NOT_EXPR, NULL_TREE, condition);
15569   /* Check $assertionsDisabled.  */
15570   condition
15571     = build (TRUTH_ANDIF_EXPR, NULL_TREE,
15572 	     build1 (TRUTH_NOT_EXPR, NULL_TREE,
15573 		     build_wfl_node (get_identifier ("$assertionsDisabled"))),
15574 	     condition);
15575   node = build_if_else_statement (location, condition, node, NULL_TREE);
15576   return node;
15577 }
15578 
15579 /* 14.18 The try/catch statements */
15580 
15581 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15582    catches TYPE and executes CATCH_STMTS.  */
15583 
15584 static tree
encapsulate_with_try_catch(location,type,try_stmts,catch_stmts)15585 encapsulate_with_try_catch (location, type, try_stmts, catch_stmts)
15586      int location;
15587      tree type, try_stmts, catch_stmts;
15588 {
15589   tree try_block, catch_clause_param, catch_block, catch;
15590 
15591   /* First build a try block */
15592   try_block = build_expr_block (try_stmts, NULL_TREE);
15593 
15594   /* Build a catch block: we need a catch clause parameter */
15595   catch_clause_param = build_decl (VAR_DECL,
15596 				   wpv_id, build_pointer_type (type));
15597   /* And a block */
15598   catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15599 
15600   /* Initialize the variable and store in the block */
15601   catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15602 		 build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15603   add_stmt_to_block (catch_block, NULL_TREE, catch);
15604 
15605   /* Add the catch statements */
15606   add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15607 
15608   /* Now we can build a CATCH_EXPR */
15609   catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
15610 
15611   return build_try_statement (location, try_block, catch_block);
15612 }
15613 
15614 static tree
build_try_statement(location,try_block,catches)15615 build_try_statement (location, try_block, catches)
15616      int location;
15617      tree try_block, catches;
15618 {
15619   tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
15620   EXPR_WFL_LINECOL (node) = location;
15621   return node;
15622 }
15623 
15624 static tree
build_try_finally_statement(location,try_block,finally)15625 build_try_finally_statement (location, try_block, finally)
15626      int location;
15627      tree try_block, finally;
15628 {
15629   tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15630   EXPR_WFL_LINECOL (node) = location;
15631   return node;
15632 }
15633 
15634 static tree
patch_try_statement(node)15635 patch_try_statement (node)
15636      tree node;
15637 {
15638   int error_found = 0;
15639   tree try = TREE_OPERAND (node, 0);
15640   /* Exception handlers are considered in left to right order */
15641   tree catch = nreverse (TREE_OPERAND (node, 1));
15642   tree current, caught_type_list = NULL_TREE;
15643 
15644   /* Check catch clauses, if any. Every time we find an error, we try
15645      to process the next catch clause. We process the catch clause before
15646      the try block so that when processing the try block we can check thrown
15647      exceptions againts the caught type list. */
15648   for (current = catch; current; current = TREE_CHAIN (current))
15649     {
15650       tree carg_decl, carg_type;
15651       tree sub_current, catch_block, catch_clause;
15652       int unreachable;
15653 
15654       /* At this point, the structure of the catch clause is
15655 	   CATCH_EXPR		(catch node)
15656 	     BLOCK	        (with the decl of the parameter)
15657                COMPOUND_EXPR
15658                  MODIFY_EXPR   (assignment of the catch parameter)
15659 		 BLOCK	        (catch clause block)
15660        */
15661       catch_clause = TREE_OPERAND (current, 0);
15662       carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15663       carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15664 
15665       /* Catch clauses can't have more than one parameter declared,
15666 	 but it's already enforced by the grammar. Make sure that the
15667 	 only parameter of the clause statement in of class Throwable
15668 	 or a subclass of Throwable, but that was done earlier. The
15669 	 catch clause parameter type has also been resolved. */
15670 
15671       /* Just make sure that the catch clause parameter type inherits
15672 	 from java.lang.Throwable */
15673       if (!inherits_from_p (carg_type, throwable_type_node))
15674 	{
15675 	  EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15676 	  parse_error_context (wfl_operator,
15677 			       "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
15678 			       lang_printable_name (carg_type, 0));
15679 	  error_found = 1;
15680 	  continue;
15681 	}
15682 
15683       /* Partial check for unreachable catch statement: The catch
15684 	 clause is reachable iff is no earlier catch block A in
15685 	 the try statement such that the type of the catch
15686 	 clause's parameter is the same as or a subclass of the
15687 	 type of A's parameter */
15688       unreachable = 0;
15689       for (sub_current = catch;
15690 	   sub_current != current; sub_current = TREE_CHAIN (sub_current))
15691 	{
15692 	  tree sub_catch_clause, decl;
15693 	  sub_catch_clause = TREE_OPERAND (sub_current, 0);
15694 	  decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15695 
15696 	  if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15697 	    {
15698 	      EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15699 	      parse_error_context
15700 		(wfl_operator,
15701 		 "`catch' not reached because of the catch clause at line %d",
15702 		 EXPR_WFL_LINENO (sub_current));
15703 	      unreachable = error_found = 1;
15704 	      break;
15705 	    }
15706 	}
15707       /* Complete the catch clause block */
15708       catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15709       if (catch_block == error_mark_node)
15710 	{
15711 	  error_found = 1;
15712 	  continue;
15713 	}
15714       if (CAN_COMPLETE_NORMALLY (catch_block))
15715 	CAN_COMPLETE_NORMALLY (node) = 1;
15716       TREE_OPERAND (current, 0) = catch_block;
15717 
15718       if (unreachable)
15719 	continue;
15720 
15721       /* Things to do here: the exception must be thrown */
15722 
15723       /* Link this type to the caught type list */
15724       caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15725     }
15726 
15727   PUSH_EXCEPTIONS (caught_type_list);
15728   if ((try = java_complete_tree (try)) == error_mark_node)
15729     error_found = 1;
15730   if (CAN_COMPLETE_NORMALLY (try))
15731     CAN_COMPLETE_NORMALLY (node) = 1;
15732   POP_EXCEPTIONS ();
15733 
15734   /* Verification ends here */
15735   if (error_found)
15736     return error_mark_node;
15737 
15738   TREE_OPERAND (node, 0) = try;
15739   TREE_OPERAND (node, 1) = catch;
15740   TREE_TYPE (node) = void_type_node;
15741   return node;
15742 }
15743 
15744 /* 14.17 The synchronized Statement */
15745 
15746 static tree
patch_synchronized_statement(node,wfl_op1)15747 patch_synchronized_statement (node, wfl_op1)
15748     tree node, wfl_op1;
15749 {
15750   tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15751   tree block = TREE_OPERAND (node, 1);
15752 
15753   tree tmp, enter, exit, expr_decl, assignment;
15754 
15755   if (expr == error_mark_node)
15756     {
15757       block = java_complete_tree (block);
15758       return expr;
15759     }
15760 
15761   /* We might be trying to synchronize on a STRING_CST */
15762   if ((tmp = patch_string (expr)))
15763     expr = tmp;
15764 
15765   /* The TYPE of expr must be a reference type */
15766   if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15767     {
15768       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15769       parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
15770 			   lang_printable_name (TREE_TYPE (expr), 0));
15771       return error_mark_node;
15772     }
15773 
15774   if (flag_emit_xref)
15775     {
15776       TREE_OPERAND (node, 0) = expr;
15777       TREE_OPERAND (node, 1) = java_complete_tree (block);
15778       CAN_COMPLETE_NORMALLY (node) = 1;
15779       return node;
15780     }
15781 
15782   /* Generate a try-finally for the synchronized statement, except
15783      that the handler that catches all throw exception calls
15784      _Jv_MonitorExit and then rethrow the exception.
15785      The synchronized statement is then implemented as:
15786      TRY
15787        {
15788          _Jv_MonitorEnter (expression)
15789 	 synchronized_block
15790          _Jv_MonitorExit (expression)
15791        }
15792      CATCH_ALL
15793        {
15794          e = _Jv_exception_info ();
15795 	 _Jv_MonitorExit (expression)
15796 	 Throw (e);
15797        } */
15798 
15799   expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15800   BUILD_MONITOR_ENTER (enter, expr_decl);
15801   BUILD_MONITOR_EXIT (exit, expr_decl);
15802   CAN_COMPLETE_NORMALLY (enter) = 1;
15803   CAN_COMPLETE_NORMALLY (exit) = 1;
15804   assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15805   TREE_SIDE_EFFECTS (assignment) = 1;
15806   node = build (COMPOUND_EXPR, NULL_TREE,
15807 		build (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15808 		build (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15809   node = build_expr_block (node, expr_decl);
15810 
15811   return java_complete_tree (node);
15812 }
15813 
15814 /* 14.16 The throw Statement */
15815 
15816 static tree
patch_throw_statement(node,wfl_op1)15817 patch_throw_statement (node, wfl_op1)
15818     tree node, wfl_op1;
15819 {
15820   tree expr = TREE_OPERAND (node, 0);
15821   tree type = TREE_TYPE (expr);
15822   int unchecked_ok = 0, tryblock_throws_ok = 0;
15823 
15824   /* Thrown expression must be assignable to java.lang.Throwable */
15825   if (!try_reference_assignconv (throwable_type_node, expr))
15826     {
15827       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15828       parse_error_context (wfl_operator,
15829     "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
15830 			   lang_printable_name (type, 0));
15831       /* If the thrown expression was a reference, we further the
15832          compile-time check. */
15833       if (!JREFERENCE_TYPE_P (type))
15834 	return error_mark_node;
15835     }
15836 
15837   /* At least one of the following must be true */
15838 
15839   /* The type of the throw expression is a not checked exception,
15840      i.e. is a unchecked expression. */
15841   unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15842 
15843   SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15844   /* An instance can't throw a checked exception unless that exception
15845      is explicitly declared in the `throws' clause of each
15846      constructor. This doesn't apply to anonymous classes, since they
15847      don't have declared constructors. */
15848   if (!unchecked_ok
15849       && DECL_INSTINIT_P (current_function_decl)
15850       && !ANONYMOUS_CLASS_P (current_class))
15851     {
15852       tree current;
15853       for (current = TYPE_METHODS (current_class); current;
15854 	   current = TREE_CHAIN (current))
15855 	if (DECL_CONSTRUCTOR_P (current)
15856 	    && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15857 	  {
15858 	    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)",
15859 				 lang_printable_name (TREE_TYPE (expr), 0));
15860 	    return error_mark_node;
15861 	  }
15862     }
15863 
15864   /* Throw is contained in a try statement and at least one catch
15865      clause can receive the thrown expression or the current method is
15866      declared to throw such an exception. Or, the throw statement is
15867      contained in a method or constructor declaration and the type of
15868      the Expression is assignable to at least one type listed in the
15869      throws clause the declaration. */
15870   if (!unchecked_ok)
15871     tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15872   if (!(unchecked_ok || tryblock_throws_ok))
15873     {
15874       /* If there is a surrounding try block that has no matching
15875 	 clatch clause, report it first. A surrounding try block exits
15876 	 only if there is something after the list of checked
15877 	 exception thrown by the current function (if any). */
15878       if (IN_TRY_BLOCK_P ())
15879 	parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
15880 			     lang_printable_name (type, 0));
15881       /* If we have no surrounding try statement and the method doesn't have
15882 	 any throws, report it now. FIXME */
15883 
15884       /* We report that the exception can't be throw from a try block
15885          in all circumstances but when the `throw' is inside a static
15886          block. */
15887       else if (!EXCEPTIONS_P (currently_caught_type_list)
15888 	       && !tryblock_throws_ok)
15889 	{
15890 	  if (DECL_CLINIT_P (current_function_decl))
15891 	    parse_error_context (wfl_operator,
15892                    "Checked exception `%s' can't be thrown in initializer",
15893 				 lang_printable_name (type, 0));
15894 	  else
15895 	    parse_error_context (wfl_operator,
15896                    "Checked exception `%s' isn't thrown from a `try' block",
15897 				 lang_printable_name (type, 0));
15898 	}
15899       /* Otherwise, the current method doesn't have the appropriate
15900          throws declaration */
15901       else
15902 	parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
15903 			     lang_printable_name (type, 0));
15904       return error_mark_node;
15905     }
15906 
15907   if (! flag_emit_class_files && ! flag_emit_xref)
15908     BUILD_THROW (node, expr);
15909 
15910   /* If doing xrefs, keep the location where the `throw' was seen. */
15911   if (flag_emit_xref)
15912     EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15913   return node;
15914 }
15915 
15916 /* Check that exception said to be thrown by method DECL can be
15917    effectively caught from where DECL is invoked.  */
15918 
15919 static void
check_thrown_exceptions(location,decl)15920 check_thrown_exceptions (location, decl)
15921      int location;
15922      tree decl;
15923 {
15924   tree throws;
15925   /* For all the unchecked exceptions thrown by DECL */
15926   for (throws = DECL_FUNCTION_THROWS (decl); throws;
15927        throws = TREE_CHAIN (throws))
15928     if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15929       {
15930 #if 1
15931 	/* Temporary hack to suppresses errors about cloning arrays. FIXME */
15932 	if (DECL_NAME (decl) == get_identifier ("clone"))
15933 	  continue;
15934 #endif
15935 	EXPR_WFL_LINECOL (wfl_operator) = location;
15936 	if (DECL_FINIT_P (current_function_decl))
15937 	  parse_error_context
15938             (wfl_operator, "Exception `%s' can't be thrown in initializer",
15939 	     lang_printable_name (TREE_VALUE (throws), 0));
15940 	else
15941 	  {
15942 	    parse_error_context
15943 	      (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
15944 	       lang_printable_name (TREE_VALUE (throws), 0),
15945 	       (DECL_INIT_P (current_function_decl) ?
15946 		IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15947 		IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15948 	  }
15949       }
15950 }
15951 
15952 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15953    try-catch blocks, OR is listed in the `throws' clause of the
15954    current method.  */
15955 
15956 static int
check_thrown_exceptions_do(exception)15957 check_thrown_exceptions_do (exception)
15958      tree exception;
15959 {
15960   tree list = currently_caught_type_list;
15961   resolve_and_layout (exception, NULL_TREE);
15962   /* First, all the nested try-catch-finally at that stage. The
15963      last element contains `throws' clause exceptions, if any. */
15964   if (IS_UNCHECKED_EXCEPTION_P (exception))
15965     return 1;
15966   while (list)
15967     {
15968       tree caught;
15969       for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15970 	if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15971 	  return 1;
15972       list = TREE_CHAIN (list);
15973     }
15974   return 0;
15975 }
15976 
15977 static void
purge_unchecked_exceptions(mdecl)15978 purge_unchecked_exceptions (mdecl)
15979      tree mdecl;
15980 {
15981   tree throws = DECL_FUNCTION_THROWS (mdecl);
15982   tree new = NULL_TREE;
15983 
15984   while (throws)
15985     {
15986       tree next = TREE_CHAIN (throws);
15987       if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15988 	{
15989 	  TREE_CHAIN (throws) = new;
15990 	  new = throws;
15991 	}
15992       throws = next;
15993     }
15994   /* List is inverted here, but it doesn't matter */
15995   DECL_FUNCTION_THROWS (mdecl) = new;
15996 }
15997 
15998 /* This function goes over all of CLASS_TYPE ctors and checks whether
15999    each of them features at least one unchecked exception in its
16000    `throws' clause. If it's the case, it returns `true', `false'
16001    otherwise.  */
16002 
16003 static bool
ctors_unchecked_throws_clause_p(class_type)16004 ctors_unchecked_throws_clause_p (class_type)
16005      tree class_type;
16006 {
16007   tree current;
16008 
16009   for (current = TYPE_METHODS (class_type); current;
16010        current = TREE_CHAIN (current))
16011     {
16012       bool ctu = false;	/* Ctor Throws Unchecked */
16013       if (DECL_CONSTRUCTOR_P (current))
16014 	{
16015 	  tree throws;
16016 	  for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
16017 	       throws = TREE_CHAIN (throws))
16018 	    if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
16019 	      ctu = true;
16020 	}
16021       /* We return false as we found one ctor that is unfit. */
16022       if (!ctu && DECL_CONSTRUCTOR_P (current))
16023 	return false;
16024     }
16025   /* All ctors feature at least one unchecked exception in their
16026      `throws' clause. */
16027   return true;
16028 }
16029 
16030 /* 15.24 Conditional Operator ?: */
16031 
16032 static tree
patch_conditional_expr(node,wfl_cond,wfl_op1)16033 patch_conditional_expr (node, wfl_cond, wfl_op1)
16034      tree node, wfl_cond, wfl_op1;
16035 {
16036   tree cond = TREE_OPERAND (node, 0);
16037   tree op1 = TREE_OPERAND (node, 1);
16038   tree op2 = TREE_OPERAND (node, 2);
16039   tree resulting_type = NULL_TREE;
16040   tree t1, t2, patched;
16041   int error_found = 0;
16042 
16043   /* Operands of ?: might be StringBuffers crafted as a result of a
16044      string concatenation. Obtain a descent operand here.  */
16045   if ((patched = patch_string (op1)))
16046     TREE_OPERAND (node, 1) = op1 = patched;
16047   if ((patched = patch_string (op2)))
16048     TREE_OPERAND (node, 2) = op2 = patched;
16049 
16050   t1 = TREE_TYPE (op1);
16051   t2 = TREE_TYPE (op2);
16052 
16053   /* The first expression must be a boolean */
16054   if (TREE_TYPE (cond) != boolean_type_node)
16055     {
16056       SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
16057       parse_error_context (wfl_operator,
16058                "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
16059 			   lang_printable_name (TREE_TYPE (cond), 0));
16060       error_found = 1;
16061     }
16062 
16063   /* Second and third can be numeric, boolean (i.e. primitive),
16064      references or null. Anything else results in an error */
16065   if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
16066 	|| ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
16067 	    && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
16068 	|| (t1 == boolean_type_node && t2 == boolean_type_node)))
16069     error_found = 1;
16070 
16071   /* Determine the type of the conditional expression. Same types are
16072      easy to deal with */
16073   else if (t1 == t2)
16074     resulting_type = t1;
16075 
16076   /* There are different rules for numeric types */
16077   else if (JNUMERIC_TYPE_P (t1))
16078     {
16079       /* if byte/short found, the resulting type is short */
16080       if ((t1 == byte_type_node && t2 == short_type_node)
16081 	  || (t1 == short_type_node && t2 == byte_type_node))
16082 	resulting_type = short_type_node;
16083 
16084       /* If t1 is a constant int and t2 is of type byte, short or char
16085 	 and t1's value fits in t2, then the resulting type is t2 */
16086       else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
16087 	  && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
16088 	resulting_type = t2;
16089 
16090       /* If t2 is a constant int and t1 is of type byte, short or char
16091 	 and t2's value fits in t1, then the resulting type is t1 */
16092       else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
16093 	  && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
16094 	resulting_type = t1;
16095 
16096       /* Otherwise, binary numeric promotion is applied and the
16097 	 resulting type is the promoted type of operand 1 and 2 */
16098       else
16099 	resulting_type = binary_numeric_promotion (t1, t2,
16100 						   &TREE_OPERAND (node, 1),
16101 						   &TREE_OPERAND (node, 2));
16102     }
16103 
16104   /* Cases of a reference and a null type */
16105   else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
16106     resulting_type = t1;
16107 
16108   else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
16109     resulting_type = t2;
16110 
16111   /* Last case: different reference types. If a type can be converted
16112      into the other one by assignment conversion, the latter
16113      determines the type of the expression */
16114   else if ((resulting_type = try_reference_assignconv (t1, op2)))
16115     resulting_type = promote_type (t1);
16116 
16117   else if ((resulting_type = try_reference_assignconv (t2, op1)))
16118     resulting_type = promote_type (t2);
16119 
16120   /* If we don't have any resulting type, we're in trouble */
16121   if (!resulting_type)
16122     {
16123       char *t = xstrdup (lang_printable_name (t1, 0));
16124       SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
16125       parse_error_context (wfl_operator,
16126 		 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
16127 			   t, lang_printable_name (t2, 0));
16128       free (t);
16129       error_found = 1;
16130     }
16131 
16132   if (error_found)
16133     {
16134       TREE_TYPE (node) = error_mark_node;
16135       return error_mark_node;
16136     }
16137 
16138   TREE_TYPE (node) = resulting_type;
16139   TREE_SET_CODE (node, COND_EXPR);
16140   CAN_COMPLETE_NORMALLY (node) = 1;
16141   return node;
16142 }
16143 
16144 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
16145 
16146 static tree
maybe_build_class_init_for_field(decl,expr)16147 maybe_build_class_init_for_field (decl, expr)
16148     tree decl, expr;
16149 {
16150   tree clas = DECL_CONTEXT (decl);
16151   if (flag_emit_class_files || flag_emit_xref)
16152     return expr;
16153 
16154   if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
16155       && FIELD_FINAL (decl))
16156     {
16157       tree init = DECL_INITIAL (decl);
16158       if (init != NULL_TREE)
16159 	init = fold_constant_for_init (init, decl);
16160       if (init != NULL_TREE && CONSTANT_VALUE_P (init))
16161 	return expr;
16162     }
16163 
16164   return build_class_init (clas, expr);
16165 }
16166 
16167 /* Try to constant fold NODE.
16168    If NODE is not a constant expression, return NULL_EXPR.
16169    CONTEXT is a static final VAR_DECL whose initializer we are folding. */
16170 
16171 static tree
fold_constant_for_init(node,context)16172 fold_constant_for_init (node, context)
16173      tree node;
16174      tree context;
16175 {
16176   tree op0, op1, val;
16177   enum tree_code code = TREE_CODE (node);
16178 
16179   switch (code)
16180     {
16181     case INTEGER_CST:
16182       if (node == null_pointer_node)
16183 	return NULL_TREE;
16184     case STRING_CST:
16185     case REAL_CST:
16186       return node;
16187 
16188     case PLUS_EXPR:
16189     case MINUS_EXPR:
16190     case MULT_EXPR:
16191     case TRUNC_MOD_EXPR:
16192     case RDIV_EXPR:
16193     case LSHIFT_EXPR:
16194     case RSHIFT_EXPR:
16195     case URSHIFT_EXPR:
16196     case BIT_AND_EXPR:
16197     case BIT_XOR_EXPR:
16198     case BIT_IOR_EXPR:
16199     case TRUTH_ANDIF_EXPR:
16200     case TRUTH_ORIF_EXPR:
16201     case EQ_EXPR:
16202     case NE_EXPR:
16203     case GT_EXPR:
16204     case GE_EXPR:
16205     case LT_EXPR:
16206     case LE_EXPR:
16207       op0 = TREE_OPERAND (node, 0);
16208       op1 = TREE_OPERAND (node, 1);
16209       val = fold_constant_for_init (op0, context);
16210       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16211 	return NULL_TREE;
16212       TREE_OPERAND (node, 0) = val;
16213       val = fold_constant_for_init (op1, context);
16214       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16215 	return NULL_TREE;
16216       TREE_OPERAND (node, 1) = val;
16217       return patch_binop (node, op0, op1);
16218 
16219     case UNARY_PLUS_EXPR:
16220     case NEGATE_EXPR:
16221     case TRUTH_NOT_EXPR:
16222     case BIT_NOT_EXPR:
16223     case CONVERT_EXPR:
16224       op0 = TREE_OPERAND (node, 0);
16225       val = fold_constant_for_init (op0, context);
16226       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16227 	return NULL_TREE;
16228       TREE_OPERAND (node, 0) = val;
16229       return patch_unaryop (node, op0);
16230       break;
16231 
16232     case COND_EXPR:
16233       val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
16234       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16235 	return NULL_TREE;
16236       TREE_OPERAND (node, 0) = val;
16237       val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
16238       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16239 	return NULL_TREE;
16240       TREE_OPERAND (node, 1) = val;
16241       val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
16242       if (val == NULL_TREE || ! TREE_CONSTANT (val))
16243 	return NULL_TREE;
16244       TREE_OPERAND (node, 2) = val;
16245       return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
16246 	: TREE_OPERAND (node, 2);
16247 
16248     case VAR_DECL:
16249     case FIELD_DECL:
16250       if (! FIELD_FINAL (node)
16251 	  || DECL_INITIAL (node) == NULL_TREE)
16252 	return NULL_TREE;
16253       val = DECL_INITIAL (node);
16254       /* Guard against infinite recursion. */
16255       DECL_INITIAL (node) = NULL_TREE;
16256       val = fold_constant_for_init (val, node);
16257       if (val != NULL_TREE && TREE_CODE (val) != STRING_CST)
16258 	val = try_builtin_assignconv (NULL_TREE, TREE_TYPE (node), val);
16259       DECL_INITIAL (node) = val;
16260       return val;
16261 
16262     case EXPR_WITH_FILE_LOCATION:
16263       /* Compare java_complete_tree and resolve_expression_name. */
16264       if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
16265 	  || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
16266 	{
16267 	  tree name = EXPR_WFL_NODE (node);
16268 	  tree decl;
16269 	  if (PRIMARY_P (node))
16270 	    return NULL_TREE;
16271 	  else if (! QUALIFIED_P (name))
16272 	    {
16273 	      decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
16274 	      if (decl == NULL_TREE
16275 		  || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
16276 		return NULL_TREE;
16277 	      return fold_constant_for_init (decl, decl);
16278 	    }
16279 	  else
16280 	    {
16281 	      /* Install the proper context for the field resolution.
16282 		 The prior context is restored once the name is
16283 		 properly qualified. */
16284 	      tree saved_current_class = current_class;
16285 	      /* Wait until the USE_COMPONENT_REF re-write.  FIXME. */
16286 	      current_class = DECL_CONTEXT (context);
16287 	      qualify_ambiguous_name (node);
16288 	      current_class = saved_current_class;
16289 	      if (resolve_field_access (node, &decl, NULL)
16290 		  && decl != NULL_TREE)
16291 		return fold_constant_for_init (decl, decl);
16292 	      return NULL_TREE;
16293 	    }
16294 	}
16295       else
16296 	{
16297 	  op0 = TREE_OPERAND (node, 0);
16298 	  val = fold_constant_for_init (op0, context);
16299 	  if (val == NULL_TREE || ! TREE_CONSTANT (val))
16300 	    return NULL_TREE;
16301 	  TREE_OPERAND (node, 0) = val;
16302 	  return val;
16303 	}
16304 
16305 #ifdef USE_COMPONENT_REF
16306     case IDENTIFIER:
16307     case COMPONENT_REF:
16308       ?;
16309 #endif
16310 
16311     default:
16312       return NULL_TREE;
16313     }
16314 }
16315 
16316 #ifdef USE_COMPONENT_REF
16317 /* Context is 'T' for TypeName, 'P' for PackageName,
16318    'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
16319 
16320 tree
resolve_simple_name(name,context)16321 resolve_simple_name (name, context)
16322      tree name;
16323      int context;
16324 {
16325 }
16326 
16327 tree
resolve_qualified_name(name,context)16328 resolve_qualified_name (name, context)
16329      tree name;
16330      int context;
16331 {
16332 }
16333 #endif
16334 
16335 /* Mark P, which is really a `struct parser_ctxt **' for GC.  */
16336 
16337 static void
mark_parser_ctxt(p)16338 mark_parser_ctxt (p)
16339      void *p;
16340 {
16341   struct parser_ctxt *pc = *((struct parser_ctxt **) p);
16342 #ifndef JC1_LITE
16343   size_t i;
16344 #endif
16345 
16346   if (!pc)
16347     return;
16348 
16349 #ifndef JC1_LITE
16350   for (i = 0; i < ARRAY_SIZE (pc->modifier_ctx); ++i)
16351     ggc_mark_tree (pc->modifier_ctx[i]);
16352   ggc_mark_tree (pc->class_type);
16353   ggc_mark_tree (pc->function_decl);
16354   ggc_mark_tree (pc->package);
16355   ggc_mark_tree (pc->class_list);
16356   ggc_mark_tree (pc->current_parsed_class);
16357   ggc_mark_tree (pc->current_parsed_class_un);
16358   ggc_mark_tree (pc->non_static_initialized);
16359   ggc_mark_tree (pc->static_initialized);
16360   ggc_mark_tree (pc->instance_initializers);
16361   ggc_mark_tree (pc->import_list);
16362   ggc_mark_tree (pc->import_demand_list);
16363   ggc_mark_tree (pc->current_loop);
16364   ggc_mark_tree (pc->current_labeled_block);
16365 #endif /* JC1_LITE */
16366 
16367   if (pc->next)
16368     mark_parser_ctxt (&pc->next);
16369 }
16370 
16371 void
init_src_parse()16372 init_src_parse ()
16373 {
16374   /* Sanity check; we've been bit by this before.  */
16375   if (ARRAY_SIZE (ctxp->modifier_ctx) != MODIFIER_TK - PUBLIC_TK)
16376     abort ();
16377 }
16378 
16379 
16380 
16381 /* This section deals with the functions that are called when tables
16382    recording class initialization information are traversed.  */
16383 
16384 /* Attach to PTR (a block) the declaration found in ENTRY. */
16385 
16386 static int
attach_init_test_initialization_flags(entry,ptr)16387 attach_init_test_initialization_flags (entry, ptr)
16388      PTR *entry;
16389      PTR ptr;
16390 {
16391   tree block = (tree)ptr;
16392   struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
16393 
16394   if (block != error_mark_node)
16395     {
16396       TREE_CHAIN (ite->value) = BLOCK_EXPR_DECLS (block);
16397       BLOCK_EXPR_DECLS (block) = ite->value;
16398     }
16399   return true;
16400 }
16401 
16402 /* This function is called for each classes that is known definitely
16403    assigned when a given static method was called. This function
16404    augments a compound expression (INFO) storing all assignment to
16405    initialized static class flags if a flag already existed, otherwise
16406    a new one is created.  */
16407 
16408 static int
emit_test_initialization(entry_p,info)16409 emit_test_initialization (entry_p, info)
16410      PTR *entry_p;
16411      PTR info;
16412 {
16413   tree l = (tree) info;
16414   tree decl, init;
16415   tree key = (tree) *entry_p;
16416   tree *ite;
16417   htab_t cf_ht = DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl);
16418 
16419   /* If we haven't found a flag and we're dealing with self registered
16420      with current_function_decl, then don't do anything. Self is
16421      always added as definitely initialized but this information is
16422      valid only if used outside the current function. */
16423   if (current_function_decl == TREE_PURPOSE (l)
16424       && java_treetreehash_find (cf_ht, key) == NULL)
16425     return true;
16426 
16427   ite = java_treetreehash_new (cf_ht, key);
16428 
16429   /* If we don't have a variable, create one and install it. */
16430   if (*ite == NULL)
16431     {
16432       tree block;
16433 
16434       decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16435       MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16436       LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16437       DECL_CONTEXT (decl) = current_function_decl;
16438       DECL_INITIAL (decl) = boolean_true_node;
16439 
16440       /* The trick is to find the right context for it. */
16441       block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16442       TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16443       BLOCK_EXPR_DECLS (block) = decl;
16444       *ite = decl;
16445     }
16446   else
16447     decl = *ite;
16448 
16449   /* Now simply augment the compound that holds all the assignments
16450      pertaining to this method invocation. */
16451   init = build (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16452   TREE_SIDE_EFFECTS (init) = 1;
16453   TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16454   TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16455 
16456   return true;
16457 }
16458 
16459 #include "gt-java-parse.h"
16460 #include "gtype-java.h"
16461