Make-lang.in, [...]: Replace "GNU CC" with "GCC" in the copyright header.
[gcc.git] / gcc / java / parse.y
1 /* Source code parsing and tree node generation for the GNU compiler
2 for the Java(TM) language.
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 Free Software Foundation, Inc.
5 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
23
24 Java and all Java-based marks are trademarks or registered trademarks
25 of Sun Microsystems, Inc. in the United States and other countries.
26 The Free Software Foundation is independent of Sun Microsystems, Inc. */
27
28 /* This file parses java source code and issues a tree node image
29 suitable for code generation (byte code and targeted CPU assembly
30 language).
31
32 The grammar conforms to the Java grammar described in "The Java(TM)
33 Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
34 1996, ISBN 0-201-63451-1"
35
36 The following modifications were brought to the original grammar:
37
38 method_body: added the rule '| block SC_TK'
39 static_initializer: added the rule 'static block SC_TK'.
40
41 Note: All the extra rules described above should go away when the
42 empty_statement rule will work.
43
44 statement_nsi: 'nsi' should be read no_short_if.
45
46 Some rules have been modified to support JDK1.1 inner classes
47 definitions and other extensions. */
48
49 %{
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include <dirent.h>
55 #include "tree.h"
56 #include "rtl.h"
57 #include "real.h"
58 #include "obstack.h"
59 #include "toplev.h"
60 #include "flags.h"
61 #include "java-tree.h"
62 #include "jcf.h"
63 #include "lex.h"
64 #include "parse.h"
65 #include "zipfile.h"
66 #include "convert.h"
67 #include "buffer.h"
68 #include "xref.h"
69 #include "function.h"
70 #include "except.h"
71 #include "ggc.h"
72 #include "debug.h"
73 #include "tree-inline.h"
74
75 /* Local function prototypes */
76 static char *java_accstring_lookup PARAMS ((int));
77 static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
78 static void variable_redefinition_error PARAMS ((tree, tree, tree, int));
79 static tree create_class PARAMS ((int, tree, tree, tree));
80 static tree create_interface PARAMS ((int, tree, tree));
81 static void end_class_declaration PARAMS ((int));
82 static tree find_field PARAMS ((tree, tree));
83 static tree lookup_field_wrapper PARAMS ((tree, tree));
84 static int duplicate_declaration_error_p PARAMS ((tree, tree, tree));
85 static void register_fields PARAMS ((int, tree, tree));
86 static tree parser_qualified_classname PARAMS ((tree));
87 static int parser_check_super PARAMS ((tree, tree, tree));
88 static int parser_check_super_interface PARAMS ((tree, tree, tree));
89 static void check_modifiers_consistency PARAMS ((int));
90 static tree lookup_cl PARAMS ((tree));
91 static tree lookup_java_method2 PARAMS ((tree, tree, int));
92 static tree method_header PARAMS ((int, tree, tree, tree));
93 static void fix_method_argument_names PARAMS ((tree ,tree));
94 static tree method_declarator PARAMS ((tree, tree));
95 static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
96 ATTRIBUTE_PRINTF_2;
97 static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list))
98 ATTRIBUTE_PRINTF (2, 0);
99 static void parse_ctor_invocation_error PARAMS ((void));
100 static tree parse_jdk1_1_error PARAMS ((const char *));
101 static void complete_class_report_errors PARAMS ((jdep *));
102 static int process_imports PARAMS ((void));
103 static void read_import_dir PARAMS ((tree));
104 static int find_in_imports_on_demand PARAMS ((tree, tree));
105 static void find_in_imports PARAMS ((tree, tree));
106 static void check_inner_class_access PARAMS ((tree, tree, tree));
107 static int check_pkg_class_access PARAMS ((tree, tree, bool));
108 static void register_package PARAMS ((tree));
109 static tree resolve_package PARAMS ((tree, tree *, tree *));
110 static tree resolve_class PARAMS ((tree, tree, tree, tree));
111 static void declare_local_variables PARAMS ((int, tree, tree));
112 static void dump_java_tree PARAMS ((enum tree_dump_index, tree));
113 static void source_start_java_method PARAMS ((tree));
114 static void source_end_java_method PARAMS ((void));
115 static tree find_name_in_single_imports PARAMS ((tree));
116 static void check_abstract_method_header PARAMS ((tree));
117 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
118 static tree resolve_expression_name PARAMS ((tree, tree *));
119 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
120 static int check_class_interface_creation PARAMS ((int, int, tree,
121 tree, tree, tree));
122 static tree patch_method_invocation PARAMS ((tree, tree, tree, int,
123 int *, tree *));
124 static int breakdown_qualified PARAMS ((tree *, tree *, tree));
125 static int in_same_package PARAMS ((tree, tree));
126 static tree resolve_and_layout PARAMS ((tree, tree));
127 static tree qualify_and_find PARAMS ((tree, tree, tree));
128 static tree resolve_no_layout PARAMS ((tree, tree));
129 static int invocation_mode PARAMS ((tree, int));
130 static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
131 tree, tree));
132 static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
133 tree *, tree *));
134 static tree find_most_specific_methods_list PARAMS ((tree));
135 static int argument_types_convertible PARAMS ((tree, tree));
136 static tree patch_invoke PARAMS ((tree, tree, tree));
137 static int maybe_use_access_method PARAMS ((int, tree *, tree *));
138 static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
139 static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
140 static tree check_inner_circular_reference PARAMS ((tree, tree));
141 static tree check_circular_reference PARAMS ((tree));
142 static tree obtain_incomplete_type PARAMS ((tree));
143 static tree java_complete_lhs PARAMS ((tree));
144 static tree java_complete_tree PARAMS ((tree));
145 static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
146 static int analyze_clinit_body PARAMS ((tree, tree));
147 static int maybe_yank_clinit PARAMS ((tree));
148 static void start_complete_expand_method PARAMS ((tree));
149 static void java_complete_expand_method PARAMS ((tree));
150 static void java_expand_method_bodies PARAMS ((tree));
151 static int unresolved_type_p PARAMS ((tree, tree *));
152 static void create_jdep_list PARAMS ((struct parser_ctxt *));
153 static tree build_expr_block PARAMS ((tree, tree));
154 static tree enter_block PARAMS ((void));
155 static tree exit_block PARAMS ((void));
156 static tree lookup_name_in_blocks PARAMS ((tree));
157 static void maybe_absorb_scoping_blocks PARAMS ((void));
158 static tree build_method_invocation PARAMS ((tree, tree));
159 static tree build_new_invocation PARAMS ((tree, tree));
160 static tree build_assignment PARAMS ((int, int, tree, tree));
161 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
162 static tree patch_assignment PARAMS ((tree, tree));
163 static tree patch_binop PARAMS ((tree, tree, tree));
164 static tree build_unaryop PARAMS ((int, int, tree));
165 static tree build_incdec PARAMS ((int, int, tree, int));
166 static tree patch_unaryop PARAMS ((tree, tree));
167 static tree build_cast PARAMS ((int, tree, tree));
168 static tree build_null_of_type PARAMS ((tree));
169 static tree patch_cast PARAMS ((tree, tree));
170 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
171 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
172 static int valid_cast_to_p PARAMS ((tree, tree));
173 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
174 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
175 static tree try_reference_assignconv PARAMS ((tree, tree));
176 static tree build_unresolved_array_type PARAMS ((tree));
177 static int build_type_name_from_array_name PARAMS ((tree, tree *));
178 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
179 static tree build_array_ref PARAMS ((int, tree, tree));
180 static tree patch_array_ref PARAMS ((tree));
181 static tree make_qualified_name PARAMS ((tree, tree, int));
182 static tree merge_qualified_name PARAMS ((tree, tree));
183 static tree make_qualified_primary PARAMS ((tree, tree, int));
184 static int resolve_qualified_expression_name PARAMS ((tree, tree *,
185 tree *, tree *));
186 static void qualify_ambiguous_name PARAMS ((tree));
187 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
188 static tree build_newarray_node PARAMS ((tree, tree, int));
189 static tree patch_newarray PARAMS ((tree));
190 static tree resolve_type_during_patch PARAMS ((tree));
191 static tree build_this PARAMS ((int));
192 static tree build_wfl_wrap PARAMS ((tree, int));
193 static tree build_return PARAMS ((int, tree));
194 static tree patch_return PARAMS ((tree));
195 static tree maybe_access_field PARAMS ((tree, tree, tree));
196 static int complete_function_arguments PARAMS ((tree));
197 static int check_for_static_method_reference PARAMS ((tree, tree, tree,
198 tree, tree));
199 static int not_accessible_p PARAMS ((tree, tree, tree, int));
200 static void check_deprecation PARAMS ((tree, tree));
201 static int class_in_current_package PARAMS ((tree));
202 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
203 static tree patch_if_else_statement PARAMS ((tree));
204 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
205 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
206 static tree patch_exit_expr PARAMS ((tree));
207 static tree build_labeled_block PARAMS ((int, tree));
208 static tree finish_labeled_statement PARAMS ((tree, tree));
209 static tree build_bc_statement PARAMS ((int, int, tree));
210 static tree patch_bc_statement PARAMS ((tree));
211 static tree patch_loop_statement PARAMS ((tree));
212 static tree build_new_loop PARAMS ((tree));
213 static tree build_loop_body PARAMS ((int, tree, int));
214 static tree finish_loop_body PARAMS ((int, tree, tree, int));
215 static tree build_debugable_stmt PARAMS ((int, tree));
216 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
217 static tree patch_switch_statement PARAMS ((tree));
218 static tree string_constant_concatenation PARAMS ((tree, tree));
219 static tree build_string_concatenation PARAMS ((tree, tree));
220 static tree patch_string_cst PARAMS ((tree));
221 static tree patch_string PARAMS ((tree));
222 static tree encapsulate_with_try_catch PARAMS ((int, tree, tree, tree));
223 static tree build_assertion PARAMS ((int, tree, tree));
224 static tree build_try_statement PARAMS ((int, tree, tree));
225 static tree build_try_finally_statement PARAMS ((int, tree, tree));
226 static tree patch_try_statement PARAMS ((tree));
227 static tree patch_synchronized_statement PARAMS ((tree, tree));
228 static tree patch_throw_statement PARAMS ((tree, tree));
229 static void check_thrown_exceptions PARAMS ((int, tree));
230 static int check_thrown_exceptions_do PARAMS ((tree));
231 static void purge_unchecked_exceptions PARAMS ((tree));
232 static bool ctors_unchecked_throws_clause_p PARAMS ((tree));
233 static void check_throws_clauses PARAMS ((tree, tree, tree));
234 static void finish_method_declaration PARAMS ((tree));
235 static tree build_super_invocation PARAMS ((tree));
236 static int verify_constructor_circularity PARAMS ((tree, tree));
237 static char *constructor_circularity_msg PARAMS ((tree, tree));
238 static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
239 int, int));
240 static const char *get_printable_method_name PARAMS ((tree));
241 static tree patch_conditional_expr PARAMS ((tree, tree, tree));
242 static tree generate_finit PARAMS ((tree));
243 static tree generate_instinit PARAMS ((tree));
244 static tree build_instinit_invocation PARAMS ((tree));
245 static void fix_constructors PARAMS ((tree));
246 static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
247 tree, int *));
248 static tree craft_constructor PARAMS ((tree, tree));
249 static int verify_constructor_super PARAMS ((tree));
250 static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
251 static void start_artificial_method_body PARAMS ((tree));
252 static void end_artificial_method_body PARAMS ((tree));
253 static int check_method_redefinition PARAMS ((tree, tree));
254 static int check_method_types_complete PARAMS ((tree));
255 static void java_check_regular_methods PARAMS ((tree));
256 static void java_check_abstract_methods PARAMS ((tree));
257 static void unreachable_stmt_error PARAMS ((tree));
258 static tree find_expr_with_wfl PARAMS ((tree));
259 static void missing_return_error PARAMS ((tree));
260 static tree build_new_array_init PARAMS ((int, tree));
261 static tree patch_new_array_init PARAMS ((tree, tree));
262 static tree maybe_build_array_element_wfl PARAMS ((tree));
263 static int array_constructor_check_entry PARAMS ((tree, tree));
264 static const char *purify_type_name PARAMS ((const char *));
265 static tree fold_constant_for_init PARAMS ((tree, tree));
266 static tree strip_out_static_field_access_decl PARAMS ((tree));
267 static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
268 static void static_ref_err PARAMS ((tree, tree, tree));
269 static void parser_add_interface PARAMS ((tree, tree, tree));
270 static void add_superinterfaces PARAMS ((tree, tree));
271 static tree jdep_resolve_class PARAMS ((jdep *));
272 static int note_possible_classname PARAMS ((const char *, int));
273 static void java_complete_expand_classes PARAMS ((void));
274 static void java_complete_expand_class PARAMS ((tree));
275 static void java_complete_expand_methods PARAMS ((tree));
276 static tree cut_identifier_in_qualified PARAMS ((tree));
277 static tree java_stabilize_reference PARAMS ((tree));
278 static tree do_unary_numeric_promotion PARAMS ((tree));
279 static char * operator_string PARAMS ((tree));
280 static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
281 static tree merge_string_cste PARAMS ((tree, tree, int));
282 static tree java_refold PARAMS ((tree));
283 static int java_decl_equiv PARAMS ((tree, tree));
284 static int binop_compound_p PARAMS ((enum tree_code));
285 static tree search_loop PARAMS ((tree));
286 static int labeled_block_contains_loop_p PARAMS ((tree, tree));
287 static int check_abstract_method_definitions PARAMS ((int, tree, tree));
288 static void java_check_abstract_method_definitions PARAMS ((tree));
289 static void java_debug_context_do PARAMS ((int));
290 static void java_parser_context_push_initialized_field PARAMS ((void));
291 static void java_parser_context_pop_initialized_field PARAMS ((void));
292 static tree reorder_static_initialized PARAMS ((tree));
293 static void java_parser_context_suspend PARAMS ((void));
294 static void java_parser_context_resume PARAMS ((void));
295 static int pop_current_osb PARAMS ((struct parser_ctxt *));
296
297 /* JDK 1.1 work. FIXME */
298
299 static tree maybe_make_nested_class_name PARAMS ((tree));
300 static int make_nested_class_name PARAMS ((tree));
301 static void set_nested_class_simple_name_value PARAMS ((tree, int));
302 static void link_nested_class_to_enclosing PARAMS ((void));
303 static tree resolve_inner_class PARAMS ((htab_t, tree, tree *, tree *, tree));
304 static tree find_as_inner_class PARAMS ((tree, tree, tree));
305 static tree find_as_inner_class_do PARAMS ((tree, tree));
306 static int check_inner_class_redefinition PARAMS ((tree, tree));
307
308 static tree build_thisn_assign PARAMS ((void));
309 static tree build_current_thisn PARAMS ((tree));
310 static tree build_access_to_thisn PARAMS ((tree, tree, int));
311 static tree maybe_build_thisn_access_method PARAMS ((tree));
312
313 static tree build_outer_field_access PARAMS ((tree, tree));
314 static tree build_outer_field_access_methods PARAMS ((tree));
315 static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
316 tree, tree));
317 static tree build_outer_method_access_method PARAMS ((tree));
318 static tree build_new_access_id PARAMS ((void));
319 static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
320 tree, tree));
321
322 static int outer_field_access_p PARAMS ((tree, tree));
323 static int outer_field_expanded_access_p PARAMS ((tree, tree *,
324 tree *, tree *));
325 static tree outer_field_access_fix PARAMS ((tree, tree, tree));
326 static tree build_incomplete_class_ref PARAMS ((int, tree));
327 static tree patch_incomplete_class_ref PARAMS ((tree));
328 static tree create_anonymous_class PARAMS ((int, tree));
329 static void patch_anonymous_class PARAMS ((tree, tree, tree));
330 static void add_inner_class_fields PARAMS ((tree, tree));
331
332 static tree build_dot_class_method PARAMS ((tree));
333 static tree build_dot_class_method_invocation PARAMS ((tree));
334 static void create_new_parser_context PARAMS ((int));
335 static void mark_parser_ctxt PARAMS ((void *));
336 static tree maybe_build_class_init_for_field PARAMS ((tree, tree));
337
338 static int attach_init_test_initialization_flags PARAMS ((PTR *, PTR));
339 static int emit_test_initialization PARAMS ((PTR *, PTR));
340
341 static char *string_convert_int_cst PARAMS ((tree));
342
343 /* Number of error found so far. */
344 int java_error_count;
345 /* Number of warning found so far. */
346 int java_warning_count;
347 /* Tell when not to fold, when doing xrefs */
348 int do_not_fold;
349 /* Cyclic inheritance report, as it can be set by layout_class */
350 const char *cyclic_inheritance_report;
351
352 /* The current parser context */
353 struct parser_ctxt *ctxp;
354
355 /* List of things that were analyzed for which code will be generated */
356 struct parser_ctxt *ctxp_for_generation = NULL;
357
358 /* binop_lookup maps token to tree_code. It is used where binary
359 operations are involved and required by the parser. RDIV_EXPR
360 covers both integral/floating point division. The code is changed
361 once the type of both operator is worked out. */
362
363 static const enum tree_code binop_lookup[19] =
364 {
365 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
366 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
367 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
368 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
369 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
370 };
371 #define BINOP_LOOKUP(VALUE) \
372 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
373
374 /* This is the end index for binary operators that can also be used
375 in compound assignements. */
376 #define BINOP_COMPOUND_CANDIDATES 11
377
378 /* The "$L" identifier we use to create labels. */
379 static GTY(()) tree label_id;
380
381 /* The "StringBuffer" identifier used for the String `+' operator. */
382 static GTY(()) tree wfl_string_buffer;
383
384 /* The "append" identifier used for String `+' operator. */
385 static GTY(()) tree wfl_append;
386
387 /* The "toString" identifier used for String `+' operator. */
388 static GTY(()) tree wfl_to_string;
389
390 /* The "java.lang" import qualified name. */
391 static GTY(()) tree java_lang_id;
392
393 /* The generated `inst$' identifier used for generated enclosing
394 instance/field access functions. */
395 static GTY(()) tree inst_id;
396
397 /* Context and flag for static blocks */
398 static GTY(()) tree current_static_block;
399
400 /* The generated `write_parm_value$' identifier. */
401 static GTY(()) tree wpv_id;
402
403 /* The list of all packages we've seen so far */
404 static GTY(()) tree package_list;
405
406 /* Hold THIS for the scope of the current method decl. */
407 static GTY(()) tree current_this;
408
409 /* Hold a list of catch clauses list. The first element of this list is
410 the list of the catch clauses of the currently analysed try block. */
411 static GTY(()) tree currently_caught_type_list;
412
413 /* This holds a linked list of all the case labels for the current
414 switch statement. It is only used when checking to see if there
415 are duplicate labels. FIXME: probably this should just be attached
416 to the switch itself; then it could be referenced via
417 `ctxp->current_loop'. */
418 static GTY(()) tree case_label_list;
419
420 /* Anonymous class counter. Will be reset to 1 every time a non
421 anonymous class gets created. */
422 static int anonymous_class_counter = 1;
423
424 static GTY(()) tree src_parse_roots[1];
425
426 /* All classes seen from source code */
427 #define gclass_list src_parse_roots[0]
428
429 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
430 line and point it out. */
431 /* Should point out the one that don't fit. ASCII/unicode, going
432 backward. FIXME */
433
434 #define check_modifiers(__message, __value, __mask) do { \
435 if ((__value) & ~(__mask)) \
436 { \
437 size_t i, remainder = (__value) & ~(__mask); \
438 for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++) \
439 if ((1 << i) & remainder) \
440 parse_error_context (ctxp->modifier_ctx [i], (__message), \
441 java_accstring_lookup (1 << i)); \
442 } \
443 } while (0)
444
445 %}
446
447 %union {
448 tree node;
449 int sub_token;
450 struct {
451 int token;
452 int location;
453 } operator;
454 int value;
455 }
456
457 %{
458 #include "lex.c"
459 %}
460
461 %pure_parser
462
463 /* Things defined here have to match the order of what's in the
464 binop_lookup table. */
465
466 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
467 %token LS_TK SRS_TK ZRS_TK
468 %token AND_TK XOR_TK OR_TK
469 %token BOOL_AND_TK BOOL_OR_TK
470 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
471
472 /* This maps to the same binop_lookup entry than the token above */
473
474 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
475 %token REM_ASSIGN_TK
476 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
477 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
478
479
480 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
481
482 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
483 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
484 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
485 %token PAD_TK ABSTRACT_TK STRICT_TK
486 %token MODIFIER_TK
487
488 /* Keep those two in order, too */
489 %token DECR_TK INCR_TK
490
491 /* From now one, things can be in any order */
492
493 %token DEFAULT_TK IF_TK THROW_TK
494 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
495 %token THROWS_TK BREAK_TK IMPORT_TK
496 %token ELSE_TK INSTANCEOF_TK RETURN_TK
497 %token VOID_TK CATCH_TK INTERFACE_TK
498 %token CASE_TK EXTENDS_TK FINALLY_TK
499 %token SUPER_TK WHILE_TK CLASS_TK
500 %token SWITCH_TK CONST_TK TRY_TK
501 %token FOR_TK NEW_TK CONTINUE_TK
502 %token GOTO_TK PACKAGE_TK THIS_TK
503 %token ASSERT_TK
504
505 %token BYTE_TK SHORT_TK INT_TK LONG_TK
506 %token CHAR_TK INTEGRAL_TK
507
508 %token FLOAT_TK DOUBLE_TK FP_TK
509
510 %token ID_TK
511
512 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
513
514 %token ASSIGN_ANY_TK ASSIGN_TK
515 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
516
517 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
518 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
519
520 %type <value> modifiers MODIFIER_TK final synchronized
521
522 %type <node> super ID_TK identifier
523 %type <node> name simple_name qualified_name
524 %type <node> type_declaration compilation_unit
525 field_declaration method_declaration extends_interfaces
526 interfaces interface_type_list
527 import_declarations package_declaration
528 type_declarations interface_body
529 interface_member_declaration constant_declaration
530 interface_member_declarations interface_type
531 abstract_method_declaration
532 %type <node> class_body_declaration class_member_declaration
533 static_initializer constructor_declaration block
534 %type <node> class_body_declarations constructor_header
535 %type <node> class_or_interface_type class_type class_type_list
536 constructor_declarator explicit_constructor_invocation
537 %type <node> dim_expr dim_exprs this_or_super throws
538
539 %type <node> variable_declarator_id variable_declarator
540 variable_declarators variable_initializer
541 variable_initializers constructor_body
542 array_initializer
543
544 %type <node> class_body block_end constructor_block_end
545 %type <node> statement statement_without_trailing_substatement
546 labeled_statement if_then_statement label_decl
547 if_then_else_statement while_statement for_statement
548 statement_nsi labeled_statement_nsi do_statement
549 if_then_else_statement_nsi while_statement_nsi
550 for_statement_nsi statement_expression_list for_init
551 for_update statement_expression expression_statement
552 primary_no_new_array expression primary
553 array_creation_expression array_type
554 class_instance_creation_expression field_access
555 method_invocation array_access something_dot_new
556 argument_list postfix_expression while_expression
557 post_increment_expression post_decrement_expression
558 unary_expression_not_plus_minus unary_expression
559 pre_increment_expression pre_decrement_expression
560 cast_expression
561 multiplicative_expression additive_expression
562 shift_expression relational_expression
563 equality_expression and_expression
564 exclusive_or_expression inclusive_or_expression
565 conditional_and_expression conditional_or_expression
566 conditional_expression assignment_expression
567 left_hand_side assignment for_header for_begin
568 constant_expression do_statement_begin empty_statement
569 switch_statement synchronized_statement throw_statement
570 try_statement assert_statement
571 switch_expression switch_block
572 catches catch_clause catch_clause_parameter finally
573 anonymous_class_creation trap_overflow_corner_case
574 %type <node> return_statement break_statement continue_statement
575
576 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
577 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
578 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
579 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
580 %type <operator> ASSIGN_ANY_TK assignment_operator
581 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
582 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
583 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
584 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
585 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
586 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
587 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
588 %type <operator> NEW_TK ASSERT_TK
589
590 %type <node> method_body
591
592 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
593 STRING_LIT_TK NULL_TK VOID_TK
594
595 %type <node> IF_TK WHILE_TK FOR_TK
596
597 %type <node> formal_parameter_list formal_parameter
598 method_declarator method_header
599
600 %type <node> primitive_type reference_type type
601 BOOLEAN_TK INTEGRAL_TK FP_TK
602
603 /* Added or modified JDK 1.1 rule types */
604 %type <node> type_literals
605
606 %%
607 /* 19.2 Production from 2.3: The Syntactic Grammar */
608 goal:
609 {
610 /* Register static variables with the garbage
611 collector. */
612 ggc_add_root (&ctxp, 1,
613 sizeof (struct parser_ctxt *),
614 mark_parser_ctxt);
615 ggc_add_root (&ctxp_for_generation, 1,
616 sizeof (struct parser_ctxt *),
617 mark_parser_ctxt);
618 }
619 compilation_unit
620 {}
621 ;
622
623 /* 19.3 Productions from 3: Lexical structure */
624 literal:
625 INT_LIT_TK
626 | FP_LIT_TK
627 | BOOL_LIT_TK
628 | CHAR_LIT_TK
629 | STRING_LIT_TK
630 | NULL_TK
631 ;
632
633 /* 19.4 Productions from 4: Types, Values and Variables */
634 type:
635 primitive_type
636 | reference_type
637 ;
638
639 primitive_type:
640 INTEGRAL_TK
641 | FP_TK
642 | BOOLEAN_TK
643 ;
644
645 reference_type:
646 class_or_interface_type
647 | array_type
648 ;
649
650 class_or_interface_type:
651 name
652 ;
653
654 class_type:
655 class_or_interface_type /* Default rule */
656 ;
657
658 interface_type:
659 class_or_interface_type
660 ;
661
662 array_type:
663 primitive_type dims
664 {
665 int osb = pop_current_osb (ctxp);
666 tree t = build_java_array_type (($1), -1);
667 while (--osb)
668 t = build_unresolved_array_type (t);
669 $$ = t;
670 }
671 | name dims
672 {
673 int osb = pop_current_osb (ctxp);
674 tree t = $1;
675 while (osb--)
676 t = build_unresolved_array_type (t);
677 $$ = t;
678 }
679 ;
680
681 /* 19.5 Productions from 6: Names */
682 name:
683 simple_name /* Default rule */
684 | qualified_name /* Default rule */
685 ;
686
687 simple_name:
688 identifier /* Default rule */
689 ;
690
691 qualified_name:
692 name DOT_TK identifier
693 { $$ = make_qualified_name ($1, $3, $2.location); }
694 ;
695
696 identifier:
697 ID_TK
698 ;
699
700 /* 19.6: Production from 7: Packages */
701 compilation_unit:
702 {$$ = NULL;}
703 | package_declaration
704 | import_declarations
705 | type_declarations
706 | package_declaration import_declarations
707 | package_declaration type_declarations
708 | import_declarations type_declarations
709 | package_declaration import_declarations type_declarations
710 ;
711
712 import_declarations:
713 import_declaration
714 {
715 $$ = NULL;
716 }
717 | import_declarations import_declaration
718 {
719 $$ = NULL;
720 }
721 ;
722
723 type_declarations:
724 type_declaration
725 | type_declarations type_declaration
726 ;
727
728 package_declaration:
729 PACKAGE_TK name SC_TK
730 {
731 ctxp->package = EXPR_WFL_NODE ($2);
732 register_package (ctxp->package);
733 }
734 | PACKAGE_TK error
735 {yyerror ("Missing name"); RECOVER;}
736 | PACKAGE_TK name error
737 {yyerror ("';' expected"); RECOVER;}
738 ;
739
740 import_declaration:
741 single_type_import_declaration
742 | type_import_on_demand_declaration
743 ;
744
745 single_type_import_declaration:
746 IMPORT_TK name SC_TK
747 {
748 tree name = EXPR_WFL_NODE ($2), last_name;
749 int i = IDENTIFIER_LENGTH (name)-1;
750 const char *last = &IDENTIFIER_POINTER (name)[i];
751 while (last != IDENTIFIER_POINTER (name))
752 {
753 if (last [0] == '.')
754 break;
755 last--;
756 }
757 last_name = get_identifier (++last);
758 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
759 {
760 tree err = find_name_in_single_imports (last_name);
761 if (err && err != name)
762 parse_error_context
763 ($2, "Ambiguous class: `%s' and `%s'",
764 IDENTIFIER_POINTER (name),
765 IDENTIFIER_POINTER (err));
766 else
767 REGISTER_IMPORT ($2, last_name);
768 }
769 else
770 REGISTER_IMPORT ($2, last_name);
771 }
772 | IMPORT_TK error
773 {yyerror ("Missing name"); RECOVER;}
774 | IMPORT_TK name error
775 {yyerror ("';' expected"); RECOVER;}
776 ;
777
778 type_import_on_demand_declaration:
779 IMPORT_TK name DOT_TK MULT_TK SC_TK
780 {
781 tree name = EXPR_WFL_NODE ($2);
782 tree it;
783 /* Search for duplicates. */
784 for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
785 if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
786 break;
787 /* Don't import the same thing more than once, just ignore
788 duplicates (7.5.2) */
789 if (! it)
790 {
791 read_import_dir ($2);
792 ctxp->import_demand_list =
793 chainon (ctxp->import_demand_list,
794 build_tree_list ($2, NULL_TREE));
795 }
796 }
797 | IMPORT_TK name DOT_TK error
798 {yyerror ("'*' expected"); RECOVER;}
799 | IMPORT_TK name DOT_TK MULT_TK error
800 {yyerror ("';' expected"); RECOVER;}
801 ;
802
803 type_declaration:
804 class_declaration
805 { end_class_declaration (0); }
806 | interface_declaration
807 { end_class_declaration (0); }
808 | empty_statement
809 | error
810 {
811 YYERROR_NOW;
812 yyerror ("Class or interface declaration expected");
813 }
814 ;
815
816 /* 19.7 Shortened from the original:
817 modifiers: modifier | modifiers modifier
818 modifier: any of public... */
819 modifiers:
820 MODIFIER_TK
821 {
822 $$ = (1 << $1);
823 }
824 | modifiers MODIFIER_TK
825 {
826 int acc = (1 << $2);
827 if ($$ & acc)
828 parse_error_context
829 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
830 java_accstring_lookup (acc));
831 else
832 {
833 $$ |= acc;
834 }
835 }
836 ;
837
838 /* 19.8.1 Production from $8.1: Class Declaration */
839 class_declaration:
840 modifiers CLASS_TK identifier super interfaces
841 { create_class ($1, $3, $4, $5); }
842 class_body
843 {;}
844 | CLASS_TK identifier super interfaces
845 { create_class (0, $2, $3, $4); }
846 class_body
847 {;}
848 | modifiers CLASS_TK error
849 { yyerror ("Missing class name"); RECOVER; }
850 | CLASS_TK error
851 { yyerror ("Missing class name"); RECOVER; }
852 | CLASS_TK identifier error
853 {
854 if (!ctxp->class_err) yyerror ("'{' expected");
855 DRECOVER(class1);
856 }
857 | modifiers CLASS_TK identifier error
858 { if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER; }
859 ;
860
861 super:
862 { $$ = NULL; }
863 | EXTENDS_TK class_type
864 { $$ = $2; }
865 | EXTENDS_TK class_type error
866 {yyerror ("'{' expected"); ctxp->class_err=1;}
867 | EXTENDS_TK error
868 {yyerror ("Missing super class name"); ctxp->class_err=1;}
869 ;
870
871 interfaces:
872 { $$ = NULL_TREE; }
873 | IMPLEMENTS_TK interface_type_list
874 { $$ = $2; }
875 | IMPLEMENTS_TK error
876 {
877 ctxp->class_err=1;
878 yyerror ("Missing interface name");
879 }
880 ;
881
882 interface_type_list:
883 interface_type
884 {
885 ctxp->interface_number = 1;
886 $$ = build_tree_list ($1, NULL_TREE);
887 }
888 | interface_type_list C_TK interface_type
889 {
890 ctxp->interface_number++;
891 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
892 }
893 | interface_type_list C_TK error
894 {yyerror ("Missing interface name"); RECOVER;}
895 ;
896
897 class_body:
898 OCB_TK CCB_TK
899 {
900 /* Store the location of the `}' when doing xrefs */
901 if (flag_emit_xref)
902 DECL_END_SOURCE_LINE (GET_CPC ()) =
903 EXPR_WFL_ADD_COL ($2.location, 1);
904 $$ = GET_CPC ();
905 }
906 | OCB_TK class_body_declarations CCB_TK
907 {
908 /* Store the location of the `}' when doing xrefs */
909 if (flag_emit_xref)
910 DECL_END_SOURCE_LINE (GET_CPC ()) =
911 EXPR_WFL_ADD_COL ($3.location, 1);
912 $$ = GET_CPC ();
913 }
914 ;
915
916 class_body_declarations:
917 class_body_declaration
918 | class_body_declarations class_body_declaration
919 ;
920
921 class_body_declaration:
922 class_member_declaration
923 | static_initializer
924 | constructor_declaration
925 | block /* Added, JDK1.1, instance initializer */
926 {
927 if ($1 != empty_stmt_node)
928 {
929 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
930 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
931 }
932 }
933 ;
934
935 class_member_declaration:
936 field_declaration
937 | method_declaration
938 | class_declaration /* Added, JDK1.1 inner classes */
939 { end_class_declaration (1); }
940 | interface_declaration /* Added, JDK1.1 inner interfaces */
941 { end_class_declaration (1); }
942 | empty_statement
943 ;
944
945 /* 19.8.2 Productions from 8.3: Field Declarations */
946 field_declaration:
947 type variable_declarators SC_TK
948 { register_fields (0, $1, $2); }
949 | modifiers type variable_declarators SC_TK
950 {
951 check_modifiers
952 ("Illegal modifier `%s' for field declaration",
953 $1, FIELD_MODIFIERS);
954 check_modifiers_consistency ($1);
955 register_fields ($1, $2, $3);
956 }
957 ;
958
959 variable_declarators:
960 /* Should we use build_decl_list () instead ? FIXME */
961 variable_declarator /* Default rule */
962 | variable_declarators C_TK variable_declarator
963 { $$ = chainon ($1, $3); }
964 | variable_declarators C_TK error
965 {yyerror ("Missing term"); RECOVER;}
966 ;
967
968 variable_declarator:
969 variable_declarator_id
970 { $$ = build_tree_list ($1, NULL_TREE); }
971 | variable_declarator_id ASSIGN_TK variable_initializer
972 {
973 if (java_error_count)
974 $3 = NULL_TREE;
975 $$ = build_tree_list
976 ($1, build_assignment ($2.token, $2.location, $1, $3));
977 }
978 | variable_declarator_id ASSIGN_TK error
979 {
980 yyerror ("Missing variable initializer");
981 $$ = build_tree_list ($1, NULL_TREE);
982 RECOVER;
983 }
984 | variable_declarator_id ASSIGN_TK variable_initializer error
985 {
986 yyerror ("';' expected");
987 $$ = build_tree_list ($1, NULL_TREE);
988 RECOVER;
989 }
990 ;
991
992 variable_declarator_id:
993 identifier
994 | variable_declarator_id OSB_TK CSB_TK
995 { $$ = build_unresolved_array_type ($1); }
996 | identifier error
997 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
998 | variable_declarator_id OSB_TK error
999 {
1000 yyerror ("']' expected");
1001 DRECOVER(vdi);
1002 }
1003 | variable_declarator_id CSB_TK error
1004 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1005 ;
1006
1007 variable_initializer:
1008 expression
1009 | array_initializer
1010 ;
1011
1012 /* 19.8.3 Productions from 8.4: Method Declarations */
1013 method_declaration:
1014 method_header
1015 {
1016 current_function_decl = $1;
1017 if (current_function_decl
1018 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1019 source_start_java_method (current_function_decl);
1020 else
1021 current_function_decl = NULL_TREE;
1022 }
1023 method_body
1024 { finish_method_declaration ($3); }
1025 | method_header error
1026 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1027 ;
1028
1029 method_header:
1030 type method_declarator throws
1031 { $$ = method_header (0, $1, $2, $3); }
1032 | VOID_TK method_declarator throws
1033 { $$ = method_header (0, void_type_node, $2, $3); }
1034 | modifiers type method_declarator throws
1035 { $$ = method_header ($1, $2, $3, $4); }
1036 | modifiers VOID_TK method_declarator throws
1037 { $$ = method_header ($1, void_type_node, $3, $4); }
1038 | type error
1039 {
1040 yyerror ("Invalid method declaration, method name required");
1041 RECOVER;
1042 }
1043 | modifiers type error
1044 {
1045 yyerror ("Identifier expected");
1046 RECOVER;
1047 }
1048 | VOID_TK error
1049 {
1050 yyerror ("Identifier expected");
1051 RECOVER;
1052 }
1053 | modifiers VOID_TK error
1054 {
1055 yyerror ("Identifier expected");
1056 RECOVER;
1057 }
1058 | modifiers error
1059 {
1060 yyerror ("Invalid method declaration, return type required");
1061 RECOVER;
1062 }
1063 ;
1064
1065 method_declarator:
1066 identifier OP_TK CP_TK
1067 {
1068 ctxp->formal_parameter_number = 0;
1069 $$ = method_declarator ($1, NULL_TREE);
1070 }
1071 | identifier OP_TK formal_parameter_list CP_TK
1072 { $$ = method_declarator ($1, $3); }
1073 | method_declarator OSB_TK CSB_TK
1074 {
1075 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1076 TREE_PURPOSE ($1) =
1077 build_unresolved_array_type (TREE_PURPOSE ($1));
1078 parse_warning_context
1079 (wfl_operator,
1080 "Discouraged form of returned type specification");
1081 }
1082 | identifier OP_TK error
1083 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1084 | method_declarator OSB_TK error
1085 {yyerror ("']' expected"); RECOVER;}
1086 ;
1087
1088 formal_parameter_list:
1089 formal_parameter
1090 {
1091 ctxp->formal_parameter_number = 1;
1092 }
1093 | formal_parameter_list C_TK formal_parameter
1094 {
1095 ctxp->formal_parameter_number += 1;
1096 $$ = chainon ($1, $3);
1097 }
1098 | formal_parameter_list C_TK error
1099 { yyerror ("Missing formal parameter term"); RECOVER; }
1100 ;
1101
1102 formal_parameter:
1103 type variable_declarator_id
1104 {
1105 $$ = build_tree_list ($2, $1);
1106 }
1107 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1108 {
1109 $$ = build_tree_list ($3, $2);
1110 ARG_FINAL_P ($$) = 1;
1111 }
1112 | type error
1113 {
1114 yyerror ("Missing identifier"); RECOVER;
1115 $$ = NULL_TREE;
1116 }
1117 | final type error
1118 {
1119 yyerror ("Missing identifier"); RECOVER;
1120 $$ = NULL_TREE;
1121 }
1122 ;
1123
1124 final:
1125 modifiers
1126 {
1127 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1128 $1, ACC_FINAL);
1129 if ($1 != ACC_FINAL)
1130 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1131 }
1132 ;
1133
1134 throws:
1135 { $$ = NULL_TREE; }
1136 | THROWS_TK class_type_list
1137 { $$ = $2; }
1138 | THROWS_TK error
1139 {yyerror ("Missing class type term"); RECOVER;}
1140 ;
1141
1142 class_type_list:
1143 class_type
1144 { $$ = build_tree_list ($1, $1); }
1145 | class_type_list C_TK class_type
1146 { $$ = tree_cons ($3, $3, $1); }
1147 | class_type_list C_TK error
1148 {yyerror ("Missing class type term"); RECOVER;}
1149 ;
1150
1151 method_body:
1152 block
1153 | SC_TK { $$ = NULL_TREE; }
1154 ;
1155
1156 /* 19.8.4 Productions from 8.5: Static Initializers */
1157 static_initializer:
1158 static block
1159 {
1160 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1161 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1162 current_static_block = NULL_TREE;
1163 }
1164 ;
1165
1166 static: /* Test lval.sub_token here */
1167 modifiers
1168 {
1169 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1170 /* Can't have a static initializer in an innerclass */
1171 if ($1 | ACC_STATIC &&
1172 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1173 parse_error_context
1174 (MODIFIER_WFL (STATIC_TK),
1175 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1176 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1177 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1178 }
1179 ;
1180
1181 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1182 constructor_declaration:
1183 constructor_header
1184 {
1185 current_function_decl = $1;
1186 source_start_java_method (current_function_decl);
1187 }
1188 constructor_body
1189 { finish_method_declaration ($3); }
1190 ;
1191
1192 constructor_header:
1193 constructor_declarator throws
1194 { $$ = method_header (0, NULL_TREE, $1, $2); }
1195 | modifiers constructor_declarator throws
1196 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1197 ;
1198
1199 constructor_declarator:
1200 simple_name OP_TK CP_TK
1201 {
1202 ctxp->formal_parameter_number = 0;
1203 $$ = method_declarator ($1, NULL_TREE);
1204 }
1205 | simple_name OP_TK formal_parameter_list CP_TK
1206 { $$ = method_declarator ($1, $3); }
1207 ;
1208
1209 constructor_body:
1210 /* Unlike regular method, we always need a complete (empty)
1211 body so we can safely perform all the required code
1212 addition (super invocation and field initialization) */
1213 block_begin constructor_block_end
1214 {
1215 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1216 $$ = $2;
1217 }
1218 | block_begin explicit_constructor_invocation constructor_block_end
1219 { $$ = $3; }
1220 | block_begin block_statements constructor_block_end
1221 { $$ = $3; }
1222 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1223 { $$ = $4; }
1224 ;
1225
1226 constructor_block_end:
1227 block_end
1228 ;
1229
1230 /* Error recovery for that rule moved down expression_statement: rule. */
1231 explicit_constructor_invocation:
1232 this_or_super OP_TK CP_TK SC_TK
1233 {
1234 $$ = build_method_invocation ($1, NULL_TREE);
1235 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1236 $$ = java_method_add_stmt (current_function_decl, $$);
1237 }
1238 | this_or_super OP_TK argument_list CP_TK SC_TK
1239 {
1240 $$ = build_method_invocation ($1, $3);
1241 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1242 $$ = java_method_add_stmt (current_function_decl, $$);
1243 }
1244 /* Added, JDK1.1 inner classes. Modified because the rule
1245 'primary' couldn't work. */
1246 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1247 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1248 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1249 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1250 ;
1251
1252 this_or_super: /* Added, simplifies error diagnostics */
1253 THIS_TK
1254 {
1255 tree wfl = build_wfl_node (this_identifier_node);
1256 EXPR_WFL_LINECOL (wfl) = $1.location;
1257 $$ = wfl;
1258 }
1259 | SUPER_TK
1260 {
1261 tree wfl = build_wfl_node (super_identifier_node);
1262 EXPR_WFL_LINECOL (wfl) = $1.location;
1263 $$ = wfl;
1264 }
1265 ;
1266
1267 /* 19.9 Productions from 9: Interfaces */
1268 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1269 interface_declaration:
1270 INTERFACE_TK identifier
1271 { create_interface (0, $2, NULL_TREE); }
1272 interface_body
1273 { ; }
1274 | modifiers INTERFACE_TK identifier
1275 { create_interface ($1, $3, NULL_TREE); }
1276 interface_body
1277 { ; }
1278 | INTERFACE_TK identifier extends_interfaces
1279 { create_interface (0, $2, $3); }
1280 interface_body
1281 { ; }
1282 | modifiers INTERFACE_TK identifier extends_interfaces
1283 { create_interface ($1, $3, $4); }
1284 interface_body
1285 { ; }
1286 | INTERFACE_TK identifier error
1287 { yyerror ("'{' expected"); RECOVER; }
1288 | modifiers INTERFACE_TK identifier error
1289 { yyerror ("'{' expected"); RECOVER; }
1290 ;
1291
1292 extends_interfaces:
1293 EXTENDS_TK interface_type
1294 {
1295 ctxp->interface_number = 1;
1296 $$ = build_tree_list ($2, NULL_TREE);
1297 }
1298 | extends_interfaces C_TK interface_type
1299 {
1300 ctxp->interface_number++;
1301 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1302 }
1303 | EXTENDS_TK error
1304 {yyerror ("Invalid interface type"); RECOVER;}
1305 | extends_interfaces C_TK error
1306 {yyerror ("Missing term"); RECOVER;}
1307 ;
1308
1309 interface_body:
1310 OCB_TK CCB_TK
1311 { $$ = NULL_TREE; }
1312 | OCB_TK interface_member_declarations CCB_TK
1313 { $$ = NULL_TREE; }
1314 ;
1315
1316 interface_member_declarations:
1317 interface_member_declaration
1318 | interface_member_declarations interface_member_declaration
1319 ;
1320
1321 interface_member_declaration:
1322 constant_declaration
1323 | abstract_method_declaration
1324 | class_declaration /* Added, JDK1.1 inner classes */
1325 { end_class_declaration (1); }
1326 | interface_declaration /* Added, JDK1.1 inner interfaces */
1327 { end_class_declaration (1); }
1328 ;
1329
1330 constant_declaration:
1331 field_declaration
1332 ;
1333
1334 abstract_method_declaration:
1335 method_header SC_TK
1336 {
1337 check_abstract_method_header ($1);
1338 current_function_decl = NULL_TREE; /* FIXME ? */
1339 }
1340 | method_header error
1341 {yyerror ("';' expected"); RECOVER;}
1342 ;
1343
1344 /* 19.10 Productions from 10: Arrays */
1345 array_initializer:
1346 OCB_TK CCB_TK
1347 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1348 | OCB_TK C_TK CCB_TK
1349 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1350 | OCB_TK variable_initializers CCB_TK
1351 { $$ = build_new_array_init ($1.location, $2); }
1352 | OCB_TK variable_initializers C_TK CCB_TK
1353 { $$ = build_new_array_init ($1.location, $2); }
1354 ;
1355
1356 variable_initializers:
1357 variable_initializer
1358 {
1359 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1360 $1, NULL_TREE);
1361 }
1362 | variable_initializers C_TK variable_initializer
1363 {
1364 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1365 }
1366 | variable_initializers C_TK error
1367 {yyerror ("Missing term"); RECOVER;}
1368 ;
1369
1370 /* 19.11 Production from 14: Blocks and Statements */
1371 block:
1372 OCB_TK CCB_TK
1373 {
1374 /* Store the location of the `}' when doing xrefs */
1375 if (current_function_decl && flag_emit_xref)
1376 DECL_END_SOURCE_LINE (current_function_decl) =
1377 EXPR_WFL_ADD_COL ($2.location, 1);
1378 $$ = empty_stmt_node;
1379 }
1380 | block_begin block_statements block_end
1381 { $$ = $3; }
1382 ;
1383
1384 block_begin:
1385 OCB_TK
1386 { enter_block (); }
1387 ;
1388
1389 block_end:
1390 CCB_TK
1391 {
1392 maybe_absorb_scoping_blocks ();
1393 /* Store the location of the `}' when doing xrefs */
1394 if (current_function_decl && flag_emit_xref)
1395 DECL_END_SOURCE_LINE (current_function_decl) =
1396 EXPR_WFL_ADD_COL ($1.location, 1);
1397 $$ = exit_block ();
1398 if (!BLOCK_SUBBLOCKS ($$))
1399 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1400 }
1401 ;
1402
1403 block_statements:
1404 block_statement
1405 | block_statements block_statement
1406 ;
1407
1408 block_statement:
1409 local_variable_declaration_statement
1410 | statement
1411 { java_method_add_stmt (current_function_decl, $1); }
1412 | class_declaration /* Added, JDK1.1 local classes */
1413 {
1414 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1415 end_class_declaration (1);
1416 }
1417 ;
1418
1419 local_variable_declaration_statement:
1420 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1421 ;
1422
1423 local_variable_declaration:
1424 type variable_declarators
1425 { declare_local_variables (0, $1, $2); }
1426 | final type variable_declarators /* Added, JDK1.1 final locals */
1427 { declare_local_variables ($1, $2, $3); }
1428 ;
1429
1430 statement:
1431 statement_without_trailing_substatement
1432 | labeled_statement
1433 | if_then_statement
1434 | if_then_else_statement
1435 | while_statement
1436 | for_statement
1437 { $$ = exit_block (); }
1438 ;
1439
1440 statement_nsi:
1441 statement_without_trailing_substatement
1442 | labeled_statement_nsi
1443 | if_then_else_statement_nsi
1444 | while_statement_nsi
1445 | for_statement_nsi
1446 { $$ = exit_block (); }
1447 ;
1448
1449 statement_without_trailing_substatement:
1450 block
1451 | empty_statement
1452 | expression_statement
1453 | switch_statement
1454 | do_statement
1455 | break_statement
1456 | continue_statement
1457 | return_statement
1458 | synchronized_statement
1459 | throw_statement
1460 | try_statement
1461 | assert_statement
1462 ;
1463
1464 empty_statement:
1465 SC_TK
1466 {
1467 if (flag_extraneous_semicolon
1468 && ! current_static_block
1469 && (! current_function_decl ||
1470 /* Verify we're not in a inner class declaration */
1471 (GET_CPC () != TYPE_NAME
1472 (DECL_CONTEXT (current_function_decl)))))
1473
1474 {
1475 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1476 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1477 }
1478 $$ = empty_stmt_node;
1479 }
1480 ;
1481
1482 label_decl:
1483 identifier REL_CL_TK
1484 {
1485 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1486 EXPR_WFL_NODE ($1));
1487 pushlevel (2);
1488 push_labeled_block ($$);
1489 PUSH_LABELED_BLOCK ($$);
1490 }
1491 ;
1492
1493 labeled_statement:
1494 label_decl statement
1495 { $$ = finish_labeled_statement ($1, $2); }
1496 | identifier error
1497 {yyerror ("':' expected"); RECOVER;}
1498 ;
1499
1500 labeled_statement_nsi:
1501 label_decl statement_nsi
1502 { $$ = finish_labeled_statement ($1, $2); }
1503 ;
1504
1505 /* We concentrate here a bunch of error handling rules that we couldn't write
1506 earlier, because expression_statement catches a missing ';'. */
1507 expression_statement:
1508 statement_expression SC_TK
1509 {
1510 /* We have a statement. Generate a WFL around it so
1511 we can debug it */
1512 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1513 /* We know we have a statement, so set the debug
1514 info to be eventually generate here. */
1515 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1516 }
1517 | error SC_TK
1518 {
1519 YYNOT_TWICE yyerror ("Invalid expression statement");
1520 DRECOVER (expr_stmt);
1521 }
1522 | error OCB_TK
1523 {
1524 YYNOT_TWICE yyerror ("Invalid expression statement");
1525 DRECOVER (expr_stmt);
1526 }
1527 | error CCB_TK
1528 {
1529 YYNOT_TWICE yyerror ("Invalid expression statement");
1530 DRECOVER (expr_stmt);
1531 }
1532 | this_or_super OP_TK error
1533 {yyerror ("')' expected"); RECOVER;}
1534 | this_or_super OP_TK CP_TK error
1535 {
1536 parse_ctor_invocation_error ();
1537 RECOVER;
1538 }
1539 | this_or_super OP_TK argument_list error
1540 {yyerror ("')' expected"); RECOVER;}
1541 | this_or_super OP_TK argument_list CP_TK error
1542 {
1543 parse_ctor_invocation_error ();
1544 RECOVER;
1545 }
1546 | name DOT_TK SUPER_TK error
1547 {yyerror ("'(' expected"); RECOVER;}
1548 | name DOT_TK SUPER_TK OP_TK error
1549 {yyerror ("')' expected"); RECOVER;}
1550 | name DOT_TK SUPER_TK OP_TK argument_list error
1551 {yyerror ("')' expected"); RECOVER;}
1552 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1553 {yyerror ("';' expected"); RECOVER;}
1554 | name DOT_TK SUPER_TK OP_TK CP_TK error
1555 {yyerror ("';' expected"); RECOVER;}
1556 ;
1557
1558 statement_expression:
1559 assignment
1560 | pre_increment_expression
1561 | pre_decrement_expression
1562 | post_increment_expression
1563 | post_decrement_expression
1564 | method_invocation
1565 | class_instance_creation_expression
1566 ;
1567
1568 if_then_statement:
1569 IF_TK OP_TK expression CP_TK statement
1570 {
1571 $$ = build_if_else_statement ($2.location, $3,
1572 $5, NULL_TREE);
1573 }
1574 | IF_TK error
1575 {yyerror ("'(' expected"); RECOVER;}
1576 | IF_TK OP_TK error
1577 {yyerror ("Missing term"); RECOVER;}
1578 | IF_TK OP_TK expression error
1579 {yyerror ("')' expected"); RECOVER;}
1580 ;
1581
1582 if_then_else_statement:
1583 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1584 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1585 ;
1586
1587 if_then_else_statement_nsi:
1588 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1589 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1590 ;
1591
1592 switch_statement:
1593 switch_expression
1594 {
1595 enter_block ();
1596 }
1597 switch_block
1598 {
1599 /* Make into "proper list" of COMPOUND_EXPRs.
1600 I.e. make the last statement also have its own
1601 COMPOUND_EXPR. */
1602 maybe_absorb_scoping_blocks ();
1603 TREE_OPERAND ($1, 1) = exit_block ();
1604 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1605 }
1606 ;
1607
1608 switch_expression:
1609 SWITCH_TK OP_TK expression CP_TK
1610 {
1611 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1612 EXPR_WFL_LINECOL ($$) = $2.location;
1613 }
1614 | SWITCH_TK error
1615 {yyerror ("'(' expected"); RECOVER;}
1616 | SWITCH_TK OP_TK error
1617 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1618 | SWITCH_TK OP_TK expression CP_TK error
1619 {yyerror ("'{' expected"); RECOVER;}
1620 ;
1621
1622 /* Default assignment is there to avoid type node on switch_block
1623 node. */
1624
1625 switch_block:
1626 OCB_TK CCB_TK
1627 { $$ = NULL_TREE; }
1628 | OCB_TK switch_labels CCB_TK
1629 { $$ = NULL_TREE; }
1630 | OCB_TK switch_block_statement_groups CCB_TK
1631 { $$ = NULL_TREE; }
1632 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1633 { $$ = NULL_TREE; }
1634 ;
1635
1636 switch_block_statement_groups:
1637 switch_block_statement_group
1638 | switch_block_statement_groups switch_block_statement_group
1639 ;
1640
1641 switch_block_statement_group:
1642 switch_labels block_statements
1643 ;
1644
1645 switch_labels:
1646 switch_label
1647 | switch_labels switch_label
1648 ;
1649
1650 switch_label:
1651 CASE_TK constant_expression REL_CL_TK
1652 {
1653 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1654 EXPR_WFL_LINECOL (lab) = $1.location;
1655 java_method_add_stmt (current_function_decl, lab);
1656 }
1657 | DEFAULT_TK REL_CL_TK
1658 {
1659 tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1660 EXPR_WFL_LINECOL (lab) = $1.location;
1661 java_method_add_stmt (current_function_decl, lab);
1662 }
1663 | CASE_TK error
1664 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1665 | CASE_TK constant_expression error
1666 {yyerror ("':' expected"); RECOVER;}
1667 | DEFAULT_TK error
1668 {yyerror ("':' expected"); RECOVER;}
1669 ;
1670
1671 while_expression:
1672 WHILE_TK OP_TK expression CP_TK
1673 {
1674 tree body = build_loop_body ($2.location, $3, 0);
1675 $$ = build_new_loop (body);
1676 }
1677 ;
1678
1679 while_statement:
1680 while_expression statement
1681 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1682 | WHILE_TK error
1683 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1684 | WHILE_TK OP_TK error
1685 {yyerror ("Missing term and ')' expected"); RECOVER;}
1686 | WHILE_TK OP_TK expression error
1687 {yyerror ("')' expected"); RECOVER;}
1688 ;
1689
1690 while_statement_nsi:
1691 while_expression statement_nsi
1692 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1693 ;
1694
1695 do_statement_begin:
1696 DO_TK
1697 {
1698 tree body = build_loop_body (0, NULL_TREE, 1);
1699 $$ = build_new_loop (body);
1700 }
1701 /* Need error handing here. FIXME */
1702 ;
1703
1704 do_statement:
1705 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1706 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1707 ;
1708
1709 for_statement:
1710 for_begin SC_TK expression SC_TK for_update CP_TK statement
1711 {
1712 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1713 $3 = build_wfl_node ($3);
1714 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1715 }
1716 | for_begin SC_TK SC_TK for_update CP_TK statement
1717 {
1718 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1719 /* We have not condition, so we get rid of the EXIT_EXPR */
1720 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1721 empty_stmt_node;
1722 }
1723 | for_begin SC_TK error
1724 {yyerror ("Invalid control expression"); RECOVER;}
1725 | for_begin SC_TK expression SC_TK error
1726 {yyerror ("Invalid update expression"); RECOVER;}
1727 | for_begin SC_TK SC_TK error
1728 {yyerror ("Invalid update expression"); RECOVER;}
1729 ;
1730
1731 for_statement_nsi:
1732 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1733 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1734 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1735 {
1736 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1737 /* We have not condition, so we get rid of the EXIT_EXPR */
1738 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1739 empty_stmt_node;
1740 }
1741 ;
1742
1743 for_header:
1744 FOR_TK OP_TK
1745 {
1746 /* This scope defined for local variable that may be
1747 defined within the scope of the for loop */
1748 enter_block ();
1749 }
1750 | FOR_TK error
1751 {yyerror ("'(' expected"); DRECOVER(for_1);}
1752 | FOR_TK OP_TK error
1753 {yyerror ("Invalid init statement"); RECOVER;}
1754 ;
1755
1756 for_begin:
1757 for_header for_init
1758 {
1759 /* We now declare the loop body. The loop is
1760 declared as a for loop. */
1761 tree body = build_loop_body (0, NULL_TREE, 0);
1762 $$ = build_new_loop (body);
1763 FOR_LOOP_P ($$) = 1;
1764 /* The loop is added to the current block the for
1765 statement is defined within */
1766 java_method_add_stmt (current_function_decl, $$);
1767 }
1768 ;
1769 for_init: /* Can be empty */
1770 { $$ = empty_stmt_node; }
1771 | statement_expression_list
1772 {
1773 /* Init statement recorded within the previously
1774 defined block scope */
1775 $$ = java_method_add_stmt (current_function_decl, $1);
1776 }
1777 | local_variable_declaration
1778 {
1779 /* Local variable are recorded within the previously
1780 defined block scope */
1781 $$ = NULL_TREE;
1782 }
1783 | statement_expression_list error
1784 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1785 ;
1786
1787 for_update: /* Can be empty */
1788 {$$ = empty_stmt_node;}
1789 | statement_expression_list
1790 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1791 ;
1792
1793 statement_expression_list:
1794 statement_expression
1795 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1796 | statement_expression_list C_TK statement_expression
1797 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1798 | statement_expression_list C_TK error
1799 {yyerror ("Missing term"); RECOVER;}
1800 ;
1801
1802 break_statement:
1803 BREAK_TK SC_TK
1804 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1805 | BREAK_TK identifier SC_TK
1806 { $$ = build_bc_statement ($1.location, 1, $2); }
1807 | BREAK_TK error
1808 {yyerror ("Missing term"); RECOVER;}
1809 | BREAK_TK identifier error
1810 {yyerror ("';' expected"); RECOVER;}
1811 ;
1812
1813 continue_statement:
1814 CONTINUE_TK SC_TK
1815 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1816 | CONTINUE_TK identifier SC_TK
1817 { $$ = build_bc_statement ($1.location, 0, $2); }
1818 | CONTINUE_TK error
1819 {yyerror ("Missing term"); RECOVER;}
1820 | CONTINUE_TK identifier error
1821 {yyerror ("';' expected"); RECOVER;}
1822 ;
1823
1824 return_statement:
1825 RETURN_TK SC_TK
1826 { $$ = build_return ($1.location, NULL_TREE); }
1827 | RETURN_TK expression SC_TK
1828 { $$ = build_return ($1.location, $2); }
1829 | RETURN_TK error
1830 {yyerror ("Missing term"); RECOVER;}
1831 | RETURN_TK expression error
1832 {yyerror ("';' expected"); RECOVER;}
1833 ;
1834
1835 throw_statement:
1836 THROW_TK expression SC_TK
1837 {
1838 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1839 EXPR_WFL_LINECOL ($$) = $1.location;
1840 }
1841 | THROW_TK error
1842 {yyerror ("Missing term"); RECOVER;}
1843 | THROW_TK expression error
1844 {yyerror ("';' expected"); RECOVER;}
1845 ;
1846
1847 assert_statement:
1848 ASSERT_TK expression REL_CL_TK expression SC_TK
1849 {
1850 $$ = build_assertion ($1.location, $2, $4);
1851 }
1852 | ASSERT_TK expression SC_TK
1853 {
1854 $$ = build_assertion ($1.location, $2, NULL_TREE);
1855 }
1856 | ASSERT_TK error
1857 {yyerror ("Missing term"); RECOVER;}
1858 | ASSERT_TK expression error
1859 {yyerror ("';' expected"); RECOVER;}
1860 ;
1861
1862 synchronized_statement:
1863 synchronized OP_TK expression CP_TK block
1864 {
1865 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1866 EXPR_WFL_LINECOL ($$) =
1867 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1868 }
1869 | synchronized OP_TK expression CP_TK error
1870 {yyerror ("'{' expected"); RECOVER;}
1871 | synchronized error
1872 {yyerror ("'(' expected"); RECOVER;}
1873 | synchronized OP_TK error CP_TK
1874 {yyerror ("Missing term"); RECOVER;}
1875 | synchronized OP_TK error
1876 {yyerror ("Missing term"); RECOVER;}
1877 ;
1878
1879 synchronized:
1880 modifiers
1881 {
1882 check_modifiers (
1883 "Illegal modifier `%s'. Only `synchronized' was expected here",
1884 $1, ACC_SYNCHRONIZED);
1885 if ($1 != ACC_SYNCHRONIZED)
1886 MODIFIER_WFL (SYNCHRONIZED_TK) =
1887 build_wfl_node (NULL_TREE);
1888 }
1889 ;
1890
1891 try_statement:
1892 TRY_TK block catches
1893 { $$ = build_try_statement ($1.location, $2, $3); }
1894 | TRY_TK block finally
1895 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1896 | TRY_TK block catches finally
1897 { $$ = build_try_finally_statement
1898 ($1.location, build_try_statement ($1.location,
1899 $2, $3), $4);
1900 }
1901 | TRY_TK error
1902 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1903 ;
1904
1905 catches:
1906 catch_clause
1907 | catches catch_clause
1908 {
1909 TREE_CHAIN ($2) = $1;
1910 $$ = $2;
1911 }
1912 ;
1913
1914 catch_clause:
1915 catch_clause_parameter block
1916 {
1917 java_method_add_stmt (current_function_decl, $2);
1918 exit_block ();
1919 $$ = $1;
1920 }
1921 ;
1922
1923 catch_clause_parameter:
1924 CATCH_TK OP_TK formal_parameter CP_TK
1925 {
1926 /* We add a block to define a scope for
1927 formal_parameter (CCBP). The formal parameter is
1928 declared initialized by the appropriate function
1929 call */
1930 tree ccpb = enter_block ();
1931 tree init = build_assignment
1932 (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1933 build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1934 declare_local_variables (0, TREE_VALUE ($3),
1935 build_tree_list (TREE_PURPOSE ($3),
1936 init));
1937 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1938 EXPR_WFL_LINECOL ($$) = $1.location;
1939 }
1940 | CATCH_TK error
1941 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1942 | CATCH_TK OP_TK error
1943 {
1944 yyerror ("Missing term or ')' expected");
1945 RECOVER; $$ = NULL_TREE;
1946 }
1947 | CATCH_TK OP_TK error CP_TK /* That's for () */
1948 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1949 ;
1950
1951 finally:
1952 FINALLY_TK block
1953 { $$ = $2; }
1954 | FINALLY_TK error
1955 {yyerror ("'{' expected"); RECOVER; }
1956 ;
1957
1958 /* 19.12 Production from 15: Expressions */
1959 primary:
1960 primary_no_new_array
1961 | array_creation_expression
1962 ;
1963
1964 primary_no_new_array:
1965 literal
1966 | THIS_TK
1967 { $$ = build_this ($1.location); }
1968 | OP_TK expression CP_TK
1969 {$$ = $2;}
1970 | class_instance_creation_expression
1971 | field_access
1972 | method_invocation
1973 | array_access
1974 | type_literals
1975 /* Added, JDK1.1 inner classes. Documentation is wrong
1976 refering to a 'ClassName' (class_name) rule that doesn't
1977 exist. Used name: instead. */
1978 | name DOT_TK THIS_TK
1979 {
1980 tree wfl = build_wfl_node (this_identifier_node);
1981 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1982 }
1983 | OP_TK expression error
1984 {yyerror ("')' expected"); RECOVER;}
1985 | name DOT_TK error
1986 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1987 | primitive_type DOT_TK error
1988 {yyerror ("'class' expected" ); RECOVER;}
1989 | VOID_TK DOT_TK error
1990 {yyerror ("'class' expected" ); RECOVER;}
1991 ;
1992
1993 type_literals:
1994 name DOT_TK CLASS_TK
1995 { $$ = build_incomplete_class_ref ($2.location, $1); }
1996 | array_type DOT_TK CLASS_TK
1997 { $$ = build_incomplete_class_ref ($2.location, $1); }
1998 | primitive_type DOT_TK CLASS_TK
1999 { $$ = build_incomplete_class_ref ($2.location, $1); }
2000 | VOID_TK DOT_TK CLASS_TK
2001 {
2002 $$ = build_incomplete_class_ref ($2.location,
2003 void_type_node);
2004 }
2005 ;
2006
2007 class_instance_creation_expression:
2008 NEW_TK class_type OP_TK argument_list CP_TK
2009 { $$ = build_new_invocation ($2, $4); }
2010 | NEW_TK class_type OP_TK CP_TK
2011 { $$ = build_new_invocation ($2, NULL_TREE); }
2012 | anonymous_class_creation
2013 /* Added, JDK1.1 inner classes, modified to use name or
2014 primary instead of primary solely which couldn't work in
2015 all situations. */
2016 | something_dot_new identifier OP_TK CP_TK
2017 {
2018 tree ctor = build_new_invocation ($2, NULL_TREE);
2019 $$ = make_qualified_primary ($1, ctor,
2020 EXPR_WFL_LINECOL ($1));
2021 }
2022 | something_dot_new identifier OP_TK CP_TK class_body
2023 | something_dot_new identifier OP_TK argument_list CP_TK
2024 {
2025 tree ctor = build_new_invocation ($2, $4);
2026 $$ = make_qualified_primary ($1, ctor,
2027 EXPR_WFL_LINECOL ($1));
2028 }
2029 | something_dot_new identifier OP_TK argument_list CP_TK class_body
2030 | NEW_TK error SC_TK
2031 {yyerror ("'(' expected"); DRECOVER(new_1);}
2032 | NEW_TK class_type error
2033 {yyerror ("'(' expected"); RECOVER;}
2034 | NEW_TK class_type OP_TK error
2035 {yyerror ("')' or term expected"); RECOVER;}
2036 | NEW_TK class_type OP_TK argument_list error
2037 {yyerror ("')' expected"); RECOVER;}
2038 | something_dot_new error
2039 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
2040 | something_dot_new identifier error
2041 {yyerror ("'(' expected"); RECOVER;}
2042 ;
2043
2044 /* Created after JDK1.1 rules originally added to
2045 class_instance_creation_expression, but modified to use
2046 'class_type' instead of 'TypeName' (type_name) which is mentionned
2047 in the documentation but doesn't exist. */
2048
2049 anonymous_class_creation:
2050 NEW_TK class_type OP_TK argument_list CP_TK
2051 { create_anonymous_class ($1.location, $2); }
2052 class_body
2053 {
2054 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2055 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2056
2057 end_class_declaration (1);
2058
2059 /* Now we can craft the new expression */
2060 $$ = build_new_invocation (id, $4);
2061
2062 /* Note that we can't possibly be here if
2063 `class_type' is an interface (in which case the
2064 anonymous class extends Object and implements
2065 `class_type', hence its constructor can't have
2066 arguments.) */
2067
2068 /* Otherwise, the innerclass must feature a
2069 constructor matching `argument_list'. Anonymous
2070 classes are a bit special: it's impossible to
2071 define constructor for them, hence constructors
2072 must be generated following the hints provided by
2073 the `new' expression. Whether a super constructor
2074 of that nature exists or not is to be verified
2075 later on in verify_constructor_super.
2076
2077 It's during the expansion of a `new' statement
2078 refering to an anonymous class that a ctor will
2079 be generated for the anonymous class, with the
2080 right arguments. */
2081
2082 }
2083 | NEW_TK class_type OP_TK CP_TK
2084 { create_anonymous_class ($1.location, $2); }
2085 class_body
2086 {
2087 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2088 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2089
2090 end_class_declaration (1);
2091
2092 /* Now we can craft the new expression. The
2093 statement doesn't need to be remember so that a
2094 constructor can be generated, since its signature
2095 is already known. */
2096 $$ = build_new_invocation (id, NULL_TREE);
2097 }
2098 ;
2099
2100 something_dot_new: /* Added, not part of the specs. */
2101 name DOT_TK NEW_TK
2102 { $$ = $1; }
2103 | primary DOT_TK NEW_TK
2104 { $$ = $1; }
2105 ;
2106
2107 argument_list:
2108 expression
2109 {
2110 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2111 ctxp->formal_parameter_number = 1;
2112 }
2113 | argument_list C_TK expression
2114 {
2115 ctxp->formal_parameter_number += 1;
2116 $$ = tree_cons (NULL_TREE, $3, $1);
2117 }
2118 | argument_list C_TK error
2119 {yyerror ("Missing term"); RECOVER;}
2120 ;
2121
2122 array_creation_expression:
2123 NEW_TK primitive_type dim_exprs
2124 { $$ = build_newarray_node ($2, $3, 0); }
2125 | NEW_TK class_or_interface_type dim_exprs
2126 { $$ = build_newarray_node ($2, $3, 0); }
2127 | NEW_TK primitive_type dim_exprs dims
2128 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2129 | NEW_TK class_or_interface_type dim_exprs dims
2130 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2131 /* Added, JDK1.1 anonymous array. Initial documentation rule
2132 modified */
2133 | NEW_TK class_or_interface_type dims array_initializer
2134 {
2135 char *sig;
2136 int osb = pop_current_osb (ctxp);
2137 while (osb--)
2138 obstack_grow (&temporary_obstack, "[]", 2);
2139 obstack_1grow (&temporary_obstack, '\0');
2140 sig = obstack_finish (&temporary_obstack);
2141 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2142 $2, get_identifier (sig), $4);
2143 }
2144 | NEW_TK primitive_type dims array_initializer
2145 {
2146 int osb = pop_current_osb (ctxp);
2147 tree type = $2;
2148 while (osb--)
2149 type = build_java_array_type (type, -1);
2150 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2151 build_pointer_type (type), NULL_TREE, $4);
2152 }
2153 | NEW_TK error CSB_TK
2154 {yyerror ("'[' expected"); DRECOVER ("]");}
2155 | NEW_TK error OSB_TK
2156 {yyerror ("']' expected"); RECOVER;}
2157 ;
2158
2159 dim_exprs:
2160 dim_expr
2161 { $$ = build_tree_list (NULL_TREE, $1); }
2162 | dim_exprs dim_expr
2163 { $$ = tree_cons (NULL_TREE, $2, $$); }
2164 ;
2165
2166 dim_expr:
2167 OSB_TK expression CSB_TK
2168 {
2169 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2170 {
2171 $2 = build_wfl_node ($2);
2172 TREE_TYPE ($2) = NULL_TREE;
2173 }
2174 EXPR_WFL_LINECOL ($2) = $1.location;
2175 $$ = $2;
2176 }
2177 | OSB_TK expression error
2178 {yyerror ("']' expected"); RECOVER;}
2179 | OSB_TK error
2180 {
2181 yyerror ("Missing term");
2182 yyerror ("']' expected");
2183 RECOVER;
2184 }
2185 ;
2186
2187 dims:
2188 OSB_TK CSB_TK
2189 {
2190 int allocate = 0;
2191 /* If not initialized, allocate memory for the osb
2192 numbers stack */
2193 if (!ctxp->osb_limit)
2194 {
2195 allocate = ctxp->osb_limit = 32;
2196 ctxp->osb_depth = -1;
2197 }
2198 /* If capacity overflown, reallocate a bigger chunk */
2199 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2200 allocate = ctxp->osb_limit << 1;
2201
2202 if (allocate)
2203 {
2204 allocate *= sizeof (int);
2205 if (ctxp->osb_number)
2206 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2207 allocate);
2208 else
2209 ctxp->osb_number = (int *)xmalloc (allocate);
2210 }
2211 ctxp->osb_depth++;
2212 CURRENT_OSB (ctxp) = 1;
2213 }
2214 | dims OSB_TK CSB_TK
2215 { CURRENT_OSB (ctxp)++; }
2216 | dims OSB_TK error
2217 { yyerror ("']' expected"); RECOVER;}
2218 ;
2219
2220 field_access:
2221 primary DOT_TK identifier
2222 { $$ = make_qualified_primary ($1, $3, $2.location); }
2223 /* FIXME - REWRITE TO:
2224 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2225 | SUPER_TK DOT_TK identifier
2226 {
2227 tree super_wfl = build_wfl_node (super_identifier_node);
2228 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2229 $$ = make_qualified_name (super_wfl, $3, $2.location);
2230 }
2231 | SUPER_TK error
2232 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2233 ;
2234
2235 method_invocation:
2236 name OP_TK CP_TK
2237 { $$ = build_method_invocation ($1, NULL_TREE); }
2238 | name OP_TK argument_list CP_TK
2239 { $$ = build_method_invocation ($1, $3); }
2240 | primary DOT_TK identifier OP_TK CP_TK
2241 {
2242 if (TREE_CODE ($1) == THIS_EXPR)
2243 $$ = build_this_super_qualified_invocation
2244 (1, $3, NULL_TREE, 0, $2.location);
2245 else
2246 {
2247 tree invok = build_method_invocation ($3, NULL_TREE);
2248 $$ = make_qualified_primary ($1, invok, $2.location);
2249 }
2250 }
2251 | primary DOT_TK identifier OP_TK argument_list CP_TK
2252 {
2253 if (TREE_CODE ($1) == THIS_EXPR)
2254 $$ = build_this_super_qualified_invocation
2255 (1, $3, $5, 0, $2.location);
2256 else
2257 {
2258 tree invok = build_method_invocation ($3, $5);
2259 $$ = make_qualified_primary ($1, invok, $2.location);
2260 }
2261 }
2262 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2263 {
2264 $$ = build_this_super_qualified_invocation
2265 (0, $3, NULL_TREE, $1.location, $2.location);
2266 }
2267 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2268 {
2269 $$ = build_this_super_qualified_invocation
2270 (0, $3, $5, $1.location, $2.location);
2271 }
2272 /* Screws up thing. I let it here until I'm convinced it can
2273 be removed. FIXME
2274 | primary DOT_TK error
2275 {yyerror ("'(' expected"); DRECOVER(bad);} */
2276 | SUPER_TK DOT_TK error CP_TK
2277 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2278 | SUPER_TK DOT_TK error DOT_TK
2279 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2280 ;
2281
2282 array_access:
2283 name OSB_TK expression CSB_TK
2284 { $$ = build_array_ref ($2.location, $1, $3); }
2285 | primary_no_new_array OSB_TK expression CSB_TK
2286 { $$ = build_array_ref ($2.location, $1, $3); }
2287 | name OSB_TK error
2288 {
2289 yyerror ("Missing term and ']' expected");
2290 DRECOVER(array_access);
2291 }
2292 | name OSB_TK expression error
2293 {
2294 yyerror ("']' expected");
2295 DRECOVER(array_access);
2296 }
2297 | primary_no_new_array OSB_TK error
2298 {
2299 yyerror ("Missing term and ']' expected");
2300 DRECOVER(array_access);
2301 }
2302 | primary_no_new_array OSB_TK expression error
2303 {
2304 yyerror ("']' expected");
2305 DRECOVER(array_access);
2306 }
2307 ;
2308
2309 postfix_expression:
2310 primary
2311 | name
2312 | post_increment_expression
2313 | post_decrement_expression
2314 ;
2315
2316 post_increment_expression:
2317 postfix_expression INCR_TK
2318 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2319 ;
2320
2321 post_decrement_expression:
2322 postfix_expression DECR_TK
2323 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2324 ;
2325
2326 trap_overflow_corner_case:
2327 pre_increment_expression
2328 | pre_decrement_expression
2329 | PLUS_TK unary_expression
2330 {$$ = build_unaryop ($1.token, $1.location, $2); }
2331 | unary_expression_not_plus_minus
2332 | PLUS_TK error
2333 {yyerror ("Missing term"); RECOVER}
2334 ;
2335
2336 unary_expression:
2337 trap_overflow_corner_case
2338 {
2339 error_if_numeric_overflow ($1);
2340 $$ = $1;
2341 }
2342 | MINUS_TK trap_overflow_corner_case
2343 {$$ = build_unaryop ($1.token, $1.location, $2); }
2344 | MINUS_TK error
2345 {yyerror ("Missing term"); RECOVER}
2346 ;
2347
2348 pre_increment_expression:
2349 INCR_TK unary_expression
2350 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2351 | INCR_TK error
2352 {yyerror ("Missing term"); RECOVER}
2353 ;
2354
2355 pre_decrement_expression:
2356 DECR_TK unary_expression
2357 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2358 | DECR_TK error
2359 {yyerror ("Missing term"); RECOVER}
2360 ;
2361
2362 unary_expression_not_plus_minus:
2363 postfix_expression
2364 | NOT_TK unary_expression
2365 {$$ = build_unaryop ($1.token, $1.location, $2); }
2366 | NEG_TK unary_expression
2367 {$$ = build_unaryop ($1.token, $1.location, $2); }
2368 | cast_expression
2369 | NOT_TK error
2370 {yyerror ("Missing term"); RECOVER}
2371 | NEG_TK error
2372 {yyerror ("Missing term"); RECOVER}
2373 ;
2374
2375 cast_expression: /* Error handling here is potentially weak */
2376 OP_TK primitive_type dims CP_TK unary_expression
2377 {
2378 tree type = $2;
2379 int osb = pop_current_osb (ctxp);
2380 while (osb--)
2381 type = build_java_array_type (type, -1);
2382 $$ = build_cast ($1.location, type, $5);
2383 }
2384 | OP_TK primitive_type CP_TK unary_expression
2385 { $$ = build_cast ($1.location, $2, $4); }
2386 | OP_TK expression CP_TK unary_expression_not_plus_minus
2387 { $$ = build_cast ($1.location, $2, $4); }
2388 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2389 {
2390 const char *ptr;
2391 int osb = pop_current_osb (ctxp);
2392 obstack_grow (&temporary_obstack,
2393 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2394 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2395 while (osb--)
2396 obstack_grow (&temporary_obstack, "[]", 2);
2397 obstack_1grow (&temporary_obstack, '\0');
2398 ptr = obstack_finish (&temporary_obstack);
2399 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2400 $$ = build_cast ($1.location, $2, $5);
2401 }
2402 | OP_TK primitive_type OSB_TK error
2403 {yyerror ("']' expected, invalid type expression");}
2404 | OP_TK error
2405 {
2406 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2407 RECOVER;
2408 }
2409 | OP_TK primitive_type dims CP_TK error
2410 {yyerror ("Missing term"); RECOVER;}
2411 | OP_TK primitive_type CP_TK error
2412 {yyerror ("Missing term"); RECOVER;}
2413 | OP_TK name dims CP_TK error
2414 {yyerror ("Missing term"); RECOVER;}
2415 ;
2416
2417 multiplicative_expression:
2418 unary_expression
2419 | multiplicative_expression MULT_TK unary_expression
2420 {
2421 $$ = build_binop (BINOP_LOOKUP ($2.token),
2422 $2.location, $1, $3);
2423 }
2424 | multiplicative_expression DIV_TK unary_expression
2425 {
2426 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2427 $1, $3);
2428 }
2429 | multiplicative_expression REM_TK unary_expression
2430 {
2431 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2432 $1, $3);
2433 }
2434 | multiplicative_expression MULT_TK error
2435 {yyerror ("Missing term"); RECOVER;}
2436 | multiplicative_expression DIV_TK error
2437 {yyerror ("Missing term"); RECOVER;}
2438 | multiplicative_expression REM_TK error
2439 {yyerror ("Missing term"); RECOVER;}
2440 ;
2441
2442 additive_expression:
2443 multiplicative_expression
2444 | additive_expression PLUS_TK multiplicative_expression
2445 {
2446 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2447 $1, $3);
2448 }
2449 | additive_expression MINUS_TK multiplicative_expression
2450 {
2451 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2452 $1, $3);
2453 }
2454 | additive_expression PLUS_TK error
2455 {yyerror ("Missing term"); RECOVER;}
2456 | additive_expression MINUS_TK error
2457 {yyerror ("Missing term"); RECOVER;}
2458 ;
2459
2460 shift_expression:
2461 additive_expression
2462 | shift_expression LS_TK additive_expression
2463 {
2464 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2465 $1, $3);
2466 }
2467 | shift_expression SRS_TK additive_expression
2468 {
2469 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2470 $1, $3);
2471 }
2472 | shift_expression ZRS_TK additive_expression
2473 {
2474 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2475 $1, $3);
2476 }
2477 | shift_expression LS_TK error
2478 {yyerror ("Missing term"); RECOVER;}
2479 | shift_expression SRS_TK error
2480 {yyerror ("Missing term"); RECOVER;}
2481 | shift_expression ZRS_TK error
2482 {yyerror ("Missing term"); RECOVER;}
2483 ;
2484
2485 relational_expression:
2486 shift_expression
2487 | relational_expression LT_TK shift_expression
2488 {
2489 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2490 $1, $3);
2491 }
2492 | relational_expression GT_TK shift_expression
2493 {
2494 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2495 $1, $3);
2496 }
2497 | relational_expression LTE_TK shift_expression
2498 {
2499 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2500 $1, $3);
2501 }
2502 | relational_expression GTE_TK shift_expression
2503 {
2504 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2505 $1, $3);
2506 }
2507 | relational_expression INSTANCEOF_TK reference_type
2508 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2509 | relational_expression LT_TK error
2510 {yyerror ("Missing term"); RECOVER;}
2511 | relational_expression GT_TK error
2512 {yyerror ("Missing term"); RECOVER;}
2513 | relational_expression LTE_TK error
2514 {yyerror ("Missing term"); RECOVER;}
2515 | relational_expression GTE_TK error
2516 {yyerror ("Missing term"); RECOVER;}
2517 | relational_expression INSTANCEOF_TK error
2518 {yyerror ("Invalid reference type"); RECOVER;}
2519 ;
2520
2521 equality_expression:
2522 relational_expression
2523 | equality_expression EQ_TK relational_expression
2524 {
2525 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2526 $1, $3);
2527 }
2528 | equality_expression NEQ_TK relational_expression
2529 {
2530 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2531 $1, $3);
2532 }
2533 | equality_expression EQ_TK error
2534 {yyerror ("Missing term"); RECOVER;}
2535 | equality_expression NEQ_TK error
2536 {yyerror ("Missing term"); RECOVER;}
2537 ;
2538
2539 and_expression:
2540 equality_expression
2541 | and_expression AND_TK equality_expression
2542 {
2543 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2544 $1, $3);
2545 }
2546 | and_expression AND_TK error
2547 {yyerror ("Missing term"); RECOVER;}
2548 ;
2549
2550 exclusive_or_expression:
2551 and_expression
2552 | exclusive_or_expression XOR_TK and_expression
2553 {
2554 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2555 $1, $3);
2556 }
2557 | exclusive_or_expression XOR_TK error
2558 {yyerror ("Missing term"); RECOVER;}
2559 ;
2560
2561 inclusive_or_expression:
2562 exclusive_or_expression
2563 | inclusive_or_expression OR_TK exclusive_or_expression
2564 {
2565 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2566 $1, $3);
2567 }
2568 | inclusive_or_expression OR_TK error
2569 {yyerror ("Missing term"); RECOVER;}
2570 ;
2571
2572 conditional_and_expression:
2573 inclusive_or_expression
2574 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2575 {
2576 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2577 $1, $3);
2578 }
2579 | conditional_and_expression BOOL_AND_TK error
2580 {yyerror ("Missing term"); RECOVER;}
2581 ;
2582
2583 conditional_or_expression:
2584 conditional_and_expression
2585 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2586 {
2587 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2588 $1, $3);
2589 }
2590 | conditional_or_expression BOOL_OR_TK error
2591 {yyerror ("Missing term"); RECOVER;}
2592 ;
2593
2594 conditional_expression: /* Error handling here is weak */
2595 conditional_or_expression
2596 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2597 {
2598 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2599 EXPR_WFL_LINECOL ($$) = $2.location;
2600 }
2601 | conditional_or_expression REL_QM_TK REL_CL_TK error
2602 {
2603 YYERROR_NOW;
2604 yyerror ("Missing term");
2605 DRECOVER (1);
2606 }
2607 | conditional_or_expression REL_QM_TK error
2608 {yyerror ("Missing term"); DRECOVER (2);}
2609 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2610 {yyerror ("Missing term"); DRECOVER (3);}
2611 ;
2612
2613 assignment_expression:
2614 conditional_expression
2615 | assignment
2616 ;
2617
2618 assignment:
2619 left_hand_side assignment_operator assignment_expression
2620 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2621 | left_hand_side assignment_operator error
2622 {
2623 YYNOT_TWICE yyerror ("Missing term");
2624 DRECOVER (assign);
2625 }
2626 ;
2627
2628 left_hand_side:
2629 name
2630 | field_access
2631 | array_access
2632 ;
2633
2634 assignment_operator:
2635 ASSIGN_ANY_TK
2636 | ASSIGN_TK
2637 ;
2638
2639 expression:
2640 assignment_expression
2641 ;
2642
2643 constant_expression:
2644 expression
2645 ;
2646
2647 %%
2648
2649 /* Helper function to retrieve an OSB count. Should be used when the
2650 `dims:' rule is being used. */
2651
2652 static int
2653 pop_current_osb (ctxp)
2654 struct parser_ctxt *ctxp;
2655 {
2656 int to_return;
2657
2658 if (ctxp->osb_depth < 0)
2659 abort ();
2660
2661 to_return = CURRENT_OSB (ctxp);
2662 ctxp->osb_depth--;
2663
2664 return to_return;
2665 }
2666
2667 \f
2668
2669 /* This section of the code deal with save/restoring parser contexts.
2670 Add mode documentation here. FIXME */
2671
2672 /* Helper function. Create a new parser context. With
2673 COPY_FROM_PREVIOUS set to a nonzero value, content of the previous
2674 context is copied, otherwise, the new context is zeroed. The newly
2675 created context becomes the current one. */
2676
2677 static void
2678 create_new_parser_context (copy_from_previous)
2679 int copy_from_previous;
2680 {
2681 struct parser_ctxt *new;
2682
2683 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2684 if (copy_from_previous)
2685 {
2686 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2687 new->saved_data_ctx = 1;
2688 }
2689 else
2690 memset ((PTR) new, 0, sizeof (struct parser_ctxt));
2691
2692 new->next = ctxp;
2693 ctxp = new;
2694 }
2695
2696 /* Create a new parser context and make it the current one. */
2697
2698 void
2699 java_push_parser_context ()
2700 {
2701 create_new_parser_context (0);
2702 }
2703
2704 void
2705 java_pop_parser_context (generate)
2706 int generate;
2707 {
2708 tree current;
2709 struct parser_ctxt *toFree, *next;
2710
2711 if (!ctxp)
2712 return;
2713
2714 toFree = ctxp;
2715 next = ctxp->next;
2716 if (next)
2717 {
2718 lineno = ctxp->lineno;
2719 current_class = ctxp->class_type;
2720 }
2721
2722 /* If the old and new lexers differ, then free the old one. */
2723 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2724 java_destroy_lexer (ctxp->lexer);
2725
2726 /* Set the single import class file flag to 0 for the current list
2727 of imported things */
2728 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2729 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2730
2731 /* And restore those of the previous context */
2732 if ((ctxp = next)) /* Assignment is really meant here */
2733 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2734 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2735
2736 /* If we pushed a context to parse a class intended to be generated,
2737 we keep it so we can remember the class. What we could actually
2738 do is to just update a list of class names. */
2739 if (generate)
2740 {
2741 toFree->next = ctxp_for_generation;
2742 ctxp_for_generation = toFree;
2743 }
2744 else
2745 free (toFree);
2746 }
2747
2748 /* Create a parser context for the use of saving some global
2749 variables. */
2750
2751 void
2752 java_parser_context_save_global ()
2753 {
2754 if (!ctxp)
2755 {
2756 java_push_parser_context ();
2757 ctxp->saved_data_ctx = 1;
2758 }
2759
2760 /* If this context already stores data, create a new one suitable
2761 for data storage. */
2762 else if (ctxp->saved_data)
2763 create_new_parser_context (1);
2764
2765 ctxp->lineno = lineno;
2766 ctxp->class_type = current_class;
2767 ctxp->filename = input_filename;
2768 ctxp->function_decl = current_function_decl;
2769 ctxp->saved_data = 1;
2770 }
2771
2772 /* Restore some global variables from the previous context. Make the
2773 previous context the current one. */
2774
2775 void
2776 java_parser_context_restore_global ()
2777 {
2778 lineno = ctxp->lineno;
2779 current_class = ctxp->class_type;
2780 input_filename = ctxp->filename;
2781 if (wfl_operator)
2782 {
2783 tree s;
2784 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2785 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2786 }
2787 current_function_decl = ctxp->function_decl;
2788 ctxp->saved_data = 0;
2789 if (ctxp->saved_data_ctx)
2790 java_pop_parser_context (0);
2791 }
2792
2793 /* Suspend vital data for the current class/function being parsed so
2794 that an other class can be parsed. Used to let local/anonymous
2795 classes be parsed. */
2796
2797 static void
2798 java_parser_context_suspend ()
2799 {
2800 /* This makes debugging through java_debug_context easier */
2801 static const char *const name = "<inner buffer context>";
2802
2803 /* Duplicate the previous context, use it to save the globals we're
2804 interested in */
2805 create_new_parser_context (1);
2806 ctxp->function_decl = current_function_decl;
2807 ctxp->class_type = current_class;
2808
2809 /* Then create a new context which inherits all data from the
2810 previous one. This will be the new current context */
2811 create_new_parser_context (1);
2812
2813 /* Help debugging */
2814 ctxp->next->filename = name;
2815 }
2816
2817 /* Resume vital data for the current class/function being parsed so
2818 that an other class can be parsed. Used to let local/anonymous
2819 classes be parsed. The trick is the data storing file position
2820 informations must be restored to their current value, so parsing
2821 can resume as if no context was ever saved. */
2822
2823 static void
2824 java_parser_context_resume ()
2825 {
2826 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2827 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2828 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2829
2830 /* We need to inherit the list of classes to complete/generate */
2831 restored->classd_list = old->classd_list;
2832 restored->class_list = old->class_list;
2833
2834 /* Restore the current class and function from the saver */
2835 current_class = saver->class_type;
2836 current_function_decl = saver->function_decl;
2837
2838 /* Retrive the restored context */
2839 ctxp = restored;
2840
2841 /* Re-installed the data for the parsing to carry on */
2842 memcpy (&ctxp->marker_begining, &old->marker_begining,
2843 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2844
2845 /* Buffer context can now be discarded */
2846 free (saver);
2847 free (old);
2848 }
2849
2850 /* Add a new anchor node to which all statement(s) initializing static
2851 and non static initialized upon declaration field(s) will be
2852 linked. */
2853
2854 static void
2855 java_parser_context_push_initialized_field ()
2856 {
2857 tree node;
2858
2859 node = build_tree_list (NULL_TREE, NULL_TREE);
2860 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2861 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2862
2863 node = build_tree_list (NULL_TREE, NULL_TREE);
2864 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2865 CPC_INITIALIZER_LIST (ctxp) = node;
2866
2867 node = build_tree_list (NULL_TREE, NULL_TREE);
2868 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2869 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2870 }
2871
2872 /* Pop the lists of initialized field. If this lists aren't empty,
2873 remember them so we can use it to create and populate the finit$
2874 or <clinit> functions. */
2875
2876 static void
2877 java_parser_context_pop_initialized_field ()
2878 {
2879 tree stmts;
2880 tree class_type = TREE_TYPE (GET_CPC ());
2881
2882 if (CPC_INITIALIZER_LIST (ctxp))
2883 {
2884 stmts = CPC_INITIALIZER_STMT (ctxp);
2885 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2886 if (stmts && !java_error_count)
2887 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2888 }
2889
2890 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2891 {
2892 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2893 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2894 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2895 /* Keep initialization in order to enforce 8.5 */
2896 if (stmts && !java_error_count)
2897 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2898 }
2899
2900 /* JDK 1.1 instance initializers */
2901 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2902 {
2903 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2904 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2905 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2906 if (stmts && !java_error_count)
2907 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2908 }
2909 }
2910
2911 static tree
2912 reorder_static_initialized (list)
2913 tree list;
2914 {
2915 /* We have to keep things in order. The alias initializer have to
2916 come first, then the initialized regular field, in reverse to
2917 keep them in lexical order. */
2918 tree marker, previous = NULL_TREE;
2919 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2920 if (TREE_CODE (marker) == TREE_LIST
2921 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2922 break;
2923
2924 /* No static initialized, the list is fine as is */
2925 if (!previous)
2926 list = TREE_CHAIN (marker);
2927
2928 /* No marker? reverse the whole list */
2929 else if (!marker)
2930 list = nreverse (list);
2931
2932 /* Otherwise, reverse what's after the marker and the new reordered
2933 sublist will replace the marker. */
2934 else
2935 {
2936 TREE_CHAIN (previous) = NULL_TREE;
2937 list = nreverse (list);
2938 list = chainon (TREE_CHAIN (marker), list);
2939 }
2940 return list;
2941 }
2942
2943 /* Helper functions to dump the parser context stack. */
2944
2945 #define TAB_CONTEXT(C) \
2946 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2947
2948 static void
2949 java_debug_context_do (tab)
2950 int tab;
2951 {
2952 struct parser_ctxt *copy = ctxp;
2953 while (copy)
2954 {
2955 TAB_CONTEXT (tab);
2956 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2957 TAB_CONTEXT (tab);
2958 fprintf (stderr, "filename: %s\n", copy->filename);
2959 TAB_CONTEXT (tab);
2960 fprintf (stderr, "lineno: %d\n", copy->lineno);
2961 TAB_CONTEXT (tab);
2962 fprintf (stderr, "package: %s\n",
2963 (copy->package ?
2964 IDENTIFIER_POINTER (copy->package) : "<none>"));
2965 TAB_CONTEXT (tab);
2966 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2967 TAB_CONTEXT (tab);
2968 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2969 copy = copy->next;
2970 tab += 2;
2971 }
2972 }
2973
2974 /* Dump the stacked up parser contexts. Intended to be called from a
2975 debugger. */
2976
2977 void
2978 java_debug_context ()
2979 {
2980 java_debug_context_do (0);
2981 }
2982
2983 \f
2984
2985 /* Flag for the error report routine to issue the error the first time
2986 it's called (overriding the default behavior which is to drop the
2987 first invocation and honor the second one, taking advantage of a
2988 richer context. */
2989 static int force_error = 0;
2990
2991 /* Reporting an constructor invocation error. */
2992 static void
2993 parse_ctor_invocation_error ()
2994 {
2995 if (DECL_CONSTRUCTOR_P (current_function_decl))
2996 yyerror ("Constructor invocation must be first thing in a constructor");
2997 else
2998 yyerror ("Only constructors can invoke constructors");
2999 }
3000
3001 /* Reporting JDK1.1 features not implemented. */
3002
3003 static tree
3004 parse_jdk1_1_error (msg)
3005 const char *msg;
3006 {
3007 sorry (": `%s' JDK1.1(TM) feature", msg);
3008 java_error_count++;
3009 return empty_stmt_node;
3010 }
3011
3012 static int do_warning = 0;
3013
3014 void
3015 yyerror (msg)
3016 const char *msg;
3017 {
3018 static java_lc elc;
3019 static int prev_lineno;
3020 static const char *prev_msg;
3021
3022 int save_lineno;
3023 char *remainder, *code_from_source;
3024
3025 if (!force_error && prev_lineno == lineno)
3026 return;
3027
3028 /* Save current error location but report latter, when the context is
3029 richer. */
3030 if (ctxp->java_error_flag == 0)
3031 {
3032 ctxp->java_error_flag = 1;
3033 elc = ctxp->elc;
3034 /* Do something to use the previous line if we're reaching the
3035 end of the file... */
3036 #ifdef VERBOSE_SKELETON
3037 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
3038 #endif
3039 return;
3040 }
3041
3042 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3043 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
3044 return;
3045
3046 ctxp->java_error_flag = 0;
3047 if (do_warning)
3048 java_warning_count++;
3049 else
3050 java_error_count++;
3051
3052 if (elc.col == 0 && msg && msg[1] == ';')
3053 {
3054 elc.col = ctxp->p_line->char_col-1;
3055 elc.line = ctxp->p_line->lineno;
3056 }
3057
3058 save_lineno = lineno;
3059 prev_lineno = lineno = elc.line;
3060 prev_msg = msg;
3061
3062 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3063 obstack_grow0 (&temporary_obstack,
3064 code_from_source, strlen (code_from_source));
3065 remainder = obstack_finish (&temporary_obstack);
3066 if (do_warning)
3067 warning ("%s.\n%s", msg, remainder);
3068 else
3069 error ("%s.\n%s", msg, remainder);
3070
3071 /* This allow us to cheaply avoid an extra 'Invalid expression
3072 statement' error report when errors have been already reported on
3073 the same line. This occurs when we report an error but don't have
3074 a synchronization point other than ';', which
3075 expression_statement is the only one to take care of. */
3076 ctxp->prevent_ese = lineno = save_lineno;
3077 }
3078
3079 static void
3080 issue_warning_error_from_context (cl, msg, ap)
3081 tree cl;
3082 const char *msg;
3083 va_list ap;
3084 {
3085 const char *saved, *saved_input_filename;
3086 char buffer [4096];
3087 vsprintf (buffer, msg, ap);
3088 force_error = 1;
3089
3090 ctxp->elc.line = EXPR_WFL_LINENO (cl);
3091 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3092 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3093
3094 /* We have a CL, that's a good reason for using it if it contains data */
3095 saved = ctxp->filename;
3096 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3097 ctxp->filename = EXPR_WFL_FILENAME (cl);
3098 saved_input_filename = input_filename;
3099 input_filename = ctxp->filename;
3100 java_error (NULL);
3101 java_error (buffer);
3102 ctxp->filename = saved;
3103 input_filename = saved_input_filename;
3104 force_error = 0;
3105 }
3106
3107 /* Issue an error message at a current source line CL */
3108
3109 void
3110 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3111 {
3112 VA_OPEN (ap, msg);
3113 VA_FIXEDARG (ap, tree, cl);
3114 VA_FIXEDARG (ap, const char *, msg);
3115 issue_warning_error_from_context (cl, msg, ap);
3116 VA_CLOSE (ap);
3117 }
3118
3119 /* Issue a warning at a current source line CL */
3120
3121 static void
3122 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3123 {
3124 VA_OPEN (ap, msg);
3125 VA_FIXEDARG (ap, tree, cl);
3126 VA_FIXEDARG (ap, const char *, msg);
3127
3128 force_error = do_warning = 1;
3129 issue_warning_error_from_context (cl, msg, ap);
3130 do_warning = force_error = 0;
3131 VA_CLOSE (ap);
3132 }
3133
3134 static tree
3135 find_expr_with_wfl (node)
3136 tree node;
3137 {
3138 while (node)
3139 {
3140 char code;
3141 tree to_return;
3142
3143 switch (TREE_CODE (node))
3144 {
3145 case BLOCK:
3146 node = BLOCK_EXPR_BODY (node);
3147 continue;
3148
3149 case COMPOUND_EXPR:
3150 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3151 if (to_return)
3152 return to_return;
3153 node = TREE_OPERAND (node, 1);
3154 continue;
3155
3156 case LOOP_EXPR:
3157 node = TREE_OPERAND (node, 0);
3158 continue;
3159
3160 case LABELED_BLOCK_EXPR:
3161 node = TREE_OPERAND (node, 1);
3162 continue;
3163
3164 default:
3165 code = TREE_CODE_CLASS (TREE_CODE (node));
3166 if (((code == '1') || (code == '2') || (code == 'e'))
3167 && EXPR_WFL_LINECOL (node))
3168 return node;
3169 return NULL_TREE;
3170 }
3171 }
3172 return NULL_TREE;
3173 }
3174
3175 /* Issue a missing return statement error. Uses METHOD to figure the
3176 last line of the method the error occurs in. */
3177
3178 static void
3179 missing_return_error (method)
3180 tree method;
3181 {
3182 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3183 parse_error_context (wfl_operator, "Missing return statement");
3184 }
3185
3186 /* Issue an unreachable statement error. From NODE, find the next
3187 statement to report appropriately. */
3188 static void
3189 unreachable_stmt_error (node)
3190 tree node;
3191 {
3192 /* Browse node to find the next expression node that has a WFL. Use
3193 the location to report the error */
3194 if (TREE_CODE (node) == COMPOUND_EXPR)
3195 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3196 else
3197 node = find_expr_with_wfl (node);
3198
3199 if (node)
3200 {
3201 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3202 parse_error_context (wfl_operator, "Unreachable statement");
3203 }
3204 else
3205 abort ();
3206 }
3207
3208 int
3209 java_report_errors ()
3210 {
3211 if (java_error_count)
3212 fprintf (stderr, "%d error%s",
3213 java_error_count, (java_error_count == 1 ? "" : "s"));
3214 if (java_warning_count)
3215 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3216 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3217 if (java_error_count || java_warning_count)
3218 putc ('\n', stderr);
3219 return java_error_count;
3220 }
3221
3222 static char *
3223 java_accstring_lookup (flags)
3224 int flags;
3225 {
3226 static char buffer [80];
3227 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3228
3229 /* Access modifier looked-up first for easier report on forbidden
3230 access. */
3231 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3232 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3233 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3234 if (flags & ACC_STATIC) COPY_RETURN ("static");
3235 if (flags & ACC_FINAL) COPY_RETURN ("final");
3236 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3237 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3238 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3239 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3240 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3241 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3242
3243 buffer [0] = '\0';
3244 return buffer;
3245 #undef COPY_RETURN
3246 }
3247
3248 /* Issuing error messages upon redefinition of classes, interfaces or
3249 variables. */
3250
3251 static void
3252 classitf_redefinition_error (context, id, decl, cl)
3253 const char *context;
3254 tree id, decl, cl;
3255 {
3256 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3257 context, IDENTIFIER_POINTER (id),
3258 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3259 /* Here we should point out where its redefined. It's a unicode. FIXME */
3260 }
3261
3262 static void
3263 variable_redefinition_error (context, name, type, line)
3264 tree context, name, type;
3265 int line;
3266 {
3267 const char *type_name;
3268
3269 /* Figure a proper name for type. We might haven't resolved it */
3270 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3271 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3272 else
3273 type_name = lang_printable_name (type, 0);
3274
3275 parse_error_context (context,
3276 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3277 IDENTIFIER_POINTER (name),
3278 type_name, IDENTIFIER_POINTER (name), line);
3279 }
3280
3281 /* If ANAME is terminated with `[]', it indicates an array. This
3282 function returns the number of `[]' found and if this number is
3283 greater than zero, it extracts the array type name and places it in
3284 the node pointed to by TRIMMED unless TRIMMED is null. */
3285
3286 static int
3287 build_type_name_from_array_name (aname, trimmed)
3288 tree aname;
3289 tree *trimmed;
3290 {
3291 const char *name = IDENTIFIER_POINTER (aname);
3292 int len = IDENTIFIER_LENGTH (aname);
3293 int array_dims;
3294
3295 STRING_STRIP_BRACKETS (name, len, array_dims);
3296
3297 if (array_dims && trimmed)
3298 *trimmed = get_identifier_with_length (name, len);
3299
3300 return array_dims;
3301 }
3302
3303 static tree
3304 build_array_from_name (type, type_wfl, name, ret_name)
3305 tree type, type_wfl, name, *ret_name;
3306 {
3307 int more_dims = 0;
3308
3309 /* Eventually get more dims */
3310 more_dims = build_type_name_from_array_name (name, &name);
3311
3312 /* If we have, then craft a new type for this variable */
3313 if (more_dims)
3314 {
3315 tree save = type;
3316
3317 /* If we have a pointer, use its type */
3318 if (TREE_CODE (type) == POINTER_TYPE)
3319 type = TREE_TYPE (type);
3320
3321 /* Building the first dimension of a primitive type uses this
3322 function */
3323 if (JPRIMITIVE_TYPE_P (type))
3324 {
3325 type = build_java_array_type (type, -1);
3326 more_dims--;
3327 }
3328 /* Otherwise, if we have a WFL for this type, use it (the type
3329 is already an array on an unresolved type, and we just keep
3330 on adding dimensions) */
3331 else if (type_wfl)
3332 {
3333 type = type_wfl;
3334 more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3335 NULL);
3336 }
3337
3338 /* Add all the dimensions */
3339 while (more_dims--)
3340 type = build_unresolved_array_type (type);
3341
3342 /* The type may have been incomplete in the first place */
3343 if (type_wfl)
3344 type = obtain_incomplete_type (type);
3345 }
3346
3347 if (ret_name)
3348 *ret_name = name;
3349 return type;
3350 }
3351
3352 /* Build something that the type identifier resolver will identify as
3353 being an array to an unresolved type. TYPE_WFL is a WFL on a
3354 identifier. */
3355
3356 static tree
3357 build_unresolved_array_type (type_or_wfl)
3358 tree type_or_wfl;
3359 {
3360 const char *ptr;
3361 tree wfl;
3362
3363 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3364 just create a array type */
3365 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3366 return build_java_array_type (type_or_wfl, -1);
3367
3368 obstack_grow (&temporary_obstack,
3369 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3370 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3371 obstack_grow0 (&temporary_obstack, "[]", 2);
3372 ptr = obstack_finish (&temporary_obstack);
3373 wfl = build_expr_wfl (get_identifier (ptr),
3374 EXPR_WFL_FILENAME (type_or_wfl),
3375 EXPR_WFL_LINENO (type_or_wfl),
3376 EXPR_WFL_COLNO (type_or_wfl));
3377 /* Re-install the existing qualifications so that the type can be
3378 resolved properly. */
3379 EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3380 return wfl;
3381 }
3382
3383 static void
3384 parser_add_interface (class_decl, interface_decl, wfl)
3385 tree class_decl, interface_decl, wfl;
3386 {
3387 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3388 parse_error_context (wfl, "Interface `%s' repeated",
3389 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3390 }
3391
3392 /* Bulk of common class/interface checks. Return 1 if an error was
3393 encountered. TAG is 0 for a class, 1 for an interface. */
3394
3395 static int
3396 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3397 int is_interface, flags;
3398 tree raw_name, qualified_name, decl, cl;
3399 {
3400 tree node;
3401 int sca = 0; /* Static class allowed */
3402 int icaf = 0; /* Inner class allowed flags */
3403 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3404
3405 if (!quiet_flag)
3406 fprintf (stderr, " %s%s %s",
3407 (CPC_INNER_P () ? "inner" : ""),
3408 (is_interface ? "interface" : "class"),
3409 IDENTIFIER_POINTER (qualified_name));
3410
3411 /* Scope of an interface/class type name:
3412 - Can't be imported by a single type import
3413 - Can't already exists in the package */
3414 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3415 && (node = find_name_in_single_imports (raw_name))
3416 && !CPC_INNER_P ())
3417 {
3418 parse_error_context
3419 (cl, "%s name `%s' clashes with imported type `%s'",
3420 (is_interface ? "Interface" : "Class"),
3421 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3422 return 1;
3423 }
3424 if (decl && CLASS_COMPLETE_P (decl))
3425 {
3426 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3427 qualified_name, decl, cl);
3428 return 1;
3429 }
3430
3431 if (check_inner_class_redefinition (raw_name, cl))
3432 return 1;
3433
3434 /* If public, file name should match class/interface name, except
3435 when dealing with an inner class */
3436 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3437 {
3438 const char *f;
3439
3440 for (f = &input_filename [strlen (input_filename)];
3441 f != input_filename && ! IS_DIR_SEPARATOR (f[0]);
3442 f--)
3443 ;
3444 if (IS_DIR_SEPARATOR (f[0]))
3445 f++;
3446 if (strncmp (IDENTIFIER_POINTER (raw_name),
3447 f , IDENTIFIER_LENGTH (raw_name)) ||
3448 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3449 parse_error_context
3450 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3451 (is_interface ? "interface" : "class"),
3452 IDENTIFIER_POINTER (qualified_name),
3453 IDENTIFIER_POINTER (raw_name));
3454 }
3455
3456 /* Static classes can be declared only in top level classes. Note:
3457 once static, a inner class is a top level class. */
3458 if (flags & ACC_STATIC)
3459 {
3460 /* Catch the specific error of declaring an class inner class
3461 with no toplevel enclosing class. Prevent check_modifiers from
3462 complaining a second time */
3463 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3464 {
3465 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3466 IDENTIFIER_POINTER (qualified_name));
3467 sca = ACC_STATIC;
3468 }
3469 /* Else, in the context of a top-level class declaration, let
3470 `check_modifiers' do its job, otherwise, give it a go */
3471 else
3472 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3473 }
3474
3475 /* Inner classes can be declared private or protected
3476 within their enclosing classes. */
3477 if (CPC_INNER_P ())
3478 {
3479 /* A class which is local to a block can't be public, private,
3480 protected or static. But it is created final, so allow this
3481 one. */
3482 if (current_function_decl)
3483 icaf = sca = uaaf = ACC_FINAL;
3484 else
3485 {
3486 check_modifiers_consistency (flags);
3487 icaf = ACC_PROTECTED;
3488 if (! CLASS_INTERFACE (GET_CPC ()))
3489 icaf |= ACC_PRIVATE;
3490 }
3491 }
3492
3493 if (is_interface)
3494 {
3495 if (CPC_INNER_P ())
3496 uaaf = INTERFACE_INNER_MODIFIERS;
3497 else
3498 uaaf = INTERFACE_MODIFIERS;
3499
3500 check_modifiers ("Illegal modifier `%s' for interface declaration",
3501 flags, uaaf);
3502 }
3503 else
3504 check_modifiers ((current_function_decl ?
3505 "Illegal modifier `%s' for local class declaration" :
3506 "Illegal modifier `%s' for class declaration"),
3507 flags, uaaf|sca|icaf);
3508 return 0;
3509 }
3510
3511 /* Construct a nested class name. If the final component starts with
3512 a digit, return true. Otherwise return false. */
3513 static int
3514 make_nested_class_name (cpc_list)
3515 tree cpc_list;
3516 {
3517 tree name;
3518
3519 if (!cpc_list)
3520 return 0;
3521
3522 make_nested_class_name (TREE_CHAIN (cpc_list));
3523
3524 /* Pick the qualified name when dealing with the first upmost
3525 enclosing class */
3526 name = (TREE_CHAIN (cpc_list)
3527 ? TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3528 obstack_grow (&temporary_obstack,
3529 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3530 obstack_1grow (&temporary_obstack, '$');
3531
3532 return ISDIGIT (IDENTIFIER_POINTER (name)[0]);
3533 }
3534
3535 /* Can't redefine a class already defined in an earlier scope. */
3536
3537 static int
3538 check_inner_class_redefinition (raw_name, cl)
3539 tree raw_name, cl;
3540 {
3541 tree scope_list;
3542
3543 for (scope_list = GET_CPC_LIST (); scope_list;
3544 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3545 if (raw_name == GET_CPC_UN_NODE (scope_list))
3546 {
3547 parse_error_context
3548 (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",
3549 IDENTIFIER_POINTER (raw_name));
3550 return 1;
3551 }
3552 return 0;
3553 }
3554
3555 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. If we fail,
3556 we remember ENCLOSING and SUPER. */
3557
3558 static tree
3559 resolve_inner_class (circularity_hash, cl, enclosing, super, class_type)
3560 htab_t circularity_hash;
3561 tree cl, *enclosing, *super, class_type;
3562 {
3563 tree local_enclosing = *enclosing;
3564 tree local_super = NULL_TREE;
3565
3566 while (local_enclosing)
3567 {
3568 tree intermediate, decl;
3569
3570 *htab_find_slot (circularity_hash, local_enclosing, INSERT) =
3571 local_enclosing;
3572
3573 if ((decl = find_as_inner_class (local_enclosing, class_type, cl)))
3574 return decl;
3575
3576 intermediate = local_enclosing;
3577 /* Explore enclosing contexts. */
3578 while (INNER_CLASS_DECL_P (intermediate))
3579 {
3580 intermediate = DECL_CONTEXT (intermediate);
3581 if ((decl = find_as_inner_class (intermediate, class_type, cl)))
3582 return decl;
3583 }
3584
3585 /* Now go to the upper classes, bail out if necessary. We will
3586 analyze the returned SUPER and act accordingly (see
3587 do_resolve_class.) */
3588 local_super = CLASSTYPE_SUPER (TREE_TYPE (local_enclosing));
3589 if (!local_super || local_super == object_type_node)
3590 break;
3591
3592 if (TREE_CODE (local_super) == POINTER_TYPE)
3593 local_super = do_resolve_class (NULL, local_super, NULL, NULL);
3594 else
3595 local_super = TYPE_NAME (local_super);
3596
3597 /* We may not have checked for circular inheritance yet, so do so
3598 here to prevent an infinite loop. */
3599 if (htab_find (circularity_hash, local_super) != NULL)
3600 {
3601 if (!cl)
3602 cl = lookup_cl (local_enclosing);
3603
3604 parse_error_context
3605 (cl, "Cyclic inheritance involving %s",
3606 IDENTIFIER_POINTER (DECL_NAME (local_enclosing)));
3607 local_enclosing = NULL_TREE;
3608 }
3609 else
3610 local_enclosing = local_super;
3611 }
3612
3613 /* We failed. Return LOCAL_SUPER and LOCAL_ENCLOSING. */
3614 *super = local_super;
3615 *enclosing = local_enclosing;
3616
3617 return NULL_TREE;
3618 }
3619
3620 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3621 qualified. */
3622
3623 static tree
3624 find_as_inner_class (enclosing, name, cl)
3625 tree enclosing, name, cl;
3626 {
3627 tree qual, to_return;
3628 if (!enclosing)
3629 return NULL_TREE;
3630
3631 name = TYPE_NAME (name);
3632
3633 /* First search: within the scope of `enclosing', search for name */
3634 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3635 qual = EXPR_WFL_QUALIFICATION (cl);
3636 else if (cl)
3637 qual = build_tree_list (cl, NULL_TREE);
3638 else
3639 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3640
3641 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3642 return to_return;
3643
3644 /* We're dealing with a qualified name. Try to resolve thing until
3645 we get something that is an enclosing class. */
3646 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3647 {
3648 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3649
3650 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3651 qual = TREE_CHAIN (qual))
3652 {
3653 acc = merge_qualified_name (acc,
3654 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3655 BUILD_PTR_FROM_NAME (ptr, acc);
3656 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3657 }
3658
3659 /* A NULL qual and a decl means that the search ended
3660 successfully?!? We have to do something then. FIXME */
3661
3662 if (decl)
3663 enclosing = decl;
3664 else
3665 qual = EXPR_WFL_QUALIFICATION (cl);
3666 }
3667 /* Otherwise, create a qual for the other part of the resolution. */
3668 else
3669 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3670
3671 return find_as_inner_class_do (qual, enclosing);
3672 }
3673
3674 /* We go inside the list of sub classes and try to find a way
3675 through. */
3676
3677 static tree
3678 find_as_inner_class_do (qual, enclosing)
3679 tree qual, enclosing;
3680 {
3681 if (!qual)
3682 return NULL_TREE;
3683
3684 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3685 {
3686 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3687 tree next_enclosing = NULL_TREE;
3688 tree inner_list;
3689
3690 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3691 inner_list; inner_list = TREE_CHAIN (inner_list))
3692 {
3693 if (TREE_VALUE (inner_list) == name_to_match)
3694 {
3695 next_enclosing = TREE_PURPOSE (inner_list);
3696 break;
3697 }
3698 }
3699 enclosing = next_enclosing;
3700 }
3701
3702 return (!qual && enclosing ? enclosing : NULL_TREE);
3703 }
3704
3705 /* Reach all inner classes and tie their unqualified name to a
3706 DECL. */
3707
3708 static void
3709 set_nested_class_simple_name_value (outer, set)
3710 tree outer;
3711 int set;
3712 {
3713 tree l;
3714
3715 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3716 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3717 TREE_PURPOSE (l) : NULL_TREE);
3718 }
3719
3720 static void
3721 link_nested_class_to_enclosing ()
3722 {
3723 if (GET_ENCLOSING_CPC ())
3724 {
3725 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3726 DECL_INNER_CLASS_LIST (enclosing) =
3727 tree_cons (GET_CPC (), GET_CPC_UN (),
3728 DECL_INNER_CLASS_LIST (enclosing));
3729 }
3730 }
3731
3732 static tree
3733 maybe_make_nested_class_name (name)
3734 tree name;
3735 {
3736 tree id = NULL_TREE;
3737
3738 if (CPC_INNER_P ())
3739 {
3740 /* If we're in a function, we must append a number to create the
3741 nested class name. However, we don't do this if the class we
3742 are constructing is anonymous, because in that case we'll
3743 already have a number as the class name. */
3744 if (! make_nested_class_name (GET_CPC_LIST ())
3745 && current_function_decl != NULL_TREE
3746 && ! ISDIGIT (IDENTIFIER_POINTER (name)[0]))
3747 {
3748 char buf[10];
3749 sprintf (buf, "%d", anonymous_class_counter);
3750 ++anonymous_class_counter;
3751 obstack_grow (&temporary_obstack, buf, strlen (buf));
3752 obstack_1grow (&temporary_obstack, '$');
3753 }
3754 obstack_grow0 (&temporary_obstack,
3755 IDENTIFIER_POINTER (name),
3756 IDENTIFIER_LENGTH (name));
3757 id = get_identifier (obstack_finish (&temporary_obstack));
3758 if (ctxp->package)
3759 QUALIFIED_P (id) = 1;
3760 }
3761 return id;
3762 }
3763
3764 /* If DECL is NULL, create and push a new DECL, record the current
3765 line CL and do other maintenance things. */
3766
3767 static tree
3768 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3769 tree decl, raw_name, qualified_name, cl;
3770 {
3771 if (!decl)
3772 decl = push_class (make_class (), qualified_name);
3773
3774 /* Take care of the file and line business */
3775 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3776 /* If we're emiting xrefs, store the line/col number information */
3777 if (flag_emit_xref)
3778 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3779 else
3780 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3781 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3782 CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3783 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3784 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3785
3786 PUSH_CPC (decl, raw_name);
3787 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3788
3789 /* Link the declaration to the already seen ones */
3790 TREE_CHAIN (decl) = ctxp->class_list;
3791 ctxp->class_list = decl;
3792
3793 /* Create a new nodes in the global lists */
3794 gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3795 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3796
3797 /* Install a new dependency list element */
3798 create_jdep_list (ctxp);
3799
3800 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3801 IDENTIFIER_POINTER (qualified_name)));
3802 return decl;
3803 }
3804
3805 static void
3806 add_superinterfaces (decl, interface_list)
3807 tree decl, interface_list;
3808 {
3809 tree node;
3810 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3811 takes care of ensuring that:
3812 - This is an accessible interface type,
3813 - Circularity detection.
3814 parser_add_interface is then called. If present but not defined,
3815 the check operation is delayed until the super interface gets
3816 defined. */
3817 for (node = interface_list; node; node = TREE_CHAIN (node))
3818 {
3819 tree current = TREE_PURPOSE (node);
3820 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3821 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3822 {
3823 if (!parser_check_super_interface (idecl, decl, current))
3824 parser_add_interface (decl, idecl, current);
3825 }
3826 else
3827 register_incomplete_type (JDEP_INTERFACE,
3828 current, decl, NULL_TREE);
3829 }
3830 }
3831
3832 /* Create an interface in pass1 and return its decl. Return the
3833 interface's decl in pass 2. */
3834
3835 static tree
3836 create_interface (flags, id, super)
3837 int flags;
3838 tree id, super;
3839 {
3840 tree raw_name = EXPR_WFL_NODE (id);
3841 tree q_name = parser_qualified_classname (raw_name);
3842 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3843
3844 /* Certain syntax errors are making SUPER be like ID. Avoid this
3845 case. */
3846 if (ctxp->class_err && id == super)
3847 super = NULL;
3848
3849 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3850
3851 /* Basic checks: scope, redefinition, modifiers */
3852 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3853 {
3854 PUSH_ERROR ();
3855 return NULL_TREE;
3856 }
3857
3858 /* Suspend the current parsing context if we're parsing an inner
3859 interface */
3860 if (CPC_INNER_P ())
3861 {
3862 java_parser_context_suspend ();
3863 /* Interface members are public. */
3864 if (CLASS_INTERFACE (GET_CPC ()))
3865 flags |= ACC_PUBLIC;
3866 }
3867
3868 /* Push a new context for (static) initialized upon declaration fields */
3869 java_parser_context_push_initialized_field ();
3870
3871 /* Interface modifiers check
3872 - public/abstract allowed (already done at that point)
3873 - abstract is obsolete (comes first, it's a warning, or should be)
3874 - Can't use twice the same (checked in the modifier rule) */
3875 if ((flags & ACC_ABSTRACT) && flag_redundant)
3876 parse_warning_context
3877 (MODIFIER_WFL (ABSTRACT_TK),
3878 "Redundant use of `abstract' modifier. Interface `%s' is implicitly abstract", IDENTIFIER_POINTER (raw_name));
3879
3880 /* Create a new decl if DECL is NULL, otherwise fix it */
3881 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3882
3883 /* Set super info and mark the class a complete */
3884 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3885 object_type_node, ctxp->interface_number);
3886 ctxp->interface_number = 0;
3887 CLASS_COMPLETE_P (decl) = 1;
3888 add_superinterfaces (decl, super);
3889
3890 return decl;
3891 }
3892
3893 /* Patch anonymous class CLASS, by either extending or implementing
3894 DEP. */
3895
3896 static void
3897 patch_anonymous_class (type_decl, class_decl, wfl)
3898 tree type_decl, class_decl, wfl;
3899 {
3900 tree class = TREE_TYPE (class_decl);
3901 tree type = TREE_TYPE (type_decl);
3902 tree binfo = TYPE_BINFO (class);
3903
3904 /* If it's an interface, implement it */
3905 if (CLASS_INTERFACE (type_decl))
3906 {
3907 tree s_binfo;
3908 int length;
3909
3910 if (parser_check_super_interface (type_decl, class_decl, wfl))
3911 return;
3912
3913 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3914 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3915 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3916 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3917 /* And add the interface */
3918 parser_add_interface (class_decl, type_decl, wfl);
3919 }
3920 /* Otherwise, it's a type we want to extend */
3921 else
3922 {
3923 if (parser_check_super (type_decl, class_decl, wfl))
3924 return;
3925 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3926 }
3927 }
3928
3929 static tree
3930 create_anonymous_class (location, type_name)
3931 int location;
3932 tree type_name;
3933 {
3934 char buffer [80];
3935 tree super = NULL_TREE, itf = NULL_TREE;
3936 tree id, type_decl, class;
3937
3938 /* The unqualified name of the anonymous class. It's just a number. */
3939 sprintf (buffer, "%d", anonymous_class_counter++);
3940 id = build_wfl_node (get_identifier (buffer));
3941 EXPR_WFL_LINECOL (id) = location;
3942
3943 /* We know about the type to extend/implement. We go ahead */
3944 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3945 {
3946 /* Create a class which either implements on extends the designated
3947 class. The class bears an innacessible name. */
3948 if (CLASS_INTERFACE (type_decl))
3949 {
3950 /* It's OK to modify it here. It's been already used and
3951 shouldn't be reused */
3952 ctxp->interface_number = 1;
3953 /* Interfaces should presented as a list of WFLs */
3954 itf = build_tree_list (type_name, NULL_TREE);
3955 }
3956 else
3957 super = type_name;
3958 }
3959
3960 class = create_class (ACC_FINAL, id, super, itf);
3961
3962 /* We didn't know anything about the stuff. We register a dependence. */
3963 if (!type_decl)
3964 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3965
3966 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3967 return class;
3968 }
3969
3970 /* Create a class in pass1 and return its decl. Return class
3971 interface's decl in pass 2. */
3972
3973 static tree
3974 create_class (flags, id, super, interfaces)
3975 int flags;
3976 tree id, super, interfaces;
3977 {
3978 tree raw_name = EXPR_WFL_NODE (id);
3979 tree class_id, decl;
3980 tree super_decl_type;
3981
3982 /* Certain syntax errors are making SUPER be like ID. Avoid this
3983 case. */
3984 if (ctxp->class_err && id == super)
3985 super = NULL;
3986
3987 class_id = parser_qualified_classname (raw_name);
3988 decl = IDENTIFIER_CLASS_VALUE (class_id);
3989 EXPR_WFL_NODE (id) = class_id;
3990
3991 /* Basic check: scope, redefinition, modifiers */
3992 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3993 {
3994 PUSH_ERROR ();
3995 return NULL_TREE;
3996 }
3997
3998 /* Suspend the current parsing context if we're parsing an inner
3999 class or an anonymous class. */
4000 if (CPC_INNER_P ())
4001 {
4002 java_parser_context_suspend ();
4003 /* Interface members are public. */
4004 if (CLASS_INTERFACE (GET_CPC ()))
4005 flags |= ACC_PUBLIC;
4006 }
4007
4008 /* Push a new context for (static) initialized upon declaration fields */
4009 java_parser_context_push_initialized_field ();
4010
4011 /* Class modifier check:
4012 - Allowed modifier (already done at that point)
4013 - abstract AND final forbidden
4014 - Public classes defined in the correct file */
4015 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
4016 parse_error_context
4017 (id, "Class `%s' can't be declared both abstract and final",
4018 IDENTIFIER_POINTER (raw_name));
4019
4020 /* Create a new decl if DECL is NULL, otherwise fix it */
4021 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
4022
4023 /* If SUPER exists, use it, otherwise use Object */
4024 if (super)
4025 {
4026 /* Can't extend java.lang.Object */
4027 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4028 {
4029 parse_error_context (id, "Can't extend `java.lang.Object'");
4030 return NULL_TREE;
4031 }
4032
4033 super_decl_type =
4034 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4035 }
4036 else if (TREE_TYPE (decl) != object_type_node)
4037 super_decl_type = object_type_node;
4038 /* We're defining java.lang.Object */
4039 else
4040 super_decl_type = NULL_TREE;
4041
4042 /* A class nested in an interface is implicitly static. */
4043 if (INNER_CLASS_DECL_P (decl)
4044 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4045 {
4046 flags |= ACC_STATIC;
4047 }
4048
4049 /* Set super info and mark the class as complete. */
4050 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
4051 ctxp->interface_number);
4052 ctxp->interface_number = 0;
4053 CLASS_COMPLETE_P (decl) = 1;
4054 add_superinterfaces (decl, interfaces);
4055
4056 /* Add the private this$<n> field, Replicate final locals still in
4057 scope as private final fields mangled like val$<local_name>.
4058 This doesn't not occur for top level (static) inner classes. */
4059 if (PURE_INNER_CLASS_DECL_P (decl))
4060 add_inner_class_fields (decl, current_function_decl);
4061
4062 /* If doing xref, store the location at which the inherited class
4063 (if any) was seen. */
4064 if (flag_emit_xref && super)
4065 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
4066
4067 /* Eventually sets the @deprecated tag flag */
4068 CHECK_DEPRECATED (decl);
4069
4070 /* Reset the anonymous class counter when declaring non inner classes */
4071 if (!INNER_CLASS_DECL_P (decl))
4072 anonymous_class_counter = 1;
4073
4074 return decl;
4075 }
4076
4077 /* End a class declaration: register the statements used to create
4078 finit$ and <clinit>, pop the current class and resume the prior
4079 parser context if necessary. */
4080
4081 static void
4082 end_class_declaration (resume)
4083 int resume;
4084 {
4085 /* If an error occurred, context weren't pushed and won't need to be
4086 popped by a resume. */
4087 int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4088
4089 if (GET_CPC () != error_mark_node)
4090 dump_java_tree (TDI_class, GET_CPC ());
4091
4092 java_parser_context_pop_initialized_field ();
4093 POP_CPC ();
4094 if (resume && no_error_occurred)
4095 java_parser_context_resume ();
4096
4097 /* We're ending a class declaration, this is a good time to reset
4098 the interface cout. Note that might have been already done in
4099 create_interface, but if at that time an inner class was being
4100 dealt with, the interface count was reset in a context created
4101 for the sake of handling inner classes declaration. */
4102 ctxp->interface_number = 0;
4103 }
4104
4105 static void
4106 add_inner_class_fields (class_decl, fct_decl)
4107 tree class_decl;
4108 tree fct_decl;
4109 {
4110 tree block, marker, f;
4111
4112 f = add_field (TREE_TYPE (class_decl),
4113 build_current_thisn (TREE_TYPE (class_decl)),
4114 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4115 ACC_PRIVATE);
4116 FIELD_THISN (f) = 1;
4117
4118 if (!fct_decl)
4119 return;
4120
4121 for (block = GET_CURRENT_BLOCK (fct_decl);
4122 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4123 {
4124 tree decl;
4125 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4126 {
4127 tree name, pname;
4128 tree wfl, init, list;
4129
4130 /* Avoid non final arguments. */
4131 if (!LOCAL_FINAL_P (decl))
4132 continue;
4133
4134 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4135 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4136 wfl = build_wfl_node (name);
4137 init = build_wfl_node (pname);
4138 /* Build an initialization for the field: it will be
4139 initialized by a parameter added to finit$, bearing a
4140 mangled name of the field itself (param$<n>.) The
4141 parameter is provided to finit$ by the constructor
4142 invoking it (hence the constructor will also feature a
4143 hidden parameter, set to the value of the outer context
4144 local at the time the inner class is created.)
4145
4146 Note: we take into account all possible locals that can
4147 be accessed by the inner class. It's actually not trivial
4148 to minimize these aliases down to the ones really
4149 used. One way to do that would be to expand all regular
4150 methods first, then finit$ to get a picture of what's
4151 used. It works with the exception that we would have to
4152 go back on all constructor invoked in regular methods to
4153 have their invokation reworked (to include the right amount
4154 of alias initializer parameters.)
4155
4156 The only real way around, I think, is a first pass to
4157 identify locals really used in the inner class. We leave
4158 the flag FIELD_LOCAL_ALIAS_USED around for that future
4159 use.
4160
4161 On the other hand, it only affect local inner classes,
4162 whose constructors (and finit$ call) will be featuring
4163 unecessary arguments. It's easy for a developper to keep
4164 this number of parameter down by using the `final'
4165 keyword only when necessary. For the time being, we can
4166 issue a warning on unecessary finals. FIXME */
4167 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4168 wfl, init);
4169
4170 /* Register the field. The TREE_LIST holding the part
4171 initialized/initializer will be marked ARG_FINAL_P so
4172 that the created field can be marked
4173 FIELD_LOCAL_ALIAS. */
4174 list = build_tree_list (wfl, init);
4175 ARG_FINAL_P (list) = 1;
4176 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4177 }
4178 }
4179
4180 if (!CPC_INITIALIZER_STMT (ctxp))
4181 return;
4182
4183 /* If we ever registered an alias field, insert and marker to
4184 remeber where the list ends. The second part of the list (the one
4185 featuring initialized fields) so it can be later reversed to
4186 enforce 8.5. The marker will be removed during that operation. */
4187 marker = build_tree_list (NULL_TREE, NULL_TREE);
4188 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4189 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4190 }
4191
4192 /* Can't use lookup_field () since we don't want to load the class and
4193 can't set the CLASS_LOADED_P flag */
4194
4195 static tree
4196 find_field (class, name)
4197 tree class;
4198 tree name;
4199 {
4200 tree decl;
4201 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4202 {
4203 if (DECL_NAME (decl) == name)
4204 return decl;
4205 }
4206 return NULL_TREE;
4207 }
4208
4209 /* Wrap around lookup_field that doesn't potentially upset the value
4210 of CLASS */
4211
4212 static tree
4213 lookup_field_wrapper (class, name)
4214 tree class, name;
4215 {
4216 tree type = class;
4217 tree decl = NULL_TREE;
4218 java_parser_context_save_global ();
4219
4220 /* Last chance: if we're within the context of an inner class, we
4221 might be trying to access a local variable defined in an outer
4222 context. We try to look for it now. */
4223 if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4224 {
4225 tree new_name;
4226 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4227 decl = lookup_field (&type, new_name);
4228 if (decl && decl != error_mark_node)
4229 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4230 }
4231 if (!decl || decl == error_mark_node)
4232 {
4233 type = class;
4234 decl = lookup_field (&type, name);
4235 }
4236
4237 /* If the field still hasn't been found, try the next enclosing context. */
4238 if (!decl && INNER_CLASS_TYPE_P (class))
4239 {
4240 tree outer_type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
4241 decl = lookup_field_wrapper (outer_type, name);
4242 }
4243
4244 java_parser_context_restore_global ();
4245 return decl == error_mark_node ? NULL : decl;
4246 }
4247
4248 /* Find duplicate field within the same class declarations and report
4249 the error. Returns 1 if a duplicated field was found, 0
4250 otherwise. */
4251
4252 static int
4253 duplicate_declaration_error_p (new_field_name, new_type, cl)
4254 tree new_field_name, new_type, cl;
4255 {
4256 /* This might be modified to work with method decl as well */
4257 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4258 if (decl)
4259 {
4260 char *t1 = xstrdup (purify_type_name
4261 ((TREE_CODE (new_type) == POINTER_TYPE
4262 && TREE_TYPE (new_type) == NULL_TREE) ?
4263 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4264 lang_printable_name (new_type, 1)));
4265 /* The type may not have been completed by the time we report
4266 the error */
4267 char *t2 = xstrdup (purify_type_name
4268 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4269 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4270 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4271 lang_printable_name (TREE_TYPE (decl), 1)));
4272 parse_error_context
4273 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4274 t1, IDENTIFIER_POINTER (new_field_name),
4275 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4276 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4277 free (t1);
4278 free (t2);
4279 return 1;
4280 }
4281 return 0;
4282 }
4283
4284 /* Field registration routine. If TYPE doesn't exist, field
4285 declarations are linked to the undefined TYPE dependency list, to
4286 be later resolved in java_complete_class () */
4287
4288 static void
4289 register_fields (flags, type, variable_list)
4290 int flags;
4291 tree type, variable_list;
4292 {
4293 tree current, saved_type;
4294 tree class_type = NULL_TREE;
4295 int saved_lineno = lineno;
4296 int must_chain = 0;
4297 tree wfl = NULL_TREE;
4298
4299 if (GET_CPC ())
4300 class_type = TREE_TYPE (GET_CPC ());
4301
4302 if (!class_type || class_type == error_mark_node)
4303 return;
4304
4305 /* If we're adding fields to interfaces, those fields are public,
4306 static, final */
4307 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4308 {
4309 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4310 flags, ACC_PUBLIC, "interface field(s)");
4311 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4312 flags, ACC_STATIC, "interface field(s)");
4313 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4314 flags, ACC_FINAL, "interface field(s)");
4315 check_modifiers ("Illegal interface member modifier `%s'", flags,
4316 INTERFACE_FIELD_MODIFIERS);
4317 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4318 }
4319
4320 /* Obtain a suitable type for resolution, if necessary */
4321 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4322
4323 /* If TYPE is fully resolved and we don't have a reference, make one */
4324 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4325
4326 for (current = variable_list, saved_type = type; current;
4327 current = TREE_CHAIN (current), type = saved_type)
4328 {
4329 tree real_type;
4330 tree field_decl;
4331 tree cl = TREE_PURPOSE (current);
4332 tree init = TREE_VALUE (current);
4333 tree current_name = EXPR_WFL_NODE (cl);
4334
4335 /* Can't declare non-final static fields in inner classes */
4336 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4337 && !(flags & ACC_FINAL))
4338 parse_error_context
4339 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4340 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4341 lang_printable_name (class_type, 0));
4342
4343 /* Process NAME, as it may specify extra dimension(s) for it */
4344 type = build_array_from_name (type, wfl, current_name, &current_name);
4345
4346 /* Type adjustment. We may have just readjusted TYPE because
4347 the variable specified more dimensions. Make sure we have
4348 a reference if we can and don't have one already. Also
4349 change the name if we have an init. */
4350 if (type != saved_type)
4351 {
4352 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4353 if (init)
4354 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4355 }
4356
4357 real_type = GET_REAL_TYPE (type);
4358 /* Check for redeclarations */
4359 if (duplicate_declaration_error_p (current_name, real_type, cl))
4360 continue;
4361
4362 /* Set lineno to the line the field was found and create a
4363 declaration for it. Eventually sets the @deprecated tag flag. */
4364 if (flag_emit_xref)
4365 lineno = EXPR_WFL_LINECOL (cl);
4366 else
4367 lineno = EXPR_WFL_LINENO (cl);
4368 field_decl = add_field (class_type, current_name, real_type, flags);
4369 CHECK_DEPRECATED (field_decl);
4370
4371 /* If the field denotes a final instance variable, then we
4372 allocate a LANG_DECL_SPECIFIC part to keep track of its
4373 initialization. We also mark whether the field was
4374 initialized upon its declaration. We don't do that if the
4375 created field is an alias to a final local. */
4376 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4377 {
4378 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4379 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4380 }
4381
4382 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4383 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4384 hide parameters to this inner class finit$ and
4385 constructors. It also means that the field isn't final per
4386 say. */
4387 if (ARG_FINAL_P (current))
4388 {
4389 FIELD_LOCAL_ALIAS (field_decl) = 1;
4390 FIELD_FINAL (field_decl) = 0;
4391 }
4392
4393 /* Check if we must chain. */
4394 if (must_chain)
4395 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4396
4397 /* If we have an initialization value tied to the field */
4398 if (init)
4399 {
4400 /* The field is declared static */
4401 if (flags & ACC_STATIC)
4402 {
4403 /* We include the field and its initialization part into
4404 a list used to generate <clinit>. After <clinit> is
4405 walked, field initializations will be processed and
4406 fields initialized with known constants will be taken
4407 out of <clinit> and have their DECL_INITIAL set
4408 appropriately. */
4409 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4410 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4411 if (TREE_OPERAND (init, 1)
4412 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4413 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4414 }
4415 /* A non-static field declared with an immediate initialization is
4416 to be initialized in <init>, if any. This field is remembered
4417 to be processed at the time of the generation of <init>. */
4418 else
4419 {
4420 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4421 SET_CPC_INITIALIZER_STMT (ctxp, init);
4422 }
4423 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4424 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4425 }
4426 }
4427 lineno = saved_lineno;
4428 }
4429
4430 /* Generate finit$, using the list of initialized fields to populate
4431 its body. finit$'s parameter(s) list is adjusted to include the
4432 one(s) used to initialized the field(s) caching outer context
4433 local(s). */
4434
4435 static tree
4436 generate_finit (class_type)
4437 tree class_type;
4438 {
4439 int count = 0;
4440 tree list = TYPE_FINIT_STMT_LIST (class_type);
4441 tree mdecl, current, parms;
4442
4443 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4444 class_type, NULL_TREE,
4445 &count);
4446 CRAFTED_PARAM_LIST_FIXUP (parms);
4447 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4448 finit_identifier_node, parms);
4449 fix_method_argument_names (parms, mdecl);
4450 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4451 mdecl, NULL_TREE);
4452 DECL_FUNCTION_NAP (mdecl) = count;
4453 start_artificial_method_body (mdecl);
4454
4455 for (current = list; current; current = TREE_CHAIN (current))
4456 java_method_add_stmt (mdecl,
4457 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4458 current));
4459 end_artificial_method_body (mdecl);
4460 return mdecl;
4461 }
4462
4463 /* Generate a function to run the instance initialization code. The
4464 private method is called `instinit$'. Unless we're dealing with an
4465 anonymous class, we determine whether all ctors of CLASS_TYPE
4466 declare a checked exception in their `throws' clause in order to
4467 see whether it's necessary to encapsulate the instance initializer
4468 statements in a try/catch/rethrow sequence. */
4469
4470 static tree
4471 generate_instinit (class_type)
4472 tree class_type;
4473 {
4474 tree current;
4475 tree compound = NULL_TREE;
4476 tree parms = tree_cons (this_identifier_node,
4477 build_pointer_type (class_type), end_params_node);
4478 tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4479 void_type_node,
4480 instinit_identifier_node, parms);
4481
4482 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4483 mdecl, NULL_TREE);
4484
4485 /* Gather all the statements in a compound */
4486 for (current = TYPE_II_STMT_LIST (class_type);
4487 current; current = TREE_CHAIN (current))
4488 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4489
4490 /* We need to encapsulate COMPOUND by a try/catch statement to
4491 rethrow exceptions that might occur in the instance initializer.
4492 We do that only if all ctors of CLASS_TYPE are set to catch a
4493 checked exception. This doesn't apply to anonymous classes (since
4494 they don't have declared ctors.) */
4495 if (!ANONYMOUS_CLASS_P (class_type) &&
4496 ctors_unchecked_throws_clause_p (class_type))
4497 {
4498 compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4499 build1 (THROW_EXPR, NULL_TREE,
4500 build_wfl_node (wpv_id)));
4501 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4502 exception_type_node);
4503 }
4504
4505 start_artificial_method_body (mdecl);
4506 java_method_add_stmt (mdecl, compound);
4507 end_artificial_method_body (mdecl);
4508
4509 return mdecl;
4510 }
4511
4512 /* FIXME */
4513 static tree
4514 build_instinit_invocation (class_type)
4515 tree class_type;
4516 {
4517 tree to_return = NULL_TREE;
4518
4519 if (TYPE_II_STMT_LIST (class_type))
4520 {
4521 tree parm = build_tree_list (NULL_TREE,
4522 build_wfl_node (this_identifier_node));
4523 to_return =
4524 build_method_invocation (build_wfl_node (instinit_identifier_node),
4525 parm);
4526 }
4527 return to_return;
4528 }
4529
4530 /* Shared accros method_declarator and method_header to remember the
4531 patch stage that was reached during the declaration of the method.
4532 A method DECL is built differently is there is no patch
4533 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4534 pending on the currently defined method. */
4535
4536 static int patch_stage;
4537
4538 /* Check the method declaration and add the method to its current
4539 class. If the argument list is known to contain incomplete types,
4540 the method is partially added and the registration will be resume
4541 once the method arguments resolved. If TYPE is NULL, we're dealing
4542 with a constructor. */
4543
4544 static tree
4545 method_header (flags, type, mdecl, throws)
4546 int flags;
4547 tree type, mdecl, throws;
4548 {
4549 tree type_wfl = NULL_TREE;
4550 tree meth_name = NULL_TREE;
4551 tree current, orig_arg, this_class = NULL;
4552 tree id, meth;
4553 int saved_lineno;
4554 int constructor_ok = 0, must_chain;
4555 int count;
4556
4557 if (mdecl == error_mark_node)
4558 return error_mark_node;
4559 meth = TREE_VALUE (mdecl);
4560 id = TREE_PURPOSE (mdecl);
4561
4562 check_modifiers_consistency (flags);
4563
4564 if (GET_CPC ())
4565 this_class = TREE_TYPE (GET_CPC ());
4566
4567 if (!this_class || this_class == error_mark_node)
4568 return NULL_TREE;
4569
4570 /* There are some forbidden modifiers for an abstract method and its
4571 class must be abstract as well. */
4572 if (type && (flags & ACC_ABSTRACT))
4573 {
4574 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4575 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4576 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4577 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4578 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED, id, "Synchronized");
4579 ABSTRACT_CHECK (flags, ACC_STRICT, id, "Strictfp");
4580 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4581 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4582 parse_error_context
4583 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4584 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4585 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4586 }
4587
4588 /* Things to be checked when declaring a constructor */
4589 if (!type)
4590 {
4591 int ec = java_error_count;
4592 /* 8.6: Constructor declarations: we might be trying to define a
4593 method without specifying a return type. */
4594 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4595 parse_error_context
4596 (id, "Invalid method declaration, return type required");
4597 /* 8.6.3: Constructor modifiers */
4598 else
4599 {
4600 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4601 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4602 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4603 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4604 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4605 JCONSTRUCTOR_CHECK (flags, ACC_STRICT, id, "strictfp");
4606 }
4607 /* If we found error here, we don't consider it's OK to tread
4608 the method definition as a constructor, for the rest of this
4609 function */
4610 if (ec == java_error_count)
4611 constructor_ok = 1;
4612 }
4613
4614 /* Method declared within the scope of an interface are implicitly
4615 abstract and public. Conflicts with other erroneously provided
4616 modifiers are checked right after. */
4617
4618 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4619 {
4620 /* If FLAGS isn't set because of a modifier, turn the
4621 corresponding modifier WFL to NULL so we issue a warning on
4622 the obsolete use of the modifier */
4623 if (!(flags & ACC_PUBLIC))
4624 MODIFIER_WFL (PUBLIC_TK) = NULL;
4625 if (!(flags & ACC_ABSTRACT))
4626 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4627 flags |= ACC_PUBLIC;
4628 flags |= ACC_ABSTRACT;
4629 }
4630
4631 /* Inner class can't declare static methods */
4632 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4633 {
4634 parse_error_context
4635 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4636 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4637 lang_printable_name (this_class, 0));
4638 }
4639
4640 /* Modifiers context reset moved up, so abstract method declaration
4641 modifiers can be later checked. */
4642
4643 /* Set constructor returned type to void and method name to <init>,
4644 unless we found an error identifier the constructor (in which
4645 case we retain the original name) */
4646 if (!type)
4647 {
4648 type = void_type_node;
4649 if (constructor_ok)
4650 meth_name = init_identifier_node;
4651 }
4652 else
4653 meth_name = EXPR_WFL_NODE (id);
4654
4655 /* Do the returned type resolution and registration if necessary */
4656 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4657
4658 if (meth_name)
4659 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4660 EXPR_WFL_NODE (id) = meth_name;
4661 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4662
4663 if (must_chain)
4664 {
4665 patch_stage = JDEP_METHOD_RETURN;
4666 register_incomplete_type (patch_stage, type_wfl, id, type);
4667 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4668 }
4669 else
4670 TREE_TYPE (meth) = type;
4671
4672 saved_lineno = lineno;
4673 /* When defining an abstract or interface method, the curly
4674 bracket at level 1 doesn't exist because there is no function
4675 body */
4676 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4677 EXPR_WFL_LINENO (id));
4678
4679 /* Remember the original argument list */
4680 orig_arg = TYPE_ARG_TYPES (meth);
4681
4682 if (patch_stage) /* includes ret type and/or all args */
4683 {
4684 jdep *jdep;
4685 meth = add_method_1 (this_class, flags, meth_name, meth);
4686 /* Patch for the return type */
4687 if (patch_stage == JDEP_METHOD_RETURN)
4688 {
4689 jdep = CLASSD_LAST (ctxp->classd_list);
4690 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4691 }
4692 /* This is the stop JDEP. METH allows the function's signature
4693 to be computed. */
4694 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4695 }
4696 else
4697 meth = add_method (this_class, flags, meth_name,
4698 build_java_signature (meth));
4699
4700 /* Remember final parameters */
4701 MARK_FINAL_PARMS (meth, orig_arg);
4702
4703 /* Fix the method argument list so we have the argument name
4704 information */
4705 fix_method_argument_names (orig_arg, meth);
4706
4707 /* Register the parameter number and re-install the current line
4708 number */
4709 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4710 lineno = saved_lineno;
4711
4712 /* Register exception specified by the `throws' keyword for
4713 resolution and set the method decl appropriate field to the list.
4714 Note: the grammar ensures that what we get here are class
4715 types. */
4716 if (throws)
4717 {
4718 throws = nreverse (throws);
4719 for (current = throws; current; current = TREE_CHAIN (current))
4720 {
4721 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4722 NULL_TREE, NULL_TREE);
4723 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4724 &TREE_VALUE (current);
4725 }
4726 DECL_FUNCTION_THROWS (meth) = throws;
4727 }
4728
4729 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4730 DECL_FUNCTION_WFL (meth) = id;
4731
4732 /* Set the flag if we correctly processed a constructor */
4733 if (constructor_ok)
4734 {
4735 DECL_CONSTRUCTOR_P (meth) = 1;
4736 /* Compute and store the number of artificial parameters declared
4737 for this constructor */
4738 for (count = 0, current = TYPE_FIELDS (this_class); current;
4739 current = TREE_CHAIN (current))
4740 if (FIELD_LOCAL_ALIAS (current))
4741 count++;
4742 DECL_FUNCTION_NAP (meth) = count;
4743 }
4744
4745 /* Eventually set the @deprecated tag flag */
4746 CHECK_DEPRECATED (meth);
4747
4748 /* If doing xref, store column and line number information instead
4749 of the line number only. */
4750 if (flag_emit_xref)
4751 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4752
4753 return meth;
4754 }
4755
4756 static void
4757 fix_method_argument_names (orig_arg, meth)
4758 tree orig_arg, meth;
4759 {
4760 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4761 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4762 {
4763 TREE_PURPOSE (arg) = this_identifier_node;
4764 arg = TREE_CHAIN (arg);
4765 }
4766 while (orig_arg != end_params_node)
4767 {
4768 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4769 orig_arg = TREE_CHAIN (orig_arg);
4770 arg = TREE_CHAIN (arg);
4771 }
4772 }
4773
4774 /* Complete the method declaration with METHOD_BODY. */
4775
4776 static void
4777 finish_method_declaration (method_body)
4778 tree method_body;
4779 {
4780 int flags;
4781
4782 if (!current_function_decl)
4783 return;
4784
4785 flags = get_access_flags_from_decl (current_function_decl);
4786
4787 /* 8.4.5 Method Body */
4788 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4789 {
4790 tree name = DECL_NAME (current_function_decl);
4791 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4792 "%s method `%s' can't have a body defined",
4793 (METHOD_NATIVE (current_function_decl) ?
4794 "Native" : "Abstract"),
4795 IDENTIFIER_POINTER (name));
4796 method_body = NULL_TREE;
4797 }
4798 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4799 {
4800 tree name = DECL_NAME (current_function_decl);
4801 parse_error_context
4802 (DECL_FUNCTION_WFL (current_function_decl),
4803 "Non native and non abstract method `%s' must have a body defined",
4804 IDENTIFIER_POINTER (name));
4805 method_body = NULL_TREE;
4806 }
4807
4808 if (flag_emit_class_files && method_body
4809 && TREE_CODE (method_body) == NOP_EXPR
4810 && TREE_TYPE (current_function_decl)
4811 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4812 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4813
4814 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4815 maybe_absorb_scoping_blocks ();
4816 /* Exit function's body */
4817 exit_block ();
4818 /* Merge last line of the function with first line, directly in the
4819 function decl. It will be used to emit correct debug info. */
4820 if (!flag_emit_xref)
4821 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4822
4823 /* Since function's argument's list are shared, reset the
4824 ARG_FINAL_P parameter that might have been set on some of this
4825 function parameters. */
4826 UNMARK_FINAL_PARMS (current_function_decl);
4827
4828 /* So we don't have an irrelevant function declaration context for
4829 the next static block we'll see. */
4830 current_function_decl = NULL_TREE;
4831 }
4832
4833 /* Build a an error message for constructor circularity errors. */
4834
4835 static char *
4836 constructor_circularity_msg (from, to)
4837 tree from, to;
4838 {
4839 static char string [4096];
4840 char *t = xstrdup (lang_printable_name (from, 0));
4841 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4842 free (t);
4843 return string;
4844 }
4845
4846 /* Verify a circular call to METH. Return 1 if an error is found, 0
4847 otherwise. */
4848
4849 static GTY(()) tree vcc_list;
4850 static int
4851 verify_constructor_circularity (meth, current)
4852 tree meth, current;
4853 {
4854 tree c;
4855
4856 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4857 {
4858 if (TREE_VALUE (c) == meth)
4859 {
4860 char *t;
4861 if (vcc_list)
4862 {
4863 tree liste;
4864 vcc_list = nreverse (vcc_list);
4865 for (liste = vcc_list; liste; liste = TREE_CHAIN (liste))
4866 {
4867 parse_error_context
4868 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4869 constructor_circularity_msg
4870 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4871 java_error_count--;
4872 }
4873 }
4874 t = xstrdup (lang_printable_name (meth, 0));
4875 parse_error_context (TREE_PURPOSE (c),
4876 "%s: recursive invocation of constructor `%s'",
4877 constructor_circularity_msg (current, meth), t);
4878 free (t);
4879 vcc_list = NULL_TREE;
4880 return 1;
4881 }
4882 }
4883 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4884 {
4885 vcc_list = tree_cons (c, current, vcc_list);
4886 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4887 return 1;
4888 vcc_list = TREE_CHAIN (vcc_list);
4889 }
4890 return 0;
4891 }
4892
4893 /* Check modifiers that can be declared but exclusively */
4894
4895 static void
4896 check_modifiers_consistency (flags)
4897 int flags;
4898 {
4899 int acc_count = 0;
4900 tree cl = NULL_TREE;
4901
4902 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4903 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4904 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4905 if (acc_count > 1)
4906 parse_error_context
4907 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4908
4909 acc_count = 0;
4910 cl = NULL_TREE;
4911 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4912 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4913 if (acc_count > 1)
4914 parse_error_context (cl,
4915 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
4916 }
4917
4918 /* Check the methode header METH for abstract specifics features */
4919
4920 static void
4921 check_abstract_method_header (meth)
4922 tree meth;
4923 {
4924 int flags = get_access_flags_from_decl (meth);
4925
4926 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4927 ACC_ABSTRACT, "abstract method",
4928 IDENTIFIER_POINTER (DECL_NAME (meth)));
4929 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4930 ACC_PUBLIC, "abstract method",
4931 IDENTIFIER_POINTER (DECL_NAME (meth)));
4932
4933 check_modifiers ("Illegal modifier `%s' for interface method",
4934 flags, INTERFACE_METHOD_MODIFIERS);
4935 }
4936
4937 /* Create a FUNCTION_TYPE node and start augmenting it with the
4938 declared function arguments. Arguments type that can't be resolved
4939 are left as they are, but the returned node is marked as containing
4940 incomplete types. */
4941
4942 static tree
4943 method_declarator (id, list)
4944 tree id, list;
4945 {
4946 tree arg_types = NULL_TREE, current, node;
4947 tree meth = make_node (FUNCTION_TYPE);
4948 jdep *jdep;
4949
4950 patch_stage = JDEP_NO_PATCH;
4951
4952 if (GET_CPC () == error_mark_node)
4953 return error_mark_node;
4954
4955 /* If we're dealing with an inner class constructor, we hide the
4956 this$<n> decl in the name field of its parameter declaration. We
4957 also might have to hide the outer context local alias
4958 initializers. Not done when the class is a toplevel class. */
4959 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4960 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4961 {
4962 tree aliases_list, type, thisn;
4963 /* First the aliases, linked to the regular parameters */
4964 aliases_list =
4965 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4966 TREE_TYPE (GET_CPC ()),
4967 NULL_TREE, NULL);
4968 list = chainon (nreverse (aliases_list), list);
4969
4970 /* Then this$<n> */
4971 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4972 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4973 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4974 list);
4975 }
4976
4977 for (current = list; current; current = TREE_CHAIN (current))
4978 {
4979 int must_chain = 0;
4980 tree wfl_name = TREE_PURPOSE (current);
4981 tree type = TREE_VALUE (current);
4982 tree name = EXPR_WFL_NODE (wfl_name);
4983 tree already, arg_node;
4984 tree type_wfl = NULL_TREE;
4985 tree real_type;
4986
4987 /* Obtain a suitable type for resolution, if necessary */
4988 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4989
4990 /* Process NAME, as it may specify extra dimension(s) for it */
4991 type = build_array_from_name (type, type_wfl, name, &name);
4992 EXPR_WFL_NODE (wfl_name) = name;
4993
4994 real_type = GET_REAL_TYPE (type);
4995 if (TREE_CODE (real_type) == RECORD_TYPE)
4996 {
4997 real_type = promote_type (real_type);
4998 if (TREE_CODE (type) == TREE_LIST)
4999 TREE_PURPOSE (type) = real_type;
5000 }
5001
5002 /* Check redefinition */
5003 for (already = arg_types; already; already = TREE_CHAIN (already))
5004 if (TREE_PURPOSE (already) == name)
5005 {
5006 parse_error_context
5007 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
5008 IDENTIFIER_POINTER (name),
5009 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
5010 break;
5011 }
5012
5013 /* If we've an incomplete argument type, we know there is a location
5014 to patch when the type get resolved, later. */
5015 jdep = NULL;
5016 if (must_chain)
5017 {
5018 patch_stage = JDEP_METHOD;
5019 type = register_incomplete_type (patch_stage,
5020 type_wfl, wfl_name, type);
5021 jdep = CLASSD_LAST (ctxp->classd_list);
5022 JDEP_MISC (jdep) = id;
5023 }
5024
5025 /* The argument node: a name and a (possibly) incomplete type. */
5026 arg_node = build_tree_list (name, real_type);
5027 /* Remeber arguments declared final. */
5028 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5029
5030 if (jdep)
5031 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5032 TREE_CHAIN (arg_node) = arg_types;
5033 arg_types = arg_node;
5034 }
5035 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5036 node = build_tree_list (id, meth);
5037 return node;
5038 }
5039
5040 static int
5041 unresolved_type_p (wfl, returned)
5042 tree wfl;
5043 tree *returned;
5044
5045 {
5046 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5047 {
5048 if (returned)
5049 {
5050 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5051 if (decl && current_class && (decl == TYPE_NAME (current_class)))
5052 *returned = TREE_TYPE (decl);
5053 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5054 *returned = TREE_TYPE (GET_CPC ());
5055 else
5056 *returned = NULL_TREE;
5057 }
5058 return 1;
5059 }
5060 if (returned)
5061 *returned = wfl;
5062 return 0;
5063 }
5064
5065 /* From NAME, build a qualified identifier node using the
5066 qualification from the current package definition. */
5067
5068 static tree
5069 parser_qualified_classname (name)
5070 tree name;
5071 {
5072 tree nested_class_name;
5073
5074 if ((nested_class_name = maybe_make_nested_class_name (name)))
5075 return nested_class_name;
5076
5077 if (ctxp->package)
5078 return merge_qualified_name (ctxp->package, name);
5079 else
5080 return name;
5081 }
5082
5083 /* Called once the type a interface extends is resolved. Returns 0 if
5084 everything is OK. */
5085
5086 static int
5087 parser_check_super_interface (super_decl, this_decl, this_wfl)
5088 tree super_decl, this_decl, this_wfl;
5089 {
5090 tree super_type = TREE_TYPE (super_decl);
5091
5092 /* Has to be an interface */
5093 if (!CLASS_INTERFACE (super_decl))
5094 {
5095 parse_error_context
5096 (this_wfl, "%s `%s' can't implement/extend %s `%s'",
5097 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5098 "Interface" : "Class"),
5099 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5100 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5101 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5102 return 1;
5103 }
5104
5105 /* Check top-level interface access. Inner classes are subject to member
5106 access rules (6.6.1). */
5107 if (! INNER_CLASS_P (super_type)
5108 && check_pkg_class_access (DECL_NAME (super_decl),
5109 lookup_cl (this_decl), true))
5110 return 1;
5111
5112 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5113 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5114 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5115 return 0;
5116 }
5117
5118 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5119 0 if everthing is OK. */
5120
5121 static int
5122 parser_check_super (super_decl, this_decl, wfl)
5123 tree super_decl, this_decl, wfl;
5124 {
5125 tree super_type = TREE_TYPE (super_decl);
5126
5127 /* SUPER should be a CLASS (neither an array nor an interface) */
5128 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5129 {
5130 parse_error_context
5131 (wfl, "Class `%s' can't subclass %s `%s'",
5132 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5133 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5134 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5135 return 1;
5136 }
5137
5138 if (CLASS_FINAL (TYPE_NAME (super_type)))
5139 {
5140 parse_error_context (wfl, "Can't subclass final classes: %s",
5141 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5142 return 1;
5143 }
5144
5145 /* Check top-level class scope. Inner classes are subject to member access
5146 rules (6.6.1). */
5147 if (! INNER_CLASS_P (super_type)
5148 && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true)))
5149 return 1;
5150
5151 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5152 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5153 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5154 return 0;
5155 }
5156
5157 /* Create a new dependency list and link it (in a LIFO manner) to the
5158 CTXP list of type dependency list. */
5159
5160 static void
5161 create_jdep_list (ctxp)
5162 struct parser_ctxt *ctxp;
5163 {
5164 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
5165 new->first = new->last = NULL;
5166 new->next = ctxp->classd_list;
5167 ctxp->classd_list = new;
5168 }
5169
5170 static jdeplist *
5171 reverse_jdep_list (ctxp)
5172 struct parser_ctxt *ctxp;
5173 {
5174 register jdeplist *prev = NULL, *current, *next;
5175 for (current = ctxp->classd_list; current; current = next)
5176 {
5177 next = current->next;
5178 current->next = prev;
5179 prev = current;
5180 }
5181 return prev;
5182 }
5183
5184 /* Create a fake pointer based on the ID stored in
5185 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5186 registered again. */
5187
5188 static tree
5189 obtain_incomplete_type (type_name)
5190 tree type_name;
5191 {
5192 tree ptr = NULL_TREE, name;
5193
5194 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5195 name = EXPR_WFL_NODE (type_name);
5196 else if (INCOMPLETE_TYPE_P (type_name))
5197 name = TYPE_NAME (type_name);
5198 else
5199 abort ();
5200
5201 /* Workaround from build_pointer_type for incomplete types. */
5202 BUILD_PTR_FROM_NAME (ptr, name);
5203 TYPE_MODE (ptr) = ptr_mode;
5204 layout_type (ptr);
5205
5206 return ptr;
5207 }
5208
5209 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5210 non NULL instead of computing a new fake type based on WFL. The new
5211 dependency is inserted in the current type dependency list, in FIFO
5212 manner. */
5213
5214 static tree
5215 register_incomplete_type (kind, wfl, decl, ptr)
5216 int kind;
5217 tree wfl, decl, ptr;
5218 {
5219 jdep *new = (jdep *)xmalloc (sizeof (jdep));
5220
5221 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5222 ptr = obtain_incomplete_type (wfl);
5223
5224 JDEP_KIND (new) = kind;
5225 JDEP_DECL (new) = decl;
5226 JDEP_TO_RESOLVE (new) = ptr;
5227 JDEP_WFL (new) = wfl;
5228 JDEP_CHAIN (new) = NULL;
5229 JDEP_MISC (new) = NULL_TREE;
5230 /* For some dependencies, set the enclosing class of the current
5231 class to be the enclosing context */
5232 if ((kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS)
5233 && GET_ENCLOSING_CPC ())
5234 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5235 else if (kind == JDEP_SUPER)
5236 JDEP_ENCLOSING (new) = (GET_ENCLOSING_CPC () ?
5237 TREE_VALUE (GET_ENCLOSING_CPC ()) : NULL_TREE);
5238 else
5239 JDEP_ENCLOSING (new) = GET_CPC ();
5240 JDEP_GET_PATCH (new) = (tree *)NULL;
5241
5242 JDEP_INSERT (ctxp->classd_list, new);
5243
5244 return ptr;
5245 }
5246
5247 /* This checks for circular references with innerclasses. We start
5248 from SOURCE and should never reach TARGET. Extended/implemented
5249 types in SOURCE have their enclosing context checked not to reach
5250 TARGET. When the last enclosing context of SOURCE is reached, its
5251 extended/implemented types are also checked not to reach TARGET.
5252 In case of error, WFL of the offending type is returned; NULL_TREE
5253 otherwise. */
5254
5255 static tree
5256 check_inner_circular_reference (source, target)
5257 tree source;
5258 tree target;
5259 {
5260 tree basetype_vec = TYPE_BINFO_BASETYPES (source);
5261 tree ctx, cl;
5262 int i;
5263
5264 if (!basetype_vec)
5265 return NULL_TREE;
5266
5267 for (i = 0; i < TREE_VEC_LENGTH (basetype_vec); i++)
5268 {
5269 tree su;
5270
5271 /* We can end up with a NULL_TREE or an incomplete type here if
5272 we encountered previous type resolution errors. It's safe to
5273 simply ignore these cases. */
5274 if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
5275 continue;
5276 su = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
5277 if (INCOMPLETE_TYPE_P (su))
5278 continue;
5279
5280 if (inherits_from_p (su, target))
5281 return lookup_cl (TYPE_NAME (su));
5282
5283 for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5284 {
5285 /* An enclosing context shouldn't be TARGET */
5286 if (ctx == TYPE_NAME (target))
5287 return lookup_cl (TYPE_NAME (su));
5288
5289 /* When we reach the enclosing last context, start a check
5290 on it, with the same target */
5291 if (! DECL_CONTEXT (ctx) &&
5292 (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5293 return cl;
5294 }
5295 }
5296 return NULL_TREE;
5297 }
5298
5299 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5300 offending type if a circularity is detected. NULL_TREE is returned
5301 otherwise. TYPE can be an interface or a class. */
5302
5303 static tree
5304 check_circular_reference (type)
5305 tree type;
5306 {
5307 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5308 int i;
5309
5310 if (!basetype_vec)
5311 return NULL_TREE;
5312
5313 if (! CLASS_INTERFACE (TYPE_NAME (type)))
5314 {
5315 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5316 return lookup_cl (TYPE_NAME (type));
5317 return NULL_TREE;
5318 }
5319
5320 for (i = 0; i < TREE_VEC_LENGTH (basetype_vec); i++)
5321 {
5322 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5323 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5324 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5325 return lookup_cl (TYPE_NAME (BINFO_TYPE (vec_elt)));
5326 }
5327 return NULL_TREE;
5328 }
5329
5330 void
5331 java_check_circular_reference ()
5332 {
5333 tree current;
5334 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5335 {
5336 tree type = TREE_TYPE (current);
5337 tree cl;
5338
5339 cl = check_circular_reference (type);
5340 if (! cl)
5341 cl = check_inner_circular_reference (type, type);
5342 if (cl)
5343 parse_error_context (cl, "Cyclic class inheritance%s",
5344 (cyclic_inheritance_report ?
5345 cyclic_inheritance_report : ""));
5346 }
5347 }
5348
5349 /* Augment the parameter list PARM with parameters crafted to
5350 initialize outer context locals aliases. Through ARTIFICIAL, a
5351 count is kept of the number of crafted parameters. MODE governs
5352 what eventually gets created: something suitable for a function
5353 creation or a function invocation, either the constructor or
5354 finit$. */
5355
5356 static tree
5357 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5358 int mode;
5359 tree class_type, parm;
5360 int *artificial;
5361 {
5362 tree field;
5363 tree additional_parms = NULL_TREE;
5364
5365 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5366 if (FIELD_LOCAL_ALIAS (field))
5367 {
5368 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5369 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5370 tree mangled_id;
5371
5372 switch (mode)
5373 {
5374 case AIPL_FUNCTION_DECLARATION:
5375 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5376 &buffer [4]);
5377 purpose = build_wfl_node (mangled_id);
5378 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5379 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5380 else
5381 value = TREE_TYPE (field);
5382 break;
5383
5384 case AIPL_FUNCTION_CREATION:
5385 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5386 &buffer [4]);
5387 value = TREE_TYPE (field);
5388 break;
5389
5390 case AIPL_FUNCTION_FINIT_INVOCATION:
5391 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5392 &buffer [4]);
5393 /* Now, this is wrong. purpose should always be the NAME
5394 of something and value its matching value (decl, type,
5395 etc...) FIXME -- but there is a lot to fix. */
5396
5397 /* When invoked for this kind of operation, we already
5398 know whether a field is used or not. */
5399 purpose = TREE_TYPE (field);
5400 value = build_wfl_node (mangled_id);
5401 break;
5402
5403 case AIPL_FUNCTION_CTOR_INVOCATION:
5404 /* There are two case: the constructor invokation happends
5405 outside the local inner, in which case, locales from the outer
5406 context are directly used.
5407
5408 Otherwise, we fold to using the alias directly. */
5409 if (class_type == current_class)
5410 value = field;
5411 else
5412 {
5413 name = get_identifier (&buffer[4]);
5414 value = IDENTIFIER_LOCAL_VALUE (name);
5415 }
5416 break;
5417 }
5418 additional_parms = tree_cons (purpose, value, additional_parms);
5419 if (artificial)
5420 *artificial +=1;
5421 }
5422 if (additional_parms)
5423 {
5424 if (ANONYMOUS_CLASS_P (class_type)
5425 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5426 additional_parms = nreverse (additional_parms);
5427 parm = chainon (additional_parms, parm);
5428 }
5429
5430 return parm;
5431 }
5432
5433 /* Craft a constructor for CLASS_DECL -- what we should do when none
5434 where found. ARGS is non NULL when a special signature must be
5435 enforced. This is the case for anonymous classes. */
5436
5437 static tree
5438 craft_constructor (class_decl, args)
5439 tree class_decl, args;
5440 {
5441 tree class_type = TREE_TYPE (class_decl);
5442 tree parm = NULL_TREE;
5443 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5444 ACC_PUBLIC : 0);
5445 int i = 0, artificial = 0;
5446 tree decl, ctor_name;
5447 char buffer [80];
5448
5449 /* The constructor name is <init> unless we're dealing with an
5450 anonymous class, in which case the name will be fixed after having
5451 be expanded. */
5452 if (ANONYMOUS_CLASS_P (class_type))
5453 ctor_name = DECL_NAME (class_decl);
5454 else
5455 ctor_name = init_identifier_node;
5456
5457 /* If we're dealing with an inner class constructor, we hide the
5458 this$<n> decl in the name field of its parameter declaration. */
5459 if (PURE_INNER_CLASS_TYPE_P (class_type))
5460 {
5461 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5462 parm = tree_cons (build_current_thisn (class_type),
5463 build_pointer_type (type), parm);
5464
5465 /* Some more arguments to be hidden here. The values of the local
5466 variables of the outer context that the inner class needs to see. */
5467 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5468 class_type, parm,
5469 &artificial);
5470 }
5471
5472 /* Then if there are any args to be enforced, enforce them now */
5473 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5474 {
5475 sprintf (buffer, "parm%d", i++);
5476 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5477 }
5478
5479 CRAFTED_PARAM_LIST_FIXUP (parm);
5480 decl = create_artificial_method (class_type, flags, void_type_node,
5481 ctor_name, parm);
5482 fix_method_argument_names (parm, decl);
5483 /* Now, mark the artificial parameters. */
5484 DECL_FUNCTION_NAP (decl) = artificial;
5485 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5486 return decl;
5487 }
5488
5489
5490 /* Fix the constructors. This will be called right after circular
5491 references have been checked. It is necessary to fix constructors
5492 early even if no code generation will take place for that class:
5493 some generated constructor might be required by the class whose
5494 compilation triggered this one to be simply loaded. */
5495
5496 void
5497 java_fix_constructors ()
5498 {
5499 tree current;
5500
5501 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5502 {
5503 tree class_type = TREE_TYPE (current);
5504 int saw_ctor = 0;
5505 tree decl;
5506
5507 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5508 continue;
5509
5510 current_class = class_type;
5511 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5512 {
5513 if (DECL_CONSTRUCTOR_P (decl))
5514 {
5515 fix_constructors (decl);
5516 saw_ctor = 1;
5517 }
5518 }
5519
5520 /* Anonymous class constructor can't be generated that early. */
5521 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5522 craft_constructor (current, NULL_TREE);
5523 }
5524 }
5525
5526 /* safe_layout_class just makes sure that we can load a class without
5527 disrupting the current_class, input_file, lineno, etc, information
5528 about the class processed currently. */
5529
5530 void
5531 safe_layout_class (class)
5532 tree class;
5533 {
5534 tree save_current_class = current_class;
5535 const char *save_input_filename = input_filename;
5536 int save_lineno = lineno;
5537
5538 layout_class (class);
5539
5540 current_class = save_current_class;
5541 input_filename = save_input_filename;
5542 lineno = save_lineno;
5543 }
5544
5545 static tree
5546 jdep_resolve_class (dep)
5547 jdep *dep;
5548 {
5549 tree decl;
5550
5551 if (JDEP_RESOLVED_P (dep))
5552 decl = JDEP_RESOLVED_DECL (dep);
5553 else
5554 {
5555 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5556 JDEP_DECL (dep), JDEP_WFL (dep));
5557 JDEP_RESOLVED (dep, decl);
5558 }
5559
5560 if (!decl)
5561 complete_class_report_errors (dep);
5562 else if (PURE_INNER_CLASS_DECL_P (decl))
5563 {
5564 tree inner = TREE_TYPE (decl);
5565 if (! CLASS_LOADED_P (inner))
5566 {
5567 safe_layout_class (inner);
5568 if (TYPE_SIZE (inner) == error_mark_node)
5569 TYPE_SIZE (inner) = NULL_TREE;
5570 }
5571 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5572 }
5573 return decl;
5574 }
5575
5576 /* Complete unsatisfied class declaration and their dependencies */
5577
5578 void
5579 java_complete_class ()
5580 {
5581 tree cclass;
5582 jdeplist *cclassd;
5583 int error_found;
5584 tree type;
5585
5586 /* Process imports */
5587 process_imports ();
5588
5589 /* Reverse things so we have the right order */
5590 ctxp->class_list = nreverse (ctxp->class_list);
5591 ctxp->classd_list = reverse_jdep_list (ctxp);
5592
5593 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5594 cclass && cclassd;
5595 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5596 {
5597 jdep *dep;
5598
5599 /* We keep the compilation unit imports in the class so that
5600 they can be used later to resolve type dependencies that
5601 aren't necessary to solve now. */
5602 TYPE_IMPORT_LIST (TREE_TYPE (cclass)) = ctxp->import_list;
5603 TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (cclass)) = ctxp->import_demand_list;
5604
5605 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5606 {
5607 tree decl;
5608 if (!(decl = jdep_resolve_class (dep)))
5609 continue;
5610
5611 /* Now it's time to patch */
5612 switch (JDEP_KIND (dep))
5613 {
5614 case JDEP_SUPER:
5615 /* Simply patch super */
5616 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5617 continue;
5618 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5619 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5620 break;
5621
5622 case JDEP_FIELD:
5623 {
5624 /* We do part of the job done in add_field */
5625 tree field_decl = JDEP_DECL (dep);
5626 tree field_type = TREE_TYPE (decl);
5627 if (TREE_CODE (field_type) == RECORD_TYPE)
5628 field_type = promote_type (field_type);
5629 TREE_TYPE (field_decl) = field_type;
5630 DECL_ALIGN (field_decl) = 0;
5631 DECL_USER_ALIGN (field_decl) = 0;
5632 layout_decl (field_decl, 0);
5633 SOURCE_FRONTEND_DEBUG
5634 (("Completed field/var decl `%s' with `%s'",
5635 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5636 IDENTIFIER_POINTER (DECL_NAME (decl))));
5637 break;
5638 }
5639 case JDEP_METHOD: /* We start patching a method */
5640 case JDEP_METHOD_RETURN:
5641 error_found = 0;
5642 while (1)
5643 {
5644 if (decl)
5645 {
5646 type = TREE_TYPE(decl);
5647 if (TREE_CODE (type) == RECORD_TYPE)
5648 type = promote_type (type);
5649 JDEP_APPLY_PATCH (dep, type);
5650 SOURCE_FRONTEND_DEBUG
5651 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5652 "Completing fct `%s' with ret type `%s'":
5653 "Completing arg `%s' with type `%s'"),
5654 IDENTIFIER_POINTER (EXPR_WFL_NODE
5655 (JDEP_DECL_WFL (dep))),
5656 IDENTIFIER_POINTER (DECL_NAME (decl))));
5657 }
5658 else
5659 error_found = 1;
5660 dep = JDEP_CHAIN (dep);
5661 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5662 break;
5663 else
5664 decl = jdep_resolve_class (dep);
5665 }
5666 if (!error_found)
5667 {
5668 tree mdecl = JDEP_DECL (dep), signature;
5669 /* Recompute and reset the signature, check first that
5670 all types are now defined. If they're not,
5671 don't build the signature. */
5672 if (check_method_types_complete (mdecl))
5673 {
5674 signature = build_java_signature (TREE_TYPE (mdecl));
5675 set_java_signature (TREE_TYPE (mdecl), signature);
5676 }
5677 }
5678 else
5679 continue;
5680 break;
5681
5682 case JDEP_INTERFACE:
5683 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5684 JDEP_WFL (dep)))
5685 continue;
5686 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5687 break;
5688
5689 case JDEP_PARM:
5690 case JDEP_VARIABLE:
5691 type = TREE_TYPE(decl);
5692 if (TREE_CODE (type) == RECORD_TYPE)
5693 type = promote_type (type);
5694 JDEP_APPLY_PATCH (dep, type);
5695 break;
5696
5697 case JDEP_TYPE:
5698 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5699 SOURCE_FRONTEND_DEBUG
5700 (("Completing a random type dependency on a '%s' node",
5701 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5702 break;
5703
5704 case JDEP_EXCEPTION:
5705 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5706 SOURCE_FRONTEND_DEBUG
5707 (("Completing `%s' `throws' argument node",
5708 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5709 break;
5710
5711 case JDEP_ANONYMOUS:
5712 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5713 break;
5714
5715 default:
5716 abort ();
5717 }
5718 }
5719 }
5720 return;
5721 }
5722
5723 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5724 array. */
5725
5726 static tree
5727 resolve_class (enclosing, class_type, decl, cl)
5728 tree enclosing, class_type, decl, cl;
5729 {
5730 tree tname = TYPE_NAME (class_type);
5731 tree resolved_type = TREE_TYPE (class_type);
5732 int array_dims = 0;
5733 tree resolved_type_decl;
5734
5735 if (resolved_type != NULL_TREE)
5736 {
5737 tree resolved_type_decl = TYPE_NAME (resolved_type);
5738 if (resolved_type_decl == NULL_TREE
5739 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5740 {
5741 resolved_type_decl = build_decl (TYPE_DECL,
5742 TYPE_NAME (class_type),
5743 resolved_type);
5744 }
5745 return resolved_type_decl;
5746 }
5747
5748 /* 1- Check to see if we have an array. If true, find what we really
5749 want to resolve */
5750 if ((array_dims = build_type_name_from_array_name (tname,
5751 &TYPE_NAME (class_type))))
5752 WFL_STRIP_BRACKET (cl, cl);
5753
5754 /* 2- Resolve the bare type */
5755 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5756 decl, cl)))
5757 return NULL_TREE;
5758 resolved_type = TREE_TYPE (resolved_type_decl);
5759
5760 /* 3- If we have an array, reconstruct the array down to its nesting */
5761 if (array_dims)
5762 {
5763 for (; array_dims; array_dims--)
5764 resolved_type = build_java_array_type (resolved_type, -1);
5765 resolved_type_decl = TYPE_NAME (resolved_type);
5766 }
5767 TREE_TYPE (class_type) = resolved_type;
5768 return resolved_type_decl;
5769 }
5770
5771 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5772 are used to report error messages. Do not try to replace TYPE_NAME
5773 (class_type) by a variable, since it is changed by
5774 find_in_imports{_on_demand} and (but it doesn't really matter)
5775 qualify_and_find. */
5776
5777 tree
5778 do_resolve_class (enclosing, class_type, decl, cl)
5779 tree enclosing, class_type, decl, cl;
5780 {
5781 tree new_class_decl = NULL_TREE, super = NULL_TREE;
5782 tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5783 tree decl_result;
5784 htab_t circularity_hash;
5785
5786 if (QUALIFIED_P (TYPE_NAME (class_type)))
5787 {
5788 /* If the type name is of the form `Q . Id', then Q is either a
5789 package name or a class name. First we try to find Q as a
5790 class and then treat Id as a member type. If we can't find Q
5791 as a class then we fall through. */
5792 tree q, left, left_type, right;
5793 breakdown_qualified (&left, &right, TYPE_NAME (class_type));
5794 BUILD_PTR_FROM_NAME (left_type, left);
5795 q = do_resolve_class (enclosing, left_type, decl, cl);
5796 if (q)
5797 {
5798 enclosing = q;
5799 saved_enclosing_type = TREE_TYPE (q);
5800 BUILD_PTR_FROM_NAME (class_type, right);
5801 }
5802 }
5803
5804 if (enclosing)
5805 {
5806 /* This hash table is used to register the classes we're going
5807 through when searching the current class as an inner class, in
5808 order to detect circular references. Remember to free it before
5809 returning the section 0- of this function. */
5810 circularity_hash = htab_create (20, htab_hash_pointer, htab_eq_pointer,
5811 NULL);
5812
5813 /* 0- Search in the current class as an inner class.
5814 Maybe some code here should be added to load the class or
5815 something, at least if the class isn't an inner class and ended
5816 being loaded from class file. FIXME. */
5817 while (enclosing)
5818 {
5819 new_class_decl = resolve_inner_class (circularity_hash, cl, &enclosing,
5820 &super, class_type);
5821 if (new_class_decl)
5822 break;
5823
5824 /* If we haven't found anything because SUPER reached Object and
5825 ENCLOSING happens to be an innerclass, try the enclosing context. */
5826 if ((!super || super == object_type_node) &&
5827 enclosing && INNER_CLASS_DECL_P (enclosing))
5828 enclosing = DECL_CONTEXT (enclosing);
5829 else
5830 enclosing = NULL_TREE;
5831 }
5832
5833 htab_delete (circularity_hash);
5834
5835 if (new_class_decl)
5836 return new_class_decl;
5837 }
5838
5839 /* 1- Check for the type in single imports. This will change
5840 TYPE_NAME() if something relevant is found */
5841 find_in_imports (saved_enclosing_type, class_type);
5842
5843 /* 2- And check for the type in the current compilation unit */
5844 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5845 {
5846 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5847 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5848 load_class (TYPE_NAME (class_type), 0);
5849 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5850 }
5851
5852 /* 3- Search according to the current package definition */
5853 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5854 {
5855 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5856 TYPE_NAME (class_type))))
5857 return new_class_decl;
5858 }
5859
5860 /* 4- Check the import on demands. Don't allow bar.baz to be
5861 imported from foo.* */
5862 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5863 if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5864 return NULL_TREE;
5865
5866 /* If found in find_in_imports_on_demand, the type has already been
5867 loaded. */
5868 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5869 return new_class_decl;
5870
5871 /* 5- Try with a name qualified with the package name we've seen so far */
5872 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5873 {
5874 tree package;
5875
5876 /* If there is a current package (ctxp->package), it's the first
5877 element of package_list and we can skip it. */
5878 for (package = (ctxp->package ?
5879 TREE_CHAIN (package_list) : package_list);
5880 package; package = TREE_CHAIN (package))
5881 if ((new_class_decl = qualify_and_find (class_type,
5882 TREE_PURPOSE (package),
5883 TYPE_NAME (class_type))))
5884 return new_class_decl;
5885 }
5886
5887 /* 5- Check another compilation unit that bears the name of type */
5888 load_class (TYPE_NAME (class_type), 0);
5889
5890 if (!cl)
5891 cl = lookup_cl (decl);
5892
5893 /* If we don't have a value for CL, then we're being called recursively.
5894 We can't check package access just yet, but it will be taken care of
5895 by the caller. */
5896 if (cl)
5897 {
5898 if (check_pkg_class_access (TYPE_NAME (class_type), cl, true))
5899 return NULL_TREE;
5900 }
5901
5902 /* 6- Last call for a resolution */
5903 decl_result = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5904
5905 /* The final lookup might have registered a.b.c into a.b$c If we
5906 failed at the first lookup, progressively change the name if
5907 applicable and use the matching DECL instead. */
5908 if (!decl_result && QUALIFIED_P (TYPE_NAME (class_type)))
5909 {
5910 char *separator;
5911 tree name = TYPE_NAME (class_type);
5912 char *namebuffer = alloca (IDENTIFIER_LENGTH (name) + 1);
5913
5914 strcpy (namebuffer, IDENTIFIER_POINTER (name));
5915
5916 do {
5917
5918 /* Reach the last '.', and if applicable, replace it by a `$' and
5919 see if this exists as a type. */
5920 if ((separator = strrchr (namebuffer, '.')))
5921 {
5922 *separator = '$';
5923 name = get_identifier (namebuffer);
5924 decl_result = IDENTIFIER_CLASS_VALUE (name);
5925 }
5926 } while (!decl_result && separator);
5927 }
5928 return decl_result;
5929 }
5930
5931 static tree
5932 qualify_and_find (class_type, package, name)
5933 tree class_type, package, name;
5934 {
5935 tree new_qualified = merge_qualified_name (package, name);
5936 tree new_class_decl;
5937
5938 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5939 load_class (new_qualified, 0);
5940 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5941 {
5942 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5943 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5944 load_class (new_qualified, 0);
5945 TYPE_NAME (class_type) = new_qualified;
5946 return IDENTIFIER_CLASS_VALUE (new_qualified);
5947 }
5948 return NULL_TREE;
5949 }
5950
5951 /* Resolve NAME and lay it out (if not done and if not the current
5952 parsed class). Return a decl node. This function is meant to be
5953 called when type resolution is necessary during the walk pass. */
5954
5955 static tree
5956 resolve_and_layout (something, cl)
5957 tree something;
5958 tree cl;
5959 {
5960 tree decl, decl_type;
5961
5962 /* Don't do that on the current class */
5963 if (something == current_class)
5964 return TYPE_NAME (current_class);
5965
5966 /* Don't do anything for void and other primitive types */
5967 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5968 return NULL_TREE;
5969
5970 /* Pointer types can be reall pointer types or fake pointers. When
5971 finding a real pointer, recheck for primitive types */
5972 if (TREE_CODE (something) == POINTER_TYPE)
5973 {
5974 if (TREE_TYPE (something))
5975 {
5976 something = TREE_TYPE (something);
5977 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5978 return NULL_TREE;
5979 }
5980 else
5981 something = TYPE_NAME (something);
5982 }
5983
5984 /* Don't do anything for arrays of primitive types */
5985 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5986 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5987 return NULL_TREE;
5988
5989 /* Something might be a WFL */
5990 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5991 something = EXPR_WFL_NODE (something);
5992
5993 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5994 TYPE_DECL or a real TYPE */
5995 else if (TREE_CODE (something) != IDENTIFIER_NODE)
5996 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5997 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5998
5999 if (!(decl = resolve_no_layout (something, cl)))
6000 return NULL_TREE;
6001
6002 /* Resolve and layout if necessary */
6003 decl_type = TREE_TYPE (decl);
6004 layout_class_methods (decl_type);
6005 /* Check methods */
6006 if (CLASS_FROM_SOURCE_P (decl_type))
6007 java_check_methods (decl);
6008 /* Layout the type if necessary */
6009 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
6010 safe_layout_class (decl_type);
6011
6012 return decl;
6013 }
6014
6015 /* Resolve a class, returns its decl but doesn't perform any
6016 layout. The current parsing context is saved and restored */
6017
6018 static tree
6019 resolve_no_layout (name, cl)
6020 tree name, cl;
6021 {
6022 tree ptr, decl;
6023 BUILD_PTR_FROM_NAME (ptr, name);
6024 java_parser_context_save_global ();
6025 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
6026 java_parser_context_restore_global ();
6027
6028 return decl;
6029 }
6030
6031 /* Called when reporting errors. Skip the '[]'s in a complex array
6032 type description that failed to be resolved. purify_type_name can't
6033 use an identifier tree. */
6034
6035 static const char *
6036 purify_type_name (name)
6037 const char *name;
6038 {
6039 int len = strlen (name);
6040 int bracket_found;
6041
6042 STRING_STRIP_BRACKETS (name, len, bracket_found);
6043 if (bracket_found)
6044 {
6045 char *stripped_name = xmemdup (name, len, len+1);
6046 stripped_name [len] = '\0';
6047 return stripped_name;
6048 }
6049 return name;
6050 }
6051
6052 /* The type CURRENT refers to can't be found. We print error messages. */
6053
6054 static void
6055 complete_class_report_errors (dep)
6056 jdep *dep;
6057 {
6058 const char *name;
6059
6060 if (!JDEP_WFL (dep))
6061 return;
6062
6063 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
6064 switch (JDEP_KIND (dep))
6065 {
6066 case JDEP_SUPER:
6067 parse_error_context
6068 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
6069 purify_type_name (name),
6070 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6071 break;
6072 case JDEP_FIELD:
6073 parse_error_context
6074 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
6075 purify_type_name (name),
6076 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6077 break;
6078 case JDEP_METHOD: /* Covers arguments */
6079 parse_error_context
6080 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
6081 purify_type_name (name),
6082 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
6083 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
6084 break;
6085 case JDEP_METHOD_RETURN: /* Covers return type */
6086 parse_error_context
6087 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
6088 purify_type_name (name),
6089 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
6090 break;
6091 case JDEP_INTERFACE:
6092 parse_error_context
6093 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
6094 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
6095 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
6096 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6097 break;
6098 case JDEP_VARIABLE:
6099 parse_error_context
6100 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
6101 purify_type_name (IDENTIFIER_POINTER
6102 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
6103 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6104 break;
6105 case JDEP_EXCEPTION: /* As specified by `throws' */
6106 parse_error_context
6107 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
6108 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
6109 break;
6110 default:
6111 /* Fix for -Wall. Just break doing nothing. The error will be
6112 caught later */
6113 break;
6114 }
6115 }
6116
6117 /* Return a static string containing the DECL prototype string. If
6118 DECL is a constructor, use the class name instead of the form
6119 <init> */
6120
6121 static const char *
6122 get_printable_method_name (decl)
6123 tree decl;
6124 {
6125 const char *to_return;
6126 tree name = NULL_TREE;
6127
6128 if (DECL_CONSTRUCTOR_P (decl))
6129 {
6130 name = DECL_NAME (decl);
6131 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6132 }
6133
6134 to_return = lang_printable_name (decl, 0);
6135 if (DECL_CONSTRUCTOR_P (decl))
6136 DECL_NAME (decl) = name;
6137
6138 return to_return;
6139 }
6140
6141 /* Track method being redefined inside the same class. As a side
6142 effect, set DECL_NAME to an IDENTIFIER (prior entering this
6143 function it's a FWL, so we can track errors more accurately.) */
6144
6145 static int
6146 check_method_redefinition (class, method)
6147 tree class, method;
6148 {
6149 tree redef, sig;
6150
6151 /* There's no need to verify <clinit> and finit$ and instinit$ */
6152 if (DECL_CLINIT_P (method)
6153 || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6154 return 0;
6155
6156 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6157 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6158 {
6159 if (redef == method)
6160 break;
6161 if (DECL_NAME (redef) == DECL_NAME (method)
6162 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6163 && !DECL_ARTIFICIAL (method))
6164 {
6165 parse_error_context
6166 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
6167 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6168 get_printable_method_name (redef));
6169 return 1;
6170 }
6171 }
6172 return 0;
6173 }
6174
6175 /* Return 1 if check went ok, 0 otherwise. */
6176 static int
6177 check_abstract_method_definitions (do_interface, class_decl, type)
6178 int do_interface;
6179 tree class_decl, type;
6180 {
6181 tree class = TREE_TYPE (class_decl);
6182 tree method, end_type;
6183 int ok = 1;
6184
6185 end_type = (do_interface ? object_type_node : type);
6186 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6187 {
6188 tree other_super, other_method, method_sig, method_name;
6189 int found = 0;
6190 int end_type_reached = 0;
6191
6192 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6193 continue;
6194
6195 /* Now verify that somewhere in between TYPE and CLASS,
6196 abstract method METHOD gets a non abstract definition
6197 that is inherited by CLASS. */
6198
6199 method_sig = build_java_signature (TREE_TYPE (method));
6200 method_name = DECL_NAME (method);
6201 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6202 method_name = EXPR_WFL_NODE (method_name);
6203
6204 other_super = class;
6205 do {
6206 if (other_super == end_type)
6207 end_type_reached = 1;
6208
6209 /* Method search */
6210 for (other_method = TYPE_METHODS (other_super); other_method;
6211 other_method = TREE_CHAIN (other_method))
6212 {
6213 tree s = build_java_signature (TREE_TYPE (other_method));
6214 tree other_name = DECL_NAME (other_method);
6215
6216 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6217 other_name = EXPR_WFL_NODE (other_name);
6218 if (!DECL_CLINIT_P (other_method)
6219 && !DECL_CONSTRUCTOR_P (other_method)
6220 && method_name == other_name
6221 && method_sig == s
6222 && !METHOD_ABSTRACT (other_method))
6223 {
6224 found = 1;
6225 break;
6226 }
6227 }
6228 other_super = CLASSTYPE_SUPER (other_super);
6229 } while (!end_type_reached);
6230
6231 /* Report that abstract METHOD didn't find an implementation
6232 that CLASS can use. */
6233 if (!found)
6234 {
6235 char *t = xstrdup (lang_printable_name
6236 (TREE_TYPE (TREE_TYPE (method)), 0));
6237 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6238
6239 parse_error_context
6240 (lookup_cl (class_decl),
6241 "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",
6242 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6243 t, lang_printable_name (method, 0),
6244 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6245 "interface" : "class"),
6246 IDENTIFIER_POINTER (ccn),
6247 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6248 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6249 ok = 0;
6250 free (t);
6251 }
6252 }
6253
6254 if (ok && do_interface)
6255 {
6256 /* Check for implemented interfaces. */
6257 int i;
6258 tree vector = TYPE_BINFO_BASETYPES (type);
6259 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
6260 {
6261 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6262 ok = check_abstract_method_definitions (1, class_decl, super);
6263 }
6264 }
6265
6266 return ok;
6267 }
6268
6269 /* Check that CLASS_DECL somehow implements all inherited abstract
6270 methods. */
6271
6272 static void
6273 java_check_abstract_method_definitions (class_decl)
6274 tree class_decl;
6275 {
6276 tree class = TREE_TYPE (class_decl);
6277 tree super, vector;
6278 int i;
6279
6280 if (CLASS_ABSTRACT (class_decl))
6281 return;
6282
6283 /* Check for inherited types */
6284 super = class;
6285 do {
6286 super = CLASSTYPE_SUPER (super);
6287 check_abstract_method_definitions (0, class_decl, super);
6288 } while (super != object_type_node);
6289
6290 /* Check for implemented interfaces. */
6291 vector = TYPE_BINFO_BASETYPES (class);
6292 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
6293 {
6294 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6295 check_abstract_method_definitions (1, class_decl, super);
6296 }
6297 }
6298
6299 /* Check all the types method DECL uses and return 1 if all of them
6300 are now complete, 0 otherwise. This is used to check whether its
6301 safe to build a method signature or not. */
6302
6303 static int
6304 check_method_types_complete (decl)
6305 tree decl;
6306 {
6307 tree type = TREE_TYPE (decl);
6308 tree args;
6309
6310 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6311 return 0;
6312
6313 args = TYPE_ARG_TYPES (type);
6314 if (TREE_CODE (type) == METHOD_TYPE)
6315 args = TREE_CHAIN (args);
6316 for (; args != end_params_node; args = TREE_CHAIN (args))
6317 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6318 return 0;
6319
6320 return 1;
6321 }
6322
6323 /* Visible interface to check methods contained in CLASS_DECL */
6324
6325 void
6326 java_check_methods (class_decl)
6327 tree class_decl;
6328 {
6329 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6330 return;
6331
6332 if (CLASS_INTERFACE (class_decl))
6333 java_check_abstract_methods (class_decl);
6334 else
6335 java_check_regular_methods (class_decl);
6336
6337 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6338 }
6339
6340 /* Check all the methods of CLASS_DECL. Methods are first completed
6341 then checked according to regular method existence rules. If no
6342 constructor for CLASS_DECL were encountered, then build its
6343 declaration. */
6344
6345 static void
6346 java_check_regular_methods (class_decl)
6347 tree class_decl;
6348 {
6349 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6350 tree method;
6351 tree class = TREE_TYPE (class_decl);
6352 tree found = NULL_TREE;
6353 tree mthrows;
6354
6355 /* It is not necessary to check methods defined in java.lang.Object */
6356 if (class == object_type_node)
6357 return;
6358
6359 if (!TYPE_NVIRTUALS (class))
6360 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6361
6362 /* Should take interfaces into account. FIXME */
6363 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6364 {
6365 tree sig;
6366 tree method_wfl = DECL_FUNCTION_WFL (method);
6367 int aflags;
6368
6369 /* Check for redefinitions */
6370 if (check_method_redefinition (class, method))
6371 continue;
6372
6373 /* If we see one constructor a mark so we don't generate the
6374 default one. Also skip other verifications: constructors
6375 can't be inherited hence hiden or overriden */
6376 if (DECL_CONSTRUCTOR_P (method))
6377 {
6378 saw_constructor = 1;
6379 continue;
6380 }
6381
6382 /* We verify things thrown by the method. They must inherits 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 sig = build_java_argument_signature (TREE_TYPE (method));
6395 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6396
6397 /* Inner class can't declare static methods */
6398 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6399 {
6400 char *t = xstrdup (lang_printable_name (class, 0));
6401 parse_error_context
6402 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6403 lang_printable_name (method, 0), t);
6404 free (t);
6405 }
6406
6407 /* Nothing overrides or it's a private method. */
6408 if (!found)
6409 continue;
6410 if (METHOD_PRIVATE (found))
6411 {
6412 found = NULL_TREE;
6413 continue;
6414 }
6415
6416 /* If `found' is declared in an interface, make sure the
6417 modifier matches. */
6418 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6419 && clinit_identifier_node != DECL_NAME (found)
6420 && !METHOD_PUBLIC (method))
6421 {
6422 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6423 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6424 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6425 lang_printable_name (method, 0),
6426 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6427 }
6428
6429 /* Can't override a method with the same name and different return
6430 types. */
6431 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6432 {
6433 char *t = xstrdup
6434 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6435 parse_error_context
6436 (method_wfl,
6437 "Method `%s' was defined with return type `%s' in class `%s'",
6438 lang_printable_name (found, 0), t,
6439 IDENTIFIER_POINTER
6440 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6441 free (t);
6442 }
6443
6444 aflags = get_access_flags_from_decl (found);
6445
6446 /* Can't override final. Can't override static. */
6447 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6448 {
6449 /* Static *can* override static */
6450 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6451 continue;
6452 parse_error_context
6453 (method_wfl,
6454 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6455 (METHOD_FINAL (found) ? "Final" : "Static"),
6456 lang_printable_name (found, 0),
6457 (METHOD_FINAL (found) ? "final" : "static"),
6458 IDENTIFIER_POINTER
6459 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6460 continue;
6461 }
6462
6463 /* Static method can't override instance method. */
6464 if (METHOD_STATIC (method))
6465 {
6466 parse_error_context
6467 (method_wfl,
6468 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6469 lang_printable_name (found, 0),
6470 IDENTIFIER_POINTER
6471 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6472 continue;
6473 }
6474
6475 /* - Overriding/hiding public must be public
6476 - Overriding/hiding protected must be protected or public
6477 - If the overriden or hidden method has default (package)
6478 access, then the overriding or hiding method must not be
6479 private; otherwise, a compile-time error occurs. If
6480 `found' belongs to an interface, things have been already
6481 taken care of. */
6482 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6483 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6484 || (METHOD_PROTECTED (found)
6485 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6486 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6487 && METHOD_PRIVATE (method))))
6488 {
6489 parse_error_context
6490 (method_wfl,
6491 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6492 (METHOD_PUBLIC (method) ? "public" :
6493 (METHOD_PRIVATE (method) ? "private" : "protected")),
6494 IDENTIFIER_POINTER (DECL_NAME
6495 (TYPE_NAME (DECL_CONTEXT (found)))));
6496 continue;
6497 }
6498
6499 /* Overriding methods must have compatible `throws' clauses on checked
6500 exceptions, if any */
6501 check_throws_clauses (method, method_wfl, found);
6502
6503 /* Inheriting multiple methods with the same signature. FIXME */
6504 }
6505
6506 if (!TYPE_NVIRTUALS (class))
6507 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6508
6509 /* Search for inherited abstract method not yet implemented in this
6510 class. */
6511 java_check_abstract_method_definitions (class_decl);
6512
6513 if (!saw_constructor)
6514 abort ();
6515 }
6516
6517 /* Return a nonzero value if the `throws' clause of METHOD (if any)
6518 is incompatible with the `throws' clause of FOUND (if any). */
6519
6520 static void
6521 check_throws_clauses (method, method_wfl, found)
6522 tree method, method_wfl, found;
6523 {
6524 tree mthrows, fthrows;
6525
6526 /* Can't check these things with class loaded from bytecode. FIXME */
6527 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6528 return;
6529
6530 for (mthrows = DECL_FUNCTION_THROWS (method);
6531 mthrows; mthrows = TREE_CHAIN (mthrows))
6532 {
6533 /* We don't verify unchecked expressions */
6534 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6535 continue;
6536 /* Checked expression must be compatible */
6537 for (fthrows = DECL_FUNCTION_THROWS (found);
6538 fthrows; fthrows = TREE_CHAIN (fthrows))
6539 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6540 break;
6541 if (!fthrows)
6542 {
6543 parse_error_context
6544 (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'",
6545 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6546 lang_printable_name (found, 0),
6547 IDENTIFIER_POINTER
6548 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6549 }
6550 }
6551 }
6552
6553 /* Check abstract method of interface INTERFACE */
6554
6555 static void
6556 java_check_abstract_methods (interface_decl)
6557 tree interface_decl;
6558 {
6559 int i, n;
6560 tree method, basetype_vec, found;
6561 tree interface = TREE_TYPE (interface_decl);
6562
6563 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6564 {
6565 /* 2- Check for double definition inside the defining interface */
6566 if (check_method_redefinition (interface, method))
6567 continue;
6568
6569 /* 3- Overriding is OK as far as we preserve the return type and
6570 the thrown exceptions (FIXME) */
6571 found = lookup_java_interface_method2 (interface, method);
6572 if (found)
6573 {
6574 char *t;
6575 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6576 parse_error_context
6577 (DECL_FUNCTION_WFL (found),
6578 "Method `%s' was defined with return type `%s' in class `%s'",
6579 lang_printable_name (found, 0), t,
6580 IDENTIFIER_POINTER
6581 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6582 free (t);
6583 continue;
6584 }
6585 }
6586
6587 /* 4- Inherited methods can't differ by their returned types */
6588 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6589 return;
6590 n = TREE_VEC_LENGTH (basetype_vec);
6591 for (i = 0; i < n; i++)
6592 {
6593 tree sub_interface_method, sub_interface;
6594 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6595 if (!vec_elt)
6596 continue;
6597 sub_interface = BINFO_TYPE (vec_elt);
6598 for (sub_interface_method = TYPE_METHODS (sub_interface);
6599 sub_interface_method;
6600 sub_interface_method = TREE_CHAIN (sub_interface_method))
6601 {
6602 found = lookup_java_interface_method2 (interface,
6603 sub_interface_method);
6604 if (found && (found != sub_interface_method))
6605 {
6606 parse_error_context
6607 (lookup_cl (sub_interface_method),
6608 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6609 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6610 lang_printable_name (found, 0),
6611 IDENTIFIER_POINTER
6612 (DECL_NAME (TYPE_NAME
6613 (DECL_CONTEXT (sub_interface_method)))),
6614 IDENTIFIER_POINTER
6615 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6616 }
6617 }
6618 }
6619 }
6620
6621 /* Lookup methods in interfaces using their name and partial
6622 signature. Return a matching method only if their types differ. */
6623
6624 static tree
6625 lookup_java_interface_method2 (class, method_decl)
6626 tree class, method_decl;
6627 {
6628 int i, n;
6629 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6630
6631 if (!basetype_vec)
6632 return NULL_TREE;
6633
6634 n = TREE_VEC_LENGTH (basetype_vec);
6635 for (i = 0; i < n; i++)
6636 {
6637 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6638 if ((BINFO_TYPE (vec_elt) != object_type_node)
6639 && (to_return =
6640 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6641 return to_return;
6642 }
6643 for (i = 0; i < n; i++)
6644 {
6645 to_return = lookup_java_interface_method2
6646 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6647 if (to_return)
6648 return to_return;
6649 }
6650
6651 return NULL_TREE;
6652 }
6653
6654 /* Lookup method using their name and partial signature. Return a
6655 matching method only if their types differ. */
6656
6657 static tree
6658 lookup_java_method2 (clas, method_decl, do_interface)
6659 tree clas, method_decl;
6660 int do_interface;
6661 {
6662 tree method, method_signature, method_name, method_type, name;
6663
6664 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6665 name = DECL_NAME (method_decl);
6666 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6667 EXPR_WFL_NODE (name) : name);
6668 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6669
6670 while (clas != NULL_TREE)
6671 {
6672 for (method = TYPE_METHODS (clas);
6673 method != NULL_TREE; method = TREE_CHAIN (method))
6674 {
6675 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6676 tree name = DECL_NAME (method);
6677 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6678 EXPR_WFL_NODE (name) : name) == method_name
6679 && method_sig == method_signature
6680 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6681 return method;
6682 }
6683 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6684 }
6685 return NULL_TREE;
6686 }
6687
6688 /* Return the line that matches DECL line number, and try its best to
6689 position the column number. Used during error reports. */
6690
6691 static GTY(()) tree cl_v;
6692 static tree
6693 lookup_cl (decl)
6694 tree decl;
6695 {
6696 char *line, *found;
6697
6698 if (!decl)
6699 return NULL_TREE;
6700
6701 if (cl_v == NULL_TREE)
6702 {
6703 cl_v = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6704 }
6705
6706 EXPR_WFL_FILENAME_NODE (cl_v) = get_identifier (DECL_SOURCE_FILE (decl));
6707 EXPR_WFL_SET_LINECOL (cl_v, DECL_SOURCE_LINE_FIRST (decl), -1);
6708
6709 line = java_get_line_col (EXPR_WFL_FILENAME (cl_v),
6710 EXPR_WFL_LINENO (cl_v), EXPR_WFL_COLNO (cl_v));
6711
6712 found = strstr ((const char *)line,
6713 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6714 if (found)
6715 EXPR_WFL_SET_LINECOL (cl_v, EXPR_WFL_LINENO (cl_v), found - line);
6716
6717 return cl_v;
6718 }
6719
6720 /* Look for a simple name in the single-type import list */
6721
6722 static tree
6723 find_name_in_single_imports (name)
6724 tree name;
6725 {
6726 tree node;
6727
6728 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6729 if (TREE_VALUE (node) == name)
6730 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6731
6732 return NULL_TREE;
6733 }
6734
6735 /* Process all single-type import. */
6736
6737 static int
6738 process_imports ()
6739 {
6740 tree import;
6741 int error_found;
6742
6743 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6744 {
6745 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6746 char *original_name;
6747
6748 obstack_grow0 (&temporary_obstack,
6749 IDENTIFIER_POINTER (to_be_found),
6750 IDENTIFIER_LENGTH (to_be_found));
6751 original_name = obstack_finish (&temporary_obstack);
6752
6753 /* Don't load twice something already defined. */
6754 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6755 continue;
6756
6757 while (1)
6758 {
6759 tree left;
6760
6761 QUALIFIED_P (to_be_found) = 1;
6762 load_class (to_be_found, 0);
6763 error_found =
6764 check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true);
6765
6766 /* We found it, we can bail out */
6767 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6768 break;
6769
6770 /* We haven't found it. Maybe we're trying to access an
6771 inner class. The only way for us to know is to try again
6772 after having dropped a qualifier. If we can't break it further,
6773 we have an error. */
6774 if (breakdown_qualified (&left, NULL, to_be_found))
6775 break;
6776
6777 to_be_found = left;
6778 }
6779 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6780 {
6781 parse_error_context (TREE_PURPOSE (import),
6782 "Class or interface `%s' not found in import",
6783 original_name);
6784 error_found = 1;
6785 }
6786
6787 obstack_free (&temporary_obstack, original_name);
6788 if (error_found)
6789 return 1;
6790 }
6791 return 0;
6792 }
6793
6794 /* Possibly find and mark a class imported by a single-type import
6795 statement. */
6796
6797 static void
6798 find_in_imports (enclosing_type, class_type)
6799 tree enclosing_type;
6800 tree class_type;
6801 {
6802 tree import = (enclosing_type ? TYPE_IMPORT_LIST (enclosing_type) :
6803 ctxp->import_list);
6804 while (import)
6805 {
6806 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6807 {
6808 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6809 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6810 return;
6811 }
6812 import = TREE_CHAIN (import);
6813 }
6814 }
6815
6816 static int
6817 note_possible_classname (name, len)
6818 const char *name;
6819 int len;
6820 {
6821 tree node;
6822 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6823 len = len - 5;
6824 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6825 len = len - 6;
6826 else
6827 return 0;
6828 node = ident_subst (name, len, "", '/', '.', "");
6829 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6830 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6831 return 1;
6832 }
6833
6834 /* Read a import directory, gathering potential match for further type
6835 references. Indifferently reads a filesystem or a ZIP archive
6836 directory. */
6837
6838 static void
6839 read_import_dir (wfl)
6840 tree wfl;
6841 {
6842 tree package_id = EXPR_WFL_NODE (wfl);
6843 const char *package_name = IDENTIFIER_POINTER (package_id);
6844 int package_length = IDENTIFIER_LENGTH (package_id);
6845 DIR *dirp = NULL;
6846 JCF *saved_jcf = current_jcf;
6847
6848 int found = 0;
6849 int k;
6850 void *entry;
6851 struct buffer filename[1];
6852
6853 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6854 return;
6855 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6856
6857 BUFFER_INIT (filename);
6858 buffer_grow (filename, package_length + 100);
6859
6860 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6861 {
6862 const char *entry_name = jcf_path_name (entry);
6863 int entry_length = strlen (entry_name);
6864 if (jcf_path_is_zipfile (entry))
6865 {
6866 ZipFile *zipf;
6867 buffer_grow (filename, entry_length);
6868 memcpy (filename->data, entry_name, entry_length - 1);
6869 filename->data[entry_length-1] = '\0';
6870 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6871 if (zipf == NULL)
6872 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6873 else
6874 {
6875 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6876 BUFFER_RESET (filename);
6877 for (k = 0; k < package_length; k++)
6878 {
6879 char ch = package_name[k];
6880 *filename->ptr++ = ch == '.' ? '/' : ch;
6881 }
6882 *filename->ptr++ = '/';
6883
6884 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
6885 {
6886 const char *current_entry = ZIPDIR_FILENAME (zipd);
6887 int current_entry_len = zipd->filename_length;
6888
6889 if (current_entry_len >= BUFFER_LENGTH (filename)
6890 && strncmp (filename->data, current_entry,
6891 BUFFER_LENGTH (filename)) != 0)
6892 continue;
6893 found |= note_possible_classname (current_entry,
6894 current_entry_len);
6895 }
6896 }
6897 }
6898 else
6899 {
6900 BUFFER_RESET (filename);
6901 buffer_grow (filename, entry_length + package_length + 4);
6902 strcpy (filename->data, entry_name);
6903 filename->ptr = filename->data + entry_length;
6904 for (k = 0; k < package_length; k++)
6905 {
6906 char ch = package_name[k];
6907 *filename->ptr++ = ch == '.' ? '/' : ch;
6908 }
6909 *filename->ptr = '\0';
6910
6911 dirp = opendir (filename->data);
6912 if (dirp == NULL)
6913 continue;
6914 *filename->ptr++ = '/';
6915 for (;;)
6916 {
6917 int len;
6918 const char *d_name;
6919 struct dirent *direntp = readdir (dirp);
6920 if (!direntp)
6921 break;
6922 d_name = direntp->d_name;
6923 len = strlen (direntp->d_name);
6924 buffer_grow (filename, len+1);
6925 strcpy (filename->ptr, d_name);
6926 found |= note_possible_classname (filename->data + entry_length,
6927 package_length+len+1);
6928 }
6929 if (dirp)
6930 closedir (dirp);
6931 }
6932 }
6933
6934 free (filename->data);
6935
6936 /* Here we should have a unified way of retrieving an entry, to be
6937 indexed. */
6938 if (!found)
6939 {
6940 static int first = 1;
6941 if (first)
6942 {
6943 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives", package_name);
6944 java_error_count++;
6945 first = 0;
6946 }
6947 else
6948 parse_error_context (wfl, "Package `%s' not found in import",
6949 package_name);
6950 current_jcf = saved_jcf;
6951 return;
6952 }
6953 current_jcf = saved_jcf;
6954 }
6955
6956 /* Possibly find a type in the import on demands specified
6957 types. Returns 1 if an error occurred, 0 otherwise. Run through the
6958 entire list, to detected potential double definitions. */
6959
6960 static int
6961 find_in_imports_on_demand (enclosing_type, class_type)
6962 tree enclosing_type;
6963 tree class_type;
6964 {
6965 tree class_type_name = TYPE_NAME (class_type);
6966 tree import = (enclosing_type ? TYPE_IMPORT_DEMAND_LIST (enclosing_type) :
6967 ctxp->import_demand_list);
6968 tree cl = NULL_TREE;
6969 int seen_once = -1; /* -1 when not set, 1 if seen once, >1 otherwise. */
6970 int to_return = -1; /* -1 when not set, 0 or 1 otherwise */
6971 tree node;
6972
6973 for (; import; import = TREE_CHAIN (import))
6974 {
6975 int saved_lineno = lineno;
6976 int access_check;
6977 const char *id_name;
6978 tree decl, type_name_copy;
6979
6980 obstack_grow (&temporary_obstack,
6981 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6982 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6983 obstack_1grow (&temporary_obstack, '.');
6984 obstack_grow0 (&temporary_obstack,
6985 IDENTIFIER_POINTER (class_type_name),
6986 IDENTIFIER_LENGTH (class_type_name));
6987 id_name = obstack_finish (&temporary_obstack);
6988
6989 if (! (node = maybe_get_identifier (id_name)))
6990 continue;
6991
6992 /* Setup lineno so that it refers to the line of the import (in
6993 case we parse a class file and encounter errors */
6994 lineno = EXPR_WFL_LINENO (TREE_PURPOSE (import));
6995
6996 type_name_copy = TYPE_NAME (class_type);
6997 TYPE_NAME (class_type) = node;
6998 QUALIFIED_P (node) = 1;
6999 decl = IDENTIFIER_CLASS_VALUE (node);
7000 access_check = -1;
7001 /* If there is no DECL set for the class or if the class isn't
7002 loaded and not seen in source yet, then load */
7003 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
7004 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
7005 {
7006 load_class (node, 0);
7007 decl = IDENTIFIER_CLASS_VALUE (node);
7008 }
7009 if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
7010 access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
7011 false);
7012 else
7013 /* 6.6.1: Inner classes are subject to member access rules. */
7014 access_check = 0;
7015
7016 lineno = saved_lineno;
7017
7018 /* If the loaded class is not accessible or couldn't be loaded,
7019 we restore the original TYPE_NAME and process the next
7020 import. */
7021 if (access_check || !decl)
7022 {
7023 TYPE_NAME (class_type) = type_name_copy;
7024 continue;
7025 }
7026
7027 /* If the loaded class is accessible, we keep a tab on it to
7028 detect and report multiple inclusions. */
7029 if (IS_A_CLASSFILE_NAME (node))
7030 {
7031 if (seen_once < 0)
7032 {
7033 cl = TREE_PURPOSE (import);
7034 seen_once = 1;
7035 }
7036 else if (seen_once >= 0)
7037 {
7038 tree location = (cl ? cl : TREE_PURPOSE (import));
7039 tree package = (cl ? EXPR_WFL_NODE (cl) :
7040 EXPR_WFL_NODE (TREE_PURPOSE (import)));
7041 seen_once++;
7042 parse_error_context
7043 (location,
7044 "Type `%s' also potentially defined in package `%s'",
7045 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
7046 IDENTIFIER_POINTER (package));
7047 }
7048 }
7049 to_return = access_check;
7050 }
7051
7052 if (seen_once == 1)
7053 return to_return;
7054 else
7055 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
7056 }
7057
7058 /* Add package NAME to the list of packages encountered so far. To
7059 speed up class lookup in do_resolve_class, we make sure a
7060 particular package is added only once. */
7061
7062 static void
7063 register_package (name)
7064 tree name;
7065 {
7066 static htab_t pht;
7067 PTR *e;
7068
7069 if (pht == NULL)
7070 pht = htab_create (50, htab_hash_pointer, htab_eq_pointer, NULL);
7071
7072 e = htab_find_slot (pht, name, INSERT);
7073 if (*e == NULL)
7074 {
7075 package_list = chainon (package_list, build_tree_list (name, NULL));
7076 *e = name;
7077 }
7078 }
7079
7080 static tree
7081 resolve_package (pkg, next, type_name)
7082 tree pkg, *next, *type_name;
7083 {
7084 tree current;
7085 tree decl = NULL_TREE;
7086 *type_name = NULL_TREE;
7087
7088 /* The trick is to determine when the package name stops and were
7089 the name of something contained in the package starts. Then we
7090 return a fully qualified name of what we want to get. */
7091
7092 *next = EXPR_WFL_QUALIFICATION (pkg);
7093
7094 /* Try to progressively construct a type name */
7095 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
7096 for (current = EXPR_WFL_QUALIFICATION (pkg);
7097 current; current = TREE_CHAIN (current))
7098 {
7099 /* If we don't have what we're expecting, exit now. TYPE_NAME
7100 will be null and the error caught later. */
7101 if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
7102 break;
7103 *type_name =
7104 merge_qualified_name (*type_name, EXPR_WFL_NODE (QUAL_WFL (current)));
7105 if ((decl = resolve_no_layout (*type_name, NULL_TREE)))
7106 {
7107 /* resolve_package should be used in a loop, hence we
7108 point at this one to naturally process the next one at
7109 the next iteration. */
7110 *next = current;
7111 break;
7112 }
7113 }
7114 return decl;
7115 }
7116
7117
7118 /* Check accessibility of inner classes according to member access rules.
7119 DECL is the inner class, ENCLOSING_DECL is the class from which the
7120 access is being attempted. */
7121
7122 static void
7123 check_inner_class_access (decl, enclosing_decl, cl)
7124 tree decl, enclosing_decl, cl;
7125 {
7126 const char *access;
7127 tree enclosing_decl_type;
7128
7129 /* We don't issue an error message when CL is null. CL can be null
7130 as a result of processing a JDEP crafted by source_start_java_method
7131 for the purpose of patching its parm decl. But the error would
7132 have been already trapped when fixing the method's signature.
7133 DECL can also be NULL in case of earlier errors. */
7134 if (!decl || !cl)
7135 return;
7136
7137 enclosing_decl_type = TREE_TYPE (enclosing_decl);
7138
7139 if (CLASS_PRIVATE (decl))
7140 {
7141 /* Access is permitted only within the body of the top-level
7142 class in which DECL is declared. */
7143 tree top_level = decl;
7144 while (DECL_CONTEXT (top_level))
7145 top_level = DECL_CONTEXT (top_level);
7146 while (DECL_CONTEXT (enclosing_decl))
7147 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7148 if (top_level == enclosing_decl)
7149 return;
7150 access = "private";
7151 }
7152 else if (CLASS_PROTECTED (decl))
7153 {
7154 tree decl_context;
7155 /* Access is permitted from within the same package... */
7156 if (in_same_package (decl, enclosing_decl))
7157 return;
7158
7159 /* ... or from within the body of a subtype of the context in which
7160 DECL is declared. */
7161 decl_context = DECL_CONTEXT (decl);
7162 while (enclosing_decl)
7163 {
7164 if (CLASS_INTERFACE (decl))
7165 {
7166 if (interface_of_p (TREE_TYPE (decl_context),
7167 enclosing_decl_type))
7168 return;
7169 }
7170 else
7171 {
7172 /* Eww. The order of the arguments is different!! */
7173 if (inherits_from_p (enclosing_decl_type,
7174 TREE_TYPE (decl_context)))
7175 return;
7176 }
7177 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7178 }
7179 access = "protected";
7180 }
7181 else if (! CLASS_PUBLIC (decl))
7182 {
7183 /* Access is permitted only from within the same package as DECL. */
7184 if (in_same_package (decl, enclosing_decl))
7185 return;
7186 access = "non-public";
7187 }
7188 else
7189 /* Class is public. */
7190 return;
7191
7192 parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7193 (CLASS_INTERFACE (decl) ? "interface" : "class"),
7194 lang_printable_name (decl, 0), access);
7195 }
7196
7197 /* Accessibility check for top-level classes. If CLASS_NAME is in a
7198 foreign package, it must be PUBLIC. Return 0 if no access
7199 violations were found, 1 otherwise. If VERBOSE is true and an error
7200 was found, it is reported and accounted for. */
7201
7202 static int
7203 check_pkg_class_access (class_name, cl, verbose)
7204 tree class_name;
7205 tree cl;
7206 bool verbose;
7207 {
7208 tree type;
7209
7210 if (!IDENTIFIER_CLASS_VALUE (class_name))
7211 return 0;
7212
7213 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7214 return 0;
7215
7216 if (!CLASS_PUBLIC (TYPE_NAME (type)))
7217 {
7218 /* Access to a private class within the same package is
7219 allowed. */
7220 tree l, r;
7221 breakdown_qualified (&l, &r, class_name);
7222 if (!QUALIFIED_P (class_name) && !ctxp->package)
7223 /* Both in the empty package. */
7224 return 0;
7225 if (l == ctxp->package)
7226 /* Both in the same package. */
7227 return 0;
7228
7229 if (verbose)
7230 parse_error_context
7231 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
7232 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7233 IDENTIFIER_POINTER (class_name));
7234 return 1;
7235 }
7236 return 0;
7237 }
7238
7239 /* Local variable declaration. */
7240
7241 static void
7242 declare_local_variables (modifier, type, vlist)
7243 int modifier;
7244 tree type;
7245 tree vlist;
7246 {
7247 tree decl, current, saved_type;
7248 tree type_wfl = NULL_TREE;
7249 int must_chain = 0;
7250 int final_p = 0;
7251
7252 /* Push a new block if statements were seen between the last time we
7253 pushed a block and now. Keep a count of blocks to close */
7254 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7255 {
7256 tree b = enter_block ();
7257 BLOCK_IS_IMPLICIT (b) = 1;
7258 }
7259
7260 if (modifier)
7261 {
7262 size_t i;
7263 for (i = 0; i < ARRAY_SIZE (ctxp->modifier_ctx); i++)
7264 if (1 << i & modifier)
7265 break;
7266 if (modifier == ACC_FINAL)
7267 final_p = 1;
7268 else
7269 {
7270 parse_error_context
7271 (ctxp->modifier_ctx [i],
7272 "Only `final' is allowed as a local variables modifier");
7273 return;
7274 }
7275 }
7276
7277 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7278 hold the TYPE value if a new incomplete has to be created (as
7279 opposed to being found already existing and reused). */
7280 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7281
7282 /* If TYPE is fully resolved and we don't have a reference, make one */
7283 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7284
7285 /* Go through all the declared variables */
7286 for (current = vlist, saved_type = type; current;
7287 current = TREE_CHAIN (current), type = saved_type)
7288 {
7289 tree other, real_type;
7290 tree wfl = TREE_PURPOSE (current);
7291 tree name = EXPR_WFL_NODE (wfl);
7292 tree init = TREE_VALUE (current);
7293
7294 /* Process NAME, as it may specify extra dimension(s) for it */
7295 type = build_array_from_name (type, type_wfl, name, &name);
7296
7297 /* Variable redefinition check */
7298 if ((other = lookup_name_in_blocks (name)))
7299 {
7300 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7301 DECL_SOURCE_LINE (other));
7302 continue;
7303 }
7304
7305 /* Type adjustment. We may have just readjusted TYPE because
7306 the variable specified more dimensions. Make sure we have
7307 a reference if we can and don't have one already. */
7308 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7309
7310 real_type = GET_REAL_TYPE (type);
7311 /* Never layout this decl. This will be done when its scope
7312 will be entered */
7313 decl = build_decl (VAR_DECL, name, real_type);
7314 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7315 DECL_FINAL (decl) = final_p;
7316 BLOCK_CHAIN_DECL (decl);
7317
7318 /* If doing xreferencing, replace the line number with the WFL
7319 compound value */
7320 if (flag_emit_xref)
7321 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7322
7323 /* Don't try to use an INIT statement when an error was found */
7324 if (init && java_error_count)
7325 init = NULL_TREE;
7326
7327 /* Add the initialization function to the current function's code */
7328 if (init)
7329 {
7330 /* Name might have been readjusted */
7331 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7332 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7333 java_method_add_stmt (current_function_decl,
7334 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7335 init));
7336 }
7337
7338 /* Setup dependency the type of the decl */
7339 if (must_chain)
7340 {
7341 jdep *dep;
7342 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7343 dep = CLASSD_LAST (ctxp->classd_list);
7344 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7345 }
7346 }
7347 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7348 }
7349
7350 /* Called during parsing. Build decls from argument list. */
7351
7352 static void
7353 source_start_java_method (fndecl)
7354 tree fndecl;
7355 {
7356 tree tem;
7357 tree parm_decl;
7358 int i;
7359
7360 if (!fndecl)
7361 return;
7362
7363 current_function_decl = fndecl;
7364
7365 /* New scope for the function */
7366 enter_block ();
7367 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7368 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7369 {
7370 tree type = TREE_VALUE (tem);
7371 tree name = TREE_PURPOSE (tem);
7372
7373 /* If type is incomplete. Create an incomplete decl and ask for
7374 the decl to be patched later */
7375 if (INCOMPLETE_TYPE_P (type))
7376 {
7377 jdep *jdep;
7378 tree real_type = GET_REAL_TYPE (type);
7379 parm_decl = build_decl (PARM_DECL, name, real_type);
7380 type = obtain_incomplete_type (type);
7381 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7382 jdep = CLASSD_LAST (ctxp->classd_list);
7383 JDEP_MISC (jdep) = name;
7384 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7385 }
7386 else
7387 parm_decl = build_decl (PARM_DECL, name, type);
7388
7389 /* Remember if a local variable was declared final (via its
7390 TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7391 if (ARG_FINAL_P (tem))
7392 {
7393 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7394 DECL_FINAL (parm_decl) = 1;
7395 }
7396
7397 BLOCK_CHAIN_DECL (parm_decl);
7398 }
7399 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7400 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7401 nreverse (tem);
7402 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7403 DECL_MAX_LOCALS (current_function_decl) = i;
7404 }
7405
7406 /* Called during parsing. Creates an artificial method declaration. */
7407
7408 static tree
7409 create_artificial_method (class, flags, type, name, args)
7410 tree class;
7411 int flags;
7412 tree type, name, args;
7413 {
7414 tree mdecl;
7415
7416 java_parser_context_save_global ();
7417 lineno = 0;
7418 mdecl = make_node (FUNCTION_TYPE);
7419 TREE_TYPE (mdecl) = type;
7420 TYPE_ARG_TYPES (mdecl) = args;
7421 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7422 java_parser_context_restore_global ();
7423 DECL_ARTIFICIAL (mdecl) = 1;
7424 return mdecl;
7425 }
7426
7427 /* Starts the body if an artificial method. */
7428
7429 static void
7430 start_artificial_method_body (mdecl)
7431 tree mdecl;
7432 {
7433 DECL_SOURCE_LINE (mdecl) = 1;
7434 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7435 source_start_java_method (mdecl);
7436 enter_block ();
7437 }
7438
7439 static void
7440 end_artificial_method_body (mdecl)
7441 tree mdecl;
7442 {
7443 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7444 It has to be evaluated first. (if mdecl is current_function_decl,
7445 we have an undefined behavior if no temporary variable is used.) */
7446 tree b = exit_block ();
7447 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7448 exit_block ();
7449 }
7450
7451 /* Dump a tree of some kind. This is a convenience wrapper for the
7452 dump_* functions in tree-dump.c. */
7453 static void
7454 dump_java_tree (phase, t)
7455 enum tree_dump_index phase;
7456 tree t;
7457 {
7458 FILE *stream;
7459 int flags;
7460
7461 stream = dump_begin (phase, &flags);
7462 flags |= TDF_SLIM;
7463 if (stream)
7464 {
7465 dump_node (t, flags, stream);
7466 dump_end (phase, stream);
7467 }
7468 }
7469
7470 /* Terminate a function and expand its body. */
7471
7472 static void
7473 source_end_java_method ()
7474 {
7475 tree fndecl = current_function_decl;
7476
7477 if (!fndecl)
7478 return;
7479
7480 java_parser_context_save_global ();
7481 lineno = ctxp->last_ccb_indent1;
7482
7483 /* Turn function bodies with only a NOP expr null, so they don't get
7484 generated at all and we won't get warnings when using the -W
7485 -Wall flags. */
7486 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7487 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7488
7489 /* We've generated all the trees for this function, and it has been
7490 patched. Dump it to a file if the user requested it. */
7491 dump_java_tree (TDI_original, fndecl);
7492
7493 java_optimize_inline (fndecl);
7494
7495 /* Generate function's code */
7496 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7497 && ! flag_emit_class_files
7498 && ! flag_emit_xref)
7499 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7500
7501 /* pop out of its parameters */
7502 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7503 poplevel (1, 0, 1);
7504 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7505
7506 /* Generate rtl for function exit. */
7507 if (! flag_emit_class_files && ! flag_emit_xref)
7508 {
7509 lineno = DECL_SOURCE_LINE_LAST (fndecl);
7510 expand_function_end (input_filename, lineno, 0);
7511
7512 DECL_SOURCE_LINE (fndecl) = DECL_SOURCE_LINE_FIRST (fndecl);
7513
7514 /* Run the optimizers and output assembler code for this function. */
7515 rest_of_compilation (fndecl);
7516 }
7517
7518 current_function_decl = NULL_TREE;
7519 java_parser_context_restore_global ();
7520 }
7521
7522 /* Record EXPR in the current function block. Complements compound
7523 expression second operand if necessary. */
7524
7525 tree
7526 java_method_add_stmt (fndecl, expr)
7527 tree fndecl, expr;
7528 {
7529 if (!GET_CURRENT_BLOCK (fndecl))
7530 return NULL_TREE;
7531 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7532 }
7533
7534 static tree
7535 add_stmt_to_block (b, type, stmt)
7536 tree b, type, stmt;
7537 {
7538 tree body = BLOCK_EXPR_BODY (b), c;
7539
7540 if (java_error_count)
7541 return body;
7542
7543 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7544 return body;
7545
7546 BLOCK_EXPR_BODY (b) = c;
7547 TREE_SIDE_EFFECTS (c) = 1;
7548 return c;
7549 }
7550
7551 /* Add STMT to EXISTING if possible, otherwise create a new
7552 COMPOUND_EXPR and add STMT to it. */
7553
7554 static tree
7555 add_stmt_to_compound (existing, type, stmt)
7556 tree existing, type, stmt;
7557 {
7558 /* Keep track of this for inlining. */
7559 if (current_function_decl)
7560 ++DECL_NUM_STMTS (current_function_decl);
7561
7562 if (existing)
7563 return build (COMPOUND_EXPR, type, existing, stmt);
7564 else
7565 return stmt;
7566 }
7567
7568 void java_layout_seen_class_methods ()
7569 {
7570 tree previous_list = all_class_list;
7571 tree end = NULL_TREE;
7572 tree current;
7573
7574 while (1)
7575 {
7576 for (current = previous_list;
7577 current != end; current = TREE_CHAIN (current))
7578 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7579
7580 if (previous_list != all_class_list)
7581 {
7582 end = previous_list;
7583 previous_list = all_class_list;
7584 }
7585 else
7586 break;
7587 }
7588 }
7589
7590 static GTY(()) tree stop_reordering;
7591 void
7592 java_reorder_fields ()
7593 {
7594 tree current;
7595
7596 for (current = gclass_list; current; current = TREE_CHAIN (current))
7597 {
7598 current_class = TREE_TYPE (TREE_VALUE (current));
7599
7600 if (current_class == stop_reordering)
7601 break;
7602
7603 /* Reverse the fields, but leave the dummy field in front.
7604 Fields are already ordered for Object and Class */
7605 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7606 && current_class != class_type_node)
7607 {
7608 /* If the dummy field is there, reverse the right fields and
7609 just layout the type for proper fields offset */
7610 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7611 {
7612 tree fields = TYPE_FIELDS (current_class);
7613 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7614 TYPE_SIZE (current_class) = NULL_TREE;
7615 }
7616 /* We don't have a dummy field, we need to layout the class,
7617 after having reversed the fields */
7618 else
7619 {
7620 TYPE_FIELDS (current_class) =
7621 nreverse (TYPE_FIELDS (current_class));
7622 TYPE_SIZE (current_class) = NULL_TREE;
7623 }
7624 }
7625 }
7626 /* There are cases were gclass_list will be empty. */
7627 if (gclass_list)
7628 stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7629 }
7630
7631 /* Layout the methods of all classes loaded in one way or another.
7632 Check methods of source parsed classes. Then reorder the
7633 fields and layout the classes or the type of all source parsed
7634 classes */
7635
7636 void
7637 java_layout_classes ()
7638 {
7639 tree current;
7640 int save_error_count = java_error_count;
7641
7642 /* Layout the methods of all classes seen so far */
7643 java_layout_seen_class_methods ();
7644 java_parse_abort_on_error ();
7645 all_class_list = NULL_TREE;
7646
7647 /* Then check the methods of all parsed classes */
7648 for (current = gclass_list; current; current = TREE_CHAIN (current))
7649 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7650 java_check_methods (TREE_VALUE (current));
7651 java_parse_abort_on_error ();
7652
7653 for (current = gclass_list; current; current = TREE_CHAIN (current))
7654 {
7655 current_class = TREE_TYPE (TREE_VALUE (current));
7656 layout_class (current_class);
7657
7658 /* Error reported by the caller */
7659 if (java_error_count)
7660 return;
7661 }
7662
7663 /* We might have reloaded classes durign the process of laying out
7664 classes for code generation. We must layout the methods of those
7665 late additions, as constructor checks might use them */
7666 java_layout_seen_class_methods ();
7667 java_parse_abort_on_error ();
7668 }
7669
7670 /* Expand methods in the current set of classes rememebered for
7671 generation. */
7672
7673 static void
7674 java_complete_expand_classes ()
7675 {
7676 tree current;
7677
7678 do_not_fold = flag_emit_xref;
7679
7680 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7681 if (!INNER_CLASS_DECL_P (current))
7682 java_complete_expand_class (current);
7683 }
7684
7685 /* Expand the methods found in OUTER, starting first by OUTER's inner
7686 classes, if any. */
7687
7688 static void
7689 java_complete_expand_class (outer)
7690 tree outer;
7691 {
7692 tree inner_list;
7693
7694 set_nested_class_simple_name_value (outer, 1); /* Set */
7695
7696 /* We need to go after all inner classes and start expanding them,
7697 starting with most nested ones. We have to do that because nested
7698 classes might add functions to outer classes */
7699
7700 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7701 inner_list; inner_list = TREE_CHAIN (inner_list))
7702 java_complete_expand_class (TREE_PURPOSE (inner_list));
7703
7704 java_complete_expand_methods (outer);
7705 set_nested_class_simple_name_value (outer, 0); /* Reset */
7706 }
7707
7708 /* Expand methods registered in CLASS_DECL. The general idea is that
7709 we expand regular methods first. This allows us get an estimate on
7710 how outer context local alias fields are really used so we can add
7711 to the constructor just enough code to initialize them properly (it
7712 also lets us generate finit$ correctly.) Then we expand the
7713 constructors and then <clinit>. */
7714
7715 static void
7716 java_complete_expand_methods (class_decl)
7717 tree class_decl;
7718 {
7719 tree clinit, decl, first_decl;
7720
7721 current_class = TREE_TYPE (class_decl);
7722
7723 /* Initialize a new constant pool */
7724 init_outgoing_cpool ();
7725
7726 /* Pre-expand <clinit> to figure whether we really need it or
7727 not. If we do need it, we pre-expand the static fields so they're
7728 ready to be used somewhere else. <clinit> will be fully expanded
7729 after we processed the constructors. */
7730 first_decl = TYPE_METHODS (current_class);
7731 clinit = maybe_generate_pre_expand_clinit (current_class);
7732
7733 /* Then generate finit$ (if we need to) because constructors will
7734 try to use it.*/
7735 if (TYPE_FINIT_STMT_LIST (current_class))
7736 java_complete_expand_method (generate_finit (current_class));
7737
7738 /* Then generate instinit$ (if we need to) because constructors will
7739 try to use it. */
7740 if (TYPE_II_STMT_LIST (current_class))
7741 java_complete_expand_method (generate_instinit (current_class));
7742
7743 /* Now do the constructors */
7744 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7745 {
7746 int no_body;
7747
7748 if (!DECL_CONSTRUCTOR_P (decl))
7749 continue;
7750
7751 no_body = !DECL_FUNCTION_BODY (decl);
7752 /* Don't generate debug info on line zero when expanding a
7753 generated constructor. */
7754 if (no_body)
7755 restore_line_number_status (1);
7756
7757 java_complete_expand_method (decl);
7758
7759 if (no_body)
7760 restore_line_number_status (0);
7761 }
7762
7763 /* First, do the ordinary methods. */
7764 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7765 {
7766 /* Ctors aren't part of this batch. */
7767 if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7768 continue;
7769
7770 /* Skip abstract or native methods -- but do handle native
7771 methods when generating JNI stubs. */
7772 if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7773 {
7774 DECL_FUNCTION_BODY (decl) = NULL_TREE;
7775 continue;
7776 }
7777
7778 if (METHOD_NATIVE (decl))
7779 {
7780 tree body;
7781 current_function_decl = decl;
7782 body = build_jni_stub (decl);
7783 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7784 }
7785
7786 java_complete_expand_method (decl);
7787 }
7788
7789 /* If there is indeed a <clinit>, fully expand it now */
7790 if (clinit)
7791 {
7792 /* Prevent the use of `this' inside <clinit> */
7793 ctxp->explicit_constructor_p = 1;
7794 java_complete_expand_method (clinit);
7795 ctxp->explicit_constructor_p = 0;
7796 }
7797
7798 /* We might have generated a class$ that we now want to expand */
7799 if (TYPE_DOT_CLASS (current_class))
7800 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7801
7802 /* Now verify constructor circularity (stop after the first one we
7803 prove wrong.) */
7804 if (!CLASS_INTERFACE (class_decl))
7805 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7806 if (DECL_CONSTRUCTOR_P (decl)
7807 && verify_constructor_circularity (decl, decl))
7808 break;
7809
7810 /* Save the constant pool. We'll need to restore it later. */
7811 TYPE_CPOOL (current_class) = outgoing_cpool;
7812 }
7813
7814 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7815 safely used in some other methods/constructors. */
7816
7817 static tree
7818 maybe_generate_pre_expand_clinit (class_type)
7819 tree class_type;
7820 {
7821 tree current, mdecl;
7822
7823 if (!TYPE_CLINIT_STMT_LIST (class_type))
7824 return NULL_TREE;
7825
7826 /* Go through all static fields and pre expand them */
7827 for (current = TYPE_FIELDS (class_type); current;
7828 current = TREE_CHAIN (current))
7829 if (FIELD_STATIC (current))
7830 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7831
7832 /* Then build the <clinit> method */
7833 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7834 clinit_identifier_node, end_params_node);
7835 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7836 mdecl, NULL_TREE);
7837 start_artificial_method_body (mdecl);
7838
7839 /* We process the list of assignment we produced as the result of
7840 the declaration of initialized static field and add them as
7841 statement to the <clinit> method. */
7842 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7843 current = TREE_CHAIN (current))
7844 {
7845 tree stmt = current;
7846 /* We build the assignment expression that will initialize the
7847 field to its value. There are strict rules on static
7848 initializers (8.5). FIXME */
7849 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7850 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7851 java_method_add_stmt (mdecl, stmt);
7852 }
7853
7854 end_artificial_method_body (mdecl);
7855
7856 /* Now we want to place <clinit> as the last method (because we need
7857 it at least for interface so that it doesn't interfere with the
7858 dispatch table based lookup. */
7859 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7860 {
7861 current = TREE_CHAIN (TYPE_METHODS (class_type));
7862 TYPE_METHODS (class_type) = current;
7863
7864 while (TREE_CHAIN (current))
7865 current = TREE_CHAIN (current);
7866
7867 TREE_CHAIN (current) = mdecl;
7868 TREE_CHAIN (mdecl) = NULL_TREE;
7869 }
7870
7871 return mdecl;
7872 }
7873
7874 /* Analyzes a method body and look for something that isn't a
7875 MODIFY_EXPR with a constant value. */
7876
7877 static int
7878 analyze_clinit_body (this_class, bbody)
7879 tree this_class, bbody;
7880 {
7881 while (bbody)
7882 switch (TREE_CODE (bbody))
7883 {
7884 case BLOCK:
7885 bbody = BLOCK_EXPR_BODY (bbody);
7886 break;
7887
7888 case EXPR_WITH_FILE_LOCATION:
7889 bbody = EXPR_WFL_NODE (bbody);
7890 break;
7891
7892 case COMPOUND_EXPR:
7893 if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
7894 return 1;
7895 bbody = TREE_OPERAND (bbody, 1);
7896 break;
7897
7898 case MODIFY_EXPR:
7899 /* If we're generating to class file and we're dealing with an
7900 array initialization, we return 1 to keep <clinit> */
7901 if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
7902 && flag_emit_class_files)
7903 return 1;
7904
7905 /* There are a few cases where we're required to keep
7906 <clinit>:
7907 - If this is an assignment whose operand is not constant,
7908 - If this is an assignment to a non-initialized field,
7909 - If this field is not a member of the current class.
7910 */
7911 return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
7912 || ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
7913 || DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
7914
7915 default:
7916 return 1;
7917 }
7918 return 0;
7919 }
7920
7921
7922 /* See whether we could get rid of <clinit>. Criteria are: all static
7923 final fields have constant initial values and the body of <clinit>
7924 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7925
7926 static int
7927 maybe_yank_clinit (mdecl)
7928 tree mdecl;
7929 {
7930 tree type, current;
7931 tree fbody, bbody;
7932
7933 if (!DECL_CLINIT_P (mdecl))
7934 return 0;
7935
7936 /* If the body isn't empty, then we keep <clinit>. Note that if
7937 we're emitting classfiles, this isn't enough not to rule it
7938 out. */
7939 fbody = DECL_FUNCTION_BODY (mdecl);
7940 bbody = BLOCK_EXPR_BODY (fbody);
7941 if (bbody && bbody != error_mark_node)
7942 bbody = BLOCK_EXPR_BODY (bbody);
7943 else
7944 return 0;
7945 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7946 return 0;
7947
7948 type = DECL_CONTEXT (mdecl);
7949 current = TYPE_FIELDS (type);
7950
7951 for (current = (current ? TREE_CHAIN (current) : current);
7952 current; current = TREE_CHAIN (current))
7953 {
7954 tree f_init;
7955
7956 /* We're not interested in non-static fields. */
7957 if (!FIELD_STATIC (current))
7958 continue;
7959
7960 /* Nor in fields without initializers. */
7961 f_init = DECL_INITIAL (current);
7962 if (f_init == NULL_TREE)
7963 continue;
7964
7965 /* Anything that isn't String or a basic type is ruled out -- or
7966 if we know how to deal with it (when doing things natively) we
7967 should generated an empty <clinit> so that SUID are computed
7968 correctly. */
7969 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7970 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7971 return 0;
7972
7973 if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
7974 return 0;
7975 }
7976
7977 /* Now we analyze the method body and look for something that
7978 isn't a MODIFY_EXPR */
7979 if (bbody != empty_stmt_node && analyze_clinit_body (type, bbody))
7980 return 0;
7981
7982 /* Get rid of <clinit> in the class' list of methods */
7983 if (TYPE_METHODS (type) == mdecl)
7984 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7985 else
7986 for (current = TYPE_METHODS (type); current;
7987 current = TREE_CHAIN (current))
7988 if (TREE_CHAIN (current) == mdecl)
7989 {
7990 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7991 break;
7992 }
7993
7994 return 1;
7995 }
7996
7997 /* Install the argument from MDECL. Suitable to completion and
7998 expansion of mdecl's body. */
7999
8000 static void
8001 start_complete_expand_method (mdecl)
8002 tree mdecl;
8003 {
8004 tree tem;
8005
8006 pushlevel (1); /* Prepare for a parameter push */
8007 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
8008 DECL_ARGUMENTS (mdecl) = tem;
8009
8010 for (; tem; tem = TREE_CHAIN (tem))
8011 {
8012 /* TREE_CHAIN (tem) will change after pushdecl. */
8013 tree next = TREE_CHAIN (tem);
8014 tree type = TREE_TYPE (tem);
8015 if (PROMOTE_PROTOTYPES
8016 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
8017 && INTEGRAL_TYPE_P (type))
8018 type = integer_type_node;
8019 DECL_ARG_TYPE (tem) = type;
8020 layout_decl (tem, 0);
8021 pushdecl (tem);
8022 /* Re-install the next so that the list is kept and the loop
8023 advances. */
8024 TREE_CHAIN (tem) = next;
8025 }
8026 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8027 lineno = DECL_SOURCE_LINE_FIRST (mdecl);
8028 build_result_decl (mdecl);
8029 }
8030
8031
8032 /* Complete and expand a method. */
8033
8034 static void
8035 java_complete_expand_method (mdecl)
8036 tree mdecl;
8037 {
8038 tree fbody, block_body, exception_copy;
8039
8040 current_function_decl = mdecl;
8041 /* Fix constructors before expanding them */
8042 if (DECL_CONSTRUCTOR_P (mdecl))
8043 fix_constructors (mdecl);
8044
8045 /* Expand functions that have a body */
8046 if (!DECL_FUNCTION_BODY (mdecl))
8047 return;
8048
8049 fbody = DECL_FUNCTION_BODY (mdecl);
8050 block_body = BLOCK_EXPR_BODY (fbody);
8051 exception_copy = NULL_TREE;
8052
8053 current_function_decl = mdecl;
8054
8055 if (! quiet_flag)
8056 fprintf (stderr, " [%s.",
8057 lang_printable_name (DECL_CONTEXT (mdecl), 0));
8058 announce_function (mdecl);
8059 if (! quiet_flag)
8060 fprintf (stderr, "]");
8061
8062 /* Prepare the function for tree completion */
8063 start_complete_expand_method (mdecl);
8064
8065 /* Install the current this */
8066 current_this = (!METHOD_STATIC (mdecl) ?
8067 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
8068
8069 /* Purge the `throws' list of unchecked exceptions (we save a copy
8070 of the list and re-install it later.) */
8071 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
8072 purge_unchecked_exceptions (mdecl);
8073
8074 /* Install exceptions thrown with `throws' */
8075 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
8076
8077 if (block_body != NULL_TREE)
8078 {
8079 block_body = java_complete_tree (block_body);
8080
8081 /* Before we check initialization, attached all class initialization
8082 variable to the block_body */
8083 htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8084 attach_init_test_initialization_flags, block_body);
8085
8086 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
8087 {
8088 check_for_initialization (block_body, mdecl);
8089
8090 /* Go through all the flags marking the initialization of
8091 static variables and see whether they're definitively
8092 assigned, in which case the type is remembered as
8093 definitively initialized in MDECL. */
8094 if (STATIC_CLASS_INIT_OPT_P ())
8095 {
8096 /* Always register the context as properly initialized in
8097 MDECL. This used with caution helps removing extra
8098 initialization of self. */
8099 if (METHOD_STATIC (mdecl))
8100 {
8101 *(htab_find_slot
8102 (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
8103 DECL_CONTEXT (mdecl), INSERT)) = DECL_CONTEXT (mdecl);
8104 }
8105 }
8106 }
8107 ctxp->explicit_constructor_p = 0;
8108 }
8109
8110 BLOCK_EXPR_BODY (fbody) = block_body;
8111
8112 /* If we saw a return but couldn't evaluate it properly, we'll have
8113 an error_mark_node here. */
8114 if (block_body != error_mark_node
8115 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
8116 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
8117 && !flag_emit_xref)
8118 missing_return_error (current_function_decl);
8119
8120 /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
8121 maybe_yank_clinit (mdecl);
8122
8123 /* Pop the current level, with special measures if we found errors. */
8124 if (java_error_count)
8125 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8126 poplevel (1, 0, 1);
8127
8128 /* Pop the exceptions and sanity check */
8129 POP_EXCEPTIONS();
8130 if (currently_caught_type_list)
8131 abort ();
8132
8133 /* Restore the copy of the list of exceptions if emitting xrefs. */
8134 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
8135 }
8136
8137 /* For with each class for which there's code to generate. */
8138
8139 static void
8140 java_expand_method_bodies (class)
8141 tree class;
8142 {
8143 tree decl;
8144 for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8145 {
8146 if (!DECL_FUNCTION_BODY (decl))
8147 continue;
8148
8149 current_function_decl = decl;
8150
8151 /* Save the function for inlining. */
8152 if (flag_inline_trees)
8153 DECL_SAVED_TREE (decl) =
8154 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
8155
8156 /* It's time to assign the variable flagging static class
8157 initialization based on which classes invoked static methods
8158 are definitely initializing. This should be flagged. */
8159 if (STATIC_CLASS_INIT_OPT_P ())
8160 {
8161 tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
8162 for (; list != NULL_TREE; list = TREE_CHAIN (list))
8163 {
8164 /* Executed for each statement calling a static function.
8165 LIST is a TREE_LIST whose PURPOSE is the called function
8166 and VALUE is a compound whose second operand can be patched
8167 with static class initialization flag assignments. */
8168
8169 tree called_method = TREE_PURPOSE (list);
8170 tree compound = TREE_VALUE (list);
8171 tree assignment_compound_list
8172 = build_tree_list (called_method, NULL);
8173
8174 /* For each class definitely initialized in
8175 CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8176 assignment to the class initialization flag. */
8177 htab_traverse (DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8178 emit_test_initialization,
8179 assignment_compound_list);
8180
8181 if (TREE_VALUE (assignment_compound_list))
8182 TREE_OPERAND (compound, 1)
8183 = TREE_VALUE (assignment_compound_list);
8184 }
8185 }
8186
8187 /* Prepare the function for RTL expansion */
8188 start_complete_expand_method (decl);
8189
8190 /* Expand function start, generate initialization flag
8191 assignment, and handle synchronized methods. */
8192 complete_start_java_method (decl);
8193
8194 /* Expand the rest of the function body and terminate
8195 expansion. */
8196 source_end_java_method ();
8197 }
8198 }
8199
8200 \f
8201
8202 /* This section of the code deals with accessing enclosing context
8203 fields either directly by using the relevant access to this$<n> or
8204 by invoking an access method crafted for that purpose. */
8205
8206 /* Build the necessary access from an inner class to an outer
8207 class. This routine could be optimized to cache previous result
8208 (decl, current_class and returned access). When an access method
8209 needs to be generated, it always takes the form of a read. It might
8210 be later turned into a write by calling outer_field_access_fix. */
8211
8212 static tree
8213 build_outer_field_access (id, decl)
8214 tree id, decl;
8215 {
8216 tree access = NULL_TREE;
8217 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8218 tree decl_ctx = DECL_CONTEXT (decl);
8219
8220 /* If the immediate enclosing context of the current class is the
8221 field decl's class or inherits from it; build the access as
8222 `this$<n>.<field>'. Note that we will break the `private' barrier
8223 if we're not emitting bytecodes. */
8224 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8225 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
8226 {
8227 tree thisn = build_current_thisn (current_class);
8228 access = make_qualified_primary (build_wfl_node (thisn),
8229 id, EXPR_WFL_LINECOL (id));
8230 }
8231 /* Otherwise, generate access methods to outer this and access the
8232 field (either using an access method or by direct access.) */
8233 else
8234 {
8235 int lc = EXPR_WFL_LINECOL (id);
8236
8237 /* Now we chain the required number of calls to the access$0 to
8238 get a hold to the enclosing instance we need, and then we
8239 build the field access. */
8240 access = build_access_to_thisn (current_class, decl_ctx, lc);
8241
8242 /* If the field is private and we're generating bytecode, then
8243 we generate an access method */
8244 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
8245 {
8246 tree name = build_outer_field_access_methods (decl);
8247 access = build_outer_field_access_expr (lc, decl_ctx,
8248 name, access, NULL_TREE);
8249 }
8250 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
8251 Once again we break the `private' access rule from a foreign
8252 class. */
8253 else
8254 access = make_qualified_primary (access, id, lc);
8255 }
8256 return resolve_expression_name (access, NULL);
8257 }
8258
8259 /* Return a nonzero value if NODE describes an outer field inner
8260 access. */
8261
8262 static int
8263 outer_field_access_p (type, decl)
8264 tree type, decl;
8265 {
8266 if (!INNER_CLASS_TYPE_P (type)
8267 || TREE_CODE (decl) != FIELD_DECL
8268 || DECL_CONTEXT (decl) == type)
8269 return 0;
8270
8271 /* If the inner class extends the declaration context of the field
8272 we're try to acces, then this isn't an outer field access */
8273 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8274 return 0;
8275
8276 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
8277 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
8278 {
8279 if (type == DECL_CONTEXT (decl))
8280 return 1;
8281
8282 if (!DECL_CONTEXT (TYPE_NAME (type)))
8283 {
8284 /* Before we give up, see whether the field is inherited from
8285 the enclosing context we're considering. */
8286 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8287 return 1;
8288 break;
8289 }
8290 }
8291
8292 return 0;
8293 }
8294
8295 /* Return a nonzero value if NODE represents an outer field inner
8296 access that was been already expanded. As a side effect, it returns
8297 the name of the field being accessed and the argument passed to the
8298 access function, suitable for a regeneration of the access method
8299 call if necessary. */
8300
8301 static int
8302 outer_field_expanded_access_p (node, name, arg_type, arg)
8303 tree node, *name, *arg_type, *arg;
8304 {
8305 int identified = 0;
8306
8307 if (TREE_CODE (node) != CALL_EXPR)
8308 return 0;
8309
8310 /* Well, gcj generates slightly different tree nodes when compiling
8311 to native or bytecodes. It's the case for function calls. */
8312
8313 if (flag_emit_class_files
8314 && TREE_CODE (node) == CALL_EXPR
8315 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8316 identified = 1;
8317 else if (!flag_emit_class_files)
8318 {
8319 node = TREE_OPERAND (node, 0);
8320
8321 if (node && TREE_OPERAND (node, 0)
8322 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8323 {
8324 node = TREE_OPERAND (node, 0);
8325 if (TREE_OPERAND (node, 0)
8326 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8327 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
8328 (DECL_NAME (TREE_OPERAND (node, 0)))))
8329 identified = 1;
8330 }
8331 }
8332
8333 if (identified && name && arg_type && arg)
8334 {
8335 tree argument = TREE_OPERAND (node, 1);
8336 *name = DECL_NAME (TREE_OPERAND (node, 0));
8337 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8338 *arg = TREE_VALUE (argument);
8339 }
8340 return identified;
8341 }
8342
8343 /* Detect in NODE an outer field read access from an inner class and
8344 transform it into a write with RHS as an argument. This function is
8345 called from the java_complete_lhs when an assignment to a LHS can
8346 be identified. */
8347
8348 static tree
8349 outer_field_access_fix (wfl, node, rhs)
8350 tree wfl, node, rhs;
8351 {
8352 tree name, arg_type, arg;
8353
8354 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
8355 {
8356 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
8357 arg_type, name, arg, rhs);
8358 return java_complete_tree (node);
8359 }
8360 return NULL_TREE;
8361 }
8362
8363 /* Construct the expression that calls an access method:
8364 <type>.access$<n>(<arg1> [, <arg2>]);
8365
8366 ARG2 can be NULL and will be omitted in that case. It will denote a
8367 read access. */
8368
8369 static tree
8370 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
8371 int lc;
8372 tree type, access_method_name, arg1, arg2;
8373 {
8374 tree args, cn, access;
8375
8376 args = arg1 ? arg1 :
8377 build_wfl_node (build_current_thisn (current_class));
8378 args = build_tree_list (NULL_TREE, args);
8379
8380 if (arg2)
8381 args = tree_cons (NULL_TREE, arg2, args);
8382
8383 access = build_method_invocation (build_wfl_node (access_method_name), args);
8384 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8385 return make_qualified_primary (cn, access, lc);
8386 }
8387
8388 static tree
8389 build_new_access_id ()
8390 {
8391 static int access_n_counter = 1;
8392 char buffer [128];
8393
8394 sprintf (buffer, "access$%d", access_n_counter++);
8395 return get_identifier (buffer);
8396 }
8397
8398 /* Create the static access functions for the outer field DECL. We define a
8399 read:
8400 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8401 return inst$.field;
8402 }
8403 and a write access:
8404 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8405 TREE_TYPE (<field>) value$) {
8406 return inst$.field = value$;
8407 }
8408 We should have a usage flags on the DECL so we can lazily turn the ones
8409 we're using for code generation. FIXME.
8410 */
8411
8412 static tree
8413 build_outer_field_access_methods (decl)
8414 tree decl;
8415 {
8416 tree id, args, stmt, mdecl;
8417
8418 if (FIELD_INNER_ACCESS_P (decl))
8419 return FIELD_INNER_ACCESS (decl);
8420
8421 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8422
8423 /* Create the identifier and a function named after it. */
8424 id = build_new_access_id ();
8425
8426 /* The identifier is marked as bearing the name of a generated write
8427 access function for outer field accessed from inner classes. */
8428 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8429
8430 /* Create the read access */
8431 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8432 TREE_CHAIN (args) = end_params_node;
8433 stmt = make_qualified_primary (build_wfl_node (inst_id),
8434 build_wfl_node (DECL_NAME (decl)), 0);
8435 stmt = build_return (0, stmt);
8436 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8437 TREE_TYPE (decl), id, args, stmt);
8438 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8439
8440 /* Create the write access method. No write access for final variable */
8441 if (!FIELD_FINAL (decl))
8442 {
8443 args = build_tree_list (inst_id,
8444 build_pointer_type (DECL_CONTEXT (decl)));
8445 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8446 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8447 stmt = make_qualified_primary (build_wfl_node (inst_id),
8448 build_wfl_node (DECL_NAME (decl)), 0);
8449 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8450 build_wfl_node (wpv_id)));
8451 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8452 TREE_TYPE (decl), id,
8453 args, stmt);
8454 }
8455 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8456
8457 /* Return the access name */
8458 return FIELD_INNER_ACCESS (decl) = id;
8459 }
8460
8461 /* Build an field access method NAME. */
8462
8463 static tree
8464 build_outer_field_access_method (class, type, name, args, body)
8465 tree class, type, name, args, body;
8466 {
8467 tree saved_current_function_decl, mdecl;
8468
8469 /* Create the method */
8470 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8471 fix_method_argument_names (args, mdecl);
8472 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8473
8474 /* Attach the method body. */
8475 saved_current_function_decl = current_function_decl;
8476 start_artificial_method_body (mdecl);
8477 java_method_add_stmt (mdecl, body);
8478 end_artificial_method_body (mdecl);
8479 current_function_decl = saved_current_function_decl;
8480
8481 return mdecl;
8482 }
8483
8484 \f
8485 /* This section deals with building access function necessary for
8486 certain kinds of method invocation from inner classes. */
8487
8488 static tree
8489 build_outer_method_access_method (decl)
8490 tree decl;
8491 {
8492 tree saved_current_function_decl, mdecl;
8493 tree args = NULL_TREE, call_args = NULL_TREE;
8494 tree carg, id, body, class;
8495 char buffer [80];
8496 int parm_id_count = 0;
8497
8498 /* Test this abort with an access to a private field */
8499 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8500 abort ();
8501
8502 /* Check the cache first */
8503 if (DECL_FUNCTION_INNER_ACCESS (decl))
8504 return DECL_FUNCTION_INNER_ACCESS (decl);
8505
8506 class = DECL_CONTEXT (decl);
8507
8508 /* Obtain an access identifier and mark it */
8509 id = build_new_access_id ();
8510 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8511
8512 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8513 /* Create the arguments, as much as the original */
8514 for (; carg && carg != end_params_node;
8515 carg = TREE_CHAIN (carg))
8516 {
8517 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8518 args = chainon (args, build_tree_list (get_identifier (buffer),
8519 TREE_VALUE (carg)));
8520 }
8521 args = chainon (args, end_params_node);
8522
8523 /* Create the method */
8524 mdecl = create_artificial_method (class, ACC_STATIC,
8525 TREE_TYPE (TREE_TYPE (decl)), id, args);
8526 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8527 /* There is a potential bug here. We should be able to use
8528 fix_method_argument_names, but then arg names get mixed up and
8529 eventually a constructor will have its this$0 altered and the
8530 outer context won't be assignment properly. The test case is
8531 stub.java FIXME */
8532 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8533
8534 /* Attach the method body. */
8535 saved_current_function_decl = current_function_decl;
8536 start_artificial_method_body (mdecl);
8537
8538 /* The actual method invocation uses the same args. When invoking a
8539 static methods that way, we don't want to skip the first
8540 argument. */
8541 carg = args;
8542 if (!METHOD_STATIC (decl))
8543 carg = TREE_CHAIN (carg);
8544 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8545 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8546 call_args);
8547
8548 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8549 call_args);
8550 if (!METHOD_STATIC (decl))
8551 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8552 body, 0);
8553 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8554 body = build_return (0, body);
8555 java_method_add_stmt (mdecl,body);
8556 end_artificial_method_body (mdecl);
8557 current_function_decl = saved_current_function_decl;
8558
8559 /* Back tag the access function so it know what it accesses */
8560 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8561
8562 /* Tag the current method so it knows it has an access generated */
8563 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8564 }
8565
8566 \f
8567 /* This section of the code deals with building expressions to access
8568 the enclosing instance of an inner class. The enclosing instance is
8569 kept in a generated field called this$<n>, with <n> being the
8570 inner class nesting level (starting from 0.) */
8571
8572 /* Build an access to a given this$<n>, always chaining access call to
8573 others. Access methods to this$<n> are build on the fly if
8574 necessary. This CAN'T be used to solely access this$<n-1> from
8575 this$<n> (which alway yield to special cases and optimization, see
8576 for example build_outer_field_access). */
8577
8578 static tree
8579 build_access_to_thisn (from, to, lc)
8580 tree from, to;
8581 int lc;
8582 {
8583 tree access = NULL_TREE;
8584
8585 while (from != to && PURE_INNER_CLASS_TYPE_P (from))
8586 {
8587 if (!access)
8588 {
8589 access = build_current_thisn (from);
8590 access = build_wfl_node (access);
8591 }
8592 else
8593 {
8594 tree access0_wfl, cn;
8595
8596 maybe_build_thisn_access_method (from);
8597 access0_wfl = build_wfl_node (access0_identifier_node);
8598 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8599 EXPR_WFL_LINECOL (access0_wfl) = lc;
8600 access = build_tree_list (NULL_TREE, access);
8601 access = build_method_invocation (access0_wfl, access);
8602 access = make_qualified_primary (cn, access, lc);
8603 }
8604
8605 /* If FROM isn't an inner class, that's fine, we've done enough.
8606 What we're looking for can be accessed from there. */
8607 from = DECL_CONTEXT (TYPE_NAME (from));
8608 if (!from)
8609 break;
8610 from = TREE_TYPE (from);
8611 }
8612 return access;
8613 }
8614
8615 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8616 is returned if nothing needs to be generated. Otherwise, the method
8617 generated and a method decl is returned.
8618
8619 NOTE: These generated methods should be declared in a class file
8620 attribute so that they can't be referred to directly. */
8621
8622 static tree
8623 maybe_build_thisn_access_method (type)
8624 tree type;
8625 {
8626 tree mdecl, args, stmt, rtype;
8627 tree saved_current_function_decl;
8628
8629 /* If TYPE is a top-level class, no access method is required.
8630 If there already is such an access method, bail out. */
8631 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8632 return NULL_TREE;
8633
8634 /* We generate the method. The method looks like:
8635 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8636 */
8637 args = build_tree_list (inst_id, build_pointer_type (type));
8638 TREE_CHAIN (args) = end_params_node;
8639 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8640 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8641 access0_identifier_node, args);
8642 fix_method_argument_names (args, mdecl);
8643 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8644 stmt = build_current_thisn (type);
8645 stmt = make_qualified_primary (build_wfl_node (inst_id),
8646 build_wfl_node (stmt), 0);
8647 stmt = build_return (0, stmt);
8648
8649 saved_current_function_decl = current_function_decl;
8650 start_artificial_method_body (mdecl);
8651 java_method_add_stmt (mdecl, stmt);
8652 end_artificial_method_body (mdecl);
8653 current_function_decl = saved_current_function_decl;
8654
8655 CLASS_ACCESS0_GENERATED_P (type) = 1;
8656
8657 return mdecl;
8658 }
8659
8660 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8661 the first level of innerclassing. this$1 for the next one, etc...
8662 This function can be invoked with TYPE to NULL, available and then
8663 has to count the parser context. */
8664
8665 static GTY(()) tree saved_thisn;
8666 static GTY(()) tree saved_type;
8667
8668 static tree
8669 build_current_thisn (type)
8670 tree type;
8671 {
8672 static int saved_i = -1;
8673 static int saved_type_i = 0;
8674 tree decl;
8675 char buffer [24];
8676 int i = 0;
8677
8678 if (type)
8679 {
8680 if (type == saved_type)
8681 i = saved_type_i;
8682 else
8683 {
8684 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8685 decl; decl = DECL_CONTEXT (decl), i++)
8686 ;
8687
8688 saved_type = type;
8689 saved_type_i = i;
8690 }
8691 }
8692 else
8693 i = list_length (GET_CPC_LIST ())-2;
8694
8695 if (i == saved_i)
8696 return saved_thisn;
8697
8698 sprintf (buffer, "this$%d", i);
8699 saved_i = i;
8700 saved_thisn = get_identifier (buffer);
8701 return saved_thisn;
8702 }
8703
8704 /* Return the assignement to the hidden enclosing context `this$<n>'
8705 by the second incoming parameter to the innerclass constructor. The
8706 form used is `this.this$<n> = this$<n>;'. */
8707
8708 static tree
8709 build_thisn_assign ()
8710 {
8711 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8712 {
8713 tree thisn = build_current_thisn (current_class);
8714 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8715 build_wfl_node (thisn), 0);
8716 tree rhs = build_wfl_node (thisn);
8717 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8718 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8719 }
8720 return NULL_TREE;
8721 }
8722
8723 \f
8724 /* Building the synthetic `class$' used to implement the `.class' 1.1
8725 extension for non primitive types. This method looks like:
8726
8727 static Class class$(String type) throws NoClassDefFoundError
8728 {
8729 try {return (java.lang.Class.forName (String));}
8730 catch (ClassNotFoundException e) {
8731 throw new NoClassDefFoundError(e.getMessage());}
8732 } */
8733
8734 static GTY(()) tree get_message_wfl;
8735 static GTY(()) tree type_parm_wfl;
8736
8737 static tree
8738 build_dot_class_method (class)
8739 tree class;
8740 {
8741 #define BWF(S) build_wfl_node (get_identifier ((S)))
8742 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8743 tree args, tmp, saved_current_function_decl, mdecl;
8744 tree stmt, throw_stmt;
8745
8746 if (!get_message_wfl)
8747 {
8748 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8749 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8750 }
8751
8752 /* Build the arguments */
8753 args = build_tree_list (get_identifier ("type$"),
8754 build_pointer_type (string_type_node));
8755 TREE_CHAIN (args) = end_params_node;
8756
8757 /* Build the qualified name java.lang.Class.forName */
8758 tmp = MQN (MQN (MQN (BWF ("java"),
8759 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8760 load_class (class_not_found_type_node, 1);
8761 load_class (no_class_def_found_type_node, 1);
8762
8763 /* Create the "class$" function */
8764 mdecl = create_artificial_method (class, ACC_STATIC,
8765 build_pointer_type (class_type_node),
8766 classdollar_identifier_node, args);
8767 DECL_FUNCTION_THROWS (mdecl) =
8768 build_tree_list (NULL_TREE, no_class_def_found_type_node);
8769
8770 /* We start by building the try block. We need to build:
8771 return (java.lang.Class.forName (type)); */
8772 stmt = build_method_invocation (tmp,
8773 build_tree_list (NULL_TREE, type_parm_wfl));
8774 stmt = build_return (0, stmt);
8775
8776 /* Now onto the catch block. We start by building the expression
8777 throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8778 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8779 get_message_wfl, 0);
8780 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8781
8782 /* Build new NoClassDefFoundError (_.getMessage) */
8783 throw_stmt = build_new_invocation
8784 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8785 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8786
8787 /* Build the throw, (it's too early to use BUILD_THROW) */
8788 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8789
8790 /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8791 stmt = encapsulate_with_try_catch (0, class_not_found_type_node,
8792 stmt, throw_stmt);
8793
8794 fix_method_argument_names (args, mdecl);
8795 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8796 saved_current_function_decl = current_function_decl;
8797 start_artificial_method_body (mdecl);
8798 java_method_add_stmt (mdecl, stmt);
8799 end_artificial_method_body (mdecl);
8800 current_function_decl = saved_current_function_decl;
8801 TYPE_DOT_CLASS (class) = mdecl;
8802
8803 return mdecl;
8804 }
8805
8806 static tree
8807 build_dot_class_method_invocation (type)
8808 tree type;
8809 {
8810 tree sig_id, s;
8811
8812 if (TYPE_ARRAY_P (type))
8813 sig_id = build_java_signature (type);
8814 else
8815 sig_id = DECL_NAME (TYPE_NAME (type));
8816
8817 /* Ensure that the proper name separator is used */
8818 sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
8819 IDENTIFIER_LENGTH (sig_id));
8820
8821 s = build_string (IDENTIFIER_LENGTH (sig_id),
8822 IDENTIFIER_POINTER (sig_id));
8823 return build_method_invocation (build_wfl_node (classdollar_identifier_node),
8824 build_tree_list (NULL_TREE, s));
8825 }
8826
8827 /* This section of the code deals with constructor. */
8828
8829 /* Craft a body for default constructor. Patch existing constructor
8830 bodies with call to super() and field initialization statements if
8831 necessary. */
8832
8833 static void
8834 fix_constructors (mdecl)
8835 tree mdecl;
8836 {
8837 tree iii; /* Instance Initializer Invocation */
8838 tree body = DECL_FUNCTION_BODY (mdecl);
8839 tree thisn_assign, compound = NULL_TREE;
8840 tree class_type = DECL_CONTEXT (mdecl);
8841
8842 if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
8843 return;
8844 DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
8845
8846 if (!body)
8847 {
8848 /* It is an error for the compiler to generate a default
8849 constructor if the superclass doesn't have a constructor that
8850 takes no argument, or the same args for an anonymous class */
8851 if (verify_constructor_super (mdecl))
8852 {
8853 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8854 tree save = DECL_NAME (mdecl);
8855 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8856 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8857 parse_error_context
8858 (lookup_cl (TYPE_NAME (class_type)),
8859 "No constructor matching `%s' found in class `%s'",
8860 lang_printable_name (mdecl, 0), n);
8861 DECL_NAME (mdecl) = save;
8862 }
8863
8864 /* The constructor body must be crafted by hand. It's the
8865 constructor we defined when we realize we didn't have the
8866 CLASSNAME() constructor */
8867 start_artificial_method_body (mdecl);
8868
8869 /* Insert an assignment to the this$<n> hidden field, if
8870 necessary */
8871 if ((thisn_assign = build_thisn_assign ()))
8872 java_method_add_stmt (mdecl, thisn_assign);
8873
8874 /* We don't generate a super constructor invocation if we're
8875 compiling java.lang.Object. build_super_invocation takes care
8876 of that. */
8877 java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8878
8879 /* FIXME */
8880 if ((iii = build_instinit_invocation (class_type)))
8881 java_method_add_stmt (mdecl, iii);
8882
8883 end_artificial_method_body (mdecl);
8884 }
8885 /* Search for an explicit constructor invocation */
8886 else
8887 {
8888 int found = 0;
8889 int invokes_this = 0;
8890 tree found_call = NULL_TREE;
8891 tree main_block = BLOCK_EXPR_BODY (body);
8892
8893 while (body)
8894 switch (TREE_CODE (body))
8895 {
8896 case CALL_EXPR:
8897 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8898 if (CALL_THIS_CONSTRUCTOR_P (body))
8899 invokes_this = 1;
8900 body = NULL_TREE;
8901 break;
8902 case COMPOUND_EXPR:
8903 case EXPR_WITH_FILE_LOCATION:
8904 found_call = body;
8905 body = TREE_OPERAND (body, 0);
8906 break;
8907 case BLOCK:
8908 found_call = body;
8909 body = BLOCK_EXPR_BODY (body);
8910 break;
8911 default:
8912 found = 0;
8913 body = NULL_TREE;
8914 }
8915
8916 /* Generate the assignment to this$<n>, if necessary */
8917 if ((thisn_assign = build_thisn_assign ()))
8918 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8919
8920 /* The constructor is missing an invocation of super() */
8921 if (!found)
8922 compound = add_stmt_to_compound (compound, NULL_TREE,
8923 build_super_invocation (mdecl));
8924 /* Explicit super() invokation should take place before the
8925 instance initializer blocks. */
8926 else
8927 {
8928 compound = add_stmt_to_compound (compound, NULL_TREE,
8929 TREE_OPERAND (found_call, 0));
8930 TREE_OPERAND (found_call, 0) = empty_stmt_node;
8931 }
8932
8933 DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
8934
8935 /* Insert the instance initializer block right after. */
8936 if (!invokes_this && (iii = build_instinit_invocation (class_type)))
8937 compound = add_stmt_to_compound (compound, NULL_TREE, iii);
8938
8939 /* Fix the constructor main block if we're adding extra stmts */
8940 if (compound)
8941 {
8942 compound = add_stmt_to_compound (compound, NULL_TREE,
8943 BLOCK_EXPR_BODY (main_block));
8944 BLOCK_EXPR_BODY (main_block) = compound;
8945 }
8946 }
8947 }
8948
8949 /* Browse constructors in the super class, searching for a constructor
8950 that doesn't take any argument. Return 0 if one is found, 1
8951 otherwise. If the current class is an anonymous inner class, look
8952 for something that has the same signature. */
8953
8954 static int
8955 verify_constructor_super (mdecl)
8956 tree mdecl;
8957 {
8958 tree class = CLASSTYPE_SUPER (current_class);
8959 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8960 tree sdecl;
8961
8962 if (!class)
8963 return 0;
8964
8965 if (ANONYMOUS_CLASS_P (current_class))
8966 {
8967 tree mdecl_arg_type;
8968 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8969 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8970 if (DECL_CONSTRUCTOR_P (sdecl))
8971 {
8972 tree m_arg_type;
8973 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8974 if (super_inner)
8975 arg_type = TREE_CHAIN (arg_type);
8976 for (m_arg_type = mdecl_arg_type;
8977 (arg_type != end_params_node
8978 && m_arg_type != end_params_node);
8979 arg_type = TREE_CHAIN (arg_type),
8980 m_arg_type = TREE_CHAIN (m_arg_type))
8981 if (!valid_method_invocation_conversion_p
8982 (TREE_VALUE (arg_type),
8983 TREE_VALUE (m_arg_type)))
8984 break;
8985
8986 if (arg_type == end_params_node && m_arg_type == end_params_node)
8987 return 0;
8988 }
8989 }
8990 else
8991 {
8992 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8993 {
8994 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8995 if (super_inner)
8996 arg = TREE_CHAIN (arg);
8997 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8998 return 0;
8999 }
9000 }
9001 return 1;
9002 }
9003
9004 /* Generate code for all context remembered for code generation. */
9005
9006 static GTY(()) tree reversed_class_list;
9007 void
9008 java_expand_classes ()
9009 {
9010 int save_error_count = 0;
9011 static struct parser_ctxt *cur_ctxp = NULL;
9012
9013 java_parse_abort_on_error ();
9014 if (!(ctxp = ctxp_for_generation))
9015 return;
9016 java_layout_classes ();
9017 java_parse_abort_on_error ();
9018
9019 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9020 {
9021 ctxp = cur_ctxp;
9022 input_filename = ctxp->filename;
9023 lang_init_source (2); /* Error msgs have method prototypes */
9024 java_complete_expand_classes (); /* Complete and expand classes */
9025 java_parse_abort_on_error ();
9026 }
9027 input_filename = main_input_filename;
9028
9029
9030 /* Find anonymous classes and expand their constructor. This extra pass is
9031 neccessary because the constructor itself is only generated when the
9032 method in which it is defined is expanded. */
9033 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9034 {
9035 tree current;
9036 ctxp = cur_ctxp;
9037 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9038 {
9039 current_class = TREE_TYPE (current);
9040 if (ANONYMOUS_CLASS_P (current_class))
9041 {
9042 tree d;
9043 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
9044 {
9045 if (DECL_CONSTRUCTOR_P (d))
9046 {
9047 restore_line_number_status (1);
9048 java_complete_expand_method (d);
9049 restore_line_number_status (0);
9050 break; /* There is only one constructor. */
9051 }
9052 }
9053 }
9054 }
9055 }
9056
9057 /* If we've found error at that stage, don't try to generate
9058 anything, unless we're emitting xrefs or checking the syntax only
9059 (but not using -fsyntax-only for the purpose of generating
9060 bytecode. */
9061 if (java_error_count && !flag_emit_xref
9062 && (!flag_syntax_only && !flag_emit_class_files))
9063 return;
9064
9065 /* Now things are stable, go for generation of the class data. */
9066
9067 /* We pessimistically marked all fields external until we knew
9068 what set of classes we were planning to compile. Now mark
9069 those that will be generated locally as not external. */
9070 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9071 {
9072 tree current;
9073 ctxp = cur_ctxp;
9074 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9075 {
9076 tree class = TREE_TYPE (current);
9077 tree field;
9078 for (field = TYPE_FIELDS (class); field ; field = TREE_CHAIN (field))
9079 if (FIELD_STATIC (field))
9080 DECL_EXTERNAL (field) = 0;
9081 }
9082 }
9083
9084 /* Compile the classes. */
9085 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9086 {
9087 tree current;
9088 reversed_class_list = NULL;
9089
9090 ctxp = cur_ctxp;
9091
9092 /* We write out the classes in reverse order. This ensures that
9093 inner classes are written before their containing classes,
9094 which is important for parallel builds. Otherwise, the
9095 class file for the outer class may be found, but the class
9096 file for the inner class may not be present. In that
9097 situation, the compiler cannot fall back to the original
9098 source, having already read the outer class, so we must
9099 prevent that situation. */
9100 for (current = ctxp->class_list;
9101 current;
9102 current = TREE_CHAIN (current))
9103 reversed_class_list
9104 = tree_cons (NULL_TREE, current, reversed_class_list);
9105
9106 for (current = reversed_class_list;
9107 current;
9108 current = TREE_CHAIN (current))
9109 {
9110 current_class = TREE_TYPE (TREE_VALUE (current));
9111 outgoing_cpool = TYPE_CPOOL (current_class);
9112 if (flag_emit_class_files)
9113 write_classfile (current_class);
9114 if (flag_emit_xref)
9115 expand_xref (current_class);
9116 else if (! flag_syntax_only)
9117 {
9118 java_expand_method_bodies (current_class);
9119 finish_class ();
9120 }
9121 }
9122 }
9123 }
9124
9125 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9126 a tree list node containing RIGHT. Fore coming RIGHTs will be
9127 chained to this hook. LOCATION contains the location of the
9128 separating `.' operator. */
9129
9130 static tree
9131 make_qualified_primary (primary, right, location)
9132 tree primary, right;
9133 int location;
9134 {
9135 tree wfl;
9136
9137 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9138 wfl = build_wfl_wrap (primary, location);
9139 else
9140 {
9141 wfl = primary;
9142 /* If wfl wasn't qualified, we build a first anchor */
9143 if (!EXPR_WFL_QUALIFICATION (wfl))
9144 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9145 }
9146
9147 /* And chain them */
9148 EXPR_WFL_LINECOL (right) = location;
9149 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9150 PRIMARY_P (wfl) = 1;
9151 return wfl;
9152 }
9153
9154 /* Simple merge of two name separated by a `.' */
9155
9156 static tree
9157 merge_qualified_name (left, right)
9158 tree left, right;
9159 {
9160 tree node;
9161 if (!left && !right)
9162 return NULL_TREE;
9163
9164 if (!left)
9165 return right;
9166
9167 if (!right)
9168 return left;
9169
9170 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9171 IDENTIFIER_LENGTH (left));
9172 obstack_1grow (&temporary_obstack, '.');
9173 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9174 IDENTIFIER_LENGTH (right));
9175 node = get_identifier (obstack_base (&temporary_obstack));
9176 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9177 QUALIFIED_P (node) = 1;
9178 return node;
9179 }
9180
9181 /* Merge the two parts of a qualified name into LEFT. Set the
9182 location information of the resulting node to LOCATION, usually
9183 inherited from the location information of the `.' operator. */
9184
9185 static tree
9186 make_qualified_name (left, right, location)
9187 tree left, right;
9188 int location;
9189 {
9190 #ifdef USE_COMPONENT_REF
9191 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
9192 EXPR_WFL_LINECOL (node) = location;
9193 return node;
9194 #else
9195 tree left_id = EXPR_WFL_NODE (left);
9196 tree right_id = EXPR_WFL_NODE (right);
9197 tree wfl, merge;
9198
9199 merge = merge_qualified_name (left_id, right_id);
9200
9201 /* Left wasn't qualified and is now qualified */
9202 if (!QUALIFIED_P (left_id))
9203 {
9204 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9205 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9206 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9207 }
9208
9209 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9210 EXPR_WFL_LINECOL (wfl) = location;
9211 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9212
9213 EXPR_WFL_NODE (left) = merge;
9214 return left;
9215 #endif
9216 }
9217
9218 /* Extract the last identifier component of the qualified in WFL. The
9219 last identifier is removed from the linked list */
9220
9221 static tree
9222 cut_identifier_in_qualified (wfl)
9223 tree wfl;
9224 {
9225 tree q;
9226 tree previous = NULL_TREE;
9227 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9228 if (!TREE_CHAIN (q))
9229 {
9230 if (!previous)
9231 /* Operating on a non qualified qualified WFL. */
9232 abort ();
9233
9234 TREE_CHAIN (previous) = NULL_TREE;
9235 return TREE_PURPOSE (q);
9236 }
9237 }
9238
9239 /* Resolve the expression name NAME. Return its decl. */
9240
9241 static tree
9242 resolve_expression_name (id, orig)
9243 tree id;
9244 tree *orig;
9245 {
9246 tree name = EXPR_WFL_NODE (id);
9247 tree decl;
9248
9249 /* 6.5.5.1: Simple expression names */
9250 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9251 {
9252 /* 15.13.1: NAME can appear within the scope of a local variable
9253 declaration */
9254 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9255 return decl;
9256
9257 /* 15.13.1: NAME can appear within a class declaration */
9258 else
9259 {
9260 decl = lookup_field_wrapper (current_class, name);
9261 if (decl)
9262 {
9263 tree access = NULL_TREE;
9264 int fs = FIELD_STATIC (decl);
9265
9266 /* If we're accessing an outer scope local alias, make
9267 sure we change the name of the field we're going to
9268 build access to. */
9269 if (FIELD_LOCAL_ALIAS_USED (decl))
9270 name = DECL_NAME (decl);
9271
9272 /* Instance variable (8.3.1.1) can't appear within
9273 static method, static initializer or initializer for
9274 a static variable. */
9275 if (!fs && METHOD_STATIC (current_function_decl))
9276 {
9277 static_ref_err (id, name, current_class);
9278 return error_mark_node;
9279 }
9280 /* Instance variables can't appear as an argument of
9281 an explicit constructor invocation */
9282 if (!fs && ctxp->explicit_constructor_p
9283 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9284 {
9285 parse_error_context
9286 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9287 return error_mark_node;
9288 }
9289
9290 /* If we're processing an inner class and we're trying
9291 to access a field belonging to an outer class, build
9292 the access to the field */
9293 if (!fs && outer_field_access_p (current_class, decl))
9294 {
9295 if (CLASS_STATIC (TYPE_NAME (current_class)))
9296 {
9297 static_ref_err (id, DECL_NAME (decl), current_class);
9298 return error_mark_node;
9299 }
9300 access = build_outer_field_access (id, decl);
9301 if (orig)
9302 *orig = access;
9303 return access;
9304 }
9305
9306 /* Otherwise build what it takes to access the field */
9307 access = build_field_ref ((fs ? NULL_TREE : current_this),
9308 DECL_CONTEXT (decl), name);
9309 if (fs)
9310 access = maybe_build_class_init_for_field (decl, access);
9311 /* We may be asked to save the real field access node */
9312 if (orig)
9313 *orig = access;
9314 /* And we return what we got */
9315 return access;
9316 }
9317 /* Fall down to error report on undefined variable */
9318 }
9319 }
9320 /* 6.5.5.2 Qualified Expression Names */
9321 else
9322 {
9323 if (orig)
9324 *orig = NULL_TREE;
9325 qualify_ambiguous_name (id);
9326 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9327 /* 15.10.2: Accessing Superclass Members using super */
9328 return resolve_field_access (id, orig, NULL);
9329 }
9330
9331 /* We've got an error here */
9332 if (INNER_CLASS_TYPE_P (current_class))
9333 parse_error_context (id,
9334 "Local variable `%s' can't be accessed from within the inner class `%s' unless it is declared final",
9335 IDENTIFIER_POINTER (name),
9336 IDENTIFIER_POINTER (DECL_NAME
9337 (TYPE_NAME (current_class))));
9338 else
9339 parse_error_context (id, "Undefined variable `%s'",
9340 IDENTIFIER_POINTER (name));
9341
9342 return error_mark_node;
9343 }
9344
9345 static void
9346 static_ref_err (wfl, field_id, class_type)
9347 tree wfl, field_id, class_type;
9348 {
9349 parse_error_context
9350 (wfl,
9351 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
9352 IDENTIFIER_POINTER (field_id),
9353 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9354 }
9355
9356 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9357 We return something suitable to generate the field access. We also
9358 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
9359 recipient's address can be null. */
9360
9361 static tree
9362 resolve_field_access (qual_wfl, field_decl, field_type)
9363 tree qual_wfl;
9364 tree *field_decl, *field_type;
9365 {
9366 int is_static = 0;
9367 tree field_ref;
9368 tree decl, where_found, type_found;
9369
9370 if (resolve_qualified_expression_name (qual_wfl, &decl,
9371 &where_found, &type_found))
9372 return error_mark_node;
9373
9374 /* Resolve the LENGTH field of an array here */
9375 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9376 && type_found && TYPE_ARRAY_P (type_found)
9377 && ! flag_emit_class_files && ! flag_emit_xref)
9378 {
9379 tree length = build_java_array_length_access (where_found);
9380 field_ref = length;
9381
9382 /* In case we're dealing with a static array, we need to
9383 initialize its class before the array length can be fetched.
9384 It's also a good time to create a DECL_RTL for the field if
9385 none already exists, otherwise if the field was declared in a
9386 class found in an external file and hasn't been (and won't
9387 be) accessed for its value, none will be created. */
9388 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9389 {
9390 build_static_field_ref (where_found);
9391 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9392 }
9393 }
9394 /* We might have been trying to resolve field.method(). In which
9395 case, the resolution is over and decl is the answer */
9396 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9397 field_ref = decl;
9398 else if (JDECL_P (decl))
9399 {
9400 if (!type_found)
9401 type_found = DECL_CONTEXT (decl);
9402 is_static = FIELD_STATIC (decl);
9403 field_ref = build_field_ref ((is_static && !flag_emit_xref?
9404 NULL_TREE : where_found),
9405 type_found, DECL_NAME (decl));
9406 if (field_ref == error_mark_node)
9407 return error_mark_node;
9408 if (is_static)
9409 field_ref = maybe_build_class_init_for_field (decl, field_ref);
9410 }
9411 else
9412 field_ref = decl;
9413
9414 if (field_decl)
9415 *field_decl = decl;
9416 if (field_type)
9417 *field_type = (QUAL_DECL_TYPE (decl) ?
9418 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9419 return field_ref;
9420 }
9421
9422 /* If NODE is an access to f static field, strip out the class
9423 initialization part and return the field decl, otherwise, return
9424 NODE. */
9425
9426 static tree
9427 strip_out_static_field_access_decl (node)
9428 tree node;
9429 {
9430 if (TREE_CODE (node) == COMPOUND_EXPR)
9431 {
9432 tree op1 = TREE_OPERAND (node, 1);
9433 if (TREE_CODE (op1) == COMPOUND_EXPR)
9434 {
9435 tree call = TREE_OPERAND (op1, 0);
9436 if (TREE_CODE (call) == CALL_EXPR
9437 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9438 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9439 == soft_initclass_node)
9440 return TREE_OPERAND (op1, 1);
9441 }
9442 else if (JDECL_P (op1))
9443 return op1;
9444 }
9445 return node;
9446 }
9447
9448 /* 6.5.5.2: Qualified Expression Names */
9449
9450 static int
9451 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9452 tree wfl;
9453 tree *found_decl, *type_found, *where_found;
9454 {
9455 int from_type = 0; /* Field search initiated from a type */
9456 int from_super = 0, from_cast = 0, from_qualified_this = 0;
9457 int previous_call_static = 0;
9458 int is_static;
9459 tree decl = NULL_TREE, type = NULL_TREE, q;
9460 /* For certain for of inner class instantiation */
9461 tree saved_current, saved_this;
9462 #define RESTORE_THIS_AND_CURRENT_CLASS \
9463 { current_class = saved_current; current_this = saved_this;}
9464
9465 *type_found = *where_found = NULL_TREE;
9466
9467 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9468 {
9469 tree qual_wfl = QUAL_WFL (q);
9470 tree ret_decl; /* for EH checking */
9471 int location; /* for EH checking */
9472
9473 /* 15.10.1 Field Access Using a Primary */
9474 switch (TREE_CODE (qual_wfl))
9475 {
9476 case CALL_EXPR:
9477 case NEW_CLASS_EXPR:
9478 /* If the access to the function call is a non static field,
9479 build the code to access it. */
9480 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9481 {
9482 decl = maybe_access_field (decl, *where_found,
9483 DECL_CONTEXT (decl));
9484 if (decl == error_mark_node)
9485 return 1;
9486 }
9487
9488 /* And code for the function call */
9489 if (complete_function_arguments (qual_wfl))
9490 return 1;
9491
9492 /* We might have to setup a new current class and a new this
9493 for the search of an inner class, relative to the type of
9494 a expression resolved as `decl'. The current values are
9495 saved and restored shortly after */
9496 saved_current = current_class;
9497 saved_this = current_this;
9498 if (decl
9499 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9500 || from_qualified_this))
9501 {
9502 /* If we still have `from_qualified_this', we have the form
9503 <T>.this.f() and we need to build <T>.this */
9504 if (from_qualified_this)
9505 {
9506 decl = build_access_to_thisn (current_class, type, 0);
9507 decl = java_complete_tree (decl);
9508 type = TREE_TYPE (TREE_TYPE (decl));
9509 }
9510 current_class = type;
9511 current_this = decl;
9512 from_qualified_this = 0;
9513 }
9514
9515 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9516 CALL_USING_SUPER (qual_wfl) = 1;
9517 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9518 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9519 *where_found = patch_method_invocation (qual_wfl, decl, type,
9520 from_super,
9521 &is_static, &ret_decl);
9522 from_super = 0;
9523 if (*where_found == error_mark_node)
9524 {
9525 RESTORE_THIS_AND_CURRENT_CLASS;
9526 return 1;
9527 }
9528 *type_found = type = QUAL_DECL_TYPE (*where_found);
9529
9530 *where_found = force_evaluation_order (*where_found);
9531
9532 /* If we're creating an inner class instance, check for that
9533 an enclosing instance is in scope */
9534 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9535 && INNER_ENCLOSING_SCOPE_CHECK (type))
9536 {
9537 parse_error_context
9538 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9539 lang_printable_name (type, 0),
9540 (!current_this ? "" :
9541 "; an explicit one must be provided when creating this inner class"));
9542 RESTORE_THIS_AND_CURRENT_CLASS;
9543 return 1;
9544 }
9545
9546 /* In case we had to change then to resolve a inner class
9547 instantiation using a primary qualified by a `new' */
9548 RESTORE_THIS_AND_CURRENT_CLASS;
9549
9550 /* EH check. No check on access$<n> functions */
9551 if (location
9552 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9553 (DECL_NAME (current_function_decl)))
9554 check_thrown_exceptions (location, ret_decl);
9555
9556 /* If the previous call was static and this one is too,
9557 build a compound expression to hold the two (because in
9558 that case, previous function calls aren't transported as
9559 forcoming function's argument. */
9560 if (previous_call_static && is_static)
9561 {
9562 decl = build (COMPOUND_EXPR, TREE_TYPE (*where_found),
9563 decl, *where_found);
9564 TREE_SIDE_EFFECTS (decl) = 1;
9565 }
9566 else
9567 {
9568 previous_call_static = is_static;
9569 decl = *where_found;
9570 }
9571 from_type = 0;
9572 continue;
9573
9574 case NEW_ARRAY_EXPR:
9575 case NEW_ANONYMOUS_ARRAY_EXPR:
9576 *where_found = decl = java_complete_tree (qual_wfl);
9577 if (decl == error_mark_node)
9578 return 1;
9579 *type_found = type = QUAL_DECL_TYPE (decl);
9580 continue;
9581
9582 case CONVERT_EXPR:
9583 *where_found = decl = java_complete_tree (qual_wfl);
9584 if (decl == error_mark_node)
9585 return 1;
9586 *type_found = type = QUAL_DECL_TYPE (decl);
9587 from_cast = 1;
9588 continue;
9589
9590 case CONDITIONAL_EXPR:
9591 case STRING_CST:
9592 case MODIFY_EXPR:
9593 *where_found = decl = java_complete_tree (qual_wfl);
9594 if (decl == error_mark_node)
9595 return 1;
9596 *type_found = type = QUAL_DECL_TYPE (decl);
9597 continue;
9598
9599 case ARRAY_REF:
9600 /* If the access to the function call is a non static field,
9601 build the code to access it. */
9602 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9603 {
9604 decl = maybe_access_field (decl, *where_found, type);
9605 if (decl == error_mark_node)
9606 return 1;
9607 }
9608 /* And code for the array reference expression */
9609 decl = java_complete_tree (qual_wfl);
9610 if (decl == error_mark_node)
9611 return 1;
9612 type = QUAL_DECL_TYPE (decl);
9613 continue;
9614
9615 case PLUS_EXPR:
9616 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9617 return 1;
9618 if ((type = patch_string (decl)))
9619 decl = type;
9620 *where_found = QUAL_RESOLUTION (q) = decl;
9621 *type_found = type = TREE_TYPE (decl);
9622 break;
9623
9624 case CLASS_LITERAL:
9625 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9626 return 1;
9627 *where_found = QUAL_RESOLUTION (q) = decl;
9628 *type_found = type = TREE_TYPE (decl);
9629 break;
9630
9631 default:
9632 /* Fix for -Wall Just go to the next statement. Don't
9633 continue */
9634 break;
9635 }
9636
9637 /* If we fall here, we weren't processing a (static) function call. */
9638 previous_call_static = 0;
9639
9640 /* It can be the keyword THIS */
9641 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9642 && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9643 {
9644 if (!current_this)
9645 {
9646 parse_error_context
9647 (wfl, "Keyword `this' used outside allowed context");
9648 return 1;
9649 }
9650 if (ctxp->explicit_constructor_p
9651 && type == current_class)
9652 {
9653 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9654 return 1;
9655 }
9656 /* We have to generate code for intermediate access */
9657 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9658 {
9659 *where_found = decl = current_this;
9660 *type_found = type = QUAL_DECL_TYPE (decl);
9661 }
9662 /* We're trying to access the this from somewhere else. Make sure
9663 it's allowed before doing so. */
9664 else
9665 {
9666 if (!enclosing_context_p (type, current_class))
9667 {
9668 char *p = xstrdup (lang_printable_name (type, 0));
9669 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9670 p, p,
9671 lang_printable_name (current_class, 0));
9672 free (p);
9673 return 1;
9674 }
9675 from_qualified_this = 1;
9676 /* If there's nothing else after that, we need to
9677 produce something now, otherwise, the section of the
9678 code that needs to produce <T>.this will generate
9679 what is necessary. */
9680 if (!TREE_CHAIN (q))
9681 {
9682 decl = build_access_to_thisn (current_class, type, 0);
9683 *where_found = decl = java_complete_tree (decl);
9684 *type_found = type = TREE_TYPE (decl);
9685 }
9686 }
9687
9688 from_type = 0;
9689 continue;
9690 }
9691
9692 /* 15.10.2 Accessing Superclass Members using SUPER */
9693 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9694 && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9695 {
9696 tree node;
9697 /* Check on the restricted use of SUPER */
9698 if (METHOD_STATIC (current_function_decl)
9699 || current_class == object_type_node)
9700 {
9701 parse_error_context
9702 (wfl, "Keyword `super' used outside allowed context");
9703 return 1;
9704 }
9705 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9706 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9707 CLASSTYPE_SUPER (current_class),
9708 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9709 *where_found = decl = java_complete_tree (node);
9710 if (decl == error_mark_node)
9711 return 1;
9712 *type_found = type = QUAL_DECL_TYPE (decl);
9713 from_super = from_type = 1;
9714 continue;
9715 }
9716
9717 /* 15.13.1: Can't search for field name in packages, so we
9718 assume a variable/class name was meant. */
9719 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9720 {
9721 tree name;
9722 if ((decl = resolve_package (wfl, &q, &name)))
9723 {
9724 tree list;
9725 *where_found = decl;
9726
9727 /* We want to be absolutely sure that the class is laid
9728 out. We're going to search something inside it. */
9729 *type_found = type = TREE_TYPE (decl);
9730 layout_class (type);
9731 from_type = 1;
9732
9733 /* Fix them all the way down, if any are left. */
9734 if (q)
9735 {
9736 list = TREE_CHAIN (q);
9737 while (list)
9738 {
9739 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9740 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9741 list = TREE_CHAIN (list);
9742 }
9743 }
9744 }
9745 else
9746 {
9747 if (from_super || from_cast)
9748 parse_error_context
9749 ((from_cast ? qual_wfl : wfl),
9750 "No variable `%s' defined in class `%s'",
9751 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9752 lang_printable_name (type, 0));
9753 else
9754 parse_error_context
9755 (qual_wfl, "Undefined variable or class name: `%s'",
9756 IDENTIFIER_POINTER (name));
9757 return 1;
9758 }
9759 }
9760
9761 /* We have a type name. It's been already resolved when the
9762 expression was qualified. */
9763 else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
9764 {
9765 decl = QUAL_RESOLUTION (q);
9766
9767 /* Sneak preview. If next we see a `new', we're facing a
9768 qualification with resulted in a type being selected
9769 instead of a field. Report the error */
9770 if(TREE_CHAIN (q)
9771 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9772 {
9773 parse_error_context (qual_wfl, "Undefined variable `%s'",
9774 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9775 return 1;
9776 }
9777
9778 if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
9779 {
9780 parse_error_context
9781 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9782 java_accstring_lookup (get_access_flags_from_decl (decl)),
9783 GET_TYPE_NAME (type),
9784 IDENTIFIER_POINTER (DECL_NAME (decl)),
9785 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9786 return 1;
9787 }
9788 check_deprecation (qual_wfl, decl);
9789
9790 type = TREE_TYPE (decl);
9791 from_type = 1;
9792 }
9793 /* We resolve an expression name */
9794 else
9795 {
9796 tree field_decl = NULL_TREE;
9797
9798 /* If there exists an early resolution, use it. That occurs
9799 only once and we know that there are more things to
9800 come. Don't do that when processing something after SUPER
9801 (we need more thing to be put in place below */
9802 if (!from_super && QUAL_RESOLUTION (q))
9803 {
9804 decl = QUAL_RESOLUTION (q);
9805 if (!type)
9806 {
9807 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9808 {
9809 if (current_this)
9810 *where_found = current_this;
9811 else
9812 {
9813 static_ref_err (qual_wfl, DECL_NAME (decl),
9814 current_class);
9815 return 1;
9816 }
9817 if (outer_field_access_p (current_class, decl))
9818 decl = build_outer_field_access (qual_wfl, decl);
9819 }
9820 else
9821 {
9822 *where_found = TREE_TYPE (decl);
9823 if (TREE_CODE (*where_found) == POINTER_TYPE)
9824 *where_found = TREE_TYPE (*where_found);
9825 }
9826 }
9827 }
9828
9829 /* Report and error if we're using a numerical litteral as a
9830 qualifier. It can only be an INTEGER_CST. */
9831 else if (TREE_CODE (qual_wfl) == INTEGER_CST)
9832 {
9833 parse_error_context
9834 (wfl, "Can't use type `%s' as a qualifier",
9835 lang_printable_name (TREE_TYPE (qual_wfl), 0));
9836 return 1;
9837 }
9838
9839 /* We have to search for a field, knowing the type of its
9840 container. The flag FROM_TYPE indicates that we resolved
9841 the last member of the expression as a type name, which
9842 means that for the resolution of this field, we'll look
9843 for other errors than if it was resolved as a member of
9844 an other field. */
9845 else
9846 {
9847 int is_static;
9848 tree field_decl_type; /* For layout */
9849
9850 if (!from_type && !JREFERENCE_TYPE_P (type))
9851 {
9852 parse_error_context
9853 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9854 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9855 lang_printable_name (type, 0),
9856 IDENTIFIER_POINTER (DECL_NAME (decl)));
9857 return 1;
9858 }
9859
9860 field_decl = lookup_field_wrapper (type,
9861 EXPR_WFL_NODE (qual_wfl));
9862
9863 /* Maybe what we're trying to access to is an inner
9864 class, only if decl is a TYPE_DECL. */
9865 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
9866 {
9867 tree ptr, inner_decl;
9868
9869 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9870 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9871 if (inner_decl)
9872 {
9873 check_inner_class_access (inner_decl, decl, qual_wfl);
9874 type = TREE_TYPE (inner_decl);
9875 decl = inner_decl;
9876 from_type = 1;
9877 continue;
9878 }
9879 }
9880
9881 if (field_decl == NULL_TREE)
9882 {
9883 parse_error_context
9884 (qual_wfl, "No variable `%s' defined in type `%s'",
9885 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9886 GET_TYPE_NAME (type));
9887 return 1;
9888 }
9889 if (field_decl == error_mark_node)
9890 return 1;
9891
9892 /* Layout the type of field_decl, since we may need
9893 it. Don't do primitive types or loaded classes. The
9894 situation of non primitive arrays may not handled
9895 properly here. FIXME */
9896 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9897 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9898 else
9899 field_decl_type = TREE_TYPE (field_decl);
9900 if (!JPRIMITIVE_TYPE_P (field_decl_type)
9901 && !CLASS_LOADED_P (field_decl_type)
9902 && !TYPE_ARRAY_P (field_decl_type))
9903 resolve_and_layout (field_decl_type, NULL_TREE);
9904
9905 /* Check on accessibility here */
9906 if (not_accessible_p (current_class, field_decl,
9907 DECL_CONTEXT (field_decl), from_super))
9908 {
9909 parse_error_context
9910 (qual_wfl,
9911 "Can't access %s field `%s.%s' from `%s'",
9912 java_accstring_lookup
9913 (get_access_flags_from_decl (field_decl)),
9914 GET_TYPE_NAME (type),
9915 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9916 IDENTIFIER_POINTER
9917 (DECL_NAME (TYPE_NAME (current_class))));
9918 return 1;
9919 }
9920 check_deprecation (qual_wfl, field_decl);
9921
9922 /* There are things to check when fields are accessed
9923 from type. There are no restrictions on a static
9924 declaration of the field when it is accessed from an
9925 interface */
9926 is_static = FIELD_STATIC (field_decl);
9927 if (!from_super && from_type
9928 && !TYPE_INTERFACE_P (type)
9929 && !is_static
9930 && (current_function_decl
9931 && METHOD_STATIC (current_function_decl)))
9932 {
9933 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9934 return 1;
9935 }
9936 from_cast = from_super = 0;
9937
9938 /* It's an access from a type but it isn't static, we
9939 make it relative to `this'. */
9940 if (!is_static && from_type)
9941 decl = current_this;
9942
9943 /* If we need to generate something to get a proper
9944 handle on what this field is accessed from, do it
9945 now. */
9946 if (!is_static)
9947 {
9948 decl = maybe_access_field (decl, *where_found, *type_found);
9949 if (decl == error_mark_node)
9950 return 1;
9951 }
9952
9953 /* We want to keep the location were found it, and the type
9954 we found. */
9955 *where_found = decl;
9956 *type_found = type;
9957
9958 /* Generate the correct expression for field access from
9959 qualified this */
9960 if (from_qualified_this)
9961 {
9962 field_decl = build_outer_field_access (qual_wfl, field_decl);
9963 from_qualified_this = 0;
9964 }
9965
9966 /* This is the decl found and eventually the next one to
9967 search from */
9968 decl = field_decl;
9969 }
9970 from_type = 0;
9971 type = QUAL_DECL_TYPE (decl);
9972
9973 /* Sneak preview. If decl is qualified by a `new', report
9974 the error here to be accurate on the peculiar construct */
9975 if (TREE_CHAIN (q)
9976 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9977 && !JREFERENCE_TYPE_P (type))
9978 {
9979 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9980 lang_printable_name (type, 0));
9981 return 1;
9982 }
9983 }
9984 /* `q' might have changed due to a after package resolution
9985 re-qualification */
9986 if (!q)
9987 break;
9988 }
9989 *found_decl = decl;
9990 return 0;
9991 }
9992
9993 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9994 can't be accessed from REFERENCE (a record type). If MEMBER
9995 features a protected access, we then use WHERE which, if non null,
9996 holds the type of MEMBER's access that is checked against
9997 6.6.2.1. This function should be used when decl is a field or a
9998 method. */
9999
10000 static int
10001 not_accessible_p (reference, member, where, from_super)
10002 tree reference, member;
10003 tree where;
10004 int from_super;
10005 {
10006 int access_flag = get_access_flags_from_decl (member);
10007
10008 /* Inner classes are processed by check_inner_class_access */
10009 if (INNER_CLASS_TYPE_P (reference))
10010 return 0;
10011
10012 /* Access always granted for members declared public */
10013 if (access_flag & ACC_PUBLIC)
10014 return 0;
10015
10016 /* Check access on protected members */
10017 if (access_flag & ACC_PROTECTED)
10018 {
10019 /* Access granted if it occurs from within the package
10020 containing the class in which the protected member is
10021 declared */
10022 if (class_in_current_package (DECL_CONTEXT (member)))
10023 return 0;
10024
10025 /* If accessed with the form `super.member', then access is granted */
10026 if (from_super)
10027 return 0;
10028
10029 /* If where is active, access was made through a
10030 qualifier. Access is granted if the type of the qualifier is
10031 or is a sublass of the type the access made from (6.6.2.1.) */
10032 if (where && !inherits_from_p (reference, where))
10033 return 1;
10034
10035 /* Otherwise, access is granted if occurring from the class where
10036 member is declared or a subclass of it. Find the right
10037 context to perform the check */
10038 if (PURE_INNER_CLASS_TYPE_P (reference))
10039 {
10040 while (INNER_CLASS_TYPE_P (reference))
10041 {
10042 if (inherits_from_p (reference, DECL_CONTEXT (member)))
10043 return 0;
10044 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
10045 }
10046 }
10047 if (inherits_from_p (reference, DECL_CONTEXT (member)))
10048 return 0;
10049 return 1;
10050 }
10051
10052 /* Check access on private members. Access is granted only if it
10053 occurs from within the class in which it is declared -- that does
10054 it for innerclasses too. */
10055 if (access_flag & ACC_PRIVATE)
10056 {
10057 if (reference == DECL_CONTEXT (member))
10058 return 0;
10059 if (enclosing_context_p (reference, DECL_CONTEXT (member)))
10060 return 0;
10061 return 1;
10062 }
10063
10064 /* Default access are permitted only when occurring within the
10065 package in which the type (REFERENCE) is declared. In other words,
10066 REFERENCE is defined in the current package */
10067 if (ctxp->package)
10068 return !class_in_current_package (reference);
10069
10070 /* Otherwise, access is granted */
10071 return 0;
10072 }
10073
10074 /* Test deprecated decl access. */
10075 static void
10076 check_deprecation (wfl, decl)
10077 tree wfl, decl;
10078 {
10079 const char *file = DECL_SOURCE_FILE (decl);
10080 /* Complain if the field is deprecated and the file it was defined
10081 in isn't compiled at the same time the file which contains its
10082 use is */
10083 if (DECL_DEPRECATED (decl)
10084 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
10085 {
10086 char the [20];
10087 switch (TREE_CODE (decl))
10088 {
10089 case FUNCTION_DECL:
10090 strcpy (the, "method");
10091 break;
10092 case FIELD_DECL:
10093 case VAR_DECL:
10094 strcpy (the, "field");
10095 break;
10096 case TYPE_DECL:
10097 parse_warning_context (wfl, "The class `%s' has been deprecated",
10098 IDENTIFIER_POINTER (DECL_NAME (decl)));
10099 return;
10100 default:
10101 abort ();
10102 }
10103 /* Don't issue a message if the context as been deprecated as a
10104 whole. */
10105 if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
10106 parse_warning_context
10107 (wfl, "The %s `%s' in class `%s' has been deprecated",
10108 the, lang_printable_name (decl, 0),
10109 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
10110 }
10111 }
10112
10113 /* Returns 1 if class was declared in the current package, 0 otherwise */
10114
10115 static GTY(()) tree cicp_cache;
10116 static int
10117 class_in_current_package (class)
10118 tree class;
10119 {
10120 int qualified_flag;
10121 tree left;
10122
10123 if (cicp_cache == class)
10124 return 1;
10125
10126 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
10127
10128 /* If the current package is empty and the name of CLASS is
10129 qualified, class isn't in the current package. If there is a
10130 current package and the name of the CLASS is not qualified, class
10131 isn't in the current package */
10132 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
10133 return 0;
10134
10135 /* If there is not package and the name of CLASS isn't qualified,
10136 they belong to the same unnamed package */
10137 if (!ctxp->package && !qualified_flag)
10138 return 1;
10139
10140 /* Compare the left part of the name of CLASS with the package name */
10141 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
10142 if (ctxp->package == left)
10143 {
10144 cicp_cache = class;
10145 return 1;
10146 }
10147 return 0;
10148 }
10149
10150 /* This function may generate code to access DECL from WHERE. This is
10151 done only if certain conditions meet. */
10152
10153 static tree
10154 maybe_access_field (decl, where, type)
10155 tree decl, where, type;
10156 {
10157 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10158 && !FIELD_STATIC (decl))
10159 decl = build_field_ref (where ? where : current_this,
10160 (type ? type : DECL_CONTEXT (decl)),
10161 DECL_NAME (decl));
10162 return decl;
10163 }
10164
10165 /* Build a method invocation, by patching PATCH. If non NULL
10166 and according to the situation, PRIMARY and WHERE may be
10167 used. IS_STATIC is set to 1 if the invoked function is static. */
10168
10169 static tree
10170 patch_method_invocation (patch, primary, where, from_super,
10171 is_static, ret_decl)
10172 tree patch, primary, where;
10173 int from_super;
10174 int *is_static;
10175 tree *ret_decl;
10176 {
10177 tree wfl = TREE_OPERAND (patch, 0);
10178 tree args = TREE_OPERAND (patch, 1);
10179 tree name = EXPR_WFL_NODE (wfl);
10180 tree list;
10181 int is_static_flag = 0;
10182 int is_super_init = 0;
10183 tree this_arg = NULL_TREE;
10184 int is_array_clone_call = 0;
10185
10186 /* Should be overriden if everything goes well. Otherwise, if
10187 something fails, it should keep this value. It stop the
10188 evaluation of a bogus assignment. See java_complete_tree,
10189 MODIFY_EXPR: for the reasons why we sometimes want to keep on
10190 evaluating an assignment */
10191 TREE_TYPE (patch) = error_mark_node;
10192
10193 /* Since lookup functions are messing with line numbers, save the
10194 context now. */
10195 java_parser_context_save_global ();
10196
10197 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10198
10199 /* Resolution of qualified name, excluding constructors */
10200 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10201 {
10202 tree identifier, identifier_wfl, type, resolved;
10203 /* Extract the last IDENTIFIER of the qualified
10204 expression. This is a wfl and we will use it's location
10205 data during error report. */
10206 identifier_wfl = cut_identifier_in_qualified (wfl);
10207 identifier = EXPR_WFL_NODE (identifier_wfl);
10208
10209 /* Given the context, IDENTIFIER is syntactically qualified
10210 as a MethodName. We need to qualify what's before */
10211 qualify_ambiguous_name (wfl);
10212 resolved = resolve_field_access (wfl, NULL, NULL);
10213
10214 if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10215 && FIELD_FINAL (resolved)
10216 && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10217 && !flag_emit_class_files && !flag_emit_xref)
10218 resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10219
10220 if (resolved == error_mark_node)
10221 PATCH_METHOD_RETURN_ERROR ();
10222
10223 type = GET_SKIP_TYPE (resolved);
10224 resolve_and_layout (type, NULL_TREE);
10225
10226 if (JPRIMITIVE_TYPE_P (type))
10227 {
10228 parse_error_context
10229 (identifier_wfl,
10230 "Can't invoke a method on primitive type `%s'",
10231 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10232 PATCH_METHOD_RETURN_ERROR ();
10233 }
10234
10235 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10236 args = nreverse (args);
10237
10238 /* We're resolving a call from a type */
10239 if (TREE_CODE (resolved) == TYPE_DECL)
10240 {
10241 if (CLASS_INTERFACE (resolved))
10242 {
10243 parse_error_context
10244 (identifier_wfl,
10245 "Can't make static reference to method `%s' in interface `%s'",
10246 IDENTIFIER_POINTER (identifier),
10247 IDENTIFIER_POINTER (name));
10248 PATCH_METHOD_RETURN_ERROR ();
10249 }
10250 if (list && !METHOD_STATIC (list))
10251 {
10252 char *fct_name = xstrdup (lang_printable_name (list, 0));
10253 parse_error_context
10254 (identifier_wfl,
10255 "Can't make static reference to method `%s %s' in class `%s'",
10256 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10257 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10258 free (fct_name);
10259 PATCH_METHOD_RETURN_ERROR ();
10260 }
10261 }
10262 else
10263 this_arg = primary = resolved;
10264
10265 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10266 is_array_clone_call = 1;
10267
10268 /* IDENTIFIER_WFL will be used to report any problem further */
10269 wfl = identifier_wfl;
10270 }
10271 /* Resolution of simple names, names generated after a primary: or
10272 constructors */
10273 else
10274 {
10275 tree class_to_search = NULL_TREE;
10276 int lc; /* Looking for Constructor */
10277
10278 /* We search constructor in their target class */
10279 if (CALL_CONSTRUCTOR_P (patch))
10280 {
10281 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10282 class_to_search = EXPR_WFL_NODE (wfl);
10283 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10284 this_identifier_node)
10285 class_to_search = NULL_TREE;
10286 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10287 super_identifier_node)
10288 {
10289 is_super_init = 1;
10290 if (CLASSTYPE_SUPER (current_class))
10291 class_to_search =
10292 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10293 else
10294 {
10295 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10296 PATCH_METHOD_RETURN_ERROR ();
10297 }
10298 }
10299
10300 /* Class to search is NULL if we're searching the current one */
10301 if (class_to_search)
10302 {
10303 class_to_search = resolve_and_layout (class_to_search, wfl);
10304
10305 if (!class_to_search)
10306 {
10307 parse_error_context
10308 (wfl, "Class `%s' not found in type declaration",
10309 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10310 PATCH_METHOD_RETURN_ERROR ();
10311 }
10312
10313 /* Can't instantiate an abstract class, but we can
10314 invoke it's constructor. It's use within the `new'
10315 context is denied here. */
10316 if (CLASS_ABSTRACT (class_to_search)
10317 && TREE_CODE (patch) == NEW_CLASS_EXPR)
10318 {
10319 parse_error_context
10320 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
10321 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10322 PATCH_METHOD_RETURN_ERROR ();
10323 }
10324
10325 class_to_search = TREE_TYPE (class_to_search);
10326 }
10327 else
10328 class_to_search = current_class;
10329 lc = 1;
10330 }
10331 /* This is a regular search in the local class, unless an
10332 alternate class is specified. */
10333 else
10334 {
10335 if (where != NULL_TREE)
10336 class_to_search = where;
10337 else if (QUALIFIED_P (name))
10338 class_to_search = current_class;
10339 else
10340 {
10341 class_to_search = current_class;
10342
10343 for (;;)
10344 {
10345 if (has_method (class_to_search, name))
10346 break;
10347 if (! INNER_CLASS_TYPE_P (class_to_search))
10348 {
10349 parse_error_context (wfl,
10350 "No method named `%s' in scope",
10351 IDENTIFIER_POINTER (name));
10352 PATCH_METHOD_RETURN_ERROR ();
10353 }
10354 class_to_search
10355 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10356 }
10357 }
10358 lc = 0;
10359 }
10360
10361 /* NAME is a simple identifier or comes from a primary. Search
10362 in the class whose declaration contain the method being
10363 invoked. */
10364 resolve_and_layout (class_to_search, NULL_TREE);
10365
10366 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10367 /* Don't continue if no method were found, as the next statement
10368 can't be executed then. */
10369 if (!list)
10370 PATCH_METHOD_RETURN_ERROR ();
10371
10372 if (TYPE_ARRAY_P (class_to_search)
10373 && DECL_NAME (list) == get_identifier ("clone"))
10374 is_array_clone_call = 1;
10375
10376 /* Check for static reference if non static methods */
10377 if (check_for_static_method_reference (wfl, patch, list,
10378 class_to_search, primary))
10379 PATCH_METHOD_RETURN_ERROR ();
10380
10381 /* Check for inner classes creation from illegal contexts */
10382 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10383 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10384 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10385 && !DECL_INIT_P (current_function_decl))
10386 {
10387 parse_error_context
10388 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
10389 lang_printable_name (class_to_search, 0),
10390 (!current_this ? "" :
10391 "; an explicit one must be provided when creating this inner class"));
10392 PATCH_METHOD_RETURN_ERROR ();
10393 }
10394
10395 /* Non static methods are called with the current object extra
10396 argument. If patch a `new TYPE()', the argument is the value
10397 returned by the object allocator. If method is resolved as a
10398 primary, use the primary otherwise use the current THIS. */
10399 args = nreverse (args);
10400 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10401 {
10402 this_arg = primary ? primary : current_this;
10403
10404 /* If we're using an access method, things are different.
10405 There are two familly of cases:
10406
10407 1) We're not generating bytecodes:
10408
10409 - LIST is non static. It's invocation is transformed from
10410 x(a1,...,an) into this$<n>.x(a1,....an).
10411 - LIST is static. It's invocation is transformed from
10412 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10413
10414 2) We're generating bytecodes:
10415
10416 - LIST is non static. It's invocation is transformed from
10417 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10418 - LIST is static. It's invocation is transformed from
10419 x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10420
10421 Of course, this$<n> can be abitrary complex, ranging from
10422 this$0 (the immediate outer context) to
10423 access$0(access$0(...(this$0))).
10424
10425 maybe_use_access_method returns a nonzero value if the
10426 this_arg has to be moved into the (then generated) stub
10427 argument list. In the meantime, the selected function
10428 might have be replaced by a generated stub. */
10429 if (!primary &&
10430 maybe_use_access_method (is_super_init, &list, &this_arg))
10431 {
10432 args = tree_cons (NULL_TREE, this_arg, args);
10433 this_arg = NULL_TREE; /* So it doesn't get chained twice */
10434 }
10435 }
10436 }
10437
10438 /* Merge point of all resolution schemes. If we have nothing, this
10439 is an error, already signaled */
10440 if (!list)
10441 PATCH_METHOD_RETURN_ERROR ();
10442
10443 /* Check accessibility, position the is_static flag, build and
10444 return the call */
10445 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10446 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10447 NULL_TREE), from_super)
10448 /* Calls to clone() on array types are permitted as a special-case. */
10449 && !is_array_clone_call)
10450 {
10451 const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10452 const char *const access =
10453 java_accstring_lookup (get_access_flags_from_decl (list));
10454 const char *const klass =
10455 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10456 const char *const refklass =
10457 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10458 const char *const what = (DECL_CONSTRUCTOR_P (list)
10459 ? "constructor" : "method");
10460 /* FIXME: WFL yields the wrong message here but I don't know
10461 what else to use. */
10462 parse_error_context (wfl,
10463 "Can't access %s %s `%s.%s' from `%s'",
10464 access, what, klass, fct_name, refklass);
10465 PATCH_METHOD_RETURN_ERROR ();
10466 }
10467
10468 /* Deprecation check: check whether the method being invoked or the
10469 instance-being-created's type are deprecated. */
10470 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10471 check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10472 else
10473 check_deprecation (wfl, list);
10474
10475 /* If invoking a innerclass constructor, there are hidden parameters
10476 to pass */
10477 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10478 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10479 {
10480 /* And make sure we add the accessed local variables to be saved
10481 in field aliases. */
10482 args = build_alias_initializer_parameter_list
10483 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10484
10485 /* Secretly pass the current_this/primary as a second argument */
10486 if (primary || current_this)
10487 {
10488 tree extra_arg;
10489 tree this_type = (current_this ?
10490 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10491 /* Method's (list) enclosing context */
10492 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10493 /* If we have a primary, use it. */
10494 if (primary)
10495 extra_arg = primary;
10496 /* The current `this' is an inner class but isn't a direct
10497 enclosing context for the inner class we're trying to
10498 create. Build an access to the proper enclosing context
10499 and use it. */
10500 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10501 && this_type != TREE_TYPE (mec))
10502 {
10503
10504 extra_arg = build_access_to_thisn (current_class,
10505 TREE_TYPE (mec), 0);
10506 extra_arg = java_complete_tree (extra_arg);
10507 }
10508 /* Otherwise, just use the current `this' as an enclosing
10509 context. */
10510 else
10511 extra_arg = current_this;
10512 args = tree_cons (NULL_TREE, extra_arg, args);
10513 }
10514 else
10515 args = tree_cons (NULL_TREE, integer_zero_node, args);
10516 }
10517
10518 /* This handles the situation where a constructor invocation needs
10519 to have an enclosing context passed as a second parameter (the
10520 constructor is one of an inner class). */
10521 if ((is_super_init ||
10522 (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10523 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10524 {
10525 tree dest = TYPE_NAME (DECL_CONTEXT (list));
10526 tree extra_arg =
10527 build_access_to_thisn (current_class, DECL_CONTEXT (dest), 0);
10528 extra_arg = java_complete_tree (extra_arg);
10529 args = tree_cons (NULL_TREE, extra_arg, args);
10530 }
10531
10532 is_static_flag = METHOD_STATIC (list);
10533 if (! is_static_flag && this_arg != NULL_TREE)
10534 args = tree_cons (NULL_TREE, this_arg, args);
10535
10536 /* In the context of an explicit constructor invocation, we can't
10537 invoke any method relying on `this'. Exceptions are: we're
10538 invoking a static function, primary exists and is not the current
10539 this, we're creating a new object. */
10540 if (ctxp->explicit_constructor_p
10541 && !is_static_flag
10542 && (!primary || primary == current_this)
10543 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10544 {
10545 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
10546 PATCH_METHOD_RETURN_ERROR ();
10547 }
10548 java_parser_context_restore_global ();
10549 if (is_static)
10550 *is_static = is_static_flag;
10551 /* Sometimes, we want the decl of the selected method. Such as for
10552 EH checking */
10553 if (ret_decl)
10554 *ret_decl = list;
10555 patch = patch_invoke (patch, list, args);
10556
10557 /* Now is a good time to insert the call to finit$ */
10558 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10559 {
10560 tree finit_parms, finit_call;
10561
10562 /* Prepare to pass hidden parameters to finit$, if any. */
10563 finit_parms = build_alias_initializer_parameter_list
10564 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10565
10566 finit_call =
10567 build_method_invocation (build_wfl_node (finit_identifier_node),
10568 finit_parms);
10569
10570 /* Generate the code used to initialize fields declared with an
10571 initialization statement and build a compound statement along
10572 with the super constructor invocation. */
10573 CAN_COMPLETE_NORMALLY (patch) = 1;
10574 patch = build (COMPOUND_EXPR, void_type_node, patch,
10575 java_complete_tree (finit_call));
10576 }
10577 return patch;
10578 }
10579
10580 /* Check that we're not trying to do a static reference to a method in
10581 non static method. Return 1 if it's the case, 0 otherwise. */
10582
10583 static int
10584 check_for_static_method_reference (wfl, node, method, where, primary)
10585 tree wfl, node, method, where, primary;
10586 {
10587 if (METHOD_STATIC (current_function_decl)
10588 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10589 {
10590 char *fct_name = xstrdup (lang_printable_name (method, 0));
10591 parse_error_context
10592 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
10593 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10594 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10595 free (fct_name);
10596 return 1;
10597 }
10598 return 0;
10599 }
10600
10601 /* Fix the invocation of *MDECL if necessary in the case of a
10602 invocation from an inner class. *THIS_ARG might be modified
10603 appropriately and an alternative access to *MDECL might be
10604 returned. */
10605
10606 static int
10607 maybe_use_access_method (is_super_init, mdecl, this_arg)
10608 int is_super_init;
10609 tree *mdecl, *this_arg;
10610 {
10611 tree ctx;
10612 tree md = *mdecl, ta = *this_arg;
10613 int to_return = 0;
10614 int non_static_context = !METHOD_STATIC (md);
10615
10616 if (is_super_init
10617 || DECL_CONTEXT (md) == current_class
10618 || !PURE_INNER_CLASS_TYPE_P (current_class)
10619 || DECL_FINIT_P (md)
10620 || DECL_INSTINIT_P (md))
10621 return 0;
10622
10623 /* If we're calling a method found in an enclosing class, generate
10624 what it takes to retrieve the right this. Don't do that if we're
10625 invoking a static method. Note that if MD's type is unrelated to
10626 CURRENT_CLASS, then the current this can be used. */
10627
10628 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10629 {
10630 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10631 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10632 {
10633 ta = build_current_thisn (current_class);
10634 ta = build_wfl_node (ta);
10635 }
10636 else
10637 {
10638 tree type = ctx;
10639 while (type)
10640 {
10641 maybe_build_thisn_access_method (type);
10642 if (inherits_from_p (type, DECL_CONTEXT (md)))
10643 {
10644 ta = build_access_to_thisn (ctx, type, 0);
10645 break;
10646 }
10647 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10648 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10649 }
10650 }
10651 ta = java_complete_tree (ta);
10652 }
10653
10654 /* We might have to use an access method to get to MD. We can
10655 break the method access rule as far as we're not generating
10656 bytecode */
10657 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10658 {
10659 md = build_outer_method_access_method (md);
10660 to_return = 1;
10661 }
10662
10663 *mdecl = md;
10664 *this_arg = ta;
10665
10666 /* Returnin a nonzero value indicates we were doing a non static
10667 method invokation that is now a static invocation. It will have
10668 callee displace `this' to insert it in the regular argument
10669 list. */
10670 return (non_static_context && to_return);
10671 }
10672
10673 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10674 mode. */
10675
10676 static tree
10677 patch_invoke (patch, method, args)
10678 tree patch, method, args;
10679 {
10680 tree dtable, func;
10681 tree original_call, t, ta;
10682 tree check = NULL_TREE;
10683
10684 /* Last step for args: convert build-in types. If we're dealing with
10685 a new TYPE() type call, the first argument to the constructor
10686 isn't found in the incoming argument list, but delivered by
10687 `new' */
10688 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10689 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10690 t = TREE_CHAIN (t);
10691 for (ta = args; t != end_params_node && ta;
10692 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10693 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10694 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10695 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10696
10697 /* Resolve unresolved returned type isses */
10698 t = TREE_TYPE (TREE_TYPE (method));
10699 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10700 resolve_and_layout (TREE_TYPE (t), NULL);
10701
10702 if (flag_emit_class_files || flag_emit_xref)
10703 func = method;
10704 else
10705 {
10706 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10707 {
10708 case INVOKE_VIRTUAL:
10709 dtable = invoke_build_dtable (0, args);
10710 func = build_invokevirtual (dtable, method);
10711 break;
10712
10713 case INVOKE_NONVIRTUAL:
10714 /* If the object for the method call is null, we throw an
10715 exception. We don't do this if the object is the current
10716 method's `this'. In other cases we just rely on an
10717 optimization pass to eliminate redundant checks. */
10718 if (TREE_VALUE (args) != current_this)
10719 {
10720 /* We use a save_expr here to make sure we only evaluate
10721 the new `self' expression once. */
10722 tree save_arg = save_expr (TREE_VALUE (args));
10723 TREE_VALUE (args) = save_arg;
10724 check = java_check_reference (save_arg, 1);
10725 }
10726 /* Fall through. */
10727
10728 case INVOKE_SUPER:
10729 case INVOKE_STATIC:
10730 {
10731 tree signature = build_java_signature (TREE_TYPE (method));
10732 func = build_known_method_ref (method, TREE_TYPE (method),
10733 DECL_CONTEXT (method),
10734 signature, args);
10735 }
10736 break;
10737
10738 case INVOKE_INTERFACE:
10739 dtable = invoke_build_dtable (1, args);
10740 func = build_invokeinterface (dtable, method);
10741 break;
10742
10743 default:
10744 abort ();
10745 }
10746
10747 /* Ensure self_type is initialized, (invokestatic). FIXME */
10748 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10749 }
10750
10751 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10752 TREE_OPERAND (patch, 0) = func;
10753 TREE_OPERAND (patch, 1) = args;
10754 patch = check_for_builtin (method, patch);
10755 original_call = patch;
10756
10757 /* We're processing a `new TYPE ()' form. New is called and its
10758 returned value is the first argument to the constructor. We build
10759 a COMPOUND_EXPR and use saved expression so that the overall NEW
10760 expression value is a pointer to a newly created and initialized
10761 class. */
10762 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10763 {
10764 tree class = DECL_CONTEXT (method);
10765 tree c1, saved_new, size, new;
10766 tree alloc_node;
10767
10768 if (flag_emit_class_files || flag_emit_xref)
10769 {
10770 TREE_TYPE (patch) = build_pointer_type (class);
10771 return patch;
10772 }
10773 if (!TYPE_SIZE (class))
10774 safe_layout_class (class);
10775 size = size_in_bytes (class);
10776 alloc_node =
10777 (class_has_finalize_method (class) ? alloc_object_node
10778 : alloc_no_finalizer_node);
10779 new = build (CALL_EXPR, promote_type (class),
10780 build_address_of (alloc_node),
10781 tree_cons (NULL_TREE, build_class_ref (class),
10782 build_tree_list (NULL_TREE,
10783 size_in_bytes (class))),
10784 NULL_TREE);
10785 saved_new = save_expr (new);
10786 c1 = build_tree_list (NULL_TREE, saved_new);
10787 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10788 TREE_OPERAND (original_call, 1) = c1;
10789 TREE_SET_CODE (original_call, CALL_EXPR);
10790 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10791 }
10792
10793 /* If CHECK is set, then we are building a check to see if the object
10794 is NULL. */
10795 if (check != NULL_TREE)
10796 {
10797 /* We have to call force_evaluation_order now because creating a
10798 COMPOUND_EXPR wraps the arg list in a way that makes it
10799 unrecognizable by force_evaluation_order later. Yuk. */
10800 patch = build (COMPOUND_EXPR, TREE_TYPE (patch), check,
10801 force_evaluation_order (patch));
10802 TREE_SIDE_EFFECTS (patch) = 1;
10803 }
10804
10805 /* In order to be able to modify PATCH later, we SAVE_EXPR it and
10806 put it as the first expression of a COMPOUND_EXPR. The second
10807 expression being an empty statement to be later patched if
10808 necessary. We remember a TREE_LIST (the PURPOSE is the method,
10809 the VALUE is the compound) in a hashtable and return a
10810 COMPOUND_EXPR built so that the result of the evaluation of the
10811 original PATCH node is returned. */
10812 if (STATIC_CLASS_INIT_OPT_P ()
10813 && current_function_decl && METHOD_STATIC (method))
10814 {
10815 tree list;
10816 tree fndecl = current_function_decl;
10817 /* We have to call force_evaluation_order now because creating a
10818 COMPOUND_EXPR wraps the arg list in a way that makes it
10819 unrecognizable by force_evaluation_order later. Yuk. */
10820 tree save = save_expr (force_evaluation_order (patch));
10821 tree type = TREE_TYPE (patch);
10822
10823 patch = build (COMPOUND_EXPR, type, save, empty_stmt_node);
10824 list = tree_cons (method, patch,
10825 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
10826
10827 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
10828
10829 patch = build (COMPOUND_EXPR, type, patch, save);
10830 }
10831
10832 return patch;
10833 }
10834
10835 static int
10836 invocation_mode (method, super)
10837 tree method;
10838 int super;
10839 {
10840 int access = get_access_flags_from_decl (method);
10841
10842 if (super)
10843 return INVOKE_SUPER;
10844
10845 if (access & ACC_STATIC)
10846 return INVOKE_STATIC;
10847
10848 /* We have to look for a constructor before we handle nonvirtual
10849 calls; otherwise the constructor will look nonvirtual. */
10850 if (DECL_CONSTRUCTOR_P (method))
10851 return INVOKE_STATIC;
10852
10853 if (access & ACC_FINAL || access & ACC_PRIVATE)
10854 return INVOKE_NONVIRTUAL;
10855
10856 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10857 return INVOKE_NONVIRTUAL;
10858
10859 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10860 return INVOKE_INTERFACE;
10861
10862 return INVOKE_VIRTUAL;
10863 }
10864
10865 /* Retrieve a refined list of matching methods. It covers the step
10866 15.11.2 (Compile-Time Step 2) */
10867
10868 static tree
10869 lookup_method_invoke (lc, cl, class, name, arg_list)
10870 int lc;
10871 tree cl;
10872 tree class, name, arg_list;
10873 {
10874 tree atl = end_params_node; /* Arg Type List */
10875 tree method, signature, list, node;
10876 const char *candidates; /* Used for error report */
10877 char *dup;
10878
10879 /* Fix the arguments */
10880 for (node = arg_list; node; node = TREE_CHAIN (node))
10881 {
10882 tree current_arg = TREE_TYPE (TREE_VALUE (node));
10883 /* Non primitive type may have to be resolved */
10884 if (!JPRIMITIVE_TYPE_P (current_arg))
10885 resolve_and_layout (current_arg, NULL_TREE);
10886 /* And promoted */
10887 if (TREE_CODE (current_arg) == RECORD_TYPE)
10888 current_arg = promote_type (current_arg);
10889 atl = tree_cons (NULL_TREE, current_arg, atl);
10890 }
10891
10892 /* Presto. If we're dealing with an anonymous class and a
10893 constructor call, generate the right constructor now, since we
10894 know the arguments' types. */
10895
10896 if (lc && ANONYMOUS_CLASS_P (class))
10897 {
10898 tree saved_current_class;
10899 tree mdecl = craft_constructor (TYPE_NAME (class), atl);
10900 saved_current_class = current_class;
10901 current_class = class;
10902 fix_constructors (mdecl);
10903 current_class = saved_current_class;
10904 }
10905
10906 /* Find all candidates and then refine the list, searching for the
10907 most specific method. */
10908 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10909 list = find_most_specific_methods_list (list);
10910 if (list && !TREE_CHAIN (list))
10911 return TREE_VALUE (list);
10912
10913 /* Issue an error. List candidates if any. Candidates are listed
10914 only if accessible (non accessible methods may end-up here for
10915 the sake of a better error report). */
10916 candidates = NULL;
10917 if (list)
10918 {
10919 tree current;
10920 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10921 for (current = list; current; current = TREE_CHAIN (current))
10922 {
10923 tree cm = TREE_VALUE (current);
10924 char string [4096];
10925 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
10926 continue;
10927 sprintf
10928 (string, " `%s' in `%s'%s",
10929 get_printable_method_name (cm),
10930 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10931 (TREE_CHAIN (current) ? "\n" : ""));
10932 obstack_grow (&temporary_obstack, string, strlen (string));
10933 }
10934 obstack_1grow (&temporary_obstack, '\0');
10935 candidates = obstack_finish (&temporary_obstack);
10936 }
10937 /* Issue the error message */
10938 method = make_node (FUNCTION_TYPE);
10939 TYPE_ARG_TYPES (method) = atl;
10940 signature = build_java_argument_signature (method);
10941 dup = xstrdup (lang_printable_name (class, 0));
10942 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10943 (lc ? "constructor" : "method"),
10944 (lc ? dup : IDENTIFIER_POINTER (name)),
10945 IDENTIFIER_POINTER (signature), dup,
10946 (candidates ? candidates : ""));
10947 free (dup);
10948 return NULL_TREE;
10949 }
10950
10951 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10952 when we're looking for a constructor. */
10953
10954 static tree
10955 find_applicable_accessible_methods_list (lc, class, name, arglist)
10956 int lc;
10957 tree class, name, arglist;
10958 {
10959 static htab_t searched_classes;
10960 static int search_not_done = 0;
10961 tree list = NULL_TREE, all_list = NULL_TREE;
10962
10963 /* Check the hash table to determine if this class has been searched
10964 already. */
10965 if (searched_classes)
10966 {
10967 if (htab_find (searched_classes, class) != NULL)
10968 return NULL;
10969 }
10970 else
10971 {
10972 searched_classes = htab_create (10, htab_hash_pointer,
10973 htab_eq_pointer, NULL);
10974 }
10975
10976 search_not_done++;
10977 *htab_find_slot (searched_classes, class, INSERT) = class;
10978
10979 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10980 {
10981 load_class (class, 1);
10982 safe_layout_class (class);
10983 }
10984
10985 /* Search interfaces */
10986 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10987 && CLASS_INTERFACE (TYPE_NAME (class)))
10988 {
10989 int i, n;
10990 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10991 search_applicable_methods_list (lc, TYPE_METHODS (class),
10992 name, arglist, &list, &all_list);
10993 n = TREE_VEC_LENGTH (basetype_vec);
10994 for (i = 1; i < n; i++)
10995 {
10996 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10997 tree rlist;
10998
10999 rlist = find_applicable_accessible_methods_list (lc, t, name,
11000 arglist);
11001 list = chainon (rlist, list);
11002 }
11003 }
11004 /* Search classes */
11005 else
11006 {
11007 search_applicable_methods_list (lc, TYPE_METHODS (class),
11008 name, arglist, &list, &all_list);
11009
11010 /* When looking finit$, class$ or instinit$, we turn LC to 1 so
11011 that we only search in class. Note that we should have found
11012 something at this point. */
11013 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
11014 {
11015 lc = 1;
11016 if (!list)
11017 abort ();
11018 }
11019
11020 /* We must search all interfaces of this class */
11021 if (!lc)
11022 {
11023 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
11024 int n = TREE_VEC_LENGTH (basetype_vec), i;
11025 for (i = 1; i < n; i++)
11026 {
11027 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
11028 if (t != object_type_node)
11029 {
11030 tree rlist
11031 = find_applicable_accessible_methods_list (lc, t,
11032 name, arglist);
11033 list = chainon (rlist, list);
11034 }
11035 }
11036 }
11037
11038 /* Search superclass */
11039 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
11040 {
11041 tree rlist;
11042 class = CLASSTYPE_SUPER (class);
11043 rlist = find_applicable_accessible_methods_list (lc, class,
11044 name, arglist);
11045 list = chainon (rlist, list);
11046 }
11047 }
11048
11049 search_not_done--;
11050
11051 /* We're done. Reset the searched classes list and finally search
11052 java.lang.Object if it wasn't searched already. */
11053 if (!search_not_done)
11054 {
11055 if (!lc
11056 && TYPE_METHODS (object_type_node)
11057 && htab_find (searched_classes, object_type_node) == NULL)
11058 {
11059 search_applicable_methods_list (lc,
11060 TYPE_METHODS (object_type_node),
11061 name, arglist, &list, &all_list);
11062 }
11063 htab_delete (searched_classes);
11064 searched_classes = NULL;
11065 }
11066
11067 /* Either return the list obtained or all selected (but
11068 inaccessible) methods for better error report. */
11069 return (!list ? all_list : list);
11070 }
11071
11072 /* Effectively search for the appropriate method in method */
11073
11074 static void
11075 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
11076 int lc;
11077 tree method, name, arglist;
11078 tree *list, *all_list;
11079 {
11080 for (; method; method = TREE_CHAIN (method))
11081 {
11082 /* When dealing with constructor, stop here, otherwise search
11083 other classes */
11084 if (lc && !DECL_CONSTRUCTOR_P (method))
11085 continue;
11086 else if (!lc && (DECL_CONSTRUCTOR_P (method)
11087 || (DECL_NAME (method) != name)))
11088 continue;
11089
11090 if (argument_types_convertible (method, arglist))
11091 {
11092 /* Retain accessible methods only */
11093 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
11094 method, NULL_TREE, 0))
11095 *list = tree_cons (NULL_TREE, method, *list);
11096 else
11097 /* Also retain all selected method here */
11098 *all_list = tree_cons (NULL_TREE, method, *list);
11099 }
11100 }
11101 }
11102
11103 /* 15.11.2.2 Choose the Most Specific Method */
11104
11105 static tree
11106 find_most_specific_methods_list (list)
11107 tree list;
11108 {
11109 int max = 0;
11110 int abstract, candidates;
11111 tree current, new_list = NULL_TREE;
11112 for (current = list; current; current = TREE_CHAIN (current))
11113 {
11114 tree method;
11115 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
11116
11117 for (method = list; method; method = TREE_CHAIN (method))
11118 {
11119 tree method_v, current_v;
11120 /* Don't test a method against itself */
11121 if (method == current)
11122 continue;
11123
11124 method_v = TREE_VALUE (method);
11125 current_v = TREE_VALUE (current);
11126
11127 /* Compare arguments and location where methods where declared */
11128 if (argument_types_convertible (method_v, current_v))
11129 {
11130 if (valid_method_invocation_conversion_p
11131 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
11132 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
11133 && enclosing_context_p (DECL_CONTEXT (method_v),
11134 DECL_CONTEXT (current_v))))
11135 {
11136 int v = (DECL_SPECIFIC_COUNT (current_v) +=
11137 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
11138 max = (v > max ? v : max);
11139 }
11140 }
11141 }
11142 }
11143
11144 /* Review the list and select the maximally specific methods */
11145 for (current = list, abstract = -1, candidates = -1;
11146 current; current = TREE_CHAIN (current))
11147 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11148 {
11149 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11150 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11151 candidates++;
11152 }
11153
11154 /* If we have several and they're all abstract, just pick the
11155 closest one. */
11156 if (candidates > 0 && (candidates == abstract))
11157 {
11158 new_list = nreverse (new_list);
11159 TREE_CHAIN (new_list) = NULL_TREE;
11160 }
11161
11162 /* We have several (we couldn't find a most specific), all but one
11163 are abstract, we pick the only non abstract one. */
11164 if (candidates > 0 && (candidates == abstract+1))
11165 {
11166 for (current = new_list; current; current = TREE_CHAIN (current))
11167 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11168 {
11169 TREE_CHAIN (current) = NULL_TREE;
11170 new_list = current;
11171 }
11172 }
11173
11174 /* If we can't find one, lower expectations and try to gather multiple
11175 maximally specific methods */
11176 while (!new_list && max)
11177 {
11178 while (--max > 0)
11179 {
11180 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11181 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11182 }
11183 }
11184
11185 return new_list;
11186 }
11187
11188 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
11189 converted by method invocation conversion (5.3) to the type of the
11190 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11191 to change less often than M1. */
11192
11193 static GTY(()) tree m2_arg_value;
11194 static GTY(()) tree m2_arg_cache;
11195
11196 static int
11197 argument_types_convertible (m1, m2_or_arglist)
11198 tree m1, m2_or_arglist;
11199 {
11200 register tree m1_arg, m2_arg;
11201
11202 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11203
11204 if (m2_arg_value == m2_or_arglist)
11205 m2_arg = m2_arg_cache;
11206 else
11207 {
11208 /* M2_OR_ARGLIST can be a function DECL or a raw list of
11209 argument types */
11210 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11211 {
11212 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11213 if (!METHOD_STATIC (m2_or_arglist))
11214 m2_arg = TREE_CHAIN (m2_arg);
11215 }
11216 else
11217 m2_arg = m2_or_arglist;
11218
11219 m2_arg_value = m2_or_arglist;
11220 m2_arg_cache = m2_arg;
11221 }
11222
11223 while (m1_arg != end_params_node && m2_arg != end_params_node)
11224 {
11225 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11226 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11227 TREE_VALUE (m2_arg)))
11228 break;
11229 m1_arg = TREE_CHAIN (m1_arg);
11230 m2_arg = TREE_CHAIN (m2_arg);
11231 }
11232 return m1_arg == end_params_node && m2_arg == end_params_node;
11233 }
11234
11235 /* Qualification routines */
11236
11237 static void
11238 qualify_ambiguous_name (id)
11239 tree id;
11240 {
11241 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
11242 saved_current_class;
11243 int again, super_found = 0, this_found = 0, new_array_found = 0;
11244 int code;
11245
11246 /* We first qualify the first element, then derive qualification of
11247 others based on the first one. If the first element is qualified
11248 by a resolution (field or type), this resolution is stored in the
11249 QUAL_RESOLUTION of the qual element being examined. We need to
11250 save the current_class since the use of SUPER might change the
11251 its value. */
11252 saved_current_class = current_class;
11253 qual = EXPR_WFL_QUALIFICATION (id);
11254 do {
11255
11256 /* Simple qualified expression feature a qual_wfl that is a
11257 WFL. Expression derived from a primary feature more complicated
11258 things like a CALL_EXPR. Expression from primary need to be
11259 worked out to extract the part on which the qualification will
11260 take place. */
11261 qual_wfl = QUAL_WFL (qual);
11262 switch (TREE_CODE (qual_wfl))
11263 {
11264 case CALL_EXPR:
11265 qual_wfl = TREE_OPERAND (qual_wfl, 0);
11266 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION
11267 || (EXPR_WFL_QUALIFICATION (qual_wfl)
11268 && TREE_CODE (EXPR_WFL_QUALIFICATION (qual_wfl)) == TREE_LIST))
11269 {
11270 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11271 qual_wfl = QUAL_WFL (qual);
11272 }
11273 break;
11274 case NEW_ARRAY_EXPR:
11275 case NEW_ANONYMOUS_ARRAY_EXPR:
11276 qual = TREE_CHAIN (qual);
11277 again = new_array_found = 1;
11278 continue;
11279 case CONVERT_EXPR:
11280 break;
11281 case NEW_CLASS_EXPR:
11282 qual_wfl = TREE_OPERAND (qual_wfl, 0);
11283 break;
11284 case ARRAY_REF:
11285 while (TREE_CODE (qual_wfl) == ARRAY_REF)
11286 qual_wfl = TREE_OPERAND (qual_wfl, 0);
11287 break;
11288 case STRING_CST:
11289 qual = TREE_CHAIN (qual);
11290 qual_wfl = QUAL_WFL (qual);
11291 break;
11292 case CLASS_LITERAL:
11293 qual = TREE_CHAIN (qual);
11294 qual_wfl = QUAL_WFL (qual);
11295 break;
11296 default:
11297 /* Fix for -Wall. Just break doing nothing */
11298 break;
11299 }
11300
11301 ptr_type = current_class;
11302 again = 0;
11303 code = TREE_CODE (qual_wfl);
11304
11305 /* Pos evaluation: non WFL leading expression nodes */
11306 if (code == CONVERT_EXPR
11307 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
11308 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
11309
11310 else if (code == INTEGER_CST)
11311 name = qual_wfl;
11312
11313 else if (code == CONVERT_EXPR &&
11314 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11315 name = TREE_OPERAND (qual_wfl, 0);
11316
11317 else if (code == CONVERT_EXPR
11318 && TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == CALL_EXPR
11319 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (qual_wfl, 0), 0))
11320 == EXPR_WITH_FILE_LOCATION))
11321 name = TREE_OPERAND (TREE_OPERAND (qual_wfl, 0), 0);
11322
11323 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
11324 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11325 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
11326
11327 else if (code == TREE_LIST)
11328 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
11329
11330 else if (code == STRING_CST || code == CONDITIONAL_EXPR
11331 || code == PLUS_EXPR)
11332 {
11333 qual = TREE_CHAIN (qual);
11334 qual_wfl = QUAL_WFL (qual);
11335 again = 1;
11336 }
11337 else
11338 {
11339 name = EXPR_WFL_NODE (qual_wfl);
11340 if (!name)
11341 {
11342 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11343 again = 1;
11344 }
11345 }
11346
11347 /* If we have a THIS (from a primary), we set the context accordingly */
11348 if (name == this_identifier_node)
11349 {
11350 /* This isn't really elegant. One more added irregularity
11351 before I start using COMPONENT_REF (hopefully very soon.) */
11352 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
11353 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
11354 EXPR_WITH_FILE_LOCATION
11355 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
11356 this_identifier_node)
11357 {
11358 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
11359 qual = EXPR_WFL_QUALIFICATION (qual);
11360 }
11361 qual = TREE_CHAIN (qual);
11362 qual_wfl = QUAL_WFL (qual);
11363 if (TREE_CODE (qual_wfl) == CALL_EXPR)
11364 again = 1;
11365 else if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION)
11366 name = EXPR_WFL_NODE (qual_wfl);
11367 else if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
11368 name = TREE_OPERAND (qual_wfl, 0);
11369 this_found = 1;
11370 }
11371 /* If we have a SUPER, we set the context accordingly */
11372 if (name == super_identifier_node)
11373 {
11374 current_class = CLASSTYPE_SUPER (ptr_type);
11375 /* Check that there is such a thing as a super class. If not,
11376 return. The error will be caught later on, during the
11377 resolution */
11378 if (!current_class)
11379 {
11380 current_class = saved_current_class;
11381 return;
11382 }
11383 qual = TREE_CHAIN (qual);
11384 /* Do one more interation to set things up */
11385 super_found = again = 1;
11386 }
11387 } while (again);
11388
11389 /* If name appears within the scope of a local variable declaration
11390 or parameter declaration, then it is an expression name. We don't
11391 carry this test out if we're in the context of the use of SUPER
11392 or THIS */
11393 if (!this_found && !super_found
11394 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
11395 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
11396 {
11397 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11398 QUAL_RESOLUTION (qual) = decl;
11399 }
11400
11401 /* If within the class/interface NAME was found to be used there
11402 exists a (possibly inherited) field named NAME, then this is an
11403 expression name. If we saw a NEW_ARRAY_EXPR before and want to
11404 address length, it is OK. */
11405 else if ((decl = lookup_field_wrapper (ptr_type, name))
11406 || name == length_identifier_node)
11407 {
11408 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11409 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
11410 }
11411
11412 /* We reclassify NAME as yielding to a type name resolution if:
11413 - NAME is a class/interface declared within the compilation
11414 unit containing NAME,
11415 - NAME is imported via a single-type-import declaration,
11416 - NAME is declared in an another compilation unit of the package
11417 of the compilation unit containing NAME,
11418 - NAME is declared by exactly on type-import-on-demand declaration
11419 of the compilation unit containing NAME.
11420 - NAME is actually a STRING_CST.
11421 This can't happen if the expression was qualified by `this.' */
11422 else if (! this_found &&
11423 (TREE_CODE (name) == STRING_CST ||
11424 TREE_CODE (name) == INTEGER_CST ||
11425 (decl = resolve_and_layout (name, NULL_TREE))))
11426 {
11427 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11428 QUAL_RESOLUTION (qual) = decl;
11429 }
11430
11431 /* Method call, array references and cast are expression name */
11432 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
11433 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
11434 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR
11435 || TREE_CODE (QUAL_WFL (qual)) == MODIFY_EXPR)
11436 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11437
11438 /* Check here that NAME isn't declared by more than one
11439 type-import-on-demand declaration of the compilation unit
11440 containing NAME. FIXME */
11441
11442 /* Otherwise, NAME is reclassified as a package name */
11443 else
11444 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11445
11446 /* Propagate the qualification accross other components of the
11447 qualified name */
11448 for (qual = TREE_CHAIN (qual); qual;
11449 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11450 {
11451 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11452 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11453 else
11454 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
11455 }
11456
11457 /* Store the global qualification for the ambiguous part of ID back
11458 into ID fields */
11459 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
11460 RESOLVE_EXPRESSION_NAME_P (id) = 1;
11461 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
11462 RESOLVE_TYPE_NAME_P (id) = 1;
11463 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11464 RESOLVE_PACKAGE_NAME_P (id) = 1;
11465
11466 /* Restore the current class */
11467 current_class = saved_current_class;
11468 }
11469
11470 static int
11471 breakdown_qualified (left, right, source)
11472 tree *left, *right, source;
11473 {
11474 char *p, *base;
11475 int l = IDENTIFIER_LENGTH (source);
11476
11477 base = alloca (l + 1);
11478 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
11479
11480 /* Breakdown NAME into REMAINDER . IDENTIFIER. */
11481 p = base + l - 1;
11482 while (*p != '.' && p != base)
11483 p--;
11484
11485 /* We didn't find a '.'. Return an error. */
11486 if (p == base)
11487 return 1;
11488
11489 *p = '\0';
11490 if (right)
11491 *right = get_identifier (p+1);
11492 *left = get_identifier (base);
11493
11494 return 0;
11495 }
11496
11497 /* Return TRUE if two classes are from the same package. */
11498
11499 static int
11500 in_same_package (name1, name2)
11501 tree name1, name2;
11502 {
11503 tree tmp;
11504 tree pkg1;
11505 tree pkg2;
11506
11507 if (TREE_CODE (name1) == TYPE_DECL)
11508 name1 = DECL_NAME (name1);
11509 if (TREE_CODE (name2) == TYPE_DECL)
11510 name2 = DECL_NAME (name2);
11511
11512 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
11513 /* One in empty package. */
11514 return 0;
11515
11516 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
11517 /* Both in empty package. */
11518 return 1;
11519
11520 breakdown_qualified (&pkg1, &tmp, name1);
11521 breakdown_qualified (&pkg2, &tmp, name2);
11522
11523 return (pkg1 == pkg2);
11524 }
11525
11526 /* Patch tree nodes in a function body. When a BLOCK is found, push
11527 local variable decls if present.
11528 Same as java_complete_lhs, but does resolve static finals to values. */
11529
11530 static tree
11531 java_complete_tree (node)
11532 tree node;
11533 {
11534 node = java_complete_lhs (node);
11535 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11536 && DECL_INITIAL (node) != NULL_TREE
11537 && !flag_emit_xref)
11538 {
11539 tree value = fold_constant_for_init (node, node);
11540 if (value != NULL_TREE)
11541 return value;
11542 }
11543 return node;
11544 }
11545
11546 static tree
11547 java_stabilize_reference (node)
11548 tree node;
11549 {
11550 if (TREE_CODE (node) == COMPOUND_EXPR)
11551 {
11552 tree op0 = TREE_OPERAND (node, 0);
11553 tree op1 = TREE_OPERAND (node, 1);
11554 TREE_OPERAND (node, 0) = save_expr (op0);
11555 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11556 return node;
11557 }
11558 return stabilize_reference (node);
11559 }
11560
11561 /* Patch tree nodes in a function body. When a BLOCK is found, push
11562 local variable decls if present.
11563 Same as java_complete_tree, but does not resolve static finals to values. */
11564
11565 static tree
11566 java_complete_lhs (node)
11567 tree node;
11568 {
11569 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11570 int flag;
11571
11572 /* CONVERT_EXPR always has its type set, even though it needs to be
11573 worked out. */
11574 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11575 return node;
11576
11577 /* The switch block implements cases processing container nodes
11578 first. Contained nodes are always written back. Leaves come
11579 next and return a value. */
11580 switch (TREE_CODE (node))
11581 {
11582 case BLOCK:
11583
11584 /* 1- Block section.
11585 Set the local values on decl names so we can identify them
11586 faster when they're referenced. At that stage, identifiers
11587 are legal so we don't check for declaration errors. */
11588 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11589 {
11590 DECL_CONTEXT (cn) = current_function_decl;
11591 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11592 }
11593 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11594 CAN_COMPLETE_NORMALLY (node) = 1;
11595 else
11596 {
11597 tree stmt = BLOCK_EXPR_BODY (node);
11598 tree *ptr;
11599 int error_seen = 0;
11600 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11601 {
11602 /* Re-order from (((A; B); C); ...; Z) to
11603 (A; (B; (C ; (...; Z)))).
11604 This makes it easier to scan the statements left-to-right
11605 without using recursion (which might overflow the stack
11606 if the block has many statements. */
11607 for (;;)
11608 {
11609 tree left = TREE_OPERAND (stmt, 0);
11610 if (TREE_CODE (left) != COMPOUND_EXPR)
11611 break;
11612 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11613 TREE_OPERAND (left, 1) = stmt;
11614 stmt = left;
11615 }
11616 BLOCK_EXPR_BODY (node) = stmt;
11617 }
11618
11619 /* Now do the actual complete, without deep recursion for
11620 long blocks. */
11621 ptr = &BLOCK_EXPR_BODY (node);
11622 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11623 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
11624 {
11625 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11626 tree *next = &TREE_OPERAND (*ptr, 1);
11627 TREE_OPERAND (*ptr, 0) = cur;
11628 if (cur == empty_stmt_node)
11629 {
11630 /* Optimization; makes it easier to detect empty bodies.
11631 Most useful for <clinit> with all-constant initializer. */
11632 *ptr = *next;
11633 continue;
11634 }
11635 if (TREE_CODE (cur) == ERROR_MARK)
11636 error_seen++;
11637 else if (! CAN_COMPLETE_NORMALLY (cur))
11638 {
11639 wfl_op2 = *next;
11640 for (;;)
11641 {
11642 if (TREE_CODE (wfl_op2) == BLOCK)
11643 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11644 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11645 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11646 else
11647 break;
11648 }
11649 if (TREE_CODE (wfl_op2) != CASE_EXPR
11650 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11651 unreachable_stmt_error (*ptr);
11652 }
11653 ptr = next;
11654 }
11655 *ptr = java_complete_tree (*ptr);
11656
11657 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11658 return error_mark_node;
11659 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11660 }
11661 /* Turn local bindings to null */
11662 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11663 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11664
11665 TREE_TYPE (node) = void_type_node;
11666 break;
11667
11668 /* 2- They are expressions but ultimately deal with statements */
11669
11670 case THROW_EXPR:
11671 wfl_op1 = TREE_OPERAND (node, 0);
11672 COMPLETE_CHECK_OP_0 (node);
11673 /* 14.19 A throw statement cannot complete normally. */
11674 CAN_COMPLETE_NORMALLY (node) = 0;
11675 return patch_throw_statement (node, wfl_op1);
11676
11677 case SYNCHRONIZED_EXPR:
11678 wfl_op1 = TREE_OPERAND (node, 0);
11679 return patch_synchronized_statement (node, wfl_op1);
11680
11681 case TRY_EXPR:
11682 return patch_try_statement (node);
11683
11684 case TRY_FINALLY_EXPR:
11685 COMPLETE_CHECK_OP_0 (node);
11686 COMPLETE_CHECK_OP_1 (node);
11687 if (TREE_OPERAND (node, 0) == empty_stmt_node)
11688 return TREE_OPERAND (node, 1);
11689 if (TREE_OPERAND (node, 1) == empty_stmt_node)
11690 return TREE_OPERAND (node, 0);
11691 CAN_COMPLETE_NORMALLY (node)
11692 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11693 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11694 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11695 return node;
11696
11697 case LABELED_BLOCK_EXPR:
11698 PUSH_LABELED_BLOCK (node);
11699 if (LABELED_BLOCK_BODY (node))
11700 COMPLETE_CHECK_OP_1 (node);
11701 TREE_TYPE (node) = void_type_node;
11702 POP_LABELED_BLOCK ();
11703
11704 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11705 {
11706 LABELED_BLOCK_BODY (node) = NULL_TREE;
11707 CAN_COMPLETE_NORMALLY (node) = 1;
11708 }
11709 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11710 CAN_COMPLETE_NORMALLY (node) = 1;
11711 return node;
11712
11713 case EXIT_BLOCK_EXPR:
11714 /* We don't complete operand 1, because it's the return value of
11715 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11716 return patch_bc_statement (node);
11717
11718 case CASE_EXPR:
11719 cn = java_complete_tree (TREE_OPERAND (node, 0));
11720 if (cn == error_mark_node)
11721 return cn;
11722
11723 /* First, the case expression must be constant. Values of final
11724 fields are accepted. */
11725 cn = fold (cn);
11726 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11727 && JDECL_P (TREE_OPERAND (cn, 1))
11728 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11729 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11730 {
11731 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11732 TREE_OPERAND (cn, 1));
11733 }
11734 /* Accept final locals too. */
11735 else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn))
11736 cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11737
11738 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11739 {
11740 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11741 parse_error_context (node, "Constant expression required");
11742 return error_mark_node;
11743 }
11744
11745 nn = ctxp->current_loop;
11746
11747 /* It must be assignable to the type of the switch expression. */
11748 if (!try_builtin_assignconv (NULL_TREE,
11749 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11750 {
11751 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11752 parse_error_context
11753 (wfl_operator,
11754 "Incompatible type for case. Can't convert `%s' to `int'",
11755 lang_printable_name (TREE_TYPE (cn), 0));
11756 return error_mark_node;
11757 }
11758
11759 cn = fold (convert (int_type_node, cn));
11760 TREE_CONSTANT_OVERFLOW (cn) = 0;
11761 CAN_COMPLETE_NORMALLY (cn) = 1;
11762
11763 /* Save the label on a list so that we can later check for
11764 duplicates. */
11765 case_label_list = tree_cons (node, cn, case_label_list);
11766
11767 /* Multiple instance of a case label bearing the same value is
11768 checked later. The case expression is all right so far. */
11769 if (TREE_CODE (cn) == VAR_DECL)
11770 cn = DECL_INITIAL (cn);
11771 TREE_OPERAND (node, 0) = cn;
11772 TREE_TYPE (node) = void_type_node;
11773 CAN_COMPLETE_NORMALLY (node) = 1;
11774 TREE_SIDE_EFFECTS (node) = 1;
11775 break;
11776
11777 case DEFAULT_EXPR:
11778 nn = ctxp->current_loop;
11779 /* Only one default label is allowed per switch statement */
11780 if (SWITCH_HAS_DEFAULT (nn))
11781 {
11782 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11783 parse_error_context (wfl_operator,
11784 "Duplicate case label: `default'");
11785 return error_mark_node;
11786 }
11787 else
11788 SWITCH_HAS_DEFAULT (nn) = 1;
11789 TREE_TYPE (node) = void_type_node;
11790 TREE_SIDE_EFFECTS (node) = 1;
11791 CAN_COMPLETE_NORMALLY (node) = 1;
11792 break;
11793
11794 case SWITCH_EXPR:
11795 case LOOP_EXPR:
11796 PUSH_LOOP (node);
11797 /* Check whether the loop was enclosed in a labeled
11798 statement. If not, create one, insert the loop in it and
11799 return the node */
11800 nn = patch_loop_statement (node);
11801
11802 /* Anyways, walk the body of the loop */
11803 if (TREE_CODE (node) == LOOP_EXPR)
11804 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11805 /* Switch statement: walk the switch expression and the cases */
11806 else
11807 node = patch_switch_statement (node);
11808
11809 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11810 nn = error_mark_node;
11811 else
11812 {
11813 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11814 /* If we returned something different, that's because we
11815 inserted a label. Pop the label too. */
11816 if (nn != node)
11817 {
11818 if (CAN_COMPLETE_NORMALLY (node))
11819 CAN_COMPLETE_NORMALLY (nn) = 1;
11820 POP_LABELED_BLOCK ();
11821 }
11822 }
11823 POP_LOOP ();
11824 return nn;
11825
11826 case EXIT_EXPR:
11827 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11828 return patch_exit_expr (node);
11829
11830 case COND_EXPR:
11831 /* Condition */
11832 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11833 if (TREE_OPERAND (node, 0) == error_mark_node)
11834 return error_mark_node;
11835 /* then-else branches */
11836 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11837 if (TREE_OPERAND (node, 1) == error_mark_node)
11838 return error_mark_node;
11839 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11840 if (TREE_OPERAND (node, 2) == error_mark_node)
11841 return error_mark_node;
11842 return patch_if_else_statement (node);
11843 break;
11844
11845 case CONDITIONAL_EXPR:
11846 /* Condition */
11847 wfl_op1 = TREE_OPERAND (node, 0);
11848 COMPLETE_CHECK_OP_0 (node);
11849 wfl_op2 = TREE_OPERAND (node, 1);
11850 COMPLETE_CHECK_OP_1 (node);
11851 wfl_op3 = TREE_OPERAND (node, 2);
11852 COMPLETE_CHECK_OP_2 (node);
11853 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11854
11855 /* 3- Expression section */
11856 case COMPOUND_EXPR:
11857 wfl_op2 = TREE_OPERAND (node, 1);
11858 TREE_OPERAND (node, 0) = nn =
11859 java_complete_tree (TREE_OPERAND (node, 0));
11860 if (wfl_op2 == empty_stmt_node)
11861 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11862 else
11863 {
11864 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11865 {
11866 /* An unreachable condition in a do-while statement
11867 is *not* (technically) an unreachable statement. */
11868 nn = wfl_op2;
11869 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11870 nn = EXPR_WFL_NODE (nn);
11871 if (TREE_CODE (nn) != EXIT_EXPR)
11872 {
11873 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11874 if (SUPPRESS_UNREACHABLE_ERROR (nn))
11875 {
11876 /* Perhaps this warning should have an
11877 associated flag. The code being compiled is
11878 pedantically correct, but useless. */
11879 parse_warning_context (wfl_operator,
11880 "Unreachable statement");
11881 }
11882 else
11883 parse_error_context (wfl_operator,
11884 "Unreachable statement");
11885 }
11886 }
11887 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11888 if (TREE_OPERAND (node, 1) == error_mark_node)
11889 return error_mark_node;
11890 /* Even though we might allow the case where the first
11891 operand doesn't return normally, we still should compute
11892 CAN_COMPLETE_NORMALLY correctly. */
11893 CAN_COMPLETE_NORMALLY (node)
11894 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11895 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11896 }
11897 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11898 break;
11899
11900 case RETURN_EXPR:
11901 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11902 return patch_return (node);
11903
11904 case EXPR_WITH_FILE_LOCATION:
11905 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11906 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11907 {
11908 tree wfl = node;
11909 node = resolve_expression_name (node, NULL);
11910 if (node == error_mark_node)
11911 return node;
11912 /* Keep line number information somewhere were it doesn't
11913 disrupt the completion process. */
11914 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11915 {
11916 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11917 TREE_OPERAND (node, 1) = wfl;
11918 }
11919 CAN_COMPLETE_NORMALLY (node) = 1;
11920 }
11921 else
11922 {
11923 tree body;
11924 int save_lineno = lineno;
11925 lineno = EXPR_WFL_LINENO (node);
11926 body = java_complete_tree (EXPR_WFL_NODE (node));
11927 lineno = save_lineno;
11928 EXPR_WFL_NODE (node) = body;
11929 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11930 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11931 if (body == empty_stmt_node || TREE_CONSTANT (body))
11932 {
11933 /* Makes it easier to constant fold, detect empty bodies. */
11934 return body;
11935 }
11936 if (body == error_mark_node)
11937 {
11938 /* Its important for the evaluation of assignment that
11939 this mark on the TREE_TYPE is propagated. */
11940 TREE_TYPE (node) = error_mark_node;
11941 return error_mark_node;
11942 }
11943 else
11944 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11945
11946 }
11947 break;
11948
11949 case NEW_ARRAY_EXPR:
11950 /* Patch all the dimensions */
11951 flag = 0;
11952 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11953 {
11954 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11955 tree dim = convert (int_type_node,
11956 java_complete_tree (TREE_VALUE (cn)));
11957 if (dim == error_mark_node)
11958 {
11959 flag = 1;
11960 continue;
11961 }
11962 else
11963 {
11964 TREE_VALUE (cn) = dim;
11965 /* Setup the location of the current dimension, for
11966 later error report. */
11967 TREE_PURPOSE (cn) =
11968 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11969 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11970 }
11971 }
11972 /* They complete the array creation expression, if no errors
11973 were found. */
11974 CAN_COMPLETE_NORMALLY (node) = 1;
11975 return (flag ? error_mark_node
11976 : force_evaluation_order (patch_newarray (node)));
11977
11978 case NEW_ANONYMOUS_ARRAY_EXPR:
11979 /* Create the array type if necessary. */
11980 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11981 {
11982 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11983 if (!(type = resolve_type_during_patch (type)))
11984 return error_mark_node;
11985 type = build_array_from_name (type, NULL_TREE,
11986 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11987 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11988 }
11989 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11990 ANONYMOUS_ARRAY_INITIALIZER (node));
11991 if (node == error_mark_node)
11992 return error_mark_node;
11993 CAN_COMPLETE_NORMALLY (node) = 1;
11994 return node;
11995
11996 case NEW_CLASS_EXPR:
11997 case CALL_EXPR:
11998 /* Complete function's argument(s) first */
11999 if (complete_function_arguments (node))
12000 return error_mark_node;
12001 else
12002 {
12003 tree decl, wfl = TREE_OPERAND (node, 0);
12004 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
12005 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
12006 super_identifier_node);
12007
12008 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
12009 from_super, 0, &decl);
12010 if (node == error_mark_node)
12011 return error_mark_node;
12012
12013 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
12014 /* If we call this(...), register signature and positions */
12015 if (in_this)
12016 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
12017 tree_cons (wfl, decl,
12018 DECL_CONSTRUCTOR_CALLS (current_function_decl));
12019 CAN_COMPLETE_NORMALLY (node) = 1;
12020 return force_evaluation_order (node);
12021 }
12022
12023 case MODIFY_EXPR:
12024 /* Save potential wfls */
12025 wfl_op1 = TREE_OPERAND (node, 0);
12026 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
12027
12028 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
12029 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
12030 && DECL_INITIAL (nn) != NULL_TREE)
12031 {
12032 tree value;
12033
12034 value = fold_constant_for_init (nn, nn);
12035
12036 /* When we have a primitype type, or a string and we're not
12037 emitting a class file, we actually don't want to generate
12038 anything for the assignment. */
12039 if (value != NULL_TREE &&
12040 (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) ||
12041 (TREE_TYPE (value) == string_ptr_type_node &&
12042 ! flag_emit_class_files)))
12043 {
12044 /* Prepare node for patch_assignment */
12045 TREE_OPERAND (node, 1) = value;
12046 /* Call patch assignment to verify the assignment */
12047 if (patch_assignment (node, wfl_op1) == error_mark_node)
12048 return error_mark_node;
12049 /* Set DECL_INITIAL properly (a conversion might have
12050 been decided by patch_assignment) and return the
12051 empty statement. */
12052 else
12053 {
12054 tree patched = patch_string (TREE_OPERAND (node, 1));
12055 if (patched)
12056 DECL_INITIAL (nn) = patched;
12057 else
12058 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12059 DECL_FIELD_FINAL_IUD (nn) = 1;
12060 return empty_stmt_node;
12061 }
12062 }
12063 if (! flag_emit_class_files)
12064 DECL_INITIAL (nn) = NULL_TREE;
12065 }
12066 wfl_op2 = TREE_OPERAND (node, 1);
12067
12068 if (TREE_OPERAND (node, 0) == error_mark_node)
12069 return error_mark_node;
12070
12071 flag = COMPOUND_ASSIGN_P (wfl_op2);
12072 if (flag)
12073 {
12074 /* This might break when accessing outer field from inner
12075 class. TESTME, FIXME */
12076 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
12077
12078 /* Hand stabilize the lhs on both places */
12079 TREE_OPERAND (node, 0) = lvalue;
12080 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
12081 (flag_emit_class_files ? lvalue : save_expr (lvalue));
12082
12083 /* 15.25.2.a: Left hand is not an array access. FIXME */
12084 /* Now complete the RHS. We write it back later on. */
12085 nn = java_complete_tree (TREE_OPERAND (node, 1));
12086
12087 if ((cn = patch_string (nn)))
12088 nn = cn;
12089
12090 /* The last part of the rewrite for E1 op= E2 is to have
12091 E1 = (T)(E1 op E2), with T being the type of E1. */
12092 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
12093 TREE_TYPE (lvalue), nn));
12094
12095 /* If the assignment is compound and has reference type,
12096 then ensure the LHS has type String and nothing else. */
12097 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
12098 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
12099 parse_error_context (wfl_op2,
12100 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
12101 lang_printable_name (TREE_TYPE (lvalue), 0));
12102
12103 /* 15.25.2.b: Left hand is an array access. FIXME */
12104 }
12105
12106 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12107 function to complete this RHS. Note that a NEW_ARRAY_INIT
12108 might have been already fully expanded if created as a result
12109 of processing an anonymous array initializer. We avoid doing
12110 the operation twice by testing whether the node already bears
12111 a type. */
12112 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12113 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12114 TREE_OPERAND (node, 1));
12115 /* Otherwise we simply complete the RHS */
12116 else
12117 nn = java_complete_tree (TREE_OPERAND (node, 1));
12118
12119 if (nn == error_mark_node)
12120 return error_mark_node;
12121
12122 /* Write back the RHS as we evaluated it. */
12123 TREE_OPERAND (node, 1) = nn;
12124
12125 /* In case we're handling = with a String as a RHS, we need to
12126 produce a String out of the RHS (it might still be a
12127 STRING_CST or a StringBuffer at this stage */
12128 if ((nn = patch_string (TREE_OPERAND (node, 1))))
12129 TREE_OPERAND (node, 1) = nn;
12130
12131 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12132 TREE_OPERAND (node, 1))))
12133 {
12134 /* We return error_mark_node if outer_field_access_fix
12135 detects we write into a final. */
12136 if (nn == error_mark_node)
12137 return error_mark_node;
12138 node = nn;
12139 }
12140 else
12141 {
12142 node = patch_assignment (node, wfl_op1);
12143 if (node == error_mark_node)
12144 return error_mark_node;
12145 /* Reorganize the tree if necessary. */
12146 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
12147 || JSTRING_P (TREE_TYPE (node))))
12148 node = java_refold (node);
12149 }
12150
12151 /* Seek to set DECL_INITIAL to a proper value, since it might have
12152 undergone a conversion in patch_assignment. We do that only when
12153 it's necessary to have DECL_INITIAL properly set. */
12154 nn = TREE_OPERAND (node, 0);
12155 if (TREE_CODE (nn) == VAR_DECL
12156 && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12157 && FIELD_STATIC (nn) && FIELD_FINAL (nn)
12158 && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12159 || TREE_TYPE (nn) == string_ptr_type_node))
12160 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12161
12162 CAN_COMPLETE_NORMALLY (node) = 1;
12163 return node;
12164
12165 case MULT_EXPR:
12166 case PLUS_EXPR:
12167 case MINUS_EXPR:
12168 case LSHIFT_EXPR:
12169 case RSHIFT_EXPR:
12170 case URSHIFT_EXPR:
12171 case BIT_AND_EXPR:
12172 case BIT_XOR_EXPR:
12173 case BIT_IOR_EXPR:
12174 case TRUNC_MOD_EXPR:
12175 case TRUNC_DIV_EXPR:
12176 case RDIV_EXPR:
12177 case TRUTH_ANDIF_EXPR:
12178 case TRUTH_ORIF_EXPR:
12179 case EQ_EXPR:
12180 case NE_EXPR:
12181 case GT_EXPR:
12182 case GE_EXPR:
12183 case LT_EXPR:
12184 case LE_EXPR:
12185 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12186 knows how to handle those cases. */
12187 wfl_op1 = TREE_OPERAND (node, 0);
12188 wfl_op2 = TREE_OPERAND (node, 1);
12189
12190 CAN_COMPLETE_NORMALLY (node) = 1;
12191 /* Don't complete string nodes if dealing with the PLUS operand. */
12192 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12193 {
12194 nn = java_complete_tree (wfl_op1);
12195 if (nn == error_mark_node)
12196 return error_mark_node;
12197
12198 TREE_OPERAND (node, 0) = nn;
12199 }
12200 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12201 {
12202 nn = java_complete_tree (wfl_op2);
12203 if (nn == error_mark_node)
12204 return error_mark_node;
12205
12206 TREE_OPERAND (node, 1) = nn;
12207 }
12208 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
12209
12210 case INSTANCEOF_EXPR:
12211 wfl_op1 = TREE_OPERAND (node, 0);
12212 COMPLETE_CHECK_OP_0 (node);
12213 if (flag_emit_xref)
12214 {
12215 TREE_TYPE (node) = boolean_type_node;
12216 return node;
12217 }
12218 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
12219
12220 case UNARY_PLUS_EXPR:
12221 case NEGATE_EXPR:
12222 case TRUTH_NOT_EXPR:
12223 case BIT_NOT_EXPR:
12224 case PREDECREMENT_EXPR:
12225 case PREINCREMENT_EXPR:
12226 case POSTDECREMENT_EXPR:
12227 case POSTINCREMENT_EXPR:
12228 case CONVERT_EXPR:
12229 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12230 how to handle those cases. */
12231 wfl_op1 = TREE_OPERAND (node, 0);
12232 CAN_COMPLETE_NORMALLY (node) = 1;
12233 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12234 if (TREE_OPERAND (node, 0) == error_mark_node)
12235 return error_mark_node;
12236 node = patch_unaryop (node, wfl_op1);
12237 CAN_COMPLETE_NORMALLY (node) = 1;
12238 break;
12239
12240 case ARRAY_REF:
12241 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12242 how to handle those cases. */
12243 wfl_op1 = TREE_OPERAND (node, 0);
12244 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12245 if (TREE_OPERAND (node, 0) == error_mark_node)
12246 return error_mark_node;
12247 if (!flag_emit_class_files && !flag_emit_xref)
12248 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12249 /* The same applies to wfl_op2 */
12250 wfl_op2 = TREE_OPERAND (node, 1);
12251 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12252 if (TREE_OPERAND (node, 1) == error_mark_node)
12253 return error_mark_node;
12254 if (!flag_emit_class_files && !flag_emit_xref)
12255 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12256 return patch_array_ref (node);
12257
12258 case RECORD_TYPE:
12259 return node;;
12260
12261 case COMPONENT_REF:
12262 /* The first step in the re-write of qualified name handling. FIXME.
12263 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
12264 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
12265 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
12266 {
12267 tree name = TREE_OPERAND (node, 1);
12268 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
12269 if (field == NULL_TREE)
12270 {
12271 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
12272 return error_mark_node;
12273 }
12274 if (! FIELD_STATIC (field))
12275 {
12276 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
12277 return error_mark_node;
12278 }
12279 return field;
12280 }
12281 else
12282 abort ();
12283 break;
12284
12285 case THIS_EXPR:
12286 /* Can't use THIS in a static environment */
12287 if (!current_this)
12288 {
12289 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12290 parse_error_context (wfl_operator,
12291 "Keyword `this' used outside allowed context");
12292 TREE_TYPE (node) = error_mark_node;
12293 return error_mark_node;
12294 }
12295 if (ctxp->explicit_constructor_p)
12296 {
12297 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12298 parse_error_context
12299 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
12300 TREE_TYPE (node) = error_mark_node;
12301 return error_mark_node;
12302 }
12303 return current_this;
12304
12305 case CLASS_LITERAL:
12306 CAN_COMPLETE_NORMALLY (node) = 1;
12307 node = patch_incomplete_class_ref (node);
12308 if (node == error_mark_node)
12309 return error_mark_node;
12310 break;
12311
12312 default:
12313 CAN_COMPLETE_NORMALLY (node) = 1;
12314 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12315 and it's time to turn it into the appropriate String object */
12316 if ((nn = patch_string (node)))
12317 node = nn;
12318 else
12319 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12320 }
12321 return node;
12322 }
12323
12324 /* Complete function call's argument. Return a nonzero value is an
12325 error was found. */
12326
12327 static int
12328 complete_function_arguments (node)
12329 tree node;
12330 {
12331 int flag = 0;
12332 tree cn;
12333
12334 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12335 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12336 {
12337 tree wfl = TREE_VALUE (cn), parm, temp;
12338 parm = java_complete_tree (wfl);
12339
12340 if (parm == error_mark_node)
12341 {
12342 flag = 1;
12343 continue;
12344 }
12345 /* If have a string literal that we haven't transformed yet or a
12346 crafted string buffer, as a result of use of the the String
12347 `+' operator. Build `parm.toString()' and expand it. */
12348 if ((temp = patch_string (parm)))
12349 parm = temp;
12350
12351 TREE_VALUE (cn) = parm;
12352 }
12353 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12354 return flag;
12355 }
12356
12357 /* Sometimes (for loops and variable initialized during their
12358 declaration), we want to wrap a statement around a WFL and turn it
12359 debugable. */
12360
12361 static tree
12362 build_debugable_stmt (location, stmt)
12363 int location;
12364 tree stmt;
12365 {
12366 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12367 {
12368 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12369 EXPR_WFL_LINECOL (stmt) = location;
12370 }
12371 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12372 return stmt;
12373 }
12374
12375 static tree
12376 build_expr_block (body, decls)
12377 tree body, decls;
12378 {
12379 tree node = make_node (BLOCK);
12380 BLOCK_EXPR_DECLS (node) = decls;
12381 BLOCK_EXPR_BODY (node) = body;
12382 if (body)
12383 TREE_TYPE (node) = TREE_TYPE (body);
12384 TREE_SIDE_EFFECTS (node) = 1;
12385 return node;
12386 }
12387
12388 /* Create a new function block and link it appropriately to current
12389 function block chain */
12390
12391 static tree
12392 enter_block ()
12393 {
12394 tree b = build_expr_block (NULL_TREE, NULL_TREE);
12395
12396 /* Link block B supercontext to the previous block. The current
12397 function DECL is used as supercontext when enter_a_block is called
12398 for the first time for a given function. The current function body
12399 (DECL_FUNCTION_BODY) is set to be block B. */
12400
12401 tree fndecl = current_function_decl;
12402
12403 if (!fndecl) {
12404 BLOCK_SUPERCONTEXT (b) = current_static_block;
12405 current_static_block = b;
12406 }
12407
12408 else if (!DECL_FUNCTION_BODY (fndecl))
12409 {
12410 BLOCK_SUPERCONTEXT (b) = fndecl;
12411 DECL_FUNCTION_BODY (fndecl) = b;
12412 }
12413 else
12414 {
12415 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12416 DECL_FUNCTION_BODY (fndecl) = b;
12417 }
12418 return b;
12419 }
12420
12421 /* Exit a block by changing the current function body
12422 (DECL_FUNCTION_BODY) to the current block super context, only if
12423 the block being exited isn't the method's top level one. */
12424
12425 static tree
12426 exit_block ()
12427 {
12428 tree b;
12429 if (current_function_decl)
12430 {
12431 b = DECL_FUNCTION_BODY (current_function_decl);
12432 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12433 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12434 }
12435 else
12436 {
12437 b = current_static_block;
12438
12439 if (BLOCK_SUPERCONTEXT (b))
12440 current_static_block = BLOCK_SUPERCONTEXT (b);
12441 }
12442 return b;
12443 }
12444
12445 /* Lookup for NAME in the nested function's blocks, all the way up to
12446 the current toplevel one. It complies with Java's local variable
12447 scoping rules. */
12448
12449 static tree
12450 lookup_name_in_blocks (name)
12451 tree name;
12452 {
12453 tree b = GET_CURRENT_BLOCK (current_function_decl);
12454
12455 while (b != current_function_decl)
12456 {
12457 tree current;
12458
12459 /* Paranoid sanity check. To be removed */
12460 if (TREE_CODE (b) != BLOCK)
12461 abort ();
12462
12463 for (current = BLOCK_EXPR_DECLS (b); current;
12464 current = TREE_CHAIN (current))
12465 if (DECL_NAME (current) == name)
12466 return current;
12467 b = BLOCK_SUPERCONTEXT (b);
12468 }
12469 return NULL_TREE;
12470 }
12471
12472 static void
12473 maybe_absorb_scoping_blocks ()
12474 {
12475 while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12476 {
12477 tree b = exit_block ();
12478 java_method_add_stmt (current_function_decl, b);
12479 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
12480 }
12481 }
12482
12483 \f
12484 /* This section of the source is reserved to build_* functions that
12485 are building incomplete tree nodes and the patch_* functions that
12486 are completing them. */
12487
12488 /* Wrap a non WFL node around a WFL. */
12489
12490 static tree
12491 build_wfl_wrap (node, location)
12492 tree node;
12493 int location;
12494 {
12495 tree wfl, node_to_insert = node;
12496
12497 /* We want to process THIS . xxx symbolicaly, to keep it consistent
12498 with the way we're processing SUPER. A THIS from a primary as a
12499 different form than a SUPER. Turn THIS into something symbolic */
12500 if (TREE_CODE (node) == THIS_EXPR)
12501 node_to_insert = wfl = build_wfl_node (this_identifier_node);
12502 else
12503 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12504
12505 EXPR_WFL_LINECOL (wfl) = location;
12506 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12507 return wfl;
12508 }
12509
12510 /* Build a super() constructor invocation. Returns empty_stmt_node if
12511 we're currently dealing with the class java.lang.Object. */
12512
12513 static tree
12514 build_super_invocation (mdecl)
12515 tree mdecl;
12516 {
12517 if (DECL_CONTEXT (mdecl) == object_type_node)
12518 return empty_stmt_node;
12519 else
12520 {
12521 tree super_wfl = build_wfl_node (super_identifier_node);
12522 tree a = NULL_TREE, t;
12523 /* If we're dealing with an anonymous class, pass the arguments
12524 of the crafted constructor along. */
12525 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12526 {
12527 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12528 for (; t != end_params_node; t = TREE_CHAIN (t))
12529 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12530 }
12531 return build_method_invocation (super_wfl, a);
12532 }
12533 }
12534
12535 /* Build a SUPER/THIS qualified method invocation. */
12536
12537 static tree
12538 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
12539 int use_this;
12540 tree name, args;
12541 int lloc, rloc;
12542 {
12543 tree invok;
12544 tree wfl =
12545 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12546 EXPR_WFL_LINECOL (wfl) = lloc;
12547 invok = build_method_invocation (name, args);
12548 return make_qualified_primary (wfl, invok, rloc);
12549 }
12550
12551 /* Build an incomplete CALL_EXPR node. */
12552
12553 static tree
12554 build_method_invocation (name, args)
12555 tree name;
12556 tree args;
12557 {
12558 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12559 TREE_SIDE_EFFECTS (call) = 1;
12560 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12561 return call;
12562 }
12563
12564 /* Build an incomplete new xxx(...) node. */
12565
12566 static tree
12567 build_new_invocation (name, args)
12568 tree name, args;
12569 {
12570 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12571 TREE_SIDE_EFFECTS (call) = 1;
12572 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12573 return call;
12574 }
12575
12576 /* Build an incomplete assignment expression. */
12577
12578 static tree
12579 build_assignment (op, op_location, lhs, rhs)
12580 int op, op_location;
12581 tree lhs, rhs;
12582 {
12583 tree assignment;
12584 /* Build the corresponding binop if we deal with a Compound
12585 Assignment operator. Mark the binop sub-tree as part of a
12586 Compound Assignment expression */
12587 if (op != ASSIGN_TK)
12588 {
12589 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12590 COMPOUND_ASSIGN_P (rhs) = 1;
12591 }
12592 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12593 TREE_SIDE_EFFECTS (assignment) = 1;
12594 EXPR_WFL_LINECOL (assignment) = op_location;
12595 return assignment;
12596 }
12597
12598 /* Print an INTEGER_CST node as decimal in a static buffer, and return
12599 the buffer. This is used only for string conversion. */
12600 static char *
12601 string_convert_int_cst (node)
12602 tree node;
12603 {
12604 static char buffer[80];
12605
12606 unsigned HOST_WIDE_INT lo = TREE_INT_CST_LOW (node);
12607 unsigned HOST_WIDE_INT hi = TREE_INT_CST_HIGH (node);
12608 char *p = buffer + sizeof (buffer) - 1;
12609 int neg = 0;
12610
12611 unsigned HOST_WIDE_INT hibit = (((unsigned HOST_WIDE_INT) 1)
12612 << (HOST_BITS_PER_WIDE_INT - 1));
12613
12614 *p-- = '\0';
12615
12616 /* If negative, note the fact and negate the value. */
12617 if ((hi & hibit))
12618 {
12619 lo = ~lo;
12620 hi = ~hi;
12621 if (++lo == 0)
12622 ++hi;
12623 neg = 1;
12624 }
12625
12626 /* Divide by 10 until there are no bits left. */
12627 while (hi || lo)
12628 {
12629 unsigned HOST_WIDE_INT acc = 0;
12630 unsigned HOST_WIDE_INT outhi = 0, outlo = 0;
12631 unsigned int i;
12632
12633 /* Use long division to compute the result and the remainder. */
12634 for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; ++i)
12635 {
12636 /* Shift a bit into accumulator. */
12637 acc <<= 1;
12638 if ((hi & hibit))
12639 acc |= 1;
12640
12641 /* Shift the value. */
12642 hi <<= 1;
12643 if ((lo & hibit))
12644 hi |= 1;
12645 lo <<= 1;
12646
12647 /* Shift the correct bit into the result. */
12648 outhi <<= 1;
12649 if ((outlo & hibit))
12650 outhi |= 1;
12651 outlo <<= 1;
12652 if (acc >= 10)
12653 {
12654 acc -= 10;
12655 outlo |= 1;
12656 }
12657 }
12658
12659 /* FIXME: ASCII assumption. */
12660 *p-- = '0' + acc;
12661
12662 hi = outhi;
12663 lo = outlo;
12664 }
12665
12666 if (neg)
12667 *p-- = '-';
12668
12669 return p + 1;
12670 }
12671
12672 /* Print an INTEGER_CST node in a static buffer, and return the
12673 buffer. This is used only for error handling. */
12674 char *
12675 print_int_node (node)
12676 tree node;
12677 {
12678 static char buffer [80];
12679 if (TREE_CONSTANT_OVERFLOW (node))
12680 sprintf (buffer, "<overflow>");
12681
12682 if (TREE_INT_CST_HIGH (node) == 0)
12683 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12684 TREE_INT_CST_LOW (node));
12685 else if (TREE_INT_CST_HIGH (node) == -1
12686 && TREE_INT_CST_LOW (node) != 0)
12687 {
12688 buffer [0] = '-';
12689 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12690 -TREE_INT_CST_LOW (node));
12691 }
12692 else
12693 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12694 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12695
12696 return buffer;
12697 }
12698
12699 \f
12700 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12701 context. */
12702
12703 /* 15.25 Assignment operators. */
12704
12705 static tree
12706 patch_assignment (node, wfl_op1)
12707 tree node;
12708 tree wfl_op1;
12709 {
12710 tree rhs = TREE_OPERAND (node, 1);
12711 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12712 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12713 int error_found = 0;
12714 int lvalue_from_array = 0;
12715 int is_return = 0;
12716
12717 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12718
12719 /* Lhs can be a named variable */
12720 if (JDECL_P (lvalue))
12721 {
12722 lhs_type = TREE_TYPE (lvalue);
12723 }
12724 /* Or Lhs can be an array access. */
12725 else if (TREE_CODE (lvalue) == ARRAY_REF)
12726 {
12727 lhs_type = TREE_TYPE (lvalue);
12728 lvalue_from_array = 1;
12729 }
12730 /* Or a field access */
12731 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12732 lhs_type = TREE_TYPE (lvalue);
12733 /* Or a function return slot */
12734 else if (TREE_CODE (lvalue) == RESULT_DECL)
12735 {
12736 /* If the return type is an integral type, then we create the
12737 RESULT_DECL with a promoted type, but we need to do these
12738 checks against the unpromoted type to ensure type safety. So
12739 here we look at the real type, not the type of the decl we
12740 are modifying. */
12741 lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12742 is_return = 1;
12743 }
12744 /* Otherwise, we might want to try to write into an optimized static
12745 final, this is an of a different nature, reported further on. */
12746 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12747 && resolve_expression_name (wfl_op1, &llvalue))
12748 {
12749 lhs_type = TREE_TYPE (lvalue);
12750 }
12751 else
12752 {
12753 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12754 error_found = 1;
12755 }
12756
12757 rhs_type = TREE_TYPE (rhs);
12758
12759 /* 5.1 Try the assignment conversion for builtin type. */
12760 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12761
12762 /* 5.2 If it failed, try a reference conversion */
12763 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12764 lhs_type = promote_type (rhs_type);
12765
12766 /* 15.25.2 If we have a compound assignment, convert RHS into the
12767 type of the LHS */
12768 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12769 new_rhs = convert (lhs_type, rhs);
12770
12771 /* Explicit cast required. This is an error */
12772 if (!new_rhs)
12773 {
12774 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12775 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12776 tree wfl;
12777 char operation [32]; /* Max size known */
12778
12779 /* If the assignment is part of a declaration, we use the WFL of
12780 the declared variable to point out the error and call it a
12781 declaration problem. If the assignment is a genuine =
12782 operator, we call is a operator `=' problem, otherwise we
12783 call it an assignment problem. In both of these last cases,
12784 we use the WFL of the operator to indicate the error. */
12785
12786 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12787 {
12788 wfl = wfl_op1;
12789 strcpy (operation, "declaration");
12790 }
12791 else
12792 {
12793 wfl = wfl_operator;
12794 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12795 strcpy (operation, "assignment");
12796 else if (is_return)
12797 strcpy (operation, "`return'");
12798 else
12799 strcpy (operation, "`='");
12800 }
12801
12802 if (!valid_cast_to_p (rhs_type, lhs_type))
12803 parse_error_context
12804 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12805 operation, t1, t2);
12806 else
12807 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12808 operation, t1, t2);
12809 free (t1); free (t2);
12810 error_found = 1;
12811 }
12812
12813 if (error_found)
12814 return error_mark_node;
12815
12816 /* If we're processing a `return' statement, promote the actual type
12817 to the promoted type. */
12818 if (is_return)
12819 new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12820
12821 /* 10.10: Array Store Exception runtime check */
12822 if (!flag_emit_class_files
12823 && !flag_emit_xref
12824 && lvalue_from_array
12825 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12826 {
12827 tree array, store_check, base, index_expr;
12828
12829 /* Save RHS so that it doesn't get re-evaluated by the store check. */
12830 new_rhs = save_expr (new_rhs);
12831
12832 /* Get the INDIRECT_REF. */
12833 array = TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0);
12834 /* Get the array pointer expr. */
12835 array = TREE_OPERAND (array, 0);
12836 store_check = build_java_arraystore_check (array, new_rhs);
12837
12838 index_expr = TREE_OPERAND (lvalue, 1);
12839
12840 if (TREE_CODE (index_expr) == COMPOUND_EXPR)
12841 {
12842 /* A COMPOUND_EXPR here is a bounds check. The bounds check must
12843 happen before the store check, so prepare to insert the store
12844 check within the second operand of the existing COMPOUND_EXPR. */
12845 base = index_expr;
12846 }
12847 else
12848 base = lvalue;
12849
12850 index_expr = TREE_OPERAND (base, 1);
12851 TREE_OPERAND (base, 1) = build (COMPOUND_EXPR, TREE_TYPE (index_expr),
12852 store_check, index_expr);
12853 }
12854
12855 /* Final locals can be used as case values in switch
12856 statement. Prepare them for this eventuality. */
12857 if (TREE_CODE (lvalue) == VAR_DECL
12858 && DECL_FINAL (lvalue)
12859 && TREE_CONSTANT (new_rhs)
12860 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12861 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12862 )
12863 {
12864 TREE_CONSTANT (lvalue) = 1;
12865 DECL_INITIAL (lvalue) = new_rhs;
12866 }
12867
12868 TREE_OPERAND (node, 0) = lvalue;
12869 TREE_OPERAND (node, 1) = new_rhs;
12870 TREE_TYPE (node) = lhs_type;
12871 return node;
12872 }
12873
12874 /* Check that type SOURCE can be cast into type DEST. If the cast
12875 can't occur at all, return NULL; otherwise, return a possibly
12876 modified rhs. */
12877
12878 static tree
12879 try_reference_assignconv (lhs_type, rhs)
12880 tree lhs_type, rhs;
12881 {
12882 tree new_rhs = NULL_TREE;
12883 tree rhs_type = TREE_TYPE (rhs);
12884
12885 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12886 {
12887 /* `null' may be assigned to any reference type */
12888 if (rhs == null_pointer_node)
12889 new_rhs = null_pointer_node;
12890 /* Try the reference assignment conversion */
12891 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12892 new_rhs = rhs;
12893 /* This is a magic assignment that we process differently */
12894 else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
12895 new_rhs = rhs;
12896 }
12897 return new_rhs;
12898 }
12899
12900 /* Check that RHS can be converted into LHS_TYPE by the assignment
12901 conversion (5.2), for the cases of RHS being a builtin type. Return
12902 NULL_TREE if the conversion fails or if because RHS isn't of a
12903 builtin type. Return a converted RHS if the conversion is possible. */
12904
12905 static tree
12906 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12907 tree wfl_op1, lhs_type, rhs;
12908 {
12909 tree new_rhs = NULL_TREE;
12910 tree rhs_type = TREE_TYPE (rhs);
12911
12912 /* Handle boolean specially. */
12913 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12914 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12915 {
12916 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12917 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12918 new_rhs = rhs;
12919 }
12920
12921 /* 5.1.1 Try Identity Conversion,
12922 5.1.2 Try Widening Primitive Conversion */
12923 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12924 new_rhs = convert (lhs_type, rhs);
12925
12926 /* Try a narrowing primitive conversion (5.1.3):
12927 - expression is a constant expression of type byte, short, char,
12928 or int, AND
12929 - variable is byte, short or char AND
12930 - The value of the expression is representable in the type of the
12931 variable */
12932 else if ((rhs_type == byte_type_node || rhs_type == short_type_node
12933 || rhs_type == char_type_node || rhs_type == int_type_node)
12934 && TREE_CONSTANT (rhs)
12935 && (lhs_type == byte_type_node || lhs_type == char_type_node
12936 || lhs_type == short_type_node))
12937 {
12938 if (int_fits_type_p (rhs, lhs_type))
12939 new_rhs = convert (lhs_type, rhs);
12940 else if (wfl_op1) /* Might be called with a NULL */
12941 parse_warning_context
12942 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
12943 print_int_node (rhs), lang_printable_name (lhs_type, 0));
12944 /* Reported a warning that will turn into an error further
12945 down, so we don't return */
12946 }
12947
12948 return new_rhs;
12949 }
12950
12951 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12952 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
12953 0 is the conversion test fails. This implements parts the method
12954 invocation convertion (5.3). */
12955
12956 static int
12957 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12958 tree lhs_type, rhs_type;
12959 {
12960 /* 5.1.1: This is the identity conversion part. */
12961 if (lhs_type == rhs_type)
12962 return 1;
12963
12964 /* Reject non primitive types and boolean conversions. */
12965 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
12966 return 0;
12967
12968 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12969 than a char can't be converted into a char. Short can't too, but
12970 the < test below takes care of that */
12971 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12972 return 0;
12973
12974 /* Accept all promoted type here. Note, we can't use <= in the test
12975 below, because we still need to bounce out assignments of short
12976 to char and the likes */
12977 if (lhs_type == int_type_node
12978 && (rhs_type == promoted_byte_type_node
12979 || rhs_type == promoted_short_type_node
12980 || rhs_type == promoted_char_type_node
12981 || rhs_type == promoted_boolean_type_node))
12982 return 1;
12983
12984 /* From here, an integral is widened if its precision is smaller
12985 than the precision of the LHS or if the LHS is a floating point
12986 type, or the RHS is a float and the RHS a double. */
12987 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12988 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12989 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12990 || (rhs_type == float_type_node && lhs_type == double_type_node))
12991 return 1;
12992
12993 return 0;
12994 }
12995
12996 /* Check that something of SOURCE type can be assigned or cast to
12997 something of DEST type at runtime. Return 1 if the operation is
12998 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12999 were SOURCE is cast into DEST, which borrows a lot of the
13000 assignment check. */
13001
13002 static int
13003 valid_ref_assignconv_cast_p (source, dest, cast)
13004 tree source;
13005 tree dest;
13006 int cast;
13007 {
13008 /* SOURCE or DEST might be null if not from a declared entity. */
13009 if (!source || !dest)
13010 return 0;
13011 if (JNULLP_TYPE_P (source))
13012 return 1;
13013 if (TREE_CODE (source) == POINTER_TYPE)
13014 source = TREE_TYPE (source);
13015 if (TREE_CODE (dest) == POINTER_TYPE)
13016 dest = TREE_TYPE (dest);
13017
13018 /* If source and dest are being compiled from bytecode, they may need to
13019 be loaded. */
13020 if (CLASS_P (source) && !CLASS_LOADED_P (source))
13021 {
13022 load_class (source, 1);
13023 safe_layout_class (source);
13024 }
13025 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
13026 {
13027 load_class (dest, 1);
13028 safe_layout_class (dest);
13029 }
13030
13031 /* Case where SOURCE is a class type */
13032 if (TYPE_CLASS_P (source))
13033 {
13034 if (TYPE_CLASS_P (dest))
13035 return (source == dest
13036 || inherits_from_p (source, dest)
13037 || (cast && inherits_from_p (dest, source)));
13038 if (TYPE_INTERFACE_P (dest))
13039 {
13040 /* If doing a cast and SOURCE is final, the operation is
13041 always correct a compile time (because even if SOURCE
13042 does not implement DEST, a subclass of SOURCE might). */
13043 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
13044 return 1;
13045 /* Otherwise, SOURCE must implement DEST */
13046 return interface_of_p (dest, source);
13047 }
13048 /* DEST is an array, cast permited if SOURCE is of Object type */
13049 return (cast && source == object_type_node ? 1 : 0);
13050 }
13051 if (TYPE_INTERFACE_P (source))
13052 {
13053 if (TYPE_CLASS_P (dest))
13054 {
13055 /* If not casting, DEST must be the Object type */
13056 if (!cast)
13057 return dest == object_type_node;
13058 /* We're doing a cast. The cast is always valid is class
13059 DEST is not final, otherwise, DEST must implement SOURCE */
13060 else if (!CLASS_FINAL (TYPE_NAME (dest)))
13061 return 1;
13062 else
13063 return interface_of_p (source, dest);
13064 }
13065 if (TYPE_INTERFACE_P (dest))
13066 {
13067 /* If doing a cast, then if SOURCE and DEST contain method
13068 with the same signature but different return type, then
13069 this is a (compile time) error */
13070 if (cast)
13071 {
13072 tree method_source, method_dest;
13073 tree source_type;
13074 tree source_sig;
13075 tree source_name;
13076 for (method_source = TYPE_METHODS (source); method_source;
13077 method_source = TREE_CHAIN (method_source))
13078 {
13079 source_sig =
13080 build_java_argument_signature (TREE_TYPE (method_source));
13081 source_type = TREE_TYPE (TREE_TYPE (method_source));
13082 source_name = DECL_NAME (method_source);
13083 for (method_dest = TYPE_METHODS (dest);
13084 method_dest; method_dest = TREE_CHAIN (method_dest))
13085 if (source_sig ==
13086 build_java_argument_signature (TREE_TYPE (method_dest))
13087 && source_name == DECL_NAME (method_dest)
13088 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
13089 return 0;
13090 }
13091 return 1;
13092 }
13093 else
13094 return source == dest || interface_of_p (dest, source);
13095 }
13096 else
13097 {
13098 /* Array */
13099 return (cast
13100 && (DECL_NAME (TYPE_NAME (source))
13101 == java_lang_cloneable_identifier_node
13102 || (DECL_NAME (TYPE_NAME (source))
13103 == java_io_serializable_identifier_node)));
13104 }
13105 }
13106 if (TYPE_ARRAY_P (source))
13107 {
13108 if (TYPE_CLASS_P (dest))
13109 return dest == object_type_node;
13110 /* Can't cast an array to an interface unless the interface is
13111 java.lang.Cloneable or java.io.Serializable. */
13112 if (TYPE_INTERFACE_P (dest))
13113 return (DECL_NAME (TYPE_NAME (dest))
13114 == java_lang_cloneable_identifier_node
13115 || (DECL_NAME (TYPE_NAME (dest))
13116 == java_io_serializable_identifier_node));
13117 else /* Arrays */
13118 {
13119 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13120 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13121
13122 /* In case of severe errors, they turn out null */
13123 if (!dest_element_type || !source_element_type)
13124 return 0;
13125 if (source_element_type == dest_element_type)
13126 return 1;
13127 return valid_ref_assignconv_cast_p (source_element_type,
13128 dest_element_type, cast);
13129 }
13130 return 0;
13131 }
13132 return 0;
13133 }
13134
13135 static int
13136 valid_cast_to_p (source, dest)
13137 tree source;
13138 tree dest;
13139 {
13140 if (TREE_CODE (source) == POINTER_TYPE)
13141 source = TREE_TYPE (source);
13142 if (TREE_CODE (dest) == POINTER_TYPE)
13143 dest = TREE_TYPE (dest);
13144
13145 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13146 return valid_ref_assignconv_cast_p (source, dest, 1);
13147
13148 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13149 return 1;
13150
13151 else if (TREE_CODE (source) == BOOLEAN_TYPE
13152 && TREE_CODE (dest) == BOOLEAN_TYPE)
13153 return 1;
13154
13155 return 0;
13156 }
13157
13158 static tree
13159 do_unary_numeric_promotion (arg)
13160 tree arg;
13161 {
13162 tree type = TREE_TYPE (arg);
13163 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13164 || TREE_CODE (type) == CHAR_TYPE)
13165 arg = convert (int_type_node, arg);
13166 return arg;
13167 }
13168
13169 /* Return a nonzero value if SOURCE can be converted into DEST using
13170 the method invocation conversion rule (5.3). */
13171 static int
13172 valid_method_invocation_conversion_p (dest, source)
13173 tree dest, source;
13174 {
13175 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13176 && valid_builtin_assignconv_identity_widening_p (dest, source))
13177 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13178 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13179 && valid_ref_assignconv_cast_p (source, dest, 0)));
13180 }
13181
13182 /* Build an incomplete binop expression. */
13183
13184 static tree
13185 build_binop (op, op_location, op1, op2)
13186 enum tree_code op;
13187 int op_location;
13188 tree op1, op2;
13189 {
13190 tree binop = build (op, NULL_TREE, op1, op2);
13191 TREE_SIDE_EFFECTS (binop) = 1;
13192 /* Store the location of the operator, for better error report. The
13193 string of the operator will be rebuild based on the OP value. */
13194 EXPR_WFL_LINECOL (binop) = op_location;
13195 return binop;
13196 }
13197
13198 /* Build the string of the operator retained by NODE. If NODE is part
13199 of a compound expression, add an '=' at the end of the string. This
13200 function is called when an error needs to be reported on an
13201 operator. The string is returned as a pointer to a static character
13202 buffer. */
13203
13204 static char *
13205 operator_string (node)
13206 tree node;
13207 {
13208 #define BUILD_OPERATOR_STRING(S) \
13209 { \
13210 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13211 return buffer; \
13212 }
13213
13214 static char buffer [10];
13215 switch (TREE_CODE (node))
13216 {
13217 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13218 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13219 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13220 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13221 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13222 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13223 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13224 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13225 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13226 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13227 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13228 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13229 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13230 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13231 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13232 case GT_EXPR: BUILD_OPERATOR_STRING (">");
13233 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13234 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13235 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13236 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13237 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13238 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13239 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13240 case PREINCREMENT_EXPR: /* Fall through */
13241 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13242 case PREDECREMENT_EXPR: /* Fall through */
13243 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13244 default:
13245 internal_error ("unregistered operator %s",
13246 tree_code_name [TREE_CODE (node)]);
13247 }
13248 return NULL;
13249 #undef BUILD_OPERATOR_STRING
13250 }
13251
13252 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
13253
13254 static int
13255 java_decl_equiv (var_acc1, var_acc2)
13256 tree var_acc1, var_acc2;
13257 {
13258 if (JDECL_P (var_acc1))
13259 return (var_acc1 == var_acc2);
13260
13261 return (TREE_CODE (var_acc1) == COMPONENT_REF
13262 && TREE_CODE (var_acc2) == COMPONENT_REF
13263 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13264 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13265 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13266 }
13267
13268 /* Return a nonzero value if CODE is one of the operators that can be
13269 used in conjunction with the `=' operator in a compound assignment. */
13270
13271 static int
13272 binop_compound_p (code)
13273 enum tree_code code;
13274 {
13275 int i;
13276 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13277 if (binop_lookup [i] == code)
13278 break;
13279
13280 return i < BINOP_COMPOUND_CANDIDATES;
13281 }
13282
13283 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13284
13285 static tree
13286 java_refold (t)
13287 tree t;
13288 {
13289 tree c, b, ns, decl;
13290
13291 if (TREE_CODE (t) != MODIFY_EXPR)
13292 return t;
13293
13294 c = TREE_OPERAND (t, 1);
13295 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13296 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13297 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13298 return t;
13299
13300 /* Now the left branch of the binary operator. */
13301 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13302 if (! (b && TREE_CODE (b) == NOP_EXPR
13303 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13304 return t;
13305
13306 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13307 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13308 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13309 return t;
13310
13311 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13312 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13313 /* It's got to be the an equivalent decl */
13314 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13315 {
13316 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13317 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13318 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13319 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13320 /* Change the right part of the BINOP_EXPR */
13321 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13322 }
13323
13324 return t;
13325 }
13326
13327 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13328 errors but we modify NODE so that it contains the type computed
13329 according to the expression, when it's fixed. Otherwise, we write
13330 error_mark_node as the type. It allows us to further the analysis
13331 of remaining nodes and detects more errors in certain cases. */
13332
13333 static tree
13334 patch_binop (node, wfl_op1, wfl_op2)
13335 tree node;
13336 tree wfl_op1;
13337 tree wfl_op2;
13338 {
13339 tree op1 = TREE_OPERAND (node, 0);
13340 tree op2 = TREE_OPERAND (node, 1);
13341 tree op1_type = TREE_TYPE (op1);
13342 tree op2_type = TREE_TYPE (op2);
13343 tree prom_type = NULL_TREE, cn;
13344 enum tree_code code = TREE_CODE (node);
13345
13346 /* If 1, tell the routine that we have to return error_mark_node
13347 after checking for the initialization of the RHS */
13348 int error_found = 0;
13349
13350 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13351
13352 /* If either op<n>_type are NULL, this might be early signs of an
13353 error situation, unless it's too early to tell (in case we're
13354 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13355 correctly so the error can be later on reported accurately. */
13356 if (! (code == PLUS_EXPR || code == NE_EXPR
13357 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13358 {
13359 tree n;
13360 if (! op1_type)
13361 {
13362 n = java_complete_tree (op1);
13363 op1_type = TREE_TYPE (n);
13364 }
13365 if (! op2_type)
13366 {
13367 n = java_complete_tree (op2);
13368 op2_type = TREE_TYPE (n);
13369 }
13370 }
13371
13372 switch (code)
13373 {
13374 /* 15.16 Multiplicative operators */
13375 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13376 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13377 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13378 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13379 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13380 {
13381 if (!JNUMERIC_TYPE_P (op1_type))
13382 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13383 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13384 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13385 TREE_TYPE (node) = error_mark_node;
13386 error_found = 1;
13387 break;
13388 }
13389 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13390
13391 /* Detect integral division by zero */
13392 if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13393 && TREE_CODE (prom_type) == INTEGER_TYPE
13394 && (op2 == integer_zero_node || op2 == long_zero_node ||
13395 (TREE_CODE (op2) == INTEGER_CST &&
13396 ! TREE_INT_CST_LOW (op2) && ! TREE_INT_CST_HIGH (op2))))
13397 {
13398 parse_warning_context (wfl_operator, "Evaluating this expression will result in an arithmetic exception being thrown");
13399 TREE_CONSTANT (node) = 0;
13400 }
13401
13402 /* Change the division operator if necessary */
13403 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13404 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13405
13406 /* Before divisions as is disapear, try to simplify and bail if
13407 applicable, otherwise we won't perform even simple
13408 simplifications like (1-1)/3. We can't do that with floating
13409 point number, folds can't handle them at this stage. */
13410 if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13411 && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13412 {
13413 TREE_TYPE (node) = prom_type;
13414 node = fold (node);
13415 if (TREE_CODE (node) != code)
13416 return node;
13417 }
13418
13419 if (TREE_CODE (prom_type) == INTEGER_TYPE
13420 && flag_use_divide_subroutine
13421 && ! flag_emit_class_files
13422 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13423 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13424
13425 /* This one is more complicated. FLOATs are processed by a
13426 function call to soft_fmod. Duplicate the value of the
13427 COMPOUND_ASSIGN_P flag. */
13428 if (code == TRUNC_MOD_EXPR)
13429 {
13430 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13431 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13432 TREE_SIDE_EFFECTS (mod)
13433 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13434 return mod;
13435 }
13436 break;
13437
13438 /* 15.17 Additive Operators */
13439 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13440
13441 /* Operation is valid if either one argument is a string
13442 constant, a String object or a StringBuffer crafted for the
13443 purpose of the a previous usage of the String concatenation
13444 operator */
13445
13446 if (TREE_CODE (op1) == STRING_CST
13447 || TREE_CODE (op2) == STRING_CST
13448 || JSTRING_TYPE_P (op1_type)
13449 || JSTRING_TYPE_P (op2_type)
13450 || IS_CRAFTED_STRING_BUFFER_P (op1)
13451 || IS_CRAFTED_STRING_BUFFER_P (op2))
13452 return build_string_concatenation (op1, op2);
13453
13454 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13455 Numeric Types */
13456 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13457 {
13458 if (!JNUMERIC_TYPE_P (op1_type))
13459 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13460 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13461 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13462 TREE_TYPE (node) = error_mark_node;
13463 error_found = 1;
13464 break;
13465 }
13466 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13467 break;
13468
13469 /* 15.18 Shift Operators */
13470 case LSHIFT_EXPR:
13471 case RSHIFT_EXPR:
13472 case URSHIFT_EXPR:
13473 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13474 {
13475 if (!JINTEGRAL_TYPE_P (op1_type))
13476 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13477 else
13478 {
13479 if (JNUMERIC_TYPE_P (op2_type))
13480 parse_error_context (wfl_operator,
13481 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
13482 operator_string (node),
13483 lang_printable_name (op2_type, 0));
13484 else
13485 parse_error_context (wfl_operator,
13486 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
13487 operator_string (node),
13488 lang_printable_name (op2_type, 0));
13489 }
13490 TREE_TYPE (node) = error_mark_node;
13491 error_found = 1;
13492 break;
13493 }
13494
13495 /* Unary numeric promotion (5.6.1) is performed on each operand
13496 separately */
13497 op1 = do_unary_numeric_promotion (op1);
13498 op2 = do_unary_numeric_promotion (op2);
13499
13500 /* If the right hand side is of type `long', first cast it to
13501 `int'. */
13502 if (TREE_TYPE (op2) == long_type_node)
13503 op2 = build1 (CONVERT_EXPR, int_type_node, op2);
13504
13505 /* The type of the shift expression is the type of the promoted
13506 type of the left-hand operand */
13507 prom_type = TREE_TYPE (op1);
13508
13509 /* Shift int only up to 0x1f and long up to 0x3f */
13510 if (prom_type == int_type_node)
13511 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13512 build_int_2 (0x1f, 0)));
13513 else
13514 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13515 build_int_2 (0x3f, 0)));
13516
13517 /* The >>> operator is a >> operating on unsigned quantities */
13518 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13519 {
13520 tree to_return;
13521 tree utype = java_unsigned_type (prom_type);
13522 op1 = convert (utype, op1);
13523 TREE_SET_CODE (node, RSHIFT_EXPR);
13524 TREE_OPERAND (node, 0) = op1;
13525 TREE_OPERAND (node, 1) = op2;
13526 TREE_TYPE (node) = utype;
13527 to_return = convert (prom_type, node);
13528 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13529 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13530 TREE_SIDE_EFFECTS (to_return)
13531 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13532 return to_return;
13533 }
13534 break;
13535
13536 /* 15.19.1 Type Comparison Operator instaceof */
13537 case INSTANCEOF_EXPR:
13538
13539 TREE_TYPE (node) = boolean_type_node;
13540
13541 /* OP1_TYPE might be NULL when OP1 is a string constant. */
13542 if ((cn = patch_string (op1)))
13543 {
13544 op1 = cn;
13545 op1_type = TREE_TYPE (op1);
13546 }
13547 if (op1_type == NULL_TREE)
13548 abort ();
13549
13550 if (!(op2_type = resolve_type_during_patch (op2)))
13551 return error_mark_node;
13552
13553 /* The first operand must be a reference type or the null type */
13554 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13555 error_found = 1; /* Error reported further below */
13556
13557 /* The second operand must be a reference type */
13558 if (!JREFERENCE_TYPE_P (op2_type))
13559 {
13560 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13561 parse_error_context
13562 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13563 lang_printable_name (op2_type, 0));
13564 error_found = 1;
13565 }
13566
13567 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13568 {
13569 /* If the first operand is null, the result is always false */
13570 if (op1 == null_pointer_node)
13571 return boolean_false_node;
13572 else if (flag_emit_class_files)
13573 {
13574 TREE_OPERAND (node, 1) = op2_type;
13575 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13576 return node;
13577 }
13578 /* Otherwise we have to invoke instance of to figure it out */
13579 else
13580 return build_instanceof (op1, op2_type);
13581 }
13582 /* There is no way the expression operand can be an instance of
13583 the type operand. This is a compile time error. */
13584 else
13585 {
13586 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13587 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13588 parse_error_context
13589 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13590 t1, lang_printable_name (op2_type, 0));
13591 free (t1);
13592 error_found = 1;
13593 }
13594
13595 break;
13596
13597 /* 15.21 Bitwise and Logical Operators */
13598 case BIT_AND_EXPR:
13599 case BIT_XOR_EXPR:
13600 case BIT_IOR_EXPR:
13601 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13602 /* Binary numeric promotion is performed on both operand and the
13603 expression retain that type */
13604 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13605
13606 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13607 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13608 /* The type of the bitwise operator expression is BOOLEAN */
13609 prom_type = boolean_type_node;
13610 else
13611 {
13612 if (!JINTEGRAL_TYPE_P (op1_type))
13613 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13614 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13615 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13616 TREE_TYPE (node) = error_mark_node;
13617 error_found = 1;
13618 /* Insert a break here if adding thing before the switch's
13619 break for this case */
13620 }
13621 break;
13622
13623 /* 15.22 Conditional-And Operator */
13624 case TRUTH_ANDIF_EXPR:
13625 /* 15.23 Conditional-Or Operator */
13626 case TRUTH_ORIF_EXPR:
13627 /* Operands must be of BOOLEAN type */
13628 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13629 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13630 {
13631 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13632 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13633 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13634 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13635 TREE_TYPE (node) = boolean_type_node;
13636 error_found = 1;
13637 break;
13638 }
13639 else if (integer_zerop (op1))
13640 {
13641 return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13642 }
13643 else if (integer_onep (op1))
13644 {
13645 return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13646 }
13647 /* The type of the conditional operators is BOOLEAN */
13648 prom_type = boolean_type_node;
13649 break;
13650
13651 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13652 case LT_EXPR:
13653 case GT_EXPR:
13654 case LE_EXPR:
13655 case GE_EXPR:
13656 /* The type of each of the operands must be a primitive numeric
13657 type */
13658 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13659 {
13660 if (!JNUMERIC_TYPE_P (op1_type))
13661 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13662 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13663 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13664 TREE_TYPE (node) = boolean_type_node;
13665 error_found = 1;
13666 break;
13667 }
13668 /* Binary numeric promotion is performed on the operands */
13669 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13670 /* The type of the relation expression is always BOOLEAN */
13671 prom_type = boolean_type_node;
13672 break;
13673
13674 /* 15.20 Equality Operator */
13675 case EQ_EXPR:
13676 case NE_EXPR:
13677 /* It's time for us to patch the strings. */
13678 if ((cn = patch_string (op1)))
13679 {
13680 op1 = cn;
13681 op1_type = TREE_TYPE (op1);
13682 }
13683 if ((cn = patch_string (op2)))
13684 {
13685 op2 = cn;
13686 op2_type = TREE_TYPE (op2);
13687 }
13688
13689 /* 15.20.1 Numerical Equality Operators == and != */
13690 /* Binary numeric promotion is performed on the operands */
13691 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13692 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13693
13694 /* 15.20.2 Boolean Equality Operators == and != */
13695 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13696 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13697 ; /* Nothing to do here */
13698
13699 /* 15.20.3 Reference Equality Operators == and != */
13700 /* Types have to be either references or the null type. If
13701 they're references, it must be possible to convert either
13702 type to the other by casting conversion. */
13703 else if (op1 == null_pointer_node || op2 == null_pointer_node
13704 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13705 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13706 || valid_ref_assignconv_cast_p (op2_type,
13707 op1_type, 1))))
13708 ; /* Nothing to do here */
13709
13710 /* Else we have an error figure what can't be converted into
13711 what and report the error */
13712 else
13713 {
13714 char *t1;
13715 t1 = xstrdup (lang_printable_name (op1_type, 0));
13716 parse_error_context
13717 (wfl_operator,
13718 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13719 operator_string (node), t1,
13720 lang_printable_name (op2_type, 0));
13721 free (t1);
13722 TREE_TYPE (node) = boolean_type_node;
13723 error_found = 1;
13724 break;
13725 }
13726 prom_type = boolean_type_node;
13727 break;
13728 default:
13729 abort ();
13730 }
13731
13732 if (error_found)
13733 return error_mark_node;
13734
13735 TREE_OPERAND (node, 0) = op1;
13736 TREE_OPERAND (node, 1) = op2;
13737 TREE_TYPE (node) = prom_type;
13738 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13739
13740 if (flag_emit_xref)
13741 return node;
13742
13743 /* fold does not respect side-effect order as required for Java but not C.
13744 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13745 * bytecode.
13746 */
13747 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13748 : ! TREE_SIDE_EFFECTS (node))
13749 node = fold (node);
13750 return node;
13751 }
13752
13753 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13754 zero value, the value of CSTE comes after the valude of STRING */
13755
13756 static tree
13757 do_merge_string_cste (cste, string, string_len, after)
13758 tree cste;
13759 const char *string;
13760 int string_len, after;
13761 {
13762 const char *old = TREE_STRING_POINTER (cste);
13763 int old_len = TREE_STRING_LENGTH (cste);
13764 int len = old_len + string_len;
13765 char *new = alloca (len+1);
13766
13767 if (after)
13768 {
13769 memcpy (new, string, string_len);
13770 memcpy (&new [string_len], old, old_len);
13771 }
13772 else
13773 {
13774 memcpy (new, old, old_len);
13775 memcpy (&new [old_len], string, string_len);
13776 }
13777 new [len] = '\0';
13778 return build_string (len, new);
13779 }
13780
13781 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13782 new STRING_CST on success, NULL_TREE on failure */
13783
13784 static tree
13785 merge_string_cste (op1, op2, after)
13786 tree op1, op2;
13787 int after;
13788 {
13789 /* Handle two string constants right away */
13790 if (TREE_CODE (op2) == STRING_CST)
13791 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13792 TREE_STRING_LENGTH (op2), after);
13793
13794 /* Reasonable integer constant can be treated right away */
13795 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13796 {
13797 static const char *const boolean_true = "true";
13798 static const char *const boolean_false = "false";
13799 static const char *const null_pointer = "null";
13800 char ch[3];
13801 const char *string;
13802
13803 if (op2 == boolean_true_node)
13804 string = boolean_true;
13805 else if (op2 == boolean_false_node)
13806 string = boolean_false;
13807 else if (op2 == null_pointer_node)
13808 string = null_pointer;
13809 else if (TREE_TYPE (op2) == char_type_node)
13810 {
13811 /* Convert the character into UTF-8. */
13812 unsigned char c = (unsigned char) TREE_INT_CST_LOW (op2);
13813 unsigned char *p = (unsigned char *) ch;
13814 if (0x01 <= c
13815 && c <= 0x7f)
13816 *p++ = c;
13817 else
13818 {
13819 *p++ = c >> 6 | 0xc0;
13820 *p++ = (c & 0x3f) | 0x80;
13821 }
13822 *p = '\0';
13823
13824 string = ch;
13825 }
13826 else
13827 string = string_convert_int_cst (op2);
13828
13829 return do_merge_string_cste (op1, string, strlen (string), after);
13830 }
13831 return NULL_TREE;
13832 }
13833
13834 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13835 has to be a STRING_CST and the other part must be a STRING_CST or a
13836 INTEGRAL constant. Return a new STRING_CST if the operation
13837 succeed, NULL_TREE otherwise.
13838
13839 If the case we want to optimize for space, we might want to return
13840 NULL_TREE for each invocation of this routine. FIXME */
13841
13842 static tree
13843 string_constant_concatenation (op1, op2)
13844 tree op1, op2;
13845 {
13846 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13847 {
13848 tree string, rest;
13849 int invert;
13850
13851 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13852 rest = (string == op1 ? op2 : op1);
13853 invert = (string == op1 ? 0 : 1 );
13854
13855 /* Walk REST, only if it looks reasonable */
13856 if (TREE_CODE (rest) != STRING_CST
13857 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13858 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13859 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13860 {
13861 rest = java_complete_tree (rest);
13862 if (rest == error_mark_node)
13863 return error_mark_node;
13864 rest = fold (rest);
13865 }
13866 return merge_string_cste (string, rest, invert);
13867 }
13868 return NULL_TREE;
13869 }
13870
13871 /* Implement the `+' operator. Does static optimization if possible,
13872 otherwise create (if necessary) and append elements to a
13873 StringBuffer. The StringBuffer will be carried around until it is
13874 used for a function call or an assignment. Then toString() will be
13875 called on it to turn it into a String object. */
13876
13877 static tree
13878 build_string_concatenation (op1, op2)
13879 tree op1, op2;
13880 {
13881 tree result;
13882 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13883
13884 if (flag_emit_xref)
13885 return build (PLUS_EXPR, string_type_node, op1, op2);
13886
13887 /* Try to do some static optimization */
13888 if ((result = string_constant_concatenation (op1, op2)))
13889 return result;
13890
13891 /* Discard empty strings on either side of the expression */
13892 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13893 {
13894 op1 = op2;
13895 op2 = NULL_TREE;
13896 }
13897 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13898 op2 = NULL_TREE;
13899
13900 /* If operands are string constant, turn then into object references */
13901 if (TREE_CODE (op1) == STRING_CST)
13902 op1 = patch_string_cst (op1);
13903 if (op2 && TREE_CODE (op2) == STRING_CST)
13904 op2 = patch_string_cst (op2);
13905
13906 /* If either one of the constant is null and the other non null
13907 operand is a String constant, return it. */
13908 if ((TREE_CODE (op1) == STRING_CST) && !op2)
13909 return op1;
13910
13911 /* If OP1 isn't already a StringBuffer, create and
13912 initialize a new one */
13913 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13914 {
13915 /* Two solutions here:
13916 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13917 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13918 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13919 op1 = BUILD_STRING_BUFFER (op1);
13920 else
13921 {
13922 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13923 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13924 }
13925 }
13926
13927 if (op2)
13928 {
13929 /* OP1 is no longer the last node holding a crafted StringBuffer */
13930 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13931 /* Create a node for `{new...,xxx}.append (op2)' */
13932 if (op2)
13933 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13934 }
13935
13936 /* Mark the last node holding a crafted StringBuffer */
13937 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13938
13939 TREE_SIDE_EFFECTS (op1) = side_effects;
13940 return op1;
13941 }
13942
13943 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13944 StringBuffer. If no string were found to be patched, return
13945 NULL. */
13946
13947 static tree
13948 patch_string (node)
13949 tree node;
13950 {
13951 if (node == error_mark_node)
13952 return error_mark_node;
13953 if (TREE_CODE (node) == STRING_CST)
13954 return patch_string_cst (node);
13955 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13956 {
13957 int saved = ctxp->explicit_constructor_p;
13958 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13959 tree ret;
13960 /* Temporary disable forbid the use of `this'. */
13961 ctxp->explicit_constructor_p = 0;
13962 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13963 /* String concatenation arguments must be evaluated in order too. */
13964 ret = force_evaluation_order (ret);
13965 /* Restore it at its previous value */
13966 ctxp->explicit_constructor_p = saved;
13967 return ret;
13968 }
13969 return NULL_TREE;
13970 }
13971
13972 /* Build the internal representation of a string constant. */
13973
13974 static tree
13975 patch_string_cst (node)
13976 tree node;
13977 {
13978 int location;
13979 if (! flag_emit_class_files)
13980 {
13981 node = get_identifier (TREE_STRING_POINTER (node));
13982 location = alloc_name_constant (CONSTANT_String, node);
13983 node = build_ref_from_constant_pool (location);
13984 }
13985 TREE_TYPE (node) = string_ptr_type_node;
13986 TREE_CONSTANT (node) = 1;
13987 return node;
13988 }
13989
13990 /* Build an incomplete unary operator expression. */
13991
13992 static tree
13993 build_unaryop (op_token, op_location, op1)
13994 int op_token, op_location;
13995 tree op1;
13996 {
13997 enum tree_code op;
13998 tree unaryop;
13999 switch (op_token)
14000 {
14001 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
14002 case MINUS_TK: op = NEGATE_EXPR; break;
14003 case NEG_TK: op = TRUTH_NOT_EXPR; break;
14004 case NOT_TK: op = BIT_NOT_EXPR; break;
14005 default: abort ();
14006 }
14007
14008 unaryop = build1 (op, NULL_TREE, op1);
14009 TREE_SIDE_EFFECTS (unaryop) = 1;
14010 /* Store the location of the operator, for better error report. The
14011 string of the operator will be rebuild based on the OP value. */
14012 EXPR_WFL_LINECOL (unaryop) = op_location;
14013 return unaryop;
14014 }
14015
14016 /* Special case for the ++/-- operators, since they require an extra
14017 argument to build, which is set to NULL and patched
14018 later. IS_POST_P is 1 if the operator, 0 otherwise. */
14019
14020 static tree
14021 build_incdec (op_token, op_location, op1, is_post_p)
14022 int op_token, op_location;
14023 tree op1;
14024 int is_post_p;
14025 {
14026 static const enum tree_code lookup [2][2] =
14027 {
14028 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
14029 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
14030 };
14031 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
14032 NULL_TREE, op1, NULL_TREE);
14033 TREE_SIDE_EFFECTS (node) = 1;
14034 /* Store the location of the operator, for better error report. The
14035 string of the operator will be rebuild based on the OP value. */
14036 EXPR_WFL_LINECOL (node) = op_location;
14037 return node;
14038 }
14039
14040 /* Build an incomplete cast operator, based on the use of the
14041 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
14042 set. java_complete_tree is trained to walk a CONVERT_EXPR even
14043 though its type is already set. */
14044
14045 static tree
14046 build_cast (location, type, exp)
14047 int location;
14048 tree type, exp;
14049 {
14050 tree node = build1 (CONVERT_EXPR, type, exp);
14051 EXPR_WFL_LINECOL (node) = location;
14052 return node;
14053 }
14054
14055 /* Build an incomplete class reference operator. */
14056 static tree
14057 build_incomplete_class_ref (location, class_name)
14058 int location;
14059 tree class_name;
14060 {
14061 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
14062 EXPR_WFL_LINECOL (node) = location;
14063 return node;
14064 }
14065
14066 /* Complete an incomplete class reference operator. */
14067 static tree
14068 patch_incomplete_class_ref (node)
14069 tree node;
14070 {
14071 tree type = TREE_OPERAND (node, 0);
14072 tree ref_type;
14073
14074 if (!(ref_type = resolve_type_during_patch (type)))
14075 return error_mark_node;
14076
14077 /* Generate the synthetic static method `class$'. (Previously we
14078 deferred this, causing different method tables to be emitted
14079 for native code and bytecode.) */
14080 if (!TYPE_DOT_CLASS (current_class))
14081 build_dot_class_method (current_class);
14082
14083 /* If we're not emitting class files and we know ref_type is a
14084 compiled class, build a direct reference. */
14085 if ((! flag_emit_class_files && is_compiled_class (ref_type))
14086 || JPRIMITIVE_TYPE_P (ref_type)
14087 || TREE_CODE (ref_type) == VOID_TYPE)
14088 {
14089 tree dot = build_class_ref (ref_type);
14090 /* A class referenced by `foo.class' is initialized. */
14091 if (!flag_emit_class_files)
14092 dot = build_class_init (ref_type, dot);
14093 return java_complete_tree (dot);
14094 }
14095
14096 /* If we're emitting class files and we have to deal with non
14097 primitive types, we invoke the synthetic static method `class$'. */
14098 ref_type = build_dot_class_method_invocation (ref_type);
14099 return java_complete_tree (ref_type);
14100 }
14101
14102 /* 15.14 Unary operators. We return error_mark_node in case of error,
14103 but preserve the type of NODE if the type is fixed. */
14104
14105 static tree
14106 patch_unaryop (node, wfl_op)
14107 tree node;
14108 tree wfl_op;
14109 {
14110 tree op = TREE_OPERAND (node, 0);
14111 tree op_type = TREE_TYPE (op);
14112 tree prom_type = NULL_TREE, value, decl;
14113 int outer_field_flag = 0;
14114 int code = TREE_CODE (node);
14115 int error_found = 0;
14116
14117 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14118
14119 switch (code)
14120 {
14121 /* 15.13.2 Postfix Increment Operator ++ */
14122 case POSTINCREMENT_EXPR:
14123 /* 15.13.3 Postfix Increment Operator -- */
14124 case POSTDECREMENT_EXPR:
14125 /* 15.14.1 Prefix Increment Operator ++ */
14126 case PREINCREMENT_EXPR:
14127 /* 15.14.2 Prefix Decrement Operator -- */
14128 case PREDECREMENT_EXPR:
14129 op = decl = strip_out_static_field_access_decl (op);
14130 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
14131 /* We might be trying to change an outer field accessed using
14132 access method. */
14133 if (outer_field_flag)
14134 {
14135 /* Retrieve the decl of the field we're trying to access. We
14136 do that by first retrieving the function we would call to
14137 access the field. It has been already verified that this
14138 field isn't final */
14139 if (flag_emit_class_files)
14140 decl = TREE_OPERAND (op, 0);
14141 else
14142 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
14143 decl = DECL_FUNCTION_ACCESS_DECL (decl);
14144 }
14145 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14146 else if (!JDECL_P (decl)
14147 && TREE_CODE (decl) != COMPONENT_REF
14148 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14149 && TREE_CODE (decl) != INDIRECT_REF
14150 && !(TREE_CODE (decl) == COMPOUND_EXPR
14151 && TREE_OPERAND (decl, 1)
14152 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14153 {
14154 TREE_TYPE (node) = error_mark_node;
14155 error_found = 1;
14156 }
14157
14158 /* From now on, we know that op if a variable and that it has a
14159 valid wfl. We use wfl_op to locate errors related to the
14160 ++/-- operand. */
14161 if (!JNUMERIC_TYPE_P (op_type))
14162 {
14163 parse_error_context
14164 (wfl_op, "Invalid argument type `%s' to `%s'",
14165 lang_printable_name (op_type, 0), operator_string (node));
14166 TREE_TYPE (node) = error_mark_node;
14167 error_found = 1;
14168 }
14169 else
14170 {
14171 /* Before the addition, binary numeric promotion is performed on
14172 both operands, if really necessary */
14173 if (JINTEGRAL_TYPE_P (op_type))
14174 {
14175 value = build_int_2 (1, 0);
14176 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
14177 }
14178 else
14179 {
14180 value = build_int_2 (1, 0);
14181 TREE_TYPE (node) =
14182 binary_numeric_promotion (op_type,
14183 TREE_TYPE (value), &op, &value);
14184 }
14185
14186 /* We remember we might be accessing an outer field */
14187 if (outer_field_flag)
14188 {
14189 /* We re-generate an access to the field */
14190 value = build (PLUS_EXPR, TREE_TYPE (op),
14191 build_outer_field_access (wfl_op, decl), value);
14192
14193 /* And we patch the original access$() into a write
14194 with plus_op as a rhs */
14195 return outer_field_access_fix (node, op, value);
14196 }
14197
14198 /* And write back into the node. */
14199 TREE_OPERAND (node, 0) = op;
14200 TREE_OPERAND (node, 1) = value;
14201 /* Convert the overall back into its original type, if
14202 necessary, and return */
14203 if (JINTEGRAL_TYPE_P (op_type))
14204 return fold (node);
14205 else
14206 return fold (convert (op_type, node));
14207 }
14208 break;
14209
14210 /* 15.14.3 Unary Plus Operator + */
14211 case UNARY_PLUS_EXPR:
14212 /* 15.14.4 Unary Minus Operator - */
14213 case NEGATE_EXPR:
14214 if (!JNUMERIC_TYPE_P (op_type))
14215 {
14216 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14217 TREE_TYPE (node) = error_mark_node;
14218 error_found = 1;
14219 }
14220 /* Unary numeric promotion is performed on operand */
14221 else
14222 {
14223 op = do_unary_numeric_promotion (op);
14224 prom_type = TREE_TYPE (op);
14225 if (code == UNARY_PLUS_EXPR)
14226 return fold (op);
14227 }
14228 break;
14229
14230 /* 15.14.5 Bitwise Complement Operator ~ */
14231 case BIT_NOT_EXPR:
14232 if (!JINTEGRAL_TYPE_P (op_type))
14233 {
14234 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14235 TREE_TYPE (node) = error_mark_node;
14236 error_found = 1;
14237 }
14238 else
14239 {
14240 op = do_unary_numeric_promotion (op);
14241 prom_type = TREE_TYPE (op);
14242 }
14243 break;
14244
14245 /* 15.14.6 Logical Complement Operator ! */
14246 case TRUTH_NOT_EXPR:
14247 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14248 {
14249 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14250 /* But the type is known. We will report an error if further
14251 attempt of a assignment is made with this rhs */
14252 TREE_TYPE (node) = boolean_type_node;
14253 error_found = 1;
14254 }
14255 else
14256 prom_type = boolean_type_node;
14257 break;
14258
14259 /* 15.15 Cast Expression */
14260 case CONVERT_EXPR:
14261 value = patch_cast (node, wfl_operator);
14262 if (value == error_mark_node)
14263 {
14264 /* If this cast is part of an assignment, we tell the code
14265 that deals with it not to complain about a mismatch,
14266 because things have been cast, anyways */
14267 TREE_TYPE (node) = error_mark_node;
14268 error_found = 1;
14269 }
14270 else
14271 {
14272 value = fold (value);
14273 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
14274 return value;
14275 }
14276 break;
14277 }
14278
14279 if (error_found)
14280 return error_mark_node;
14281
14282 /* There are cases where node has been replaced by something else
14283 and we don't end up returning here: UNARY_PLUS_EXPR,
14284 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14285 TREE_OPERAND (node, 0) = fold (op);
14286 TREE_TYPE (node) = prom_type;
14287 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14288 return fold (node);
14289 }
14290
14291 /* Generic type resolution that sometimes takes place during node
14292 patching. Returned the resolved type or generate an error
14293 message. Return the resolved type or NULL_TREE. */
14294
14295 static tree
14296 resolve_type_during_patch (type)
14297 tree type;
14298 {
14299 if (unresolved_type_p (type, NULL))
14300 {
14301 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14302 if (!type_decl)
14303 {
14304 parse_error_context (type,
14305 "Class `%s' not found in type declaration",
14306 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14307 return NULL_TREE;
14308 }
14309 return TREE_TYPE (type_decl);
14310 }
14311 return type;
14312 }
14313 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14314 found. Otherwise NODE or something meant to replace it is returned. */
14315
14316 static tree
14317 patch_cast (node, wfl_op)
14318 tree node;
14319 tree wfl_op;
14320 {
14321 tree op = TREE_OPERAND (node, 0);
14322 tree cast_type = TREE_TYPE (node);
14323 tree patched, op_type;
14324 char *t1;
14325
14326 /* Some string patching might be necessary at this stage */
14327 if ((patched = patch_string (op)))
14328 TREE_OPERAND (node, 0) = op = patched;
14329 op_type = TREE_TYPE (op);
14330
14331 /* First resolve OP_TYPE if unresolved */
14332 if (!(cast_type = resolve_type_during_patch (cast_type)))
14333 return error_mark_node;
14334
14335 /* Check on cast that are proven correct at compile time */
14336 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14337 {
14338 /* Same type */
14339 if (cast_type == op_type)
14340 return node;
14341
14342 /* float and double type are converted to the original type main
14343 variant and then to the target type. */
14344 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
14345 op = convert (integer_type_node, op);
14346
14347 /* Try widening/narowwing convertion. Potentially, things need
14348 to be worked out in gcc so we implement the extreme cases
14349 correctly. fold_convert() needs to be fixed. */
14350 return convert (cast_type, op);
14351 }
14352
14353 /* It's also valid to cast a boolean into a boolean */
14354 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14355 return node;
14356
14357 /* null can be casted to references */
14358 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14359 return build_null_of_type (cast_type);
14360
14361 /* The remaining legal casts involve conversion between reference
14362 types. Check for their compile time correctness. */
14363 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14364 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14365 {
14366 TREE_TYPE (node) = promote_type (cast_type);
14367 /* Now, the case can be determined correct at compile time if
14368 OP_TYPE can be converted into CAST_TYPE by assignment
14369 conversion (5.2) */
14370
14371 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14372 {
14373 TREE_SET_CODE (node, NOP_EXPR);
14374 return node;
14375 }
14376
14377 if (flag_emit_class_files)
14378 {
14379 TREE_SET_CODE (node, CONVERT_EXPR);
14380 return node;
14381 }
14382
14383 /* The cast requires a run-time check */
14384 return build (CALL_EXPR, promote_type (cast_type),
14385 build_address_of (soft_checkcast_node),
14386 tree_cons (NULL_TREE, build_class_ref (cast_type),
14387 build_tree_list (NULL_TREE, op)),
14388 NULL_TREE);
14389 }
14390
14391 /* Any other casts are proven incorrect at compile time */
14392 t1 = xstrdup (lang_printable_name (op_type, 0));
14393 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
14394 t1, lang_printable_name (cast_type, 0));
14395 free (t1);
14396 return error_mark_node;
14397 }
14398
14399 /* Build a null constant and give it the type TYPE. */
14400
14401 static tree
14402 build_null_of_type (type)
14403 tree type;
14404 {
14405 tree node = build_int_2 (0, 0);
14406 TREE_TYPE (node) = promote_type (type);
14407 return node;
14408 }
14409
14410 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14411 a list of indices. */
14412 static tree
14413 build_array_ref (location, array, index)
14414 int location;
14415 tree array, index;
14416 {
14417 tree node = build (ARRAY_REF, NULL_TREE, array, index);
14418 EXPR_WFL_LINECOL (node) = location;
14419 return node;
14420 }
14421
14422 /* 15.12 Array Access Expression */
14423
14424 static tree
14425 patch_array_ref (node)
14426 tree node;
14427 {
14428 tree array = TREE_OPERAND (node, 0);
14429 tree array_type = TREE_TYPE (array);
14430 tree index = TREE_OPERAND (node, 1);
14431 tree index_type = TREE_TYPE (index);
14432 int error_found = 0;
14433
14434 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14435
14436 if (TREE_CODE (array_type) == POINTER_TYPE)
14437 array_type = TREE_TYPE (array_type);
14438
14439 /* The array reference must be an array */
14440 if (!TYPE_ARRAY_P (array_type))
14441 {
14442 parse_error_context
14443 (wfl_operator,
14444 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14445 lang_printable_name (array_type, 0));
14446 TREE_TYPE (node) = error_mark_node;
14447 error_found = 1;
14448 }
14449
14450 /* The array index undergoes unary numeric promotion. The promoted
14451 type must be int */
14452 index = do_unary_numeric_promotion (index);
14453 if (TREE_TYPE (index) != int_type_node)
14454 {
14455 if (valid_cast_to_p (index_type, int_type_node))
14456 parse_error_context (wfl_operator,
14457 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
14458 lang_printable_name (index_type, 0));
14459 else
14460 parse_error_context (wfl_operator,
14461 "Incompatible type for `[]'. Can't convert `%s' to `int'",
14462 lang_printable_name (index_type, 0));
14463 TREE_TYPE (node) = error_mark_node;
14464 error_found = 1;
14465 }
14466
14467 if (error_found)
14468 return error_mark_node;
14469
14470 array_type = TYPE_ARRAY_ELEMENT (array_type);
14471
14472 if (flag_emit_class_files || flag_emit_xref)
14473 {
14474 TREE_OPERAND (node, 0) = array;
14475 TREE_OPERAND (node, 1) = index;
14476 }
14477 else
14478 node = build_java_arrayaccess (array, array_type, index);
14479 TREE_TYPE (node) = array_type;
14480 return node;
14481 }
14482
14483 /* 15.9 Array Creation Expressions */
14484
14485 static tree
14486 build_newarray_node (type, dims, extra_dims)
14487 tree type;
14488 tree dims;
14489 int extra_dims;
14490 {
14491 tree node =
14492 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
14493 build_int_2 (extra_dims, 0));
14494 return node;
14495 }
14496
14497 static tree
14498 patch_newarray (node)
14499 tree node;
14500 {
14501 tree type = TREE_OPERAND (node, 0);
14502 tree dims = TREE_OPERAND (node, 1);
14503 tree cdim, array_type;
14504 int error_found = 0;
14505 int ndims = 0;
14506 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14507
14508 /* Dimension types are verified. It's better for the types to be
14509 verified in order. */
14510 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14511 {
14512 int dim_error = 0;
14513 tree dim = TREE_VALUE (cdim);
14514
14515 /* Dim might have been saved during its evaluation */
14516 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14517
14518 /* The type of each specified dimension must be an integral type. */
14519 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14520 dim_error = 1;
14521
14522 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14523 promoted type must be int. */
14524 else
14525 {
14526 dim = do_unary_numeric_promotion (dim);
14527 if (TREE_TYPE (dim) != int_type_node)
14528 dim_error = 1;
14529 }
14530
14531 /* Report errors on types here */
14532 if (dim_error)
14533 {
14534 parse_error_context
14535 (TREE_PURPOSE (cdim),
14536 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
14537 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14538 "Explicit cast needed to" : "Can't"),
14539 lang_printable_name (TREE_TYPE (dim), 0));
14540 error_found = 1;
14541 }
14542
14543 TREE_PURPOSE (cdim) = NULL_TREE;
14544 }
14545
14546 /* Resolve array base type if unresolved */
14547 if (!(type = resolve_type_during_patch (type)))
14548 error_found = 1;
14549
14550 if (error_found)
14551 {
14552 /* We don't want further evaluation of this bogus array creation
14553 operation */
14554 TREE_TYPE (node) = error_mark_node;
14555 return error_mark_node;
14556 }
14557
14558 /* Set array_type to the actual (promoted) array type of the result. */
14559 if (TREE_CODE (type) == RECORD_TYPE)
14560 type = build_pointer_type (type);
14561 while (--xdims >= 0)
14562 {
14563 type = promote_type (build_java_array_type (type, -1));
14564 }
14565 dims = nreverse (dims);
14566 array_type = type;
14567 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14568 {
14569 type = array_type;
14570 array_type
14571 = build_java_array_type (type,
14572 TREE_CODE (cdim) == INTEGER_CST
14573 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14574 : -1);
14575 array_type = promote_type (array_type);
14576 }
14577 dims = nreverse (dims);
14578
14579 /* The node is transformed into a function call. Things are done
14580 differently according to the number of dimensions. If the number
14581 of dimension is equal to 1, then the nature of the base type
14582 (primitive or not) matters. */
14583 if (ndims == 1)
14584 return build_new_array (type, TREE_VALUE (dims));
14585
14586 /* Can't reuse what's already written in expr.c because it uses the
14587 JVM stack representation. Provide a build_multianewarray. FIXME */
14588 return build (CALL_EXPR, array_type,
14589 build_address_of (soft_multianewarray_node),
14590 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
14591 tree_cons (NULL_TREE,
14592 build_int_2 (ndims, 0), dims )),
14593 NULL_TREE);
14594 }
14595
14596 /* 10.6 Array initializer. */
14597
14598 /* Build a wfl for array element that don't have one, so we can
14599 pin-point errors. */
14600
14601 static tree
14602 maybe_build_array_element_wfl (node)
14603 tree node;
14604 {
14605 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14606 return build_expr_wfl (NULL_TREE, ctxp->filename,
14607 ctxp->elc.line, ctxp->elc.prev_col);
14608 else
14609 return NULL_TREE;
14610 }
14611
14612 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14613 identification of initialized arrays easier to detect during walk
14614 and expansion. */
14615
14616 static tree
14617 build_new_array_init (location, values)
14618 int location;
14619 tree values;
14620 {
14621 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14622 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14623 EXPR_WFL_LINECOL (to_return) = location;
14624 return to_return;
14625 }
14626
14627 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14628 occurred. Otherwise return NODE after having set its type
14629 appropriately. */
14630
14631 static tree
14632 patch_new_array_init (type, node)
14633 tree type, node;
14634 {
14635 int error_seen = 0;
14636 tree current, element_type;
14637 HOST_WIDE_INT length;
14638 int all_constant = 1;
14639 tree init = TREE_OPERAND (node, 0);
14640
14641 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14642 {
14643 parse_error_context (node,
14644 "Invalid array initializer for non-array type `%s'",
14645 lang_printable_name (type, 1));
14646 return error_mark_node;
14647 }
14648 type = TREE_TYPE (type);
14649 element_type = TYPE_ARRAY_ELEMENT (type);
14650
14651 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14652
14653 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14654 current; length++, current = TREE_CHAIN (current))
14655 {
14656 tree elt = TREE_VALUE (current);
14657 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14658 {
14659 error_seen |= array_constructor_check_entry (element_type, current);
14660 elt = TREE_VALUE (current);
14661 /* When compiling to native code, STRING_CST is converted to
14662 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14663 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14664 all_constant = 0;
14665 }
14666 else
14667 {
14668 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14669 TREE_PURPOSE (current) = NULL_TREE;
14670 all_constant = 0;
14671 }
14672 if (elt && TREE_CODE (elt) == TREE_LIST
14673 && TREE_VALUE (elt) == error_mark_node)
14674 error_seen = 1;
14675 }
14676
14677 if (error_seen)
14678 return error_mark_node;
14679
14680 /* Create a new type. We can't reuse the one we have here by
14681 patching its dimension because it originally is of dimension -1
14682 hence reused by gcc. This would prevent triangular arrays. */
14683 type = build_java_array_type (element_type, length);
14684 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14685 TREE_TYPE (node) = promote_type (type);
14686 TREE_CONSTANT (init) = all_constant;
14687 TREE_CONSTANT (node) = all_constant;
14688 return node;
14689 }
14690
14691 /* Verify that one entry of the initializer element list can be
14692 assigned to the array base type. Report 1 if an error occurred, 0
14693 otherwise. */
14694
14695 static int
14696 array_constructor_check_entry (type, entry)
14697 tree type, entry;
14698 {
14699 char *array_type_string = NULL; /* For error reports */
14700 tree value, type_value, new_value, wfl_value, patched;
14701 int error_seen = 0;
14702
14703 new_value = NULL_TREE;
14704 wfl_value = TREE_VALUE (entry);
14705
14706 value = java_complete_tree (TREE_VALUE (entry));
14707 /* patch_string return error_mark_node if arg is error_mark_node */
14708 if ((patched = patch_string (value)))
14709 value = patched;
14710 if (value == error_mark_node)
14711 return 1;
14712
14713 type_value = TREE_TYPE (value);
14714
14715 /* At anytime, try_builtin_assignconv can report a warning on
14716 constant overflow during narrowing. */
14717 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14718 new_value = try_builtin_assignconv (wfl_operator, type, value);
14719 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14720 type_value = promote_type (type);
14721
14722 /* Check and report errors */
14723 if (!new_value)
14724 {
14725 const char *const msg = (!valid_cast_to_p (type_value, type) ?
14726 "Can't" : "Explicit cast needed to");
14727 if (!array_type_string)
14728 array_type_string = xstrdup (lang_printable_name (type, 1));
14729 parse_error_context
14730 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14731 msg, lang_printable_name (type_value, 1), array_type_string);
14732 error_seen = 1;
14733 }
14734
14735 if (new_value)
14736 TREE_VALUE (entry) = new_value;
14737
14738 if (array_type_string)
14739 free (array_type_string);
14740
14741 TREE_PURPOSE (entry) = NULL_TREE;
14742 return error_seen;
14743 }
14744
14745 static tree
14746 build_this (location)
14747 int location;
14748 {
14749 tree node = build_wfl_node (this_identifier_node);
14750 TREE_SET_CODE (node, THIS_EXPR);
14751 EXPR_WFL_LINECOL (node) = location;
14752 return node;
14753 }
14754
14755 /* 14.15 The return statement. It builds a modify expression that
14756 assigns the returned value to the RESULT_DECL that hold the value
14757 to be returned. */
14758
14759 static tree
14760 build_return (location, op)
14761 int location;
14762 tree op;
14763 {
14764 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14765 EXPR_WFL_LINECOL (node) = location;
14766 node = build_debugable_stmt (location, node);
14767 return node;
14768 }
14769
14770 static tree
14771 patch_return (node)
14772 tree node;
14773 {
14774 tree return_exp = TREE_OPERAND (node, 0);
14775 tree meth = current_function_decl;
14776 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14777 int error_found = 0;
14778
14779 TREE_TYPE (node) = error_mark_node;
14780 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14781
14782 /* It's invalid to have a return value within a function that is
14783 declared with the keyword void or that is a constructor */
14784 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14785 error_found = 1;
14786
14787 /* It's invalid to use a return statement in a static block */
14788 if (DECL_CLINIT_P (current_function_decl))
14789 error_found = 1;
14790
14791 /* It's invalid to have a no return value within a function that
14792 isn't declared with the keyword `void' */
14793 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14794 error_found = 2;
14795
14796 if (DECL_INSTINIT_P (current_function_decl))
14797 error_found = 1;
14798
14799 if (error_found)
14800 {
14801 if (DECL_INSTINIT_P (current_function_decl))
14802 parse_error_context (wfl_operator,
14803 "`return' inside instance initializer");
14804
14805 else if (DECL_CLINIT_P (current_function_decl))
14806 parse_error_context (wfl_operator,
14807 "`return' inside static initializer");
14808
14809 else if (!DECL_CONSTRUCTOR_P (meth))
14810 {
14811 char *t = xstrdup (lang_printable_name (mtype, 0));
14812 parse_error_context (wfl_operator,
14813 "`return' with%s value from `%s %s'",
14814 (error_found == 1 ? "" : "out"),
14815 t, lang_printable_name (meth, 0));
14816 free (t);
14817 }
14818 else
14819 parse_error_context (wfl_operator,
14820 "`return' with value from constructor `%s'",
14821 lang_printable_name (meth, 0));
14822 return error_mark_node;
14823 }
14824
14825 /* If we have a return_exp, build a modify expression and expand
14826 it. Note: at that point, the assignment is declared valid, but we
14827 may want to carry some more hacks */
14828 if (return_exp)
14829 {
14830 tree exp = java_complete_tree (return_exp);
14831 tree modify, patched;
14832
14833 if ((patched = patch_string (exp)))
14834 exp = patched;
14835
14836 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14837 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14838 modify = java_complete_tree (modify);
14839
14840 if (modify != error_mark_node)
14841 {
14842 TREE_SIDE_EFFECTS (modify) = 1;
14843 TREE_OPERAND (node, 0) = modify;
14844 }
14845 else
14846 return error_mark_node;
14847 }
14848 TREE_TYPE (node) = void_type_node;
14849 TREE_SIDE_EFFECTS (node) = 1;
14850 return node;
14851 }
14852
14853 /* 14.8 The if Statement */
14854
14855 static tree
14856 build_if_else_statement (location, expression, if_body, else_body)
14857 int location;
14858 tree expression, if_body, else_body;
14859 {
14860 tree node;
14861 if (!else_body)
14862 else_body = empty_stmt_node;
14863 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14864 EXPR_WFL_LINECOL (node) = location;
14865 node = build_debugable_stmt (location, node);
14866 return node;
14867 }
14868
14869 static tree
14870 patch_if_else_statement (node)
14871 tree node;
14872 {
14873 tree expression = TREE_OPERAND (node, 0);
14874 int can_complete_normally
14875 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14876 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
14877
14878 TREE_TYPE (node) = error_mark_node;
14879 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14880
14881 /* The type of expression must be boolean */
14882 if (TREE_TYPE (expression) != boolean_type_node
14883 && TREE_TYPE (expression) != promoted_boolean_type_node)
14884 {
14885 parse_error_context
14886 (wfl_operator,
14887 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
14888 lang_printable_name (TREE_TYPE (expression), 0));
14889 return error_mark_node;
14890 }
14891
14892 if (TREE_CODE (expression) == INTEGER_CST)
14893 {
14894 if (integer_zerop (expression))
14895 node = TREE_OPERAND (node, 2);
14896 else
14897 node = TREE_OPERAND (node, 1);
14898 if (CAN_COMPLETE_NORMALLY (node) != can_complete_normally)
14899 {
14900 node = build (COMPOUND_EXPR, void_type_node, node, empty_stmt_node);
14901 CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
14902 }
14903 return node;
14904 }
14905 TREE_TYPE (node) = void_type_node;
14906 TREE_SIDE_EFFECTS (node) = 1;
14907 CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
14908 return node;
14909 }
14910
14911 /* 14.6 Labeled Statements */
14912
14913 /* Action taken when a lableled statement is parsed. a new
14914 LABELED_BLOCK_EXPR is created. No statement is attached to the
14915 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
14916
14917 static tree
14918 build_labeled_block (location, label)
14919 int location;
14920 tree label;
14921 {
14922 tree label_name ;
14923 tree label_decl, node;
14924 if (label == NULL_TREE || label == continue_identifier_node)
14925 label_name = label;
14926 else
14927 {
14928 label_name = merge_qualified_name (label_id, label);
14929 /* Issue an error if we try to reuse a label that was previously
14930 declared */
14931 if (IDENTIFIER_LOCAL_VALUE (label_name))
14932 {
14933 EXPR_WFL_LINECOL (wfl_operator) = location;
14934 parse_error_context (wfl_operator,
14935 "Declaration of `%s' shadows a previous label declaration",
14936 IDENTIFIER_POINTER (label));
14937 EXPR_WFL_LINECOL (wfl_operator) =
14938 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14939 parse_error_context (wfl_operator,
14940 "This is the location of the previous declaration of label `%s'",
14941 IDENTIFIER_POINTER (label));
14942 java_error_count--;
14943 }
14944 }
14945
14946 label_decl = create_label_decl (label_name);
14947 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14948 EXPR_WFL_LINECOL (node) = location;
14949 TREE_SIDE_EFFECTS (node) = 1;
14950 return node;
14951 }
14952
14953 /* A labeled statement LBE is attached a statement. */
14954
14955 static tree
14956 finish_labeled_statement (lbe, statement)
14957 tree lbe; /* Labeled block expr */
14958 tree statement;
14959 {
14960 /* In anyways, tie the loop to its statement */
14961 LABELED_BLOCK_BODY (lbe) = statement;
14962 pop_labeled_block ();
14963 POP_LABELED_BLOCK ();
14964 return lbe;
14965 }
14966
14967 /* 14.10, 14.11, 14.12 Loop Statements */
14968
14969 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14970 list. */
14971
14972 static tree
14973 build_new_loop (loop_body)
14974 tree loop_body;
14975 {
14976 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14977 TREE_SIDE_EFFECTS (loop) = 1;
14978 PUSH_LOOP (loop);
14979 return loop;
14980 }
14981
14982 /* Create a loop body according to the following structure:
14983 COMPOUND_EXPR
14984 COMPOUND_EXPR (loop main body)
14985 EXIT_EXPR (this order is for while/for loops.
14986 LABELED_BLOCK_EXPR the order is reversed for do loops)
14987 LABEL_DECL (a continue occurring here branches at the
14988 BODY end of this labeled block)
14989 INCREMENT (if any)
14990
14991 REVERSED, if nonzero, tells that the loop condition expr comes
14992 after the body, like in the do-while loop.
14993
14994 To obtain a loop, the loop body structure described above is
14995 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14996
14997 LABELED_BLOCK_EXPR
14998 LABEL_DECL (use this label to exit the loop)
14999 LOOP_EXPR
15000 <structure described above> */
15001
15002 static tree
15003 build_loop_body (location, condition, reversed)
15004 int location;
15005 tree condition;
15006 int reversed;
15007 {
15008 tree first, second, body;
15009
15010 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
15011 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
15012 condition = build_debugable_stmt (location, condition);
15013 TREE_SIDE_EFFECTS (condition) = 1;
15014
15015 body = build_labeled_block (0, continue_identifier_node);
15016 first = (reversed ? body : condition);
15017 second = (reversed ? condition : body);
15018 return
15019 build (COMPOUND_EXPR, NULL_TREE,
15020 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
15021 }
15022
15023 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
15024 their order) on the current loop. Unlink the current loop from the
15025 loop list. */
15026
15027 static tree
15028 finish_loop_body (location, condition, body, reversed)
15029 int location;
15030 tree condition, body;
15031 int reversed;
15032 {
15033 tree to_return = ctxp->current_loop;
15034 tree loop_body = LOOP_EXPR_BODY (to_return);
15035 if (condition)
15036 {
15037 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
15038 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
15039 The real EXIT_EXPR is one operand further. */
15040 EXPR_WFL_LINECOL (cnode) = location;
15041 /* This one is for accurate error reports */
15042 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
15043 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
15044 }
15045 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
15046 POP_LOOP ();
15047 return to_return;
15048 }
15049
15050 /* Tailored version of finish_loop_body for FOR loops, when FOR
15051 loops feature the condition part */
15052
15053 static tree
15054 finish_for_loop (location, condition, update, body)
15055 int location;
15056 tree condition, update, body;
15057 {
15058 /* Put the condition and the loop body in place */
15059 tree loop = finish_loop_body (location, condition, body, 0);
15060 /* LOOP is the current loop which has been now popped of the loop
15061 stack. Mark the update block as reachable and install it. We do
15062 this because the (current interpretation of the) JLS requires
15063 that the update expression be considered reachable even if the
15064 for loop's body doesn't complete normally. */
15065 if (update != NULL_TREE && update != empty_stmt_node)
15066 {
15067 tree up2 = update;
15068 if (TREE_CODE (up2) == EXPR_WITH_FILE_LOCATION)
15069 up2 = EXPR_WFL_NODE (up2);
15070 /* Try to detect constraint violations. These would be
15071 programming errors somewhere. */
15072 if (! IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (up2)))
15073 || TREE_CODE (up2) == LOOP_EXPR)
15074 abort ();
15075 SUPPRESS_UNREACHABLE_ERROR (up2) = 1;
15076 }
15077 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
15078 return loop;
15079 }
15080
15081 /* Try to find the loop a block might be related to. This comprises
15082 the case where the LOOP_EXPR is found as the second operand of a
15083 COMPOUND_EXPR, because the loop happens to have an initialization
15084 part, then expressed as the first operand of the COMPOUND_EXPR. If
15085 the search finds something, 1 is returned. Otherwise, 0 is
15086 returned. The search is assumed to start from a
15087 LABELED_BLOCK_EXPR's block. */
15088
15089 static tree
15090 search_loop (statement)
15091 tree statement;
15092 {
15093 if (TREE_CODE (statement) == LOOP_EXPR)
15094 return statement;
15095
15096 if (TREE_CODE (statement) == BLOCK)
15097 statement = BLOCK_SUBBLOCKS (statement);
15098 else
15099 return NULL_TREE;
15100
15101 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15102 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
15103 statement = TREE_OPERAND (statement, 1);
15104
15105 return (TREE_CODE (statement) == LOOP_EXPR
15106 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
15107 }
15108
15109 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
15110 returned otherwise. */
15111
15112 static int
15113 labeled_block_contains_loop_p (block, loop)
15114 tree block, loop;
15115 {
15116 if (!block)
15117 return 0;
15118
15119 if (LABELED_BLOCK_BODY (block) == loop)
15120 return 1;
15121
15122 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
15123 return 1;
15124
15125 return 0;
15126 }
15127
15128 /* If the loop isn't surrounded by a labeled statement, create one and
15129 insert LOOP as its body. */
15130
15131 static tree
15132 patch_loop_statement (loop)
15133 tree loop;
15134 {
15135 tree loop_label;
15136
15137 TREE_TYPE (loop) = void_type_node;
15138 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
15139 return loop;
15140
15141 loop_label = build_labeled_block (0, NULL_TREE);
15142 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
15143 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
15144 LABELED_BLOCK_BODY (loop_label) = loop;
15145 PUSH_LABELED_BLOCK (loop_label);
15146 return loop_label;
15147 }
15148
15149 /* 14.13, 14.14: break and continue Statements */
15150
15151 /* Build a break or a continue statement. a null NAME indicates an
15152 unlabeled break/continue statement. */
15153
15154 static tree
15155 build_bc_statement (location, is_break, name)
15156 int location, is_break;
15157 tree name;
15158 {
15159 tree break_continue, label_block_expr = NULL_TREE;
15160
15161 if (name)
15162 {
15163 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15164 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15165 /* Null means that we don't have a target for this named
15166 break/continue. In this case, we make the target to be the
15167 label name, so that the error can be reported accuratly in
15168 patch_bc_statement. */
15169 label_block_expr = EXPR_WFL_NODE (name);
15170 }
15171 /* Unlabeled break/continue will be handled during the
15172 break/continue patch operation */
15173 break_continue
15174 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
15175
15176 IS_BREAK_STMT_P (break_continue) = is_break;
15177 TREE_SIDE_EFFECTS (break_continue) = 1;
15178 EXPR_WFL_LINECOL (break_continue) = location;
15179 break_continue = build_debugable_stmt (location, break_continue);
15180 return break_continue;
15181 }
15182
15183 /* Verification of a break/continue statement. */
15184
15185 static tree
15186 patch_bc_statement (node)
15187 tree node;
15188 {
15189 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15190 tree labeled_block = ctxp->current_labeled_block;
15191 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15192
15193 /* Having an identifier here means that the target is unknown. */
15194 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15195 {
15196 parse_error_context (wfl_operator, "No label definition found for `%s'",
15197 IDENTIFIER_POINTER (bc_label));
15198 return error_mark_node;
15199 }
15200 if (! IS_BREAK_STMT_P (node))
15201 {
15202 /* It's a continue statement. */
15203 for (;; labeled_block = TREE_CHAIN (labeled_block))
15204 {
15205 if (labeled_block == NULL_TREE)
15206 {
15207 if (bc_label == NULL_TREE)
15208 parse_error_context (wfl_operator,
15209 "`continue' must be in loop");
15210 else
15211 parse_error_context
15212 (wfl_operator, "continue label `%s' does not name a loop",
15213 IDENTIFIER_POINTER (bc_label));
15214 return error_mark_node;
15215 }
15216 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15217 == continue_identifier_node)
15218 && (bc_label == NULL_TREE
15219 || TREE_CHAIN (labeled_block) == bc_label))
15220 {
15221 bc_label = labeled_block;
15222 break;
15223 }
15224 }
15225 }
15226 else if (!bc_label)
15227 {
15228 for (;; labeled_block = TREE_CHAIN (labeled_block))
15229 {
15230 if (labeled_block == NULL_TREE)
15231 {
15232 parse_error_context (wfl_operator,
15233 "`break' must be in loop or switch");
15234 return error_mark_node;
15235 }
15236 target_stmt = LABELED_BLOCK_BODY (labeled_block);
15237 if (TREE_CODE (target_stmt) == SWITCH_EXPR
15238 || search_loop (target_stmt))
15239 {
15240 bc_label = labeled_block;
15241 break;
15242 }
15243 }
15244 }
15245
15246 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15247 CAN_COMPLETE_NORMALLY (bc_label) = 1;
15248
15249 /* Our break/continue don't return values. */
15250 TREE_TYPE (node) = void_type_node;
15251 /* Encapsulate the break within a compound statement so that it's
15252 expanded all the times by expand_expr (and not clobbered
15253 sometimes, like after a if statement) */
15254 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15255 TREE_SIDE_EFFECTS (node) = 1;
15256 return node;
15257 }
15258
15259 /* Process the exit expression belonging to a loop. Its type must be
15260 boolean. */
15261
15262 static tree
15263 patch_exit_expr (node)
15264 tree node;
15265 {
15266 tree expression = TREE_OPERAND (node, 0);
15267 TREE_TYPE (node) = error_mark_node;
15268 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15269
15270 /* The type of expression must be boolean */
15271 if (TREE_TYPE (expression) != boolean_type_node)
15272 {
15273 parse_error_context
15274 (wfl_operator,
15275 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
15276 lang_printable_name (TREE_TYPE (expression), 0));
15277 return error_mark_node;
15278 }
15279 /* Now we know things are allright, invert the condition, fold and
15280 return */
15281 TREE_OPERAND (node, 0) =
15282 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15283
15284 if (! integer_zerop (TREE_OPERAND (node, 0))
15285 && ctxp->current_loop != NULL_TREE
15286 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15287 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15288 if (! integer_onep (TREE_OPERAND (node, 0)))
15289 CAN_COMPLETE_NORMALLY (node) = 1;
15290
15291
15292 TREE_TYPE (node) = void_type_node;
15293 return node;
15294 }
15295
15296 /* 14.9 Switch statement */
15297
15298 static tree
15299 patch_switch_statement (node)
15300 tree node;
15301 {
15302 tree se = TREE_OPERAND (node, 0), se_type;
15303 tree save, iter;
15304
15305 /* Complete the switch expression */
15306 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15307 se_type = TREE_TYPE (se);
15308 /* The type of the switch expression must be char, byte, short or
15309 int */
15310 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15311 {
15312 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15313 parse_error_context (wfl_operator,
15314 "Incompatible type for `switch'. Can't convert `%s' to `int'",
15315 lang_printable_name (se_type, 0));
15316 /* This is what java_complete_tree will check */
15317 TREE_OPERAND (node, 0) = error_mark_node;
15318 return error_mark_node;
15319 }
15320
15321 /* Save and restore the outer case label list. */
15322 save = case_label_list;
15323 case_label_list = NULL_TREE;
15324
15325 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15326
15327 /* See if we've found a duplicate label. We can't leave this until
15328 code generation, because in `--syntax-only' and `-C' modes we
15329 don't do ordinary code generation. */
15330 for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15331 {
15332 HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15333 tree subiter;
15334 for (subiter = TREE_CHAIN (iter);
15335 subiter != NULL_TREE;
15336 subiter = TREE_CHAIN (subiter))
15337 {
15338 HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15339 if (val == subval)
15340 {
15341 EXPR_WFL_LINECOL (wfl_operator)
15342 = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15343 /* The case_label_list is in reverse order, so print the
15344 outer label first. */
15345 parse_error_context (wfl_operator, "duplicate case label: `"
15346 HOST_WIDE_INT_PRINT_DEC "'", subval);
15347 EXPR_WFL_LINECOL (wfl_operator)
15348 = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15349 parse_error_context (wfl_operator, "original label is here");
15350
15351 break;
15352 }
15353 }
15354 }
15355
15356 case_label_list = save;
15357
15358 /* Ready to return */
15359 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15360 {
15361 TREE_TYPE (node) = error_mark_node;
15362 return error_mark_node;
15363 }
15364 TREE_TYPE (node) = void_type_node;
15365 TREE_SIDE_EFFECTS (node) = 1;
15366 CAN_COMPLETE_NORMALLY (node)
15367 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15368 || ! SWITCH_HAS_DEFAULT (node);
15369 return node;
15370 }
15371
15372 /* Assertions. */
15373
15374 /* Build an assertion expression for `assert CONDITION : VALUE'; VALUE
15375 might be NULL_TREE. */
15376 static tree
15377 build_assertion (location, condition, value)
15378 int location;
15379 tree condition, value;
15380 {
15381 tree node;
15382 tree klass = GET_CPC ();
15383
15384 if (! CLASS_USES_ASSERTIONS (klass))
15385 {
15386 tree field, classdollar, id, call;
15387 tree class_type = TREE_TYPE (klass);
15388
15389 field = add_field (class_type,
15390 get_identifier ("$assertionsDisabled"),
15391 boolean_type_node,
15392 ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
15393 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field);
15394 FIELD_SYNTHETIC (field) = 1;
15395
15396 if (!TYPE_DOT_CLASS (class_type))
15397 build_dot_class_method (class_type);
15398 classdollar = build_dot_class_method_invocation (class_type);
15399
15400 /* Call CLASS.desiredAssertionStatus(). */
15401 id = build_wfl_node (get_identifier ("desiredAssertionStatus"));
15402 call = build (CALL_EXPR, NULL_TREE, id, NULL_TREE, NULL_TREE);
15403 call = make_qualified_primary (classdollar, call, location);
15404 TREE_SIDE_EFFECTS (call) = 1;
15405
15406 /* Invert to obtain !CLASS.desiredAssertionStatus(). This may
15407 seem odd, but we do it to generate code identical to that of
15408 the JDK. */
15409 call = build1 (TRUTH_NOT_EXPR, NULL_TREE, call);
15410 TREE_SIDE_EFFECTS (call) = 1;
15411 DECL_INITIAL (field) = call;
15412
15413 /* Record the initializer in the initializer statement list. */
15414 call = build (MODIFY_EXPR, NULL_TREE, field, call);
15415 TREE_CHAIN (call) = CPC_STATIC_INITIALIZER_STMT (ctxp);
15416 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, call);
15417 MODIFY_EXPR_FROM_INITIALIZATION_P (call) = 1;
15418
15419 CLASS_USES_ASSERTIONS (klass) = 1;
15420 }
15421
15422 if (value != NULL_TREE)
15423 value = tree_cons (NULL_TREE, value, NULL_TREE);
15424
15425 node = build_wfl_node (get_identifier ("java"));
15426 node = make_qualified_name (node, build_wfl_node (get_identifier ("lang")),
15427 location);
15428 node = make_qualified_name (node, build_wfl_node (get_identifier ("AssertionError")),
15429 location);
15430
15431 node = build (NEW_CLASS_EXPR, NULL_TREE, node, value, NULL_TREE);
15432 TREE_SIDE_EFFECTS (node) = 1;
15433 /* It is too early to use BUILD_THROW. */
15434 node = build1 (THROW_EXPR, NULL_TREE, node);
15435 TREE_SIDE_EFFECTS (node) = 1;
15436
15437 /* We invert the condition; if we just put NODE as the `else' part
15438 then we generate weird-looking bytecode. */
15439 condition = build1 (TRUTH_NOT_EXPR, NULL_TREE, condition);
15440 /* Check $assertionsDisabled. */
15441 condition
15442 = build (TRUTH_ANDIF_EXPR, NULL_TREE,
15443 build1 (TRUTH_NOT_EXPR, NULL_TREE,
15444 build_wfl_node (get_identifier ("$assertionsDisabled"))),
15445 condition);
15446 node = build_if_else_statement (location, condition, node, NULL_TREE);
15447 return node;
15448 }
15449
15450 /* 14.18 The try/catch statements */
15451
15452 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15453 catches TYPE and executes CATCH_STMTS. */
15454
15455 static tree
15456 encapsulate_with_try_catch (location, type, try_stmts, catch_stmts)
15457 int location;
15458 tree type, try_stmts, catch_stmts;
15459 {
15460 tree try_block, catch_clause_param, catch_block, catch;
15461
15462 /* First build a try block */
15463 try_block = build_expr_block (try_stmts, NULL_TREE);
15464
15465 /* Build a catch block: we need a catch clause parameter */
15466 catch_clause_param = build_decl (VAR_DECL,
15467 wpv_id, build_pointer_type (type));
15468 /* And a block */
15469 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15470
15471 /* Initialize the variable and store in the block */
15472 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15473 build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15474 add_stmt_to_block (catch_block, NULL_TREE, catch);
15475
15476 /* Add the catch statements */
15477 add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15478
15479 /* Now we can build a CATCH_EXPR */
15480 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
15481
15482 return build_try_statement (location, try_block, catch_block);
15483 }
15484
15485 static tree
15486 build_try_statement (location, try_block, catches)
15487 int location;
15488 tree try_block, catches;
15489 {
15490 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
15491 EXPR_WFL_LINECOL (node) = location;
15492 return node;
15493 }
15494
15495 static tree
15496 build_try_finally_statement (location, try_block, finally)
15497 int location;
15498 tree try_block, finally;
15499 {
15500 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15501 EXPR_WFL_LINECOL (node) = location;
15502 return node;
15503 }
15504
15505 static tree
15506 patch_try_statement (node)
15507 tree node;
15508 {
15509 int error_found = 0;
15510 tree try = TREE_OPERAND (node, 0);
15511 /* Exception handlers are considered in left to right order */
15512 tree catch = nreverse (TREE_OPERAND (node, 1));
15513 tree current, caught_type_list = NULL_TREE;
15514
15515 /* Check catch clauses, if any. Every time we find an error, we try
15516 to process the next catch clause. We process the catch clause before
15517 the try block so that when processing the try block we can check thrown
15518 exceptions againts the caught type list. */
15519 for (current = catch; current; current = TREE_CHAIN (current))
15520 {
15521 tree carg_decl, carg_type;
15522 tree sub_current, catch_block, catch_clause;
15523 int unreachable;
15524
15525 /* At this point, the structure of the catch clause is
15526 CATCH_EXPR (catch node)
15527 BLOCK (with the decl of the parameter)
15528 COMPOUND_EXPR
15529 MODIFY_EXPR (assignment of the catch parameter)
15530 BLOCK (catch clause block)
15531 */
15532 catch_clause = TREE_OPERAND (current, 0);
15533 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15534 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15535
15536 /* Catch clauses can't have more than one parameter declared,
15537 but it's already enforced by the grammar. Make sure that the
15538 only parameter of the clause statement in of class Throwable
15539 or a subclass of Throwable, but that was done earlier. The
15540 catch clause parameter type has also been resolved. */
15541
15542 /* Just make sure that the catch clause parameter type inherits
15543 from java.lang.Throwable */
15544 if (!inherits_from_p (carg_type, throwable_type_node))
15545 {
15546 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15547 parse_error_context (wfl_operator,
15548 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
15549 lang_printable_name (carg_type, 0));
15550 error_found = 1;
15551 continue;
15552 }
15553
15554 /* Partial check for unreachable catch statement: The catch
15555 clause is reachable iff is no earlier catch block A in
15556 the try statement such that the type of the catch
15557 clause's parameter is the same as or a subclass of the
15558 type of A's parameter */
15559 unreachable = 0;
15560 for (sub_current = catch;
15561 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15562 {
15563 tree sub_catch_clause, decl;
15564 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15565 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15566
15567 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15568 {
15569 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15570 parse_error_context
15571 (wfl_operator,
15572 "`catch' not reached because of the catch clause at line %d",
15573 EXPR_WFL_LINENO (sub_current));
15574 unreachable = error_found = 1;
15575 break;
15576 }
15577 }
15578 /* Complete the catch clause block */
15579 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15580 if (catch_block == error_mark_node)
15581 {
15582 error_found = 1;
15583 continue;
15584 }
15585 if (CAN_COMPLETE_NORMALLY (catch_block))
15586 CAN_COMPLETE_NORMALLY (node) = 1;
15587 TREE_OPERAND (current, 0) = catch_block;
15588
15589 if (unreachable)
15590 continue;
15591
15592 /* Things to do here: the exception must be thrown */
15593
15594 /* Link this type to the caught type list */
15595 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15596 }
15597
15598 PUSH_EXCEPTIONS (caught_type_list);
15599 if ((try = java_complete_tree (try)) == error_mark_node)
15600 error_found = 1;
15601 if (CAN_COMPLETE_NORMALLY (try))
15602 CAN_COMPLETE_NORMALLY (node) = 1;
15603 POP_EXCEPTIONS ();
15604
15605 /* Verification ends here */
15606 if (error_found)
15607 return error_mark_node;
15608
15609 TREE_OPERAND (node, 0) = try;
15610 TREE_OPERAND (node, 1) = catch;
15611 TREE_TYPE (node) = void_type_node;
15612 return node;
15613 }
15614
15615 /* 14.17 The synchronized Statement */
15616
15617 static tree
15618 patch_synchronized_statement (node, wfl_op1)
15619 tree node, wfl_op1;
15620 {
15621 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15622 tree block = TREE_OPERAND (node, 1);
15623
15624 tree tmp, enter, exit, expr_decl, assignment;
15625
15626 if (expr == error_mark_node)
15627 {
15628 block = java_complete_tree (block);
15629 return expr;
15630 }
15631
15632 /* We might be trying to synchronize on a STRING_CST */
15633 if ((tmp = patch_string (expr)))
15634 expr = tmp;
15635
15636 /* The TYPE of expr must be a reference type */
15637 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15638 {
15639 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15640 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
15641 lang_printable_name (TREE_TYPE (expr), 0));
15642 return error_mark_node;
15643 }
15644
15645 if (flag_emit_xref)
15646 {
15647 TREE_OPERAND (node, 0) = expr;
15648 TREE_OPERAND (node, 1) = java_complete_tree (block);
15649 CAN_COMPLETE_NORMALLY (node) = 1;
15650 return node;
15651 }
15652
15653 /* Generate a try-finally for the synchronized statement, except
15654 that the handler that catches all throw exception calls
15655 _Jv_MonitorExit and then rethrow the exception.
15656 The synchronized statement is then implemented as:
15657 TRY
15658 {
15659 _Jv_MonitorEnter (expression)
15660 synchronized_block
15661 _Jv_MonitorExit (expression)
15662 }
15663 CATCH_ALL
15664 {
15665 e = _Jv_exception_info ();
15666 _Jv_MonitorExit (expression)
15667 Throw (e);
15668 } */
15669
15670 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15671 BUILD_MONITOR_ENTER (enter, expr_decl);
15672 BUILD_MONITOR_EXIT (exit, expr_decl);
15673 CAN_COMPLETE_NORMALLY (enter) = 1;
15674 CAN_COMPLETE_NORMALLY (exit) = 1;
15675 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15676 TREE_SIDE_EFFECTS (assignment) = 1;
15677 node = build (COMPOUND_EXPR, NULL_TREE,
15678 build (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15679 build (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15680 node = build_expr_block (node, expr_decl);
15681
15682 return java_complete_tree (node);
15683 }
15684
15685 /* 14.16 The throw Statement */
15686
15687 static tree
15688 patch_throw_statement (node, wfl_op1)
15689 tree node, wfl_op1;
15690 {
15691 tree expr = TREE_OPERAND (node, 0);
15692 tree type = TREE_TYPE (expr);
15693 int unchecked_ok = 0, tryblock_throws_ok = 0;
15694
15695 /* Thrown expression must be assignable to java.lang.Throwable */
15696 if (!try_reference_assignconv (throwable_type_node, expr))
15697 {
15698 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15699 parse_error_context (wfl_operator,
15700 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
15701 lang_printable_name (type, 0));
15702 /* If the thrown expression was a reference, we further the
15703 compile-time check. */
15704 if (!JREFERENCE_TYPE_P (type))
15705 return error_mark_node;
15706 }
15707
15708 /* At least one of the following must be true */
15709
15710 /* The type of the throw expression is a not checked exception,
15711 i.e. is a unchecked expression. */
15712 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15713
15714 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15715 /* An instance can't throw a checked exception unless that exception
15716 is explicitly declared in the `throws' clause of each
15717 constructor. This doesn't apply to anonymous classes, since they
15718 don't have declared constructors. */
15719 if (!unchecked_ok
15720 && DECL_INSTINIT_P (current_function_decl)
15721 && !ANONYMOUS_CLASS_P (current_class))
15722 {
15723 tree current;
15724 for (current = TYPE_METHODS (current_class); current;
15725 current = TREE_CHAIN (current))
15726 if (DECL_CONSTRUCTOR_P (current)
15727 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15728 {
15729 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)",
15730 lang_printable_name (TREE_TYPE (expr), 0));
15731 return error_mark_node;
15732 }
15733 }
15734
15735 /* Throw is contained in a try statement and at least one catch
15736 clause can receive the thrown expression or the current method is
15737 declared to throw such an exception. Or, the throw statement is
15738 contained in a method or constructor declaration and the type of
15739 the Expression is assignable to at least one type listed in the
15740 throws clause the declaration. */
15741 if (!unchecked_ok)
15742 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15743 if (!(unchecked_ok || tryblock_throws_ok))
15744 {
15745 /* If there is a surrounding try block that has no matching
15746 clatch clause, report it first. A surrounding try block exits
15747 only if there is something after the list of checked
15748 exception thrown by the current function (if any). */
15749 if (IN_TRY_BLOCK_P ())
15750 parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
15751 lang_printable_name (type, 0));
15752 /* If we have no surrounding try statement and the method doesn't have
15753 any throws, report it now. FIXME */
15754
15755 /* We report that the exception can't be throw from a try block
15756 in all circumstances but when the `throw' is inside a static
15757 block. */
15758 else if (!EXCEPTIONS_P (currently_caught_type_list)
15759 && !tryblock_throws_ok)
15760 {
15761 if (DECL_CLINIT_P (current_function_decl))
15762 parse_error_context (wfl_operator,
15763 "Checked exception `%s' can't be thrown in initializer",
15764 lang_printable_name (type, 0));
15765 else
15766 parse_error_context (wfl_operator,
15767 "Checked exception `%s' isn't thrown from a `try' block",
15768 lang_printable_name (type, 0));
15769 }
15770 /* Otherwise, the current method doesn't have the appropriate
15771 throws declaration */
15772 else
15773 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
15774 lang_printable_name (type, 0));
15775 return error_mark_node;
15776 }
15777
15778 if (! flag_emit_class_files && ! flag_emit_xref)
15779 BUILD_THROW (node, expr);
15780
15781 /* If doing xrefs, keep the location where the `throw' was seen. */
15782 if (flag_emit_xref)
15783 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15784 return node;
15785 }
15786
15787 /* Check that exception said to be thrown by method DECL can be
15788 effectively caught from where DECL is invoked. */
15789
15790 static void
15791 check_thrown_exceptions (location, decl)
15792 int location;
15793 tree decl;
15794 {
15795 tree throws;
15796 /* For all the unchecked exceptions thrown by DECL */
15797 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15798 throws = TREE_CHAIN (throws))
15799 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15800 {
15801 #if 1
15802 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15803 if (DECL_NAME (decl) == get_identifier ("clone"))
15804 continue;
15805 #endif
15806 EXPR_WFL_LINECOL (wfl_operator) = location;
15807 if (DECL_FINIT_P (current_function_decl))
15808 parse_error_context
15809 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15810 lang_printable_name (TREE_VALUE (throws), 0));
15811 else
15812 {
15813 parse_error_context
15814 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
15815 lang_printable_name (TREE_VALUE (throws), 0),
15816 (DECL_INIT_P (current_function_decl) ?
15817 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15818 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15819 }
15820 }
15821 }
15822
15823 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15824 try-catch blocks, OR is listed in the `throws' clause of the
15825 current method. */
15826
15827 static int
15828 check_thrown_exceptions_do (exception)
15829 tree exception;
15830 {
15831 tree list = currently_caught_type_list;
15832 resolve_and_layout (exception, NULL_TREE);
15833 /* First, all the nested try-catch-finally at that stage. The
15834 last element contains `throws' clause exceptions, if any. */
15835 if (IS_UNCHECKED_EXCEPTION_P (exception))
15836 return 1;
15837 while (list)
15838 {
15839 tree caught;
15840 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15841 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15842 return 1;
15843 list = TREE_CHAIN (list);
15844 }
15845 return 0;
15846 }
15847
15848 static void
15849 purge_unchecked_exceptions (mdecl)
15850 tree mdecl;
15851 {
15852 tree throws = DECL_FUNCTION_THROWS (mdecl);
15853 tree new = NULL_TREE;
15854
15855 while (throws)
15856 {
15857 tree next = TREE_CHAIN (throws);
15858 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15859 {
15860 TREE_CHAIN (throws) = new;
15861 new = throws;
15862 }
15863 throws = next;
15864 }
15865 /* List is inverted here, but it doesn't matter */
15866 DECL_FUNCTION_THROWS (mdecl) = new;
15867 }
15868
15869 /* This function goes over all of CLASS_TYPE ctors and checks whether
15870 each of them features at least one unchecked exception in its
15871 `throws' clause. If it's the case, it returns `true', `false'
15872 otherwise. */
15873
15874 static bool
15875 ctors_unchecked_throws_clause_p (class_type)
15876 tree class_type;
15877 {
15878 tree current;
15879
15880 for (current = TYPE_METHODS (class_type); current;
15881 current = TREE_CHAIN (current))
15882 {
15883 bool ctu = false; /* Ctor Throws Unchecked */
15884 if (DECL_CONSTRUCTOR_P (current))
15885 {
15886 tree throws;
15887 for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
15888 throws = TREE_CHAIN (throws))
15889 if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
15890 ctu = true;
15891 }
15892 /* We return false as we found one ctor that is unfit. */
15893 if (!ctu && DECL_CONSTRUCTOR_P (current))
15894 return false;
15895 }
15896 /* All ctors feature at least one unchecked exception in their
15897 `throws' clause. */
15898 return true;
15899 }
15900
15901 /* 15.24 Conditional Operator ?: */
15902
15903 static tree
15904 patch_conditional_expr (node, wfl_cond, wfl_op1)
15905 tree node, wfl_cond, wfl_op1;
15906 {
15907 tree cond = TREE_OPERAND (node, 0);
15908 tree op1 = TREE_OPERAND (node, 1);
15909 tree op2 = TREE_OPERAND (node, 2);
15910 tree resulting_type = NULL_TREE;
15911 tree t1, t2, patched;
15912 int error_found = 0;
15913
15914 /* Operands of ?: might be StringBuffers crafted as a result of a
15915 string concatenation. Obtain a descent operand here. */
15916 if ((patched = patch_string (op1)))
15917 TREE_OPERAND (node, 1) = op1 = patched;
15918 if ((patched = patch_string (op2)))
15919 TREE_OPERAND (node, 2) = op2 = patched;
15920
15921 t1 = TREE_TYPE (op1);
15922 t2 = TREE_TYPE (op2);
15923
15924 /* The first expression must be a boolean */
15925 if (TREE_TYPE (cond) != boolean_type_node)
15926 {
15927 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15928 parse_error_context (wfl_operator,
15929 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
15930 lang_printable_name (TREE_TYPE (cond), 0));
15931 error_found = 1;
15932 }
15933
15934 /* Second and third can be numeric, boolean (i.e. primitive),
15935 references or null. Anything else results in an error */
15936 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15937 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15938 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15939 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15940 error_found = 1;
15941
15942 /* Determine the type of the conditional expression. Same types are
15943 easy to deal with */
15944 else if (t1 == t2)
15945 resulting_type = t1;
15946
15947 /* There are different rules for numeric types */
15948 else if (JNUMERIC_TYPE_P (t1))
15949 {
15950 /* if byte/short found, the resulting type is short */
15951 if ((t1 == byte_type_node && t2 == short_type_node)
15952 || (t1 == short_type_node && t2 == byte_type_node))
15953 resulting_type = short_type_node;
15954
15955 /* If t1 is a constant int and t2 is of type byte, short or char
15956 and t1's value fits in t2, then the resulting type is t2 */
15957 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15958 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15959 resulting_type = t2;
15960
15961 /* If t2 is a constant int and t1 is of type byte, short or char
15962 and t2's value fits in t1, then the resulting type is t1 */
15963 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15964 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15965 resulting_type = t1;
15966
15967 /* Otherwise, binary numeric promotion is applied and the
15968 resulting type is the promoted type of operand 1 and 2 */
15969 else
15970 resulting_type = binary_numeric_promotion (t1, t2,
15971 &TREE_OPERAND (node, 1),
15972 &TREE_OPERAND (node, 2));
15973 }
15974
15975 /* Cases of a reference and a null type */
15976 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15977 resulting_type = t1;
15978
15979 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15980 resulting_type = t2;
15981
15982 /* Last case: different reference types. If a type can be converted
15983 into the other one by assignment conversion, the latter
15984 determines the type of the expression */
15985 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15986 resulting_type = promote_type (t1);
15987
15988 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15989 resulting_type = promote_type (t2);
15990
15991 /* If we don't have any resulting type, we're in trouble */
15992 if (!resulting_type)
15993 {
15994 char *t = xstrdup (lang_printable_name (t1, 0));
15995 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15996 parse_error_context (wfl_operator,
15997 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15998 t, lang_printable_name (t2, 0));
15999 free (t);
16000 error_found = 1;
16001 }
16002
16003 if (error_found)
16004 {
16005 TREE_TYPE (node) = error_mark_node;
16006 return error_mark_node;
16007 }
16008
16009 TREE_TYPE (node) = resulting_type;
16010 TREE_SET_CODE (node, COND_EXPR);
16011 CAN_COMPLETE_NORMALLY (node) = 1;
16012 return node;
16013 }
16014
16015 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
16016
16017 static tree
16018 maybe_build_class_init_for_field (decl, expr)
16019 tree decl, expr;
16020 {
16021 tree clas = DECL_CONTEXT (decl);
16022 if (flag_emit_class_files || flag_emit_xref)
16023 return expr;
16024
16025 if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
16026 && FIELD_FINAL (decl))
16027 {
16028 tree init = DECL_INITIAL (decl);
16029 if (init != NULL_TREE)
16030 init = fold_constant_for_init (init, decl);
16031 if (init != NULL_TREE && CONSTANT_VALUE_P (init))
16032 return expr;
16033 }
16034
16035 return build_class_init (clas, expr);
16036 }
16037
16038 /* Try to constant fold NODE.
16039 If NODE is not a constant expression, return NULL_EXPR.
16040 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
16041
16042 static tree
16043 fold_constant_for_init (node, context)
16044 tree node;
16045 tree context;
16046 {
16047 tree op0, op1, val;
16048 enum tree_code code = TREE_CODE (node);
16049
16050 switch (code)
16051 {
16052 case INTEGER_CST:
16053 if (node == null_pointer_node)
16054 return NULL_TREE;
16055 case STRING_CST:
16056 case REAL_CST:
16057 return node;
16058
16059 case PLUS_EXPR:
16060 case MINUS_EXPR:
16061 case MULT_EXPR:
16062 case TRUNC_MOD_EXPR:
16063 case RDIV_EXPR:
16064 case LSHIFT_EXPR:
16065 case RSHIFT_EXPR:
16066 case URSHIFT_EXPR:
16067 case BIT_AND_EXPR:
16068 case BIT_XOR_EXPR:
16069 case BIT_IOR_EXPR:
16070 case TRUTH_ANDIF_EXPR:
16071 case TRUTH_ORIF_EXPR:
16072 case EQ_EXPR:
16073 case NE_EXPR:
16074 case GT_EXPR:
16075 case GE_EXPR:
16076 case LT_EXPR:
16077 case LE_EXPR:
16078 op0 = TREE_OPERAND (node, 0);
16079 op1 = TREE_OPERAND (node, 1);
16080 val = fold_constant_for_init (op0, context);
16081 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16082 return NULL_TREE;
16083 TREE_OPERAND (node, 0) = val;
16084 val = fold_constant_for_init (op1, context);
16085 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16086 return NULL_TREE;
16087 TREE_OPERAND (node, 1) = val;
16088 return patch_binop (node, op0, op1);
16089
16090 case UNARY_PLUS_EXPR:
16091 case NEGATE_EXPR:
16092 case TRUTH_NOT_EXPR:
16093 case BIT_NOT_EXPR:
16094 case CONVERT_EXPR:
16095 op0 = TREE_OPERAND (node, 0);
16096 val = fold_constant_for_init (op0, context);
16097 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16098 return NULL_TREE;
16099 TREE_OPERAND (node, 0) = val;
16100 return patch_unaryop (node, op0);
16101 break;
16102
16103 case COND_EXPR:
16104 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
16105 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16106 return NULL_TREE;
16107 TREE_OPERAND (node, 0) = val;
16108 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
16109 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16110 return NULL_TREE;
16111 TREE_OPERAND (node, 1) = val;
16112 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
16113 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16114 return NULL_TREE;
16115 TREE_OPERAND (node, 2) = val;
16116 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
16117 : TREE_OPERAND (node, 2);
16118
16119 case VAR_DECL:
16120 case FIELD_DECL:
16121 if (! FIELD_FINAL (node)
16122 || DECL_INITIAL (node) == NULL_TREE)
16123 return NULL_TREE;
16124 val = DECL_INITIAL (node);
16125 /* Guard against infinite recursion. */
16126 DECL_INITIAL (node) = NULL_TREE;
16127 val = fold_constant_for_init (val, node);
16128 if (val != NULL_TREE && TREE_CODE (val) != STRING_CST)
16129 val = try_builtin_assignconv (NULL_TREE, TREE_TYPE (node), val);
16130 DECL_INITIAL (node) = val;
16131 return val;
16132
16133 case EXPR_WITH_FILE_LOCATION:
16134 /* Compare java_complete_tree and resolve_expression_name. */
16135 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
16136 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
16137 {
16138 tree name = EXPR_WFL_NODE (node);
16139 tree decl;
16140 if (PRIMARY_P (node))
16141 return NULL_TREE;
16142 else if (! QUALIFIED_P (name))
16143 {
16144 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
16145 if (decl == NULL_TREE
16146 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
16147 return NULL_TREE;
16148 return fold_constant_for_init (decl, decl);
16149 }
16150 else
16151 {
16152 /* Install the proper context for the field resolution.
16153 The prior context is restored once the name is
16154 properly qualified. */
16155 tree saved_current_class = current_class;
16156 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
16157 current_class = DECL_CONTEXT (context);
16158 qualify_ambiguous_name (node);
16159 current_class = saved_current_class;
16160 if (resolve_field_access (node, &decl, NULL)
16161 && decl != NULL_TREE)
16162 return fold_constant_for_init (decl, decl);
16163 return NULL_TREE;
16164 }
16165 }
16166 else
16167 {
16168 op0 = TREE_OPERAND (node, 0);
16169 val = fold_constant_for_init (op0, context);
16170 if (val == NULL_TREE || ! TREE_CONSTANT (val))
16171 return NULL_TREE;
16172 TREE_OPERAND (node, 0) = val;
16173 return val;
16174 }
16175
16176 #ifdef USE_COMPONENT_REF
16177 case IDENTIFIER:
16178 case COMPONENT_REF:
16179 ?;
16180 #endif
16181
16182 default:
16183 return NULL_TREE;
16184 }
16185 }
16186
16187 #ifdef USE_COMPONENT_REF
16188 /* Context is 'T' for TypeName, 'P' for PackageName,
16189 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
16190
16191 tree
16192 resolve_simple_name (name, context)
16193 tree name;
16194 int context;
16195 {
16196 }
16197
16198 tree
16199 resolve_qualified_name (name, context)
16200 tree name;
16201 int context;
16202 {
16203 }
16204 #endif
16205
16206 /* Mark P, which is really a `struct parser_ctxt **' for GC. */
16207
16208 static void
16209 mark_parser_ctxt (p)
16210 void *p;
16211 {
16212 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
16213 #ifndef JC1_LITE
16214 size_t i;
16215 #endif
16216
16217 if (!pc)
16218 return;
16219
16220 #ifndef JC1_LITE
16221 for (i = 0; i < ARRAY_SIZE (pc->modifier_ctx); ++i)
16222 ggc_mark_tree (pc->modifier_ctx[i]);
16223 ggc_mark_tree (pc->class_type);
16224 ggc_mark_tree (pc->function_decl);
16225 ggc_mark_tree (pc->package);
16226 ggc_mark_tree (pc->class_list);
16227 ggc_mark_tree (pc->current_parsed_class);
16228 ggc_mark_tree (pc->current_parsed_class_un);
16229 ggc_mark_tree (pc->non_static_initialized);
16230 ggc_mark_tree (pc->static_initialized);
16231 ggc_mark_tree (pc->instance_initializers);
16232 ggc_mark_tree (pc->import_list);
16233 ggc_mark_tree (pc->import_demand_list);
16234 ggc_mark_tree (pc->current_loop);
16235 ggc_mark_tree (pc->current_labeled_block);
16236 #endif /* JC1_LITE */
16237
16238 if (pc->next)
16239 mark_parser_ctxt (&pc->next);
16240 }
16241
16242 void
16243 init_src_parse ()
16244 {
16245 /* Sanity check; we've been bit by this before. */
16246 if (ARRAY_SIZE (ctxp->modifier_ctx) != MODIFIER_TK - PUBLIC_TK)
16247 abort ();
16248 }
16249
16250 \f
16251
16252 /* This section deals with the functions that are called when tables
16253 recording class initialization information are traversed. */
16254
16255 /* Attach to PTR (a block) the declaration found in ENTRY. */
16256
16257 static int
16258 attach_init_test_initialization_flags (entry, ptr)
16259 PTR *entry;
16260 PTR ptr;
16261 {
16262 tree block = (tree)ptr;
16263 struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
16264
16265 if (block != error_mark_node)
16266 {
16267 TREE_CHAIN (ite->value) = BLOCK_EXPR_DECLS (block);
16268 BLOCK_EXPR_DECLS (block) = ite->value;
16269 }
16270 return true;
16271 }
16272
16273 /* This function is called for each classes that is known definitely
16274 assigned when a given static method was called. This function
16275 augments a compound expression (INFO) storing all assignment to
16276 initialized static class flags if a flag already existed, otherwise
16277 a new one is created. */
16278
16279 static int
16280 emit_test_initialization (entry_p, info)
16281 PTR *entry_p;
16282 PTR info;
16283 {
16284 tree l = (tree) info;
16285 tree decl, init;
16286 tree key = (tree) *entry_p;
16287 tree *ite;
16288 htab_t cf_ht = DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl);
16289
16290 /* If we haven't found a flag and we're dealing with self registered
16291 with current_function_decl, then don't do anything. Self is
16292 always added as definitely initialized but this information is
16293 valid only if used outside the current function. */
16294 if (current_function_decl == TREE_PURPOSE (l)
16295 && java_treetreehash_find (cf_ht, key) == NULL)
16296 return true;
16297
16298 ite = java_treetreehash_new (cf_ht, key);
16299
16300 /* If we don't have a variable, create one and install it. */
16301 if (*ite == NULL)
16302 {
16303 tree block;
16304
16305 decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16306 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16307 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16308 DECL_CONTEXT (decl) = current_function_decl;
16309 DECL_INITIAL (decl) = boolean_true_node;
16310
16311 /* The trick is to find the right context for it. */
16312 block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16313 TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16314 BLOCK_EXPR_DECLS (block) = decl;
16315 *ite = decl;
16316 }
16317 else
16318 decl = *ite;
16319
16320 /* Now simply augment the compound that holds all the assignments
16321 pertaining to this method invocation. */
16322 init = build (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16323 TREE_SIDE_EFFECTS (init) = 1;
16324 TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16325 TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;
16326
16327 return true;
16328 }
16329
16330 #include "gt-java-parse.h"
16331 #include "gtype-java.h"