1 /* Source code parsing and tree node generation for the GNU compiler
2 for the Java(TM) language.
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
23 Java and all Java-based marks are trademarks or registered trademarks
24 of Sun Microsystems, Inc. in the United States and other countries.
25 The Free Software Foundation is independent of Sun Microsystems, Inc. */
27 /* This file parses java source code and issues a tree node image
28 suitable for code generation (byte code and targeted CPU assembly
31 The grammar conforms to the Java grammar described in "The Java(TM)
32 Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
33 1996, ISBN 0-201-63451-1"
35 The following modifications were brought to the original grammar:
37 method_body: added the rule '| block SC_TK'
38 static_initializer: added the rule 'static block SC_TK'.
40 Note: All the extra rules described above should go away when the
41 empty_statement rule will work.
43 statement_nsi: 'nsi' should be read no_short_if.
45 Some rules have been modified to support JDK1.1 inner classes
46 definitions and other extensions. */
57 #include "java-tree.h"
70 #define DIR_SEPARATOR '/'
73 /* Local function prototypes */
74 static char *java_accstring_lookup PARAMS ((int));
75 static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
76 static void variable_redefinition_error PARAMS ((tree, tree, tree, int));
77 static tree create_class PARAMS ((int, tree, tree, tree));
78 static tree create_interface PARAMS ((int, tree, tree));
79 static void end_class_declaration PARAMS ((int));
80 static tree find_field PARAMS ((tree, tree));
81 static tree lookup_field_wrapper PARAMS ((tree, tree));
82 static int duplicate_declaration_error_p PARAMS ((tree, tree, tree));
83 static void register_fields PARAMS ((int, tree, tree));
84 static tree parser_qualified_classname PARAMS ((tree));
85 static int parser_check_super PARAMS ((tree, tree, tree));
86 static int parser_check_super_interface PARAMS ((tree, tree, tree));
87 static void check_modifiers_consistency PARAMS ((int));
88 static tree lookup_cl PARAMS ((tree));
89 static tree lookup_java_method2 PARAMS ((tree, tree, int));
90 static tree method_header PARAMS ((int, tree, tree, tree));
91 static void fix_method_argument_names PARAMS ((tree ,tree));
92 static tree method_declarator PARAMS ((tree, tree));
93 static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
95 static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list))
96 ATTRIBUTE_PRINTF (2, 0);
97 static void parse_ctor_invocation_error PARAMS ((void));
98 static tree parse_jdk1_1_error PARAMS ((const char *));
99 static void complete_class_report_errors PARAMS ((jdep *));
100 static int process_imports PARAMS ((void));
101 static void read_import_dir PARAMS ((tree));
102 static int find_in_imports_on_demand PARAMS ((tree, tree));
103 static void find_in_imports PARAMS ((tree, tree));
104 static void check_inner_class_access PARAMS ((tree, tree, tree));
105 static int check_pkg_class_access PARAMS ((tree, tree, bool));
106 static void register_package PARAMS ((tree));
107 static tree resolve_package PARAMS ((tree, tree *));
108 static tree lookup_package_type PARAMS ((const char *, int));
109 static tree resolve_class PARAMS ((tree, tree, tree, tree));
110 static void declare_local_variables PARAMS ((int, tree, tree));
111 static void dump_java_tree PARAMS ((enum tree_dump_index, tree));
112 static void source_start_java_method PARAMS ((tree));
113 static void source_end_java_method PARAMS ((void));
114 static tree find_name_in_single_imports PARAMS ((tree));
115 static void check_abstract_method_header PARAMS ((tree));
116 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
117 static tree resolve_expression_name PARAMS ((tree, tree *));
118 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
119 static int check_class_interface_creation PARAMS ((int, int, tree,
121 static tree patch_method_invocation PARAMS ((tree, tree, tree, int,
123 static int breakdown_qualified PARAMS ((tree *, tree *, tree));
124 static int in_same_package PARAMS ((tree, tree));
125 static tree resolve_and_layout PARAMS ((tree, tree));
126 static tree qualify_and_find PARAMS ((tree, tree, tree));
127 static tree resolve_no_layout PARAMS ((tree, tree));
128 static int invocation_mode PARAMS ((tree, int));
129 static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
131 static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
133 static tree find_most_specific_methods_list PARAMS ((tree));
134 static int argument_types_convertible PARAMS ((tree, tree));
135 static tree patch_invoke PARAMS ((tree, tree, tree));
136 static int maybe_use_access_method PARAMS ((int, tree *, tree *));
137 static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
138 static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
139 static tree check_inner_circular_reference PARAMS ((tree, tree));
140 static tree check_circular_reference PARAMS ((tree));
141 static tree obtain_incomplete_type PARAMS ((tree));
142 static tree java_complete_lhs PARAMS ((tree));
143 static tree java_complete_tree PARAMS ((tree));
144 static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
145 static int analyze_clinit_body PARAMS ((tree, tree));
146 static int maybe_yank_clinit PARAMS ((tree));
147 static void start_complete_expand_method PARAMS ((tree));
148 static void java_complete_expand_method PARAMS ((tree));
149 static void java_expand_method_bodies PARAMS ((tree));
150 static int unresolved_type_p PARAMS ((tree, tree *));
151 static void create_jdep_list PARAMS ((struct parser_ctxt *));
152 static tree build_expr_block PARAMS ((tree, tree));
153 static tree enter_block PARAMS ((void));
154 static tree exit_block PARAMS ((void));
155 static tree lookup_name_in_blocks PARAMS ((tree));
156 static void maybe_absorb_scoping_blocks PARAMS ((void));
157 static tree build_method_invocation PARAMS ((tree, tree));
158 static tree build_new_invocation PARAMS ((tree, tree));
159 static tree build_assignment PARAMS ((int, int, tree, tree));
160 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
161 static tree patch_assignment PARAMS ((tree, tree));
162 static tree patch_binop PARAMS ((tree, tree, tree));
163 static tree build_unaryop PARAMS ((int, int, tree));
164 static tree build_incdec PARAMS ((int, int, tree, int));
165 static tree patch_unaryop PARAMS ((tree, tree));
166 static tree build_cast PARAMS ((int, tree, tree));
167 static tree build_null_of_type PARAMS ((tree));
168 static tree patch_cast PARAMS ((tree, tree));
169 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
170 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
171 static int valid_cast_to_p PARAMS ((tree, tree));
172 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
173 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
174 static tree try_reference_assignconv PARAMS ((tree, tree));
175 static tree build_unresolved_array_type PARAMS ((tree));
176 static int build_type_name_from_array_name PARAMS ((tree, tree *));
177 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
178 static tree build_array_ref PARAMS ((int, tree, tree));
179 static tree patch_array_ref PARAMS ((tree));
180 static tree make_qualified_name PARAMS ((tree, tree, int));
181 static tree merge_qualified_name PARAMS ((tree, tree));
182 static tree make_qualified_primary PARAMS ((tree, tree, int));
183 static int resolve_qualified_expression_name PARAMS ((tree, tree *,
185 static void qualify_ambiguous_name PARAMS ((tree));
186 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
187 static tree build_newarray_node PARAMS ((tree, tree, int));
188 static tree patch_newarray PARAMS ((tree));
189 static tree resolve_type_during_patch PARAMS ((tree));
190 static tree build_this PARAMS ((int));
191 static tree build_wfl_wrap PARAMS ((tree, int));
192 static tree build_return PARAMS ((int, tree));
193 static tree patch_return PARAMS ((tree));
194 static tree maybe_access_field PARAMS ((tree, tree, tree));
195 static int complete_function_arguments PARAMS ((tree));
196 static int check_for_static_method_reference PARAMS ((tree, tree, tree,
198 static int not_accessible_p PARAMS ((tree, tree, tree, int));
199 static void check_deprecation PARAMS ((tree, tree));
200 static int class_in_current_package PARAMS ((tree));
201 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
202 static tree patch_if_else_statement PARAMS ((tree));
203 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
204 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
205 static tree patch_exit_expr PARAMS ((tree));
206 static tree build_labeled_block PARAMS ((int, tree));
207 static tree finish_labeled_statement PARAMS ((tree, tree));
208 static tree build_bc_statement PARAMS ((int, int, tree));
209 static tree patch_bc_statement PARAMS ((tree));
210 static tree patch_loop_statement PARAMS ((tree));
211 static tree build_new_loop PARAMS ((tree));
212 static tree build_loop_body PARAMS ((int, tree, int));
213 static tree finish_loop_body PARAMS ((int, tree, tree, int));
214 static tree build_debugable_stmt PARAMS ((int, tree));
215 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
216 static tree patch_switch_statement PARAMS ((tree));
217 static tree string_constant_concatenation PARAMS ((tree, tree));
218 static tree build_string_concatenation PARAMS ((tree, tree));
219 static tree patch_string_cst PARAMS ((tree));
220 static tree patch_string PARAMS ((tree));
221 static tree encapsulate_with_try_catch PARAMS ((int, tree, tree, tree));
222 static tree build_try_statement PARAMS ((int, tree, tree));
223 static tree build_try_finally_statement PARAMS ((int, tree, tree));
224 static tree patch_try_statement PARAMS ((tree));
225 static tree patch_synchronized_statement PARAMS ((tree, tree));
226 static tree patch_throw_statement PARAMS ((tree, tree));
227 static void check_thrown_exceptions PARAMS ((int, tree));
228 static int check_thrown_exceptions_do PARAMS ((tree));
229 static void purge_unchecked_exceptions PARAMS ((tree));
230 static bool ctors_unchecked_throws_clause_p PARAMS ((tree));
231 static void check_throws_clauses PARAMS ((tree, tree, tree));
232 static void finish_method_declaration PARAMS ((tree));
233 static tree build_super_invocation PARAMS ((tree));
234 static int verify_constructor_circularity PARAMS ((tree, tree));
235 static char *constructor_circularity_msg PARAMS ((tree, tree));
236 static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
238 static const char *get_printable_method_name PARAMS ((tree));
239 static tree patch_conditional_expr PARAMS ((tree, tree, tree));
240 static tree generate_finit PARAMS ((tree));
241 static tree generate_instinit PARAMS ((tree));
242 static tree build_instinit_invocation PARAMS ((tree));
243 static void fix_constructors PARAMS ((tree));
244 static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
246 static void craft_constructor PARAMS ((tree, tree));
247 static int verify_constructor_super PARAMS ((tree));
248 static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
249 static void start_artificial_method_body PARAMS ((tree));
250 static void end_artificial_method_body PARAMS ((tree));
251 static int check_method_redefinition PARAMS ((tree, tree));
252 static int check_method_types_complete PARAMS ((tree));
253 static void java_check_regular_methods PARAMS ((tree));
254 static void java_check_abstract_methods PARAMS ((tree));
255 static void unreachable_stmt_error PARAMS ((tree));
256 static tree find_expr_with_wfl PARAMS ((tree));
257 static void missing_return_error PARAMS ((tree));
258 static tree build_new_array_init PARAMS ((int, tree));
259 static tree patch_new_array_init PARAMS ((tree, tree));
260 static tree maybe_build_array_element_wfl PARAMS ((tree));
261 static int array_constructor_check_entry PARAMS ((tree, tree));
262 static const char *purify_type_name PARAMS ((const char *));
263 static tree fold_constant_for_init PARAMS ((tree, tree));
264 static tree strip_out_static_field_access_decl PARAMS ((tree));
265 static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
266 static void static_ref_err PARAMS ((tree, tree, tree));
267 static void parser_add_interface PARAMS ((tree, tree, tree));
268 static void add_superinterfaces PARAMS ((tree, tree));
269 static tree jdep_resolve_class PARAMS ((jdep *));
270 static int note_possible_classname PARAMS ((const char *, int));
271 static void java_complete_expand_classes PARAMS ((void));
272 static void java_complete_expand_class PARAMS ((tree));
273 static void java_complete_expand_methods PARAMS ((tree));
274 static tree cut_identifier_in_qualified PARAMS ((tree));
275 static tree java_stabilize_reference PARAMS ((tree));
276 static tree do_unary_numeric_promotion PARAMS ((tree));
277 static char * operator_string PARAMS ((tree));
278 static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
279 static tree merge_string_cste PARAMS ((tree, tree, int));
280 static tree java_refold PARAMS ((tree));
281 static int java_decl_equiv PARAMS ((tree, tree));
282 static int binop_compound_p PARAMS ((enum tree_code));
283 static tree search_loop PARAMS ((tree));
284 static int labeled_block_contains_loop_p PARAMS ((tree, tree));
285 static int check_abstract_method_definitions PARAMS ((int, tree, tree));
286 static void java_check_abstract_method_definitions PARAMS ((tree));
287 static void java_debug_context_do PARAMS ((int));
288 static void java_parser_context_push_initialized_field PARAMS ((void));
289 static void java_parser_context_pop_initialized_field PARAMS ((void));
290 static tree reorder_static_initialized PARAMS ((tree));
291 static void java_parser_context_suspend PARAMS ((void));
292 static void java_parser_context_resume PARAMS ((void));
293 static int pop_current_osb PARAMS ((struct parser_ctxt *));
295 /* JDK 1.1 work. FIXME */
297 static tree maybe_make_nested_class_name PARAMS ((tree));
298 static void make_nested_class_name PARAMS ((tree));
299 static void set_nested_class_simple_name_value PARAMS ((tree, int));
300 static void link_nested_class_to_enclosing PARAMS ((void));
301 static tree resolve_inner_class PARAMS ((struct hash_table *, tree, tree *,
303 static tree find_as_inner_class PARAMS ((tree, tree, tree));
304 static tree find_as_inner_class_do PARAMS ((tree, tree));
305 static int check_inner_class_redefinition PARAMS ((tree, tree));
307 static tree build_thisn_assign PARAMS ((void));
308 static tree build_current_thisn PARAMS ((tree));
309 static tree build_access_to_thisn PARAMS ((tree, tree, int));
310 static tree maybe_build_thisn_access_method PARAMS ((tree));
312 static tree build_outer_field_access PARAMS ((tree, tree));
313 static tree build_outer_field_access_methods PARAMS ((tree));
314 static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
316 static tree build_outer_method_access_method PARAMS ((tree));
317 static tree build_new_access_id PARAMS ((void));
318 static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
321 static int outer_field_access_p PARAMS ((tree, tree));
322 static int outer_field_expanded_access_p PARAMS ((tree, tree *,
324 static tree outer_field_access_fix PARAMS ((tree, tree, tree));
325 static tree build_incomplete_class_ref PARAMS ((int, tree));
326 static tree patch_incomplete_class_ref PARAMS ((tree));
327 static tree create_anonymous_class PARAMS ((int, tree));
328 static void patch_anonymous_class PARAMS ((tree, tree, tree));
329 static void add_inner_class_fields PARAMS ((tree, tree));
331 static tree build_dot_class_method PARAMS ((tree));
332 static tree build_dot_class_method_invocation PARAMS ((tree));
333 static void create_new_parser_context PARAMS ((int));
334 static void mark_parser_ctxt PARAMS ((void *));
335 static tree maybe_build_class_init_for_field PARAMS ((tree, tree));
337 static bool attach_init_test_initialization_flags PARAMS ((struct hash_entry *,
339 static bool emit_test_initialization PARAMS ((struct hash_entry *, PTR));
341 /* Number of error found so far. */
342 int java_error_count;
343 /* Number of warning found so far. */
344 int java_warning_count;
345 /* Tell when not to fold, when doing xrefs */
347 /* Cyclic inheritance report, as it can be set by layout_class */
348 const char *cyclic_inheritance_report;
350 /* The current parser context */
351 struct parser_ctxt *ctxp;
353 /* List of things that were analyzed for which code will be generated */
354 struct parser_ctxt *ctxp_for_generation = NULL;
356 /* binop_lookup maps token to tree_code. It is used where binary
357 operations are involved and required by the parser. RDIV_EXPR
358 covers both integral/floating point division. The code is changed
359 once the type of both operator is worked out. */
361 static const enum tree_code binop_lookup[19] =
363 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
364 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
365 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
366 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
367 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
369 #define BINOP_LOOKUP(VALUE) \
370 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
372 /* This is the end index for binary operators that can also be used
373 in compound assignements. */
374 #define BINOP_COMPOUND_CANDIDATES 11
376 /* The "$L" identifier we use to create labels. */
377 static tree label_id = NULL_TREE;
379 /* The "StringBuffer" identifier used for the String `+' operator. */
380 static tree wfl_string_buffer = NULL_TREE;
382 /* The "append" identifier used for String `+' operator. */
383 static tree wfl_append = NULL_TREE;
385 /* The "toString" identifier used for String `+' operator. */
386 static tree wfl_to_string = NULL_TREE;
388 /* The "java.lang" import qualified name. */
389 static tree java_lang_id = NULL_TREE;
391 /* The generated `inst$' identifier used for generated enclosing
392 instance/field access functions. */
393 static tree inst_id = NULL_TREE;
395 /* The "java.lang.Cloneable" qualified name. */
396 static tree java_lang_cloneable = NULL_TREE;
398 /* The "java.io.Serializable" qualified name. */
399 static tree java_io_serializable = NULL_TREE;
401 /* Context and flag for static blocks */
402 static tree current_static_block = NULL_TREE;
404 /* The generated `write_parm_value$' identifier. */
407 /* The list of all packages we've seen so far */
408 static tree package_list = NULL_TREE;
410 /* Hold THIS for the scope of the current method decl. */
411 static tree current_this;
413 /* Hold a list of catch clauses list. The first element of this list is
414 the list of the catch clauses of the currently analysed try block. */
415 static tree currently_caught_type_list;
417 /* This holds a linked list of all the case labels for the current
418 switch statement. It is only used when checking to see if there
419 are duplicate labels. FIXME: probably this should just be attached
420 to the switch itself; then it could be referenced via
421 `ctxp->current_loop'. */
422 static tree case_label_list;
424 static tree src_parse_roots[1];
426 /* All classes seen from source code */
427 #define gclass_list src_parse_roots[0]
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
434 #define check_modifiers(__message, __value, __mask) do { \
435 if ((__value) & ~(__mask)) \
437 int i, remainder = (__value) & ~(__mask); \
438 for (i = 0; i <= 10; i++) \
439 if ((1 << i) & remainder) \
440 parse_error_context (ctxp->modifier_ctx [i], (__message), \
441 java_accstring_lookup (1 << i)); \
463 /* Things defined here have to match the order of what's in the
464 binop_lookup table. */
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
472 /* This maps to the same binop_lookup entry than the token above */
474 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
476 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
477 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
480 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
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
488 /* Keep those two in order, too */
489 %token DECR_TK INCR_TK
491 /* From now one, things can be in any order */
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
504 %token BYTE_TK SHORT_TK INT_TK LONG_TK
505 %token CHAR_TK INTEGRAL_TK
507 %token FLOAT_TK DOUBLE_TK FP_TK
511 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
513 %token ASSIGN_ANY_TK ASSIGN_TK
514 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
516 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
517 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
519 %type <value> modifiers MODIFIER_TK final synchronized
521 %type <node> super ID_TK identifier
522 %type <node> name simple_name qualified_name
523 %type <node> type_declaration compilation_unit
524 field_declaration method_declaration extends_interfaces
525 interfaces interface_type_list
526 class_member_declaration
527 import_declarations package_declaration
528 type_declarations interface_body
529 interface_member_declaration constant_declaration
530 interface_member_declarations interface_type
531 abstract_method_declaration interface_type_list
532 %type <node> class_body_declaration class_member_declaration
533 static_initializer constructor_declaration block
534 %type <node> class_body_declarations constructor_header
535 %type <node> class_or_interface_type class_type class_type_list
536 constructor_declarator explicit_constructor_invocation
537 %type <node> dim_expr dim_exprs this_or_super throws
539 %type <node> variable_declarator_id variable_declarator
540 variable_declarators variable_initializer
541 variable_initializers constructor_body
544 %type <node> class_body block_end constructor_block_end
545 %type <node> statement statement_without_trailing_substatement
546 labeled_statement if_then_statement label_decl
547 if_then_else_statement while_statement for_statement
548 statement_nsi labeled_statement_nsi do_statement
549 if_then_else_statement_nsi while_statement_nsi
550 for_statement_nsi statement_expression_list for_init
551 for_update statement_expression expression_statement
552 primary_no_new_array expression primary
553 array_creation_expression array_type
554 class_instance_creation_expression field_access
555 method_invocation array_access something_dot_new
556 argument_list postfix_expression while_expression
557 post_increment_expression post_decrement_expression
558 unary_expression_not_plus_minus unary_expression
559 pre_increment_expression pre_decrement_expression
560 unary_expression_not_plus_minus cast_expression
561 multiplicative_expression additive_expression
562 shift_expression relational_expression
563 equality_expression and_expression
564 exclusive_or_expression inclusive_or_expression
565 conditional_and_expression conditional_or_expression
566 conditional_expression assignment_expression
567 left_hand_side assignment for_header for_begin
568 constant_expression do_statement_begin empty_statement
569 switch_statement synchronized_statement throw_statement
570 try_statement switch_expression switch_block
571 catches catch_clause catch_clause_parameter finally
572 anonymous_class_creation trap_overflow_corner_case
573 %type <node> return_statement break_statement continue_statement
575 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
576 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
577 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
578 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
579 %type <operator> ASSIGN_ANY_TK assignment_operator
580 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
581 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
582 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
583 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
584 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
585 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
586 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
587 %type <operator> NEW_TK
589 %type <node> method_body
591 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
592 STRING_LIT_TK NULL_TK VOID_TK
594 %type <node> IF_TK WHILE_TK FOR_TK
596 %type <node> formal_parameter_list formal_parameter
597 method_declarator method_header
599 %type <node> primitive_type reference_type type
600 BOOLEAN_TK INTEGRAL_TK FP_TK
602 /* Added or modified JDK 1.1 rule types */
603 %type <node> type_literals
606 /* 19.2 Production from 2.3: The Syntactic Grammar */
609 /* Register static variables with the garbage
611 ggc_add_tree_root (&label_id, 1);
612 ggc_add_tree_root (&wfl_string_buffer, 1);
613 ggc_add_tree_root (&wfl_append, 1);
614 ggc_add_tree_root (&wfl_to_string, 1);
615 ggc_add_tree_root (&java_lang_id, 1);
616 ggc_add_tree_root (&inst_id, 1);
617 ggc_add_tree_root (&java_lang_cloneable, 1);
618 ggc_add_tree_root (&java_io_serializable, 1);
619 ggc_add_tree_root (¤t_static_block, 1);
620 ggc_add_tree_root (&wpv_id, 1);
621 ggc_add_tree_root (&package_list, 1);
622 ggc_add_tree_root (¤t_this, 1);
623 ggc_add_tree_root (¤tly_caught_type_list, 1);
624 ggc_add_tree_root (&case_label_list, 1);
625 ggc_add_root (&ctxp, 1,
626 sizeof (struct parser_ctxt *),
628 ggc_add_root (&ctxp_for_generation, 1,
629 sizeof (struct parser_ctxt *),
636 /* 19.3 Productions from 3: Lexical structure */
646 /* 19.4 Productions from 4: Types, Values and Variables */
659 class_or_interface_type
663 class_or_interface_type:
668 class_or_interface_type /* Default rule */
672 class_or_interface_type
678 int osb = pop_current_osb (ctxp);
679 tree t = build_java_array_type (($1), -1);
681 t = build_unresolved_array_type (t);
686 int osb = pop_current_osb (ctxp);
689 t = build_unresolved_array_type (t);
694 /* 19.5 Productions from 6: Names */
696 simple_name /* Default rule */
697 | qualified_name /* Default rule */
701 identifier /* Default rule */
705 name DOT_TK identifier
706 { $$ = make_qualified_name ($1, $3, $2.location); }
713 /* 19.6: Production from 7: Packages */
716 | package_declaration
717 | import_declarations
719 | package_declaration import_declarations
720 | package_declaration type_declarations
721 | import_declarations type_declarations
722 | package_declaration import_declarations type_declarations
730 | import_declarations import_declaration
738 | type_declarations type_declaration
742 PACKAGE_TK name SC_TK
744 ctxp->package = EXPR_WFL_NODE ($2);
745 register_package (ctxp->package);
748 {yyerror ("Missing name"); RECOVER;}
749 | PACKAGE_TK name error
750 {yyerror ("';' expected"); RECOVER;}
754 single_type_import_declaration
755 | type_import_on_demand_declaration
758 single_type_import_declaration:
761 tree name = EXPR_WFL_NODE ($2), last_name;
762 int i = IDENTIFIER_LENGTH (name)-1;
763 const char *last = &IDENTIFIER_POINTER (name)[i];
764 while (last != IDENTIFIER_POINTER (name))
770 last_name = get_identifier (++last);
771 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
773 tree err = find_name_in_single_imports (last_name);
774 if (err && err != name)
776 ($2, "Ambiguous class: `%s' and `%s'",
777 IDENTIFIER_POINTER (name),
778 IDENTIFIER_POINTER (err));
780 REGISTER_IMPORT ($2, last_name);
783 REGISTER_IMPORT ($2, last_name);
786 {yyerror ("Missing name"); RECOVER;}
787 | IMPORT_TK name error
788 {yyerror ("';' expected"); RECOVER;}
791 type_import_on_demand_declaration:
792 IMPORT_TK name DOT_TK MULT_TK SC_TK
794 tree name = EXPR_WFL_NODE ($2);
796 /* Search for duplicates. */
797 for (it = ctxp->import_demand_list; it; it = TREE_CHAIN (it))
798 if (EXPR_WFL_NODE (TREE_PURPOSE (it)) == name)
800 /* Don't import the same thing more than once, just ignore
801 duplicates (7.5.2) */
804 read_import_dir ($2);
805 ctxp->import_demand_list =
806 chainon (ctxp->import_demand_list,
807 build_tree_list ($2, NULL_TREE));
810 | IMPORT_TK name DOT_TK error
811 {yyerror ("'*' expected"); RECOVER;}
812 | IMPORT_TK name DOT_TK MULT_TK error
813 {yyerror ("';' expected"); RECOVER;}
818 { end_class_declaration (0); }
819 | interface_declaration
820 { end_class_declaration (0); }
825 yyerror ("Class or interface declaration expected");
829 /* 19.7 Shortened from the original:
830 modifiers: modifier | modifiers modifier
831 modifier: any of public... */
837 | modifiers MODIFIER_TK
842 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
843 java_accstring_lookup (acc));
851 /* 19.8.1 Production from $8.1: Class Declaration */
853 modifiers CLASS_TK identifier super interfaces
854 { create_class ($1, $3, $4, $5); }
856 | CLASS_TK identifier super interfaces
857 { create_class (0, $2, $3, $4); }
859 | modifiers CLASS_TK error
860 {yyerror ("Missing class name"); RECOVER;}
862 {yyerror ("Missing class name"); RECOVER;}
863 | CLASS_TK identifier error
865 if (!ctxp->class_err) yyerror ("'{' expected");
868 | modifiers CLASS_TK identifier error
869 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
874 | EXTENDS_TK class_type
876 | EXTENDS_TK class_type error
877 {yyerror ("'{' expected"); ctxp->class_err=1;}
879 {yyerror ("Missing super class name"); ctxp->class_err=1;}
884 | IMPLEMENTS_TK interface_type_list
886 | IMPLEMENTS_TK error
889 yyerror ("Missing interface name");
896 ctxp->interface_number = 1;
897 $$ = build_tree_list ($1, NULL_TREE);
899 | interface_type_list C_TK interface_type
901 ctxp->interface_number++;
902 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
904 | interface_type_list C_TK error
905 {yyerror ("Missing interface name"); RECOVER;}
911 /* Store the location of the `}' when doing xrefs */
913 DECL_END_SOURCE_LINE (GET_CPC ()) =
914 EXPR_WFL_ADD_COL ($2.location, 1);
917 | OCB_TK class_body_declarations CCB_TK
919 /* Store the location of the `}' when doing xrefs */
921 DECL_END_SOURCE_LINE (GET_CPC ()) =
922 EXPR_WFL_ADD_COL ($3.location, 1);
927 class_body_declarations:
928 class_body_declaration
929 | class_body_declarations class_body_declaration
932 class_body_declaration:
933 class_member_declaration
935 | constructor_declaration
936 | block /* Added, JDK1.1, instance initializer */
938 if ($1 != empty_stmt_node)
940 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
941 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
946 class_member_declaration:
949 | class_declaration /* Added, JDK1.1 inner classes */
950 { end_class_declaration (1); }
951 | interface_declaration /* Added, JDK1.1 inner interfaces */
952 { end_class_declaration (1); }
956 /* 19.8.2 Productions from 8.3: Field Declarations */
958 type variable_declarators SC_TK
959 { register_fields (0, $1, $2); }
960 | modifiers type variable_declarators SC_TK
963 ("Illegal modifier `%s' for field declaration",
964 $1, FIELD_MODIFIERS);
965 check_modifiers_consistency ($1);
966 register_fields ($1, $2, $3);
970 variable_declarators:
971 /* Should we use build_decl_list () instead ? FIXME */
972 variable_declarator /* Default rule */
973 | variable_declarators C_TK variable_declarator
974 { $$ = chainon ($1, $3); }
975 | variable_declarators C_TK error
976 {yyerror ("Missing term"); RECOVER;}
980 variable_declarator_id
981 { $$ = build_tree_list ($1, NULL_TREE); }
982 | variable_declarator_id ASSIGN_TK variable_initializer
984 if (java_error_count)
987 ($1, build_assignment ($2.token, $2.location, $1, $3));
989 | variable_declarator_id ASSIGN_TK error
991 yyerror ("Missing variable initializer");
992 $$ = build_tree_list ($1, NULL_TREE);
995 | variable_declarator_id ASSIGN_TK variable_initializer error
997 yyerror ("';' expected");
998 $$ = build_tree_list ($1, NULL_TREE);
1003 variable_declarator_id:
1005 | variable_declarator_id OSB_TK CSB_TK
1006 { $$ = build_unresolved_array_type ($1); }
1008 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
1009 | variable_declarator_id OSB_TK error
1011 tree node = java_lval.node;
1012 if (node && (TREE_CODE (node) == INTEGER_CST
1013 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
1014 yyerror ("Can't specify array dimension in a declaration");
1016 yyerror ("']' expected");
1019 | variable_declarator_id CSB_TK error
1020 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1023 variable_initializer:
1028 /* 19.8.3 Productions from 8.4: Method Declarations */
1032 current_function_decl = $1;
1033 if (current_function_decl
1034 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1035 source_start_java_method (current_function_decl);
1037 current_function_decl = NULL_TREE;
1040 { finish_method_declaration ($3); }
1041 | method_header error
1042 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1046 type method_declarator throws
1047 { $$ = method_header (0, $1, $2, $3); }
1048 | VOID_TK method_declarator throws
1049 { $$ = method_header (0, void_type_node, $2, $3); }
1050 | modifiers type method_declarator throws
1051 { $$ = method_header ($1, $2, $3, $4); }
1052 | modifiers VOID_TK method_declarator throws
1053 { $$ = method_header ($1, void_type_node, $3, $4); }
1056 yyerror ("Invalid method declaration, method name required");
1059 | modifiers type error
1062 {yyerror ("Identifier expected"); RECOVER;}
1063 | modifiers VOID_TK error
1064 {yyerror ("Identifier expected"); RECOVER;}
1067 yyerror ("Invalid method declaration, return type required");
1073 identifier OP_TK CP_TK
1075 ctxp->formal_parameter_number = 0;
1076 $$ = method_declarator ($1, NULL_TREE);
1078 | identifier OP_TK formal_parameter_list CP_TK
1079 { $$ = method_declarator ($1, $3); }
1080 | method_declarator OSB_TK CSB_TK
1082 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1084 build_unresolved_array_type (TREE_PURPOSE ($1));
1085 parse_warning_context
1087 "Discouraged form of returned type specification");
1089 | identifier OP_TK error
1090 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1091 | method_declarator OSB_TK error
1092 {yyerror ("']' expected"); RECOVER;}
1095 formal_parameter_list:
1098 ctxp->formal_parameter_number = 1;
1100 | formal_parameter_list C_TK formal_parameter
1102 ctxp->formal_parameter_number += 1;
1103 $$ = chainon ($1, $3);
1105 | formal_parameter_list C_TK error
1106 { yyerror ("Missing formal parameter term"); RECOVER; }
1110 type variable_declarator_id
1112 $$ = build_tree_list ($2, $1);
1114 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1116 $$ = build_tree_list ($3, $2);
1117 ARG_FINAL_P ($$) = 1;
1121 yyerror ("Missing identifier"); RECOVER;
1126 yyerror ("Missing identifier"); RECOVER;
1134 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1136 if ($1 != ACC_FINAL)
1137 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1143 | THROWS_TK class_type_list
1146 {yyerror ("Missing class type term"); RECOVER;}
1151 { $$ = build_tree_list ($1, $1); }
1152 | class_type_list C_TK class_type
1153 { $$ = tree_cons ($3, $3, $1); }
1154 | class_type_list C_TK error
1155 {yyerror ("Missing class type term"); RECOVER;}
1160 | SC_TK { $$ = NULL_TREE; }
1163 /* 19.8.4 Productions from 8.5: Static Initializers */
1167 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1168 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1169 current_static_block = NULL_TREE;
1173 static: /* Test lval.sub_token here */
1176 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1177 /* Can't have a static initializer in an innerclass */
1178 if ($1 | ACC_STATIC &&
1179 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1181 (MODIFIER_WFL (STATIC_TK),
1182 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1183 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1184 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1188 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1189 constructor_declaration:
1192 current_function_decl = $1;
1193 source_start_java_method (current_function_decl);
1196 { finish_method_declaration ($3); }
1200 constructor_declarator throws
1201 { $$ = method_header (0, NULL_TREE, $1, $2); }
1202 | modifiers constructor_declarator throws
1203 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1206 constructor_declarator:
1207 simple_name OP_TK CP_TK
1209 ctxp->formal_parameter_number = 0;
1210 $$ = method_declarator ($1, NULL_TREE);
1212 | simple_name OP_TK formal_parameter_list CP_TK
1213 { $$ = method_declarator ($1, $3); }
1217 /* Unlike regular method, we always need a complete (empty)
1218 body so we can safely perform all the required code
1219 addition (super invocation and field initialization) */
1220 block_begin constructor_block_end
1222 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1225 | block_begin explicit_constructor_invocation constructor_block_end
1227 | block_begin block_statements constructor_block_end
1229 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1233 constructor_block_end:
1237 /* Error recovery for that rule moved down expression_statement: rule. */
1238 explicit_constructor_invocation:
1239 this_or_super OP_TK CP_TK SC_TK
1241 $$ = build_method_invocation ($1, NULL_TREE);
1242 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1243 $$ = java_method_add_stmt (current_function_decl, $$);
1245 | this_or_super OP_TK argument_list CP_TK SC_TK
1247 $$ = build_method_invocation ($1, $3);
1248 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1249 $$ = java_method_add_stmt (current_function_decl, $$);
1251 /* Added, JDK1.1 inner classes. Modified because the rule
1252 'primary' couldn't work. */
1253 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1254 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1255 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1256 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1259 this_or_super: /* Added, simplifies error diagnostics */
1262 tree wfl = build_wfl_node (this_identifier_node);
1263 EXPR_WFL_LINECOL (wfl) = $1.location;
1268 tree wfl = build_wfl_node (super_identifier_node);
1269 EXPR_WFL_LINECOL (wfl) = $1.location;
1274 /* 19.9 Productions from 9: Interfaces */
1275 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1276 interface_declaration:
1277 INTERFACE_TK identifier
1278 { create_interface (0, $2, NULL_TREE); }
1280 | modifiers INTERFACE_TK identifier
1281 { create_interface ($1, $3, NULL_TREE); }
1283 | INTERFACE_TK identifier extends_interfaces
1284 { create_interface (0, $2, $3); }
1286 | modifiers INTERFACE_TK identifier extends_interfaces
1287 { create_interface ($1, $3, $4); }
1289 | INTERFACE_TK identifier error
1290 {yyerror ("'{' expected"); RECOVER;}
1291 | modifiers INTERFACE_TK identifier error
1292 {yyerror ("'{' expected"); RECOVER;}
1296 EXTENDS_TK interface_type
1298 ctxp->interface_number = 1;
1299 $$ = build_tree_list ($2, NULL_TREE);
1301 | extends_interfaces C_TK interface_type
1303 ctxp->interface_number++;
1304 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1307 {yyerror ("Invalid interface type"); RECOVER;}
1308 | extends_interfaces C_TK error
1309 {yyerror ("Missing term"); RECOVER;}
1315 | OCB_TK interface_member_declarations CCB_TK
1319 interface_member_declarations:
1320 interface_member_declaration
1321 | interface_member_declarations interface_member_declaration
1324 interface_member_declaration:
1325 constant_declaration
1326 | abstract_method_declaration
1327 | class_declaration /* Added, JDK1.1 inner classes */
1328 { end_class_declaration (1); }
1329 | interface_declaration /* Added, JDK1.1 inner interfaces */
1330 { end_class_declaration (1); }
1333 constant_declaration:
1337 abstract_method_declaration:
1340 check_abstract_method_header ($1);
1341 current_function_decl = NULL_TREE; /* FIXME ? */
1343 | method_header error
1344 {yyerror ("';' expected"); RECOVER;}
1347 /* 19.10 Productions from 10: Arrays */
1350 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1351 | OCB_TK C_TK CCB_TK
1352 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1353 | OCB_TK variable_initializers CCB_TK
1354 { $$ = build_new_array_init ($1.location, $2); }
1355 | OCB_TK variable_initializers C_TK CCB_TK
1356 { $$ = build_new_array_init ($1.location, $2); }
1359 variable_initializers:
1360 variable_initializer
1362 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1365 | variable_initializers C_TK variable_initializer
1367 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1369 | variable_initializers C_TK error
1370 {yyerror ("Missing term"); RECOVER;}
1373 /* 19.11 Production from 14: Blocks and Statements */
1377 /* Store the location of the `}' when doing xrefs */
1378 if (current_function_decl && flag_emit_xref)
1379 DECL_END_SOURCE_LINE (current_function_decl) =
1380 EXPR_WFL_ADD_COL ($2.location, 1);
1381 $$ = empty_stmt_node;
1383 | block_begin block_statements block_end
1395 maybe_absorb_scoping_blocks ();
1396 /* Store the location of the `}' when doing xrefs */
1397 if (current_function_decl && flag_emit_xref)
1398 DECL_END_SOURCE_LINE (current_function_decl) =
1399 EXPR_WFL_ADD_COL ($1.location, 1);
1401 if (!BLOCK_SUBBLOCKS ($$))
1402 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1408 | block_statements block_statement
1412 local_variable_declaration_statement
1414 { java_method_add_stmt (current_function_decl, $1); }
1415 | class_declaration /* Added, JDK1.1 local classes */
1417 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1418 end_class_declaration (1);
1422 local_variable_declaration_statement:
1423 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1426 local_variable_declaration:
1427 type variable_declarators
1428 { declare_local_variables (0, $1, $2); }
1429 | final type variable_declarators /* Added, JDK1.1 final locals */
1430 { declare_local_variables ($1, $2, $3); }
1434 statement_without_trailing_substatement
1437 | if_then_else_statement
1440 { $$ = exit_block (); }
1444 statement_without_trailing_substatement
1445 | labeled_statement_nsi
1446 | if_then_else_statement_nsi
1447 | while_statement_nsi
1449 { $$ = exit_block (); }
1452 statement_without_trailing_substatement:
1455 | expression_statement
1459 | continue_statement
1461 | synchronized_statement
1469 if (flag_extraneous_semicolon
1470 && ! current_static_block
1471 && (! current_function_decl ||
1472 /* Verify we're not in a inner class declaration */
1473 (GET_CPC () != TYPE_NAME
1474 (DECL_CONTEXT (current_function_decl)))))
1477 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1478 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1480 $$ = empty_stmt_node;
1485 identifier REL_CL_TK
1487 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1488 EXPR_WFL_NODE ($1));
1490 push_labeled_block ($$);
1491 PUSH_LABELED_BLOCK ($$);
1496 label_decl statement
1497 { $$ = finish_labeled_statement ($1, $2); }
1499 {yyerror ("':' expected"); RECOVER;}
1502 labeled_statement_nsi:
1503 label_decl statement_nsi
1504 { $$ = finish_labeled_statement ($1, $2); }
1507 /* We concentrate here a bunch of error handling rules that we couldn't write
1508 earlier, because expression_statement catches a missing ';'. */
1509 expression_statement:
1510 statement_expression SC_TK
1512 /* We have a statement. Generate a WFL around it so
1514 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1515 /* We know we have a statement, so set the debug
1516 info to be eventually generate here. */
1517 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1521 YYNOT_TWICE yyerror ("Invalid expression statement");
1522 DRECOVER (expr_stmt);
1526 YYNOT_TWICE yyerror ("Invalid expression statement");
1527 DRECOVER (expr_stmt);
1531 YYNOT_TWICE yyerror ("Invalid expression statement");
1532 DRECOVER (expr_stmt);
1534 | this_or_super OP_TK error
1535 {yyerror ("')' expected"); RECOVER;}
1536 | this_or_super OP_TK CP_TK error
1538 parse_ctor_invocation_error ();
1541 | this_or_super OP_TK argument_list error
1542 {yyerror ("')' expected"); RECOVER;}
1543 | this_or_super OP_TK argument_list CP_TK error
1545 parse_ctor_invocation_error ();
1548 | name DOT_TK SUPER_TK error
1549 {yyerror ("'(' expected"); RECOVER;}
1550 | name DOT_TK SUPER_TK OP_TK error
1551 {yyerror ("')' expected"); RECOVER;}
1552 | name DOT_TK SUPER_TK OP_TK argument_list error
1553 {yyerror ("')' expected"); RECOVER;}
1554 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1555 {yyerror ("';' expected"); RECOVER;}
1556 | name DOT_TK SUPER_TK OP_TK CP_TK error
1557 {yyerror ("';' expected"); RECOVER;}
1560 statement_expression:
1562 | pre_increment_expression
1563 | pre_decrement_expression
1564 | post_increment_expression
1565 | post_decrement_expression
1567 | class_instance_creation_expression
1571 IF_TK OP_TK expression CP_TK statement
1573 $$ = build_if_else_statement ($2.location, $3,
1577 {yyerror ("'(' expected"); RECOVER;}
1579 {yyerror ("Missing term"); RECOVER;}
1580 | IF_TK OP_TK expression error
1581 {yyerror ("')' expected"); RECOVER;}
1584 if_then_else_statement:
1585 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1586 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1589 if_then_else_statement_nsi:
1590 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1591 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1601 /* Make into "proper list" of COMPOUND_EXPRs.
1602 I.e. make the last statement also have its own
1604 maybe_absorb_scoping_blocks ();
1605 TREE_OPERAND ($1, 1) = exit_block ();
1606 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1611 SWITCH_TK OP_TK expression CP_TK
1613 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1614 EXPR_WFL_LINECOL ($$) = $2.location;
1617 {yyerror ("'(' expected"); RECOVER;}
1618 | SWITCH_TK OP_TK error
1619 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1620 | SWITCH_TK OP_TK expression CP_TK error
1621 {yyerror ("'{' expected"); RECOVER;}
1624 /* Default assignment is there to avoid type node on switch_block
1630 | OCB_TK switch_labels CCB_TK
1632 | OCB_TK switch_block_statement_groups CCB_TK
1634 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1638 switch_block_statement_groups:
1639 switch_block_statement_group
1640 | switch_block_statement_groups switch_block_statement_group
1643 switch_block_statement_group:
1644 switch_labels block_statements
1649 | switch_labels switch_label
1653 CASE_TK constant_expression REL_CL_TK
1655 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1656 EXPR_WFL_LINECOL (lab) = $1.location;
1657 java_method_add_stmt (current_function_decl, lab);
1659 | DEFAULT_TK REL_CL_TK
1661 tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1662 EXPR_WFL_LINECOL (lab) = $1.location;
1663 java_method_add_stmt (current_function_decl, lab);
1666 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1667 | CASE_TK constant_expression error
1668 {yyerror ("':' expected"); RECOVER;}
1670 {yyerror ("':' expected"); RECOVER;}
1674 WHILE_TK OP_TK expression CP_TK
1676 tree body = build_loop_body ($2.location, $3, 0);
1677 $$ = build_new_loop (body);
1682 while_expression statement
1683 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1685 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1686 | WHILE_TK OP_TK error
1687 {yyerror ("Missing term and ')' expected"); RECOVER;}
1688 | WHILE_TK OP_TK expression error
1689 {yyerror ("')' expected"); RECOVER;}
1692 while_statement_nsi:
1693 while_expression statement_nsi
1694 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1700 tree body = build_loop_body (0, NULL_TREE, 1);
1701 $$ = build_new_loop (body);
1703 /* Need error handing here. FIXME */
1707 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1708 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1712 for_begin SC_TK expression SC_TK for_update CP_TK statement
1714 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1715 $3 = build_wfl_node ($3);
1716 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1718 | for_begin SC_TK SC_TK for_update CP_TK statement
1720 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1721 /* We have not condition, so we get rid of the EXIT_EXPR */
1722 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1725 | for_begin SC_TK error
1726 {yyerror ("Invalid control expression"); RECOVER;}
1727 | for_begin SC_TK expression SC_TK error
1728 {yyerror ("Invalid update expression"); RECOVER;}
1729 | for_begin SC_TK SC_TK error
1730 {yyerror ("Invalid update expression"); RECOVER;}
1734 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1735 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1736 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1738 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1739 /* We have not condition, so we get rid of the EXIT_EXPR */
1740 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1748 /* This scope defined for local variable that may be
1749 defined within the scope of the for loop */
1753 {yyerror ("'(' expected"); DRECOVER(for_1);}
1754 | FOR_TK OP_TK error
1755 {yyerror ("Invalid init statement"); RECOVER;}
1761 /* We now declare the loop body. The loop is
1762 declared as a for loop. */
1763 tree body = build_loop_body (0, NULL_TREE, 0);
1764 $$ = build_new_loop (body);
1765 FOR_LOOP_P ($$) = 1;
1766 /* The loop is added to the current block the for
1767 statement is defined within */
1768 java_method_add_stmt (current_function_decl, $$);
1771 for_init: /* Can be empty */
1772 { $$ = empty_stmt_node; }
1773 | statement_expression_list
1775 /* Init statement recorded within the previously
1776 defined block scope */
1777 $$ = java_method_add_stmt (current_function_decl, $1);
1779 | local_variable_declaration
1781 /* Local variable are recorded within the previously
1782 defined block scope */
1785 | statement_expression_list error
1786 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1789 for_update: /* Can be empty */
1790 {$$ = empty_stmt_node;}
1791 | statement_expression_list
1792 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1795 statement_expression_list:
1796 statement_expression
1797 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1798 | statement_expression_list C_TK statement_expression
1799 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1800 | statement_expression_list C_TK error
1801 {yyerror ("Missing term"); RECOVER;}
1806 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1807 | BREAK_TK identifier SC_TK
1808 { $$ = build_bc_statement ($1.location, 1, $2); }
1810 {yyerror ("Missing term"); RECOVER;}
1811 | BREAK_TK identifier error
1812 {yyerror ("';' expected"); RECOVER;}
1817 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1818 | CONTINUE_TK identifier SC_TK
1819 { $$ = build_bc_statement ($1.location, 0, $2); }
1821 {yyerror ("Missing term"); RECOVER;}
1822 | CONTINUE_TK identifier error
1823 {yyerror ("';' expected"); RECOVER;}
1828 { $$ = build_return ($1.location, NULL_TREE); }
1829 | RETURN_TK expression SC_TK
1830 { $$ = build_return ($1.location, $2); }
1832 {yyerror ("Missing term"); RECOVER;}
1833 | RETURN_TK expression error
1834 {yyerror ("';' expected"); RECOVER;}
1838 THROW_TK expression SC_TK
1840 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1841 EXPR_WFL_LINECOL ($$) = $1.location;
1844 {yyerror ("Missing term"); RECOVER;}
1845 | THROW_TK expression error
1846 {yyerror ("';' expected"); RECOVER;}
1849 synchronized_statement:
1850 synchronized OP_TK expression CP_TK block
1852 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1853 EXPR_WFL_LINECOL ($$) =
1854 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1856 | synchronized OP_TK expression CP_TK error
1857 {yyerror ("'{' expected"); RECOVER;}
1858 | synchronized error
1859 {yyerror ("'(' expected"); RECOVER;}
1860 | synchronized OP_TK error CP_TK
1861 {yyerror ("Missing term"); RECOVER;}
1862 | synchronized OP_TK error
1863 {yyerror ("Missing term"); RECOVER;}
1870 "Illegal modifier `%s'. Only `synchronized' was expected here",
1871 $1, ACC_SYNCHRONIZED);
1872 if ($1 != ACC_SYNCHRONIZED)
1873 MODIFIER_WFL (SYNCHRONIZED_TK) =
1874 build_wfl_node (NULL_TREE);
1879 TRY_TK block catches
1880 { $$ = build_try_statement ($1.location, $2, $3); }
1881 | TRY_TK block finally
1882 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1883 | TRY_TK block catches finally
1884 { $$ = build_try_finally_statement
1885 ($1.location, build_try_statement ($1.location,
1889 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1894 | catches catch_clause
1896 TREE_CHAIN ($2) = $1;
1902 catch_clause_parameter block
1904 java_method_add_stmt (current_function_decl, $2);
1909 catch_clause_parameter:
1910 CATCH_TK OP_TK formal_parameter CP_TK
1912 /* We add a block to define a scope for
1913 formal_parameter (CCBP). The formal parameter is
1914 declared initialized by the appropriate function
1916 tree ccpb = enter_block ();
1917 tree init = build_assignment
1918 (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1919 build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1920 declare_local_variables (0, TREE_VALUE ($3),
1921 build_tree_list (TREE_PURPOSE ($3),
1923 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1924 EXPR_WFL_LINECOL ($$) = $1.location;
1927 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1928 | CATCH_TK OP_TK error
1930 yyerror ("Missing term or ')' expected");
1931 RECOVER; $$ = NULL_TREE;
1933 | CATCH_TK OP_TK error CP_TK /* That's for () */
1934 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1941 {yyerror ("'{' expected"); RECOVER; }
1944 /* 19.12 Production from 15: Expressions */
1946 primary_no_new_array
1947 | array_creation_expression
1950 primary_no_new_array:
1953 { $$ = build_this ($1.location); }
1954 | OP_TK expression CP_TK
1956 | class_instance_creation_expression
1961 /* Added, JDK1.1 inner classes. Documentation is wrong
1962 refering to a 'ClassName' (class_name) rule that doesn't
1963 exist. Used name: instead. */
1964 | name DOT_TK THIS_TK
1966 tree wfl = build_wfl_node (this_identifier_node);
1967 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1969 | OP_TK expression error
1970 {yyerror ("')' expected"); RECOVER;}
1972 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1973 | primitive_type DOT_TK error
1974 {yyerror ("'class' expected" ); RECOVER;}
1975 | VOID_TK DOT_TK error
1976 {yyerror ("'class' expected" ); RECOVER;}
1980 name DOT_TK CLASS_TK
1981 { $$ = build_incomplete_class_ref ($2.location, $1); }
1982 | array_type DOT_TK CLASS_TK
1983 { $$ = build_incomplete_class_ref ($2.location, $1); }
1984 | primitive_type DOT_TK CLASS_TK
1985 { $$ = build_incomplete_class_ref ($2.location, $1); }
1986 | VOID_TK DOT_TK CLASS_TK
1988 $$ = build_incomplete_class_ref ($2.location,
1993 class_instance_creation_expression:
1994 NEW_TK class_type OP_TK argument_list CP_TK
1995 { $$ = build_new_invocation ($2, $4); }
1996 | NEW_TK class_type OP_TK CP_TK
1997 { $$ = build_new_invocation ($2, NULL_TREE); }
1998 | anonymous_class_creation
1999 /* Added, JDK1.1 inner classes, modified to use name or
2000 primary instead of primary solely which couldn't work in
2002 | something_dot_new identifier OP_TK CP_TK
2004 tree ctor = build_new_invocation ($2, NULL_TREE);
2005 $$ = make_qualified_primary ($1, ctor,
2006 EXPR_WFL_LINECOL ($1));
2008 | something_dot_new identifier OP_TK CP_TK class_body
2009 | something_dot_new identifier OP_TK argument_list CP_TK
2011 tree ctor = build_new_invocation ($2, $4);
2012 $$ = make_qualified_primary ($1, ctor,
2013 EXPR_WFL_LINECOL ($1));
2015 | something_dot_new identifier OP_TK argument_list CP_TK class_body
2016 | NEW_TK error SC_TK
2017 {yyerror ("'(' expected"); DRECOVER(new_1);}
2018 | NEW_TK class_type error
2019 {yyerror ("'(' expected"); RECOVER;}
2020 | NEW_TK class_type OP_TK error
2021 {yyerror ("')' or term expected"); RECOVER;}
2022 | NEW_TK class_type OP_TK argument_list error
2023 {yyerror ("')' expected"); RECOVER;}
2024 | something_dot_new error
2025 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
2026 | something_dot_new identifier error
2027 {yyerror ("'(' expected"); RECOVER;}
2030 /* Created after JDK1.1 rules originally added to
2031 class_instance_creation_expression, but modified to use
2032 'class_type' instead of 'TypeName' (type_name) which is mentionned
2033 in the documentation but doesn't exist. */
2035 anonymous_class_creation:
2036 NEW_TK class_type OP_TK argument_list CP_TK
2037 { create_anonymous_class ($1.location, $2); }
2040 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2041 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2043 end_class_declaration (1);
2045 /* Now we can craft the new expression */
2046 $$ = build_new_invocation (id, $4);
2048 /* Note that we can't possibly be here if
2049 `class_type' is an interface (in which case the
2050 anonymous class extends Object and implements
2051 `class_type', hence its constructor can't have
2054 /* Otherwise, the innerclass must feature a
2055 constructor matching `argument_list'. Anonymous
2056 classes are a bit special: it's impossible to
2057 define constructor for them, hence constructors
2058 must be generated following the hints provided by
2059 the `new' expression. Whether a super constructor
2060 of that nature exists or not is to be verified
2061 later on in verify_constructor_super.
2063 It's during the expansion of a `new' statement
2064 refering to an anonymous class that a ctor will
2065 be generated for the anonymous class, with the
2069 | NEW_TK class_type OP_TK CP_TK
2070 { create_anonymous_class ($1.location, $2); }
2073 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2074 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2076 end_class_declaration (1);
2078 /* Now we can craft the new expression. The
2079 statement doesn't need to be remember so that a
2080 constructor can be generated, since its signature
2081 is already known. */
2082 $$ = build_new_invocation (id, NULL_TREE);
2086 something_dot_new: /* Added, not part of the specs. */
2089 | primary DOT_TK NEW_TK
2096 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2097 ctxp->formal_parameter_number = 1;
2099 | argument_list C_TK expression
2101 ctxp->formal_parameter_number += 1;
2102 $$ = tree_cons (NULL_TREE, $3, $1);
2104 | argument_list C_TK error
2105 {yyerror ("Missing term"); RECOVER;}
2108 array_creation_expression:
2109 NEW_TK primitive_type dim_exprs
2110 { $$ = build_newarray_node ($2, $3, 0); }
2111 | NEW_TK class_or_interface_type dim_exprs
2112 { $$ = build_newarray_node ($2, $3, 0); }
2113 | NEW_TK primitive_type dim_exprs dims
2114 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2115 | NEW_TK class_or_interface_type dim_exprs dims
2116 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2117 /* Added, JDK1.1 anonymous array. Initial documentation rule
2119 | NEW_TK class_or_interface_type dims array_initializer
2122 int osb = pop_current_osb (ctxp);
2124 obstack_grow (&temporary_obstack, "[]", 2);
2125 obstack_1grow (&temporary_obstack, '\0');
2126 sig = obstack_finish (&temporary_obstack);
2127 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2128 $2, get_identifier (sig), $4);
2130 | NEW_TK primitive_type dims array_initializer
2132 int osb = pop_current_osb (ctxp);
2135 type = build_java_array_type (type, -1);
2136 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2137 build_pointer_type (type), NULL_TREE, $4);
2139 | NEW_TK error CSB_TK
2140 {yyerror ("'[' expected"); DRECOVER ("]");}
2141 | NEW_TK error OSB_TK
2142 {yyerror ("']' expected"); RECOVER;}
2147 { $$ = build_tree_list (NULL_TREE, $1); }
2148 | dim_exprs dim_expr
2149 { $$ = tree_cons (NULL_TREE, $2, $$); }
2153 OSB_TK expression CSB_TK
2155 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2157 $2 = build_wfl_node ($2);
2158 TREE_TYPE ($2) = NULL_TREE;
2160 EXPR_WFL_LINECOL ($2) = $1.location;
2163 | OSB_TK expression error
2164 {yyerror ("']' expected"); RECOVER;}
2167 yyerror ("Missing term");
2168 yyerror ("']' expected");
2177 /* If not initialized, allocate memory for the osb
2179 if (!ctxp->osb_limit)
2181 allocate = ctxp->osb_limit = 32;
2182 ctxp->osb_depth = -1;
2184 /* If capacity overflown, reallocate a bigger chunk */
2185 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2186 allocate = ctxp->osb_limit << 1;
2190 allocate *= sizeof (int);
2191 if (ctxp->osb_number)
2192 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2195 ctxp->osb_number = (int *)xmalloc (allocate);
2198 CURRENT_OSB (ctxp) = 1;
2200 | dims OSB_TK CSB_TK
2201 { CURRENT_OSB (ctxp)++; }
2203 { yyerror ("']' expected"); RECOVER;}
2207 primary DOT_TK identifier
2208 { $$ = make_qualified_primary ($1, $3, $2.location); }
2209 /* FIXME - REWRITE TO:
2210 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2211 | SUPER_TK DOT_TK identifier
2213 tree super_wfl = build_wfl_node (super_identifier_node);
2214 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2215 $$ = make_qualified_name (super_wfl, $3, $2.location);
2218 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2223 { $$ = build_method_invocation ($1, NULL_TREE); }
2224 | name OP_TK argument_list CP_TK
2225 { $$ = build_method_invocation ($1, $3); }
2226 | primary DOT_TK identifier OP_TK CP_TK
2228 if (TREE_CODE ($1) == THIS_EXPR)
2229 $$ = build_this_super_qualified_invocation
2230 (1, $3, NULL_TREE, 0, $2.location);
2233 tree invok = build_method_invocation ($3, NULL_TREE);
2234 $$ = make_qualified_primary ($1, invok, $2.location);
2237 | primary DOT_TK identifier OP_TK argument_list CP_TK
2239 if (TREE_CODE ($1) == THIS_EXPR)
2240 $$ = build_this_super_qualified_invocation
2241 (1, $3, $5, 0, $2.location);
2244 tree invok = build_method_invocation ($3, $5);
2245 $$ = make_qualified_primary ($1, invok, $2.location);
2248 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2250 $$ = build_this_super_qualified_invocation
2251 (0, $3, NULL_TREE, $1.location, $2.location);
2253 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2255 $$ = build_this_super_qualified_invocation
2256 (0, $3, $5, $1.location, $2.location);
2258 /* Screws up thing. I let it here until I'm convinced it can
2260 | primary DOT_TK error
2261 {yyerror ("'(' expected"); DRECOVER(bad);} */
2262 | SUPER_TK DOT_TK error CP_TK
2263 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2264 | SUPER_TK DOT_TK error DOT_TK
2265 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2269 name OSB_TK expression CSB_TK
2270 { $$ = build_array_ref ($2.location, $1, $3); }
2271 | primary_no_new_array OSB_TK expression CSB_TK
2272 { $$ = build_array_ref ($2.location, $1, $3); }
2275 yyerror ("Missing term and ']' expected");
2276 DRECOVER(array_access);
2278 | name OSB_TK expression error
2280 yyerror ("']' expected");
2281 DRECOVER(array_access);
2283 | primary_no_new_array OSB_TK error
2285 yyerror ("Missing term and ']' expected");
2286 DRECOVER(array_access);
2288 | primary_no_new_array OSB_TK expression error
2290 yyerror ("']' expected");
2291 DRECOVER(array_access);
2298 | post_increment_expression
2299 | post_decrement_expression
2302 post_increment_expression:
2303 postfix_expression INCR_TK
2304 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2307 post_decrement_expression:
2308 postfix_expression DECR_TK
2309 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2312 trap_overflow_corner_case:
2313 pre_increment_expression
2314 | pre_decrement_expression
2315 | PLUS_TK unary_expression
2316 {$$ = build_unaryop ($1.token, $1.location, $2); }
2317 | unary_expression_not_plus_minus
2319 {yyerror ("Missing term"); RECOVER}
2323 trap_overflow_corner_case
2325 error_if_numeric_overflow ($1);
2328 | MINUS_TK trap_overflow_corner_case
2329 {$$ = build_unaryop ($1.token, $1.location, $2); }
2331 {yyerror ("Missing term"); RECOVER}
2334 pre_increment_expression:
2335 INCR_TK unary_expression
2336 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2338 {yyerror ("Missing term"); RECOVER}
2341 pre_decrement_expression:
2342 DECR_TK unary_expression
2343 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2345 {yyerror ("Missing term"); RECOVER}
2348 unary_expression_not_plus_minus:
2350 | NOT_TK unary_expression
2351 {$$ = build_unaryop ($1.token, $1.location, $2); }
2352 | NEG_TK unary_expression
2353 {$$ = build_unaryop ($1.token, $1.location, $2); }
2356 {yyerror ("Missing term"); RECOVER}
2358 {yyerror ("Missing term"); RECOVER}
2361 cast_expression: /* Error handling here is potentially weak */
2362 OP_TK primitive_type dims CP_TK unary_expression
2365 int osb = pop_current_osb (ctxp);
2367 type = build_java_array_type (type, -1);
2368 $$ = build_cast ($1.location, type, $5);
2370 | OP_TK primitive_type CP_TK unary_expression
2371 { $$ = build_cast ($1.location, $2, $4); }
2372 | OP_TK expression CP_TK unary_expression_not_plus_minus
2373 { $$ = build_cast ($1.location, $2, $4); }
2374 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2377 int osb = pop_current_osb (ctxp);
2378 obstack_grow (&temporary_obstack,
2379 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2380 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2382 obstack_grow (&temporary_obstack, "[]", 2);
2383 obstack_1grow (&temporary_obstack, '\0');
2384 ptr = obstack_finish (&temporary_obstack);
2385 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2386 $$ = build_cast ($1.location, $2, $5);
2388 | OP_TK primitive_type OSB_TK error
2389 {yyerror ("']' expected, invalid type expression");}
2392 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2395 | OP_TK primitive_type dims CP_TK error
2396 {yyerror ("Missing term"); RECOVER;}
2397 | OP_TK primitive_type CP_TK error
2398 {yyerror ("Missing term"); RECOVER;}
2399 | OP_TK name dims CP_TK error
2400 {yyerror ("Missing term"); RECOVER;}
2403 multiplicative_expression:
2405 | multiplicative_expression MULT_TK unary_expression
2407 $$ = build_binop (BINOP_LOOKUP ($2.token),
2408 $2.location, $1, $3);
2410 | multiplicative_expression DIV_TK unary_expression
2412 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2415 | multiplicative_expression REM_TK unary_expression
2417 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2420 | multiplicative_expression MULT_TK error
2421 {yyerror ("Missing term"); RECOVER;}
2422 | multiplicative_expression DIV_TK error
2423 {yyerror ("Missing term"); RECOVER;}
2424 | multiplicative_expression REM_TK error
2425 {yyerror ("Missing term"); RECOVER;}
2428 additive_expression:
2429 multiplicative_expression
2430 | additive_expression PLUS_TK multiplicative_expression
2432 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2435 | additive_expression MINUS_TK multiplicative_expression
2437 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2440 | additive_expression PLUS_TK error
2441 {yyerror ("Missing term"); RECOVER;}
2442 | additive_expression MINUS_TK error
2443 {yyerror ("Missing term"); RECOVER;}
2448 | shift_expression LS_TK additive_expression
2450 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2453 | shift_expression SRS_TK additive_expression
2455 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2458 | shift_expression ZRS_TK additive_expression
2460 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2463 | shift_expression LS_TK error
2464 {yyerror ("Missing term"); RECOVER;}
2465 | shift_expression SRS_TK error
2466 {yyerror ("Missing term"); RECOVER;}
2467 | shift_expression ZRS_TK error
2468 {yyerror ("Missing term"); RECOVER;}
2471 relational_expression:
2473 | relational_expression LT_TK shift_expression
2475 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2478 | relational_expression GT_TK shift_expression
2480 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2483 | relational_expression LTE_TK shift_expression
2485 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2488 | relational_expression GTE_TK shift_expression
2490 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2493 | relational_expression INSTANCEOF_TK reference_type
2494 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2495 | relational_expression LT_TK error
2496 {yyerror ("Missing term"); RECOVER;}
2497 | relational_expression GT_TK error
2498 {yyerror ("Missing term"); RECOVER;}
2499 | relational_expression LTE_TK error
2500 {yyerror ("Missing term"); RECOVER;}
2501 | relational_expression GTE_TK error
2502 {yyerror ("Missing term"); RECOVER;}
2503 | relational_expression INSTANCEOF_TK error
2504 {yyerror ("Invalid reference type"); RECOVER;}
2507 equality_expression:
2508 relational_expression
2509 | equality_expression EQ_TK relational_expression
2511 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2514 | equality_expression NEQ_TK relational_expression
2516 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2519 | equality_expression EQ_TK error
2520 {yyerror ("Missing term"); RECOVER;}
2521 | equality_expression NEQ_TK error
2522 {yyerror ("Missing term"); RECOVER;}
2527 | and_expression AND_TK equality_expression
2529 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2532 | and_expression AND_TK error
2533 {yyerror ("Missing term"); RECOVER;}
2536 exclusive_or_expression:
2538 | exclusive_or_expression XOR_TK and_expression
2540 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2543 | exclusive_or_expression XOR_TK error
2544 {yyerror ("Missing term"); RECOVER;}
2547 inclusive_or_expression:
2548 exclusive_or_expression
2549 | inclusive_or_expression OR_TK exclusive_or_expression
2551 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2554 | inclusive_or_expression OR_TK error
2555 {yyerror ("Missing term"); RECOVER;}
2558 conditional_and_expression:
2559 inclusive_or_expression
2560 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2562 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2565 | conditional_and_expression BOOL_AND_TK error
2566 {yyerror ("Missing term"); RECOVER;}
2569 conditional_or_expression:
2570 conditional_and_expression
2571 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2573 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2576 | conditional_or_expression BOOL_OR_TK error
2577 {yyerror ("Missing term"); RECOVER;}
2580 conditional_expression: /* Error handling here is weak */
2581 conditional_or_expression
2582 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2584 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2585 EXPR_WFL_LINECOL ($$) = $2.location;
2587 | conditional_or_expression REL_QM_TK REL_CL_TK error
2590 yyerror ("Missing term");
2593 | conditional_or_expression REL_QM_TK error
2594 {yyerror ("Missing term"); DRECOVER (2);}
2595 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2596 {yyerror ("Missing term"); DRECOVER (3);}
2599 assignment_expression:
2600 conditional_expression
2605 left_hand_side assignment_operator assignment_expression
2606 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2607 | left_hand_side assignment_operator error
2609 YYNOT_TWICE yyerror ("Missing term");
2620 assignment_operator:
2626 assignment_expression
2629 constant_expression:
2635 /* Helper function to retrieve an OSB count. Should be used when the
2636 `dims:' rule is being used. */
2639 pop_current_osb (ctxp)
2640 struct parser_ctxt *ctxp;
2644 if (ctxp->osb_depth < 0)
2647 to_return = CURRENT_OSB (ctxp);
2655 /* This section of the code deal with save/restoring parser contexts.
2656 Add mode documentation here. FIXME */
2658 /* Helper function. Create a new parser context. With
2659 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2660 context is copied, otherwise, the new context is zeroed. The newly
2661 created context becomes the current one. */
2664 create_new_parser_context (copy_from_previous)
2665 int copy_from_previous;
2667 struct parser_ctxt *new;
2669 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2670 if (copy_from_previous)
2672 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2673 new->saved_data_ctx = 1;
2676 memset ((PTR) new, 0, sizeof (struct parser_ctxt));
2682 /* Create a new parser context and make it the current one. */
2685 java_push_parser_context ()
2687 create_new_parser_context (0);
2691 java_pop_parser_context (generate)
2695 struct parser_ctxt *toFree, *next;
2704 lineno = ctxp->lineno;
2705 current_class = ctxp->class_type;
2708 /* If the old and new lexers differ, then free the old one. */
2709 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2710 java_destroy_lexer (ctxp->lexer);
2712 /* Set the single import class file flag to 0 for the current list
2713 of imported things */
2714 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2715 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2717 /* And restore those of the previous context */
2718 if ((ctxp = next)) /* Assignment is really meant here */
2719 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2720 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2722 /* If we pushed a context to parse a class intended to be generated,
2723 we keep it so we can remember the class. What we could actually
2724 do is to just update a list of class names. */
2727 toFree->next = ctxp_for_generation;
2728 ctxp_for_generation = toFree;
2734 /* Create a parser context for the use of saving some global
2738 java_parser_context_save_global ()
2742 java_push_parser_context ();
2743 ctxp->saved_data_ctx = 1;
2746 /* If this context already stores data, create a new one suitable
2747 for data storage. */
2748 else if (ctxp->saved_data)
2749 create_new_parser_context (1);
2751 ctxp->lineno = lineno;
2752 ctxp->class_type = current_class;
2753 ctxp->filename = input_filename;
2754 ctxp->function_decl = current_function_decl;
2755 ctxp->saved_data = 1;
2758 /* Restore some global variables from the previous context. Make the
2759 previous context the current one. */
2762 java_parser_context_restore_global ()
2764 lineno = ctxp->lineno;
2765 current_class = ctxp->class_type;
2766 input_filename = ctxp->filename;
2770 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2771 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2773 current_function_decl = ctxp->function_decl;
2774 ctxp->saved_data = 0;
2775 if (ctxp->saved_data_ctx)
2776 java_pop_parser_context (0);
2779 /* Suspend vital data for the current class/function being parsed so
2780 that an other class can be parsed. Used to let local/anonymous
2781 classes be parsed. */
2784 java_parser_context_suspend ()
2786 /* This makes debugging through java_debug_context easier */
2787 static const char *const name = "<inner buffer context>";
2789 /* Duplicate the previous context, use it to save the globals we're
2791 create_new_parser_context (1);
2792 ctxp->function_decl = current_function_decl;
2793 ctxp->class_type = current_class;
2795 /* Then create a new context which inherits all data from the
2796 previous one. This will be the new current context */
2797 create_new_parser_context (1);
2799 /* Help debugging */
2800 ctxp->next->filename = name;
2803 /* Resume vital data for the current class/function being parsed so
2804 that an other class can be parsed. Used to let local/anonymous
2805 classes be parsed. The trick is the data storing file position
2806 informations must be restored to their current value, so parsing
2807 can resume as if no context was ever saved. */
2810 java_parser_context_resume ()
2812 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2813 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2814 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2816 /* We need to inherit the list of classes to complete/generate */
2817 restored->classd_list = old->classd_list;
2818 restored->class_list = old->class_list;
2820 /* Restore the current class and function from the saver */
2821 current_class = saver->class_type;
2822 current_function_decl = saver->function_decl;
2824 /* Retrive the restored context */
2827 /* Re-installed the data for the parsing to carry on */
2828 memcpy (&ctxp->marker_begining, &old->marker_begining,
2829 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2831 /* Buffer context can now be discarded */
2836 /* Add a new anchor node to which all statement(s) initializing static
2837 and non static initialized upon declaration field(s) will be
2841 java_parser_context_push_initialized_field ()
2845 node = build_tree_list (NULL_TREE, NULL_TREE);
2846 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2847 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2849 node = build_tree_list (NULL_TREE, NULL_TREE);
2850 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2851 CPC_INITIALIZER_LIST (ctxp) = node;
2853 node = build_tree_list (NULL_TREE, NULL_TREE);
2854 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2855 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2858 /* Pop the lists of initialized field. If this lists aren't empty,
2859 remember them so we can use it to create and populate the finit$
2860 or <clinit> functions. */
2863 java_parser_context_pop_initialized_field ()
2866 tree class_type = TREE_TYPE (GET_CPC ());
2868 if (CPC_INITIALIZER_LIST (ctxp))
2870 stmts = CPC_INITIALIZER_STMT (ctxp);
2871 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2872 if (stmts && !java_error_count)
2873 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2876 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2878 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2879 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2880 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2881 /* Keep initialization in order to enforce 8.5 */
2882 if (stmts && !java_error_count)
2883 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2886 /* JDK 1.1 instance initializers */
2887 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2889 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2890 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2891 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2892 if (stmts && !java_error_count)
2893 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2898 reorder_static_initialized (list)
2901 /* We have to keep things in order. The alias initializer have to
2902 come first, then the initialized regular field, in reverse to
2903 keep them in lexical order. */
2904 tree marker, previous = NULL_TREE;
2905 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2906 if (TREE_CODE (marker) == TREE_LIST
2907 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2910 /* No static initialized, the list is fine as is */
2912 list = TREE_CHAIN (marker);
2914 /* No marker? reverse the whole list */
2916 list = nreverse (list);
2918 /* Otherwise, reverse what's after the marker and the new reordered
2919 sublist will replace the marker. */
2922 TREE_CHAIN (previous) = NULL_TREE;
2923 list = nreverse (list);
2924 list = chainon (TREE_CHAIN (marker), list);
2929 /* Helper functions to dump the parser context stack. */
2931 #define TAB_CONTEXT(C) \
2932 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2935 java_debug_context_do (tab)
2938 struct parser_ctxt *copy = ctxp;
2942 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2944 fprintf (stderr, "filename: %s\n", copy->filename);
2946 fprintf (stderr, "lineno: %d\n", copy->lineno);
2948 fprintf (stderr, "package: %s\n",
2950 IDENTIFIER_POINTER (copy->package) : "<none>"));
2952 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2954 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2960 /* Dump the stacked up parser contexts. Intended to be called from a
2964 java_debug_context ()
2966 java_debug_context_do (0);
2971 /* Flag for the error report routine to issue the error the first time
2972 it's called (overriding the default behavior which is to drop the
2973 first invocation and honor the second one, taking advantage of a
2975 static int force_error = 0;
2977 /* Reporting an constructor invocation error. */
2979 parse_ctor_invocation_error ()
2981 if (DECL_CONSTRUCTOR_P (current_function_decl))
2982 yyerror ("Constructor invocation must be first thing in a constructor");
2984 yyerror ("Only constructors can invoke constructors");
2987 /* Reporting JDK1.1 features not implemented. */
2990 parse_jdk1_1_error (msg)
2993 sorry (": `%s' JDK1.1(TM) feature", msg);
2995 return empty_stmt_node;
2998 static int do_warning = 0;
3005 static int prev_lineno;
3006 static const char *prev_msg;
3009 char *remainder, *code_from_source;
3011 if (!force_error && prev_lineno == lineno)
3014 /* Save current error location but report latter, when the context is
3016 if (ctxp->java_error_flag == 0)
3018 ctxp->java_error_flag = 1;
3020 /* Do something to use the previous line if we're reaching the
3021 end of the file... */
3022 #ifdef VERBOSE_SKELETON
3023 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
3028 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
3029 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
3032 ctxp->java_error_flag = 0;
3034 java_warning_count++;
3038 if (elc.col == 0 && msg && msg[1] == ';')
3040 elc.col = ctxp->p_line->char_col-1;
3041 elc.line = ctxp->p_line->lineno;
3044 save_lineno = lineno;
3045 prev_lineno = lineno = elc.line;
3048 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3049 obstack_grow0 (&temporary_obstack,
3050 code_from_source, strlen (code_from_source));
3051 remainder = obstack_finish (&temporary_obstack);
3053 warning ("%s.\n%s", msg, remainder);
3055 error ("%s.\n%s", msg, remainder);
3057 /* This allow us to cheaply avoid an extra 'Invalid expression
3058 statement' error report when errors have been already reported on
3059 the same line. This occurs when we report an error but don't have
3060 a synchronization point other than ';', which
3061 expression_statement is the only one to take care of. */
3062 ctxp->prevent_ese = lineno = save_lineno;
3066 issue_warning_error_from_context (cl, msg, ap)
3071 const char *saved, *saved_input_filename;
3073 vsprintf (buffer, msg, ap);
3076 ctxp->elc.line = EXPR_WFL_LINENO (cl);
3077 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3078 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3080 /* We have a CL, that's a good reason for using it if it contains data */
3081 saved = ctxp->filename;
3082 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3083 ctxp->filename = EXPR_WFL_FILENAME (cl);
3084 saved_input_filename = input_filename;
3085 input_filename = ctxp->filename;
3087 java_error (buffer);
3088 ctxp->filename = saved;
3089 input_filename = saved_input_filename;
3093 /* Issue an error message at a current source line CL */
3096 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3099 VA_FIXEDARG (ap, tree, cl);
3100 VA_FIXEDARG (ap, const char *, msg);
3101 issue_warning_error_from_context (cl, msg, ap);
3105 /* Issue a warning at a current source line CL */
3108 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3111 VA_FIXEDARG (ap, tree, cl);
3112 VA_FIXEDARG (ap, const char *, msg);
3114 force_error = do_warning = 1;
3115 issue_warning_error_from_context (cl, msg, ap);
3116 do_warning = force_error = 0;
3121 find_expr_with_wfl (node)
3129 switch (TREE_CODE (node))
3132 node = BLOCK_EXPR_BODY (node);
3136 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3139 node = TREE_OPERAND (node, 1);
3143 node = TREE_OPERAND (node, 0);
3146 case LABELED_BLOCK_EXPR:
3147 node = TREE_OPERAND (node, 1);
3151 code = TREE_CODE_CLASS (TREE_CODE (node));
3152 if (((code == '1') || (code == '2') || (code == 'e'))
3153 && EXPR_WFL_LINECOL (node))
3161 /* Issue a missing return statement error. Uses METHOD to figure the
3162 last line of the method the error occurs in. */
3165 missing_return_error (method)
3168 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3169 parse_error_context (wfl_operator, "Missing return statement");
3172 /* Issue an unreachable statement error. From NODE, find the next
3173 statement to report appropriately. */
3175 unreachable_stmt_error (node)
3178 /* Browse node to find the next expression node that has a WFL. Use
3179 the location to report the error */
3180 if (TREE_CODE (node) == COMPOUND_EXPR)
3181 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3183 node = find_expr_with_wfl (node);
3187 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3188 parse_error_context (wfl_operator, "Unreachable statement");
3195 java_report_errors ()
3197 if (java_error_count)
3198 fprintf (stderr, "%d error%s",
3199 java_error_count, (java_error_count == 1 ? "" : "s"));
3200 if (java_warning_count)
3201 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3202 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3203 if (java_error_count || java_warning_count)
3204 putc ('\n', stderr);
3205 return java_error_count;
3209 java_accstring_lookup (flags)
3212 static char buffer [80];
3213 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3215 /* Access modifier looked-up first for easier report on forbidden
3217 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3218 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3219 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3220 if (flags & ACC_STATIC) COPY_RETURN ("static");
3221 if (flags & ACC_FINAL) COPY_RETURN ("final");
3222 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3223 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3224 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3225 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3226 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3227 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3234 /* Issuing error messages upon redefinition of classes, interfaces or
3238 classitf_redefinition_error (context, id, decl, cl)
3239 const char *context;
3242 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3243 context, IDENTIFIER_POINTER (id),
3244 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3245 /* Here we should point out where its redefined. It's a unicode. FIXME */
3249 variable_redefinition_error (context, name, type, line)
3250 tree context, name, type;
3253 const char *type_name;
3255 /* Figure a proper name for type. We might haven't resolved it */
3256 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3257 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3259 type_name = lang_printable_name (type, 0);
3261 parse_error_context (context,
3262 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3263 IDENTIFIER_POINTER (name),
3264 type_name, IDENTIFIER_POINTER (name), line);
3267 /* If ANAME is terminated with `[]', it indicates an array. This
3268 function returns the number of `[]' found and if this number is
3269 greater than zero, it extracts the array type name and places it in
3270 the node pointed to by TRIMMED unless TRIMMED is null. */
3273 build_type_name_from_array_name (aname, trimmed)
3277 const char *name = IDENTIFIER_POINTER (aname);
3278 int len = IDENTIFIER_LENGTH (aname);
3281 STRING_STRIP_BRACKETS (name, len, array_dims);
3283 if (array_dims && trimmed)
3284 *trimmed = get_identifier_with_length (name, len);
3290 build_array_from_name (type, type_wfl, name, ret_name)
3291 tree type, type_wfl, name, *ret_name;
3295 /* Eventually get more dims */
3296 more_dims = build_type_name_from_array_name (name, &name);
3298 /* If we have, then craft a new type for this variable */
3303 /* If we have a pointer, use its type */
3304 if (TREE_CODE (type) == POINTER_TYPE)
3305 type = TREE_TYPE (type);
3307 /* Building the first dimension of a primitive type uses this
3309 if (JPRIMITIVE_TYPE_P (type))
3311 type = build_java_array_type (type, -1);
3314 /* Otherwise, if we have a WFL for this type, use it (the type
3315 is already an array on an unresolved type, and we just keep
3316 on adding dimensions) */
3320 more_dims += build_type_name_from_array_name (TYPE_NAME (save),
3324 /* Add all the dimensions */
3326 type = build_unresolved_array_type (type);
3328 /* The type may have been incomplete in the first place */
3330 type = obtain_incomplete_type (type);
3338 /* Build something that the type identifier resolver will identify as
3339 being an array to an unresolved type. TYPE_WFL is a WFL on a
3343 build_unresolved_array_type (type_or_wfl)
3349 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3350 just create a array type */
3351 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3352 return build_java_array_type (type_or_wfl, -1);
3354 obstack_grow (&temporary_obstack,
3355 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3356 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3357 obstack_grow0 (&temporary_obstack, "[]", 2);
3358 ptr = obstack_finish (&temporary_obstack);
3359 wfl = build_expr_wfl (get_identifier (ptr),
3360 EXPR_WFL_FILENAME (type_or_wfl),
3361 EXPR_WFL_LINENO (type_or_wfl),
3362 EXPR_WFL_COLNO (type_or_wfl));
3363 /* Re-install the existing qualifications so that the type can be
3364 resolved properly. */
3365 EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3370 parser_add_interface (class_decl, interface_decl, wfl)
3371 tree class_decl, interface_decl, wfl;
3373 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3374 parse_error_context (wfl, "Interface `%s' repeated",
3375 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3378 /* Bulk of common class/interface checks. Return 1 if an error was
3379 encountered. TAG is 0 for a class, 1 for an interface. */
3382 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3383 int is_interface, flags;
3384 tree raw_name, qualified_name, decl, cl;
3387 int sca = 0; /* Static class allowed */
3388 int icaf = 0; /* Inner class allowed flags */
3389 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3392 fprintf (stderr, " %s%s %s",
3393 (CPC_INNER_P () ? "inner" : ""),
3394 (is_interface ? "interface" : "class"),
3395 IDENTIFIER_POINTER (qualified_name));
3397 /* Scope of an interface/class type name:
3398 - Can't be imported by a single type import
3399 - Can't already exists in the package */
3400 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3401 && (node = find_name_in_single_imports (raw_name))
3405 (cl, "%s name `%s' clashes with imported type `%s'",
3406 (is_interface ? "Interface" : "Class"),
3407 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3410 if (decl && CLASS_COMPLETE_P (decl))
3412 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3413 qualified_name, decl, cl);
3417 if (check_inner_class_redefinition (raw_name, cl))
3420 /* If public, file name should match class/interface name, except
3421 when dealing with an inner class */
3422 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3426 /* Contains OS dependent assumption on path separator. FIXME */
3427 for (f = &input_filename [strlen (input_filename)];
3428 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3431 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
3433 if (strncmp (IDENTIFIER_POINTER (raw_name),
3434 f , IDENTIFIER_LENGTH (raw_name)) ||
3435 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3437 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3438 (is_interface ? "interface" : "class"),
3439 IDENTIFIER_POINTER (qualified_name),
3440 IDENTIFIER_POINTER (raw_name));
3443 /* Static classes can be declared only in top level classes. Note:
3444 once static, a inner class is a top level class. */
3445 if (flags & ACC_STATIC)
3447 /* Catch the specific error of declaring an class inner class
3448 with no toplevel enclosing class. Prevent check_modifiers from
3449 complaining a second time */
3450 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3452 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3453 IDENTIFIER_POINTER (qualified_name));
3456 /* Else, in the context of a top-level class declaration, let
3457 `check_modifiers' do its job, otherwise, give it a go */
3459 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3462 /* Inner classes can be declared private or protected
3463 within their enclosing classes. */
3466 /* A class which is local to a block can't be public, private,
3467 protected or static. But it is created final, so allow this
3469 if (current_function_decl)
3470 icaf = sca = uaaf = ACC_FINAL;
3473 check_modifiers_consistency (flags);
3474 icaf = ACC_PROTECTED;
3475 if (! CLASS_INTERFACE (GET_CPC ()))
3476 icaf |= ACC_PRIVATE;
3483 uaaf = INTERFACE_INNER_MODIFIERS;
3485 uaaf = INTERFACE_MODIFIERS;
3487 check_modifiers ("Illegal modifier `%s' for interface declaration",
3491 check_modifiers ((current_function_decl ?
3492 "Illegal modifier `%s' for local class declaration" :
3493 "Illegal modifier `%s' for class declaration"),
3494 flags, uaaf|sca|icaf);
3499 make_nested_class_name (cpc_list)
3507 make_nested_class_name (TREE_CHAIN (cpc_list));
3509 /* Pick the qualified name when dealing with the first upmost
3511 name = (TREE_CHAIN (cpc_list) ?
3512 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3513 obstack_grow (&temporary_obstack,
3514 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3515 obstack_1grow (&temporary_obstack, '$');
3518 /* Can't redefine a class already defined in an earlier scope. */
3521 check_inner_class_redefinition (raw_name, cl)
3526 for (scope_list = GET_CPC_LIST (); scope_list;
3527 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3528 if (raw_name == GET_CPC_UN_NODE (scope_list))
3531 (cl, "The class name `%s' is already defined in this scope. An inner class may not have the same simple name as any of its enclosing classes",
3532 IDENTIFIER_POINTER (raw_name));
3538 /* Tries to find a decl for CLASS_TYPE within ENCLOSING. If we fail,
3539 we remember ENCLOSING and SUPER. */
3542 resolve_inner_class (circularity_hash, cl, enclosing, super, class_type)
3543 struct hash_table *circularity_hash;
3544 tree cl, *enclosing, *super, class_type;
3546 tree local_enclosing = *enclosing;
3547 tree local_super = NULL_TREE;
3549 while (local_enclosing)
3551 tree intermediate, decl;
3553 hash_lookup (circularity_hash,
3554 (const hash_table_key) local_enclosing, TRUE, NULL);
3556 if ((decl = find_as_inner_class (local_enclosing, class_type, cl)))
3559 intermediate = local_enclosing;
3560 /* Explore enclosing contexts. */
3561 while (INNER_CLASS_DECL_P (intermediate))
3563 intermediate = DECL_CONTEXT (intermediate);
3564 if ((decl = find_as_inner_class (intermediate, class_type, cl)))
3568 /* Now go to the upper classes, bail out if necessary. We will
3569 analyze the returned SUPER and act accordingly (see
3570 do_resolve_class.) */
3571 local_super = CLASSTYPE_SUPER (TREE_TYPE (local_enclosing));
3572 if (!local_super || local_super == object_type_node)
3575 if (TREE_CODE (local_super) == POINTER_TYPE)
3576 local_super = do_resolve_class (NULL, local_super, NULL, NULL);
3578 local_super = TYPE_NAME (local_super);
3580 /* We may not have checked for circular inheritance yet, so do so
3581 here to prevent an infinite loop. */
3582 if (hash_lookup (circularity_hash,
3583 (const hash_table_key) local_super, FALSE, NULL))
3586 cl = lookup_cl (local_enclosing);
3589 (cl, "Cyclic inheritance involving %s",
3590 IDENTIFIER_POINTER (DECL_NAME (local_enclosing)));
3591 local_enclosing = NULL_TREE;
3594 local_enclosing = local_super;
3597 /* We failed. Return LOCAL_SUPER and LOCAL_ENCLOSING. */
3598 *super = local_super;
3599 *enclosing = local_enclosing;
3604 /* Within ENCLOSING, find a decl for NAME and return it. NAME can be
3608 find_as_inner_class (enclosing, name, cl)
3609 tree enclosing, name, cl;
3611 tree qual, to_return;
3615 name = TYPE_NAME (name);
3617 /* First search: within the scope of `enclosing', search for name */
3618 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3619 qual = EXPR_WFL_QUALIFICATION (cl);
3621 qual = build_tree_list (cl, NULL_TREE);
3623 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3625 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3628 /* We're dealing with a qualified name. Try to resolve thing until
3629 we get something that is an enclosing class. */
3630 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3632 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3634 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3635 qual = TREE_CHAIN (qual))
3637 acc = merge_qualified_name (acc,
3638 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3639 BUILD_PTR_FROM_NAME (ptr, acc);
3640 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3643 /* A NULL qual and a decl means that the search ended
3644 successfully?!? We have to do something then. FIXME */
3649 qual = EXPR_WFL_QUALIFICATION (cl);
3651 /* Otherwise, create a qual for the other part of the resolution. */
3653 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3655 return find_as_inner_class_do (qual, enclosing);
3658 /* We go inside the list of sub classes and try to find a way
3662 find_as_inner_class_do (qual, enclosing)
3663 tree qual, enclosing;
3668 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3670 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3671 tree next_enclosing = NULL_TREE;
3674 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3675 inner_list; inner_list = TREE_CHAIN (inner_list))
3677 if (TREE_VALUE (inner_list) == name_to_match)
3679 next_enclosing = TREE_PURPOSE (inner_list);
3683 enclosing = next_enclosing;
3686 return (!qual && enclosing ? enclosing : NULL_TREE);
3689 /* Reach all inner classes and tie their unqualified name to a
3693 set_nested_class_simple_name_value (outer, set)
3699 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3700 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3701 TREE_PURPOSE (l) : NULL_TREE);
3705 link_nested_class_to_enclosing ()
3707 if (GET_ENCLOSING_CPC ())
3709 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3710 DECL_INNER_CLASS_LIST (enclosing) =
3711 tree_cons (GET_CPC (), GET_CPC_UN (),
3712 DECL_INNER_CLASS_LIST (enclosing));
3717 maybe_make_nested_class_name (name)
3720 tree id = NULL_TREE;
3724 make_nested_class_name (GET_CPC_LIST ());
3725 obstack_grow0 (&temporary_obstack,
3726 IDENTIFIER_POINTER (name),
3727 IDENTIFIER_LENGTH (name));
3728 id = get_identifier (obstack_finish (&temporary_obstack));
3730 QUALIFIED_P (id) = 1;
3735 /* If DECL is NULL, create and push a new DECL, record the current
3736 line CL and do other maintenance things. */
3739 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3740 tree decl, raw_name, qualified_name, cl;
3743 decl = push_class (make_class (), qualified_name);
3745 /* Take care of the file and line business */
3746 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3747 /* If we're emiting xrefs, store the line/col number information */
3749 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3751 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3752 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3753 CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3754 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3755 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3757 PUSH_CPC (decl, raw_name);
3758 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3760 /* Link the declaration to the already seen ones */
3761 TREE_CHAIN (decl) = ctxp->class_list;
3762 ctxp->class_list = decl;
3764 /* Create a new nodes in the global lists */
3765 gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3766 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3768 /* Install a new dependency list element */
3769 create_jdep_list (ctxp);
3771 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3772 IDENTIFIER_POINTER (qualified_name)));
3777 add_superinterfaces (decl, interface_list)
3778 tree decl, interface_list;
3781 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3782 takes care of ensuring that:
3783 - This is an accessible interface type,
3784 - Circularity detection.
3785 parser_add_interface is then called. If present but not defined,
3786 the check operation is delayed until the super interface gets
3788 for (node = interface_list; node; node = TREE_CHAIN (node))
3790 tree current = TREE_PURPOSE (node);
3791 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3792 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3794 if (!parser_check_super_interface (idecl, decl, current))
3795 parser_add_interface (decl, idecl, current);
3798 register_incomplete_type (JDEP_INTERFACE,
3799 current, decl, NULL_TREE);
3803 /* Create an interface in pass1 and return its decl. Return the
3804 interface's decl in pass 2. */
3807 create_interface (flags, id, super)
3811 tree raw_name = EXPR_WFL_NODE (id);
3812 tree q_name = parser_qualified_classname (raw_name);
3813 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3815 /* Certain syntax errors are making SUPER be like ID. Avoid this
3817 if (ctxp->class_err && id == super)
3820 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3822 /* Basic checks: scope, redefinition, modifiers */
3823 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3829 /* Suspend the current parsing context if we're parsing an inner
3833 java_parser_context_suspend ();
3834 /* Interface members are public. */
3835 if (CLASS_INTERFACE (GET_CPC ()))
3836 flags |= ACC_PUBLIC;
3839 /* Push a new context for (static) initialized upon declaration fields */
3840 java_parser_context_push_initialized_field ();
3842 /* Interface modifiers check
3843 - public/abstract allowed (already done at that point)
3844 - abstract is obsolete (comes first, it's a warning, or should be)
3845 - Can't use twice the same (checked in the modifier rule) */
3846 if ((flags & ACC_ABSTRACT) && flag_redundant)
3847 parse_warning_context
3848 (MODIFIER_WFL (ABSTRACT_TK),
3849 "Redundant use of `abstract' modifier. Interface `%s' is implicitly abstract", IDENTIFIER_POINTER (raw_name));
3851 /* Create a new decl if DECL is NULL, otherwise fix it */
3852 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3854 /* Set super info and mark the class a complete */
3855 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3856 object_type_node, ctxp->interface_number);
3857 ctxp->interface_number = 0;
3858 CLASS_COMPLETE_P (decl) = 1;
3859 add_superinterfaces (decl, super);
3864 /* Anonymous class counter. Will be reset to 1 every time a non
3865 anonymous class gets created. */
3866 static int anonymous_class_counter = 1;
3868 /* Patch anonymous class CLASS, by either extending or implementing
3872 patch_anonymous_class (type_decl, class_decl, wfl)
3873 tree type_decl, class_decl, wfl;
3875 tree class = TREE_TYPE (class_decl);
3876 tree type = TREE_TYPE (type_decl);
3877 tree binfo = TYPE_BINFO (class);
3879 /* If it's an interface, implement it */
3880 if (CLASS_INTERFACE (type_decl))
3885 if (parser_check_super_interface (type_decl, class_decl, wfl))
3888 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3889 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3890 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3891 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3892 /* And add the interface */
3893 parser_add_interface (class_decl, type_decl, wfl);
3895 /* Otherwise, it's a type we want to extend */
3898 if (parser_check_super (type_decl, class_decl, wfl))
3900 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3905 create_anonymous_class (location, type_name)
3910 tree super = NULL_TREE, itf = NULL_TREE;
3911 tree id, type_decl, class;
3913 /* The unqualified name of the anonymous class. It's just a number. */
3914 sprintf (buffer, "%d", anonymous_class_counter++);
3915 id = build_wfl_node (get_identifier (buffer));
3916 EXPR_WFL_LINECOL (id) = location;
3918 /* We know about the type to extend/implement. We go ahead */
3919 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3921 /* Create a class which either implements on extends the designated
3922 class. The class bears an innacessible name. */
3923 if (CLASS_INTERFACE (type_decl))
3925 /* It's OK to modify it here. It's been already used and
3926 shouldn't be reused */
3927 ctxp->interface_number = 1;
3928 /* Interfaces should presented as a list of WFLs */
3929 itf = build_tree_list (type_name, NULL_TREE);
3935 class = create_class (ACC_FINAL, id, super, itf);
3937 /* We didn't know anything about the stuff. We register a dependence. */
3939 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3941 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3945 /* Create a class in pass1 and return its decl. Return class
3946 interface's decl in pass 2. */
3949 create_class (flags, id, super, interfaces)
3951 tree id, super, interfaces;
3953 tree raw_name = EXPR_WFL_NODE (id);
3954 tree class_id, decl;
3955 tree super_decl_type;
3957 /* Certain syntax errors are making SUPER be like ID. Avoid this
3959 if (ctxp->class_err && id == super)
3962 class_id = parser_qualified_classname (raw_name);
3963 decl = IDENTIFIER_CLASS_VALUE (class_id);
3964 EXPR_WFL_NODE (id) = class_id;
3966 /* Basic check: scope, redefinition, modifiers */
3967 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3973 /* Suspend the current parsing context if we're parsing an inner
3974 class or an anonymous class. */
3977 java_parser_context_suspend ();
3978 /* Interface members are public. */
3979 if (CLASS_INTERFACE (GET_CPC ()))
3980 flags |= ACC_PUBLIC;
3983 /* Push a new context for (static) initialized upon declaration fields */
3984 java_parser_context_push_initialized_field ();
3986 /* Class modifier check:
3987 - Allowed modifier (already done at that point)
3988 - abstract AND final forbidden
3989 - Public classes defined in the correct file */
3990 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3992 (id, "Class `%s' can't be declared both abstract and final",
3993 IDENTIFIER_POINTER (raw_name));
3995 /* Create a new decl if DECL is NULL, otherwise fix it */
3996 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
3998 /* If SUPER exists, use it, otherwise use Object */
4001 /* Can't extend java.lang.Object */
4002 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
4004 parse_error_context (id, "Can't extend `java.lang.Object'");
4009 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
4011 else if (TREE_TYPE (decl) != object_type_node)
4012 super_decl_type = object_type_node;
4013 /* We're defining java.lang.Object */
4015 super_decl_type = NULL_TREE;
4017 /* A class nested in an interface is implicitly static. */
4018 if (INNER_CLASS_DECL_P (decl)
4019 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
4021 flags |= ACC_STATIC;
4024 /* Set super info and mark the class as complete. */
4025 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
4026 ctxp->interface_number);
4027 ctxp->interface_number = 0;
4028 CLASS_COMPLETE_P (decl) = 1;
4029 add_superinterfaces (decl, interfaces);
4031 /* Add the private this$<n> field, Replicate final locals still in
4032 scope as private final fields mangled like val$<local_name>.
4033 This doesn't not occur for top level (static) inner classes. */
4034 if (PURE_INNER_CLASS_DECL_P (decl))
4035 add_inner_class_fields (decl, current_function_decl);
4037 /* If doing xref, store the location at which the inherited class
4038 (if any) was seen. */
4039 if (flag_emit_xref && super)
4040 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
4042 /* Eventually sets the @deprecated tag flag */
4043 CHECK_DEPRECATED (decl);
4045 /* Reset the anonymous class counter when declaring non inner classes */
4046 if (!INNER_CLASS_DECL_P (decl))
4047 anonymous_class_counter = 1;
4052 /* End a class declaration: register the statements used to create
4053 finit$ and <clinit>, pop the current class and resume the prior
4054 parser context if necessary. */
4057 end_class_declaration (resume)
4060 /* If an error occurred, context weren't pushed and won't need to be
4061 popped by a resume. */
4062 int no_error_occurred = ctxp->next && GET_CPC () != error_mark_node;
4064 if (GET_CPC () != error_mark_node)
4065 dump_java_tree (TDI_class, GET_CPC ());
4067 java_parser_context_pop_initialized_field ();
4069 if (resume && no_error_occurred)
4070 java_parser_context_resume ();
4072 /* We're ending a class declaration, this is a good time to reset
4073 the interface cout. Note that might have been already done in
4074 create_interface, but if at that time an inner class was being
4075 dealt with, the interface count was reset in a context created
4076 for the sake of handling inner classes declaration. */
4077 ctxp->interface_number = 0;
4081 add_inner_class_fields (class_decl, fct_decl)
4085 tree block, marker, f;
4087 f = add_field (TREE_TYPE (class_decl),
4088 build_current_thisn (TREE_TYPE (class_decl)),
4089 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
4091 FIELD_THISN (f) = 1;
4096 for (block = GET_CURRENT_BLOCK (fct_decl);
4097 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
4100 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
4103 tree wfl, init, list;
4105 /* Avoid non final arguments. */
4106 if (!LOCAL_FINAL_P (decl))
4109 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
4110 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
4111 wfl = build_wfl_node (name);
4112 init = build_wfl_node (pname);
4113 /* Build an initialization for the field: it will be
4114 initialized by a parameter added to finit$, bearing a
4115 mangled name of the field itself (param$<n>.) The
4116 parameter is provided to finit$ by the constructor
4117 invoking it (hence the constructor will also feature a
4118 hidden parameter, set to the value of the outer context
4119 local at the time the inner class is created.)
4121 Note: we take into account all possible locals that can
4122 be accessed by the inner class. It's actually not trivial
4123 to minimize these aliases down to the ones really
4124 used. One way to do that would be to expand all regular
4125 methods first, then finit$ to get a picture of what's
4126 used. It works with the exception that we would have to
4127 go back on all constructor invoked in regular methods to
4128 have their invokation reworked (to include the right amount
4129 of alias initializer parameters.)
4131 The only real way around, I think, is a first pass to
4132 identify locals really used in the inner class. We leave
4133 the flag FIELD_LOCAL_ALIAS_USED around for that future
4136 On the other hand, it only affect local inner classes,
4137 whose constructors (and finit$ call) will be featuring
4138 unecessary arguments. It's easy for a developper to keep
4139 this number of parameter down by using the `final'
4140 keyword only when necessary. For the time being, we can
4141 issue a warning on unecessary finals. FIXME */
4142 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4145 /* Register the field. The TREE_LIST holding the part
4146 initialized/initializer will be marked ARG_FINAL_P so
4147 that the created field can be marked
4148 FIELD_LOCAL_ALIAS. */
4149 list = build_tree_list (wfl, init);
4150 ARG_FINAL_P (list) = 1;
4151 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4155 if (!CPC_INITIALIZER_STMT (ctxp))
4158 /* If we ever registered an alias field, insert and marker to
4159 remeber where the list ends. The second part of the list (the one
4160 featuring initialized fields) so it can be later reversed to
4161 enforce 8.5. The marker will be removed during that operation. */
4162 marker = build_tree_list (NULL_TREE, NULL_TREE);
4163 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4164 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4167 /* Can't use lookup_field () since we don't want to load the class and
4168 can't set the CLASS_LOADED_P flag */
4171 find_field (class, name)
4176 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4178 if (DECL_NAME (decl) == name)
4184 /* Wrap around lookup_field that doesn't potentially upset the value
4188 lookup_field_wrapper (class, name)
4192 tree decl = NULL_TREE;
4193 java_parser_context_save_global ();
4195 /* Last chance: if we're within the context of an inner class, we
4196 might be trying to access a local variable defined in an outer
4197 context. We try to look for it now. */
4198 if (INNER_CLASS_TYPE_P (class) && TREE_CODE (name) == IDENTIFIER_NODE)
4201 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4202 decl = lookup_field (&type, new_name);
4203 if (decl && decl != error_mark_node)
4204 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4206 if (!decl || decl == error_mark_node)
4209 decl = lookup_field (&type, name);
4212 java_parser_context_restore_global ();
4213 return decl == error_mark_node ? NULL : decl;
4216 /* Find duplicate field within the same class declarations and report
4217 the error. Returns 1 if a duplicated field was found, 0
4221 duplicate_declaration_error_p (new_field_name, new_type, cl)
4222 tree new_field_name, new_type, cl;
4224 /* This might be modified to work with method decl as well */
4225 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4228 char *t1 = xstrdup (purify_type_name
4229 ((TREE_CODE (new_type) == POINTER_TYPE
4230 && TREE_TYPE (new_type) == NULL_TREE) ?
4231 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4232 lang_printable_name (new_type, 1)));
4233 /* The type may not have been completed by the time we report
4235 char *t2 = xstrdup (purify_type_name
4236 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4237 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4238 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4239 lang_printable_name (TREE_TYPE (decl), 1)));
4241 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4242 t1, IDENTIFIER_POINTER (new_field_name),
4243 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4244 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4252 /* Field registration routine. If TYPE doesn't exist, field
4253 declarations are linked to the undefined TYPE dependency list, to
4254 be later resolved in java_complete_class () */
4257 register_fields (flags, type, variable_list)
4259 tree type, variable_list;
4261 tree current, saved_type;
4262 tree class_type = NULL_TREE;
4263 int saved_lineno = lineno;
4265 tree wfl = NULL_TREE;
4268 class_type = TREE_TYPE (GET_CPC ());
4270 if (!class_type || class_type == error_mark_node)
4273 /* If we're adding fields to interfaces, those fields are public,
4275 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4277 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4278 flags, ACC_PUBLIC, "interface field(s)");
4279 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4280 flags, ACC_STATIC, "interface field(s)");
4281 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4282 flags, ACC_FINAL, "interface field(s)");
4283 check_modifiers ("Illegal interface member modifier `%s'", flags,
4284 INTERFACE_FIELD_MODIFIERS);
4285 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4288 /* Obtain a suitable type for resolution, if necessary */
4289 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4291 /* If TYPE is fully resolved and we don't have a reference, make one */
4292 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4294 for (current = variable_list, saved_type = type; current;
4295 current = TREE_CHAIN (current), type = saved_type)
4299 tree cl = TREE_PURPOSE (current);
4300 tree init = TREE_VALUE (current);
4301 tree current_name = EXPR_WFL_NODE (cl);
4303 /* Can't declare non-final static fields in inner classes */
4304 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4305 && !(flags & ACC_FINAL))
4307 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4308 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4309 lang_printable_name (class_type, 0));
4311 /* Process NAME, as it may specify extra dimension(s) for it */
4312 type = build_array_from_name (type, wfl, current_name, ¤t_name);
4314 /* Type adjustment. We may have just readjusted TYPE because
4315 the variable specified more dimensions. Make sure we have
4316 a reference if we can and don't have one already. Also
4317 change the name if we have an init. */
4318 if (type != saved_type)
4320 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4322 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4325 real_type = GET_REAL_TYPE (type);
4326 /* Check for redeclarations */
4327 if (duplicate_declaration_error_p (current_name, real_type, cl))
4330 /* Set lineno to the line the field was found and create a
4331 declaration for it. Eventually sets the @deprecated tag flag. */
4333 lineno = EXPR_WFL_LINECOL (cl);
4335 lineno = EXPR_WFL_LINENO (cl);
4336 field_decl = add_field (class_type, current_name, real_type, flags);
4337 CHECK_DEPRECATED (field_decl);
4339 /* If the field denotes a final instance variable, then we
4340 allocate a LANG_DECL_SPECIFIC part to keep track of its
4341 initialization. We also mark whether the field was
4342 initialized upon its declaration. We don't do that if the
4343 created field is an alias to a final local. */
4344 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4346 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4347 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4350 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4351 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4352 hide parameters to this inner class finit$ and
4353 constructors. It also means that the field isn't final per
4355 if (ARG_FINAL_P (current))
4357 FIELD_LOCAL_ALIAS (field_decl) = 1;
4358 FIELD_FINAL (field_decl) = 0;
4361 /* Check if we must chain. */
4363 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4365 /* If we have an initialization value tied to the field */
4368 /* The field is declared static */
4369 if (flags & ACC_STATIC)
4371 /* We include the field and its initialization part into
4372 a list used to generate <clinit>. After <clinit> is
4373 walked, field initializations will be processed and
4374 fields initialized with known constants will be taken
4375 out of <clinit> and have their DECL_INITIAL set
4377 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4378 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4379 if (TREE_OPERAND (init, 1)
4380 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4381 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4383 /* A non-static field declared with an immediate initialization is
4384 to be initialized in <init>, if any. This field is remembered
4385 to be processed at the time of the generation of <init>. */
4388 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4389 SET_CPC_INITIALIZER_STMT (ctxp, init);
4391 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4392 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4395 lineno = saved_lineno;
4398 /* Generate finit$, using the list of initialized fields to populate
4399 its body. finit$'s parameter(s) list is adjusted to include the
4400 one(s) used to initialized the field(s) caching outer context
4404 generate_finit (class_type)
4408 tree list = TYPE_FINIT_STMT_LIST (class_type);
4409 tree mdecl, current, parms;
4411 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4412 class_type, NULL_TREE,
4414 CRAFTED_PARAM_LIST_FIXUP (parms);
4415 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4416 finit_identifier_node, parms);
4417 fix_method_argument_names (parms, mdecl);
4418 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4420 DECL_FUNCTION_NAP (mdecl) = count;
4421 start_artificial_method_body (mdecl);
4423 for (current = list; current; current = TREE_CHAIN (current))
4424 java_method_add_stmt (mdecl,
4425 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4427 end_artificial_method_body (mdecl);
4431 /* Generate a function to run the instance initialization code. The
4432 private method is called `instinit$'. Unless we're dealing with an
4433 anonymous class, we determine whether all ctors of CLASS_TYPE
4434 declare a checked exception in their `throws' clause in order to
4435 see whether it's necessary to encapsulate the instance initializer
4436 statements in a try/catch/rethrow sequence. */
4439 generate_instinit (class_type)
4443 tree compound = NULL_TREE;
4444 tree parms = tree_cons (this_identifier_node,
4445 build_pointer_type (class_type), end_params_node);
4446 tree mdecl = create_artificial_method (class_type, ACC_PRIVATE,
4448 instinit_identifier_node, parms);
4450 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4453 /* Gather all the statements in a compound */
4454 for (current = TYPE_II_STMT_LIST (class_type);
4455 current; current = TREE_CHAIN (current))
4456 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4458 /* We need to encapsulate COMPOUND by a try/catch statement to
4459 rethrow exceptions that might occur in the instance initializer.
4460 We do that only if all ctors of CLASS_TYPE are set to catch a
4461 checked exception. This doesn't apply to anonymous classes (since
4462 they don't have declared ctors.) */
4463 if (!ANONYMOUS_CLASS_P (class_type) &&
4464 ctors_unchecked_throws_clause_p (class_type))
4466 compound = encapsulate_with_try_catch (0, exception_type_node, compound,
4467 build1 (THROW_EXPR, NULL_TREE,
4468 build_wfl_node (wpv_id)));
4469 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
4470 exception_type_node);
4473 start_artificial_method_body (mdecl);
4474 java_method_add_stmt (mdecl, compound);
4475 end_artificial_method_body (mdecl);
4482 build_instinit_invocation (class_type)
4485 tree to_return = NULL_TREE;
4487 if (TYPE_II_STMT_LIST (class_type))
4489 tree parm = build_tree_list (NULL_TREE,
4490 build_wfl_node (this_identifier_node));
4492 build_method_invocation (build_wfl_node (instinit_identifier_node),
4498 /* Shared accros method_declarator and method_header to remember the
4499 patch stage that was reached during the declaration of the method.
4500 A method DECL is built differently is there is no patch
4501 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4502 pending on the currently defined method. */
4504 static int patch_stage;
4506 /* Check the method declaration and add the method to its current
4507 class. If the argument list is known to contain incomplete types,
4508 the method is partially added and the registration will be resume
4509 once the method arguments resolved. If TYPE is NULL, we're dealing
4510 with a constructor. */
4513 method_header (flags, type, mdecl, throws)
4515 tree type, mdecl, throws;
4517 tree type_wfl = NULL_TREE;
4518 tree meth_name = NULL_TREE;
4519 tree current, orig_arg, this_class = NULL;
4522 int constructor_ok = 0, must_chain;
4525 if (mdecl == error_mark_node)
4526 return error_mark_node;
4527 meth = TREE_VALUE (mdecl);
4528 id = TREE_PURPOSE (mdecl);
4530 check_modifiers_consistency (flags);
4533 this_class = TREE_TYPE (GET_CPC ());
4535 if (!this_class || this_class == error_mark_node)
4538 /* There are some forbidden modifiers for an abstract method and its
4539 class must be abstract as well. */
4540 if (type && (flags & ACC_ABSTRACT))
4542 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4543 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4544 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4545 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4546 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED, id, "Synchronized");
4547 ABSTRACT_CHECK (flags, ACC_STRICT, id, "Strictfp");
4548 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4549 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4551 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4552 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4553 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4556 /* Things to be checked when declaring a constructor */
4559 int ec = java_error_count;
4560 /* 8.6: Constructor declarations: we might be trying to define a
4561 method without specifying a return type. */
4562 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4564 (id, "Invalid method declaration, return type required");
4565 /* 8.6.3: Constructor modifiers */
4568 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4569 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4570 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4571 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4572 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4573 JCONSTRUCTOR_CHECK (flags, ACC_STRICT, id, "strictfp");
4575 /* If we found error here, we don't consider it's OK to tread
4576 the method definition as a constructor, for the rest of this
4578 if (ec == java_error_count)
4582 /* Method declared within the scope of an interface are implicitly
4583 abstract and public. Conflicts with other erroneously provided
4584 modifiers are checked right after. */
4586 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4588 /* If FLAGS isn't set because of a modifier, turn the
4589 corresponding modifier WFL to NULL so we issue a warning on
4590 the obsolete use of the modifier */
4591 if (!(flags & ACC_PUBLIC))
4592 MODIFIER_WFL (PUBLIC_TK) = NULL;
4593 if (!(flags & ACC_ABSTRACT))
4594 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4595 flags |= ACC_PUBLIC;
4596 flags |= ACC_ABSTRACT;
4599 /* Inner class can't declare static methods */
4600 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4603 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4604 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4605 lang_printable_name (this_class, 0));
4608 /* Modifiers context reset moved up, so abstract method declaration
4609 modifiers can be later checked. */
4611 /* Set constructor returned type to void and method name to <init>,
4612 unless we found an error identifier the constructor (in which
4613 case we retain the original name) */
4616 type = void_type_node;
4618 meth_name = init_identifier_node;
4621 meth_name = EXPR_WFL_NODE (id);
4623 /* Do the returned type resolution and registration if necessary */
4624 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4627 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4628 EXPR_WFL_NODE (id) = meth_name;
4629 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4633 patch_stage = JDEP_METHOD_RETURN;
4634 register_incomplete_type (patch_stage, type_wfl, id, type);
4635 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4638 TREE_TYPE (meth) = type;
4640 saved_lineno = lineno;
4641 /* When defining an abstract or interface method, the curly
4642 bracket at level 1 doesn't exist because there is no function
4644 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4645 EXPR_WFL_LINENO (id));
4647 /* Remember the original argument list */
4648 orig_arg = TYPE_ARG_TYPES (meth);
4650 if (patch_stage) /* includes ret type and/or all args */
4653 meth = add_method_1 (this_class, flags, meth_name, meth);
4654 /* Patch for the return type */
4655 if (patch_stage == JDEP_METHOD_RETURN)
4657 jdep = CLASSD_LAST (ctxp->classd_list);
4658 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4660 /* This is the stop JDEP. METH allows the function's signature
4662 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4665 meth = add_method (this_class, flags, meth_name,
4666 build_java_signature (meth));
4668 /* Remember final parameters */
4669 MARK_FINAL_PARMS (meth, orig_arg);
4671 /* Fix the method argument list so we have the argument name
4673 fix_method_argument_names (orig_arg, meth);
4675 /* Register the parameter number and re-install the current line
4677 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4678 lineno = saved_lineno;
4680 /* Register exception specified by the `throws' keyword for
4681 resolution and set the method decl appropriate field to the list.
4682 Note: the grammar ensures that what we get here are class
4686 throws = nreverse (throws);
4687 for (current = throws; current; current = TREE_CHAIN (current))
4689 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4690 NULL_TREE, NULL_TREE);
4691 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4692 &TREE_VALUE (current);
4694 DECL_FUNCTION_THROWS (meth) = throws;
4697 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4698 DECL_FUNCTION_WFL (meth) = id;
4700 /* Set the flag if we correctly processed a constructor */
4703 DECL_CONSTRUCTOR_P (meth) = 1;
4704 /* Compute and store the number of artificial parameters declared
4705 for this constructor */
4706 for (count = 0, current = TYPE_FIELDS (this_class); current;
4707 current = TREE_CHAIN (current))
4708 if (FIELD_LOCAL_ALIAS (current))
4710 DECL_FUNCTION_NAP (meth) = count;
4713 /* Eventually set the @deprecated tag flag */
4714 CHECK_DEPRECATED (meth);
4716 /* If doing xref, store column and line number information instead
4717 of the line number only. */
4719 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4725 fix_method_argument_names (orig_arg, meth)
4726 tree orig_arg, meth;
4728 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4729 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4731 TREE_PURPOSE (arg) = this_identifier_node;
4732 arg = TREE_CHAIN (arg);
4734 while (orig_arg != end_params_node)
4736 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4737 orig_arg = TREE_CHAIN (orig_arg);
4738 arg = TREE_CHAIN (arg);
4742 /* Complete the method declaration with METHOD_BODY. */
4745 finish_method_declaration (method_body)
4750 if (!current_function_decl)
4753 flags = get_access_flags_from_decl (current_function_decl);
4755 /* 8.4.5 Method Body */
4756 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4758 tree name = DECL_NAME (current_function_decl);
4759 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4760 "%s method `%s' can't have a body defined",
4761 (METHOD_NATIVE (current_function_decl) ?
4762 "Native" : "Abstract"),
4763 IDENTIFIER_POINTER (name));
4764 method_body = NULL_TREE;
4766 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4768 tree name = DECL_NAME (current_function_decl);
4770 (DECL_FUNCTION_WFL (current_function_decl),
4771 "Non native and non abstract method `%s' must have a body defined",
4772 IDENTIFIER_POINTER (name));
4773 method_body = NULL_TREE;
4776 if (flag_emit_class_files && method_body
4777 && TREE_CODE (method_body) == NOP_EXPR
4778 && TREE_TYPE (current_function_decl)
4779 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4780 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4782 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4783 maybe_absorb_scoping_blocks ();
4784 /* Exit function's body */
4786 /* Merge last line of the function with first line, directly in the
4787 function decl. It will be used to emit correct debug info. */
4788 if (!flag_emit_xref)
4789 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4791 /* Since function's argument's list are shared, reset the
4792 ARG_FINAL_P parameter that might have been set on some of this
4793 function parameters. */
4794 UNMARK_FINAL_PARMS (current_function_decl);
4796 /* So we don't have an irrelevant function declaration context for
4797 the next static block we'll see. */
4798 current_function_decl = NULL_TREE;
4801 /* Build a an error message for constructor circularity errors. */
4804 constructor_circularity_msg (from, to)
4807 static char string [4096];
4808 char *t = xstrdup (lang_printable_name (from, 0));
4809 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4814 /* Verify a circular call to METH. Return 1 if an error is found, 0
4818 verify_constructor_circularity (meth, current)
4821 static tree list = NULL_TREE;
4822 static int initialized_p;
4825 /* If we haven't already registered LIST with the garbage collector,
4829 ggc_add_tree_root (&list, 1);
4833 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4835 if (TREE_VALUE (c) == meth)
4841 list = nreverse (list);
4842 for (liste = list; liste; liste = TREE_CHAIN (liste))
4845 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4846 constructor_circularity_msg
4847 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4851 t = xstrdup (lang_printable_name (meth, 0));
4852 parse_error_context (TREE_PURPOSE (c),
4853 "%s: recursive invocation of constructor `%s'",
4854 constructor_circularity_msg (current, meth), t);
4860 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4862 list = tree_cons (c, current, list);
4863 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4865 list = TREE_CHAIN (list);
4870 /* Check modifiers that can be declared but exclusively */
4873 check_modifiers_consistency (flags)
4877 tree cl = NULL_TREE;
4879 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4880 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4881 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4884 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4888 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4889 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4891 parse_error_context (cl,
4892 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
4895 /* Check the methode header METH for abstract specifics features */
4898 check_abstract_method_header (meth)
4901 int flags = get_access_flags_from_decl (meth);
4903 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4904 ACC_ABSTRACT, "abstract method",
4905 IDENTIFIER_POINTER (DECL_NAME (meth)));
4906 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4907 ACC_PUBLIC, "abstract method",
4908 IDENTIFIER_POINTER (DECL_NAME (meth)));
4910 check_modifiers ("Illegal modifier `%s' for interface method",
4911 flags, INTERFACE_METHOD_MODIFIERS);
4914 /* Create a FUNCTION_TYPE node and start augmenting it with the
4915 declared function arguments. Arguments type that can't be resolved
4916 are left as they are, but the returned node is marked as containing
4917 incomplete types. */
4920 method_declarator (id, list)
4923 tree arg_types = NULL_TREE, current, node;
4924 tree meth = make_node (FUNCTION_TYPE);
4927 patch_stage = JDEP_NO_PATCH;
4929 if (GET_CPC () == error_mark_node)
4930 return error_mark_node;
4932 /* If we're dealing with an inner class constructor, we hide the
4933 this$<n> decl in the name field of its parameter declaration. We
4934 also might have to hide the outer context local alias
4935 initializers. Not done when the class is a toplevel class. */
4936 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4937 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4939 tree aliases_list, type, thisn;
4940 /* First the aliases, linked to the regular parameters */
4942 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4943 TREE_TYPE (GET_CPC ()),
4945 list = chainon (nreverse (aliases_list), list);
4948 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4949 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4950 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4954 for (current = list; current; current = TREE_CHAIN (current))
4957 tree wfl_name = TREE_PURPOSE (current);
4958 tree type = TREE_VALUE (current);
4959 tree name = EXPR_WFL_NODE (wfl_name);
4960 tree already, arg_node;
4961 tree type_wfl = NULL_TREE;
4964 /* Obtain a suitable type for resolution, if necessary */
4965 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4967 /* Process NAME, as it may specify extra dimension(s) for it */
4968 type = build_array_from_name (type, type_wfl, name, &name);
4969 EXPR_WFL_NODE (wfl_name) = name;
4971 real_type = GET_REAL_TYPE (type);
4972 if (TREE_CODE (real_type) == RECORD_TYPE)
4974 real_type = promote_type (real_type);
4975 if (TREE_CODE (type) == TREE_LIST)
4976 TREE_PURPOSE (type) = real_type;
4979 /* Check redefinition */
4980 for (already = arg_types; already; already = TREE_CHAIN (already))
4981 if (TREE_PURPOSE (already) == name)
4984 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4985 IDENTIFIER_POINTER (name),
4986 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4990 /* If we've an incomplete argument type, we know there is a location
4991 to patch when the type get resolved, later. */
4995 patch_stage = JDEP_METHOD;
4996 type = register_incomplete_type (patch_stage,
4997 type_wfl, wfl_name, type);
4998 jdep = CLASSD_LAST (ctxp->classd_list);
4999 JDEP_MISC (jdep) = id;
5002 /* The argument node: a name and a (possibly) incomplete type. */
5003 arg_node = build_tree_list (name, real_type);
5004 /* Remeber arguments declared final. */
5005 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
5008 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
5009 TREE_CHAIN (arg_node) = arg_types;
5010 arg_types = arg_node;
5012 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
5013 node = build_tree_list (id, meth);
5018 unresolved_type_p (wfl, returned)
5023 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
5027 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
5028 if (decl && current_class && (decl == TYPE_NAME (current_class)))
5029 *returned = TREE_TYPE (decl);
5030 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
5031 *returned = TREE_TYPE (GET_CPC ());
5033 *returned = NULL_TREE;
5042 /* From NAME, build a qualified identifier node using the
5043 qualification from the current package definition. */
5046 parser_qualified_classname (name)
5049 tree nested_class_name;
5051 if ((nested_class_name = maybe_make_nested_class_name (name)))
5052 return nested_class_name;
5055 return merge_qualified_name (ctxp->package, name);
5060 /* Called once the type a interface extends is resolved. Returns 0 if
5061 everything is OK. */
5064 parser_check_super_interface (super_decl, this_decl, this_wfl)
5065 tree super_decl, this_decl, this_wfl;
5067 tree super_type = TREE_TYPE (super_decl);
5069 /* Has to be an interface */
5070 if (!CLASS_INTERFACE (super_decl))
5073 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
5074 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
5075 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
5076 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
5077 "interface" : "class"),
5078 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
5082 /* Check top-level interface access. Inner classes are subject to member
5083 access rules (6.6.1). */
5084 if (! INNER_CLASS_P (super_type)
5085 && check_pkg_class_access (DECL_NAME (super_decl),
5086 lookup_cl (this_decl), true))
5089 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
5090 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5091 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5095 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
5096 0 if everthing is OK. */
5099 parser_check_super (super_decl, this_decl, wfl)
5100 tree super_decl, this_decl, wfl;
5102 tree super_type = TREE_TYPE (super_decl);
5104 /* SUPER should be a CLASS (neither an array nor an interface) */
5105 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
5108 (wfl, "Class `%s' can't subclass %s `%s'",
5109 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5110 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
5111 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5115 if (CLASS_FINAL (TYPE_NAME (super_type)))
5117 parse_error_context (wfl, "Can't subclass final classes: %s",
5118 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
5122 /* Check top-level class scope. Inner classes are subject to member access
5124 if (! INNER_CLASS_P (super_type)
5125 && (check_pkg_class_access (DECL_NAME (super_decl), wfl, true)))
5128 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
5129 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
5130 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
5134 /* Create a new dependency list and link it (in a LIFO manner) to the
5135 CTXP list of type dependency list. */
5138 create_jdep_list (ctxp)
5139 struct parser_ctxt *ctxp;
5141 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
5142 new->first = new->last = NULL;
5143 new->next = ctxp->classd_list;
5144 ctxp->classd_list = new;
5148 reverse_jdep_list (ctxp)
5149 struct parser_ctxt *ctxp;
5151 register jdeplist *prev = NULL, *current, *next;
5152 for (current = ctxp->classd_list; current; current = next)
5154 next = current->next;
5155 current->next = prev;
5161 /* Create a fake pointer based on the ID stored in
5162 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5163 registered again. */
5166 obtain_incomplete_type (type_name)
5169 tree ptr = NULL_TREE, name;
5171 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5172 name = EXPR_WFL_NODE (type_name);
5173 else if (INCOMPLETE_TYPE_P (type_name))
5174 name = TYPE_NAME (type_name);
5178 BUILD_PTR_FROM_NAME (ptr, name);
5184 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5185 non NULL instead of computing a new fake type based on WFL. The new
5186 dependency is inserted in the current type dependency list, in FIFO
5190 register_incomplete_type (kind, wfl, decl, ptr)
5192 tree wfl, decl, ptr;
5194 jdep *new = (jdep *)xmalloc (sizeof (jdep));
5196 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5197 ptr = obtain_incomplete_type (wfl);
5199 JDEP_KIND (new) = kind;
5200 JDEP_DECL (new) = decl;
5201 JDEP_TO_RESOLVE (new) = ptr;
5202 JDEP_WFL (new) = wfl;
5203 JDEP_CHAIN (new) = NULL;
5204 JDEP_MISC (new) = NULL_TREE;
5205 /* For some dependencies, set the enclosing class of the current
5206 class to be the enclosing context */
5207 if ((kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS)
5208 && GET_ENCLOSING_CPC ())
5209 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5210 else if (kind == JDEP_SUPER)
5211 JDEP_ENCLOSING (new) = (GET_ENCLOSING_CPC () ?
5212 TREE_VALUE (GET_ENCLOSING_CPC ()) : NULL_TREE);
5214 JDEP_ENCLOSING (new) = GET_CPC ();
5215 JDEP_GET_PATCH (new) = (tree *)NULL;
5217 JDEP_INSERT (ctxp->classd_list, new);
5222 /* This checks for circular references with innerclasses. We start
5223 from SOURCE and should never reach TARGET. Extended/implemented
5224 types in SOURCE have their enclosing context checked not to reach
5225 TARGET. When the last enclosing context of SOURCE is reached, its
5226 extended/implemented types are also checked not to reach TARGET.
5227 In case of error, WFL of the offending type is returned; NULL_TREE
5231 check_inner_circular_reference (source, target)
5235 tree basetype_vec = TYPE_BINFO_BASETYPES (source);
5242 for (i = 0; i < TREE_VEC_LENGTH (basetype_vec); i++)
5244 tree su = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
5246 if (inherits_from_p (su, target))
5247 return lookup_cl (TYPE_NAME (su));
5249 for (ctx = DECL_CONTEXT (TYPE_NAME (su)); ctx; ctx = DECL_CONTEXT (ctx))
5251 /* An enclosing context shouldn't be TARGET */
5252 if (ctx == TYPE_NAME (target))
5253 return lookup_cl (TYPE_NAME (su));
5255 /* When we reach the enclosing last context, start a check
5256 on it, with the same target */
5257 if (! DECL_CONTEXT (ctx) &&
5258 (cl = check_inner_circular_reference (TREE_TYPE (ctx), target)))
5265 /* Explore TYPE's `extends' clause member(s) and return the WFL of the
5266 offending type if a circularity is detected. NULL_TREE is returned
5267 otherwise. TYPE can be an interface or a class. */
5270 check_circular_reference (type)
5273 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5279 if (! CLASS_INTERFACE (TYPE_NAME (type)))
5281 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5282 return lookup_cl (TYPE_NAME (type));
5286 for (i = 0; i < TREE_VEC_LENGTH (basetype_vec); i++)
5288 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5289 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5290 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5291 return lookup_cl (TYPE_NAME (BINFO_TYPE (vec_elt)));
5297 java_check_circular_reference ()
5300 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5302 tree type = TREE_TYPE (current);
5305 cl = check_circular_reference (type);
5307 cl = check_inner_circular_reference (type, type);
5309 parse_error_context (cl, "Cyclic class inheritance%s",
5310 (cyclic_inheritance_report ?
5311 cyclic_inheritance_report : ""));
5315 /* Augment the parameter list PARM with parameters crafted to
5316 initialize outer context locals aliases. Through ARTIFICIAL, a
5317 count is kept of the number of crafted parameters. MODE governs
5318 what eventually gets created: something suitable for a function
5319 creation or a function invocation, either the constructor or
5323 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5325 tree class_type, parm;
5329 tree additional_parms = NULL_TREE;
5331 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5332 if (FIELD_LOCAL_ALIAS (field))
5334 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5335 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5340 case AIPL_FUNCTION_DECLARATION:
5341 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5343 purpose = build_wfl_node (mangled_id);
5344 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5345 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5347 value = TREE_TYPE (field);
5350 case AIPL_FUNCTION_CREATION:
5351 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5353 value = TREE_TYPE (field);
5356 case AIPL_FUNCTION_FINIT_INVOCATION:
5357 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5359 /* Now, this is wrong. purpose should always be the NAME
5360 of something and value its matching value (decl, type,
5361 etc...) FIXME -- but there is a lot to fix. */
5363 /* When invoked for this kind of operation, we already
5364 know whether a field is used or not. */
5365 purpose = TREE_TYPE (field);
5366 value = build_wfl_node (mangled_id);
5369 case AIPL_FUNCTION_CTOR_INVOCATION:
5370 /* There are two case: the constructor invokation happends
5371 outside the local inner, in which case, locales from the outer
5372 context are directly used.
5374 Otherwise, we fold to using the alias directly. */
5375 if (class_type == current_class)
5379 name = get_identifier (&buffer[4]);
5380 value = IDENTIFIER_LOCAL_VALUE (name);
5384 additional_parms = tree_cons (purpose, value, additional_parms);
5388 if (additional_parms)
5390 if (ANONYMOUS_CLASS_P (class_type)
5391 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5392 additional_parms = nreverse (additional_parms);
5393 parm = chainon (additional_parms, parm);
5399 /* Craft a constructor for CLASS_DECL -- what we should do when none
5400 where found. ARGS is non NULL when a special signature must be
5401 enforced. This is the case for anonymous classes. */
5404 craft_constructor (class_decl, args)
5405 tree class_decl, args;
5407 tree class_type = TREE_TYPE (class_decl);
5408 tree parm = NULL_TREE;
5409 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5411 int i = 0, artificial = 0;
5412 tree decl, ctor_name;
5415 /* The constructor name is <init> unless we're dealing with an
5416 anonymous class, in which case the name will be fixed after having
5418 if (ANONYMOUS_CLASS_P (class_type))
5419 ctor_name = DECL_NAME (class_decl);
5421 ctor_name = init_identifier_node;
5423 /* If we're dealing with an inner class constructor, we hide the
5424 this$<n> decl in the name field of its parameter declaration. */
5425 if (PURE_INNER_CLASS_TYPE_P (class_type))
5427 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5428 parm = tree_cons (build_current_thisn (class_type),
5429 build_pointer_type (type), parm);
5431 /* Some more arguments to be hidden here. The values of the local
5432 variables of the outer context that the inner class needs to see. */
5433 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5438 /* Then if there are any args to be enforced, enforce them now */
5439 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5441 sprintf (buffer, "parm%d", i++);
5442 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5445 CRAFTED_PARAM_LIST_FIXUP (parm);
5446 decl = create_artificial_method (class_type, flags, void_type_node,
5448 fix_method_argument_names (parm, decl);
5449 /* Now, mark the artificial parameters. */
5450 DECL_FUNCTION_NAP (decl) = artificial;
5451 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5455 /* Fix the constructors. This will be called right after circular
5456 references have been checked. It is necessary to fix constructors
5457 early even if no code generation will take place for that class:
5458 some generated constructor might be required by the class whose
5459 compilation triggered this one to be simply loaded. */
5462 java_fix_constructors ()
5466 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5468 tree class_type = TREE_TYPE (current);
5472 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5475 current_class = class_type;
5476 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5478 if (DECL_CONSTRUCTOR_P (decl))
5480 fix_constructors (decl);
5485 /* Anonymous class constructor can't be generated that early. */
5486 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5487 craft_constructor (current, NULL_TREE);
5491 /* safe_layout_class just makes sure that we can load a class without
5492 disrupting the current_class, input_file, lineno, etc, information
5493 about the class processed currently. */
5496 safe_layout_class (class)
5499 tree save_current_class = current_class;
5500 const char *save_input_filename = input_filename;
5501 int save_lineno = lineno;
5503 layout_class (class);
5505 current_class = save_current_class;
5506 input_filename = save_input_filename;
5507 lineno = save_lineno;
5511 jdep_resolve_class (dep)
5516 if (JDEP_RESOLVED_P (dep))
5517 decl = JDEP_RESOLVED_DECL (dep);
5520 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5521 JDEP_DECL (dep), JDEP_WFL (dep));
5522 JDEP_RESOLVED (dep, decl);
5526 complete_class_report_errors (dep);
5527 else if (PURE_INNER_CLASS_DECL_P (decl))
5529 tree inner = TREE_TYPE (decl);
5530 if (! CLASS_LOADED_P (inner))
5532 safe_layout_class (inner);
5533 if (TYPE_SIZE (inner) == error_mark_node)
5534 TYPE_SIZE (inner) = NULL_TREE;
5536 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5541 /* Complete unsatisfied class declaration and their dependencies */
5544 java_complete_class ()
5551 /* Process imports */
5554 /* Rever things so we have the right order */
5555 ctxp->class_list = nreverse (ctxp->class_list);
5556 ctxp->classd_list = reverse_jdep_list (ctxp);
5558 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5560 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5564 /* We keep the compilation unit imports in the class so that
5565 they can be used later to resolve type dependencies that
5566 aren't necessary to solve now. */
5567 TYPE_IMPORT_LIST (TREE_TYPE (cclass)) = ctxp->import_list;
5568 TYPE_IMPORT_DEMAND_LIST (TREE_TYPE (cclass)) = ctxp->import_demand_list;
5570 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5573 if (!(decl = jdep_resolve_class (dep)))
5576 /* Now it's time to patch */
5577 switch (JDEP_KIND (dep))
5580 /* Simply patch super */
5581 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5583 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5584 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5589 /* We do part of the job done in add_field */
5590 tree field_decl = JDEP_DECL (dep);
5591 tree field_type = TREE_TYPE (decl);
5592 if (TREE_CODE (field_type) == RECORD_TYPE)
5593 field_type = promote_type (field_type);
5594 TREE_TYPE (field_decl) = field_type;
5595 DECL_ALIGN (field_decl) = 0;
5596 DECL_USER_ALIGN (field_decl) = 0;
5597 layout_decl (field_decl, 0);
5598 SOURCE_FRONTEND_DEBUG
5599 (("Completed field/var decl `%s' with `%s'",
5600 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5601 IDENTIFIER_POINTER (DECL_NAME (decl))));
5604 case JDEP_METHOD: /* We start patching a method */
5605 case JDEP_METHOD_RETURN:
5611 type = TREE_TYPE(decl);
5612 if (TREE_CODE (type) == RECORD_TYPE)
5613 type = promote_type (type);
5614 JDEP_APPLY_PATCH (dep, type);
5615 SOURCE_FRONTEND_DEBUG
5616 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5617 "Completing fct `%s' with ret type `%s'":
5618 "Completing arg `%s' with type `%s'"),
5619 IDENTIFIER_POINTER (EXPR_WFL_NODE
5620 (JDEP_DECL_WFL (dep))),
5621 IDENTIFIER_POINTER (DECL_NAME (decl))));
5625 dep = JDEP_CHAIN (dep);
5626 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5629 decl = jdep_resolve_class (dep);
5633 tree mdecl = JDEP_DECL (dep), signature;
5634 /* Recompute and reset the signature, check first that
5635 all types are now defined. If they're not,
5636 don't build the signature. */
5637 if (check_method_types_complete (mdecl))
5639 signature = build_java_signature (TREE_TYPE (mdecl));
5640 set_java_signature (TREE_TYPE (mdecl), signature);
5647 case JDEP_INTERFACE:
5648 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5651 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5656 type = TREE_TYPE(decl);
5657 if (TREE_CODE (type) == RECORD_TYPE)
5658 type = promote_type (type);
5659 JDEP_APPLY_PATCH (dep, type);
5663 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5664 SOURCE_FRONTEND_DEBUG
5665 (("Completing a random type dependency on a '%s' node",
5666 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5669 case JDEP_EXCEPTION:
5670 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5671 SOURCE_FRONTEND_DEBUG
5672 (("Completing `%s' `throws' argument node",
5673 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5676 case JDEP_ANONYMOUS:
5677 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5688 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5692 resolve_class (enclosing, class_type, decl, cl)
5693 tree enclosing, class_type, decl, cl;
5695 tree tname = TYPE_NAME (class_type);
5696 tree resolved_type = TREE_TYPE (class_type);
5698 tree resolved_type_decl;
5700 if (resolved_type != NULL_TREE)
5702 tree resolved_type_decl = TYPE_NAME (resolved_type);
5703 if (resolved_type_decl == NULL_TREE
5704 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5706 resolved_type_decl = build_decl (TYPE_DECL,
5707 TYPE_NAME (class_type),
5710 return resolved_type_decl;
5713 /* 1- Check to see if we have an array. If true, find what we really
5715 if ((array_dims = build_type_name_from_array_name (tname,
5716 &TYPE_NAME (class_type))))
5717 WFL_STRIP_BRACKET (cl, cl);
5719 /* 2- Resolve the bare type */
5720 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5723 resolved_type = TREE_TYPE (resolved_type_decl);
5725 /* 3- If we have and array, reconstruct the array down to its nesting */
5728 for (; array_dims; array_dims--)
5729 resolved_type = build_java_array_type (resolved_type, -1);
5730 resolved_type_decl = TYPE_NAME (resolved_type);
5732 TREE_TYPE (class_type) = resolved_type;
5733 return resolved_type_decl;
5736 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5737 are used to report error messages. Do not try to replace TYPE_NAME
5738 (class_type) by a variable, since it is changed by
5739 find_in_imports{_on_demand} and (but it doesn't really matter)
5740 qualify_and_find. */
5743 do_resolve_class (enclosing, class_type, decl, cl)
5744 tree enclosing, class_type, decl, cl;
5746 tree new_class_decl = NULL_TREE, super = NULL_TREE;
5747 tree saved_enclosing_type = enclosing ? TREE_TYPE (enclosing) : NULL_TREE;
5748 struct hash_table _ht, *circularity_hash = &_ht;
5750 /* This hash table is used to register the classes we're going
5751 through when searching the current class as an inner class, in
5752 order to detect circular references. Remember to free it before
5753 returning the section 0- of this function. */
5754 hash_table_init (circularity_hash, hash_newfunc,
5755 java_hash_hash_tree_node, java_hash_compare_tree_node);
5757 /* 0- Search in the current class as an inner class.
5758 Maybe some code here should be added to load the class or
5759 something, at least if the class isn't an inner class and ended
5760 being loaded from class file. FIXME. */
5763 new_class_decl = resolve_inner_class (circularity_hash, cl, &enclosing,
5764 &super, class_type);
5768 /* If we haven't found anything because SUPER reached Object and
5769 ENCLOSING happens to be an innerclass, try the enclosing context. */
5770 if ((!super || super == object_type_node) &&
5771 enclosing && INNER_CLASS_DECL_P (enclosing))
5772 enclosing = DECL_CONTEXT (enclosing);
5774 enclosing = NULL_TREE;
5777 hash_table_free (circularity_hash);
5780 return new_class_decl;
5782 /* 1- Check for the type in single imports. This will change
5783 TYPE_NAME() if something relevant is found */
5784 find_in_imports (saved_enclosing_type, class_type);
5786 /* 2- And check for the type in the current compilation unit */
5787 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5789 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5790 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5791 load_class (TYPE_NAME (class_type), 0);
5792 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5795 /* 3- Search according to the current package definition */
5796 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5798 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5799 TYPE_NAME (class_type))))
5800 return new_class_decl;
5803 /* 4- Check the import on demands. Don't allow bar.baz to be
5804 imported from foo.* */
5805 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5806 if (find_in_imports_on_demand (saved_enclosing_type, class_type))
5809 /* If found in find_in_imports_on_demant, the type has already been
5811 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5812 return new_class_decl;
5814 /* 5- Try with a name qualified with the package name we've seen so far */
5815 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5819 /* If there is a current package (ctxp->package), it's the first
5820 element of package_list and we can skip it. */
5821 for (package = (ctxp->package ?
5822 TREE_CHAIN (package_list) : package_list);
5823 package; package = TREE_CHAIN (package))
5824 if ((new_class_decl = qualify_and_find (class_type,
5825 TREE_PURPOSE (package),
5826 TYPE_NAME (class_type))))
5827 return new_class_decl;
5830 /* 5- Check an other compilation unit that bears the name of type */
5831 load_class (TYPE_NAME (class_type), 0);
5834 cl = lookup_cl (decl);
5836 /* If we don't have a value for CL, then we're being called recursively.
5837 We can't check package access just yet, but it will be taken care of
5841 if (check_pkg_class_access (TYPE_NAME (class_type), cl, true))
5845 /* 6- Last call for a resolution */
5846 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5850 qualify_and_find (class_type, package, name)
5851 tree class_type, package, name;
5853 tree new_qualified = merge_qualified_name (package, name);
5854 tree new_class_decl;
5856 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5857 load_class (new_qualified, 0);
5858 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5860 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5861 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5862 load_class (new_qualified, 0);
5863 TYPE_NAME (class_type) = new_qualified;
5864 return IDENTIFIER_CLASS_VALUE (new_qualified);
5869 /* Resolve NAME and lay it out (if not done and if not the current
5870 parsed class). Return a decl node. This function is meant to be
5871 called when type resolution is necessary during the walk pass. */
5874 resolve_and_layout (something, cl)
5878 tree decl, decl_type;
5880 /* Don't do that on the current class */
5881 if (something == current_class)
5882 return TYPE_NAME (current_class);
5884 /* Don't do anything for void and other primitive types */
5885 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5888 /* Pointer types can be reall pointer types or fake pointers. When
5889 finding a real pointer, recheck for primitive types */
5890 if (TREE_CODE (something) == POINTER_TYPE)
5892 if (TREE_TYPE (something))
5894 something = TREE_TYPE (something);
5895 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5899 something = TYPE_NAME (something);
5902 /* Don't do anything for arrays of primitive types */
5903 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5904 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5907 /* Something might be a WFL */
5908 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5909 something = EXPR_WFL_NODE (something);
5911 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5912 TYPE_DECL or a real TYPE */
5913 else if (TREE_CODE (something) != IDENTIFIER_NODE)
5914 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5915 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5917 if (!(decl = resolve_no_layout (something, cl)))
5920 /* Resolve and layout if necessary */
5921 decl_type = TREE_TYPE (decl);
5922 layout_class_methods (decl_type);
5924 if (CLASS_FROM_SOURCE_P (decl_type))
5925 java_check_methods (decl);
5926 /* Layout the type if necessary */
5927 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5928 safe_layout_class (decl_type);
5933 /* Resolve a class, returns its decl but doesn't perform any
5934 layout. The current parsing context is saved and restored */
5937 resolve_no_layout (name, cl)
5941 BUILD_PTR_FROM_NAME (ptr, name);
5942 java_parser_context_save_global ();
5943 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5944 java_parser_context_restore_global ();
5949 /* Called when reporting errors. Skip the '[]'s in a complex array
5950 type description that failed to be resolved. purify_type_name can't
5951 use an identifier tree. */
5954 purify_type_name (name)
5957 int len = strlen (name);
5960 STRING_STRIP_BRACKETS (name, len, bracket_found);
5963 char *stripped_name = xmemdup (name, len, len+1);
5964 stripped_name [len] = '\0';
5965 return stripped_name;
5970 /* The type CURRENT refers to can't be found. We print error messages. */
5973 complete_class_report_errors (dep)
5978 if (!JDEP_WFL (dep))
5981 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
5982 switch (JDEP_KIND (dep))
5986 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
5987 purify_type_name (name),
5988 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5992 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
5993 purify_type_name (name),
5994 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5996 case JDEP_METHOD: /* Covers arguments */
5998 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
5999 purify_type_name (name),
6000 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
6001 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
6003 case JDEP_METHOD_RETURN: /* Covers return type */
6005 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
6006 purify_type_name (name),
6007 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
6009 case JDEP_INTERFACE:
6011 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
6012 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
6013 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
6014 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6018 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
6019 purify_type_name (IDENTIFIER_POINTER
6020 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
6021 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
6023 case JDEP_EXCEPTION: /* As specified by `throws' */
6025 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
6026 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
6029 /* Fix for -Wall. Just break doing nothing. The error will be
6035 /* Return a static string containing the DECL prototype string. If
6036 DECL is a constructor, use the class name instead of the form
6040 get_printable_method_name (decl)
6043 const char *to_return;
6044 tree name = NULL_TREE;
6046 if (DECL_CONSTRUCTOR_P (decl))
6048 name = DECL_NAME (decl);
6049 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
6052 to_return = lang_printable_name (decl, 0);
6053 if (DECL_CONSTRUCTOR_P (decl))
6054 DECL_NAME (decl) = name;
6059 /* Track method being redefined inside the same class. As a side
6060 effect, set DECL_NAME to an IDENTIFIER (prior entering this
6061 function it's a FWL, so we can track errors more accurately.) */
6064 check_method_redefinition (class, method)
6069 /* There's no need to verify <clinit> and finit$ and instinit$ */
6070 if (DECL_CLINIT_P (method)
6071 || DECL_FINIT_P (method) || DECL_INSTINIT_P (method))
6074 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
6075 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
6077 if (redef == method)
6079 if (DECL_NAME (redef) == DECL_NAME (method)
6080 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
6081 && !DECL_ARTIFICIAL (method))
6084 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
6085 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
6086 get_printable_method_name (redef));
6093 /* Return 1 if check went ok, 0 otherwise. */
6095 check_abstract_method_definitions (do_interface, class_decl, type)
6097 tree class_decl, type;
6099 tree class = TREE_TYPE (class_decl);
6100 tree method, end_type;
6103 end_type = (do_interface ? object_type_node : type);
6104 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
6106 tree other_super, other_method, method_sig, method_name;
6108 int end_type_reached = 0;
6110 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
6113 /* Now verify that somewhere in between TYPE and CLASS,
6114 abstract method METHOD gets a non abstract definition
6115 that is inherited by CLASS. */
6117 method_sig = build_java_signature (TREE_TYPE (method));
6118 method_name = DECL_NAME (method);
6119 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
6120 method_name = EXPR_WFL_NODE (method_name);
6122 other_super = class;
6124 if (other_super == end_type)
6125 end_type_reached = 1;
6128 for (other_method = TYPE_METHODS (other_super); other_method;
6129 other_method = TREE_CHAIN (other_method))
6131 tree s = build_java_signature (TREE_TYPE (other_method));
6132 tree other_name = DECL_NAME (other_method);
6134 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
6135 other_name = EXPR_WFL_NODE (other_name);
6136 if (!DECL_CLINIT_P (other_method)
6137 && !DECL_CONSTRUCTOR_P (other_method)
6138 && method_name == other_name
6140 && !METHOD_ABSTRACT (other_method))
6146 other_super = CLASSTYPE_SUPER (other_super);
6147 } while (!end_type_reached);
6149 /* Report that abstract METHOD didn't find an implementation
6150 that CLASS can use. */
6153 char *t = xstrdup (lang_printable_name
6154 (TREE_TYPE (TREE_TYPE (method)), 0));
6155 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
6158 (lookup_cl (class_decl),
6159 "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",
6160 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6161 t, lang_printable_name (method, 0),
6162 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
6163 "interface" : "class"),
6164 IDENTIFIER_POINTER (ccn),
6165 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
6166 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
6172 if (ok && do_interface)
6174 /* Check for implemented interfaces. */
6176 tree vector = TYPE_BINFO_BASETYPES (type);
6177 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
6179 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6180 ok = check_abstract_method_definitions (1, class_decl, super);
6187 /* Check that CLASS_DECL somehow implements all inherited abstract
6191 java_check_abstract_method_definitions (class_decl)
6194 tree class = TREE_TYPE (class_decl);
6198 if (CLASS_ABSTRACT (class_decl))
6201 /* Check for inherited types */
6204 super = CLASSTYPE_SUPER (super);
6205 check_abstract_method_definitions (0, class_decl, super);
6206 } while (super != object_type_node);
6208 /* Check for implemented interfaces. */
6209 vector = TYPE_BINFO_BASETYPES (class);
6210 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
6212 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6213 check_abstract_method_definitions (1, class_decl, super);
6217 /* Check all the types method DECL uses and return 1 if all of them
6218 are now complete, 0 otherwise. This is used to check whether its
6219 safe to build a method signature or not. */
6222 check_method_types_complete (decl)
6225 tree type = TREE_TYPE (decl);
6228 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6231 args = TYPE_ARG_TYPES (type);
6232 if (TREE_CODE (type) == METHOD_TYPE)
6233 args = TREE_CHAIN (args);
6234 for (; args != end_params_node; args = TREE_CHAIN (args))
6235 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6241 /* Visible interface to check methods contained in CLASS_DECL */
6244 java_check_methods (class_decl)
6247 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6250 if (CLASS_INTERFACE (class_decl))
6251 java_check_abstract_methods (class_decl);
6253 java_check_regular_methods (class_decl);
6255 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6258 /* Check all the methods of CLASS_DECL. Methods are first completed
6259 then checked according to regular method existence rules. If no
6260 constructor for CLASS_DECL were encountered, then build its
6264 java_check_regular_methods (class_decl)
6267 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6269 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
6270 tree found = NULL_TREE;
6273 /* It is not necessary to check methods defined in java.lang.Object */
6274 if (class == object_type_node)
6277 if (!TYPE_NVIRTUALS (class))
6278 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6280 /* Should take interfaces into account. FIXME */
6281 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6284 tree method_wfl = DECL_FUNCTION_WFL (method);
6287 /* Check for redefinitions */
6288 if (check_method_redefinition (class, method))
6291 /* If we see one constructor a mark so we don't generate the
6292 default one. Also skip other verifications: constructors
6293 can't be inherited hence hiden or overriden */
6294 if (DECL_CONSTRUCTOR_P (method))
6296 saw_constructor = 1;
6300 /* We verify things thrown by the method. They must inherits from
6301 java.lang.Throwable */
6302 for (mthrows = DECL_FUNCTION_THROWS (method);
6303 mthrows; mthrows = TREE_CHAIN (mthrows))
6305 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6307 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6309 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6312 sig = build_java_argument_signature (TREE_TYPE (method));
6313 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6315 /* Inner class can't declare static methods */
6316 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6318 char *t = xstrdup (lang_printable_name (class, 0));
6320 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6321 lang_printable_name (method, 0), t);
6325 /* Nothing overrides or it's a private method. */
6328 if (METHOD_PRIVATE (found))
6334 /* If `found' is declared in an interface, make sure the
6335 modifier matches. */
6336 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6337 && clinit_identifier_node != DECL_NAME (found)
6338 && !METHOD_PUBLIC (method))
6340 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6341 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6342 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6343 lang_printable_name (method, 0),
6344 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6347 /* Can't override a method with the same name and different return
6349 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6352 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6355 "Method `%s' was defined with return type `%s' in class `%s'",
6356 lang_printable_name (found, 0), t,
6358 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6362 aflags = get_access_flags_from_decl (found);
6364 /* Can't override final. Can't override static. */
6365 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6367 /* Static *can* override static */
6368 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6372 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6373 (METHOD_FINAL (found) ? "Final" : "Static"),
6374 lang_printable_name (found, 0),
6375 (METHOD_FINAL (found) ? "final" : "static"),
6377 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6381 /* Static method can't override instance method. */
6382 if (METHOD_STATIC (method))
6386 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6387 lang_printable_name (found, 0),
6389 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6393 /* - Overriding/hiding public must be public
6394 - Overriding/hiding protected must be protected or public
6395 - If the overriden or hidden method has default (package)
6396 access, then the overriding or hiding method must not be
6397 private; otherwise, a compile-time error occurs. If
6398 `found' belongs to an interface, things have been already
6400 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6401 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6402 || (METHOD_PROTECTED (found)
6403 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6404 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6405 && METHOD_PRIVATE (method))))
6409 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6410 (METHOD_PUBLIC (method) ? "public" :
6411 (METHOD_PRIVATE (method) ? "private" : "protected")),
6412 IDENTIFIER_POINTER (DECL_NAME
6413 (TYPE_NAME (DECL_CONTEXT (found)))));
6417 /* Overriding methods must have compatible `throws' clauses on checked
6418 exceptions, if any */
6419 check_throws_clauses (method, method_wfl, found);
6421 /* Inheriting multiple methods with the same signature. FIXME */
6424 if (!TYPE_NVIRTUALS (class))
6425 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6427 /* Search for inherited abstract method not yet implemented in this
6429 java_check_abstract_method_definitions (class_decl);
6431 if (!saw_constructor)
6435 /* Return a non zero value if the `throws' clause of METHOD (if any)
6436 is incompatible with the `throws' clause of FOUND (if any). */
6439 check_throws_clauses (method, method_wfl, found)
6440 tree method, method_wfl, found;
6442 tree mthrows, fthrows;
6444 /* Can't check these things with class loaded from bytecode. FIXME */
6445 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6448 for (mthrows = DECL_FUNCTION_THROWS (method);
6449 mthrows; mthrows = TREE_CHAIN (mthrows))
6451 /* We don't verify unchecked expressions */
6452 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6454 /* Checked expression must be compatible */
6455 for (fthrows = DECL_FUNCTION_THROWS (found);
6456 fthrows; fthrows = TREE_CHAIN (fthrows))
6457 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6462 (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'",
6463 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6464 lang_printable_name (found, 0),
6466 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6471 /* Check abstract method of interface INTERFACE */
6474 java_check_abstract_methods (interface_decl)
6475 tree interface_decl;
6478 tree method, basetype_vec, found;
6479 tree interface = TREE_TYPE (interface_decl);
6481 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6483 /* 2- Check for double definition inside the defining interface */
6484 if (check_method_redefinition (interface, method))
6487 /* 3- Overriding is OK as far as we preserve the return type and
6488 the thrown exceptions (FIXME) */
6489 found = lookup_java_interface_method2 (interface, method);
6493 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6495 (DECL_FUNCTION_WFL (found),
6496 "Method `%s' was defined with return type `%s' in class `%s'",
6497 lang_printable_name (found, 0), t,
6499 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6505 /* 4- Inherited methods can't differ by their returned types */
6506 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6508 n = TREE_VEC_LENGTH (basetype_vec);
6509 for (i = 0; i < n; i++)
6511 tree sub_interface_method, sub_interface;
6512 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6515 sub_interface = BINFO_TYPE (vec_elt);
6516 for (sub_interface_method = TYPE_METHODS (sub_interface);
6517 sub_interface_method;
6518 sub_interface_method = TREE_CHAIN (sub_interface_method))
6520 found = lookup_java_interface_method2 (interface,
6521 sub_interface_method);
6522 if (found && (found != sub_interface_method))
6525 (lookup_cl (sub_interface_method),
6526 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6527 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6528 lang_printable_name (found, 0),
6530 (DECL_NAME (TYPE_NAME
6531 (DECL_CONTEXT (sub_interface_method)))),
6533 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6539 /* Lookup methods in interfaces using their name and partial
6540 signature. Return a matching method only if their types differ. */
6543 lookup_java_interface_method2 (class, method_decl)
6544 tree class, method_decl;
6547 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6552 n = TREE_VEC_LENGTH (basetype_vec);
6553 for (i = 0; i < n; i++)
6555 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6556 if ((BINFO_TYPE (vec_elt) != object_type_node)
6558 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6561 for (i = 0; i < n; i++)
6563 to_return = lookup_java_interface_method2
6564 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6572 /* Lookup method using their name and partial signature. Return a
6573 matching method only if their types differ. */
6576 lookup_java_method2 (clas, method_decl, do_interface)
6577 tree clas, method_decl;
6580 tree method, method_signature, method_name, method_type, name;
6582 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6583 name = DECL_NAME (method_decl);
6584 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6585 EXPR_WFL_NODE (name) : name);
6586 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6588 while (clas != NULL_TREE)
6590 for (method = TYPE_METHODS (clas);
6591 method != NULL_TREE; method = TREE_CHAIN (method))
6593 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6594 tree name = DECL_NAME (method);
6595 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6596 EXPR_WFL_NODE (name) : name) == method_name
6597 && method_sig == method_signature
6598 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6601 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6606 /* Return the line that matches DECL line number, and try its best to
6607 position the column number. Used during error reports. */
6613 static tree cl = NULL_TREE;
6619 if (cl == NULL_TREE)
6621 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6622 ggc_add_tree_root (&cl, 1);
6625 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6626 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6628 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
6629 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6631 found = strstr ((const char *)line,
6632 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6634 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6639 /* Look for a simple name in the single-type import list */
6642 find_name_in_single_imports (name)
6647 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6648 if (TREE_VALUE (node) == name)
6649 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6654 /* Process all single-type import. */
6662 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6664 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6665 char *original_name;
6667 obstack_grow0 (&temporary_obstack,
6668 IDENTIFIER_POINTER (to_be_found),
6669 IDENTIFIER_LENGTH (to_be_found));
6670 original_name = obstack_finish (&temporary_obstack);
6672 /* Don't load twice something already defined. */
6673 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6680 QUALIFIED_P (to_be_found) = 1;
6681 load_class (to_be_found, 0);
6683 check_pkg_class_access (to_be_found, TREE_PURPOSE (import), true);
6685 /* We found it, we can bail out */
6686 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6689 /* We haven't found it. Maybe we're trying to access an
6690 inner class. The only way for us to know is to try again
6691 after having dropped a qualifier. If we can't break it further,
6692 we have an error. */
6693 if (breakdown_qualified (&left, NULL, to_be_found))
6698 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6700 parse_error_context (TREE_PURPOSE (import),
6701 "Class or interface `%s' not found in import",
6706 obstack_free (&temporary_obstack, original_name);
6713 /* Possibly find and mark a class imported by a single-type import
6717 find_in_imports (enclosing_type, class_type)
6718 tree enclosing_type;
6721 tree import = (enclosing_type ? TYPE_IMPORT_LIST (enclosing_type) :
6725 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6727 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6728 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6731 import = TREE_CHAIN (import);
6736 note_possible_classname (name, len)
6741 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6743 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6747 node = ident_subst (name, len, "", '/', '.', "");
6748 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6749 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6753 /* Read a import directory, gathering potential match for further type
6754 references. Indifferently reads a filesystem or a ZIP archive
6758 read_import_dir (wfl)
6761 tree package_id = EXPR_WFL_NODE (wfl);
6762 const char *package_name = IDENTIFIER_POINTER (package_id);
6763 int package_length = IDENTIFIER_LENGTH (package_id);
6765 JCF *saved_jcf = current_jcf;
6770 struct buffer filename[1];
6772 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6774 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6776 BUFFER_INIT (filename);
6777 buffer_grow (filename, package_length + 100);
6779 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6781 const char *entry_name = jcf_path_name (entry);
6782 int entry_length = strlen (entry_name);
6783 if (jcf_path_is_zipfile (entry))
6786 buffer_grow (filename, entry_length);
6787 memcpy (filename->data, entry_name, entry_length - 1);
6788 filename->data[entry_length-1] = '\0';
6789 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6791 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6794 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6795 BUFFER_RESET (filename);
6796 for (k = 0; k < package_length; k++)
6798 char ch = package_name[k];
6799 *filename->ptr++ = ch == '.' ? '/' : ch;
6801 *filename->ptr++ = '/';
6803 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
6805 const char *current_entry = ZIPDIR_FILENAME (zipd);
6806 int current_entry_len = zipd->filename_length;
6808 if (current_entry_len >= BUFFER_LENGTH (filename)
6809 && strncmp (filename->data, current_entry,
6810 BUFFER_LENGTH (filename)) != 0)
6812 found |= note_possible_classname (current_entry,
6819 BUFFER_RESET (filename);
6820 buffer_grow (filename, entry_length + package_length + 4);
6821 strcpy (filename->data, entry_name);
6822 filename->ptr = filename->data + entry_length;
6823 for (k = 0; k < package_length; k++)
6825 char ch = package_name[k];
6826 *filename->ptr++ = ch == '.' ? '/' : ch;
6828 *filename->ptr = '\0';
6830 dirp = opendir (filename->data);
6833 *filename->ptr++ = '/';
6838 struct dirent *direntp = readdir (dirp);
6841 d_name = direntp->d_name;
6842 len = strlen (direntp->d_name);
6843 buffer_grow (filename, len+1);
6844 strcpy (filename->ptr, d_name);
6845 found |= note_possible_classname (filename->data + entry_length,
6846 package_length+len+1);
6853 free (filename->data);
6855 /* Here we should have a unified way of retrieving an entry, to be
6859 static int first = 1;
6862 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives", package_name);
6867 parse_error_context (wfl, "Package `%s' not found in import",
6869 current_jcf = saved_jcf;
6872 current_jcf = saved_jcf;
6875 /* Possibly find a type in the import on demands specified
6876 types. Returns 1 if an error occurred, 0 otherwise. Run through the
6877 entire list, to detected potential double definitions. */
6880 find_in_imports_on_demand (enclosing_type, class_type)
6881 tree enclosing_type;
6884 tree class_type_name = TYPE_NAME (class_type);
6885 tree import = (enclosing_type ? TYPE_IMPORT_DEMAND_LIST (enclosing_type) :
6886 ctxp->import_demand_list);
6887 tree cl = NULL_TREE;
6888 int seen_once = -1; /* -1 when not set, 1 if seen once, >1 otherwise. */
6889 int to_return = -1; /* -1 when not set, 0 or 1 otherwise */
6892 for (; import; import = TREE_CHAIN (import))
6894 int saved_lineno = lineno;
6896 const char *id_name;
6897 tree decl, type_name_copy;
6899 obstack_grow (&temporary_obstack,
6900 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6901 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6902 obstack_1grow (&temporary_obstack, '.');
6903 obstack_grow0 (&temporary_obstack,
6904 IDENTIFIER_POINTER (class_type_name),
6905 IDENTIFIER_LENGTH (class_type_name));
6906 id_name = obstack_finish (&temporary_obstack);
6908 if (! (node = maybe_get_identifier (id_name)))
6911 /* Setup lineno so that it refers to the line of the import (in
6912 case we parse a class file and encounter errors */
6913 lineno = EXPR_WFL_LINENO (TREE_PURPOSE (import));
6915 type_name_copy = TYPE_NAME (class_type);
6916 TYPE_NAME (class_type) = node;
6917 QUALIFIED_P (node) = 1;
6918 decl = IDENTIFIER_CLASS_VALUE (node);
6920 /* If there is no DECL set for the class or if the class isn't
6921 loaded and not seen in source yet, then load */
6922 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6923 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6925 load_class (node, 0);
6926 decl = IDENTIFIER_CLASS_VALUE (node);
6928 if (decl && ! INNER_CLASS_P (TREE_TYPE (decl)))
6929 access_check = check_pkg_class_access (node, TREE_PURPOSE (import),
6932 /* 6.6.1: Inner classes are subject to member access rules. */
6935 lineno = saved_lineno;
6937 /* If the loaded class is not accessible or couldn't be loaded,
6938 we restore the original TYPE_NAME and process the next
6940 if (access_check || !decl)
6942 TYPE_NAME (class_type) = type_name_copy;
6946 /* If the loaded class is accessible, we keep a tab on it to
6947 detect and report multiple inclusions. */
6948 if (IS_A_CLASSFILE_NAME (node))
6952 cl = TREE_PURPOSE (import);
6955 else if (seen_once >= 0)
6957 tree location = (cl ? cl : TREE_PURPOSE (import));
6958 tree package = (cl ? EXPR_WFL_NODE (cl) :
6959 EXPR_WFL_NODE (TREE_PURPOSE (import)));
6963 "Type `%s' also potentially defined in package `%s'",
6964 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6965 IDENTIFIER_POINTER (package));
6968 to_return = access_check;
6974 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6977 /* Add package NAME to the list of package encountered so far. To
6978 speed up class lookup in do_resolve_class, we make sure a
6979 particular package is added only once. */
6982 register_package (name)
6985 static struct hash_table _pht, *pht = NULL;
6989 hash_table_init (&_pht, hash_newfunc,
6990 java_hash_hash_tree_node, java_hash_compare_tree_node);
6994 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6996 package_list = chainon (package_list, build_tree_list (name, NULL));
6997 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
7002 resolve_package (pkg, next)
7006 tree type_name = NULL_TREE;
7007 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
7009 /* The trick is to determine when the package name stops and were
7010 the name of something contained in the package starts. Then we
7011 return a fully qualified name of what we want to get. */
7013 /* Do a quick search on well known package names */
7014 if (!strncmp (name, "java.lang.reflect", 17))
7017 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
7018 type_name = lookup_package_type (name, 17);
7020 else if (!strncmp (name, "java.lang", 9))
7022 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
7023 type_name = lookup_package_type (name, 9);
7026 /* If we found something here, return */
7030 *next = EXPR_WFL_QUALIFICATION (pkg);
7032 /* Try to progressively construct a type name */
7033 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
7034 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
7035 current; current = TREE_CHAIN (current))
7037 /* If we don't have what we're expecting, exit now. TYPE_NAME
7038 will be null and the error caught later. */
7039 if (TREE_CODE (QUAL_WFL (current)) != EXPR_WITH_FILE_LOCATION)
7041 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
7042 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
7045 /* resolve_package should be used in a loop, hence we
7046 point at this one to naturally process the next one at
7047 the next iteration. */
7056 lookup_package_type (name, from)
7061 const char *sub = &name[from+1];
7062 while (*sub != '.' && *sub)
7064 strncpy (subname, name, sub-name);
7065 subname [sub-name] = '\0';
7066 return get_identifier (subname);
7069 /* Check accessibility of inner classes according to member access rules.
7070 DECL is the inner class, ENCLOSING_DECL is the class from which the
7071 access is being attempted. */
7074 check_inner_class_access (decl, enclosing_decl, cl)
7075 tree decl, enclosing_decl, cl;
7078 tree enclosing_decl_type;
7080 /* We don't issue an error message when CL is null. CL can be null
7081 as a result of processing a JDEP crafted by source_start_java_method
7082 for the purpose of patching its parm decl. But the error would
7083 have been already trapped when fixing the method's signature.
7084 DECL can also be NULL in case of earlier errors. */
7088 enclosing_decl_type = TREE_TYPE (enclosing_decl);
7090 if (CLASS_PRIVATE (decl))
7092 /* Access is permitted only within the body of the top-level
7093 class in which DECL is declared. */
7094 tree top_level = decl;
7095 while (DECL_CONTEXT (top_level))
7096 top_level = DECL_CONTEXT (top_level);
7097 while (DECL_CONTEXT (enclosing_decl))
7098 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7099 if (top_level == enclosing_decl)
7103 else if (CLASS_PROTECTED (decl))
7106 /* Access is permitted from within the same package... */
7107 if (in_same_package (decl, enclosing_decl))
7110 /* ... or from within the body of a subtype of the context in which
7111 DECL is declared. */
7112 decl_context = DECL_CONTEXT (decl);
7113 while (enclosing_decl)
7115 if (CLASS_INTERFACE (decl))
7117 if (interface_of_p (TREE_TYPE (decl_context),
7118 enclosing_decl_type))
7123 /* Eww. The order of the arguments is different!! */
7124 if (inherits_from_p (enclosing_decl_type,
7125 TREE_TYPE (decl_context)))
7128 enclosing_decl = DECL_CONTEXT (enclosing_decl);
7130 access = "protected";
7132 else if (! CLASS_PUBLIC (decl))
7134 /* Access is permitted only from within the same package as DECL. */
7135 if (in_same_package (decl, enclosing_decl))
7137 access = "non-public";
7140 /* Class is public. */
7143 parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
7144 (CLASS_INTERFACE (decl) ? "interface" : "class"),
7145 lang_printable_name (decl, 0), access);
7148 /* Accessibility check for top-level classes. If CLASS_NAME is in a
7149 foreign package, it must be PUBLIC. Return 0 if no access
7150 violations were found, 1 otherwise. If VERBOSE is true and an error
7151 was found, it is reported and accounted for. */
7154 check_pkg_class_access (class_name, cl, verbose)
7161 if (!IDENTIFIER_CLASS_VALUE (class_name))
7164 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
7167 if (!CLASS_PUBLIC (TYPE_NAME (type)))
7169 /* Access to a private class within the same package is
7172 breakdown_qualified (&l, &r, class_name);
7173 if (!QUALIFIED_P (class_name) && !ctxp->package)
7174 /* Both in the empty package. */
7176 if (l == ctxp->package)
7177 /* Both in the same package. */
7182 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
7183 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
7184 IDENTIFIER_POINTER (class_name));
7190 /* Local variable declaration. */
7193 declare_local_variables (modifier, type, vlist)
7198 tree decl, current, saved_type;
7199 tree type_wfl = NULL_TREE;
7203 /* Push a new block if statements were seen between the last time we
7204 pushed a block and now. Keep a count of blocks to close */
7205 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
7207 tree b = enter_block ();
7208 BLOCK_IS_IMPLICIT (b) = 1;
7214 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
7215 if (modifier == ACC_FINAL)
7220 (ctxp->modifier_ctx [i],
7221 "Only `final' is allowed as a local variables modifier");
7226 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
7227 hold the TYPE value if a new incomplete has to be created (as
7228 opposed to being found already existing and reused). */
7229 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
7231 /* If TYPE is fully resolved and we don't have a reference, make one */
7232 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7234 /* Go through all the declared variables */
7235 for (current = vlist, saved_type = type; current;
7236 current = TREE_CHAIN (current), type = saved_type)
7238 tree other, real_type;
7239 tree wfl = TREE_PURPOSE (current);
7240 tree name = EXPR_WFL_NODE (wfl);
7241 tree init = TREE_VALUE (current);
7243 /* Process NAME, as it may specify extra dimension(s) for it */
7244 type = build_array_from_name (type, type_wfl, name, &name);
7246 /* Variable redefinition check */
7247 if ((other = lookup_name_in_blocks (name)))
7249 variable_redefinition_error (wfl, name, TREE_TYPE (other),
7250 DECL_SOURCE_LINE (other));
7254 /* Type adjustment. We may have just readjusted TYPE because
7255 the variable specified more dimensions. Make sure we have
7256 a reference if we can and don't have one already. */
7257 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7259 real_type = GET_REAL_TYPE (type);
7260 /* Never layout this decl. This will be done when its scope
7262 decl = build_decl (VAR_DECL, name, real_type);
7263 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7264 DECL_FINAL (decl) = final_p;
7265 BLOCK_CHAIN_DECL (decl);
7267 /* If doing xreferencing, replace the line number with the WFL
7270 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7272 /* Don't try to use an INIT statement when an error was found */
7273 if (init && java_error_count)
7276 /* Add the initialization function to the current function's code */
7279 /* Name might have been readjusted */
7280 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7281 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7282 java_method_add_stmt (current_function_decl,
7283 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7287 /* Setup dependency the type of the decl */
7291 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7292 dep = CLASSD_LAST (ctxp->classd_list);
7293 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7296 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7299 /* Called during parsing. Build decls from argument list. */
7302 source_start_java_method (fndecl)
7312 current_function_decl = fndecl;
7314 /* New scope for the function */
7316 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7317 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7319 tree type = TREE_VALUE (tem);
7320 tree name = TREE_PURPOSE (tem);
7322 /* If type is incomplete. Create an incomplete decl and ask for
7323 the decl to be patched later */
7324 if (INCOMPLETE_TYPE_P (type))
7327 tree real_type = GET_REAL_TYPE (type);
7328 parm_decl = build_decl (PARM_DECL, name, real_type);
7329 type = obtain_incomplete_type (type);
7330 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7331 jdep = CLASSD_LAST (ctxp->classd_list);
7332 JDEP_MISC (jdep) = name;
7333 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7336 parm_decl = build_decl (PARM_DECL, name, type);
7338 /* Remember if a local variable was declared final (via its
7339 TREE_LIST of type/name.) Set DECL_FINAL accordingly. */
7340 if (ARG_FINAL_P (tem))
7342 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7343 DECL_FINAL (parm_decl) = 1;
7346 BLOCK_CHAIN_DECL (parm_decl);
7348 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7349 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7351 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7352 DECL_MAX_LOCALS (current_function_decl) = i;
7355 /* Called during parsing. Creates an artificial method declaration. */
7358 create_artificial_method (class, flags, type, name, args)
7361 tree type, name, args;
7365 java_parser_context_save_global ();
7367 mdecl = make_node (FUNCTION_TYPE);
7368 TREE_TYPE (mdecl) = type;
7369 TYPE_ARG_TYPES (mdecl) = args;
7370 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7371 java_parser_context_restore_global ();
7372 DECL_ARTIFICIAL (mdecl) = 1;
7376 /* Starts the body if an artificial method. */
7379 start_artificial_method_body (mdecl)
7382 DECL_SOURCE_LINE (mdecl) = 1;
7383 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7384 source_start_java_method (mdecl);
7389 end_artificial_method_body (mdecl)
7392 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7393 It has to be evaluated first. (if mdecl is current_function_decl,
7394 we have an undefined behavior if no temporary variable is used.) */
7395 tree b = exit_block ();
7396 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7400 /* Dump a tree of some kind. This is a convenience wrapper for the
7401 dump_* functions in tree-dump.c. */
7403 dump_java_tree (phase, t)
7404 enum tree_dump_index phase;
7410 stream = dump_begin (phase, &flags);
7413 dump_node (t, flags, stream);
7414 dump_end (phase, stream);
7418 /* Terminate a function and expand its body. */
7421 source_end_java_method ()
7423 tree fndecl = current_function_decl;
7428 java_parser_context_save_global ();
7429 lineno = ctxp->last_ccb_indent1;
7431 /* Turn function bodies with only a NOP expr null, so they don't get
7432 generated at all and we won't get warnings when using the -W
7434 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7435 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7437 /* We've generated all the trees for this function, and it has been
7438 patched. Dump it to a file if the user requested it. */
7439 dump_java_tree (TDI_original, fndecl);
7441 /* Generate function's code */
7442 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7443 && ! flag_emit_class_files
7444 && ! flag_emit_xref)
7445 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7447 /* pop out of its parameters */
7448 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7450 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7452 /* Generate rtl for function exit. */
7453 if (! flag_emit_class_files && ! flag_emit_xref)
7455 lineno = DECL_SOURCE_LINE_LAST (fndecl);
7456 expand_function_end (input_filename, lineno, 0);
7458 /* Run the optimizers and output assembler code for this function. */
7459 rest_of_compilation (fndecl);
7462 current_function_decl = NULL_TREE;
7463 java_parser_context_restore_global ();
7466 /* Record EXPR in the current function block. Complements compound
7467 expression second operand if necessary. */
7470 java_method_add_stmt (fndecl, expr)
7473 if (!GET_CURRENT_BLOCK (fndecl))
7475 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7479 add_stmt_to_block (b, type, stmt)
7482 tree body = BLOCK_EXPR_BODY (b), c;
7484 if (java_error_count)
7487 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7490 BLOCK_EXPR_BODY (b) = c;
7491 TREE_SIDE_EFFECTS (c) = 1;
7495 /* Add STMT to EXISTING if possible, otherwise create a new
7496 COMPOUND_EXPR and add STMT to it. */
7499 add_stmt_to_compound (existing, type, stmt)
7500 tree existing, type, stmt;
7503 return build (COMPOUND_EXPR, type, existing, stmt);
7508 void java_layout_seen_class_methods ()
7510 tree previous_list = all_class_list;
7511 tree end = NULL_TREE;
7516 for (current = previous_list;
7517 current != end; current = TREE_CHAIN (current))
7518 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7520 if (previous_list != all_class_list)
7522 end = previous_list;
7523 previous_list = all_class_list;
7531 java_reorder_fields ()
7533 static tree stop_reordering = NULL_TREE;
7534 static int initialized_p;
7537 /* Register STOP_REORDERING with the garbage collector. */
7540 ggc_add_tree_root (&stop_reordering, 1);
7544 for (current = gclass_list; current; current = TREE_CHAIN (current))
7546 current_class = TREE_TYPE (TREE_VALUE (current));
7548 if (current_class == stop_reordering)
7551 /* Reverse the fields, but leave the dummy field in front.
7552 Fields are already ordered for Object and Class */
7553 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7554 && current_class != class_type_node)
7556 /* If the dummy field is there, reverse the right fields and
7557 just layout the type for proper fields offset */
7558 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7560 tree fields = TYPE_FIELDS (current_class);
7561 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7562 TYPE_SIZE (current_class) = NULL_TREE;
7564 /* We don't have a dummy field, we need to layout the class,
7565 after having reversed the fields */
7568 TYPE_FIELDS (current_class) =
7569 nreverse (TYPE_FIELDS (current_class));
7570 TYPE_SIZE (current_class) = NULL_TREE;
7574 /* There are cases were gclass_list will be empty. */
7576 stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7579 /* Layout the methods of all classes loaded in one way or another.
7580 Check methods of source parsed classes. Then reorder the
7581 fields and layout the classes or the type of all source parsed
7585 java_layout_classes ()
7588 int save_error_count = java_error_count;
7590 /* Layout the methods of all classes seen so far */
7591 java_layout_seen_class_methods ();
7592 java_parse_abort_on_error ();
7593 all_class_list = NULL_TREE;
7595 /* Then check the methods of all parsed classes */
7596 for (current = gclass_list; current; current = TREE_CHAIN (current))
7597 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7598 java_check_methods (TREE_VALUE (current));
7599 java_parse_abort_on_error ();
7601 for (current = gclass_list; current; current = TREE_CHAIN (current))
7603 current_class = TREE_TYPE (TREE_VALUE (current));
7604 layout_class (current_class);
7606 /* Error reported by the caller */
7607 if (java_error_count)
7611 /* We might have reloaded classes durign the process of laying out
7612 classes for code generation. We must layout the methods of those
7613 late additions, as constructor checks might use them */
7614 java_layout_seen_class_methods ();
7615 java_parse_abort_on_error ();
7618 /* Expand methods in the current set of classes rememebered for
7622 java_complete_expand_classes ()
7626 do_not_fold = flag_emit_xref;
7628 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7629 if (!INNER_CLASS_DECL_P (current))
7630 java_complete_expand_class (current);
7633 /* Expand the methods found in OUTER, starting first by OUTER's inner
7637 java_complete_expand_class (outer)
7642 set_nested_class_simple_name_value (outer, 1); /* Set */
7644 /* We need to go after all inner classes and start expanding them,
7645 starting with most nested ones. We have to do that because nested
7646 classes might add functions to outer classes */
7648 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7649 inner_list; inner_list = TREE_CHAIN (inner_list))
7650 java_complete_expand_class (TREE_PURPOSE (inner_list));
7652 java_complete_expand_methods (outer);
7653 set_nested_class_simple_name_value (outer, 0); /* Reset */
7656 /* Expand methods registered in CLASS_DECL. The general idea is that
7657 we expand regular methods first. This allows us get an estimate on
7658 how outer context local alias fields are really used so we can add
7659 to the constructor just enough code to initialize them properly (it
7660 also lets us generate finit$ correctly.) Then we expand the
7661 constructors and then <clinit>. */
7664 java_complete_expand_methods (class_decl)
7667 tree clinit, decl, first_decl;
7669 current_class = TREE_TYPE (class_decl);
7671 /* Initialize a new constant pool */
7672 init_outgoing_cpool ();
7674 /* Pre-expand <clinit> to figure whether we really need it or
7675 not. If we do need it, we pre-expand the static fields so they're
7676 ready to be used somewhere else. <clinit> will be fully expanded
7677 after we processed the constructors. */
7678 first_decl = TYPE_METHODS (current_class);
7679 clinit = maybe_generate_pre_expand_clinit (current_class);
7681 /* Then generate finit$ (if we need to) because constructors will
7683 if (TYPE_FINIT_STMT_LIST (current_class))
7684 java_complete_expand_method (generate_finit (current_class));
7686 /* Then generate instinit$ (if we need to) because constructors will
7688 if (TYPE_II_STMT_LIST (current_class))
7689 java_complete_expand_method (generate_instinit (current_class));
7691 /* Now do the constructors */
7692 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7696 if (!DECL_CONSTRUCTOR_P (decl))
7699 no_body = !DECL_FUNCTION_BODY (decl);
7700 /* Don't generate debug info on line zero when expanding a
7701 generated constructor. */
7703 restore_line_number_status (1);
7705 java_complete_expand_method (decl);
7708 restore_line_number_status (0);
7711 /* First, do the ordinary methods. */
7712 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7714 /* Ctors aren't part of this batch. */
7715 if (DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7718 /* Skip abstract or native methods -- but do handle native
7719 methods when generating JNI stubs. */
7720 if (METHOD_ABSTRACT (decl) || (! flag_jni && METHOD_NATIVE (decl)))
7722 DECL_FUNCTION_BODY (decl) = NULL_TREE;
7726 if (METHOD_NATIVE (decl))
7729 current_function_decl = decl;
7730 body = build_jni_stub (decl);
7731 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7734 java_complete_expand_method (decl);
7737 /* If there is indeed a <clinit>, fully expand it now */
7740 /* Prevent the use of `this' inside <clinit> */
7741 ctxp->explicit_constructor_p = 1;
7742 java_complete_expand_method (clinit);
7743 ctxp->explicit_constructor_p = 0;
7746 /* We might have generated a class$ that we now want to expand */
7747 if (TYPE_DOT_CLASS (current_class))
7748 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7750 /* Now verify constructor circularity (stop after the first one we
7752 if (!CLASS_INTERFACE (class_decl))
7753 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7754 if (DECL_CONSTRUCTOR_P (decl)
7755 && verify_constructor_circularity (decl, decl))
7758 /* Save the constant pool. We'll need to restore it later. */
7759 TYPE_CPOOL (current_class) = outgoing_cpool;
7762 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7763 safely used in some other methods/constructors. */
7766 maybe_generate_pre_expand_clinit (class_type)
7769 tree current, mdecl;
7771 if (!TYPE_CLINIT_STMT_LIST (class_type))
7774 /* Go through all static fields and pre expand them */
7775 for (current = TYPE_FIELDS (class_type); current;
7776 current = TREE_CHAIN (current))
7777 if (FIELD_STATIC (current))
7778 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7780 /* Then build the <clinit> method */
7781 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7782 clinit_identifier_node, end_params_node);
7783 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7785 start_artificial_method_body (mdecl);
7787 /* We process the list of assignment we produced as the result of
7788 the declaration of initialized static field and add them as
7789 statement to the <clinit> method. */
7790 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7791 current = TREE_CHAIN (current))
7793 tree stmt = current;
7794 /* We build the assignment expression that will initialize the
7795 field to its value. There are strict rules on static
7796 initializers (8.5). FIXME */
7797 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7798 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7799 java_method_add_stmt (mdecl, stmt);
7802 end_artificial_method_body (mdecl);
7804 /* Now we want to place <clinit> as the last method (because we need
7805 it at least for interface so that it doesn't interfere with the
7806 dispatch table based lookup. */
7807 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7809 current = TREE_CHAIN (TYPE_METHODS (class_type));
7810 TYPE_METHODS (class_type) = current;
7812 while (TREE_CHAIN (current))
7813 current = TREE_CHAIN (current);
7815 TREE_CHAIN (current) = mdecl;
7816 TREE_CHAIN (mdecl) = NULL_TREE;
7822 /* Analyzes a method body and look for something that isn't a
7823 MODIFY_EXPR with a constant value. */
7826 analyze_clinit_body (this_class, bbody)
7827 tree this_class, bbody;
7830 switch (TREE_CODE (bbody))
7833 bbody = BLOCK_EXPR_BODY (bbody);
7836 case EXPR_WITH_FILE_LOCATION:
7837 bbody = EXPR_WFL_NODE (bbody);
7841 if (analyze_clinit_body (this_class, TREE_OPERAND (bbody, 0)))
7843 bbody = TREE_OPERAND (bbody, 1);
7847 /* If we're generating to class file and we're dealing with an
7848 array initialization, we return 1 to keep <clinit> */
7849 if (TREE_CODE (TREE_OPERAND (bbody, 1)) == NEW_ARRAY_INIT
7850 && flag_emit_class_files)
7853 /* There are a few cases where we're required to keep
7855 - If this is an assignment whose operand is not constant,
7856 - If this is an assignment to a non-initialized field,
7857 - If this field is not a member of the current class.
7859 return (! TREE_CONSTANT (TREE_OPERAND (bbody, 1))
7860 || ! DECL_INITIAL (TREE_OPERAND (bbody, 0))
7861 || DECL_CONTEXT (TREE_OPERAND (bbody, 0)) != this_class);
7870 /* See whether we could get rid of <clinit>. Criteria are: all static
7871 final fields have constant initial values and the body of <clinit>
7872 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7875 maybe_yank_clinit (mdecl)
7881 if (!DECL_CLINIT_P (mdecl))
7884 /* If the body isn't empty, then we keep <clinit>. Note that if
7885 we're emitting classfiles, this isn't enough not to rule it
7887 fbody = DECL_FUNCTION_BODY (mdecl);
7888 bbody = BLOCK_EXPR_BODY (fbody);
7889 if (bbody && bbody != error_mark_node)
7890 bbody = BLOCK_EXPR_BODY (bbody);
7893 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7896 type = DECL_CONTEXT (mdecl);
7897 current = TYPE_FIELDS (type);
7899 for (current = (current ? TREE_CHAIN (current) : current);
7900 current; current = TREE_CHAIN (current))
7904 /* We're not interested in non-static fields. */
7905 if (!FIELD_STATIC (current))
7908 /* Nor in fields without initializers. */
7909 f_init = DECL_INITIAL (current);
7910 if (f_init == NULL_TREE)
7913 /* Anything that isn't String or a basic type is ruled out -- or
7914 if we know how to deal with it (when doing things natively) we
7915 should generated an empty <clinit> so that SUID are computed
7917 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7918 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7921 if (! FIELD_FINAL (current) || ! TREE_CONSTANT (f_init))
7925 /* Now we analyze the method body and look for something that
7926 isn't a MODIFY_EXPR */
7927 if (bbody != empty_stmt_node && analyze_clinit_body (type, bbody))
7930 /* Get rid of <clinit> in the class' list of methods */
7931 if (TYPE_METHODS (type) == mdecl)
7932 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7934 for (current = TYPE_METHODS (type); current;
7935 current = TREE_CHAIN (current))
7936 if (TREE_CHAIN (current) == mdecl)
7938 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7945 /* Install the argument from MDECL. Suitable to completion and
7946 expansion of mdecl's body. */
7949 start_complete_expand_method (mdecl)
7954 pushlevel (1); /* Prepare for a parameter push */
7955 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7956 DECL_ARGUMENTS (mdecl) = tem;
7958 for (; tem; tem = TREE_CHAIN (tem))
7960 /* TREE_CHAIN (tem) will change after pushdecl. */
7961 tree next = TREE_CHAIN (tem);
7962 tree type = TREE_TYPE (tem);
7963 if (PROMOTE_PROTOTYPES
7964 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7965 && INTEGRAL_TYPE_P (type))
7966 type = integer_type_node;
7967 DECL_ARG_TYPE (tem) = type;
7968 layout_decl (tem, 0);
7970 /* Re-install the next so that the list is kept and the loop
7972 TREE_CHAIN (tem) = next;
7974 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7975 lineno = DECL_SOURCE_LINE_FIRST (mdecl);
7976 build_result_decl (mdecl);
7980 /* Complete and expand a method. */
7983 java_complete_expand_method (mdecl)
7986 tree fbody, block_body, exception_copy;
7988 current_function_decl = mdecl;
7989 /* Fix constructors before expanding them */
7990 if (DECL_CONSTRUCTOR_P (mdecl))
7991 fix_constructors (mdecl);
7993 /* Expand functions that have a body */
7994 if (!DECL_FUNCTION_BODY (mdecl))
7997 fbody = DECL_FUNCTION_BODY (mdecl);
7998 block_body = BLOCK_EXPR_BODY (fbody);
7999 exception_copy = NULL_TREE;
8001 current_function_decl = mdecl;
8004 fprintf (stderr, " [%s.",
8005 lang_printable_name (DECL_CONTEXT (mdecl), 0));
8006 announce_function (mdecl);
8008 fprintf (stderr, "]");
8010 /* Prepare the function for tree completion */
8011 start_complete_expand_method (mdecl);
8013 /* Install the current this */
8014 current_this = (!METHOD_STATIC (mdecl) ?
8015 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
8017 /* Purge the `throws' list of unchecked exceptions (we save a copy
8018 of the list and re-install it later.) */
8019 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
8020 purge_unchecked_exceptions (mdecl);
8022 /* Install exceptions thrown with `throws' */
8023 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
8025 if (block_body != NULL_TREE)
8027 block_body = java_complete_tree (block_body);
8029 /* Before we check initialization, attached all class initialization
8030 variable to the block_body */
8031 hash_traverse (&DECL_FUNCTION_INIT_TEST_TABLE (mdecl),
8032 attach_init_test_initialization_flags, block_body);
8034 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
8036 check_for_initialization (block_body, mdecl);
8038 /* Go through all the flags marking the initialization of
8039 static variables and see whether they're definitively
8040 assigned, in which case the type is remembered as
8041 definitively initialized in MDECL. */
8042 if (STATIC_CLASS_INIT_OPT_P ())
8044 /* Always register the context as properly initialized in
8045 MDECL. This used with caution helps removing extra
8046 initialization of self. */
8047 if (METHOD_STATIC (mdecl))
8048 hash_lookup (&DECL_FUNCTION_INITIALIZED_CLASS_TABLE (mdecl),
8049 (hash_table_key) DECL_CONTEXT (mdecl),
8053 ctxp->explicit_constructor_p = 0;
8056 BLOCK_EXPR_BODY (fbody) = block_body;
8058 /* If we saw a return but couldn't evaluate it properly, we'll have
8059 an error_mark_node here. */
8060 if (block_body != error_mark_node
8061 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
8062 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
8064 missing_return_error (current_function_decl);
8066 /* See if we can get rid of <clinit> if MDECL happens to be <clinit> */
8067 maybe_yank_clinit (mdecl);
8069 /* Pop the current level, with special measures if we found errors. */
8070 if (java_error_count)
8071 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
8074 /* Pop the exceptions and sanity check */
8076 if (currently_caught_type_list)
8079 /* Restore the copy of the list of exceptions if emitting xrefs. */
8080 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
8083 /* For with each class for which there's code to generate. */
8086 java_expand_method_bodies (class)
8090 for (decl = TYPE_METHODS (class); decl; decl = TREE_CHAIN (decl))
8092 if (!DECL_FUNCTION_BODY (decl))
8095 current_function_decl = decl;
8097 /* It's time to assign the variable flagging static class
8098 initialization based on which classes invoked static methods
8099 are definitely initializing. This should be flagged. */
8100 if (STATIC_CLASS_INIT_OPT_P ())
8102 tree list = DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (decl);
8103 for (; list != NULL_TREE; list = TREE_CHAIN (list))
8105 /* Executed for each statement calling a static function.
8106 LIST is a TREE_LIST whose PURPOSE is the called function
8107 and VALUE is a compound whose second operand can be patched
8108 with static class initialization flag assignments. */
8110 tree called_method = TREE_PURPOSE (list);
8111 tree compound = TREE_VALUE (list);
8112 tree assignment_compound_list
8113 = build_tree_list (called_method, NULL);
8115 /* For each class definitely initialized in
8116 CALLED_METHOD, fill ASSIGNMENT_COMPOUND with
8117 assignment to the class initialization flag. */
8118 hash_traverse (&DECL_FUNCTION_INITIALIZED_CLASS_TABLE (called_method),
8119 emit_test_initialization,
8120 assignment_compound_list);
8122 if (TREE_VALUE (assignment_compound_list))
8123 TREE_OPERAND (compound, 1)
8124 = TREE_VALUE (assignment_compound_list);
8128 /* Prepare the function for RTL expansion */
8129 start_complete_expand_method (decl);
8131 /* Expand function start, generate initialization flag
8132 assignment, and handle synchronized methods. */
8133 complete_start_java_method (decl);
8135 /* Expand the rest of the function body and terminate
8137 source_end_java_method ();
8143 /* This section of the code deals with accessing enclosing context
8144 fields either directly by using the relevant access to this$<n> or
8145 by invoking an access method crafted for that purpose. */
8147 /* Build the necessary access from an inner class to an outer
8148 class. This routine could be optimized to cache previous result
8149 (decl, current_class and returned access). When an access method
8150 needs to be generated, it always takes the form of a read. It might
8151 be later turned into a write by calling outer_field_access_fix. */
8154 build_outer_field_access (id, decl)
8157 tree access = NULL_TREE;
8158 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
8159 tree decl_ctx = DECL_CONTEXT (decl);
8161 /* If the immediate enclosing context of the current class is the
8162 field decl's class or inherits from it; build the access as
8163 `this$<n>.<field>'. Note that we will break the `private' barrier
8164 if we're not emitting bytecodes. */
8165 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
8166 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
8168 tree thisn = build_current_thisn (current_class);
8169 access = make_qualified_primary (build_wfl_node (thisn),
8170 id, EXPR_WFL_LINECOL (id));
8172 /* Otherwise, generate access methods to outer this and access the
8173 field (either using an access method or by direct access.) */
8176 int lc = EXPR_WFL_LINECOL (id);
8178 /* Now we chain the required number of calls to the access$0 to
8179 get a hold to the enclosing instance we need, and then we
8180 build the field access. */
8181 access = build_access_to_thisn (current_class, decl_ctx, lc);
8183 /* If the field is private and we're generating bytecode, then
8184 we generate an access method */
8185 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
8187 tree name = build_outer_field_access_methods (decl);
8188 access = build_outer_field_access_expr (lc, decl_ctx,
8189 name, access, NULL_TREE);
8191 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
8192 Once again we break the `private' access rule from a foreign
8195 access = make_qualified_primary (access, id, lc);
8197 return resolve_expression_name (access, NULL);
8200 /* Return a non zero value if NODE describes an outer field inner
8204 outer_field_access_p (type, decl)
8207 if (!INNER_CLASS_TYPE_P (type)
8208 || TREE_CODE (decl) != FIELD_DECL
8209 || DECL_CONTEXT (decl) == type)
8212 /* If the inner class extends the declaration context of the field
8213 we're try to acces, then this isn't an outer field access */
8214 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8217 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
8218 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
8220 if (type == DECL_CONTEXT (decl))
8223 if (!DECL_CONTEXT (TYPE_NAME (type)))
8225 /* Before we give up, see whether the field is inherited from
8226 the enclosing context we're considering. */
8227 if (inherits_from_p (type, DECL_CONTEXT (decl)))
8236 /* Return a non zero value if NODE represents an outer field inner
8237 access that was been already expanded. As a side effect, it returns
8238 the name of the field being accessed and the argument passed to the
8239 access function, suitable for a regeneration of the access method
8240 call if necessary. */
8243 outer_field_expanded_access_p (node, name, arg_type, arg)
8244 tree node, *name, *arg_type, *arg;
8248 if (TREE_CODE (node) != CALL_EXPR)
8251 /* Well, gcj generates slightly different tree nodes when compiling
8252 to native or bytecodes. It's the case for function calls. */
8254 if (flag_emit_class_files
8255 && TREE_CODE (node) == CALL_EXPR
8256 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
8258 else if (!flag_emit_class_files)
8260 node = TREE_OPERAND (node, 0);
8262 if (node && TREE_OPERAND (node, 0)
8263 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
8265 node = TREE_OPERAND (node, 0);
8266 if (TREE_OPERAND (node, 0)
8267 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
8268 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
8269 (DECL_NAME (TREE_OPERAND (node, 0)))))
8274 if (identified && name && arg_type && arg)
8276 tree argument = TREE_OPERAND (node, 1);
8277 *name = DECL_NAME (TREE_OPERAND (node, 0));
8278 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
8279 *arg = TREE_VALUE (argument);
8284 /* Detect in NODE an outer field read access from an inner class and
8285 transform it into a write with RHS as an argument. This function is
8286 called from the java_complete_lhs when an assignment to a LHS can
8290 outer_field_access_fix (wfl, node, rhs)
8291 tree wfl, node, rhs;
8293 tree name, arg_type, arg;
8295 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
8297 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
8298 arg_type, name, arg, rhs);
8299 return java_complete_tree (node);
8304 /* Construct the expression that calls an access method:
8305 <type>.access$<n>(<arg1> [, <arg2>]);
8307 ARG2 can be NULL and will be omitted in that case. It will denote a
8311 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
8313 tree type, access_method_name, arg1, arg2;
8315 tree args, cn, access;
8317 args = arg1 ? arg1 :
8318 build_wfl_node (build_current_thisn (current_class));
8319 args = build_tree_list (NULL_TREE, args);
8322 args = tree_cons (NULL_TREE, arg2, args);
8324 access = build_method_invocation (build_wfl_node (access_method_name), args);
8325 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
8326 return make_qualified_primary (cn, access, lc);
8330 build_new_access_id ()
8332 static int access_n_counter = 1;
8335 sprintf (buffer, "access$%d", access_n_counter++);
8336 return get_identifier (buffer);
8339 /* Create the static access functions for the outer field DECL. We define a
8341 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8345 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8346 TREE_TYPE (<field>) value$) {
8347 return inst$.field = value$;
8349 We should have a usage flags on the DECL so we can lazily turn the ones
8350 we're using for code generation. FIXME.
8354 build_outer_field_access_methods (decl)
8357 tree id, args, stmt, mdecl;
8359 if (FIELD_INNER_ACCESS_P (decl))
8360 return FIELD_INNER_ACCESS (decl);
8362 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8364 /* Create the identifier and a function named after it. */
8365 id = build_new_access_id ();
8367 /* The identifier is marked as bearing the name of a generated write
8368 access function for outer field accessed from inner classes. */
8369 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8371 /* Create the read access */
8372 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8373 TREE_CHAIN (args) = end_params_node;
8374 stmt = make_qualified_primary (build_wfl_node (inst_id),
8375 build_wfl_node (DECL_NAME (decl)), 0);
8376 stmt = build_return (0, stmt);
8377 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8378 TREE_TYPE (decl), id, args, stmt);
8379 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8381 /* Create the write access method. No write access for final variable */
8382 if (!FIELD_FINAL (decl))
8384 args = build_tree_list (inst_id,
8385 build_pointer_type (DECL_CONTEXT (decl)));
8386 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8387 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8388 stmt = make_qualified_primary (build_wfl_node (inst_id),
8389 build_wfl_node (DECL_NAME (decl)), 0);
8390 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8391 build_wfl_node (wpv_id)));
8392 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8393 TREE_TYPE (decl), id,
8396 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8398 /* Return the access name */
8399 return FIELD_INNER_ACCESS (decl) = id;
8402 /* Build an field access method NAME. */
8405 build_outer_field_access_method (class, type, name, args, body)
8406 tree class, type, name, args, body;
8408 tree saved_current_function_decl, mdecl;
8410 /* Create the method */
8411 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8412 fix_method_argument_names (args, mdecl);
8413 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8415 /* Attach the method body. */
8416 saved_current_function_decl = current_function_decl;
8417 start_artificial_method_body (mdecl);
8418 java_method_add_stmt (mdecl, body);
8419 end_artificial_method_body (mdecl);
8420 current_function_decl = saved_current_function_decl;
8426 /* This section deals with building access function necessary for
8427 certain kinds of method invocation from inner classes. */
8430 build_outer_method_access_method (decl)
8433 tree saved_current_function_decl, mdecl;
8434 tree args = NULL_TREE, call_args = NULL_TREE;
8435 tree carg, id, body, class;
8437 int parm_id_count = 0;
8439 /* Test this abort with an access to a private field */
8440 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8443 /* Check the cache first */
8444 if (DECL_FUNCTION_INNER_ACCESS (decl))
8445 return DECL_FUNCTION_INNER_ACCESS (decl);
8447 class = DECL_CONTEXT (decl);
8449 /* Obtain an access identifier and mark it */
8450 id = build_new_access_id ();
8451 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8453 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8454 /* Create the arguments, as much as the original */
8455 for (; carg && carg != end_params_node;
8456 carg = TREE_CHAIN (carg))
8458 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8459 args = chainon (args, build_tree_list (get_identifier (buffer),
8460 TREE_VALUE (carg)));
8462 args = chainon (args, end_params_node);
8464 /* Create the method */
8465 mdecl = create_artificial_method (class, ACC_STATIC,
8466 TREE_TYPE (TREE_TYPE (decl)), id, args);
8467 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8468 /* There is a potential bug here. We should be able to use
8469 fix_method_argument_names, but then arg names get mixed up and
8470 eventually a constructor will have its this$0 altered and the
8471 outer context won't be assignment properly. The test case is
8473 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8475 /* Attach the method body. */
8476 saved_current_function_decl = current_function_decl;
8477 start_artificial_method_body (mdecl);
8479 /* The actual method invocation uses the same args. When invoking a
8480 static methods that way, we don't want to skip the first
8483 if (!METHOD_STATIC (decl))
8484 carg = TREE_CHAIN (carg);
8485 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8486 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8489 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8491 if (!METHOD_STATIC (decl))
8492 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8494 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8495 body = build_return (0, body);
8496 java_method_add_stmt (mdecl,body);
8497 end_artificial_method_body (mdecl);
8498 current_function_decl = saved_current_function_decl;
8500 /* Back tag the access function so it know what it accesses */
8501 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8503 /* Tag the current method so it knows it has an access generated */
8504 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8508 /* This section of the code deals with building expressions to access
8509 the enclosing instance of an inner class. The enclosing instance is
8510 kept in a generated field called this$<n>, with <n> being the
8511 inner class nesting level (starting from 0.) */
8513 /* Build an access to a given this$<n>, always chaining access call to
8514 others. Access methods to this$<n> are build on the fly if
8515 necessary. This CAN'T be used to solely access this$<n-1> from
8516 this$<n> (which alway yield to special cases and optimization, see
8517 for example build_outer_field_access). */
8520 build_access_to_thisn (from, to, lc)
8524 tree access = NULL_TREE;
8530 access = build_current_thisn (from);
8531 access = build_wfl_node (access);
8535 tree access0_wfl, cn;
8537 maybe_build_thisn_access_method (from);
8538 access0_wfl = build_wfl_node (access0_identifier_node);
8539 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8540 EXPR_WFL_LINECOL (access0_wfl) = lc;
8541 access = build_tree_list (NULL_TREE, access);
8542 access = build_method_invocation (access0_wfl, access);
8543 access = make_qualified_primary (cn, access, lc);
8546 /* if FROM isn't an inter class, that's fine, we've done
8547 enough. What we're looking for can be accessed from there. */
8548 from = DECL_CONTEXT (TYPE_NAME (from));
8551 from = TREE_TYPE (from);
8556 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8557 is returned if nothing needs to be generated. Otherwise, the method
8558 generated and a method decl is returned.
8560 NOTE: These generated methods should be declared in a class file
8561 attribute so that they can't be referred to directly. */
8564 maybe_build_thisn_access_method (type)
8567 tree mdecl, args, stmt, rtype;
8568 tree saved_current_function_decl;
8570 /* If TYPE is a top-level class, no access method is required.
8571 If there already is such an access method, bail out. */
8572 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8575 /* We generate the method. The method looks like:
8576 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8578 args = build_tree_list (inst_id, build_pointer_type (type));
8579 TREE_CHAIN (args) = end_params_node;
8580 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8581 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8582 access0_identifier_node, args);
8583 fix_method_argument_names (args, mdecl);
8584 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8585 stmt = build_current_thisn (type);
8586 stmt = make_qualified_primary (build_wfl_node (inst_id),
8587 build_wfl_node (stmt), 0);
8588 stmt = build_return (0, stmt);
8590 saved_current_function_decl = current_function_decl;
8591 start_artificial_method_body (mdecl);
8592 java_method_add_stmt (mdecl, stmt);
8593 end_artificial_method_body (mdecl);
8594 current_function_decl = saved_current_function_decl;
8596 CLASS_ACCESS0_GENERATED_P (type) = 1;
8601 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8602 the first level of innerclassing. this$1 for the next one, etc...
8603 This function can be invoked with TYPE to NULL, available and then
8604 has to count the parser context. */
8607 build_current_thisn (type)
8610 static int saved_i = -1;
8611 static tree saved_thisn = NULL_TREE;
8612 static tree saved_type = NULL_TREE;
8613 static int saved_type_i = 0;
8614 static int initialized_p;
8619 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8622 ggc_add_tree_root (&saved_thisn, 1);
8623 ggc_add_tree_root (&saved_type, 1);
8629 if (type == saved_type)
8633 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8634 decl; decl = DECL_CONTEXT (decl), i++)
8642 i = list_length (GET_CPC_LIST ())-2;
8647 sprintf (buffer, "this$%d", i);
8649 saved_thisn = get_identifier (buffer);
8653 /* Return the assignement to the hidden enclosing context `this$<n>'
8654 by the second incoming parameter to the innerclass constructor. The
8655 form used is `this.this$<n> = this$<n>;'. */
8658 build_thisn_assign ()
8660 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8662 tree thisn = build_current_thisn (current_class);
8663 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8664 build_wfl_node (thisn), 0);
8665 tree rhs = build_wfl_node (thisn);
8666 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8667 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8673 /* Building the synthetic `class$' used to implement the `.class' 1.1
8674 extension for non primitive types. This method looks like:
8676 static Class class$(String type) throws NoClassDefFoundError
8678 try {return (java.lang.Class.forName (String));}
8679 catch (ClassNotFoundException e) {
8680 throw new NoClassDefFoundError(e.getMessage());}
8684 build_dot_class_method (class)
8687 #define BWF(S) build_wfl_node (get_identifier ((S)))
8688 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8689 tree args, tmp, saved_current_function_decl, mdecl;
8690 tree stmt, throw_stmt;
8692 static tree get_message_wfl, type_parm_wfl;
8694 if (!get_message_wfl)
8696 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8697 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8698 ggc_add_tree_root (&get_message_wfl, 1);
8699 ggc_add_tree_root (&type_parm_wfl, 1);
8702 /* Build the arguments */
8703 args = build_tree_list (get_identifier ("type$"),
8704 build_pointer_type (string_type_node));
8705 TREE_CHAIN (args) = end_params_node;
8707 /* Build the qualified name java.lang.Class.forName */
8708 tmp = MQN (MQN (MQN (BWF ("java"),
8709 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8710 load_class (class_not_found_type_node, 1);
8711 load_class (no_class_def_found_type_node, 1);
8713 /* Create the "class$" function */
8714 mdecl = create_artificial_method (class, ACC_STATIC,
8715 build_pointer_type (class_type_node),
8716 classdollar_identifier_node, args);
8717 DECL_FUNCTION_THROWS (mdecl) =
8718 build_tree_list (NULL_TREE, no_class_def_found_type_node);
8720 /* We start by building the try block. We need to build:
8721 return (java.lang.Class.forName (type)); */
8722 stmt = build_method_invocation (tmp,
8723 build_tree_list (NULL_TREE, type_parm_wfl));
8724 stmt = build_return (0, stmt);
8726 /* Now onto the catch block. We start by building the expression
8727 throwing a new exception: throw new NoClassDefFoundError (_.getMessage) */
8728 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8729 get_message_wfl, 0);
8730 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8732 /* Build new NoClassDefFoundError (_.getMessage) */
8733 throw_stmt = build_new_invocation
8734 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8735 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8737 /* Build the throw, (it's too early to use BUILD_THROW) */
8738 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8740 /* Encapsulate STMT in a try block. The catch clause executes THROW_STMT */
8741 stmt = encapsulate_with_try_catch (0, class_not_found_type_node,
8744 fix_method_argument_names (args, mdecl);
8745 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8746 saved_current_function_decl = current_function_decl;
8747 start_artificial_method_body (mdecl);
8748 java_method_add_stmt (mdecl, stmt);
8749 end_artificial_method_body (mdecl);
8750 current_function_decl = saved_current_function_decl;
8751 TYPE_DOT_CLASS (class) = mdecl;
8757 build_dot_class_method_invocation (type)
8762 if (TYPE_ARRAY_P (type))
8763 sig_id = build_java_signature (type);
8765 sig_id = DECL_NAME (TYPE_NAME (type));
8767 /* Ensure that the proper name separator is used */
8768 sig_id = unmangle_classname (IDENTIFIER_POINTER (sig_id),
8769 IDENTIFIER_LENGTH (sig_id));
8771 s = build_string (IDENTIFIER_LENGTH (sig_id),
8772 IDENTIFIER_POINTER (sig_id));
8773 return build_method_invocation (build_wfl_node (classdollar_identifier_node),
8774 build_tree_list (NULL_TREE, s));
8777 /* This section of the code deals with constructor. */
8779 /* Craft a body for default constructor. Patch existing constructor
8780 bodies with call to super() and field initialization statements if
8784 fix_constructors (mdecl)
8787 tree iii; /* Instance Initializer Invocation */
8788 tree body = DECL_FUNCTION_BODY (mdecl);
8789 tree thisn_assign, compound = NULL_TREE;
8790 tree class_type = DECL_CONTEXT (mdecl);
8792 if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
8794 DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
8798 /* It is an error for the compiler to generate a default
8799 constructor if the superclass doesn't have a constructor that
8800 takes no argument, or the same args for an anonymous class */
8801 if (verify_constructor_super (mdecl))
8803 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8804 tree save = DECL_NAME (mdecl);
8805 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8806 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8808 (lookup_cl (TYPE_NAME (class_type)),
8809 "No constructor matching `%s' found in class `%s'",
8810 lang_printable_name (mdecl, 0), n);
8811 DECL_NAME (mdecl) = save;
8814 /* The constructor body must be crafted by hand. It's the
8815 constructor we defined when we realize we didn't have the
8816 CLASSNAME() constructor */
8817 start_artificial_method_body (mdecl);
8819 /* Insert an assignment to the this$<n> hidden field, if
8821 if ((thisn_assign = build_thisn_assign ()))
8822 java_method_add_stmt (mdecl, thisn_assign);
8824 /* We don't generate a super constructor invocation if we're
8825 compiling java.lang.Object. build_super_invocation takes care
8827 java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8830 if ((iii = build_instinit_invocation (class_type)))
8831 java_method_add_stmt (mdecl, iii);
8833 end_artificial_method_body (mdecl);
8835 /* Search for an explicit constructor invocation */
8839 int invokes_this = 0;
8840 tree found_call = NULL_TREE;
8841 tree main_block = BLOCK_EXPR_BODY (body);
8844 switch (TREE_CODE (body))
8847 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8848 if (CALL_THIS_CONSTRUCTOR_P (body))
8853 case EXPR_WITH_FILE_LOCATION:
8855 body = TREE_OPERAND (body, 0);
8859 body = BLOCK_EXPR_BODY (body);
8866 /* Generate the assignment to this$<n>, if necessary */
8867 if ((thisn_assign = build_thisn_assign ()))
8868 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8870 /* The constructor is missing an invocation of super() */
8872 compound = add_stmt_to_compound (compound, NULL_TREE,
8873 build_super_invocation (mdecl));
8874 /* Explicit super() invokation should take place before the
8875 instance initializer blocks. */
8878 compound = add_stmt_to_compound (compound, NULL_TREE,
8879 TREE_OPERAND (found_call, 0));
8880 TREE_OPERAND (found_call, 0) = empty_stmt_node;
8883 DECL_INIT_CALLS_THIS (mdecl) = invokes_this;
8885 /* Insert the instance initializer block right after. */
8886 if (!invokes_this && (iii = build_instinit_invocation (class_type)))
8887 compound = add_stmt_to_compound (compound, NULL_TREE, iii);
8889 /* Fix the constructor main block if we're adding extra stmts */
8892 compound = add_stmt_to_compound (compound, NULL_TREE,
8893 BLOCK_EXPR_BODY (main_block));
8894 BLOCK_EXPR_BODY (main_block) = compound;
8899 /* Browse constructors in the super class, searching for a constructor
8900 that doesn't take any argument. Return 0 if one is found, 1
8901 otherwise. If the current class is an anonymous inner class, look
8902 for something that has the same signature. */
8905 verify_constructor_super (mdecl)
8908 tree class = CLASSTYPE_SUPER (current_class);
8909 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8915 if (ANONYMOUS_CLASS_P (current_class))
8917 tree mdecl_arg_type;
8918 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8919 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8920 if (DECL_CONSTRUCTOR_P (sdecl))
8923 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8925 arg_type = TREE_CHAIN (arg_type);
8926 for (m_arg_type = mdecl_arg_type;
8927 (arg_type != end_params_node
8928 && m_arg_type != end_params_node);
8929 arg_type = TREE_CHAIN (arg_type),
8930 m_arg_type = TREE_CHAIN (m_arg_type))
8931 if (!valid_method_invocation_conversion_p
8932 (TREE_VALUE (arg_type),
8933 TREE_VALUE (m_arg_type)))
8936 if (arg_type == end_params_node && m_arg_type == end_params_node)
8942 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8944 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8946 arg = TREE_CHAIN (arg);
8947 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8954 /* Generate code for all context remembered for code generation. */
8957 java_expand_classes ()
8959 int save_error_count = 0;
8960 static struct parser_ctxt *cur_ctxp = NULL;
8962 java_parse_abort_on_error ();
8963 if (!(ctxp = ctxp_for_generation))
8965 java_layout_classes ();
8966 java_parse_abort_on_error ();
8968 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
8971 input_filename = ctxp->filename;
8972 lang_init_source (2); /* Error msgs have method prototypes */
8973 java_complete_expand_classes (); /* Complete and expand classes */
8974 java_parse_abort_on_error ();
8976 input_filename = main_input_filename;
8978 /* Find anonymous classes and expand their constructor, now they
8980 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
8984 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8986 current_class = TREE_TYPE (current);
8987 if (ANONYMOUS_CLASS_P (current_class))
8990 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8992 if (DECL_CONSTRUCTOR_P (d))
8994 restore_line_number_status (1);
8995 java_complete_expand_method (d);
8996 restore_line_number_status (0);
8997 break; /* We now there are no other ones */
9004 /* If we've found error at that stage, don't try to generate
9005 anything, unless we're emitting xrefs or checking the syntax only
9006 (but not using -fsyntax-only for the purpose of generating
9008 if (java_error_count && !flag_emit_xref
9009 && (!flag_syntax_only && !flag_emit_class_files))
9012 /* Now things are stable, go for generation of the class data. */
9014 /* We pessimistically marked all fields external until we knew
9015 what set of classes we were planning to compile. Now mark
9016 those that will be generated locally as not external. */
9017 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9021 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9023 tree class = TREE_TYPE (current);
9025 for (field = TYPE_FIELDS (class); field ; field = TREE_CHAIN (field))
9026 if (FIELD_STATIC (field))
9027 DECL_EXTERNAL (field) = 0;
9031 /* Compile the classes. */
9032 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
9036 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
9038 current_class = TREE_TYPE (current);
9039 outgoing_cpool = TYPE_CPOOL (current_class);
9040 if (flag_emit_class_files)
9041 write_classfile (current_class);
9043 expand_xref (current_class);
9044 else if (! flag_syntax_only)
9046 java_expand_method_bodies (current_class);
9053 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
9054 a tree list node containing RIGHT. Fore coming RIGHTs will be
9055 chained to this hook. LOCATION contains the location of the
9056 separating `.' operator. */
9059 make_qualified_primary (primary, right, location)
9060 tree primary, right;
9065 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
9066 wfl = build_wfl_wrap (primary, location);
9070 /* If wfl wasn't qualified, we build a first anchor */
9071 if (!EXPR_WFL_QUALIFICATION (wfl))
9072 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
9075 /* And chain them */
9076 EXPR_WFL_LINECOL (right) = location;
9077 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
9078 PRIMARY_P (wfl) = 1;
9082 /* Simple merge of two name separated by a `.' */
9085 merge_qualified_name (left, right)
9089 if (!left && !right)
9098 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
9099 IDENTIFIER_LENGTH (left));
9100 obstack_1grow (&temporary_obstack, '.');
9101 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
9102 IDENTIFIER_LENGTH (right));
9103 node = get_identifier (obstack_base (&temporary_obstack));
9104 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
9105 QUALIFIED_P (node) = 1;
9109 /* Merge the two parts of a qualified name into LEFT. Set the
9110 location information of the resulting node to LOCATION, usually
9111 inherited from the location information of the `.' operator. */
9114 make_qualified_name (left, right, location)
9118 #ifdef USE_COMPONENT_REF
9119 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
9120 EXPR_WFL_LINECOL (node) = location;
9123 tree left_id = EXPR_WFL_NODE (left);
9124 tree right_id = EXPR_WFL_NODE (right);
9127 merge = merge_qualified_name (left_id, right_id);
9129 /* Left wasn't qualified and is now qualified */
9130 if (!QUALIFIED_P (left_id))
9132 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
9133 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
9134 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
9137 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
9138 EXPR_WFL_LINECOL (wfl) = location;
9139 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
9141 EXPR_WFL_NODE (left) = merge;
9146 /* Extract the last identifier component of the qualified in WFL. The
9147 last identifier is removed from the linked list */
9150 cut_identifier_in_qualified (wfl)
9154 tree previous = NULL_TREE;
9155 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
9156 if (!TREE_CHAIN (q))
9159 /* Operating on a non qualified qualified WFL. */
9162 TREE_CHAIN (previous) = NULL_TREE;
9163 return TREE_PURPOSE (q);
9167 /* Resolve the expression name NAME. Return its decl. */
9170 resolve_expression_name (id, orig)
9174 tree name = EXPR_WFL_NODE (id);
9177 /* 6.5.5.1: Simple expression names */
9178 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
9180 /* 15.13.1: NAME can appear within the scope of a local variable
9182 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
9185 /* 15.13.1: NAME can appear within a class declaration */
9188 decl = lookup_field_wrapper (current_class, name);
9191 tree access = NULL_TREE;
9192 int fs = FIELD_STATIC (decl);
9194 /* If we're accessing an outer scope local alias, make
9195 sure we change the name of the field we're going to
9197 if (FIELD_LOCAL_ALIAS_USED (decl))
9198 name = DECL_NAME (decl);
9200 /* Instance variable (8.3.1.1) can't appear within
9201 static method, static initializer or initializer for
9202 a static variable. */
9203 if (!fs && METHOD_STATIC (current_function_decl))
9205 static_ref_err (id, name, current_class);
9206 return error_mark_node;
9208 /* Instance variables can't appear as an argument of
9209 an explicit constructor invocation */
9210 if (!fs && ctxp->explicit_constructor_p
9211 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
9214 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
9215 return error_mark_node;
9218 /* If we're processing an inner class and we're trying
9219 to access a field belonging to an outer class, build
9220 the access to the field */
9221 if (!fs && outer_field_access_p (current_class, decl))
9223 if (CLASS_STATIC (TYPE_NAME (current_class)))
9225 static_ref_err (id, DECL_NAME (decl), current_class);
9226 return error_mark_node;
9228 access = build_outer_field_access (id, decl);
9234 /* Otherwise build what it takes to access the field */
9235 access = build_field_ref ((fs ? NULL_TREE : current_this),
9236 DECL_CONTEXT (decl), name);
9238 access = maybe_build_class_init_for_field (decl, access);
9239 /* We may be asked to save the real field access node */
9242 /* And we return what we got */
9245 /* Fall down to error report on undefined variable */
9248 /* 6.5.5.2 Qualified Expression Names */
9253 qualify_ambiguous_name (id);
9254 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
9255 /* 15.10.2: Accessing Superclass Members using super */
9256 return resolve_field_access (id, orig, NULL);
9259 /* We've got an error here */
9260 if (INNER_CLASS_TYPE_P (current_class))
9261 parse_error_context (id,
9262 "Local variable `%s' can't be accessed from within the inner class `%s' unless it is declared final",
9263 IDENTIFIER_POINTER (name),
9264 IDENTIFIER_POINTER (DECL_NAME
9265 (TYPE_NAME (current_class))));
9267 parse_error_context (id, "Undefined variable `%s'",
9268 IDENTIFIER_POINTER (name));
9270 return error_mark_node;
9274 static_ref_err (wfl, field_id, class_type)
9275 tree wfl, field_id, class_type;
9279 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
9280 IDENTIFIER_POINTER (field_id),
9281 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
9284 /* 15.10.1 Field Access Using a Primary and/or Expression Name.
9285 We return something suitable to generate the field access. We also
9286 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
9287 recipient's address can be null. */
9290 resolve_field_access (qual_wfl, field_decl, field_type)
9292 tree *field_decl, *field_type;
9296 tree decl, where_found, type_found;
9298 if (resolve_qualified_expression_name (qual_wfl, &decl,
9299 &where_found, &type_found))
9300 return error_mark_node;
9302 /* Resolve the LENGTH field of an array here */
9303 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
9304 && type_found && TYPE_ARRAY_P (type_found)
9305 && ! flag_emit_class_files && ! flag_emit_xref)
9307 tree length = build_java_array_length_access (where_found);
9310 /* In case we're dealing with a static array, we need to
9311 initialize its class before the array length can be fetched.
9312 It's also a good time to create a DECL_RTL for the field if
9313 none already exists, otherwise if the field was declared in a
9314 class found in an external file and hasn't been (and won't
9315 be) accessed for its value, none will be created. */
9316 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
9318 build_static_field_ref (where_found);
9319 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
9322 /* We might have been trying to resolve field.method(). In which
9323 case, the resolution is over and decl is the answer */
9324 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
9326 else if (JDECL_P (decl))
9329 type_found = DECL_CONTEXT (decl);
9330 is_static = FIELD_STATIC (decl);
9331 field_ref = build_field_ref ((is_static && !flag_emit_xref?
9332 NULL_TREE : where_found),
9333 type_found, DECL_NAME (decl));
9334 if (field_ref == error_mark_node)
9335 return error_mark_node;
9337 field_ref = maybe_build_class_init_for_field (decl, field_ref);
9345 *field_type = (QUAL_DECL_TYPE (decl) ?
9346 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9350 /* If NODE is an access to f static field, strip out the class
9351 initialization part and return the field decl, otherwise, return
9355 strip_out_static_field_access_decl (node)
9358 if (TREE_CODE (node) == COMPOUND_EXPR)
9360 tree op1 = TREE_OPERAND (node, 1);
9361 if (TREE_CODE (op1) == COMPOUND_EXPR)
9363 tree call = TREE_OPERAND (op1, 0);
9364 if (TREE_CODE (call) == CALL_EXPR
9365 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9366 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9367 == soft_initclass_node)
9368 return TREE_OPERAND (op1, 1);
9370 else if (JDECL_P (op1))
9376 /* 6.5.5.2: Qualified Expression Names */
9379 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9381 tree *found_decl, *type_found, *where_found;
9383 int from_type = 0; /* Field search initiated from a type */
9384 int from_super = 0, from_cast = 0, from_qualified_this = 0;
9385 int previous_call_static = 0;
9387 tree decl = NULL_TREE, type = NULL_TREE, q;
9388 /* For certain for of inner class instantiation */
9389 tree saved_current, saved_this;
9390 #define RESTORE_THIS_AND_CURRENT_CLASS \
9391 { current_class = saved_current; current_this = saved_this;}
9393 *type_found = *where_found = NULL_TREE;
9395 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9397 tree qual_wfl = QUAL_WFL (q);
9398 tree ret_decl; /* for EH checking */
9399 int location; /* for EH checking */
9401 /* 15.10.1 Field Access Using a Primary */
9402 switch (TREE_CODE (qual_wfl))
9405 case NEW_CLASS_EXPR:
9406 /* If the access to the function call is a non static field,
9407 build the code to access it. */
9408 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9410 decl = maybe_access_field (decl, *where_found,
9411 DECL_CONTEXT (decl));
9412 if (decl == error_mark_node)
9416 /* And code for the function call */
9417 if (complete_function_arguments (qual_wfl))
9420 /* We might have to setup a new current class and a new this
9421 for the search of an inner class, relative to the type of
9422 a expression resolved as `decl'. The current values are
9423 saved and restored shortly after */
9424 saved_current = current_class;
9425 saved_this = current_this;
9427 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9428 || from_qualified_this))
9430 /* If we still have `from_qualified_this', we have the form
9431 <T>.this.f() and we need to build <T>.this */
9432 if (from_qualified_this)
9434 decl = build_access_to_thisn (current_class, type, 0);
9435 decl = java_complete_tree (decl);
9436 type = TREE_TYPE (TREE_TYPE (decl));
9438 current_class = type;
9439 current_this = decl;
9440 from_qualified_this = 0;
9443 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9444 CALL_USING_SUPER (qual_wfl) = 1;
9445 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9446 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9447 *where_found = patch_method_invocation (qual_wfl, decl, type,
9449 &is_static, &ret_decl);
9450 if (*where_found == error_mark_node)
9452 RESTORE_THIS_AND_CURRENT_CLASS;
9455 *type_found = type = QUAL_DECL_TYPE (*where_found);
9457 /* If we're creating an inner class instance, check for that
9458 an enclosing instance is in scope */
9459 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9460 && INNER_ENCLOSING_SCOPE_CHECK (type))
9463 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9464 lang_printable_name (type, 0),
9465 (!current_this ? "" :
9466 "; an explicit one must be provided when creating this inner class"));
9467 RESTORE_THIS_AND_CURRENT_CLASS;
9471 /* In case we had to change then to resolve a inner class
9472 instantiation using a primary qualified by a `new' */
9473 RESTORE_THIS_AND_CURRENT_CLASS;
9475 /* EH check. No check on access$<n> functions */
9477 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9478 (DECL_NAME (current_function_decl)))
9479 check_thrown_exceptions (location, ret_decl);
9481 /* If the previous call was static and this one is too,
9482 build a compound expression to hold the two (because in
9483 that case, previous function calls aren't transported as
9484 forcoming function's argument. */
9485 if (previous_call_static && is_static)
9487 decl = build (COMPOUND_EXPR, TREE_TYPE (*where_found),
9488 decl, *where_found);
9489 TREE_SIDE_EFFECTS (decl) = 1;
9493 previous_call_static = is_static;
9494 decl = *where_found;
9499 case NEW_ARRAY_EXPR:
9500 case NEW_ANONYMOUS_ARRAY_EXPR:
9501 *where_found = decl = java_complete_tree (qual_wfl);
9502 if (decl == error_mark_node)
9504 *type_found = type = QUAL_DECL_TYPE (decl);
9508 *where_found = decl = java_complete_tree (qual_wfl);
9509 if (decl == error_mark_node)
9511 *type_found = type = QUAL_DECL_TYPE (decl);
9515 case CONDITIONAL_EXPR:
9518 *where_found = decl = java_complete_tree (qual_wfl);
9519 if (decl == error_mark_node)
9521 *type_found = type = QUAL_DECL_TYPE (decl);
9525 /* If the access to the function call is a non static field,
9526 build the code to access it. */
9527 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9529 decl = maybe_access_field (decl, *where_found, type);
9530 if (decl == error_mark_node)
9533 /* And code for the array reference expression */
9534 decl = java_complete_tree (qual_wfl);
9535 if (decl == error_mark_node)
9537 type = QUAL_DECL_TYPE (decl);
9541 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9543 if ((type = patch_string (decl)))
9545 *where_found = QUAL_RESOLUTION (q) = decl;
9546 *type_found = type = TREE_TYPE (decl);
9550 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9552 *where_found = QUAL_RESOLUTION (q) = decl;
9553 *type_found = type = TREE_TYPE (decl);
9557 /* Fix for -Wall Just go to the next statement. Don't
9562 /* If we fall here, we weren't processing a (static) function call. */
9563 previous_call_static = 0;
9565 /* It can be the keyword THIS */
9566 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9567 && EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9572 (wfl, "Keyword `this' used outside allowed context");
9575 if (ctxp->explicit_constructor_p
9576 && type == current_class)
9578 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9581 /* We have to generate code for intermediate access */
9582 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9584 *where_found = decl = current_this;
9585 *type_found = type = QUAL_DECL_TYPE (decl);
9587 /* We're trying to access the this from somewhere else. Make sure
9588 it's allowed before doing so. */
9591 if (!enclosing_context_p (type, current_class))
9593 char *p = xstrdup (lang_printable_name (type, 0));
9594 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9596 lang_printable_name (current_class, 0));
9600 from_qualified_this = 1;
9601 /* If there's nothing else after that, we need to
9602 produce something now, otherwise, the section of the
9603 code that needs to produce <T>.this will generate
9604 what is necessary. */
9605 if (!TREE_CHAIN (q))
9607 decl = build_access_to_thisn (current_class, type, 0);
9608 *where_found = decl = java_complete_tree (decl);
9609 *type_found = type = TREE_TYPE (decl);
9617 /* 15.10.2 Accessing Superclass Members using SUPER */
9618 if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION
9619 && EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9622 /* Check on the restricted use of SUPER */
9623 if (METHOD_STATIC (current_function_decl)
9624 || current_class == object_type_node)
9627 (wfl, "Keyword `super' used outside allowed context");
9630 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9631 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9632 CLASSTYPE_SUPER (current_class),
9633 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9634 *where_found = decl = java_complete_tree (node);
9635 if (decl == error_mark_node)
9637 *type_found = type = QUAL_DECL_TYPE (decl);
9638 from_super = from_type = 1;
9642 /* 15.13.1: Can't search for field name in packages, so we
9643 assume a variable/class name was meant. */
9644 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9646 tree name = resolve_package (wfl, &q);
9650 *where_found = decl = resolve_no_layout (name, qual_wfl);
9651 /* We want to be absolutely sure that the class is laid
9652 out. We're going to search something inside it. */
9653 *type_found = type = TREE_TYPE (decl);
9654 layout_class (type);
9657 /* Fix them all the way down, if any are left. */
9660 list = TREE_CHAIN (q);
9663 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9664 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9665 list = TREE_CHAIN (list);
9671 if (from_super || from_cast)
9673 ((from_cast ? qual_wfl : wfl),
9674 "No variable `%s' defined in class `%s'",
9675 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9676 lang_printable_name (type, 0));
9679 (qual_wfl, "Undefined variable or class name: `%s'",
9680 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9685 /* We have a type name. It's been already resolved when the
9686 expression was qualified. */
9687 else if (RESOLVE_TYPE_NAME_P (qual_wfl) && QUAL_RESOLUTION (q))
9689 decl = QUAL_RESOLUTION (q);
9691 /* Sneak preview. If next we see a `new', we're facing a
9692 qualification with resulted in a type being selected
9693 instead of a field. Report the error */
9695 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9697 parse_error_context (qual_wfl, "Undefined variable `%s'",
9698 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9702 if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
9705 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9706 java_accstring_lookup (get_access_flags_from_decl (decl)),
9707 GET_TYPE_NAME (type),
9708 IDENTIFIER_POINTER (DECL_NAME (decl)),
9709 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9712 check_deprecation (qual_wfl, decl);
9714 type = TREE_TYPE (decl);
9717 /* We resolve an expression name */
9720 tree field_decl = NULL_TREE;
9722 /* If there exists an early resolution, use it. That occurs
9723 only once and we know that there are more things to
9724 come. Don't do that when processing something after SUPER
9725 (we need more thing to be put in place below */
9726 if (!from_super && QUAL_RESOLUTION (q))
9728 decl = QUAL_RESOLUTION (q);
9731 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9734 *where_found = current_this;
9737 static_ref_err (qual_wfl, DECL_NAME (decl),
9741 if (outer_field_access_p (current_class, decl))
9742 decl = build_outer_field_access (qual_wfl, decl);
9746 *where_found = TREE_TYPE (decl);
9747 if (TREE_CODE (*where_found) == POINTER_TYPE)
9748 *where_found = TREE_TYPE (*where_found);
9753 /* Report and error if we're using a numerical litteral as a
9754 qualifier. It can only be an INTEGER_CST. */
9755 else if (TREE_CODE (qual_wfl) == INTEGER_CST)
9758 (wfl, "Can't use type `%s' as a qualifier",
9759 lang_printable_name (TREE_TYPE (qual_wfl), 0));
9763 /* We have to search for a field, knowing the type of its
9764 container. The flag FROM_TYPE indicates that we resolved
9765 the last member of the expression as a type name, which
9766 means that for the resolution of this field, we'll look
9767 for other errors than if it was resolved as a member of
9772 tree field_decl_type; /* For layout */
9774 if (!from_type && !JREFERENCE_TYPE_P (type))
9777 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9778 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9779 lang_printable_name (type, 0),
9780 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9784 field_decl = lookup_field_wrapper (type,
9785 EXPR_WFL_NODE (qual_wfl));
9787 /* Maybe what we're trying to access to is an inner
9788 class, only if decl is a TYPE_DECL. */
9789 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
9791 tree ptr, inner_decl;
9793 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9794 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9797 check_inner_class_access (inner_decl, decl, qual_wfl);
9798 type = TREE_TYPE (inner_decl);
9805 if (field_decl == NULL_TREE)
9808 (qual_wfl, "No variable `%s' defined in type `%s'",
9809 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9810 GET_TYPE_NAME (type));
9813 if (field_decl == error_mark_node)
9816 /* Layout the type of field_decl, since we may need
9817 it. Don't do primitive types or loaded classes. The
9818 situation of non primitive arrays may not handled
9819 properly here. FIXME */
9820 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9821 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9823 field_decl_type = TREE_TYPE (field_decl);
9824 if (!JPRIMITIVE_TYPE_P (field_decl_type)
9825 && !CLASS_LOADED_P (field_decl_type)
9826 && !TYPE_ARRAY_P (field_decl_type))
9827 resolve_and_layout (field_decl_type, NULL_TREE);
9829 /* Check on accessibility here */
9830 if (not_accessible_p (current_class, field_decl,
9831 DECL_CONTEXT (field_decl), from_super))
9835 "Can't access %s field `%s.%s' from `%s'",
9836 java_accstring_lookup
9837 (get_access_flags_from_decl (field_decl)),
9838 GET_TYPE_NAME (type),
9839 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9841 (DECL_NAME (TYPE_NAME (current_class))));
9844 check_deprecation (qual_wfl, field_decl);
9846 /* There are things to check when fields are accessed
9847 from type. There are no restrictions on a static
9848 declaration of the field when it is accessed from an
9850 is_static = FIELD_STATIC (field_decl);
9851 if (!from_super && from_type
9852 && !TYPE_INTERFACE_P (type)
9854 && (current_function_decl
9855 && METHOD_STATIC (current_function_decl)))
9857 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9860 from_cast = from_super = 0;
9862 /* It's an access from a type but it isn't static, we
9863 make it relative to `this'. */
9864 if (!is_static && from_type)
9865 decl = current_this;
9867 /* If we need to generate something to get a proper
9868 handle on what this field is accessed from, do it
9872 decl = maybe_access_field (decl, *where_found, *type_found);
9873 if (decl == error_mark_node)
9877 /* We want to keep the location were found it, and the type
9879 *where_found = decl;
9882 /* Generate the correct expression for field access from
9884 if (from_qualified_this)
9886 field_decl = build_outer_field_access (qual_wfl, field_decl);
9887 from_qualified_this = 0;
9890 /* This is the decl found and eventually the next one to
9895 type = QUAL_DECL_TYPE (decl);
9897 /* Sneak preview. If decl is qualified by a `new', report
9898 the error here to be accurate on the peculiar construct */
9900 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9901 && !JREFERENCE_TYPE_P (type))
9903 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9904 lang_printable_name (type, 0));
9908 /* `q' might have changed due to a after package resolution
9917 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9918 can't be accessed from REFERENCE (a record type). If MEMBER
9919 features a protected access, we then use WHERE which, if non null,
9920 holds the type of MEMBER's access that is checked against
9921 6.6.2.1. This function should be used when decl is a field or a
9925 not_accessible_p (reference, member, where, from_super)
9926 tree reference, member;
9930 int access_flag = get_access_flags_from_decl (member);
9932 /* Inner classes are processed by check_inner_class_access */
9933 if (INNER_CLASS_TYPE_P (reference))
9936 /* Access always granted for members declared public */
9937 if (access_flag & ACC_PUBLIC)
9940 /* Check access on protected members */
9941 if (access_flag & ACC_PROTECTED)
9943 /* Access granted if it occurs from within the package
9944 containing the class in which the protected member is
9946 if (class_in_current_package (DECL_CONTEXT (member)))
9949 /* If accessed with the form `super.member', then access is granted */
9953 /* If where is active, access was made through a
9954 qualifier. Access is granted if the type of the qualifier is
9955 or is a sublass of the type the access made from (6.6.2.1.) */
9956 if (where && !inherits_from_p (reference, where))
9959 /* Otherwise, access is granted if occurring from the class where
9960 member is declared or a subclass of it. Find the right
9961 context to perform the check */
9962 if (PURE_INNER_CLASS_TYPE_P (reference))
9964 while (INNER_CLASS_TYPE_P (reference))
9966 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9968 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9971 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9976 /* Check access on private members. Access is granted only if it
9977 occurs from within the class in which it is declared -- that does
9978 it for innerclasses too. */
9979 if (access_flag & ACC_PRIVATE)
9981 if (reference == DECL_CONTEXT (member))
9983 if (enclosing_context_p (reference, DECL_CONTEXT (member)))
9988 /* Default access are permitted only when occurring within the
9989 package in which the type (REFERENCE) is declared. In other words,
9990 REFERENCE is defined in the current package */
9992 return !class_in_current_package (reference);
9994 /* Otherwise, access is granted */
9998 /* Test deprecated decl access. */
10000 check_deprecation (wfl, decl)
10003 const char *file = DECL_SOURCE_FILE (decl);
10004 /* Complain if the field is deprecated and the file it was defined
10005 in isn't compiled at the same time the file which contains its
10007 if (DECL_DEPRECATED (decl)
10008 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
10011 switch (TREE_CODE (decl))
10013 case FUNCTION_DECL:
10014 strcpy (the, "method");
10018 strcpy (the, "field");
10021 parse_warning_context (wfl, "The class `%s' has been deprecated",
10022 IDENTIFIER_POINTER (DECL_NAME (decl)));
10027 /* Don't issue a message if the context as been deprecated as a
10029 if (! CLASS_DEPRECATED (TYPE_NAME (DECL_CONTEXT (decl))))
10030 parse_warning_context
10031 (wfl, "The %s `%s' in class `%s' has been deprecated",
10032 the, lang_printable_name (decl, 0),
10033 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
10037 /* Returns 1 if class was declared in the current package, 0 otherwise */
10040 class_in_current_package (class)
10043 static tree cache = NULL_TREE;
10044 int qualified_flag;
10047 if (cache == class)
10050 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
10052 /* If the current package is empty and the name of CLASS is
10053 qualified, class isn't in the current package. If there is a
10054 current package and the name of the CLASS is not qualified, class
10055 isn't in the current package */
10056 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
10059 /* If there is not package and the name of CLASS isn't qualified,
10060 they belong to the same unnamed package */
10061 if (!ctxp->package && !qualified_flag)
10064 /* Compare the left part of the name of CLASS with the package name */
10065 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
10066 if (ctxp->package == left)
10068 static int initialized_p;
10069 /* Register CACHE with the garbage collector. */
10070 if (!initialized_p)
10072 ggc_add_tree_root (&cache, 1);
10082 /* This function may generate code to access DECL from WHERE. This is
10083 done only if certain conditions meet. */
10086 maybe_access_field (decl, where, type)
10087 tree decl, where, type;
10089 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
10090 && !FIELD_STATIC (decl))
10091 decl = build_field_ref (where ? where : current_this,
10092 (type ? type : DECL_CONTEXT (decl)),
10097 /* Build a method invocation, by patching PATCH. If non NULL
10098 and according to the situation, PRIMARY and WHERE may be
10099 used. IS_STATIC is set to 1 if the invoked function is static. */
10102 patch_method_invocation (patch, primary, where, from_super,
10103 is_static, ret_decl)
10104 tree patch, primary, where;
10109 tree wfl = TREE_OPERAND (patch, 0);
10110 tree args = TREE_OPERAND (patch, 1);
10111 tree name = EXPR_WFL_NODE (wfl);
10113 int is_static_flag = 0;
10114 int is_super_init = 0;
10115 tree this_arg = NULL_TREE;
10116 int is_array_clone_call = 0;
10118 /* Should be overriden if everything goes well. Otherwise, if
10119 something fails, it should keep this value. It stop the
10120 evaluation of a bogus assignment. See java_complete_tree,
10121 MODIFY_EXPR: for the reasons why we sometimes want to keep on
10122 evaluating an assignment */
10123 TREE_TYPE (patch) = error_mark_node;
10125 /* Since lookup functions are messing with line numbers, save the
10127 java_parser_context_save_global ();
10129 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
10131 /* Resolution of qualified name, excluding constructors */
10132 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
10134 tree identifier, identifier_wfl, type, resolved;
10135 /* Extract the last IDENTIFIER of the qualified
10136 expression. This is a wfl and we will use it's location
10137 data during error report. */
10138 identifier_wfl = cut_identifier_in_qualified (wfl);
10139 identifier = EXPR_WFL_NODE (identifier_wfl);
10141 /* Given the context, IDENTIFIER is syntactically qualified
10142 as a MethodName. We need to qualify what's before */
10143 qualify_ambiguous_name (wfl);
10144 resolved = resolve_field_access (wfl, NULL, NULL);
10146 if (TREE_CODE (resolved) == VAR_DECL && FIELD_STATIC (resolved)
10147 && FIELD_FINAL (resolved)
10148 && !inherits_from_p (DECL_CONTEXT (resolved), current_class)
10149 && !flag_emit_class_files && !flag_emit_xref)
10150 resolved = build_class_init (DECL_CONTEXT (resolved), resolved);
10152 if (resolved == error_mark_node)
10153 PATCH_METHOD_RETURN_ERROR ();
10155 type = GET_SKIP_TYPE (resolved);
10156 resolve_and_layout (type, NULL_TREE);
10158 if (JPRIMITIVE_TYPE_P (type))
10160 parse_error_context
10162 "Can't invoke a method on primitive type `%s'",
10163 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10164 PATCH_METHOD_RETURN_ERROR ();
10167 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
10168 args = nreverse (args);
10170 /* We're resolving a call from a type */
10171 if (TREE_CODE (resolved) == TYPE_DECL)
10173 if (CLASS_INTERFACE (resolved))
10175 parse_error_context
10177 "Can't make static reference to method `%s' in interface `%s'",
10178 IDENTIFIER_POINTER (identifier),
10179 IDENTIFIER_POINTER (name));
10180 PATCH_METHOD_RETURN_ERROR ();
10182 if (list && !METHOD_STATIC (list))
10184 char *fct_name = xstrdup (lang_printable_name (list, 0));
10185 parse_error_context
10187 "Can't make static reference to method `%s %s' in class `%s'",
10188 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
10189 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
10191 PATCH_METHOD_RETURN_ERROR ();
10195 this_arg = primary = resolved;
10197 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
10198 is_array_clone_call = 1;
10200 /* IDENTIFIER_WFL will be used to report any problem further */
10201 wfl = identifier_wfl;
10203 /* Resolution of simple names, names generated after a primary: or
10207 tree class_to_search = NULL_TREE;
10208 int lc; /* Looking for Constructor */
10210 /* We search constructor in their target class */
10211 if (CALL_CONSTRUCTOR_P (patch))
10213 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10214 class_to_search = EXPR_WFL_NODE (wfl);
10215 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10216 this_identifier_node)
10217 class_to_search = NULL_TREE;
10218 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
10219 super_identifier_node)
10222 if (CLASSTYPE_SUPER (current_class))
10224 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
10227 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
10228 PATCH_METHOD_RETURN_ERROR ();
10232 /* Class to search is NULL if we're searching the current one */
10233 if (class_to_search)
10235 class_to_search = resolve_and_layout (class_to_search, wfl);
10237 if (!class_to_search)
10239 parse_error_context
10240 (wfl, "Class `%s' not found in type declaration",
10241 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10242 PATCH_METHOD_RETURN_ERROR ();
10245 /* Can't instantiate an abstract class, but we can
10246 invoke it's constructor. It's use within the `new'
10247 context is denied here. */
10248 if (CLASS_ABSTRACT (class_to_search)
10249 && TREE_CODE (patch) == NEW_CLASS_EXPR)
10251 parse_error_context
10252 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
10253 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
10254 PATCH_METHOD_RETURN_ERROR ();
10257 class_to_search = TREE_TYPE (class_to_search);
10260 class_to_search = current_class;
10263 /* This is a regular search in the local class, unless an
10264 alternate class is specified. */
10267 if (where != NULL_TREE)
10268 class_to_search = where;
10269 else if (QUALIFIED_P (name))
10270 class_to_search = current_class;
10273 class_to_search = current_class;
10277 if (has_method (class_to_search, name))
10279 if (! INNER_CLASS_TYPE_P (class_to_search))
10281 parse_error_context (wfl,
10282 "No method named `%s' in scope",
10283 IDENTIFIER_POINTER (name));
10284 PATCH_METHOD_RETURN_ERROR ();
10287 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
10293 /* NAME is a simple identifier or comes from a primary. Search
10294 in the class whose declaration contain the method being
10296 resolve_and_layout (class_to_search, NULL_TREE);
10298 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
10299 /* Don't continue if no method were found, as the next statement
10300 can't be executed then. */
10302 PATCH_METHOD_RETURN_ERROR ();
10304 if (TYPE_ARRAY_P (class_to_search)
10305 && DECL_NAME (list) == get_identifier ("clone"))
10306 is_array_clone_call = 1;
10308 /* Check for static reference if non static methods */
10309 if (check_for_static_method_reference (wfl, patch, list,
10310 class_to_search, primary))
10311 PATCH_METHOD_RETURN_ERROR ();
10313 /* Check for inner classes creation from illegal contexts */
10314 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
10315 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
10316 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search)
10317 && !DECL_INIT_P (current_function_decl))
10319 parse_error_context
10320 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
10321 lang_printable_name (class_to_search, 0),
10322 (!current_this ? "" :
10323 "; an explicit one must be provided when creating this inner class"));
10324 PATCH_METHOD_RETURN_ERROR ();
10327 /* Non static methods are called with the current object extra
10328 argument. If patch a `new TYPE()', the argument is the value
10329 returned by the object allocator. If method is resolved as a
10330 primary, use the primary otherwise use the current THIS. */
10331 args = nreverse (args);
10332 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
10334 this_arg = primary ? primary : current_this;
10336 /* If we're using an access method, things are different.
10337 There are two familly of cases:
10339 1) We're not generating bytecodes:
10341 - LIST is non static. It's invocation is transformed from
10342 x(a1,...,an) into this$<n>.x(a1,....an).
10343 - LIST is static. It's invocation is transformed from
10344 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
10346 2) We're generating bytecodes:
10348 - LIST is non static. It's invocation is transformed from
10349 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
10350 - LIST is static. It's invocation is transformed from
10351 x(a1,....,an) into TYPE_OF(this$<n>).x(a1,....an).
10353 Of course, this$<n> can be abitrary complex, ranging from
10354 this$0 (the immediate outer context) to
10355 access$0(access$0(...(this$0))).
10357 maybe_use_access_method returns a non zero value if the
10358 this_arg has to be moved into the (then generated) stub
10359 argument list. In the meantime, the selected function
10360 might have be replaced by a generated stub. */
10362 maybe_use_access_method (is_super_init, &list, &this_arg))
10364 args = tree_cons (NULL_TREE, this_arg, args);
10365 this_arg = NULL_TREE; /* So it doesn't get chained twice */
10370 /* Merge point of all resolution schemes. If we have nothing, this
10371 is an error, already signaled */
10373 PATCH_METHOD_RETURN_ERROR ();
10375 /* Check accessibility, position the is_static flag, build and
10377 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10378 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10379 NULL_TREE), from_super)
10380 /* Calls to clone() on array types are permitted as a special-case. */
10381 && !is_array_clone_call)
10383 const char *const fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10384 const char *const access =
10385 java_accstring_lookup (get_access_flags_from_decl (list));
10386 const char *const klass =
10387 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10388 const char *const refklass =
10389 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10390 const char *const what = (DECL_CONSTRUCTOR_P (list)
10391 ? "constructor" : "method");
10392 /* FIXME: WFL yields the wrong message here but I don't know
10393 what else to use. */
10394 parse_error_context (wfl,
10395 "Can't access %s %s `%s.%s' from `%s'",
10396 access, what, klass, fct_name, refklass);
10397 PATCH_METHOD_RETURN_ERROR ();
10400 /* Deprecation check: check whether the method being invoked or the
10401 instance-being-created's type are deprecated. */
10402 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10403 check_deprecation (wfl, TYPE_NAME (DECL_CONTEXT (list)));
10405 check_deprecation (wfl, list);
10407 /* If invoking a innerclass constructor, there are hidden parameters
10409 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10410 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10412 /* And make sure we add the accessed local variables to be saved
10413 in field aliases. */
10414 args = build_alias_initializer_parameter_list
10415 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10417 /* Secretly pass the current_this/primary as a second argument */
10418 if (primary || current_this)
10421 tree this_type = (current_this ?
10422 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10423 /* Method's (list) enclosing context */
10424 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10425 /* If we have a primary, use it. */
10427 extra_arg = primary;
10428 /* The current `this' is an inner class but isn't a direct
10429 enclosing context for the inner class we're trying to
10430 create. Build an access to the proper enclosing context
10432 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10433 && this_type != TREE_TYPE (mec))
10436 extra_arg = build_access_to_thisn (current_class,
10437 TREE_TYPE (mec), 0);
10438 extra_arg = java_complete_tree (extra_arg);
10440 /* Otherwise, just use the current `this' as an enclosing
10443 extra_arg = current_this;
10444 args = tree_cons (NULL_TREE, extra_arg, args);
10447 args = tree_cons (NULL_TREE, integer_zero_node, args);
10450 /* This handles the situation where a constructor invocation needs
10451 to have an enclosing context passed as a second parameter (the
10452 constructor is one of an inner class. We extract it from the
10453 current function. */
10454 if ((is_super_init ||
10455 (TREE_CODE (patch) == CALL_EXPR && name == this_identifier_node))
10456 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10458 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
10461 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
10463 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
10464 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
10468 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
10470 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
10471 extra_arg = java_complete_tree (extra_arg);
10473 args = tree_cons (NULL_TREE, extra_arg, args);
10476 is_static_flag = METHOD_STATIC (list);
10477 if (! is_static_flag && this_arg != NULL_TREE)
10478 args = tree_cons (NULL_TREE, this_arg, args);
10480 /* In the context of an explicit constructor invocation, we can't
10481 invoke any method relying on `this'. Exceptions are: we're
10482 invoking a static function, primary exists and is not the current
10483 this, we're creating a new object. */
10484 if (ctxp->explicit_constructor_p
10486 && (!primary || primary == current_this)
10487 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10489 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
10490 PATCH_METHOD_RETURN_ERROR ();
10492 java_parser_context_restore_global ();
10494 *is_static = is_static_flag;
10495 /* Sometimes, we want the decl of the selected method. Such as for
10499 patch = patch_invoke (patch, list, args);
10501 /* Now is a good time to insert the call to finit$ */
10502 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10504 tree finit_parms, finit_call;
10506 /* Prepare to pass hidden parameters to finit$, if any. */
10507 finit_parms = build_alias_initializer_parameter_list
10508 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10511 build_method_invocation (build_wfl_node (finit_identifier_node),
10514 /* Generate the code used to initialize fields declared with an
10515 initialization statement and build a compound statement along
10516 with the super constructor invocation. */
10517 patch = build (COMPOUND_EXPR, void_type_node, patch,
10518 java_complete_tree (finit_call));
10519 CAN_COMPLETE_NORMALLY (patch) = 1;
10524 /* Check that we're not trying to do a static reference to a method in
10525 non static method. Return 1 if it's the case, 0 otherwise. */
10528 check_for_static_method_reference (wfl, node, method, where, primary)
10529 tree wfl, node, method, where, primary;
10531 if (METHOD_STATIC (current_function_decl)
10532 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10534 char *fct_name = xstrdup (lang_printable_name (method, 0));
10535 parse_error_context
10536 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
10537 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10538 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10545 /* Fix the invocation of *MDECL if necessary in the case of a
10546 invocation from an inner class. *THIS_ARG might be modified
10547 appropriately and an alternative access to *MDECL might be
10551 maybe_use_access_method (is_super_init, mdecl, this_arg)
10553 tree *mdecl, *this_arg;
10556 tree md = *mdecl, ta = *this_arg;
10558 int non_static_context = !METHOD_STATIC (md);
10561 || DECL_CONTEXT (md) == current_class
10562 || !PURE_INNER_CLASS_TYPE_P (current_class)
10563 || DECL_FINIT_P (md)
10564 || DECL_INSTINIT_P (md))
10567 /* If we're calling a method found in an enclosing class, generate
10568 what it takes to retrieve the right this. Don't do that if we're
10569 invoking a static method. Note that if MD's type is unrelated to
10570 CURRENT_CLASS, then the current this can be used. */
10572 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10574 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10575 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10577 ta = build_current_thisn (current_class);
10578 ta = build_wfl_node (ta);
10585 maybe_build_thisn_access_method (type);
10586 if (inherits_from_p (type, DECL_CONTEXT (md)))
10588 ta = build_access_to_thisn (ctx, type, 0);
10591 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10592 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10595 ta = java_complete_tree (ta);
10598 /* We might have to use an access method to get to MD. We can
10599 break the method access rule as far as we're not generating
10601 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10603 md = build_outer_method_access_method (md);
10610 /* Returnin a non zero value indicates we were doing a non static
10611 method invokation that is now a static invocation. It will have
10612 callee displace `this' to insert it in the regular argument
10614 return (non_static_context && to_return);
10617 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10621 patch_invoke (patch, method, args)
10622 tree patch, method, args;
10625 tree original_call, t, ta;
10626 tree check = NULL_TREE;
10628 /* Last step for args: convert build-in types. If we're dealing with
10629 a new TYPE() type call, the first argument to the constructor
10630 isn't found in the incoming argument list, but delivered by
10632 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10633 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10634 t = TREE_CHAIN (t);
10635 for (ta = args; t != end_params_node && ta;
10636 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10637 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10638 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10639 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10641 /* Resolve unresolved returned type isses */
10642 t = TREE_TYPE (TREE_TYPE (method));
10643 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10644 resolve_and_layout (TREE_TYPE (t), NULL);
10646 if (flag_emit_class_files || flag_emit_xref)
10650 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10652 case INVOKE_VIRTUAL:
10653 dtable = invoke_build_dtable (0, args);
10654 func = build_invokevirtual (dtable, method);
10657 case INVOKE_NONVIRTUAL:
10658 /* If the object for the method call is null, we throw an
10659 exception. We don't do this if the object is the current
10660 method's `this'. In other cases we just rely on an
10661 optimization pass to eliminate redundant checks. */
10662 if (TREE_VALUE (args) != current_this)
10664 /* We use a save_expr here to make sure we only evaluate
10665 the new `self' expression once. */
10666 tree save_arg = save_expr (TREE_VALUE (args));
10667 TREE_VALUE (args) = save_arg;
10668 check = java_check_reference (save_arg, 1);
10670 /* Fall through. */
10673 case INVOKE_STATIC:
10675 tree signature = build_java_signature (TREE_TYPE (method));
10676 func = build_known_method_ref (method, TREE_TYPE (method),
10677 DECL_CONTEXT (method),
10682 case INVOKE_INTERFACE:
10683 dtable = invoke_build_dtable (1, args);
10684 func = build_invokeinterface (dtable, method);
10691 /* Ensure self_type is initialized, (invokestatic). FIXME */
10692 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10695 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10696 TREE_OPERAND (patch, 0) = func;
10697 TREE_OPERAND (patch, 1) = args;
10698 patch = check_for_builtin (method, patch);
10699 original_call = patch;
10701 /* We're processing a `new TYPE ()' form. New is called and its
10702 returned value is the first argument to the constructor. We build
10703 a COMPOUND_EXPR and use saved expression so that the overall NEW
10704 expression value is a pointer to a newly created and initialized
10706 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10708 tree class = DECL_CONTEXT (method);
10709 tree c1, saved_new, size, new;
10712 if (flag_emit_class_files || flag_emit_xref)
10714 TREE_TYPE (patch) = build_pointer_type (class);
10717 if (!TYPE_SIZE (class))
10718 safe_layout_class (class);
10719 size = size_in_bytes (class);
10721 (class_has_finalize_method (class) ? alloc_object_node
10722 : alloc_no_finalizer_node);
10723 new = build (CALL_EXPR, promote_type (class),
10724 build_address_of (alloc_node),
10725 tree_cons (NULL_TREE, build_class_ref (class),
10726 build_tree_list (NULL_TREE,
10727 size_in_bytes (class))),
10729 saved_new = save_expr (new);
10730 c1 = build_tree_list (NULL_TREE, saved_new);
10731 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10732 TREE_OPERAND (original_call, 1) = c1;
10733 TREE_SET_CODE (original_call, CALL_EXPR);
10734 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10737 /* If CHECK is set, then we are building a check to see if the object
10739 if (check != NULL_TREE)
10741 patch = build (COMPOUND_EXPR, TREE_TYPE (patch), check, patch);
10742 TREE_SIDE_EFFECTS (patch) = 1;
10745 /* In order to be able to modify PATCH later, we SAVE_EXPR it and
10746 put it as the first expression of a COMPOUND_EXPR. The second
10747 expression being an empty statement to be later patched if
10748 necessary. We remember a TREE_LIST (the PURPOSE is the method,
10749 the VALUE is the compound) in a hashtable and return a
10750 COMPOUND_EXPR built so that the result of the evaluation of the
10751 original PATCH node is returned. */
10752 if (STATIC_CLASS_INIT_OPT_P ()
10753 && current_function_decl && METHOD_STATIC (method))
10756 tree fndecl = current_function_decl;
10757 tree save = save_expr (patch);
10758 tree type = TREE_TYPE (patch);
10760 patch = build (COMPOUND_EXPR, type, save, empty_stmt_node);
10761 list = tree_cons (method, patch,
10762 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl));
10764 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = list;
10766 patch = build (COMPOUND_EXPR, type, patch, save);
10773 invocation_mode (method, super)
10777 int access = get_access_flags_from_decl (method);
10780 return INVOKE_SUPER;
10782 if (access & ACC_STATIC)
10783 return INVOKE_STATIC;
10785 /* We have to look for a constructor before we handle nonvirtual
10786 calls; otherwise the constructor will look nonvirtual. */
10787 if (DECL_CONSTRUCTOR_P (method))
10788 return INVOKE_STATIC;
10790 if (access & ACC_FINAL || access & ACC_PRIVATE)
10791 return INVOKE_NONVIRTUAL;
10793 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10794 return INVOKE_NONVIRTUAL;
10796 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10797 return INVOKE_INTERFACE;
10799 return INVOKE_VIRTUAL;
10802 /* Retrieve a refined list of matching methods. It covers the step
10803 15.11.2 (Compile-Time Step 2) */
10806 lookup_method_invoke (lc, cl, class, name, arg_list)
10809 tree class, name, arg_list;
10811 tree atl = end_params_node; /* Arg Type List */
10812 tree method, signature, list, node;
10813 const char *candidates; /* Used for error report */
10816 /* Fix the arguments */
10817 for (node = arg_list; node; node = TREE_CHAIN (node))
10819 tree current_arg = TREE_TYPE (TREE_VALUE (node));
10820 /* Non primitive type may have to be resolved */
10821 if (!JPRIMITIVE_TYPE_P (current_arg))
10822 resolve_and_layout (current_arg, NULL_TREE);
10824 if (TREE_CODE (current_arg) == RECORD_TYPE)
10825 current_arg = promote_type (current_arg);
10826 atl = tree_cons (NULL_TREE, current_arg, atl);
10829 /* Presto. If we're dealing with an anonymous class and a
10830 constructor call, generate the right constructor now, since we
10831 know the arguments' types. */
10833 if (lc && ANONYMOUS_CLASS_P (class))
10834 craft_constructor (TYPE_NAME (class), atl);
10836 /* Find all candidates and then refine the list, searching for the
10837 most specific method. */
10838 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10839 list = find_most_specific_methods_list (list);
10840 if (list && !TREE_CHAIN (list))
10841 return TREE_VALUE (list);
10843 /* Issue an error. List candidates if any. Candidates are listed
10844 only if accessible (non accessible methods may end-up here for
10845 the sake of a better error report). */
10850 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10851 for (current = list; current; current = TREE_CHAIN (current))
10853 tree cm = TREE_VALUE (current);
10854 char string [4096];
10855 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
10858 (string, " `%s' in `%s'%s",
10859 get_printable_method_name (cm),
10860 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10861 (TREE_CHAIN (current) ? "\n" : ""));
10862 obstack_grow (&temporary_obstack, string, strlen (string));
10864 obstack_1grow (&temporary_obstack, '\0');
10865 candidates = obstack_finish (&temporary_obstack);
10867 /* Issue the error message */
10868 method = make_node (FUNCTION_TYPE);
10869 TYPE_ARG_TYPES (method) = atl;
10870 signature = build_java_argument_signature (method);
10871 dup = xstrdup (lang_printable_name (class, 0));
10872 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10873 (lc ? "constructor" : "method"),
10874 (lc ? dup : IDENTIFIER_POINTER (name)),
10875 IDENTIFIER_POINTER (signature), dup,
10876 (candidates ? candidates : ""));
10881 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10882 when we're looking for a constructor. */
10885 find_applicable_accessible_methods_list (lc, class, name, arglist)
10887 tree class, name, arglist;
10889 static struct hash_table t, *searched_classes = NULL;
10890 static int search_not_done = 0;
10891 tree list = NULL_TREE, all_list = NULL_TREE;
10893 /* Check the hash table to determine if this class has been searched
10895 if (searched_classes)
10897 if (hash_lookup (searched_classes,
10898 (const hash_table_key) class, FALSE, NULL))
10903 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10904 java_hash_compare_tree_node);
10905 searched_classes = &t;
10909 hash_lookup (searched_classes,
10910 (const hash_table_key) class, TRUE, NULL);
10912 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10914 load_class (class, 1);
10915 safe_layout_class (class);
10918 /* Search interfaces */
10919 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10920 && CLASS_INTERFACE (TYPE_NAME (class)))
10923 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10924 search_applicable_methods_list (lc, TYPE_METHODS (class),
10925 name, arglist, &list, &all_list);
10926 n = TREE_VEC_LENGTH (basetype_vec);
10927 for (i = 1; i < n; i++)
10929 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10932 rlist = find_applicable_accessible_methods_list (lc, t, name,
10934 list = chainon (rlist, list);
10937 /* Search classes */
10940 search_applicable_methods_list (lc, TYPE_METHODS (class),
10941 name, arglist, &list, &all_list);
10943 /* When looking finit$, class$ or instinit$, we turn LC to 1 so
10944 that we only search in class. Note that we should have found
10945 something at this point. */
10946 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name) || ID_INSTINIT_P (name))
10953 /* We must search all interfaces of this class */
10956 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10957 int n = TREE_VEC_LENGTH (basetype_vec), i;
10958 for (i = 1; i < n; i++)
10960 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10961 if (t != object_type_node)
10964 = find_applicable_accessible_methods_list (lc, t,
10966 list = chainon (rlist, list);
10971 /* Search superclass */
10972 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10975 class = CLASSTYPE_SUPER (class);
10976 rlist = find_applicable_accessible_methods_list (lc, class,
10978 list = chainon (rlist, list);
10984 /* We're done. Reset the searched classes list and finally search
10985 java.lang.Object if it wasn't searched already. */
10986 if (!search_not_done)
10989 && TYPE_METHODS (object_type_node)
10990 && !hash_lookup (searched_classes,
10991 (const hash_table_key) object_type_node,
10994 search_applicable_methods_list (lc,
10995 TYPE_METHODS (object_type_node),
10996 name, arglist, &list, &all_list);
10998 hash_table_free (searched_classes);
10999 searched_classes = NULL;
11002 /* Either return the list obtained or all selected (but
11003 inaccessible) methods for better error report. */
11004 return (!list ? all_list : list);
11007 /* Effectively search for the appropriate method in method */
11010 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
11012 tree method, name, arglist;
11013 tree *list, *all_list;
11015 for (; method; method = TREE_CHAIN (method))
11017 /* When dealing with constructor, stop here, otherwise search
11019 if (lc && !DECL_CONSTRUCTOR_P (method))
11021 else if (!lc && (DECL_CONSTRUCTOR_P (method)
11022 || (DECL_NAME (method) != name)))
11025 if (argument_types_convertible (method, arglist))
11027 /* Retain accessible methods only */
11028 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
11029 method, NULL_TREE, 0))
11030 *list = tree_cons (NULL_TREE, method, *list);
11032 /* Also retain all selected method here */
11033 *all_list = tree_cons (NULL_TREE, method, *list);
11038 /* 15.11.2.2 Choose the Most Specific Method */
11041 find_most_specific_methods_list (list)
11045 int abstract, candidates;
11046 tree current, new_list = NULL_TREE;
11047 for (current = list; current; current = TREE_CHAIN (current))
11050 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
11052 for (method = list; method; method = TREE_CHAIN (method))
11054 tree method_v, current_v;
11055 /* Don't test a method against itself */
11056 if (method == current)
11059 method_v = TREE_VALUE (method);
11060 current_v = TREE_VALUE (current);
11062 /* Compare arguments and location where methods where declared */
11063 if (argument_types_convertible (method_v, current_v))
11065 if (valid_method_invocation_conversion_p
11066 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
11067 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
11068 && enclosing_context_p (DECL_CONTEXT (method_v),
11069 DECL_CONTEXT (current_v))))
11071 int v = (DECL_SPECIFIC_COUNT (current_v) +=
11072 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
11073 max = (v > max ? v : max);
11079 /* Review the list and select the maximally specific methods */
11080 for (current = list, abstract = -1, candidates = -1;
11081 current; current = TREE_CHAIN (current))
11082 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11084 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11085 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
11089 /* If we have several and they're all abstract, just pick the
11091 if (candidates > 0 && (candidates == abstract))
11093 new_list = nreverse (new_list);
11094 TREE_CHAIN (new_list) = NULL_TREE;
11097 /* We have several (we couldn't find a most specific), all but one
11098 are abstract, we pick the only non abstract one. */
11099 if (candidates > 0 && (candidates == abstract+1))
11101 for (current = new_list; current; current = TREE_CHAIN (current))
11102 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
11104 TREE_CHAIN (current) = NULL_TREE;
11105 new_list = current;
11109 /* If we can't find one, lower expectations and try to gather multiple
11110 maximally specific methods */
11111 while (!new_list && max)
11115 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
11116 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
11123 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
11124 converted by method invocation conversion (5.3) to the type of the
11125 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
11126 to change less often than M1. */
11129 argument_types_convertible (m1, m2_or_arglist)
11130 tree m1, m2_or_arglist;
11132 static tree m2_arg_value = NULL_TREE;
11133 static tree m2_arg_cache = NULL_TREE;
11134 static int initialized_p;
11136 register tree m1_arg, m2_arg;
11138 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
11140 if (!initialized_p)
11142 ggc_add_tree_root (&m2_arg_value, 1);
11143 ggc_add_tree_root (&m2_arg_cache, 1);
11147 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
11149 if (m2_arg_value == m2_or_arglist)
11150 m2_arg = m2_arg_cache;
11153 /* M2_OR_ARGLIST can be a function DECL or a raw list of
11155 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
11157 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
11158 if (!METHOD_STATIC (m2_or_arglist))
11159 m2_arg = TREE_CHAIN (m2_arg);
11162 m2_arg = m2_or_arglist;
11164 m2_arg_value = m2_or_arglist;
11165 m2_arg_cache = m2_arg;
11168 while (m1_arg != end_params_node && m2_arg != end_params_node)
11170 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
11171 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
11172 TREE_VALUE (m2_arg)))
11174 m1_arg = TREE_CHAIN (m1_arg);
11175 m2_arg = TREE_CHAIN (m2_arg);
11177 return m1_arg == end_params_node && m2_arg == end_params_node;
11180 /* Qualification routines */
11183 qualify_ambiguous_name (id)
11186 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
11187 saved_current_class;
11188 int again, super_found = 0, this_found = 0, new_array_found = 0;
11191 /* We first qualify the first element, then derive qualification of
11192 others based on the first one. If the first element is qualified
11193 by a resolution (field or type), this resolution is stored in the
11194 QUAL_RESOLUTION of the qual element being examined. We need to
11195 save the current_class since the use of SUPER might change the
11197 saved_current_class = current_class;
11198 qual = EXPR_WFL_QUALIFICATION (id);
11201 /* Simple qualified expression feature a qual_wfl that is a
11202 WFL. Expression derived from a primary feature more complicated
11203 things like a CALL_EXPR. Expression from primary need to be
11204 worked out to extract the part on which the qualification will
11206 qual_wfl = QUAL_WFL (qual);
11207 switch (TREE_CODE (qual_wfl))
11210 qual_wfl = TREE_OPERAND (qual_wfl, 0);
11211 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
11213 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11214 qual_wfl = QUAL_WFL (qual);
11217 case NEW_ARRAY_EXPR:
11218 case NEW_ANONYMOUS_ARRAY_EXPR:
11219 qual = TREE_CHAIN (qual);
11220 again = new_array_found = 1;
11224 case NEW_CLASS_EXPR:
11225 qual_wfl = TREE_OPERAND (qual_wfl, 0);
11228 while (TREE_CODE (qual_wfl) == ARRAY_REF)
11229 qual_wfl = TREE_OPERAND (qual_wfl, 0);
11232 qual = TREE_CHAIN (qual);
11233 qual_wfl = QUAL_WFL (qual);
11235 case CLASS_LITERAL:
11236 qual = TREE_CHAIN (qual);
11237 qual_wfl = QUAL_WFL (qual);
11240 /* Fix for -Wall. Just break doing nothing */
11244 ptr_type = current_class;
11246 code = TREE_CODE (qual_wfl);
11248 /* Pos evaluation: non WFL leading expression nodes */
11249 if (code == CONVERT_EXPR
11250 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
11251 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
11253 else if (code == INTEGER_CST)
11256 else if (code == CONVERT_EXPR &&
11257 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11258 name = TREE_OPERAND (qual_wfl, 0);
11260 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
11261 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
11262 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
11264 else if (code == TREE_LIST)
11265 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
11267 else if (code == STRING_CST || code == CONDITIONAL_EXPR
11268 || code == PLUS_EXPR)
11270 qual = TREE_CHAIN (qual);
11271 qual_wfl = QUAL_WFL (qual);
11276 name = EXPR_WFL_NODE (qual_wfl);
11279 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
11284 /* If we have a THIS (from a primary), we set the context accordingly */
11285 if (name == this_identifier_node)
11287 /* This isn't really elegant. One more added irregularity
11288 before I start using COMPONENT_REF (hopefully very soon.) */
11289 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
11290 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
11291 EXPR_WITH_FILE_LOCATION
11292 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
11293 this_identifier_node)
11295 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
11296 qual = EXPR_WFL_QUALIFICATION (qual);
11298 qual = TREE_CHAIN (qual);
11299 qual_wfl = QUAL_WFL (qual);
11300 if (TREE_CODE (qual_wfl) == CALL_EXPR)
11302 else if (TREE_CODE (qual_wfl) == EXPR_WITH_FILE_LOCATION)
11303 name = EXPR_WFL_NODE (qual_wfl);
11304 else if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
11305 name = TREE_OPERAND (qual_wfl, 0);
11308 /* If we have a SUPER, we set the context accordingly */
11309 if (name == super_identifier_node)
11311 current_class = CLASSTYPE_SUPER (ptr_type);
11312 /* Check that there is such a thing as a super class. If not,
11313 return. The error will be caught later on, during the
11315 if (!current_class)
11317 current_class = saved_current_class;
11320 qual = TREE_CHAIN (qual);
11321 /* Do one more interation to set things up */
11322 super_found = again = 1;
11326 /* If name appears within the scope of a local variable declaration
11327 or parameter declaration, then it is an expression name. We don't
11328 carry this test out if we're in the context of the use of SUPER
11330 if (!this_found && !super_found
11331 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
11332 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
11334 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11335 QUAL_RESOLUTION (qual) = decl;
11338 /* If within the class/interface NAME was found to be used there
11339 exists a (possibly inherited) field named NAME, then this is an
11340 expression name. If we saw a NEW_ARRAY_EXPR before and want to
11341 address length, it is OK. */
11342 else if ((decl = lookup_field_wrapper (ptr_type, name))
11343 || name == length_identifier_node)
11345 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11346 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
11349 /* We reclassify NAME as yielding to a type name resolution if:
11350 - NAME is a class/interface declared within the compilation
11351 unit containing NAME,
11352 - NAME is imported via a single-type-import declaration,
11353 - NAME is declared in an another compilation unit of the package
11354 of the compilation unit containing NAME,
11355 - NAME is declared by exactly on type-import-on-demand declaration
11356 of the compilation unit containing NAME.
11357 - NAME is actually a STRING_CST.
11358 This can't happen if the expression was qualified by `this.' */
11359 else if (! this_found &&
11360 (TREE_CODE (name) == STRING_CST ||
11361 TREE_CODE (name) == INTEGER_CST ||
11362 (decl = resolve_and_layout (name, NULL_TREE))))
11364 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
11365 QUAL_RESOLUTION (qual) = decl;
11368 /* Method call, array references and cast are expression name */
11369 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
11370 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
11371 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR
11372 || TREE_CODE (QUAL_WFL (qual)) == MODIFY_EXPR)
11373 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
11375 /* Check here that NAME isn't declared by more than one
11376 type-import-on-demand declaration of the compilation unit
11377 containing NAME. FIXME */
11379 /* Otherwise, NAME is reclassified as a package name */
11381 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
11383 /* Propagate the qualification accross other components of the
11385 for (qual = TREE_CHAIN (qual); qual;
11386 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
11388 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11389 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
11391 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
11394 /* Store the global qualification for the ambiguous part of ID back
11396 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
11397 RESOLVE_EXPRESSION_NAME_P (id) = 1;
11398 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
11399 RESOLVE_TYPE_NAME_P (id) = 1;
11400 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
11401 RESOLVE_PACKAGE_NAME_P (id) = 1;
11403 /* Restore the current class */
11404 current_class = saved_current_class;
11408 breakdown_qualified (left, right, source)
11409 tree *left, *right, source;
11412 int l = IDENTIFIER_LENGTH (source);
11414 base = alloca (l + 1);
11415 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
11417 /* Breakdown NAME into REMAINDER . IDENTIFIER */
11419 while (*p != '.' && p != base)
11422 /* We didn't find a '.'. Return an error */
11428 *right = get_identifier (p+1);
11429 *left = get_identifier (base);
11434 /* Return TRUE if two classes are from the same package. */
11437 in_same_package (name1, name2)
11444 if (TREE_CODE (name1) == TYPE_DECL)
11445 name1 = DECL_NAME (name1);
11446 if (TREE_CODE (name2) == TYPE_DECL)
11447 name2 = DECL_NAME (name2);
11449 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
11450 /* One in empty package. */
11453 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
11454 /* Both in empty package. */
11457 breakdown_qualified (&pkg1, &tmp, name1);
11458 breakdown_qualified (&pkg2, &tmp, name2);
11460 return (pkg1 == pkg2);
11463 /* Patch tree nodes in a function body. When a BLOCK is found, push
11464 local variable decls if present.
11465 Same as java_complete_lhs, but does resolve static finals to values. */
11468 java_complete_tree (node)
11471 node = java_complete_lhs (node);
11472 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11473 && DECL_INITIAL (node) != NULL_TREE
11474 && !flag_emit_xref)
11476 tree value = DECL_INITIAL (node);
11477 DECL_INITIAL (node) = NULL_TREE;
11478 value = fold_constant_for_init (value, node);
11479 DECL_INITIAL (node) = value;
11480 if (value != NULL_TREE)
11482 /* fold_constant_for_init sometimes widens the original type
11483 of the constant (i.e. byte to int). It's not desirable,
11484 especially if NODE is a function argument. */
11485 if ((TREE_CODE (value) == INTEGER_CST
11486 || TREE_CODE (value) == REAL_CST)
11487 && TREE_TYPE (node) != TREE_TYPE (value))
11488 return convert (TREE_TYPE (node), value);
11497 java_stabilize_reference (node)
11500 if (TREE_CODE (node) == COMPOUND_EXPR)
11502 tree op0 = TREE_OPERAND (node, 0);
11503 tree op1 = TREE_OPERAND (node, 1);
11504 TREE_OPERAND (node, 0) = save_expr (op0);
11505 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11508 return stabilize_reference (node);
11511 /* Patch tree nodes in a function body. When a BLOCK is found, push
11512 local variable decls if present.
11513 Same as java_complete_tree, but does not resolve static finals to values. */
11516 java_complete_lhs (node)
11519 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11522 /* CONVERT_EXPR always has its type set, even though it needs to be
11524 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11527 /* The switch block implements cases processing container nodes
11528 first. Contained nodes are always written back. Leaves come
11529 next and return a value. */
11530 switch (TREE_CODE (node))
11534 /* 1- Block section.
11535 Set the local values on decl names so we can identify them
11536 faster when they're referenced. At that stage, identifiers
11537 are legal so we don't check for declaration errors. */
11538 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11540 DECL_CONTEXT (cn) = current_function_decl;
11541 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11543 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11544 CAN_COMPLETE_NORMALLY (node) = 1;
11547 tree stmt = BLOCK_EXPR_BODY (node);
11549 int error_seen = 0;
11550 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11552 /* Re-order from (((A; B); C); ...; Z) to
11553 (A; (B; (C ; (...; Z)))).
11554 This makes it easier to scan the statements left-to-right
11555 without using recursion (which might overflow the stack
11556 if the block has many statements. */
11559 tree left = TREE_OPERAND (stmt, 0);
11560 if (TREE_CODE (left) != COMPOUND_EXPR)
11562 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11563 TREE_OPERAND (left, 1) = stmt;
11566 BLOCK_EXPR_BODY (node) = stmt;
11569 /* Now do the actual complete, without deep recursion for
11571 ptr = &BLOCK_EXPR_BODY (node);
11572 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11573 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
11575 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11576 tree *next = &TREE_OPERAND (*ptr, 1);
11577 TREE_OPERAND (*ptr, 0) = cur;
11578 if (cur == empty_stmt_node)
11580 /* Optimization; makes it easier to detect empty bodies.
11581 Most useful for <clinit> with all-constant initializer. */
11585 if (TREE_CODE (cur) == ERROR_MARK)
11587 else if (! CAN_COMPLETE_NORMALLY (cur))
11592 if (TREE_CODE (wfl_op2) == BLOCK)
11593 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11594 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11595 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11599 if (TREE_CODE (wfl_op2) != CASE_EXPR
11600 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11601 unreachable_stmt_error (*ptr);
11605 *ptr = java_complete_tree (*ptr);
11607 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11608 return error_mark_node;
11609 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11611 /* Turn local bindings to null */
11612 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11613 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11615 TREE_TYPE (node) = void_type_node;
11618 /* 2- They are expressions but ultimately deal with statements */
11621 wfl_op1 = TREE_OPERAND (node, 0);
11622 COMPLETE_CHECK_OP_0 (node);
11623 /* 14.19 A throw statement cannot complete normally. */
11624 CAN_COMPLETE_NORMALLY (node) = 0;
11625 return patch_throw_statement (node, wfl_op1);
11627 case SYNCHRONIZED_EXPR:
11628 wfl_op1 = TREE_OPERAND (node, 0);
11629 return patch_synchronized_statement (node, wfl_op1);
11632 return patch_try_statement (node);
11634 case TRY_FINALLY_EXPR:
11635 COMPLETE_CHECK_OP_0 (node);
11636 COMPLETE_CHECK_OP_1 (node);
11637 if (TREE_OPERAND (node, 0) == empty_stmt_node)
11638 return TREE_OPERAND (node, 1);
11639 if (TREE_OPERAND (node, 1) == empty_stmt_node)
11640 return TREE_OPERAND (node, 0);
11641 CAN_COMPLETE_NORMALLY (node)
11642 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11643 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11644 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11647 case LABELED_BLOCK_EXPR:
11648 PUSH_LABELED_BLOCK (node);
11649 if (LABELED_BLOCK_BODY (node))
11650 COMPLETE_CHECK_OP_1 (node);
11651 TREE_TYPE (node) = void_type_node;
11652 POP_LABELED_BLOCK ();
11654 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11656 LABELED_BLOCK_BODY (node) = NULL_TREE;
11657 CAN_COMPLETE_NORMALLY (node) = 1;
11659 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11660 CAN_COMPLETE_NORMALLY (node) = 1;
11663 case EXIT_BLOCK_EXPR:
11664 /* We don't complete operand 1, because it's the return value of
11665 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11666 return patch_bc_statement (node);
11669 cn = java_complete_tree (TREE_OPERAND (node, 0));
11670 if (cn == error_mark_node)
11673 /* First, the case expression must be constant. Values of final
11674 fields are accepted. */
11676 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11677 && JDECL_P (TREE_OPERAND (cn, 1))
11678 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11679 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11681 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11682 TREE_OPERAND (cn, 1));
11684 /* Accept final locals too. */
11685 else if (TREE_CODE (cn) == VAR_DECL && DECL_FINAL (cn))
11686 cn = fold_constant_for_init (DECL_INITIAL (cn), cn);
11688 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11690 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11691 parse_error_context (node, "Constant expression required");
11692 return error_mark_node;
11695 nn = ctxp->current_loop;
11697 /* It must be assignable to the type of the switch expression. */
11698 if (!try_builtin_assignconv (NULL_TREE,
11699 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11701 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11702 parse_error_context
11704 "Incompatible type for case. Can't convert `%s' to `int'",
11705 lang_printable_name (TREE_TYPE (cn), 0));
11706 return error_mark_node;
11709 cn = fold (convert (int_type_node, cn));
11710 TREE_CONSTANT_OVERFLOW (cn) = 0;
11711 CAN_COMPLETE_NORMALLY (cn) = 1;
11713 /* Save the label on a list so that we can later check for
11715 case_label_list = tree_cons (node, cn, case_label_list);
11717 /* Multiple instance of a case label bearing the same value is
11718 checked later. The case expression is all right so far. */
11719 if (TREE_CODE (cn) == VAR_DECL)
11720 cn = DECL_INITIAL (cn);
11721 TREE_OPERAND (node, 0) = cn;
11722 TREE_TYPE (node) = void_type_node;
11723 CAN_COMPLETE_NORMALLY (node) = 1;
11724 TREE_SIDE_EFFECTS (node) = 1;
11728 nn = ctxp->current_loop;
11729 /* Only one default label is allowed per switch statement */
11730 if (SWITCH_HAS_DEFAULT (nn))
11732 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11733 parse_error_context (wfl_operator,
11734 "Duplicate case label: `default'");
11735 return error_mark_node;
11738 SWITCH_HAS_DEFAULT (nn) = 1;
11739 TREE_TYPE (node) = void_type_node;
11740 TREE_SIDE_EFFECTS (node) = 1;
11741 CAN_COMPLETE_NORMALLY (node) = 1;
11747 /* Check whether the loop was enclosed in a labeled
11748 statement. If not, create one, insert the loop in it and
11750 nn = patch_loop_statement (node);
11752 /* Anyways, walk the body of the loop */
11753 if (TREE_CODE (node) == LOOP_EXPR)
11754 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11755 /* Switch statement: walk the switch expression and the cases */
11757 node = patch_switch_statement (node);
11759 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11760 nn = error_mark_node;
11763 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11764 /* If we returned something different, that's because we
11765 inserted a label. Pop the label too. */
11768 if (CAN_COMPLETE_NORMALLY (node))
11769 CAN_COMPLETE_NORMALLY (nn) = 1;
11770 POP_LABELED_BLOCK ();
11777 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11778 return patch_exit_expr (node);
11782 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11783 if (TREE_OPERAND (node, 0) == error_mark_node)
11784 return error_mark_node;
11785 /* then-else branches */
11786 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11787 if (TREE_OPERAND (node, 1) == error_mark_node)
11788 return error_mark_node;
11789 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11790 if (TREE_OPERAND (node, 2) == error_mark_node)
11791 return error_mark_node;
11792 return patch_if_else_statement (node);
11795 case CONDITIONAL_EXPR:
11797 wfl_op1 = TREE_OPERAND (node, 0);
11798 COMPLETE_CHECK_OP_0 (node);
11799 wfl_op2 = TREE_OPERAND (node, 1);
11800 COMPLETE_CHECK_OP_1 (node);
11801 wfl_op3 = TREE_OPERAND (node, 2);
11802 COMPLETE_CHECK_OP_2 (node);
11803 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11805 /* 3- Expression section */
11806 case COMPOUND_EXPR:
11807 wfl_op2 = TREE_OPERAND (node, 1);
11808 TREE_OPERAND (node, 0) = nn =
11809 java_complete_tree (TREE_OPERAND (node, 0));
11810 if (wfl_op2 == empty_stmt_node)
11811 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11814 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11816 /* An unreachable condition in a do-while statement
11817 is *not* (technically) an unreachable statement. */
11819 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11820 nn = EXPR_WFL_NODE (nn);
11821 if (TREE_CODE (nn) != EXIT_EXPR)
11823 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11824 parse_error_context (wfl_operator, "Unreachable statement");
11827 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11828 if (TREE_OPERAND (node, 1) == error_mark_node)
11829 return error_mark_node;
11830 /* Even though we might allow the case where the first
11831 operand doesn't return normally, we still should compute
11832 CAN_COMPLETE_NORMALLY correctly. */
11833 CAN_COMPLETE_NORMALLY (node)
11834 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11835 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11837 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11841 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11842 return patch_return (node);
11844 case EXPR_WITH_FILE_LOCATION:
11845 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11846 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11849 node = resolve_expression_name (node, NULL);
11850 if (node == error_mark_node)
11852 /* Keep line number information somewhere were it doesn't
11853 disrupt the completion process. */
11854 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11856 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11857 TREE_OPERAND (node, 1) = wfl;
11859 CAN_COMPLETE_NORMALLY (node) = 1;
11864 int save_lineno = lineno;
11865 lineno = EXPR_WFL_LINENO (node);
11866 body = java_complete_tree (EXPR_WFL_NODE (node));
11867 lineno = save_lineno;
11868 EXPR_WFL_NODE (node) = body;
11869 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11870 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11871 if (body == empty_stmt_node || TREE_CONSTANT (body))
11873 /* Makes it easier to constant fold, detect empty bodies. */
11876 if (body == error_mark_node)
11878 /* Its important for the evaluation of assignment that
11879 this mark on the TREE_TYPE is propagated. */
11880 TREE_TYPE (node) = error_mark_node;
11881 return error_mark_node;
11884 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11889 case NEW_ARRAY_EXPR:
11890 /* Patch all the dimensions */
11892 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11894 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11895 tree dim = convert (int_type_node,
11896 java_complete_tree (TREE_VALUE (cn)));
11897 if (dim == error_mark_node)
11904 TREE_VALUE (cn) = dim;
11905 /* Setup the location of the current dimension, for
11906 later error report. */
11907 TREE_PURPOSE (cn) =
11908 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11909 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11912 /* They complete the array creation expression, if no errors
11914 CAN_COMPLETE_NORMALLY (node) = 1;
11915 return (flag ? error_mark_node
11916 : force_evaluation_order (patch_newarray (node)));
11918 case NEW_ANONYMOUS_ARRAY_EXPR:
11919 /* Create the array type if necessary. */
11920 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11922 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11923 if (!(type = resolve_type_during_patch (type)))
11924 return error_mark_node;
11925 type = build_array_from_name (type, NULL_TREE,
11926 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11927 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11929 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11930 ANONYMOUS_ARRAY_INITIALIZER (node));
11931 if (node == error_mark_node)
11932 return error_mark_node;
11933 CAN_COMPLETE_NORMALLY (node) = 1;
11936 case NEW_CLASS_EXPR:
11938 /* Complete function's argument(s) first */
11939 if (complete_function_arguments (node))
11940 return error_mark_node;
11943 tree decl, wfl = TREE_OPERAND (node, 0);
11944 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11945 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
11946 super_identifier_node);
11948 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
11949 from_super, 0, &decl);
11950 if (node == error_mark_node)
11951 return error_mark_node;
11953 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11954 /* If we call this(...), register signature and positions */
11956 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11957 tree_cons (wfl, decl,
11958 DECL_CONSTRUCTOR_CALLS (current_function_decl));
11959 CAN_COMPLETE_NORMALLY (node) = 1;
11960 return force_evaluation_order (node);
11964 /* Save potential wfls */
11965 wfl_op1 = TREE_OPERAND (node, 0);
11966 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11968 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11969 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11970 && DECL_INITIAL (nn) != NULL_TREE)
11974 value = fold_constant_for_init (nn, nn);
11976 /* When we have a primitype type, or a string and we're not
11977 emitting a class file, we actually don't want to generate
11978 anything for the assignment. */
11979 if (value != NULL_TREE &&
11980 (JPRIMITIVE_TYPE_P (TREE_TYPE (value)) ||
11981 (TREE_TYPE (value) == string_ptr_type_node &&
11982 ! flag_emit_class_files)))
11984 /* Prepare node for patch_assignment */
11985 TREE_OPERAND (node, 1) = value;
11986 /* Call patch assignment to verify the assignment */
11987 if (patch_assignment (node, wfl_op1) == error_mark_node)
11988 return error_mark_node;
11989 /* Set DECL_INITIAL properly (a conversion might have
11990 been decided by patch_assignment) and return the
11991 empty statement. */
11994 tree patched = patch_string (TREE_OPERAND (node, 1));
11996 DECL_INITIAL (nn) = patched;
11998 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
11999 DECL_FIELD_FINAL_IUD (nn) = 1;
12000 return empty_stmt_node;
12003 if (! flag_emit_class_files)
12004 DECL_INITIAL (nn) = NULL_TREE;
12006 wfl_op2 = TREE_OPERAND (node, 1);
12008 if (TREE_OPERAND (node, 0) == error_mark_node)
12009 return error_mark_node;
12011 flag = COMPOUND_ASSIGN_P (wfl_op2);
12014 /* This might break when accessing outer field from inner
12015 class. TESTME, FIXME */
12016 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
12018 /* Hand stabilize the lhs on both places */
12019 TREE_OPERAND (node, 0) = lvalue;
12020 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
12021 (flag_emit_class_files ? lvalue : save_expr (lvalue));
12023 /* 15.25.2.a: Left hand is not an array access. FIXME */
12024 /* Now complete the RHS. We write it back later on. */
12025 nn = java_complete_tree (TREE_OPERAND (node, 1));
12027 if ((cn = patch_string (nn)))
12030 /* The last part of the rewrite for E1 op= E2 is to have
12031 E1 = (T)(E1 op E2), with T being the type of E1. */
12032 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
12033 TREE_TYPE (lvalue), nn));
12035 /* If the assignment is compound and has reference type,
12036 then ensure the LHS has type String and nothing else. */
12037 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
12038 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
12039 parse_error_context (wfl_op2,
12040 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
12041 lang_printable_name (TREE_TYPE (lvalue), 0));
12043 /* 15.25.2.b: Left hand is an array access. FIXME */
12046 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
12047 function to complete this RHS. Note that a NEW_ARRAY_INIT
12048 might have been already fully expanded if created as a result
12049 of processing an anonymous array initializer. We avoid doing
12050 the operation twice by testing whether the node already bears
12052 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
12053 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
12054 TREE_OPERAND (node, 1));
12055 /* Otherwise we simply complete the RHS */
12057 nn = java_complete_tree (TREE_OPERAND (node, 1));
12059 if (nn == error_mark_node)
12060 return error_mark_node;
12062 /* Write back the RHS as we evaluated it. */
12063 TREE_OPERAND (node, 1) = nn;
12065 /* In case we're handling = with a String as a RHS, we need to
12066 produce a String out of the RHS (it might still be a
12067 STRING_CST or a StringBuffer at this stage */
12068 if ((nn = patch_string (TREE_OPERAND (node, 1))))
12069 TREE_OPERAND (node, 1) = nn;
12071 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
12072 TREE_OPERAND (node, 1))))
12074 /* We return error_mark_node if outer_field_access_fix
12075 detects we write into a final. */
12076 if (nn == error_mark_node)
12077 return error_mark_node;
12082 node = patch_assignment (node, wfl_op1);
12083 /* Reorganize the tree if necessary. */
12084 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
12085 || JSTRING_P (TREE_TYPE (node))))
12086 node = java_refold (node);
12089 /* Seek to set DECL_INITIAL to a proper value, since it might have
12090 undergone a conversion in patch_assignment. We do that only when
12091 it's necessary to have DECL_INITIAL properly set. */
12092 nn = TREE_OPERAND (node, 0);
12093 if (TREE_CODE (nn) == VAR_DECL
12094 && DECL_INITIAL (nn) && CONSTANT_VALUE_P (DECL_INITIAL (nn))
12095 && FIELD_STATIC (nn) && FIELD_FINAL (nn)
12096 && (JPRIMITIVE_TYPE_P (TREE_TYPE (nn))
12097 || TREE_TYPE (nn) == string_ptr_type_node))
12098 DECL_INITIAL (nn) = TREE_OPERAND (node, 1);
12100 CAN_COMPLETE_NORMALLY (node) = 1;
12112 case TRUNC_MOD_EXPR:
12113 case TRUNC_DIV_EXPR:
12115 case TRUTH_ANDIF_EXPR:
12116 case TRUTH_ORIF_EXPR:
12123 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
12124 knows how to handle those cases. */
12125 wfl_op1 = TREE_OPERAND (node, 0);
12126 wfl_op2 = TREE_OPERAND (node, 1);
12128 CAN_COMPLETE_NORMALLY (node) = 1;
12129 /* Don't complete string nodes if dealing with the PLUS operand. */
12130 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
12132 nn = java_complete_tree (wfl_op1);
12133 if (nn == error_mark_node)
12134 return error_mark_node;
12136 TREE_OPERAND (node, 0) = nn;
12138 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
12140 nn = java_complete_tree (wfl_op2);
12141 if (nn == error_mark_node)
12142 return error_mark_node;
12144 TREE_OPERAND (node, 1) = nn;
12146 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
12148 case INSTANCEOF_EXPR:
12149 wfl_op1 = TREE_OPERAND (node, 0);
12150 COMPLETE_CHECK_OP_0 (node);
12151 if (flag_emit_xref)
12153 TREE_TYPE (node) = boolean_type_node;
12156 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
12158 case UNARY_PLUS_EXPR:
12160 case TRUTH_NOT_EXPR:
12162 case PREDECREMENT_EXPR:
12163 case PREINCREMENT_EXPR:
12164 case POSTDECREMENT_EXPR:
12165 case POSTINCREMENT_EXPR:
12167 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
12168 how to handle those cases. */
12169 wfl_op1 = TREE_OPERAND (node, 0);
12170 CAN_COMPLETE_NORMALLY (node) = 1;
12171 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12172 if (TREE_OPERAND (node, 0) == error_mark_node)
12173 return error_mark_node;
12174 node = patch_unaryop (node, wfl_op1);
12175 CAN_COMPLETE_NORMALLY (node) = 1;
12179 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
12180 how to handle those cases. */
12181 wfl_op1 = TREE_OPERAND (node, 0);
12182 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
12183 if (TREE_OPERAND (node, 0) == error_mark_node)
12184 return error_mark_node;
12185 if (!flag_emit_class_files && !flag_emit_xref)
12186 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
12187 /* The same applies to wfl_op2 */
12188 wfl_op2 = TREE_OPERAND (node, 1);
12189 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
12190 if (TREE_OPERAND (node, 1) == error_mark_node)
12191 return error_mark_node;
12192 if (!flag_emit_class_files && !flag_emit_xref)
12193 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
12194 return patch_array_ref (node);
12199 case COMPONENT_REF:
12200 /* The first step in the re-write of qualified name handling. FIXME.
12201 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
12202 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
12203 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
12205 tree name = TREE_OPERAND (node, 1);
12206 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
12207 if (field == NULL_TREE)
12209 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
12210 return error_mark_node;
12212 if (! FIELD_STATIC (field))
12214 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
12215 return error_mark_node;
12224 /* Can't use THIS in a static environment */
12227 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12228 parse_error_context (wfl_operator,
12229 "Keyword `this' used outside allowed context");
12230 TREE_TYPE (node) = error_mark_node;
12231 return error_mark_node;
12233 if (ctxp->explicit_constructor_p)
12235 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12236 parse_error_context
12237 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
12238 TREE_TYPE (node) = error_mark_node;
12239 return error_mark_node;
12241 return current_this;
12243 case CLASS_LITERAL:
12244 CAN_COMPLETE_NORMALLY (node) = 1;
12245 node = patch_incomplete_class_ref (node);
12246 if (node == error_mark_node)
12247 return error_mark_node;
12251 CAN_COMPLETE_NORMALLY (node) = 1;
12252 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
12253 and it's time to turn it into the appropriate String object */
12254 if ((nn = patch_string (node)))
12257 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
12262 /* Complete function call's argument. Return a non zero value is an
12263 error was found. */
12266 complete_function_arguments (node)
12272 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12273 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
12275 tree wfl = TREE_VALUE (cn), parm, temp;
12276 parm = java_complete_tree (wfl);
12278 if (parm == error_mark_node)
12283 /* If have a string literal that we haven't transformed yet or a
12284 crafted string buffer, as a result of use of the the String
12285 `+' operator. Build `parm.toString()' and expand it. */
12286 if ((temp = patch_string (parm)))
12289 TREE_VALUE (cn) = parm;
12291 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
12295 /* Sometimes (for loops and variable initialized during their
12296 declaration), we want to wrap a statement around a WFL and turn it
12300 build_debugable_stmt (location, stmt)
12304 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
12306 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
12307 EXPR_WFL_LINECOL (stmt) = location;
12309 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
12314 build_expr_block (body, decls)
12317 tree node = make_node (BLOCK);
12318 BLOCK_EXPR_DECLS (node) = decls;
12319 BLOCK_EXPR_BODY (node) = body;
12321 TREE_TYPE (node) = TREE_TYPE (body);
12322 TREE_SIDE_EFFECTS (node) = 1;
12326 /* Create a new function block and link it appropriately to current
12327 function block chain */
12332 tree b = build_expr_block (NULL_TREE, NULL_TREE);
12334 /* Link block B supercontext to the previous block. The current
12335 function DECL is used as supercontext when enter_a_block is called
12336 for the first time for a given function. The current function body
12337 (DECL_FUNCTION_BODY) is set to be block B. */
12339 tree fndecl = current_function_decl;
12342 BLOCK_SUPERCONTEXT (b) = current_static_block;
12343 current_static_block = b;
12346 else if (!DECL_FUNCTION_BODY (fndecl))
12348 BLOCK_SUPERCONTEXT (b) = fndecl;
12349 DECL_FUNCTION_BODY (fndecl) = b;
12353 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
12354 DECL_FUNCTION_BODY (fndecl) = b;
12359 /* Exit a block by changing the current function body
12360 (DECL_FUNCTION_BODY) to the current block super context, only if
12361 the block being exited isn't the method's top level one. */
12367 if (current_function_decl)
12369 b = DECL_FUNCTION_BODY (current_function_decl);
12370 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
12371 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
12375 b = current_static_block;
12377 if (BLOCK_SUPERCONTEXT (b))
12378 current_static_block = BLOCK_SUPERCONTEXT (b);
12383 /* Lookup for NAME in the nested function's blocks, all the way up to
12384 the current toplevel one. It complies with Java's local variable
12388 lookup_name_in_blocks (name)
12391 tree b = GET_CURRENT_BLOCK (current_function_decl);
12393 while (b != current_function_decl)
12397 /* Paranoid sanity check. To be removed */
12398 if (TREE_CODE (b) != BLOCK)
12401 for (current = BLOCK_EXPR_DECLS (b); current;
12402 current = TREE_CHAIN (current))
12403 if (DECL_NAME (current) == name)
12405 b = BLOCK_SUPERCONTEXT (b);
12411 maybe_absorb_scoping_blocks ()
12413 while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
12415 tree b = exit_block ();
12416 java_method_add_stmt (current_function_decl, b);
12417 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
12422 /* This section of the source is reserved to build_* functions that
12423 are building incomplete tree nodes and the patch_* functions that
12424 are completing them. */
12426 /* Wrap a non WFL node around a WFL. */
12429 build_wfl_wrap (node, location)
12433 tree wfl, node_to_insert = node;
12435 /* We want to process THIS . xxx symbolicaly, to keep it consistent
12436 with the way we're processing SUPER. A THIS from a primary as a
12437 different form than a SUPER. Turn THIS into something symbolic */
12438 if (TREE_CODE (node) == THIS_EXPR)
12439 node_to_insert = wfl = build_wfl_node (this_identifier_node);
12441 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12443 EXPR_WFL_LINECOL (wfl) = location;
12444 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12448 /* Build a super() constructor invocation. Returns empty_stmt_node if
12449 we're currently dealing with the class java.lang.Object. */
12452 build_super_invocation (mdecl)
12455 if (DECL_CONTEXT (mdecl) == object_type_node)
12456 return empty_stmt_node;
12459 tree super_wfl = build_wfl_node (super_identifier_node);
12460 tree a = NULL_TREE, t;
12461 /* If we're dealing with an anonymous class, pass the arguments
12462 of the crafted constructor along. */
12463 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12465 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12466 for (; t != end_params_node; t = TREE_CHAIN (t))
12467 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12469 return build_method_invocation (super_wfl, a);
12473 /* Build a SUPER/THIS qualified method invocation. */
12476 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
12483 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12484 EXPR_WFL_LINECOL (wfl) = lloc;
12485 invok = build_method_invocation (name, args);
12486 return make_qualified_primary (wfl, invok, rloc);
12489 /* Build an incomplete CALL_EXPR node. */
12492 build_method_invocation (name, args)
12496 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12497 TREE_SIDE_EFFECTS (call) = 1;
12498 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12502 /* Build an incomplete new xxx(...) node. */
12505 build_new_invocation (name, args)
12508 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12509 TREE_SIDE_EFFECTS (call) = 1;
12510 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12514 /* Build an incomplete assignment expression. */
12517 build_assignment (op, op_location, lhs, rhs)
12518 int op, op_location;
12522 /* Build the corresponding binop if we deal with a Compound
12523 Assignment operator. Mark the binop sub-tree as part of a
12524 Compound Assignment expression */
12525 if (op != ASSIGN_TK)
12527 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12528 COMPOUND_ASSIGN_P (rhs) = 1;
12530 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12531 TREE_SIDE_EFFECTS (assignment) = 1;
12532 EXPR_WFL_LINECOL (assignment) = op_location;
12536 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
12539 print_int_node (node)
12542 static char buffer [80];
12543 if (TREE_CONSTANT_OVERFLOW (node))
12544 sprintf (buffer, "<overflow>");
12546 if (TREE_INT_CST_HIGH (node) == 0)
12547 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12548 TREE_INT_CST_LOW (node));
12549 else if (TREE_INT_CST_HIGH (node) == -1
12550 && TREE_INT_CST_LOW (node) != 0)
12553 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12554 -TREE_INT_CST_LOW (node));
12557 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12558 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12564 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12567 /* 15.25 Assignment operators. */
12570 patch_assignment (node, wfl_op1)
12574 tree rhs = TREE_OPERAND (node, 1);
12575 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12576 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12577 int error_found = 0;
12578 int lvalue_from_array = 0;
12581 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12583 /* Lhs can be a named variable */
12584 if (JDECL_P (lvalue))
12586 lhs_type = TREE_TYPE (lvalue);
12588 /* Or Lhs can be a array access. Should that be lvalue ? FIXME +
12589 comment on reason why */
12590 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12592 lhs_type = TREE_TYPE (lvalue);
12593 lvalue_from_array = 1;
12595 /* Or a field access */
12596 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12597 lhs_type = TREE_TYPE (lvalue);
12598 /* Or a function return slot */
12599 else if (TREE_CODE (lvalue) == RESULT_DECL)
12601 /* If the return type is an integral type, then we create the
12602 RESULT_DECL with a promoted type, but we need to do these
12603 checks against the unpromoted type to ensure type safety. So
12604 here we look at the real type, not the type of the decl we
12606 lhs_type = TREE_TYPE (TREE_TYPE (current_function_decl));
12609 /* Otherwise, we might want to try to write into an optimized static
12610 final, this is an of a different nature, reported further on. */
12611 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12612 && resolve_expression_name (wfl_op1, &llvalue))
12614 lhs_type = TREE_TYPE (lvalue);
12618 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12622 rhs_type = TREE_TYPE (rhs);
12624 /* 5.1 Try the assignment conversion for builtin type. */
12625 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12627 /* 5.2 If it failed, try a reference conversion */
12628 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12629 lhs_type = promote_type (rhs_type);
12631 /* 15.25.2 If we have a compound assignment, convert RHS into the
12633 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12634 new_rhs = convert (lhs_type, rhs);
12636 /* Explicit cast required. This is an error */
12639 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12640 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12642 char operation [32]; /* Max size known */
12644 /* If the assignment is part of a declaration, we use the WFL of
12645 the declared variable to point out the error and call it a
12646 declaration problem. If the assignment is a genuine =
12647 operator, we call is a operator `=' problem, otherwise we
12648 call it an assignment problem. In both of these last cases,
12649 we use the WFL of the operator to indicate the error. */
12651 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12654 strcpy (operation, "declaration");
12658 wfl = wfl_operator;
12659 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12660 strcpy (operation, "assignment");
12661 else if (is_return)
12662 strcpy (operation, "`return'");
12664 strcpy (operation, "`='");
12667 if (!valid_cast_to_p (rhs_type, lhs_type))
12668 parse_error_context
12669 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12670 operation, t1, t2);
12672 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12673 operation, t1, t2);
12674 free (t1); free (t2);
12679 return error_mark_node;
12681 /* If we're processing a `return' statement, promote the actual type
12682 to the promoted type. */
12684 new_rhs = convert (TREE_TYPE (lvalue), new_rhs);
12686 /* 10.10: Array Store Exception runtime check */
12687 if (!flag_emit_class_files
12689 && lvalue_from_array
12690 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12693 tree base = lvalue;
12695 /* We need to retrieve the right argument for
12696 _Jv_CheckArrayStore. This is somewhat complicated by bounds
12697 and null pointer checks, both of which wrap the operand in
12698 one layer of COMPOUND_EXPR. */
12699 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12700 base = TREE_OPERAND (lvalue, 0);
12703 tree op = TREE_OPERAND (base, 0);
12705 /* We can have a SAVE_EXPR here when doing String +=. */
12706 if (TREE_CODE (op) == SAVE_EXPR)
12707 op = TREE_OPERAND (op, 0);
12708 /* We can have a COMPOUND_EXPR here when doing bounds check. */
12709 if (TREE_CODE (op) == COMPOUND_EXPR)
12710 op = TREE_OPERAND (op, 1);
12711 base = TREE_OPERAND (op, 0);
12712 /* Strip the last PLUS_EXPR to obtain the base. */
12713 if (TREE_CODE (base) == PLUS_EXPR)
12714 base = TREE_OPERAND (base, 0);
12717 /* Build the invocation of _Jv_CheckArrayStore */
12718 new_rhs = save_expr (new_rhs);
12719 check = build (CALL_EXPR, void_type_node,
12720 build_address_of (soft_checkarraystore_node),
12721 tree_cons (NULL_TREE, base,
12722 build_tree_list (NULL_TREE, new_rhs)),
12724 TREE_SIDE_EFFECTS (check) = 1;
12726 /* We have to decide on an insertion point */
12727 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12730 if (flag_bounds_check)
12732 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12733 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12734 build (COMPOUND_EXPR, void_type_node, t, check);
12737 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12738 check, TREE_OPERAND (lvalue, 1));
12740 else if (flag_bounds_check)
12742 tree hook = lvalue;
12743 tree compound = TREE_OPERAND (lvalue, 0);
12744 tree bound_check, new_compound;
12746 if (TREE_CODE (compound) == SAVE_EXPR)
12748 compound = TREE_OPERAND (compound, 0);
12749 hook = TREE_OPERAND (hook, 0);
12752 /* Find the array bound check, hook the original array access. */
12753 bound_check = TREE_OPERAND (compound, 0);
12754 TREE_OPERAND (hook, 0) = TREE_OPERAND (compound, 1);
12756 /* Make sure the bound check will happen before the store check */
12758 build (COMPOUND_EXPR, void_type_node, bound_check, check);
12760 /* Re-assemble the augmented array access. */
12761 lvalue = build (COMPOUND_EXPR, TREE_TYPE (lvalue),
12762 new_compound, lvalue);
12765 lvalue = build (COMPOUND_EXPR, TREE_TYPE (lvalue), check, lvalue);
12768 /* Final locals can be used as case values in switch
12769 statement. Prepare them for this eventuality. */
12770 if (TREE_CODE (lvalue) == VAR_DECL
12771 && DECL_FINAL (lvalue)
12772 && TREE_CONSTANT (new_rhs)
12773 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12774 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12777 TREE_CONSTANT (lvalue) = 1;
12778 DECL_INITIAL (lvalue) = new_rhs;
12781 TREE_OPERAND (node, 0) = lvalue;
12782 TREE_OPERAND (node, 1) = new_rhs;
12783 TREE_TYPE (node) = lhs_type;
12787 /* Check that type SOURCE can be cast into type DEST. If the cast
12788 can't occur at all, return NULL; otherwise, return a possibly
12792 try_reference_assignconv (lhs_type, rhs)
12793 tree lhs_type, rhs;
12795 tree new_rhs = NULL_TREE;
12796 tree rhs_type = TREE_TYPE (rhs);
12798 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12800 /* `null' may be assigned to any reference type */
12801 if (rhs == null_pointer_node)
12802 new_rhs = null_pointer_node;
12803 /* Try the reference assignment conversion */
12804 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12806 /* This is a magic assignment that we process differently */
12807 else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
12813 /* Check that RHS can be converted into LHS_TYPE by the assignment
12814 conversion (5.2), for the cases of RHS being a builtin type. Return
12815 NULL_TREE if the conversion fails or if because RHS isn't of a
12816 builtin type. Return a converted RHS if the conversion is possible. */
12819 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12820 tree wfl_op1, lhs_type, rhs;
12822 tree new_rhs = NULL_TREE;
12823 tree rhs_type = TREE_TYPE (rhs);
12825 /* Handle boolean specially. */
12826 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12827 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12829 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12830 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12834 /* Zero accepted everywhere */
12835 else if (TREE_CODE (rhs) == INTEGER_CST
12836 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12837 && JPRIMITIVE_TYPE_P (rhs_type))
12838 new_rhs = convert (lhs_type, rhs);
12840 /* 5.1.1 Try Identity Conversion,
12841 5.1.2 Try Widening Primitive Conversion */
12842 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12843 new_rhs = convert (lhs_type, rhs);
12845 /* Try a narrowing primitive conversion (5.1.3):
12846 - expression is a constant expression of type int AND
12847 - variable is byte, short or char AND
12848 - The value of the expression is representable in the type of the
12850 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12851 && (lhs_type == byte_type_node || lhs_type == char_type_node
12852 || lhs_type == short_type_node))
12854 if (int_fits_type_p (rhs, lhs_type))
12855 new_rhs = convert (lhs_type, rhs);
12856 else if (wfl_op1) /* Might be called with a NULL */
12857 parse_warning_context
12858 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
12859 print_int_node (rhs), lang_printable_name (lhs_type, 0));
12860 /* Reported a warning that will turn into an error further
12861 down, so we don't return */
12867 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12868 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
12869 0 is the conversion test fails. This implements parts the method
12870 invocation convertion (5.3). */
12873 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12874 tree lhs_type, rhs_type;
12876 /* 5.1.1: This is the identity conversion part. */
12877 if (lhs_type == rhs_type)
12880 /* Reject non primitive types and boolean conversions. */
12881 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
12884 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12885 than a char can't be converted into a char. Short can't too, but
12886 the < test below takes care of that */
12887 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12890 /* Accept all promoted type here. Note, we can't use <= in the test
12891 below, because we still need to bounce out assignments of short
12892 to char and the likes */
12893 if (lhs_type == int_type_node
12894 && (rhs_type == promoted_byte_type_node
12895 || rhs_type == promoted_short_type_node
12896 || rhs_type == promoted_char_type_node
12897 || rhs_type == promoted_boolean_type_node))
12900 /* From here, an integral is widened if its precision is smaller
12901 than the precision of the LHS or if the LHS is a floating point
12902 type, or the RHS is a float and the RHS a double. */
12903 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12904 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12905 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12906 || (rhs_type == float_type_node && lhs_type == double_type_node))
12912 /* Check that something of SOURCE type can be assigned or cast to
12913 something of DEST type at runtime. Return 1 if the operation is
12914 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12915 were SOURCE is cast into DEST, which borrows a lot of the
12916 assignment check. */
12919 valid_ref_assignconv_cast_p (source, dest, cast)
12924 /* SOURCE or DEST might be null if not from a declared entity. */
12925 if (!source || !dest)
12927 if (JNULLP_TYPE_P (source))
12929 if (TREE_CODE (source) == POINTER_TYPE)
12930 source = TREE_TYPE (source);
12931 if (TREE_CODE (dest) == POINTER_TYPE)
12932 dest = TREE_TYPE (dest);
12934 /* If source and dest are being compiled from bytecode, they may need to
12936 if (CLASS_P (source) && !CLASS_LOADED_P (source))
12938 load_class (source, 1);
12939 safe_layout_class (source);
12941 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12943 load_class (dest, 1);
12944 safe_layout_class (dest);
12947 /* Case where SOURCE is a class type */
12948 if (TYPE_CLASS_P (source))
12950 if (TYPE_CLASS_P (dest))
12951 return (source == dest
12952 || inherits_from_p (source, dest)
12953 || (cast && inherits_from_p (dest, source)));
12954 if (TYPE_INTERFACE_P (dest))
12956 /* If doing a cast and SOURCE is final, the operation is
12957 always correct a compile time (because even if SOURCE
12958 does not implement DEST, a subclass of SOURCE might). */
12959 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12961 /* Otherwise, SOURCE must implement DEST */
12962 return interface_of_p (dest, source);
12964 /* DEST is an array, cast permited if SOURCE is of Object type */
12965 return (cast && source == object_type_node ? 1 : 0);
12967 if (TYPE_INTERFACE_P (source))
12969 if (TYPE_CLASS_P (dest))
12971 /* If not casting, DEST must be the Object type */
12973 return dest == object_type_node;
12974 /* We're doing a cast. The cast is always valid is class
12975 DEST is not final, otherwise, DEST must implement SOURCE */
12976 else if (!CLASS_FINAL (TYPE_NAME (dest)))
12979 return interface_of_p (source, dest);
12981 if (TYPE_INTERFACE_P (dest))
12983 /* If doing a cast, then if SOURCE and DEST contain method
12984 with the same signature but different return type, then
12985 this is a (compile time) error */
12988 tree method_source, method_dest;
12992 for (method_source = TYPE_METHODS (source); method_source;
12993 method_source = TREE_CHAIN (method_source))
12996 build_java_argument_signature (TREE_TYPE (method_source));
12997 source_type = TREE_TYPE (TREE_TYPE (method_source));
12998 source_name = DECL_NAME (method_source);
12999 for (method_dest = TYPE_METHODS (dest);
13000 method_dest; method_dest = TREE_CHAIN (method_dest))
13002 build_java_argument_signature (TREE_TYPE (method_dest))
13003 && source_name == DECL_NAME (method_dest)
13004 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
13010 return source == dest || interface_of_p (dest, source);
13016 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
13017 || (DECL_NAME (TYPE_NAME (source))
13018 == java_io_serializable)));
13021 if (TYPE_ARRAY_P (source))
13023 if (TYPE_CLASS_P (dest))
13024 return dest == object_type_node;
13025 /* Can't cast an array to an interface unless the interface is
13026 java.lang.Cloneable or java.io.Serializable. */
13027 if (TYPE_INTERFACE_P (dest))
13028 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
13029 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
13032 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
13033 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
13035 /* In case of severe errors, they turn out null */
13036 if (!dest_element_type || !source_element_type)
13038 if (source_element_type == dest_element_type)
13040 return valid_ref_assignconv_cast_p (source_element_type,
13041 dest_element_type, cast);
13049 valid_cast_to_p (source, dest)
13053 if (TREE_CODE (source) == POINTER_TYPE)
13054 source = TREE_TYPE (source);
13055 if (TREE_CODE (dest) == POINTER_TYPE)
13056 dest = TREE_TYPE (dest);
13058 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
13059 return valid_ref_assignconv_cast_p (source, dest, 1);
13061 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
13064 else if (TREE_CODE (source) == BOOLEAN_TYPE
13065 && TREE_CODE (dest) == BOOLEAN_TYPE)
13072 do_unary_numeric_promotion (arg)
13075 tree type = TREE_TYPE (arg);
13076 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
13077 || TREE_CODE (type) == CHAR_TYPE)
13078 arg = convert (int_type_node, arg);
13082 /* Return a non zero value if SOURCE can be converted into DEST using
13083 the method invocation conversion rule (5.3). */
13085 valid_method_invocation_conversion_p (dest, source)
13088 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
13089 && valid_builtin_assignconv_identity_widening_p (dest, source))
13090 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
13091 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
13092 && valid_ref_assignconv_cast_p (source, dest, 0)));
13095 /* Build an incomplete binop expression. */
13098 build_binop (op, op_location, op1, op2)
13103 tree binop = build (op, NULL_TREE, op1, op2);
13104 TREE_SIDE_EFFECTS (binop) = 1;
13105 /* Store the location of the operator, for better error report. The
13106 string of the operator will be rebuild based on the OP value. */
13107 EXPR_WFL_LINECOL (binop) = op_location;
13111 /* Build the string of the operator retained by NODE. If NODE is part
13112 of a compound expression, add an '=' at the end of the string. This
13113 function is called when an error needs to be reported on an
13114 operator. The string is returned as a pointer to a static character
13118 operator_string (node)
13121 #define BUILD_OPERATOR_STRING(S) \
13123 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
13127 static char buffer [10];
13128 switch (TREE_CODE (node))
13130 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
13131 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
13132 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
13133 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13134 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
13135 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
13136 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
13137 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
13138 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
13139 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
13140 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
13141 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
13142 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
13143 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
13144 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
13145 case GT_EXPR: BUILD_OPERATOR_STRING (">");
13146 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
13147 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
13148 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
13149 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
13150 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
13151 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
13152 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
13153 case PREINCREMENT_EXPR: /* Fall through */
13154 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
13155 case PREDECREMENT_EXPR: /* Fall through */
13156 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
13158 internal_error ("unregistered operator %s",
13159 tree_code_name [TREE_CODE (node)]);
13162 #undef BUILD_OPERATOR_STRING
13165 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
13168 java_decl_equiv (var_acc1, var_acc2)
13169 tree var_acc1, var_acc2;
13171 if (JDECL_P (var_acc1))
13172 return (var_acc1 == var_acc2);
13174 return (TREE_CODE (var_acc1) == COMPONENT_REF
13175 && TREE_CODE (var_acc2) == COMPONENT_REF
13176 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13177 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13178 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13181 /* Return a non zero value if CODE is one of the operators that can be
13182 used in conjunction with the `=' operator in a compound assignment. */
13185 binop_compound_p (code)
13186 enum tree_code code;
13189 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13190 if (binop_lookup [i] == code)
13193 return i < BINOP_COMPOUND_CANDIDATES;
13196 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13202 tree c, b, ns, decl;
13204 if (TREE_CODE (t) != MODIFY_EXPR)
13207 c = TREE_OPERAND (t, 1);
13208 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13209 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13210 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13213 /* Now the left branch of the binary operator. */
13214 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13215 if (! (b && TREE_CODE (b) == NOP_EXPR
13216 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13219 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13220 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13221 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13224 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13225 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13226 /* It's got to be the an equivalent decl */
13227 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13229 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13230 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13231 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13232 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13233 /* Change the right part of the BINOP_EXPR */
13234 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13240 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13241 errors but we modify NODE so that it contains the type computed
13242 according to the expression, when it's fixed. Otherwise, we write
13243 error_mark_node as the type. It allows us to further the analysis
13244 of remaining nodes and detects more errors in certain cases. */
13247 patch_binop (node, wfl_op1, wfl_op2)
13252 tree op1 = TREE_OPERAND (node, 0);
13253 tree op2 = TREE_OPERAND (node, 1);
13254 tree op1_type = TREE_TYPE (op1);
13255 tree op2_type = TREE_TYPE (op2);
13256 tree prom_type = NULL_TREE, cn;
13257 enum tree_code code = TREE_CODE (node);
13259 /* If 1, tell the routine that we have to return error_mark_node
13260 after checking for the initialization of the RHS */
13261 int error_found = 0;
13263 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13265 /* If either op<n>_type are NULL, this might be early signs of an
13266 error situation, unless it's too early to tell (in case we're
13267 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13268 correctly so the error can be later on reported accurately. */
13269 if (! (code == PLUS_EXPR || code == NE_EXPR
13270 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13275 n = java_complete_tree (op1);
13276 op1_type = TREE_TYPE (n);
13280 n = java_complete_tree (op2);
13281 op2_type = TREE_TYPE (n);
13287 /* 15.16 Multiplicative operators */
13288 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13289 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13290 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13291 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13292 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13294 if (!JNUMERIC_TYPE_P (op1_type))
13295 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13296 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13297 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13298 TREE_TYPE (node) = error_mark_node;
13302 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13304 /* Detect integral division by zero */
13305 if ((code == RDIV_EXPR || code == TRUNC_MOD_EXPR)
13306 && TREE_CODE (prom_type) == INTEGER_TYPE
13307 && (op2 == integer_zero_node || op2 == long_zero_node ||
13308 (TREE_CODE (op2) == INTEGER_CST &&
13309 ! TREE_INT_CST_LOW (op2) && ! TREE_INT_CST_HIGH (op2))))
13311 parse_warning_context (wfl_operator, "Evaluating this expression will result in an arithmetic exception being thrown");
13312 TREE_CONSTANT (node) = 0;
13315 /* Change the division operator if necessary */
13316 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13317 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13319 /* Before divisions as is disapear, try to simplify and bail if
13320 applicable, otherwise we won't perform even simple
13321 simplifications like (1-1)/3. We can't do that with floating
13322 point number, folds can't handle them at this stage. */
13323 if (code == RDIV_EXPR && TREE_CONSTANT (op1) && TREE_CONSTANT (op2)
13324 && JINTEGRAL_TYPE_P (op1) && JINTEGRAL_TYPE_P (op2))
13326 TREE_TYPE (node) = prom_type;
13327 node = fold (node);
13328 if (TREE_CODE (node) != code)
13332 if (TREE_CODE (prom_type) == INTEGER_TYPE
13333 && flag_use_divide_subroutine
13334 && ! flag_emit_class_files
13335 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13336 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13338 /* This one is more complicated. FLOATs are processed by a
13339 function call to soft_fmod. Duplicate the value of the
13340 COMPOUND_ASSIGN_P flag. */
13341 if (code == TRUNC_MOD_EXPR)
13343 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13344 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13345 TREE_SIDE_EFFECTS (mod)
13346 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13351 /* 15.17 Additive Operators */
13352 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13354 /* Operation is valid if either one argument is a string
13355 constant, a String object or a StringBuffer crafted for the
13356 purpose of the a previous usage of the String concatenation
13359 if (TREE_CODE (op1) == STRING_CST
13360 || TREE_CODE (op2) == STRING_CST
13361 || JSTRING_TYPE_P (op1_type)
13362 || JSTRING_TYPE_P (op2_type)
13363 || IS_CRAFTED_STRING_BUFFER_P (op1)
13364 || IS_CRAFTED_STRING_BUFFER_P (op2))
13365 return build_string_concatenation (op1, op2);
13367 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13369 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13371 if (!JNUMERIC_TYPE_P (op1_type))
13372 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13373 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13374 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13375 TREE_TYPE (node) = error_mark_node;
13379 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13382 /* 15.18 Shift Operators */
13386 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13388 if (!JINTEGRAL_TYPE_P (op1_type))
13389 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13392 if (JNUMERIC_TYPE_P (op2_type))
13393 parse_error_context (wfl_operator,
13394 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
13395 operator_string (node),
13396 lang_printable_name (op2_type, 0));
13398 parse_error_context (wfl_operator,
13399 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
13400 operator_string (node),
13401 lang_printable_name (op2_type, 0));
13403 TREE_TYPE (node) = error_mark_node;
13408 /* Unary numeric promotion (5.6.1) is performed on each operand
13410 op1 = do_unary_numeric_promotion (op1);
13411 op2 = do_unary_numeric_promotion (op2);
13413 /* The type of the shift expression is the type of the promoted
13414 type of the left-hand operand */
13415 prom_type = TREE_TYPE (op1);
13417 /* Shift int only up to 0x1f and long up to 0x3f */
13418 if (prom_type == int_type_node)
13419 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13420 build_int_2 (0x1f, 0)));
13422 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13423 build_int_2 (0x3f, 0)));
13425 /* The >>> operator is a >> operating on unsigned quantities */
13426 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13429 tree utype = unsigned_type (prom_type);
13430 op1 = convert (utype, op1);
13431 TREE_SET_CODE (node, RSHIFT_EXPR);
13432 TREE_OPERAND (node, 0) = op1;
13433 TREE_OPERAND (node, 1) = op2;
13434 TREE_TYPE (node) = utype;
13435 to_return = convert (prom_type, node);
13436 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13437 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13438 TREE_SIDE_EFFECTS (to_return)
13439 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13444 /* 15.19.1 Type Comparison Operator instaceof */
13445 case INSTANCEOF_EXPR:
13447 TREE_TYPE (node) = boolean_type_node;
13449 if (!(op2_type = resolve_type_during_patch (op2)))
13450 return error_mark_node;
13452 /* The first operand must be a reference type or the null type */
13453 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13454 error_found = 1; /* Error reported further below */
13456 /* The second operand must be a reference type */
13457 if (!JREFERENCE_TYPE_P (op2_type))
13459 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13460 parse_error_context
13461 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13462 lang_printable_name (op2_type, 0));
13466 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13468 /* If the first operand is null, the result is always false */
13469 if (op1 == null_pointer_node)
13470 return boolean_false_node;
13471 else if (flag_emit_class_files)
13473 TREE_OPERAND (node, 1) = op2_type;
13474 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13477 /* Otherwise we have to invoke instance of to figure it out */
13479 return build_instanceof (op1, op2_type);
13481 /* There is no way the expression operand can be an instance of
13482 the type operand. This is a compile time error. */
13485 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13486 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13487 parse_error_context
13488 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13489 t1, lang_printable_name (op2_type, 0));
13496 /* 15.21 Bitwise and Logical Operators */
13500 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13501 /* Binary numeric promotion is performed on both operand and the
13502 expression retain that type */
13503 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13505 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13506 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13507 /* The type of the bitwise operator expression is BOOLEAN */
13508 prom_type = boolean_type_node;
13511 if (!JINTEGRAL_TYPE_P (op1_type))
13512 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13513 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13514 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13515 TREE_TYPE (node) = error_mark_node;
13517 /* Insert a break here if adding thing before the switch's
13518 break for this case */
13522 /* 15.22 Conditional-And Operator */
13523 case TRUTH_ANDIF_EXPR:
13524 /* 15.23 Conditional-Or Operator */
13525 case TRUTH_ORIF_EXPR:
13526 /* Operands must be of BOOLEAN type */
13527 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13528 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13530 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13531 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13532 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13533 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13534 TREE_TYPE (node) = boolean_type_node;
13538 else if (integer_zerop (op1))
13540 return code == TRUTH_ANDIF_EXPR ? op1 : op2;
13542 else if (integer_onep (op1))
13544 return code == TRUTH_ANDIF_EXPR ? op2 : op1;
13546 /* The type of the conditional operators is BOOLEAN */
13547 prom_type = boolean_type_node;
13550 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13555 /* The type of each of the operands must be a primitive numeric
13557 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13559 if (!JNUMERIC_TYPE_P (op1_type))
13560 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13561 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13562 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13563 TREE_TYPE (node) = boolean_type_node;
13567 /* Binary numeric promotion is performed on the operands */
13568 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13569 /* The type of the relation expression is always BOOLEAN */
13570 prom_type = boolean_type_node;
13573 /* 15.20 Equality Operator */
13576 /* It's time for us to patch the strings. */
13577 if ((cn = patch_string (op1)))
13580 op1_type = TREE_TYPE (op1);
13582 if ((cn = patch_string (op2)))
13585 op2_type = TREE_TYPE (op2);
13588 /* 15.20.1 Numerical Equality Operators == and != */
13589 /* Binary numeric promotion is performed on the operands */
13590 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13591 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13593 /* 15.20.2 Boolean Equality Operators == and != */
13594 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13595 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13596 ; /* Nothing to do here */
13598 /* 15.20.3 Reference Equality Operators == and != */
13599 /* Types have to be either references or the null type. If
13600 they're references, it must be possible to convert either
13601 type to the other by casting conversion. */
13602 else if (op1 == null_pointer_node || op2 == null_pointer_node
13603 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13604 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13605 || valid_ref_assignconv_cast_p (op2_type,
13607 ; /* Nothing to do here */
13609 /* Else we have an error figure what can't be converted into
13610 what and report the error */
13614 t1 = xstrdup (lang_printable_name (op1_type, 0));
13615 parse_error_context
13617 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13618 operator_string (node), t1,
13619 lang_printable_name (op2_type, 0));
13621 TREE_TYPE (node) = boolean_type_node;
13625 prom_type = boolean_type_node;
13632 return error_mark_node;
13634 TREE_OPERAND (node, 0) = op1;
13635 TREE_OPERAND (node, 1) = op2;
13636 TREE_TYPE (node) = prom_type;
13637 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13639 if (flag_emit_xref)
13642 /* fold does not respect side-effect order as required for Java but not C.
13643 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13646 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13647 : ! TREE_SIDE_EFFECTS (node))
13648 node = fold (node);
13652 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13653 zero value, the value of CSTE comes after the valude of STRING */
13656 do_merge_string_cste (cste, string, string_len, after)
13658 const char *string;
13659 int string_len, after;
13661 const char *old = TREE_STRING_POINTER (cste);
13662 int old_len = TREE_STRING_LENGTH (cste);
13663 int len = old_len + string_len;
13664 char *new = alloca (len+1);
13668 memcpy (new, string, string_len);
13669 memcpy (&new [string_len], old, old_len);
13673 memcpy (new, old, old_len);
13674 memcpy (&new [old_len], string, string_len);
13677 return build_string (len, new);
13680 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13681 new STRING_CST on success, NULL_TREE on failure */
13684 merge_string_cste (op1, op2, after)
13688 /* Handle two string constants right away */
13689 if (TREE_CODE (op2) == STRING_CST)
13690 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13691 TREE_STRING_LENGTH (op2), after);
13693 /* Reasonable integer constant can be treated right away */
13694 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13696 static const char *const boolean_true = "true";
13697 static const char *const boolean_false = "false";
13698 static const char *const null_pointer = "null";
13700 const char *string;
13702 if (op2 == boolean_true_node)
13703 string = boolean_true;
13704 else if (op2 == boolean_false_node)
13705 string = boolean_false;
13706 else if (op2 == null_pointer_node)
13707 string = null_pointer;
13708 else if (TREE_TYPE (op2) == char_type_node)
13710 ch[0] = (char )TREE_INT_CST_LOW (op2);
13715 string = print_int_node (op2);
13717 return do_merge_string_cste (op1, string, strlen (string), after);
13722 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13723 has to be a STRING_CST and the other part must be a STRING_CST or a
13724 INTEGRAL constant. Return a new STRING_CST if the operation
13725 succeed, NULL_TREE otherwise.
13727 If the case we want to optimize for space, we might want to return
13728 NULL_TREE for each invocation of this routine. FIXME */
13731 string_constant_concatenation (op1, op2)
13734 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13739 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13740 rest = (string == op1 ? op2 : op1);
13741 invert = (string == op1 ? 0 : 1 );
13743 /* Walk REST, only if it looks reasonable */
13744 if (TREE_CODE (rest) != STRING_CST
13745 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13746 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13747 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13749 rest = java_complete_tree (rest);
13750 if (rest == error_mark_node)
13751 return error_mark_node;
13752 rest = fold (rest);
13754 return merge_string_cste (string, rest, invert);
13759 /* Implement the `+' operator. Does static optimization if possible,
13760 otherwise create (if necessary) and append elements to a
13761 StringBuffer. The StringBuffer will be carried around until it is
13762 used for a function call or an assignment. Then toString() will be
13763 called on it to turn it into a String object. */
13766 build_string_concatenation (op1, op2)
13770 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13772 if (flag_emit_xref)
13773 return build (PLUS_EXPR, string_type_node, op1, op2);
13775 /* Try to do some static optimization */
13776 if ((result = string_constant_concatenation (op1, op2)))
13779 /* Discard empty strings on either side of the expression */
13780 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13785 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13788 /* If operands are string constant, turn then into object references */
13789 if (TREE_CODE (op1) == STRING_CST)
13790 op1 = patch_string_cst (op1);
13791 if (op2 && TREE_CODE (op2) == STRING_CST)
13792 op2 = patch_string_cst (op2);
13794 /* If either one of the constant is null and the other non null
13795 operand is a String object, return it. */
13796 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13799 /* If OP1 isn't already a StringBuffer, create and
13800 initialize a new one */
13801 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13803 /* Two solutions here:
13804 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13805 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13806 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13807 op1 = BUILD_STRING_BUFFER (op1);
13810 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13811 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13817 /* OP1 is no longer the last node holding a crafted StringBuffer */
13818 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13819 /* Create a node for `{new...,xxx}.append (op2)' */
13821 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13824 /* Mark the last node holding a crafted StringBuffer */
13825 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13827 TREE_SIDE_EFFECTS (op1) = side_effects;
13831 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13832 StringBuffer. If no string were found to be patched, return
13836 patch_string (node)
13839 if (node == error_mark_node)
13840 return error_mark_node;
13841 if (TREE_CODE (node) == STRING_CST)
13842 return patch_string_cst (node);
13843 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13845 int saved = ctxp->explicit_constructor_p;
13846 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13848 /* Temporary disable forbid the use of `this'. */
13849 ctxp->explicit_constructor_p = 0;
13850 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13851 /* String concatenation arguments must be evaluated in order too. */
13852 ret = force_evaluation_order (ret);
13853 /* Restore it at its previous value */
13854 ctxp->explicit_constructor_p = saved;
13860 /* Build the internal representation of a string constant. */
13863 patch_string_cst (node)
13867 if (! flag_emit_class_files)
13869 node = get_identifier (TREE_STRING_POINTER (node));
13870 location = alloc_name_constant (CONSTANT_String, node);
13871 node = build_ref_from_constant_pool (location);
13873 TREE_TYPE (node) = string_ptr_type_node;
13874 TREE_CONSTANT (node) = 1;
13878 /* Build an incomplete unary operator expression. */
13881 build_unaryop (op_token, op_location, op1)
13882 int op_token, op_location;
13889 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13890 case MINUS_TK: op = NEGATE_EXPR; break;
13891 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13892 case NOT_TK: op = BIT_NOT_EXPR; break;
13896 unaryop = build1 (op, NULL_TREE, op1);
13897 TREE_SIDE_EFFECTS (unaryop) = 1;
13898 /* Store the location of the operator, for better error report. The
13899 string of the operator will be rebuild based on the OP value. */
13900 EXPR_WFL_LINECOL (unaryop) = op_location;
13904 /* Special case for the ++/-- operators, since they require an extra
13905 argument to build, which is set to NULL and patched
13906 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13909 build_incdec (op_token, op_location, op1, is_post_p)
13910 int op_token, op_location;
13914 static const enum tree_code lookup [2][2] =
13916 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13917 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13919 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13920 NULL_TREE, op1, NULL_TREE);
13921 TREE_SIDE_EFFECTS (node) = 1;
13922 /* Store the location of the operator, for better error report. The
13923 string of the operator will be rebuild based on the OP value. */
13924 EXPR_WFL_LINECOL (node) = op_location;
13928 /* Build an incomplete cast operator, based on the use of the
13929 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13930 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13931 though its type is already set. */
13934 build_cast (location, type, exp)
13938 tree node = build1 (CONVERT_EXPR, type, exp);
13939 EXPR_WFL_LINECOL (node) = location;
13943 /* Build an incomplete class reference operator. */
13945 build_incomplete_class_ref (location, class_name)
13949 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13950 EXPR_WFL_LINECOL (node) = location;
13954 /* Complete an incomplete class reference operator. */
13956 patch_incomplete_class_ref (node)
13959 tree type = TREE_OPERAND (node, 0);
13962 if (!(ref_type = resolve_type_during_patch (type)))
13963 return error_mark_node;
13965 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
13967 tree dot = build_class_ref (ref_type);
13968 /* A class referenced by `foo.class' is initialized. */
13969 if (!flag_emit_class_files)
13970 dot = build_class_init (ref_type, dot);
13971 return java_complete_tree (dot);
13974 /* If we're emitting class files and we have to deal with non
13975 primitive types, we invoke (and consider generating) the
13976 synthetic static method `class$'. */
13977 if (!TYPE_DOT_CLASS (current_class))
13978 build_dot_class_method (current_class);
13979 ref_type = build_dot_class_method_invocation (ref_type);
13980 return java_complete_tree (ref_type);
13983 /* 15.14 Unary operators. We return error_mark_node in case of error,
13984 but preserve the type of NODE if the type is fixed. */
13987 patch_unaryop (node, wfl_op)
13991 tree op = TREE_OPERAND (node, 0);
13992 tree op_type = TREE_TYPE (op);
13993 tree prom_type = NULL_TREE, value, decl;
13994 int outer_field_flag = 0;
13995 int code = TREE_CODE (node);
13996 int error_found = 0;
13998 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14002 /* 15.13.2 Postfix Increment Operator ++ */
14003 case POSTINCREMENT_EXPR:
14004 /* 15.13.3 Postfix Increment Operator -- */
14005 case POSTDECREMENT_EXPR:
14006 /* 15.14.1 Prefix Increment Operator ++ */
14007 case PREINCREMENT_EXPR:
14008 /* 15.14.2 Prefix Decrement Operator -- */
14009 case PREDECREMENT_EXPR:
14010 op = decl = strip_out_static_field_access_decl (op);
14011 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
14012 /* We might be trying to change an outer field accessed using
14014 if (outer_field_flag)
14016 /* Retrieve the decl of the field we're trying to access. We
14017 do that by first retrieving the function we would call to
14018 access the field. It has been already verified that this
14019 field isn't final */
14020 if (flag_emit_class_files)
14021 decl = TREE_OPERAND (op, 0);
14023 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
14024 decl = DECL_FUNCTION_ACCESS_DECL (decl);
14026 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
14027 else if (!JDECL_P (decl)
14028 && TREE_CODE (decl) != COMPONENT_REF
14029 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
14030 && TREE_CODE (decl) != INDIRECT_REF
14031 && !(TREE_CODE (decl) == COMPOUND_EXPR
14032 && TREE_OPERAND (decl, 1)
14033 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
14035 TREE_TYPE (node) = error_mark_node;
14039 /* From now on, we know that op if a variable and that it has a
14040 valid wfl. We use wfl_op to locate errors related to the
14042 if (!JNUMERIC_TYPE_P (op_type))
14044 parse_error_context
14045 (wfl_op, "Invalid argument type `%s' to `%s'",
14046 lang_printable_name (op_type, 0), operator_string (node));
14047 TREE_TYPE (node) = error_mark_node;
14052 /* Before the addition, binary numeric promotion is performed on
14053 both operands, if really necessary */
14054 if (JINTEGRAL_TYPE_P (op_type))
14056 value = build_int_2 (1, 0);
14057 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
14061 value = build_int_2 (1, 0);
14063 binary_numeric_promotion (op_type,
14064 TREE_TYPE (value), &op, &value);
14067 /* We remember we might be accessing an outer field */
14068 if (outer_field_flag)
14070 /* We re-generate an access to the field */
14071 value = build (PLUS_EXPR, TREE_TYPE (op),
14072 build_outer_field_access (wfl_op, decl), value);
14074 /* And we patch the original access$() into a write
14075 with plus_op as a rhs */
14076 return outer_field_access_fix (node, op, value);
14079 /* And write back into the node. */
14080 TREE_OPERAND (node, 0) = op;
14081 TREE_OPERAND (node, 1) = value;
14082 /* Convert the overall back into its original type, if
14083 necessary, and return */
14084 if (JINTEGRAL_TYPE_P (op_type))
14085 return fold (node);
14087 return fold (convert (op_type, node));
14091 /* 15.14.3 Unary Plus Operator + */
14092 case UNARY_PLUS_EXPR:
14093 /* 15.14.4 Unary Minus Operator - */
14095 if (!JNUMERIC_TYPE_P (op_type))
14097 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
14098 TREE_TYPE (node) = error_mark_node;
14101 /* Unary numeric promotion is performed on operand */
14104 op = do_unary_numeric_promotion (op);
14105 prom_type = TREE_TYPE (op);
14106 if (code == UNARY_PLUS_EXPR)
14111 /* 15.14.5 Bitwise Complement Operator ~ */
14113 if (!JINTEGRAL_TYPE_P (op_type))
14115 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
14116 TREE_TYPE (node) = error_mark_node;
14121 op = do_unary_numeric_promotion (op);
14122 prom_type = TREE_TYPE (op);
14126 /* 15.14.6 Logical Complement Operator ! */
14127 case TRUTH_NOT_EXPR:
14128 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
14130 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
14131 /* But the type is known. We will report an error if further
14132 attempt of a assignment is made with this rhs */
14133 TREE_TYPE (node) = boolean_type_node;
14137 prom_type = boolean_type_node;
14140 /* 15.15 Cast Expression */
14142 value = patch_cast (node, wfl_operator);
14143 if (value == error_mark_node)
14145 /* If this cast is part of an assignment, we tell the code
14146 that deals with it not to complain about a mismatch,
14147 because things have been cast, anyways */
14148 TREE_TYPE (node) = error_mark_node;
14153 value = fold (value);
14154 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
14161 return error_mark_node;
14163 /* There are cases where node has been replaced by something else
14164 and we don't end up returning here: UNARY_PLUS_EXPR,
14165 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
14166 TREE_OPERAND (node, 0) = fold (op);
14167 TREE_TYPE (node) = prom_type;
14168 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
14169 return fold (node);
14172 /* Generic type resolution that sometimes takes place during node
14173 patching. Returned the resolved type or generate an error
14174 message. Return the resolved type or NULL_TREE. */
14177 resolve_type_during_patch (type)
14180 if (unresolved_type_p (type, NULL))
14182 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
14185 parse_error_context (type,
14186 "Class `%s' not found in type declaration",
14187 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
14190 return TREE_TYPE (type_decl);
14194 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14195 found. Otherwise NODE or something meant to replace it is returned. */
14198 patch_cast (node, wfl_op)
14202 tree op = TREE_OPERAND (node, 0);
14203 tree cast_type = TREE_TYPE (node);
14204 tree patched, op_type;
14207 /* Some string patching might be necessary at this stage */
14208 if ((patched = patch_string (op)))
14209 TREE_OPERAND (node, 0) = op = patched;
14210 op_type = TREE_TYPE (op);
14212 /* First resolve OP_TYPE if unresolved */
14213 if (!(cast_type = resolve_type_during_patch (cast_type)))
14214 return error_mark_node;
14216 /* Check on cast that are proven correct at compile time */
14217 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14220 if (cast_type == op_type)
14223 /* float and double type are converted to the original type main
14224 variant and then to the target type. */
14225 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
14226 op = convert (integer_type_node, op);
14228 /* Try widening/narowwing convertion. Potentially, things need
14229 to be worked out in gcc so we implement the extreme cases
14230 correctly. fold_convert() needs to be fixed. */
14231 return convert (cast_type, op);
14234 /* It's also valid to cast a boolean into a boolean */
14235 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14238 /* null can be casted to references */
14239 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14240 return build_null_of_type (cast_type);
14242 /* The remaining legal casts involve conversion between reference
14243 types. Check for their compile time correctness. */
14244 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14245 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14247 TREE_TYPE (node) = promote_type (cast_type);
14248 /* Now, the case can be determined correct at compile time if
14249 OP_TYPE can be converted into CAST_TYPE by assignment
14250 conversion (5.2) */
14252 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14254 TREE_SET_CODE (node, NOP_EXPR);
14258 if (flag_emit_class_files)
14260 TREE_SET_CODE (node, CONVERT_EXPR);
14264 /* The cast requires a run-time check */
14265 return build (CALL_EXPR, promote_type (cast_type),
14266 build_address_of (soft_checkcast_node),
14267 tree_cons (NULL_TREE, build_class_ref (cast_type),
14268 build_tree_list (NULL_TREE, op)),
14272 /* Any other casts are proven incorrect at compile time */
14273 t1 = xstrdup (lang_printable_name (op_type, 0));
14274 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
14275 t1, lang_printable_name (cast_type, 0));
14277 return error_mark_node;
14280 /* Build a null constant and give it the type TYPE. */
14283 build_null_of_type (type)
14286 tree node = build_int_2 (0, 0);
14287 TREE_TYPE (node) = promote_type (type);
14291 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14292 a list of indices. */
14294 build_array_ref (location, array, index)
14298 tree node = build (ARRAY_REF, NULL_TREE, array, index);
14299 EXPR_WFL_LINECOL (node) = location;
14303 /* 15.12 Array Access Expression */
14306 patch_array_ref (node)
14309 tree array = TREE_OPERAND (node, 0);
14310 tree array_type = TREE_TYPE (array);
14311 tree index = TREE_OPERAND (node, 1);
14312 tree index_type = TREE_TYPE (index);
14313 int error_found = 0;
14315 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14317 if (TREE_CODE (array_type) == POINTER_TYPE)
14318 array_type = TREE_TYPE (array_type);
14320 /* The array reference must be an array */
14321 if (!TYPE_ARRAY_P (array_type))
14323 parse_error_context
14325 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14326 lang_printable_name (array_type, 0));
14327 TREE_TYPE (node) = error_mark_node;
14331 /* The array index undergoes unary numeric promotion. The promoted
14332 type must be int */
14333 index = do_unary_numeric_promotion (index);
14334 if (TREE_TYPE (index) != int_type_node)
14336 if (valid_cast_to_p (index_type, int_type_node))
14337 parse_error_context (wfl_operator,
14338 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
14339 lang_printable_name (index_type, 0));
14341 parse_error_context (wfl_operator,
14342 "Incompatible type for `[]'. Can't convert `%s' to `int'",
14343 lang_printable_name (index_type, 0));
14344 TREE_TYPE (node) = error_mark_node;
14349 return error_mark_node;
14351 array_type = TYPE_ARRAY_ELEMENT (array_type);
14353 if (flag_emit_class_files || flag_emit_xref)
14355 TREE_OPERAND (node, 0) = array;
14356 TREE_OPERAND (node, 1) = index;
14359 node = build_java_arrayaccess (array, array_type, index);
14360 TREE_TYPE (node) = array_type;
14364 /* 15.9 Array Creation Expressions */
14367 build_newarray_node (type, dims, extra_dims)
14373 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
14374 build_int_2 (extra_dims, 0));
14379 patch_newarray (node)
14382 tree type = TREE_OPERAND (node, 0);
14383 tree dims = TREE_OPERAND (node, 1);
14384 tree cdim, array_type;
14385 int error_found = 0;
14387 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14389 /* Dimension types are verified. It's better for the types to be
14390 verified in order. */
14391 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14394 tree dim = TREE_VALUE (cdim);
14396 /* Dim might have been saved during its evaluation */
14397 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14399 /* The type of each specified dimension must be an integral type. */
14400 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14403 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14404 promoted type must be int. */
14407 dim = do_unary_numeric_promotion (dim);
14408 if (TREE_TYPE (dim) != int_type_node)
14412 /* Report errors on types here */
14415 parse_error_context
14416 (TREE_PURPOSE (cdim),
14417 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
14418 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14419 "Explicit cast needed to" : "Can't"),
14420 lang_printable_name (TREE_TYPE (dim), 0));
14424 TREE_PURPOSE (cdim) = NULL_TREE;
14427 /* Resolve array base type if unresolved */
14428 if (!(type = resolve_type_during_patch (type)))
14433 /* We don't want further evaluation of this bogus array creation
14435 TREE_TYPE (node) = error_mark_node;
14436 return error_mark_node;
14439 /* Set array_type to the actual (promoted) array type of the result. */
14440 if (TREE_CODE (type) == RECORD_TYPE)
14441 type = build_pointer_type (type);
14442 while (--xdims >= 0)
14444 type = promote_type (build_java_array_type (type, -1));
14446 dims = nreverse (dims);
14448 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14452 = build_java_array_type (type,
14453 TREE_CODE (cdim) == INTEGER_CST
14454 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14456 array_type = promote_type (array_type);
14458 dims = nreverse (dims);
14460 /* The node is transformed into a function call. Things are done
14461 differently according to the number of dimensions. If the number
14462 of dimension is equal to 1, then the nature of the base type
14463 (primitive or not) matters. */
14465 return build_new_array (type, TREE_VALUE (dims));
14467 /* Can't reuse what's already written in expr.c because it uses the
14468 JVM stack representation. Provide a build_multianewarray. FIXME */
14469 return build (CALL_EXPR, array_type,
14470 build_address_of (soft_multianewarray_node),
14471 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
14472 tree_cons (NULL_TREE,
14473 build_int_2 (ndims, 0), dims )),
14477 /* 10.6 Array initializer. */
14479 /* Build a wfl for array element that don't have one, so we can
14480 pin-point errors. */
14483 maybe_build_array_element_wfl (node)
14486 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14487 return build_expr_wfl (NULL_TREE, ctxp->filename,
14488 ctxp->elc.line, ctxp->elc.prev_col);
14493 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14494 identification of initialized arrays easier to detect during walk
14498 build_new_array_init (location, values)
14502 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14503 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14504 EXPR_WFL_LINECOL (to_return) = location;
14508 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14509 occurred. Otherwise return NODE after having set its type
14513 patch_new_array_init (type, node)
14516 int error_seen = 0;
14517 tree current, element_type;
14518 HOST_WIDE_INT length;
14519 int all_constant = 1;
14520 tree init = TREE_OPERAND (node, 0);
14522 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14524 parse_error_context (node,
14525 "Invalid array initializer for non-array type `%s'",
14526 lang_printable_name (type, 1));
14527 return error_mark_node;
14529 type = TREE_TYPE (type);
14530 element_type = TYPE_ARRAY_ELEMENT (type);
14532 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14534 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14535 current; length++, current = TREE_CHAIN (current))
14537 tree elt = TREE_VALUE (current);
14538 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14540 error_seen |= array_constructor_check_entry (element_type, current);
14541 elt = TREE_VALUE (current);
14542 /* When compiling to native code, STRING_CST is converted to
14543 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14544 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14549 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14550 TREE_PURPOSE (current) = NULL_TREE;
14553 if (elt && TREE_CODE (elt) == TREE_LIST
14554 && TREE_VALUE (elt) == error_mark_node)
14559 return error_mark_node;
14561 /* Create a new type. We can't reuse the one we have here by
14562 patching its dimension because it originally is of dimension -1
14563 hence reused by gcc. This would prevent triangular arrays. */
14564 type = build_java_array_type (element_type, length);
14565 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14566 TREE_TYPE (node) = promote_type (type);
14567 TREE_CONSTANT (init) = all_constant;
14568 TREE_CONSTANT (node) = all_constant;
14572 /* Verify that one entry of the initializer element list can be
14573 assigned to the array base type. Report 1 if an error occurred, 0
14577 array_constructor_check_entry (type, entry)
14580 char *array_type_string = NULL; /* For error reports */
14581 tree value, type_value, new_value, wfl_value, patched;
14582 int error_seen = 0;
14584 new_value = NULL_TREE;
14585 wfl_value = TREE_VALUE (entry);
14587 value = java_complete_tree (TREE_VALUE (entry));
14588 /* patch_string return error_mark_node if arg is error_mark_node */
14589 if ((patched = patch_string (value)))
14591 if (value == error_mark_node)
14594 type_value = TREE_TYPE (value);
14596 /* At anytime, try_builtin_assignconv can report a warning on
14597 constant overflow during narrowing. */
14598 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14599 new_value = try_builtin_assignconv (wfl_operator, type, value);
14600 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14601 type_value = promote_type (type);
14603 /* Check and report errors */
14606 const char *const msg = (!valid_cast_to_p (type_value, type) ?
14607 "Can't" : "Explicit cast needed to");
14608 if (!array_type_string)
14609 array_type_string = xstrdup (lang_printable_name (type, 1));
14610 parse_error_context
14611 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14612 msg, lang_printable_name (type_value, 1), array_type_string);
14617 TREE_VALUE (entry) = new_value;
14619 if (array_type_string)
14620 free (array_type_string);
14622 TREE_PURPOSE (entry) = NULL_TREE;
14627 build_this (location)
14630 tree node = build_wfl_node (this_identifier_node);
14631 TREE_SET_CODE (node, THIS_EXPR);
14632 EXPR_WFL_LINECOL (node) = location;
14636 /* 14.15 The return statement. It builds a modify expression that
14637 assigns the returned value to the RESULT_DECL that hold the value
14641 build_return (location, op)
14645 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14646 EXPR_WFL_LINECOL (node) = location;
14647 node = build_debugable_stmt (location, node);
14652 patch_return (node)
14655 tree return_exp = TREE_OPERAND (node, 0);
14656 tree meth = current_function_decl;
14657 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14658 int error_found = 0;
14660 TREE_TYPE (node) = error_mark_node;
14661 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14663 /* It's invalid to have a return value within a function that is
14664 declared with the keyword void or that is a constructor */
14665 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14668 /* It's invalid to use a return statement in a static block */
14669 if (DECL_CLINIT_P (current_function_decl))
14672 /* It's invalid to have a no return value within a function that
14673 isn't declared with the keyword `void' */
14674 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14677 if (DECL_INSTINIT_P (current_function_decl))
14682 if (DECL_INSTINIT_P (current_function_decl))
14683 parse_error_context (wfl_operator,
14684 "`return' inside instance initializer");
14686 else if (DECL_CLINIT_P (current_function_decl))
14687 parse_error_context (wfl_operator,
14688 "`return' inside static initializer");
14690 else if (!DECL_CONSTRUCTOR_P (meth))
14692 char *t = xstrdup (lang_printable_name (mtype, 0));
14693 parse_error_context (wfl_operator,
14694 "`return' with%s value from `%s %s'",
14695 (error_found == 1 ? "" : "out"),
14696 t, lang_printable_name (meth, 0));
14700 parse_error_context (wfl_operator,
14701 "`return' with value from constructor `%s'",
14702 lang_printable_name (meth, 0));
14703 return error_mark_node;
14706 /* If we have a return_exp, build a modify expression and expand
14707 it. Note: at that point, the assignment is declared valid, but we
14708 may want to carry some more hacks */
14711 tree exp = java_complete_tree (return_exp);
14712 tree modify, patched;
14714 if ((patched = patch_string (exp)))
14717 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14718 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14719 modify = java_complete_tree (modify);
14721 if (modify != error_mark_node)
14723 TREE_SIDE_EFFECTS (modify) = 1;
14724 TREE_OPERAND (node, 0) = modify;
14727 return error_mark_node;
14729 TREE_TYPE (node) = void_type_node;
14730 TREE_SIDE_EFFECTS (node) = 1;
14734 /* 14.8 The if Statement */
14737 build_if_else_statement (location, expression, if_body, else_body)
14739 tree expression, if_body, else_body;
14743 else_body = empty_stmt_node;
14744 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14745 EXPR_WFL_LINECOL (node) = location;
14746 node = build_debugable_stmt (location, node);
14751 patch_if_else_statement (node)
14754 tree expression = TREE_OPERAND (node, 0);
14755 int can_complete_normally
14756 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14757 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2)));
14759 TREE_TYPE (node) = error_mark_node;
14760 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14762 /* The type of expression must be boolean */
14763 if (TREE_TYPE (expression) != boolean_type_node
14764 && TREE_TYPE (expression) != promoted_boolean_type_node)
14766 parse_error_context
14768 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
14769 lang_printable_name (TREE_TYPE (expression), 0));
14770 return error_mark_node;
14773 if (TREE_CODE (expression) == INTEGER_CST)
14775 if (integer_zerop (expression))
14776 node = TREE_OPERAND (node, 2);
14778 node = TREE_OPERAND (node, 1);
14779 if (CAN_COMPLETE_NORMALLY (node) != can_complete_normally)
14781 node = build (COMPOUND_EXPR, void_type_node, node, empty_stmt_node);
14782 CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
14786 TREE_TYPE (node) = void_type_node;
14787 TREE_SIDE_EFFECTS (node) = 1;
14788 CAN_COMPLETE_NORMALLY (node) = can_complete_normally;
14792 /* 14.6 Labeled Statements */
14794 /* Action taken when a lableled statement is parsed. a new
14795 LABELED_BLOCK_EXPR is created. No statement is attached to the
14796 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
14799 build_labeled_block (location, label)
14804 tree label_decl, node;
14805 if (label == NULL_TREE || label == continue_identifier_node)
14806 label_name = label;
14809 label_name = merge_qualified_name (label_id, label);
14810 /* Issue an error if we try to reuse a label that was previously
14812 if (IDENTIFIER_LOCAL_VALUE (label_name))
14814 EXPR_WFL_LINECOL (wfl_operator) = location;
14815 parse_error_context (wfl_operator,
14816 "Declaration of `%s' shadows a previous label declaration",
14817 IDENTIFIER_POINTER (label));
14818 EXPR_WFL_LINECOL (wfl_operator) =
14819 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14820 parse_error_context (wfl_operator,
14821 "This is the location of the previous declaration of label `%s'",
14822 IDENTIFIER_POINTER (label));
14823 java_error_count--;
14827 label_decl = create_label_decl (label_name);
14828 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14829 EXPR_WFL_LINECOL (node) = location;
14830 TREE_SIDE_EFFECTS (node) = 1;
14834 /* A labeled statement LBE is attached a statement. */
14837 finish_labeled_statement (lbe, statement)
14838 tree lbe; /* Labeled block expr */
14841 /* In anyways, tie the loop to its statement */
14842 LABELED_BLOCK_BODY (lbe) = statement;
14843 pop_labeled_block ();
14844 POP_LABELED_BLOCK ();
14848 /* 14.10, 14.11, 14.12 Loop Statements */
14850 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14854 build_new_loop (loop_body)
14857 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14858 TREE_SIDE_EFFECTS (loop) = 1;
14863 /* Create a loop body according to the following structure:
14865 COMPOUND_EXPR (loop main body)
14866 EXIT_EXPR (this order is for while/for loops.
14867 LABELED_BLOCK_EXPR the order is reversed for do loops)
14868 LABEL_DECL (a continue occurring here branches at the
14869 BODY end of this labeled block)
14872 REVERSED, if non zero, tells that the loop condition expr comes
14873 after the body, like in the do-while loop.
14875 To obtain a loop, the loop body structure described above is
14876 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14879 LABEL_DECL (use this label to exit the loop)
14881 <structure described above> */
14884 build_loop_body (location, condition, reversed)
14889 tree first, second, body;
14891 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14892 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14893 condition = build_debugable_stmt (location, condition);
14894 TREE_SIDE_EFFECTS (condition) = 1;
14896 body = build_labeled_block (0, continue_identifier_node);
14897 first = (reversed ? body : condition);
14898 second = (reversed ? condition : body);
14900 build (COMPOUND_EXPR, NULL_TREE,
14901 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14904 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14905 their order) on the current loop. Unlink the current loop from the
14909 finish_loop_body (location, condition, body, reversed)
14911 tree condition, body;
14914 tree to_return = ctxp->current_loop;
14915 tree loop_body = LOOP_EXPR_BODY (to_return);
14918 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14919 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14920 The real EXIT_EXPR is one operand further. */
14921 EXPR_WFL_LINECOL (cnode) = location;
14922 /* This one is for accurate error reports */
14923 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14924 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14926 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14931 /* Tailored version of finish_loop_body for FOR loops, when FOR
14932 loops feature the condition part */
14935 finish_for_loop (location, condition, update, body)
14937 tree condition, update, body;
14939 /* Put the condition and the loop body in place */
14940 tree loop = finish_loop_body (location, condition, body, 0);
14941 /* LOOP is the current loop which has been now popped of the loop
14942 stack. Install the update block */
14943 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14947 /* Try to find the loop a block might be related to. This comprises
14948 the case where the LOOP_EXPR is found as the second operand of a
14949 COMPOUND_EXPR, because the loop happens to have an initialization
14950 part, then expressed as the first operand of the COMPOUND_EXPR. If
14951 the search finds something, 1 is returned. Otherwise, 0 is
14952 returned. The search is assumed to start from a
14953 LABELED_BLOCK_EXPR's block. */
14956 search_loop (statement)
14959 if (TREE_CODE (statement) == LOOP_EXPR)
14962 if (TREE_CODE (statement) == BLOCK)
14963 statement = BLOCK_SUBBLOCKS (statement);
14967 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14968 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14969 statement = TREE_OPERAND (statement, 1);
14971 return (TREE_CODE (statement) == LOOP_EXPR
14972 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
14975 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14976 returned otherwise. */
14979 labeled_block_contains_loop_p (block, loop)
14985 if (LABELED_BLOCK_BODY (block) == loop)
14988 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
14994 /* If the loop isn't surrounded by a labeled statement, create one and
14995 insert LOOP as its body. */
14998 patch_loop_statement (loop)
15003 TREE_TYPE (loop) = void_type_node;
15004 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
15007 loop_label = build_labeled_block (0, NULL_TREE);
15008 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
15009 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
15010 LABELED_BLOCK_BODY (loop_label) = loop;
15011 PUSH_LABELED_BLOCK (loop_label);
15015 /* 14.13, 14.14: break and continue Statements */
15017 /* Build a break or a continue statement. a null NAME indicates an
15018 unlabeled break/continue statement. */
15021 build_bc_statement (location, is_break, name)
15022 int location, is_break;
15025 tree break_continue, label_block_expr = NULL_TREE;
15029 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
15030 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
15031 /* Null means that we don't have a target for this named
15032 break/continue. In this case, we make the target to be the
15033 label name, so that the error can be reported accuratly in
15034 patch_bc_statement. */
15035 label_block_expr = EXPR_WFL_NODE (name);
15037 /* Unlabeled break/continue will be handled during the
15038 break/continue patch operation */
15040 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
15042 IS_BREAK_STMT_P (break_continue) = is_break;
15043 TREE_SIDE_EFFECTS (break_continue) = 1;
15044 EXPR_WFL_LINECOL (break_continue) = location;
15045 break_continue = build_debugable_stmt (location, break_continue);
15046 return break_continue;
15049 /* Verification of a break/continue statement. */
15052 patch_bc_statement (node)
15055 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
15056 tree labeled_block = ctxp->current_labeled_block;
15057 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15059 /* Having an identifier here means that the target is unknown. */
15060 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
15062 parse_error_context (wfl_operator, "No label definition found for `%s'",
15063 IDENTIFIER_POINTER (bc_label));
15064 return error_mark_node;
15066 if (! IS_BREAK_STMT_P (node))
15068 /* It's a continue statement. */
15069 for (;; labeled_block = TREE_CHAIN (labeled_block))
15071 if (labeled_block == NULL_TREE)
15073 if (bc_label == NULL_TREE)
15074 parse_error_context (wfl_operator,
15075 "`continue' must be in loop");
15077 parse_error_context
15078 (wfl_operator, "continue label `%s' does not name a loop",
15079 IDENTIFIER_POINTER (bc_label));
15080 return error_mark_node;
15082 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
15083 == continue_identifier_node)
15084 && (bc_label == NULL_TREE
15085 || TREE_CHAIN (labeled_block) == bc_label))
15087 bc_label = labeled_block;
15092 else if (!bc_label)
15094 for (;; labeled_block = TREE_CHAIN (labeled_block))
15096 if (labeled_block == NULL_TREE)
15098 parse_error_context (wfl_operator,
15099 "`break' must be in loop or switch");
15100 return error_mark_node;
15102 target_stmt = LABELED_BLOCK_BODY (labeled_block);
15103 if (TREE_CODE (target_stmt) == SWITCH_EXPR
15104 || search_loop (target_stmt))
15106 bc_label = labeled_block;
15112 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
15113 CAN_COMPLETE_NORMALLY (bc_label) = 1;
15115 /* Our break/continue don't return values. */
15116 TREE_TYPE (node) = void_type_node;
15117 /* Encapsulate the break within a compound statement so that it's
15118 expanded all the times by expand_expr (and not clobbered
15119 sometimes, like after a if statement) */
15120 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
15121 TREE_SIDE_EFFECTS (node) = 1;
15125 /* Process the exit expression belonging to a loop. Its type must be
15129 patch_exit_expr (node)
15132 tree expression = TREE_OPERAND (node, 0);
15133 TREE_TYPE (node) = error_mark_node;
15134 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15136 /* The type of expression must be boolean */
15137 if (TREE_TYPE (expression) != boolean_type_node)
15139 parse_error_context
15141 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
15142 lang_printable_name (TREE_TYPE (expression), 0));
15143 return error_mark_node;
15145 /* Now we know things are allright, invert the condition, fold and
15147 TREE_OPERAND (node, 0) =
15148 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
15150 if (! integer_zerop (TREE_OPERAND (node, 0))
15151 && ctxp->current_loop != NULL_TREE
15152 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
15153 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
15154 if (! integer_onep (TREE_OPERAND (node, 0)))
15155 CAN_COMPLETE_NORMALLY (node) = 1;
15158 TREE_TYPE (node) = void_type_node;
15162 /* 14.9 Switch statement */
15165 patch_switch_statement (node)
15168 tree se = TREE_OPERAND (node, 0), se_type;
15171 /* Complete the switch expression */
15172 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
15173 se_type = TREE_TYPE (se);
15174 /* The type of the switch expression must be char, byte, short or
15176 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
15178 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
15179 parse_error_context (wfl_operator,
15180 "Incompatible type for `switch'. Can't convert `%s' to `int'",
15181 lang_printable_name (se_type, 0));
15182 /* This is what java_complete_tree will check */
15183 TREE_OPERAND (node, 0) = error_mark_node;
15184 return error_mark_node;
15187 /* Save and restore the outer case label list. */
15188 save = case_label_list;
15189 case_label_list = NULL_TREE;
15191 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15193 /* See if we've found a duplicate label. We can't leave this until
15194 code generation, because in `--syntax-only' and `-C' modes we
15195 don't do ordinary code generation. */
15196 for (iter = case_label_list; iter != NULL_TREE; iter = TREE_CHAIN (iter))
15198 HOST_WIDE_INT val = TREE_INT_CST_LOW (TREE_VALUE (iter));
15200 for (subiter = TREE_CHAIN (iter);
15201 subiter != NULL_TREE;
15202 subiter = TREE_CHAIN (subiter))
15204 HOST_WIDE_INT subval = TREE_INT_CST_LOW (TREE_VALUE (subiter));
15207 EXPR_WFL_LINECOL (wfl_operator)
15208 = EXPR_WFL_LINECOL (TREE_PURPOSE (iter));
15209 /* The case_label_list is in reverse order, so print the
15210 outer label first. */
15211 parse_error_context (wfl_operator, "duplicate case label: `"
15212 HOST_WIDE_INT_PRINT_DEC "'", subval);
15213 EXPR_WFL_LINECOL (wfl_operator)
15214 = EXPR_WFL_LINECOL (TREE_PURPOSE (subiter));
15215 parse_error_context (wfl_operator, "original label is here");
15222 case_label_list = save;
15224 /* Ready to return */
15225 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15227 TREE_TYPE (node) = error_mark_node;
15228 return error_mark_node;
15230 TREE_TYPE (node) = void_type_node;
15231 TREE_SIDE_EFFECTS (node) = 1;
15232 CAN_COMPLETE_NORMALLY (node)
15233 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15234 || ! SWITCH_HAS_DEFAULT (node);
15238 /* 14.18 The try/catch statements */
15240 /* Encapsulate TRY_STMTS' in a try catch sequence. The catch clause
15241 catches TYPE and executes CATCH_STMTS. */
15244 encapsulate_with_try_catch (location, type, try_stmts, catch_stmts)
15246 tree type, try_stmts, catch_stmts;
15248 tree try_block, catch_clause_param, catch_block, catch;
15250 /* First build a try block */
15251 try_block = build_expr_block (try_stmts, NULL_TREE);
15253 /* Build a catch block: we need a catch clause parameter */
15254 catch_clause_param = build_decl (VAR_DECL,
15255 wpv_id, build_pointer_type (type));
15257 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
15259 /* Initialize the variable and store in the block */
15260 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
15261 build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
15262 add_stmt_to_block (catch_block, NULL_TREE, catch);
15264 /* Add the catch statements */
15265 add_stmt_to_block (catch_block, NULL_TREE, catch_stmts);
15267 /* Now we can build a CATCH_EXPR */
15268 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
15270 return build_try_statement (location, try_block, catch_block);
15274 build_try_statement (location, try_block, catches)
15276 tree try_block, catches;
15278 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
15279 EXPR_WFL_LINECOL (node) = location;
15284 build_try_finally_statement (location, try_block, finally)
15286 tree try_block, finally;
15288 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15289 EXPR_WFL_LINECOL (node) = location;
15294 patch_try_statement (node)
15297 int error_found = 0;
15298 tree try = TREE_OPERAND (node, 0);
15299 /* Exception handlers are considered in left to right order */
15300 tree catch = nreverse (TREE_OPERAND (node, 1));
15301 tree current, caught_type_list = NULL_TREE;
15303 /* Check catch clauses, if any. Every time we find an error, we try
15304 to process the next catch clause. We process the catch clause before
15305 the try block so that when processing the try block we can check thrown
15306 exceptions againts the caught type list. */
15307 for (current = catch; current; current = TREE_CHAIN (current))
15309 tree carg_decl, carg_type;
15310 tree sub_current, catch_block, catch_clause;
15313 /* At this point, the structure of the catch clause is
15314 CATCH_EXPR (catch node)
15315 BLOCK (with the decl of the parameter)
15317 MODIFY_EXPR (assignment of the catch parameter)
15318 BLOCK (catch clause block)
15320 catch_clause = TREE_OPERAND (current, 0);
15321 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15322 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15324 /* Catch clauses can't have more than one parameter declared,
15325 but it's already enforced by the grammar. Make sure that the
15326 only parameter of the clause statement in of class Throwable
15327 or a subclass of Throwable, but that was done earlier. The
15328 catch clause parameter type has also been resolved. */
15330 /* Just make sure that the catch clause parameter type inherits
15331 from java.lang.Throwable */
15332 if (!inherits_from_p (carg_type, throwable_type_node))
15334 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15335 parse_error_context (wfl_operator,
15336 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
15337 lang_printable_name (carg_type, 0));
15342 /* Partial check for unreachable catch statement: The catch
15343 clause is reachable iff is no earlier catch block A in
15344 the try statement such that the type of the catch
15345 clause's parameter is the same as or a subclass of the
15346 type of A's parameter */
15348 for (sub_current = catch;
15349 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15351 tree sub_catch_clause, decl;
15352 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15353 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15355 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15357 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15358 parse_error_context
15360 "`catch' not reached because of the catch clause at line %d",
15361 EXPR_WFL_LINENO (sub_current));
15362 unreachable = error_found = 1;
15366 /* Complete the catch clause block */
15367 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15368 if (catch_block == error_mark_node)
15373 if (CAN_COMPLETE_NORMALLY (catch_block))
15374 CAN_COMPLETE_NORMALLY (node) = 1;
15375 TREE_OPERAND (current, 0) = catch_block;
15380 /* Things to do here: the exception must be thrown */
15382 /* Link this type to the caught type list */
15383 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15386 PUSH_EXCEPTIONS (caught_type_list);
15387 if ((try = java_complete_tree (try)) == error_mark_node)
15389 if (CAN_COMPLETE_NORMALLY (try))
15390 CAN_COMPLETE_NORMALLY (node) = 1;
15393 /* Verification ends here */
15395 return error_mark_node;
15397 TREE_OPERAND (node, 0) = try;
15398 TREE_OPERAND (node, 1) = catch;
15399 TREE_TYPE (node) = void_type_node;
15403 /* 14.17 The synchronized Statement */
15406 patch_synchronized_statement (node, wfl_op1)
15407 tree node, wfl_op1;
15409 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15410 tree block = TREE_OPERAND (node, 1);
15412 tree tmp, enter, exit, expr_decl, assignment;
15414 if (expr == error_mark_node)
15416 block = java_complete_tree (block);
15420 /* We might be trying to synchronize on a STRING_CST */
15421 if ((tmp = patch_string (expr)))
15424 /* The TYPE of expr must be a reference type */
15425 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15427 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15428 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
15429 lang_printable_name (TREE_TYPE (expr), 0));
15430 return error_mark_node;
15433 if (flag_emit_xref)
15435 TREE_OPERAND (node, 0) = expr;
15436 TREE_OPERAND (node, 1) = java_complete_tree (block);
15437 CAN_COMPLETE_NORMALLY (node) = 1;
15441 /* Generate a try-finally for the synchronized statement, except
15442 that the handler that catches all throw exception calls
15443 _Jv_MonitorExit and then rethrow the exception.
15444 The synchronized statement is then implemented as:
15447 _Jv_MonitorEnter (expression)
15449 _Jv_MonitorExit (expression)
15453 e = _Jv_exception_info ();
15454 _Jv_MonitorExit (expression)
15458 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15459 BUILD_MONITOR_ENTER (enter, expr_decl);
15460 BUILD_MONITOR_EXIT (exit, expr_decl);
15461 CAN_COMPLETE_NORMALLY (enter) = 1;
15462 CAN_COMPLETE_NORMALLY (exit) = 1;
15463 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15464 TREE_SIDE_EFFECTS (assignment) = 1;
15465 node = build (COMPOUND_EXPR, NULL_TREE,
15466 build (COMPOUND_EXPR, NULL_TREE, assignment, enter),
15467 build (TRY_FINALLY_EXPR, NULL_TREE, block, exit));
15468 node = build_expr_block (node, expr_decl);
15470 return java_complete_tree (node);
15473 /* 14.16 The throw Statement */
15476 patch_throw_statement (node, wfl_op1)
15477 tree node, wfl_op1;
15479 tree expr = TREE_OPERAND (node, 0);
15480 tree type = TREE_TYPE (expr);
15481 int unchecked_ok = 0, tryblock_throws_ok = 0;
15483 /* Thrown expression must be assignable to java.lang.Throwable */
15484 if (!try_reference_assignconv (throwable_type_node, expr))
15486 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15487 parse_error_context (wfl_operator,
15488 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
15489 lang_printable_name (type, 0));
15490 /* If the thrown expression was a reference, we further the
15491 compile-time check. */
15492 if (!JREFERENCE_TYPE_P (type))
15493 return error_mark_node;
15496 /* At least one of the following must be true */
15498 /* The type of the throw expression is a not checked exception,
15499 i.e. is a unchecked expression. */
15500 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15502 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15503 /* An instance can't throw a checked exception unless that exception
15504 is explicitly declared in the `throws' clause of each
15505 constructor. This doesn't apply to anonymous classes, since they
15506 don't have declared constructors. */
15508 && DECL_INSTINIT_P (current_function_decl)
15509 && !ANONYMOUS_CLASS_P (current_class))
15512 for (current = TYPE_METHODS (current_class); current;
15513 current = TREE_CHAIN (current))
15514 if (DECL_CONSTRUCTOR_P (current)
15515 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15517 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)",
15518 lang_printable_name (TREE_TYPE (expr), 0));
15519 return error_mark_node;
15523 /* Throw is contained in a try statement and at least one catch
15524 clause can receive the thrown expression or the current method is
15525 declared to throw such an exception. Or, the throw statement is
15526 contained in a method or constructor declaration and the type of
15527 the Expression is assignable to at least one type listed in the
15528 throws clause the declaration. */
15530 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15531 if (!(unchecked_ok || tryblock_throws_ok))
15533 /* If there is a surrounding try block that has no matching
15534 clatch clause, report it first. A surrounding try block exits
15535 only if there is something after the list of checked
15536 exception thrown by the current function (if any). */
15537 if (IN_TRY_BLOCK_P ())
15538 parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
15539 lang_printable_name (type, 0));
15540 /* If we have no surrounding try statement and the method doesn't have
15541 any throws, report it now. FIXME */
15543 /* We report that the exception can't be throw from a try block
15544 in all circumstances but when the `throw' is inside a static
15546 else if (!EXCEPTIONS_P (currently_caught_type_list)
15547 && !tryblock_throws_ok)
15549 if (DECL_CLINIT_P (current_function_decl))
15550 parse_error_context (wfl_operator,
15551 "Checked exception `%s' can't be thrown in initializer",
15552 lang_printable_name (type, 0));
15554 parse_error_context (wfl_operator,
15555 "Checked exception `%s' isn't thrown from a `try' block",
15556 lang_printable_name (type, 0));
15558 /* Otherwise, the current method doesn't have the appropriate
15559 throws declaration */
15561 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
15562 lang_printable_name (type, 0));
15563 return error_mark_node;
15566 if (! flag_emit_class_files && ! flag_emit_xref)
15567 BUILD_THROW (node, expr);
15569 /* If doing xrefs, keep the location where the `throw' was seen. */
15570 if (flag_emit_xref)
15571 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15575 /* Check that exception said to be thrown by method DECL can be
15576 effectively caught from where DECL is invoked. */
15579 check_thrown_exceptions (location, decl)
15584 /* For all the unchecked exceptions thrown by DECL */
15585 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15586 throws = TREE_CHAIN (throws))
15587 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15590 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15591 if (DECL_NAME (decl) == get_identifier ("clone"))
15594 EXPR_WFL_LINECOL (wfl_operator) = location;
15595 if (DECL_FINIT_P (current_function_decl))
15596 parse_error_context
15597 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15598 lang_printable_name (TREE_VALUE (throws), 0));
15601 parse_error_context
15602 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
15603 lang_printable_name (TREE_VALUE (throws), 0),
15604 (DECL_INIT_P (current_function_decl) ?
15605 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15606 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15611 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15612 try-catch blocks, OR is listed in the `throws' clause of the
15616 check_thrown_exceptions_do (exception)
15619 tree list = currently_caught_type_list;
15620 resolve_and_layout (exception, NULL_TREE);
15621 /* First, all the nested try-catch-finally at that stage. The
15622 last element contains `throws' clause exceptions, if any. */
15623 if (IS_UNCHECKED_EXCEPTION_P (exception))
15628 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15629 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15631 list = TREE_CHAIN (list);
15637 purge_unchecked_exceptions (mdecl)
15640 tree throws = DECL_FUNCTION_THROWS (mdecl);
15641 tree new = NULL_TREE;
15645 tree next = TREE_CHAIN (throws);
15646 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15648 TREE_CHAIN (throws) = new;
15653 /* List is inverted here, but it doesn't matter */
15654 DECL_FUNCTION_THROWS (mdecl) = new;
15657 /* This function goes over all of CLASS_TYPE ctors and checks whether
15658 each of them features at least one unchecked exception in its
15659 `throws' clause. If it's the case, it returns `true', `false'
15663 ctors_unchecked_throws_clause_p (class_type)
15668 for (current = TYPE_METHODS (class_type); current;
15669 current = TREE_CHAIN (current))
15671 bool ctu = false; /* Ctor Throws Unchecked */
15672 if (DECL_CONSTRUCTOR_P (current))
15675 for (throws = DECL_FUNCTION_THROWS (current); throws && !ctu;
15676 throws = TREE_CHAIN (throws))
15677 if (inherits_from_p (TREE_VALUE (throws), exception_type_node))
15680 /* We return false as we found one ctor that is unfit. */
15681 if (!ctu && DECL_CONSTRUCTOR_P (current))
15684 /* All ctors feature at least one unchecked exception in their
15685 `throws' clause. */
15689 /* 15.24 Conditional Operator ?: */
15692 patch_conditional_expr (node, wfl_cond, wfl_op1)
15693 tree node, wfl_cond, wfl_op1;
15695 tree cond = TREE_OPERAND (node, 0);
15696 tree op1 = TREE_OPERAND (node, 1);
15697 tree op2 = TREE_OPERAND (node, 2);
15698 tree resulting_type = NULL_TREE;
15699 tree t1, t2, patched;
15700 int error_found = 0;
15702 /* Operands of ?: might be StringBuffers crafted as a result of a
15703 string concatenation. Obtain a descent operand here. */
15704 if ((patched = patch_string (op1)))
15705 TREE_OPERAND (node, 1) = op1 = patched;
15706 if ((patched = patch_string (op2)))
15707 TREE_OPERAND (node, 2) = op2 = patched;
15709 t1 = TREE_TYPE (op1);
15710 t2 = TREE_TYPE (op2);
15712 /* The first expression must be a boolean */
15713 if (TREE_TYPE (cond) != boolean_type_node)
15715 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15716 parse_error_context (wfl_operator,
15717 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
15718 lang_printable_name (TREE_TYPE (cond), 0));
15722 /* Second and third can be numeric, boolean (i.e. primitive),
15723 references or null. Anything else results in an error */
15724 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15725 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15726 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15727 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15730 /* Determine the type of the conditional expression. Same types are
15731 easy to deal with */
15733 resulting_type = t1;
15735 /* There are different rules for numeric types */
15736 else if (JNUMERIC_TYPE_P (t1))
15738 /* if byte/short found, the resulting type is short */
15739 if ((t1 == byte_type_node && t2 == short_type_node)
15740 || (t1 == short_type_node && t2 == byte_type_node))
15741 resulting_type = short_type_node;
15743 /* If t1 is a constant int and t2 is of type byte, short or char
15744 and t1's value fits in t2, then the resulting type is t2 */
15745 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15746 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15747 resulting_type = t2;
15749 /* If t2 is a constant int and t1 is of type byte, short or char
15750 and t2's value fits in t1, then the resulting type is t1 */
15751 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15752 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15753 resulting_type = t1;
15755 /* Otherwise, binary numeric promotion is applied and the
15756 resulting type is the promoted type of operand 1 and 2 */
15758 resulting_type = binary_numeric_promotion (t1, t2,
15759 &TREE_OPERAND (node, 1),
15760 &TREE_OPERAND (node, 2));
15763 /* Cases of a reference and a null type */
15764 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15765 resulting_type = t1;
15767 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15768 resulting_type = t2;
15770 /* Last case: different reference types. If a type can be converted
15771 into the other one by assignment conversion, the latter
15772 determines the type of the expression */
15773 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15774 resulting_type = promote_type (t1);
15776 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15777 resulting_type = promote_type (t2);
15779 /* If we don't have any resulting type, we're in trouble */
15780 if (!resulting_type)
15782 char *t = xstrdup (lang_printable_name (t1, 0));
15783 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15784 parse_error_context (wfl_operator,
15785 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15786 t, lang_printable_name (t2, 0));
15793 TREE_TYPE (node) = error_mark_node;
15794 return error_mark_node;
15797 TREE_TYPE (node) = resulting_type;
15798 TREE_SET_CODE (node, COND_EXPR);
15799 CAN_COMPLETE_NORMALLY (node) = 1;
15803 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
15806 maybe_build_class_init_for_field (decl, expr)
15809 tree clas = DECL_CONTEXT (decl);
15810 if (flag_emit_class_files || flag_emit_xref)
15813 if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
15814 && FIELD_FINAL (decl))
15816 tree init = DECL_INITIAL (decl);
15817 if (init != NULL_TREE)
15818 init = fold_constant_for_init (init, decl);
15819 if (init != NULL_TREE && CONSTANT_VALUE_P (init))
15823 return build_class_init (clas, expr);
15826 /* Try to constant fold NODE.
15827 If NODE is not a constant expression, return NULL_EXPR.
15828 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15831 fold_constant_for_init (node, context)
15835 tree op0, op1, val;
15836 enum tree_code code = TREE_CODE (node);
15848 case TRUNC_MOD_EXPR:
15856 case TRUTH_ANDIF_EXPR:
15857 case TRUTH_ORIF_EXPR:
15864 op0 = TREE_OPERAND (node, 0);
15865 op1 = TREE_OPERAND (node, 1);
15866 val = fold_constant_for_init (op0, context);
15867 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15869 TREE_OPERAND (node, 0) = val;
15870 val = fold_constant_for_init (op1, context);
15871 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15873 TREE_OPERAND (node, 1) = val;
15874 return patch_binop (node, op0, op1);
15876 case UNARY_PLUS_EXPR:
15878 case TRUTH_NOT_EXPR:
15881 op0 = TREE_OPERAND (node, 0);
15882 val = fold_constant_for_init (op0, context);
15883 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15885 TREE_OPERAND (node, 0) = val;
15886 return patch_unaryop (node, op0);
15890 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15891 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15893 TREE_OPERAND (node, 0) = val;
15894 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15895 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15897 TREE_OPERAND (node, 1) = val;
15898 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15899 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15901 TREE_OPERAND (node, 2) = val;
15902 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15903 : TREE_OPERAND (node, 2);
15907 if (! FIELD_FINAL (node)
15908 || DECL_INITIAL (node) == NULL_TREE)
15910 val = DECL_INITIAL (node);
15911 /* Guard against infinite recursion. */
15912 DECL_INITIAL (node) = NULL_TREE;
15913 val = fold_constant_for_init (val, node);
15914 DECL_INITIAL (node) = val;
15917 case EXPR_WITH_FILE_LOCATION:
15918 /* Compare java_complete_tree and resolve_expression_name. */
15919 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15920 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15922 tree name = EXPR_WFL_NODE (node);
15924 if (PRIMARY_P (node))
15926 else if (! QUALIFIED_P (name))
15928 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
15929 if (decl == NULL_TREE
15930 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
15932 return fold_constant_for_init (decl, decl);
15936 /* Install the proper context for the field resolution.
15937 The prior context is restored once the name is
15938 properly qualified. */
15939 tree saved_current_class = current_class;
15940 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15941 current_class = DECL_CONTEXT (context);
15942 qualify_ambiguous_name (node);
15943 current_class = saved_current_class;
15944 if (resolve_field_access (node, &decl, NULL)
15945 && decl != NULL_TREE)
15946 return fold_constant_for_init (decl, decl);
15952 op0 = TREE_OPERAND (node, 0);
15953 val = fold_constant_for_init (op0, context);
15954 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15956 TREE_OPERAND (node, 0) = val;
15960 #ifdef USE_COMPONENT_REF
15962 case COMPONENT_REF:
15971 #ifdef USE_COMPONENT_REF
15972 /* Context is 'T' for TypeName, 'P' for PackageName,
15973 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15976 resolve_simple_name (name, context)
15983 resolve_qualified_name (name, context)
15990 /* Mark P, which is really a `struct parser_ctxt **' for GC. */
15993 mark_parser_ctxt (p)
15996 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
16003 for (i = 0; i < 11; ++i)
16004 ggc_mark_tree (pc->modifier_ctx[i]);
16005 ggc_mark_tree (pc->class_type);
16006 ggc_mark_tree (pc->function_decl);
16007 ggc_mark_tree (pc->package);
16008 ggc_mark_tree (pc->class_list);
16009 ggc_mark_tree (pc->current_parsed_class);
16010 ggc_mark_tree (pc->current_parsed_class_un);
16011 ggc_mark_tree (pc->non_static_initialized);
16012 ggc_mark_tree (pc->static_initialized);
16013 ggc_mark_tree (pc->instance_initializers);
16014 ggc_mark_tree (pc->import_list);
16015 ggc_mark_tree (pc->import_demand_list);
16016 ggc_mark_tree (pc->current_loop);
16017 ggc_mark_tree (pc->current_labeled_block);
16018 #endif /* JC1_LITE */
16021 mark_parser_ctxt (&pc->next);
16027 /* Register roots with the garbage collector. */
16028 ggc_add_tree_root (src_parse_roots, sizeof (src_parse_roots) / sizeof(tree));
16033 /* This section deals with the functions that are called when tables
16034 recording class initialization information are traversed. */
16036 /* Attach to PTR (a block) the declaration found in ENTRY. */
16039 attach_init_test_initialization_flags (entry, ptr)
16040 struct hash_entry *entry;
16043 tree block = (tree)ptr;
16044 struct init_test_hash_entry *ite = (struct init_test_hash_entry *) entry;
16046 TREE_CHAIN (ite->init_test_decl) = BLOCK_EXPR_DECLS (block);
16047 BLOCK_EXPR_DECLS (block) = ite->init_test_decl;
16051 /* This function is called for each classes that is known definitely
16052 assigned when a given static method was called. This function
16053 augments a compound expression (INFO) storing all assignment to
16054 initialized static class flags if a flag already existed, otherwise
16055 a new one is created. */
16058 emit_test_initialization (entry, info)
16059 struct hash_entry *entry;
16062 tree l = (tree) info;
16065 struct init_test_hash_entry *ite = (struct init_test_hash_entry *)
16066 hash_lookup (&DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl),
16068 current_function_decl != TREE_PURPOSE (l), NULL);
16070 /* If we haven't found a flag and we're dealing with self registered
16071 with current_function_decl, then don't do anything. Self is
16072 always added as definitely initialized but this information is
16073 valid only if used outside the current function. */
16077 /* If we don't have a variable, create one and install it. */
16078 if (! ite->init_test_decl)
16082 decl = build_decl (VAR_DECL, NULL_TREE, boolean_type_node);
16083 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
16084 LOCAL_CLASS_INITIALIZATION_FLAG (decl) = 1;
16085 DECL_CONTEXT (decl) = current_function_decl;
16086 DECL_INITIAL (decl) = boolean_true_node;
16088 /* The trick is to find the right context for it. */
16089 block = BLOCK_SUBBLOCKS (GET_CURRENT_BLOCK (current_function_decl));
16090 TREE_CHAIN (decl) = BLOCK_EXPR_DECLS (block);
16091 BLOCK_EXPR_DECLS (block) = decl;
16092 ite->init_test_decl = decl;
16095 decl = ite->init_test_decl;
16097 /* Now simply augment the compound that holds all the assignments
16098 pertaining to this method invocation. */
16099 init = build (MODIFY_EXPR, boolean_type_node, decl, boolean_true_node);
16100 TREE_SIDE_EFFECTS (init) = 1;
16101 TREE_VALUE (l) = add_stmt_to_compound (TREE_VALUE (l), void_type_node, init);
16102 TREE_SIDE_EFFECTS (TREE_VALUE (l)) = 1;