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 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));
103 static void find_in_imports PARAMS ((tree));
104 static void check_static_final_variable_assignment_flag PARAMS ((tree));
105 static void reset_static_final_variable_assignment_flag PARAMS ((tree));
106 static void check_final_variable_local_assignment_flag PARAMS ((tree, tree));
107 static void reset_final_variable_local_assignment_flag PARAMS ((tree));
108 static int check_final_variable_indirect_assignment PARAMS ((tree));
109 static void check_final_variable_global_assignment_flag PARAMS ((tree));
110 static void check_inner_class_access PARAMS ((tree, tree, tree));
111 static int check_pkg_class_access PARAMS ((tree, tree));
112 static void register_package PARAMS ((tree));
113 static tree resolve_package PARAMS ((tree, tree *));
114 static tree lookup_package_type PARAMS ((const char *, int));
115 static tree resolve_class PARAMS ((tree, tree, tree, tree));
116 static void declare_local_variables PARAMS ((int, tree, tree));
117 static void source_start_java_method PARAMS ((tree));
118 static void source_end_java_method PARAMS ((void));
119 static tree find_name_in_single_imports PARAMS ((tree));
120 static void check_abstract_method_header PARAMS ((tree));
121 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
122 static tree resolve_expression_name PARAMS ((tree, tree *));
123 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
124 static int check_class_interface_creation PARAMS ((int, int, tree,
126 static tree patch_method_invocation PARAMS ((tree, tree, tree, int,
128 static int breakdown_qualified PARAMS ((tree *, tree *, tree));
129 static int in_same_package PARAMS ((tree, tree));
130 static tree resolve_and_layout PARAMS ((tree, tree));
131 static tree qualify_and_find PARAMS ((tree, tree, tree));
132 static tree resolve_no_layout PARAMS ((tree, tree));
133 static int invocation_mode PARAMS ((tree, int));
134 static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
136 static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
138 static tree find_most_specific_methods_list PARAMS ((tree));
139 static int argument_types_convertible PARAMS ((tree, tree));
140 static tree patch_invoke PARAMS ((tree, tree, tree));
141 static int maybe_use_access_method PARAMS ((int, tree *, tree *));
142 static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
143 static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
144 static tree obtain_incomplete_type PARAMS ((tree));
145 static tree java_complete_lhs PARAMS ((tree));
146 static tree java_complete_tree PARAMS ((tree));
147 static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
148 static int analyze_clinit_body PARAMS ((tree));
149 static int maybe_yank_clinit PARAMS ((tree));
150 static void java_complete_expand_method PARAMS ((tree));
151 static int unresolved_type_p PARAMS ((tree, tree *));
152 static void create_jdep_list PARAMS ((struct parser_ctxt *));
153 static tree build_expr_block PARAMS ((tree, tree));
154 static tree enter_block PARAMS ((void));
155 static tree exit_block PARAMS ((void));
156 static tree lookup_name_in_blocks PARAMS ((tree));
157 static void maybe_absorb_scoping_blocks PARAMS ((void));
158 static tree build_method_invocation PARAMS ((tree, tree));
159 static tree build_new_invocation PARAMS ((tree, tree));
160 static tree build_assignment PARAMS ((int, int, tree, tree));
161 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
162 static int check_final_assignment PARAMS ((tree ,tree));
163 static tree patch_assignment PARAMS ((tree, tree, tree ));
164 static tree patch_binop PARAMS ((tree, tree, tree));
165 static tree build_unaryop PARAMS ((int, int, tree));
166 static tree build_incdec PARAMS ((int, int, tree, int));
167 static tree patch_unaryop PARAMS ((tree, tree));
168 static tree build_cast PARAMS ((int, tree, tree));
169 static tree build_null_of_type PARAMS ((tree));
170 static tree patch_cast PARAMS ((tree, tree));
171 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
172 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
173 static int valid_cast_to_p PARAMS ((tree, tree));
174 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
175 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
176 static tree try_reference_assignconv PARAMS ((tree, tree));
177 static tree build_unresolved_array_type PARAMS ((tree));
178 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
179 static tree build_array_ref PARAMS ((int, tree, tree));
180 static tree patch_array_ref PARAMS ((tree));
181 static tree make_qualified_name PARAMS ((tree, tree, int));
182 static tree merge_qualified_name PARAMS ((tree, tree));
183 static tree make_qualified_primary PARAMS ((tree, tree, int));
184 static int resolve_qualified_expression_name PARAMS ((tree, tree *,
186 static void qualify_ambiguous_name PARAMS ((tree));
187 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
188 static tree build_newarray_node PARAMS ((tree, tree, int));
189 static tree patch_newarray PARAMS ((tree));
190 static tree resolve_type_during_patch PARAMS ((tree));
191 static tree build_this PARAMS ((int));
192 static tree build_wfl_wrap PARAMS ((tree, int));
193 static tree build_return PARAMS ((int, tree));
194 static tree patch_return PARAMS ((tree));
195 static tree maybe_access_field PARAMS ((tree, tree, tree));
196 static int complete_function_arguments PARAMS ((tree));
197 static int check_for_static_method_reference PARAMS ((tree, tree, tree,
199 static int not_accessible_p PARAMS ((tree, tree, tree, int));
200 static void check_deprecation PARAMS ((tree, tree));
201 static int class_in_current_package PARAMS ((tree));
202 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
203 static tree patch_if_else_statement PARAMS ((tree));
204 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
205 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
206 static tree patch_exit_expr PARAMS ((tree));
207 static tree build_labeled_block PARAMS ((int, tree));
208 static tree finish_labeled_statement PARAMS ((tree, tree));
209 static tree build_bc_statement PARAMS ((int, int, tree));
210 static tree patch_bc_statement PARAMS ((tree));
211 static tree patch_loop_statement PARAMS ((tree));
212 static tree build_new_loop PARAMS ((tree));
213 static tree build_loop_body PARAMS ((int, tree, int));
214 static tree finish_loop_body PARAMS ((int, tree, tree, int));
215 static tree build_debugable_stmt PARAMS ((int, tree));
216 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
217 static tree patch_switch_statement PARAMS ((tree));
218 static tree string_constant_concatenation PARAMS ((tree, tree));
219 static tree build_string_concatenation PARAMS ((tree, tree));
220 static tree patch_string_cst PARAMS ((tree));
221 static tree patch_string PARAMS ((tree));
222 static tree 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 void check_throws_clauses PARAMS ((tree, tree, tree));
231 static void finish_method_declaration PARAMS ((tree));
232 static tree build_super_invocation PARAMS ((tree));
233 static int verify_constructor_circularity PARAMS ((tree, tree));
234 static char *constructor_circularity_msg PARAMS ((tree, tree));
235 static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
237 static const char *get_printable_method_name PARAMS ((tree));
238 static tree patch_conditional_expr PARAMS ((tree, tree, tree));
239 static tree generate_finit PARAMS ((tree));
240 static void add_instance_initializer PARAMS ((tree));
241 static tree build_instance_initializer PARAMS ((tree));
242 static void fix_constructors PARAMS ((tree));
243 static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
245 static void craft_constructor PARAMS ((tree, tree));
246 static int verify_constructor_super PARAMS ((tree));
247 static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
248 static void start_artificial_method_body PARAMS ((tree));
249 static void end_artificial_method_body PARAMS ((tree));
250 static int check_method_redefinition PARAMS ((tree, tree));
251 static int check_method_types_complete PARAMS ((tree));
252 static void java_check_regular_methods PARAMS ((tree));
253 static void java_check_abstract_methods PARAMS ((tree));
254 static tree maybe_build_primttype_type_ref PARAMS ((tree, 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 find_as_inner_class PARAMS ((tree, tree, tree));
302 static tree find_as_inner_class_do PARAMS ((tree, tree));
303 static int check_inner_class_redefinition PARAMS ((tree, tree));
305 static tree build_thisn_assign PARAMS ((void));
306 static tree build_current_thisn PARAMS ((tree));
307 static tree build_access_to_thisn PARAMS ((tree, tree, int));
308 static tree maybe_build_thisn_access_method PARAMS ((tree));
310 static tree build_outer_field_access PARAMS ((tree, tree));
311 static tree build_outer_field_access_methods PARAMS ((tree));
312 static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
314 static tree build_outer_method_access_method PARAMS ((tree));
315 static tree build_new_access_id PARAMS ((void));
316 static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
319 static int outer_field_access_p PARAMS ((tree, tree));
320 static int outer_field_expanded_access_p PARAMS ((tree, tree *,
322 static tree outer_field_access_fix PARAMS ((tree, tree, tree));
323 static tree build_incomplete_class_ref PARAMS ((int, tree));
324 static tree patch_incomplete_class_ref PARAMS ((tree));
325 static tree create_anonymous_class PARAMS ((int, tree));
326 static void patch_anonymous_class PARAMS ((tree, tree, tree));
327 static void add_inner_class_fields PARAMS ((tree, tree));
329 static tree build_dot_class_method PARAMS ((tree));
330 static tree build_dot_class_method_invocation PARAMS ((tree));
331 static void create_new_parser_context PARAMS ((int));
332 static void mark_parser_ctxt PARAMS ((void *));
333 static tree maybe_build_class_init_for_field PARAMS ((tree, tree));
335 /* Number of error found so far. */
336 int java_error_count;
337 /* Number of warning found so far. */
338 int java_warning_count;
339 /* Tell when not to fold, when doing xrefs */
341 /* Cyclic inheritance report, as it can be set by layout_class */
342 const char *cyclic_inheritance_report;
344 /* Tell when we're within an instance initializer */
345 static int in_instance_initializer;
347 /* The current parser context */
348 struct parser_ctxt *ctxp;
350 /* List of things that were analyzed for which code will be generated */
351 struct parser_ctxt *ctxp_for_generation = NULL;
353 /* binop_lookup maps token to tree_code. It is used where binary
354 operations are involved and required by the parser. RDIV_EXPR
355 covers both integral/floating point division. The code is changed
356 once the type of both operator is worked out. */
358 static enum tree_code binop_lookup[19] =
360 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
361 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
362 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
363 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
364 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
366 #define BINOP_LOOKUP(VALUE) \
367 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
369 /* This is the end index for binary operators that can also be used
370 in compound assignements. */
371 #define BINOP_COMPOUND_CANDIDATES 11
373 /* The "$L" identifier we use to create labels. */
374 static tree label_id = NULL_TREE;
376 /* The "StringBuffer" identifier used for the String `+' operator. */
377 static tree wfl_string_buffer = NULL_TREE;
379 /* The "append" identifier used for String `+' operator. */
380 static tree wfl_append = NULL_TREE;
382 /* The "toString" identifier used for String `+' operator. */
383 static tree wfl_to_string = NULL_TREE;
385 /* The "java.lang" import qualified name. */
386 static tree java_lang_id = NULL_TREE;
388 /* The generated `inst$' identifier used for generated enclosing
389 instance/field access functions. */
390 static tree inst_id = NULL_TREE;
392 /* The "java.lang.Cloneable" qualified name. */
393 static tree java_lang_cloneable = NULL_TREE;
395 /* The "java.io.Serializable" qualified name. */
396 static tree java_io_serializable = NULL_TREE;
398 /* Context and flag for static blocks */
399 static tree current_static_block = NULL_TREE;
401 /* The generated `write_parm_value$' identifier. */
404 /* The list of all packages we've seen so far */
405 static tree package_list = NULL_TREE;
407 /* Hold THIS for the scope of the current public method decl. */
408 static tree current_this;
410 /* Hold a list of catch clauses list. The first element of this list is
411 the list of the catch clauses of the currently analysed try block. */
412 static tree currently_caught_type_list;
414 static tree src_parse_roots[1] = { NULL_TREE };
416 /* All classes seen from source code */
417 #define gclass_list src_parse_roots[0]
419 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
420 line and point it out. */
421 /* Should point out the one that don't fit. ASCII/unicode, going
424 #define check_modifiers(__message, __value, __mask) do { \
425 if ((__value) & ~(__mask)) \
427 int i, remainder = (__value) & ~(__mask); \
428 for (i = 0; i <= 10; i++) \
429 if ((1 << i) & remainder) \
430 parse_error_context (ctxp->modifier_ctx [i], (__message), \
431 java_accstring_lookup (1 << i)); \
453 /* Things defined here have to match the order of what's in the
454 binop_lookup table. */
456 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
457 %token LS_TK SRS_TK ZRS_TK
458 %token AND_TK XOR_TK OR_TK
459 %token BOOL_AND_TK BOOL_OR_TK
460 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
462 /* This maps to the same binop_lookup entry than the token above */
464 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
466 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
467 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
470 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
472 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
473 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
474 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
475 %token PAD_TK ABSTRACT_TK MODIFIER_TK
478 /* Keep those two in order, too */
479 %token DECR_TK INCR_TK
481 /* From now one, things can be in any order */
483 %token DEFAULT_TK IF_TK THROW_TK
484 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
485 %token THROWS_TK BREAK_TK IMPORT_TK
486 %token ELSE_TK INSTANCEOF_TK RETURN_TK
487 %token VOID_TK CATCH_TK INTERFACE_TK
488 %token CASE_TK EXTENDS_TK FINALLY_TK
489 %token SUPER_TK WHILE_TK CLASS_TK
490 %token SWITCH_TK CONST_TK TRY_TK
491 %token FOR_TK NEW_TK CONTINUE_TK
492 %token GOTO_TK PACKAGE_TK THIS_TK
494 %token BYTE_TK SHORT_TK INT_TK LONG_TK
495 %token CHAR_TK INTEGRAL_TK
497 %token FLOAT_TK DOUBLE_TK FP_TK
501 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
503 %token ASSIGN_ANY_TK ASSIGN_TK
504 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
506 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
507 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
509 %type <value> modifiers MODIFIER_TK final synchronized
511 %type <node> super ID_TK identifier
512 %type <node> name simple_name qualified_name
513 %type <node> type_declaration compilation_unit
514 field_declaration method_declaration extends_interfaces
515 interfaces interface_type_list
516 class_member_declaration
517 import_declarations package_declaration
518 type_declarations interface_body
519 interface_member_declaration constant_declaration
520 interface_member_declarations interface_type
521 abstract_method_declaration interface_type_list
522 %type <node> class_body_declaration class_member_declaration
523 static_initializer constructor_declaration block
524 %type <node> class_body_declarations constructor_header
525 %type <node> class_or_interface_type class_type class_type_list
526 constructor_declarator explicit_constructor_invocation
527 %type <node> dim_expr dim_exprs this_or_super throws
529 %type <node> variable_declarator_id variable_declarator
530 variable_declarators variable_initializer
531 variable_initializers constructor_body
534 %type <node> class_body block_end constructor_block_end
535 %type <node> statement statement_without_trailing_substatement
536 labeled_statement if_then_statement label_decl
537 if_then_else_statement while_statement for_statement
538 statement_nsi labeled_statement_nsi do_statement
539 if_then_else_statement_nsi while_statement_nsi
540 for_statement_nsi statement_expression_list for_init
541 for_update statement_expression expression_statement
542 primary_no_new_array expression primary
543 array_creation_expression array_type
544 class_instance_creation_expression field_access
545 method_invocation array_access something_dot_new
546 argument_list postfix_expression while_expression
547 post_increment_expression post_decrement_expression
548 unary_expression_not_plus_minus unary_expression
549 pre_increment_expression pre_decrement_expression
550 unary_expression_not_plus_minus cast_expression
551 multiplicative_expression additive_expression
552 shift_expression relational_expression
553 equality_expression and_expression
554 exclusive_or_expression inclusive_or_expression
555 conditional_and_expression conditional_or_expression
556 conditional_expression assignment_expression
557 left_hand_side assignment for_header for_begin
558 constant_expression do_statement_begin empty_statement
559 switch_statement synchronized_statement throw_statement
560 try_statement switch_expression switch_block
561 catches catch_clause catch_clause_parameter finally
562 anonymous_class_creation
563 %type <node> return_statement break_statement continue_statement
565 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
566 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
567 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
568 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
569 %type <operator> ASSIGN_ANY_TK assignment_operator
570 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
571 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
572 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
573 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
574 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
575 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
576 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
577 %type <operator> NEW_TK
579 %type <node> method_body
581 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
582 STRING_LIT_TK NULL_TK VOID_TK
584 %type <node> IF_TK WHILE_TK FOR_TK
586 %type <node> formal_parameter_list formal_parameter
587 method_declarator method_header
589 %type <node> primitive_type reference_type type
590 BOOLEAN_TK INTEGRAL_TK FP_TK
592 /* Added or modified JDK 1.1 rule types */
593 %type <node> type_literals
596 /* 19.2 Production from 2.3: The Syntactic Grammar */
599 /* Register static variables with the garbage
601 ggc_add_tree_root (&label_id, 1);
602 ggc_add_tree_root (&wfl_string_buffer, 1);
603 ggc_add_tree_root (&wfl_append, 1);
604 ggc_add_tree_root (&wfl_to_string, 1);
605 ggc_add_tree_root (&java_lang_id, 1);
606 ggc_add_tree_root (&inst_id, 1);
607 ggc_add_tree_root (&java_lang_cloneable, 1);
608 ggc_add_tree_root (&java_io_serializable, 1);
609 ggc_add_tree_root (¤t_static_block, 1);
610 ggc_add_tree_root (&wpv_id, 1);
611 ggc_add_tree_root (&package_list, 1);
612 ggc_add_tree_root (¤t_this, 1);
613 ggc_add_tree_root (¤tly_caught_type_list, 1);
614 ggc_add_root (&ctxp, 1,
615 sizeof (struct parser_ctxt *),
617 ggc_add_root (&ctxp_for_generation, 1,
618 sizeof (struct parser_ctxt *),
625 /* 19.3 Productions from 3: Lexical structure */
635 /* 19.4 Productions from 4: Types, Values and Variables */
648 class_or_interface_type
652 class_or_interface_type:
657 class_or_interface_type /* Default rule */
661 class_or_interface_type
667 int osb = pop_current_osb (ctxp);
668 tree t = build_java_array_type (($1), -1);
670 t = build_unresolved_array_type (t);
675 int osb = pop_current_osb (ctxp);
678 t = build_unresolved_array_type (t);
683 /* 19.5 Productions from 6: Names */
685 simple_name /* Default rule */
686 | qualified_name /* Default rule */
690 identifier /* Default rule */
694 name DOT_TK identifier
695 { $$ = make_qualified_name ($1, $3, $2.location); }
702 /* 19.6: Production from 7: Packages */
705 | package_declaration
706 | import_declarations
708 | package_declaration import_declarations
709 | package_declaration type_declarations
710 | import_declarations type_declarations
711 | package_declaration import_declarations type_declarations
719 | import_declarations import_declaration
727 | type_declarations type_declaration
731 PACKAGE_TK name SC_TK
733 ctxp->package = EXPR_WFL_NODE ($2);
734 register_package (ctxp->package);
737 {yyerror ("Missing name"); RECOVER;}
738 | PACKAGE_TK name error
739 {yyerror ("';' expected"); RECOVER;}
743 single_type_import_declaration
744 | type_import_on_demand_declaration
747 single_type_import_declaration:
750 tree name = EXPR_WFL_NODE ($2), last_name;
751 int i = IDENTIFIER_LENGTH (name)-1;
752 const char *last = &IDENTIFIER_POINTER (name)[i];
753 while (last != IDENTIFIER_POINTER (name))
759 last_name = get_identifier (++last);
760 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
762 tree err = find_name_in_single_imports (last_name);
763 if (err && err != name)
765 ($2, "Ambiguous class: `%s' and `%s'",
766 IDENTIFIER_POINTER (name),
767 IDENTIFIER_POINTER (err));
769 REGISTER_IMPORT ($2, last_name);
772 REGISTER_IMPORT ($2, last_name);
775 {yyerror ("Missing name"); RECOVER;}
776 | IMPORT_TK name error
777 {yyerror ("';' expected"); RECOVER;}
780 type_import_on_demand_declaration:
781 IMPORT_TK name DOT_TK MULT_TK SC_TK
783 tree name = EXPR_WFL_NODE ($2);
784 /* Don't import java.lang.* twice. */
785 if (name != java_lang_id)
787 read_import_dir ($2);
788 ctxp->import_demand_list =
789 chainon (ctxp->import_demand_list,
790 build_tree_list ($2, NULL_TREE));
793 | IMPORT_TK name DOT_TK error
794 {yyerror ("'*' expected"); RECOVER;}
795 | IMPORT_TK name DOT_TK MULT_TK error
796 {yyerror ("';' expected"); RECOVER;}
801 { end_class_declaration (0); }
802 | interface_declaration
803 { end_class_declaration (0); }
808 yyerror ("Class or interface declaration expected");
812 /* 19.7 Shortened from the original:
813 modifiers: modifier | modifiers modifier
814 modifier: any of public... */
820 | modifiers MODIFIER_TK
825 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
826 java_accstring_lookup (acc));
834 /* 19.8.1 Production from $8.1: Class Declaration */
836 modifiers CLASS_TK identifier super interfaces
837 { create_class ($1, $3, $4, $5); }
839 | CLASS_TK identifier super interfaces
840 { create_class (0, $2, $3, $4); }
842 | modifiers CLASS_TK error
843 {yyerror ("Missing class name"); RECOVER;}
845 {yyerror ("Missing class name"); RECOVER;}
846 | CLASS_TK identifier error
848 if (!ctxp->class_err) yyerror ("'{' expected");
851 | modifiers CLASS_TK identifier error
852 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
857 | EXTENDS_TK class_type
859 | EXTENDS_TK class_type error
860 {yyerror ("'{' expected"); ctxp->class_err=1;}
862 {yyerror ("Missing super class name"); ctxp->class_err=1;}
867 | IMPLEMENTS_TK interface_type_list
869 | IMPLEMENTS_TK error
872 yyerror ("Missing interface name");
879 ctxp->interface_number = 1;
880 $$ = build_tree_list ($1, NULL_TREE);
882 | interface_type_list C_TK interface_type
884 ctxp->interface_number++;
885 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
887 | interface_type_list C_TK error
888 {yyerror ("Missing interface name"); RECOVER;}
894 /* Store the location of the `}' when doing xrefs */
896 DECL_END_SOURCE_LINE (GET_CPC ()) =
897 EXPR_WFL_ADD_COL ($2.location, 1);
900 | OCB_TK class_body_declarations CCB_TK
902 /* Store the location of the `}' when doing xrefs */
904 DECL_END_SOURCE_LINE (GET_CPC ()) =
905 EXPR_WFL_ADD_COL ($3.location, 1);
910 class_body_declarations:
911 class_body_declaration
912 | class_body_declarations class_body_declaration
915 class_body_declaration:
916 class_member_declaration
918 | constructor_declaration
919 | block /* Added, JDK1.1, instance initializer */
921 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
922 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
926 class_member_declaration:
929 | class_declaration /* Added, JDK1.1 inner classes */
930 { end_class_declaration (1); }
931 | interface_declaration /* Added, JDK1.1 inner interfaces */
932 { end_class_declaration (1); }
936 /* 19.8.2 Productions from 8.3: Field Declarations */
938 type variable_declarators SC_TK
939 { register_fields (0, $1, $2); }
940 | modifiers type variable_declarators SC_TK
943 ("Illegal modifier `%s' for field declaration",
944 $1, FIELD_MODIFIERS);
945 check_modifiers_consistency ($1);
946 register_fields ($1, $2, $3);
950 variable_declarators:
951 /* Should we use build_decl_list () instead ? FIXME */
952 variable_declarator /* Default rule */
953 | variable_declarators C_TK variable_declarator
954 { $$ = chainon ($1, $3); }
955 | variable_declarators C_TK error
956 {yyerror ("Missing term"); RECOVER;}
960 variable_declarator_id
961 { $$ = build_tree_list ($1, NULL_TREE); }
962 | variable_declarator_id ASSIGN_TK variable_initializer
964 if (java_error_count)
967 ($1, build_assignment ($2.token, $2.location, $1, $3));
969 | variable_declarator_id ASSIGN_TK error
971 yyerror ("Missing variable initializer");
972 $$ = build_tree_list ($1, NULL_TREE);
975 | variable_declarator_id ASSIGN_TK variable_initializer error
977 yyerror ("';' expected");
978 $$ = build_tree_list ($1, NULL_TREE);
983 variable_declarator_id:
985 | variable_declarator_id OSB_TK CSB_TK
986 { $$ = build_unresolved_array_type ($1); }
988 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
989 | variable_declarator_id OSB_TK error
991 tree node = java_lval.node;
992 if (node && (TREE_CODE (node) == INTEGER_CST
993 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
994 yyerror ("Can't specify array dimension in a declaration");
996 yyerror ("']' expected");
999 | variable_declarator_id CSB_TK error
1000 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
1003 variable_initializer:
1008 /* 19.8.3 Productions from 8.4: Method Declarations */
1012 current_function_decl = $1;
1013 if (current_function_decl
1014 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1015 source_start_java_method (current_function_decl);
1017 current_function_decl = NULL_TREE;
1020 { finish_method_declaration ($3); }
1021 | method_header error
1022 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1026 type method_declarator throws
1027 { $$ = method_header (0, $1, $2, $3); }
1028 | VOID_TK method_declarator throws
1029 { $$ = method_header (0, void_type_node, $2, $3); }
1030 | modifiers type method_declarator throws
1031 { $$ = method_header ($1, $2, $3, $4); }
1032 | modifiers VOID_TK method_declarator throws
1033 { $$ = method_header ($1, void_type_node, $3, $4); }
1036 yyerror ("Invalid method declaration, method name required");
1039 | modifiers type error
1042 {yyerror ("Identifier expected"); RECOVER;}
1043 | modifiers VOID_TK error
1044 {yyerror ("Identifier expected"); RECOVER;}
1047 yyerror ("Invalid method declaration, return type required");
1053 identifier OP_TK CP_TK
1055 ctxp->formal_parameter_number = 0;
1056 $$ = method_declarator ($1, NULL_TREE);
1058 | identifier OP_TK formal_parameter_list CP_TK
1059 { $$ = method_declarator ($1, $3); }
1060 | method_declarator OSB_TK CSB_TK
1062 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1064 build_unresolved_array_type (TREE_PURPOSE ($1));
1065 parse_warning_context
1067 "Discouraged form of returned type specification");
1069 | identifier OP_TK error
1070 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1071 | method_declarator OSB_TK error
1072 {yyerror ("']' expected"); RECOVER;}
1075 formal_parameter_list:
1078 ctxp->formal_parameter_number = 1;
1080 | formal_parameter_list C_TK formal_parameter
1082 ctxp->formal_parameter_number += 1;
1083 $$ = chainon ($1, $3);
1085 | formal_parameter_list C_TK error
1086 { yyerror ("Missing formal parameter term"); RECOVER; }
1090 type variable_declarator_id
1092 $$ = build_tree_list ($2, $1);
1094 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1096 $$ = build_tree_list ($3, $2);
1097 ARG_FINAL_P ($$) = 1;
1101 yyerror ("Missing identifier"); RECOVER;
1106 yyerror ("Missing identifier"); RECOVER;
1114 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1116 if ($1 != ACC_FINAL)
1117 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1123 | THROWS_TK class_type_list
1126 {yyerror ("Missing class type term"); RECOVER;}
1131 { $$ = build_tree_list ($1, $1); }
1132 | class_type_list C_TK class_type
1133 { $$ = tree_cons ($3, $3, $1); }
1134 | class_type_list C_TK error
1135 {yyerror ("Missing class type term"); RECOVER;}
1140 | SC_TK { $$ = NULL_TREE; }
1143 /* 19.8.4 Productions from 8.5: Static Initializers */
1147 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1148 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1149 current_static_block = NULL_TREE;
1153 static: /* Test lval.sub_token here */
1156 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1157 /* Can't have a static initializer in an innerclass */
1158 if ($1 | ACC_STATIC &&
1159 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1161 (MODIFIER_WFL (STATIC_TK),
1162 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1163 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1164 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1168 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1169 constructor_declaration:
1172 current_function_decl = $1;
1173 source_start_java_method (current_function_decl);
1176 { finish_method_declaration ($3); }
1180 constructor_declarator throws
1181 { $$ = method_header (0, NULL_TREE, $1, $2); }
1182 | modifiers constructor_declarator throws
1183 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1186 constructor_declarator:
1187 simple_name OP_TK CP_TK
1189 ctxp->formal_parameter_number = 0;
1190 $$ = method_declarator ($1, NULL_TREE);
1192 | simple_name OP_TK formal_parameter_list CP_TK
1193 { $$ = method_declarator ($1, $3); }
1197 /* Unlike regular method, we always need a complete (empty)
1198 body so we can safely perform all the required code
1199 addition (super invocation and field initialization) */
1200 block_begin constructor_block_end
1202 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1205 | block_begin explicit_constructor_invocation constructor_block_end
1207 | block_begin block_statements constructor_block_end
1209 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1213 constructor_block_end:
1217 /* Error recovery for that rule moved down expression_statement: rule. */
1218 explicit_constructor_invocation:
1219 this_or_super OP_TK CP_TK SC_TK
1221 $$ = build_method_invocation ($1, NULL_TREE);
1222 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1223 $$ = java_method_add_stmt (current_function_decl, $$);
1225 | this_or_super OP_TK argument_list CP_TK SC_TK
1227 $$ = build_method_invocation ($1, $3);
1228 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1229 $$ = java_method_add_stmt (current_function_decl, $$);
1231 /* Added, JDK1.1 inner classes. Modified because the rule
1232 'primary' couldn't work. */
1233 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1234 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1235 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1236 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1239 this_or_super: /* Added, simplifies error diagnostics */
1242 tree wfl = build_wfl_node (this_identifier_node);
1243 EXPR_WFL_LINECOL (wfl) = $1.location;
1248 tree wfl = build_wfl_node (super_identifier_node);
1249 EXPR_WFL_LINECOL (wfl) = $1.location;
1254 /* 19.9 Productions from 9: Interfaces */
1255 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1256 interface_declaration:
1257 INTERFACE_TK identifier
1258 { create_interface (0, $2, NULL_TREE); }
1260 | modifiers INTERFACE_TK identifier
1261 { create_interface ($1, $3, NULL_TREE); }
1263 | INTERFACE_TK identifier extends_interfaces
1264 { create_interface (0, $2, $3); }
1266 | modifiers INTERFACE_TK identifier extends_interfaces
1267 { create_interface ($1, $3, $4); }
1269 | INTERFACE_TK identifier error
1270 {yyerror ("'{' expected"); RECOVER;}
1271 | modifiers INTERFACE_TK identifier error
1272 {yyerror ("'{' expected"); RECOVER;}
1276 EXTENDS_TK interface_type
1278 ctxp->interface_number = 1;
1279 $$ = build_tree_list ($2, NULL_TREE);
1281 | extends_interfaces C_TK interface_type
1283 ctxp->interface_number++;
1284 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1287 {yyerror ("Invalid interface type"); RECOVER;}
1288 | extends_interfaces C_TK error
1289 {yyerror ("Missing term"); RECOVER;}
1295 | OCB_TK interface_member_declarations CCB_TK
1299 interface_member_declarations:
1300 interface_member_declaration
1301 | interface_member_declarations interface_member_declaration
1304 interface_member_declaration:
1305 constant_declaration
1306 | abstract_method_declaration
1307 | class_declaration /* Added, JDK1.1 inner classes */
1308 { end_class_declaration (1); }
1309 | interface_declaration /* Added, JDK1.1 inner interfaces */
1310 { end_class_declaration (1); }
1313 constant_declaration:
1317 abstract_method_declaration:
1320 check_abstract_method_header ($1);
1321 current_function_decl = NULL_TREE; /* FIXME ? */
1323 | method_header error
1324 {yyerror ("';' expected"); RECOVER;}
1327 /* 19.10 Productions from 10: Arrays */
1330 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1331 | OCB_TK variable_initializers CCB_TK
1332 { $$ = build_new_array_init ($1.location, $2); }
1333 | OCB_TK variable_initializers C_TK CCB_TK
1334 { $$ = build_new_array_init ($1.location, $2); }
1337 variable_initializers:
1338 variable_initializer
1340 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1343 | variable_initializers C_TK variable_initializer
1345 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1347 | variable_initializers C_TK error
1348 {yyerror ("Missing term"); RECOVER;}
1351 /* 19.11 Production from 14: Blocks and Statements */
1355 /* Store the location of the `}' when doing xrefs */
1356 if (current_function_decl && flag_emit_xref)
1357 DECL_END_SOURCE_LINE (current_function_decl) =
1358 EXPR_WFL_ADD_COL ($2.location, 1);
1359 $$ = empty_stmt_node;
1361 | block_begin block_statements block_end
1373 maybe_absorb_scoping_blocks ();
1374 /* Store the location of the `}' when doing xrefs */
1375 if (current_function_decl && flag_emit_xref)
1376 DECL_END_SOURCE_LINE (current_function_decl) =
1377 EXPR_WFL_ADD_COL ($1.location, 1);
1379 if (!BLOCK_SUBBLOCKS ($$))
1380 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1386 | block_statements block_statement
1390 local_variable_declaration_statement
1392 { java_method_add_stmt (current_function_decl, $1); }
1393 | class_declaration /* Added, JDK1.1 local classes */
1395 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1396 end_class_declaration (1);
1400 local_variable_declaration_statement:
1401 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1404 local_variable_declaration:
1405 type variable_declarators
1406 { declare_local_variables (0, $1, $2); }
1407 | final type variable_declarators /* Added, JDK1.1 final locals */
1408 { declare_local_variables ($1, $2, $3); }
1412 statement_without_trailing_substatement
1415 | if_then_else_statement
1418 { $$ = exit_block (); }
1422 statement_without_trailing_substatement
1423 | labeled_statement_nsi
1424 | if_then_else_statement_nsi
1425 | while_statement_nsi
1427 { $$ = exit_block (); }
1430 statement_without_trailing_substatement:
1433 | expression_statement
1437 | continue_statement
1439 | synchronized_statement
1447 if (flag_extraneous_semicolon)
1449 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1450 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1452 $$ = empty_stmt_node;
1457 identifier REL_CL_TK
1459 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1460 EXPR_WFL_NODE ($1));
1462 push_labeled_block ($$);
1463 PUSH_LABELED_BLOCK ($$);
1468 label_decl statement
1469 { $$ = finish_labeled_statement ($1, $2); }
1471 {yyerror ("':' expected"); RECOVER;}
1474 labeled_statement_nsi:
1475 label_decl statement_nsi
1476 { $$ = finish_labeled_statement ($1, $2); }
1479 /* We concentrate here a bunch of error handling rules that we couldn't write
1480 earlier, because expression_statement catches a missing ';'. */
1481 expression_statement:
1482 statement_expression SC_TK
1484 /* We have a statement. Generate a WFL around it so
1486 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1487 /* We know we have a statement, so set the debug
1488 info to be eventually generate here. */
1489 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1493 YYNOT_TWICE yyerror ("Invalid expression statement");
1494 DRECOVER (expr_stmt);
1498 YYNOT_TWICE yyerror ("Invalid expression statement");
1499 DRECOVER (expr_stmt);
1503 YYNOT_TWICE yyerror ("Invalid expression statement");
1504 DRECOVER (expr_stmt);
1506 | this_or_super OP_TK error
1507 {yyerror ("')' expected"); RECOVER;}
1508 | this_or_super OP_TK CP_TK error
1510 parse_ctor_invocation_error ();
1513 | this_or_super OP_TK argument_list error
1514 {yyerror ("')' expected"); RECOVER;}
1515 | this_or_super OP_TK argument_list CP_TK error
1517 parse_ctor_invocation_error ();
1520 | name DOT_TK SUPER_TK error
1521 {yyerror ("'(' expected"); RECOVER;}
1522 | name DOT_TK SUPER_TK OP_TK error
1523 {yyerror ("')' expected"); RECOVER;}
1524 | name DOT_TK SUPER_TK OP_TK argument_list error
1525 {yyerror ("')' expected"); RECOVER;}
1526 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1527 {yyerror ("';' expected"); RECOVER;}
1528 | name DOT_TK SUPER_TK OP_TK CP_TK error
1529 {yyerror ("';' expected"); RECOVER;}
1532 statement_expression:
1534 | pre_increment_expression
1535 | pre_decrement_expression
1536 | post_increment_expression
1537 | post_decrement_expression
1539 | class_instance_creation_expression
1543 IF_TK OP_TK expression CP_TK statement
1545 $$ = build_if_else_statement ($2.location, $3,
1549 {yyerror ("'(' expected"); RECOVER;}
1551 {yyerror ("Missing term"); RECOVER;}
1552 | IF_TK OP_TK expression error
1553 {yyerror ("')' expected"); RECOVER;}
1556 if_then_else_statement:
1557 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1558 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1561 if_then_else_statement_nsi:
1562 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1563 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1573 /* Make into "proper list" of COMPOUND_EXPRs.
1574 I.e. make the last statment also have its own
1576 maybe_absorb_scoping_blocks ();
1577 TREE_OPERAND ($1, 1) = exit_block ();
1578 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1583 SWITCH_TK OP_TK expression CP_TK
1585 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1586 EXPR_WFL_LINECOL ($$) = $2.location;
1589 {yyerror ("'(' expected"); RECOVER;}
1590 | SWITCH_TK OP_TK error
1591 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1592 | SWITCH_TK OP_TK expression CP_TK error
1593 {yyerror ("'{' expected"); RECOVER;}
1596 /* Default assignment is there to avoid type node on switch_block
1602 | OCB_TK switch_labels CCB_TK
1604 | OCB_TK switch_block_statement_groups CCB_TK
1606 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1610 switch_block_statement_groups:
1611 switch_block_statement_group
1612 | switch_block_statement_groups switch_block_statement_group
1615 switch_block_statement_group:
1616 switch_labels block_statements
1621 | switch_labels switch_label
1625 CASE_TK constant_expression REL_CL_TK
1627 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1628 EXPR_WFL_LINECOL (lab) = $1.location;
1629 java_method_add_stmt (current_function_decl, lab);
1631 | DEFAULT_TK REL_CL_TK
1633 tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1634 EXPR_WFL_LINECOL (lab) = $1.location;
1635 java_method_add_stmt (current_function_decl, lab);
1638 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1639 | CASE_TK constant_expression error
1640 {yyerror ("':' expected"); RECOVER;}
1642 {yyerror ("':' expected"); RECOVER;}
1646 WHILE_TK OP_TK expression CP_TK
1648 tree body = build_loop_body ($2.location, $3, 0);
1649 $$ = build_new_loop (body);
1654 while_expression statement
1655 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1657 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1658 | WHILE_TK OP_TK error
1659 {yyerror ("Missing term and ')' expected"); RECOVER;}
1660 | WHILE_TK OP_TK expression error
1661 {yyerror ("')' expected"); RECOVER;}
1664 while_statement_nsi:
1665 while_expression statement_nsi
1666 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1672 tree body = build_loop_body (0, NULL_TREE, 1);
1673 $$ = build_new_loop (body);
1675 /* Need error handing here. FIXME */
1679 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1680 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1684 for_begin SC_TK expression SC_TK for_update CP_TK statement
1686 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1687 $3 = build_wfl_node ($3);
1688 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1690 | for_begin SC_TK SC_TK for_update CP_TK statement
1692 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1693 /* We have not condition, so we get rid of the EXIT_EXPR */
1694 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1697 | for_begin SC_TK error
1698 {yyerror ("Invalid control expression"); RECOVER;}
1699 | for_begin SC_TK expression SC_TK error
1700 {yyerror ("Invalid update expression"); RECOVER;}
1701 | for_begin SC_TK SC_TK error
1702 {yyerror ("Invalid update expression"); RECOVER;}
1706 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1707 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1708 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1710 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1711 /* We have not condition, so we get rid of the EXIT_EXPR */
1712 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1720 /* This scope defined for local variable that may be
1721 defined within the scope of the for loop */
1725 {yyerror ("'(' expected"); DRECOVER(for_1);}
1726 | FOR_TK OP_TK error
1727 {yyerror ("Invalid init statement"); RECOVER;}
1733 /* We now declare the loop body. The loop is
1734 declared as a for loop. */
1735 tree body = build_loop_body (0, NULL_TREE, 0);
1736 $$ = build_new_loop (body);
1737 FOR_LOOP_P ($$) = 1;
1738 /* The loop is added to the current block the for
1739 statement is defined within */
1740 java_method_add_stmt (current_function_decl, $$);
1743 for_init: /* Can be empty */
1744 { $$ = empty_stmt_node; }
1745 | statement_expression_list
1747 /* Init statement recorded within the previously
1748 defined block scope */
1749 $$ = java_method_add_stmt (current_function_decl, $1);
1751 | local_variable_declaration
1753 /* Local variable are recorded within the previously
1754 defined block scope */
1757 | statement_expression_list error
1758 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1761 for_update: /* Can be empty */
1762 {$$ = empty_stmt_node;}
1763 | statement_expression_list
1764 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1767 statement_expression_list:
1768 statement_expression
1769 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1770 | statement_expression_list C_TK statement_expression
1771 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1772 | statement_expression_list C_TK error
1773 {yyerror ("Missing term"); RECOVER;}
1778 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1779 | BREAK_TK identifier SC_TK
1780 { $$ = build_bc_statement ($1.location, 1, $2); }
1782 {yyerror ("Missing term"); RECOVER;}
1783 | BREAK_TK identifier error
1784 {yyerror ("';' expected"); RECOVER;}
1789 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1790 | CONTINUE_TK identifier SC_TK
1791 { $$ = build_bc_statement ($1.location, 0, $2); }
1793 {yyerror ("Missing term"); RECOVER;}
1794 | CONTINUE_TK identifier error
1795 {yyerror ("';' expected"); RECOVER;}
1800 { $$ = build_return ($1.location, NULL_TREE); }
1801 | RETURN_TK expression SC_TK
1802 { $$ = build_return ($1.location, $2); }
1804 {yyerror ("Missing term"); RECOVER;}
1805 | RETURN_TK expression error
1806 {yyerror ("';' expected"); RECOVER;}
1810 THROW_TK expression SC_TK
1812 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1813 EXPR_WFL_LINECOL ($$) = $1.location;
1816 {yyerror ("Missing term"); RECOVER;}
1817 | THROW_TK expression error
1818 {yyerror ("';' expected"); RECOVER;}
1821 synchronized_statement:
1822 synchronized OP_TK expression CP_TK block
1824 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1825 EXPR_WFL_LINECOL ($$) =
1826 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1828 | synchronized OP_TK expression CP_TK error
1829 {yyerror ("'{' expected"); RECOVER;}
1830 | synchronized error
1831 {yyerror ("'(' expected"); RECOVER;}
1832 | synchronized OP_TK error CP_TK
1833 {yyerror ("Missing term"); RECOVER;}
1834 | synchronized OP_TK error
1835 {yyerror ("Missing term"); RECOVER;}
1842 "Illegal modifier `%s'. Only `synchronized' was expected here",
1843 $1, ACC_SYNCHRONIZED);
1844 if ($1 != ACC_SYNCHRONIZED)
1845 MODIFIER_WFL (SYNCHRONIZED_TK) =
1846 build_wfl_node (NULL_TREE);
1851 TRY_TK block catches
1852 { $$ = build_try_statement ($1.location, $2, $3); }
1853 | TRY_TK block finally
1854 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1855 | TRY_TK block catches finally
1856 { $$ = build_try_finally_statement
1857 ($1.location, build_try_statement ($1.location,
1861 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1866 | catches catch_clause
1868 TREE_CHAIN ($2) = $1;
1874 catch_clause_parameter block
1876 java_method_add_stmt (current_function_decl, $2);
1881 catch_clause_parameter:
1882 CATCH_TK OP_TK formal_parameter CP_TK
1884 /* We add a block to define a scope for
1885 formal_parameter (CCBP). The formal parameter is
1886 declared initialized by the appropriate function
1888 tree ccpb = enter_block ();
1889 tree init = build_assignment
1890 (ASSIGN_TK, $2.location, TREE_PURPOSE ($3),
1891 build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
1892 declare_local_variables (0, TREE_VALUE ($3),
1893 build_tree_list (TREE_PURPOSE ($3),
1895 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1896 EXPR_WFL_LINECOL ($$) = $1.location;
1899 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1900 | CATCH_TK OP_TK error
1902 yyerror ("Missing term or ')' expected");
1903 RECOVER; $$ = NULL_TREE;
1905 | CATCH_TK OP_TK error CP_TK /* That's for () */
1906 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1913 {yyerror ("'{' expected"); RECOVER; }
1916 /* 19.12 Production from 15: Expressions */
1918 primary_no_new_array
1919 | array_creation_expression
1922 primary_no_new_array:
1925 { $$ = build_this ($1.location); }
1926 | OP_TK expression CP_TK
1928 | class_instance_creation_expression
1933 /* Added, JDK1.1 inner classes. Documentation is wrong
1934 refering to a 'ClassName' (class_name) rule that doesn't
1935 exist. Used name: instead. */
1936 | name DOT_TK THIS_TK
1938 tree wfl = build_wfl_node (this_identifier_node);
1939 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1941 | OP_TK expression error
1942 {yyerror ("')' expected"); RECOVER;}
1944 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1945 | primitive_type DOT_TK error
1946 {yyerror ("'class' expected" ); RECOVER;}
1947 | VOID_TK DOT_TK error
1948 {yyerror ("'class' expected" ); RECOVER;}
1952 name DOT_TK CLASS_TK
1953 { $$ = build_incomplete_class_ref ($2.location, $1); }
1954 | array_type DOT_TK CLASS_TK
1955 { $$ = build_incomplete_class_ref ($2.location, $1); }
1956 | primitive_type DOT_TK CLASS_TK
1957 { $$ = build_class_ref ($1); }
1958 | VOID_TK DOT_TK CLASS_TK
1959 { $$ = build_class_ref (void_type_node); }
1962 class_instance_creation_expression:
1963 NEW_TK class_type OP_TK argument_list CP_TK
1964 { $$ = build_new_invocation ($2, $4); }
1965 | NEW_TK class_type OP_TK CP_TK
1966 { $$ = build_new_invocation ($2, NULL_TREE); }
1967 | anonymous_class_creation
1968 /* Added, JDK1.1 inner classes, modified to use name or
1969 primary instead of primary solely which couldn't work in
1971 | something_dot_new identifier OP_TK CP_TK
1973 tree ctor = build_new_invocation ($2, NULL_TREE);
1974 $$ = make_qualified_primary ($1, ctor,
1975 EXPR_WFL_LINECOL ($1));
1977 | something_dot_new identifier OP_TK CP_TK class_body
1978 | something_dot_new identifier OP_TK argument_list CP_TK
1980 tree ctor = build_new_invocation ($2, $4);
1981 $$ = make_qualified_primary ($1, ctor,
1982 EXPR_WFL_LINECOL ($1));
1984 | something_dot_new identifier OP_TK argument_list CP_TK class_body
1985 | NEW_TK error SC_TK
1986 {yyerror ("'(' expected"); DRECOVER(new_1);}
1987 | NEW_TK class_type error
1988 {yyerror ("'(' expected"); RECOVER;}
1989 | NEW_TK class_type OP_TK error
1990 {yyerror ("')' or term expected"); RECOVER;}
1991 | NEW_TK class_type OP_TK argument_list error
1992 {yyerror ("')' expected"); RECOVER;}
1993 | something_dot_new error
1994 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1995 | something_dot_new identifier error
1996 {yyerror ("'(' expected"); RECOVER;}
1999 /* Created after JDK1.1 rules originally added to
2000 class_instance_creation_expression, but modified to use
2001 'class_type' instead of 'TypeName' (type_name) which is mentionned
2002 in the documentation but doesn't exist. */
2004 anonymous_class_creation:
2005 NEW_TK class_type OP_TK argument_list CP_TK
2006 { create_anonymous_class ($1.location, $2); }
2009 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2010 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2012 end_class_declaration (1);
2014 /* Now we can craft the new expression */
2015 $$ = build_new_invocation (id, $4);
2017 /* Note that we can't possibly be here if
2018 `class_type' is an interface (in which case the
2019 anonymous class extends Object and implements
2020 `class_type', hence its constructor can't have
2023 /* Otherwise, the innerclass must feature a
2024 constructor matching `argument_list'. Anonymous
2025 classes are a bit special: it's impossible to
2026 define constructor for them, hence constructors
2027 must be generated following the hints provided by
2028 the `new' expression. Whether a super constructor
2029 of that nature exists or not is to be verified
2030 later on in verify_constructor_super.
2032 It's during the expansion of a `new' statement
2033 refering to an anonymous class that a ctor will
2034 be generated for the anonymous class, with the
2038 | NEW_TK class_type OP_TK CP_TK
2039 { create_anonymous_class ($1.location, $2); }
2042 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2043 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2045 end_class_declaration (1);
2047 /* Now we can craft the new expression. The
2048 statement doesn't need to be remember so that a
2049 constructor can be generated, since its signature
2050 is already known. */
2051 $$ = build_new_invocation (id, NULL_TREE);
2055 something_dot_new: /* Added, not part of the specs. */
2058 | primary DOT_TK NEW_TK
2065 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2066 ctxp->formal_parameter_number = 1;
2068 | argument_list C_TK expression
2070 ctxp->formal_parameter_number += 1;
2071 $$ = tree_cons (NULL_TREE, $3, $1);
2073 | argument_list C_TK error
2074 {yyerror ("Missing term"); RECOVER;}
2077 array_creation_expression:
2078 NEW_TK primitive_type dim_exprs
2079 { $$ = build_newarray_node ($2, $3, 0); }
2080 | NEW_TK class_or_interface_type dim_exprs
2081 { $$ = build_newarray_node ($2, $3, 0); }
2082 | NEW_TK primitive_type dim_exprs dims
2083 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2084 | NEW_TK class_or_interface_type dim_exprs dims
2085 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2086 /* Added, JDK1.1 anonymous array. Initial documentation rule
2088 | NEW_TK class_or_interface_type dims array_initializer
2091 int osb = pop_current_osb (ctxp);
2093 obstack_1grow (&temporary_obstack, '[');
2094 sig = obstack_finish (&temporary_obstack);
2095 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2096 $2, get_identifier (sig), $4);
2098 | NEW_TK primitive_type dims array_initializer
2100 int osb = pop_current_osb (ctxp);
2103 type = build_java_array_type (type, -1);
2104 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2105 build_pointer_type (type), NULL_TREE, $4);
2107 | NEW_TK error CSB_TK
2108 {yyerror ("'[' expected"); DRECOVER ("]");}
2109 | NEW_TK error OSB_TK
2110 {yyerror ("']' expected"); RECOVER;}
2115 { $$ = build_tree_list (NULL_TREE, $1); }
2116 | dim_exprs dim_expr
2117 { $$ = tree_cons (NULL_TREE, $2, $$); }
2121 OSB_TK expression CSB_TK
2123 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2125 $2 = build_wfl_node ($2);
2126 TREE_TYPE ($2) = NULL_TREE;
2128 EXPR_WFL_LINECOL ($2) = $1.location;
2131 | OSB_TK expression error
2132 {yyerror ("']' expected"); RECOVER;}
2135 yyerror ("Missing term");
2136 yyerror ("']' expected");
2145 /* If not initialized, allocate memory for the osb
2147 if (!ctxp->osb_limit)
2149 allocate = ctxp->osb_limit = 32;
2150 ctxp->osb_depth = -1;
2152 /* If capacity overflown, reallocate a bigger chunk */
2153 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2154 allocate = ctxp->osb_limit << 1;
2158 allocate *= sizeof (int);
2159 if (ctxp->osb_number)
2160 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2163 ctxp->osb_number = (int *)xmalloc (allocate);
2166 CURRENT_OSB (ctxp) = 1;
2168 | dims OSB_TK CSB_TK
2169 { CURRENT_OSB (ctxp)++; }
2171 { yyerror ("']' expected"); RECOVER;}
2175 primary DOT_TK identifier
2176 { $$ = make_qualified_primary ($1, $3, $2.location); }
2177 /* FIXME - REWRITE TO:
2178 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2179 | SUPER_TK DOT_TK identifier
2181 tree super_wfl = build_wfl_node (super_identifier_node);
2182 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2183 $$ = make_qualified_name (super_wfl, $3, $2.location);
2186 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2191 { $$ = build_method_invocation ($1, NULL_TREE); }
2192 | name OP_TK argument_list CP_TK
2193 { $$ = build_method_invocation ($1, $3); }
2194 | primary DOT_TK identifier OP_TK CP_TK
2196 if (TREE_CODE ($1) == THIS_EXPR)
2197 $$ = build_this_super_qualified_invocation
2198 (1, $3, NULL_TREE, 0, $2.location);
2201 tree invok = build_method_invocation ($3, NULL_TREE);
2202 $$ = make_qualified_primary ($1, invok, $2.location);
2205 | primary DOT_TK identifier OP_TK argument_list CP_TK
2207 if (TREE_CODE ($1) == THIS_EXPR)
2208 $$ = build_this_super_qualified_invocation
2209 (1, $3, $5, 0, $2.location);
2212 tree invok = build_method_invocation ($3, $5);
2213 $$ = make_qualified_primary ($1, invok, $2.location);
2216 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2218 $$ = build_this_super_qualified_invocation
2219 (0, $3, NULL_TREE, $1.location, $2.location);
2221 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2223 $$ = build_this_super_qualified_invocation
2224 (0, $3, $5, $1.location, $2.location);
2226 /* Screws up thing. I let it here until I'm convinced it can
2228 | primary DOT_TK error
2229 {yyerror ("'(' expected"); DRECOVER(bad);} */
2230 | SUPER_TK DOT_TK error CP_TK
2231 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2232 | SUPER_TK DOT_TK error DOT_TK
2233 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2237 name OSB_TK expression CSB_TK
2238 { $$ = build_array_ref ($2.location, $1, $3); }
2239 | primary_no_new_array OSB_TK expression CSB_TK
2240 { $$ = build_array_ref ($2.location, $1, $3); }
2243 yyerror ("Missing term and ']' expected");
2244 DRECOVER(array_access);
2246 | name OSB_TK expression error
2248 yyerror ("']' expected");
2249 DRECOVER(array_access);
2251 | primary_no_new_array OSB_TK error
2253 yyerror ("Missing term and ']' expected");
2254 DRECOVER(array_access);
2256 | primary_no_new_array OSB_TK expression error
2258 yyerror ("']' expected");
2259 DRECOVER(array_access);
2266 | post_increment_expression
2267 | post_decrement_expression
2270 post_increment_expression:
2271 postfix_expression INCR_TK
2272 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2275 post_decrement_expression:
2276 postfix_expression DECR_TK
2277 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2281 pre_increment_expression
2282 | pre_decrement_expression
2283 | PLUS_TK unary_expression
2284 {$$ = build_unaryop ($1.token, $1.location, $2); }
2285 | MINUS_TK unary_expression
2286 {$$ = build_unaryop ($1.token, $1.location, $2); }
2287 | unary_expression_not_plus_minus
2289 {yyerror ("Missing term"); RECOVER}
2291 {yyerror ("Missing term"); RECOVER}
2294 pre_increment_expression:
2295 INCR_TK unary_expression
2296 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2298 {yyerror ("Missing term"); RECOVER}
2301 pre_decrement_expression:
2302 DECR_TK unary_expression
2303 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2305 {yyerror ("Missing term"); RECOVER}
2308 unary_expression_not_plus_minus:
2310 | NOT_TK unary_expression
2311 {$$ = build_unaryop ($1.token, $1.location, $2); }
2312 | NEG_TK unary_expression
2313 {$$ = build_unaryop ($1.token, $1.location, $2); }
2316 {yyerror ("Missing term"); RECOVER}
2318 {yyerror ("Missing term"); RECOVER}
2321 cast_expression: /* Error handling here is potentially weak */
2322 OP_TK primitive_type dims CP_TK unary_expression
2325 int osb = pop_current_osb (ctxp);
2327 type = build_java_array_type (type, -1);
2328 $$ = build_cast ($1.location, type, $5);
2330 | OP_TK primitive_type CP_TK unary_expression
2331 { $$ = build_cast ($1.location, $2, $4); }
2332 | OP_TK expression CP_TK unary_expression_not_plus_minus
2333 { $$ = build_cast ($1.location, $2, $4); }
2334 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2337 int osb = pop_current_osb (ctxp);
2339 obstack_1grow (&temporary_obstack, '[');
2340 obstack_grow0 (&temporary_obstack,
2341 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2342 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2343 ptr = obstack_finish (&temporary_obstack);
2344 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2345 $$ = build_cast ($1.location, $2, $5);
2347 | OP_TK primitive_type OSB_TK error
2348 {yyerror ("']' expected, invalid type expression");}
2351 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2354 | OP_TK primitive_type dims CP_TK error
2355 {yyerror ("Missing term"); RECOVER;}
2356 | OP_TK primitive_type CP_TK error
2357 {yyerror ("Missing term"); RECOVER;}
2358 | OP_TK name dims CP_TK error
2359 {yyerror ("Missing term"); RECOVER;}
2362 multiplicative_expression:
2364 | multiplicative_expression MULT_TK unary_expression
2366 $$ = build_binop (BINOP_LOOKUP ($2.token),
2367 $2.location, $1, $3);
2369 | multiplicative_expression DIV_TK unary_expression
2371 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2374 | multiplicative_expression REM_TK unary_expression
2376 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2379 | multiplicative_expression MULT_TK error
2380 {yyerror ("Missing term"); RECOVER;}
2381 | multiplicative_expression DIV_TK error
2382 {yyerror ("Missing term"); RECOVER;}
2383 | multiplicative_expression REM_TK error
2384 {yyerror ("Missing term"); RECOVER;}
2387 additive_expression:
2388 multiplicative_expression
2389 | additive_expression PLUS_TK multiplicative_expression
2391 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2394 | additive_expression MINUS_TK multiplicative_expression
2396 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2399 | additive_expression PLUS_TK error
2400 {yyerror ("Missing term"); RECOVER;}
2401 | additive_expression MINUS_TK error
2402 {yyerror ("Missing term"); RECOVER;}
2407 | shift_expression LS_TK additive_expression
2409 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2412 | shift_expression SRS_TK additive_expression
2414 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2417 | shift_expression ZRS_TK additive_expression
2419 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2422 | shift_expression LS_TK error
2423 {yyerror ("Missing term"); RECOVER;}
2424 | shift_expression SRS_TK error
2425 {yyerror ("Missing term"); RECOVER;}
2426 | shift_expression ZRS_TK error
2427 {yyerror ("Missing term"); RECOVER;}
2430 relational_expression:
2432 | relational_expression LT_TK shift_expression
2434 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2437 | relational_expression GT_TK shift_expression
2439 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2442 | relational_expression LTE_TK shift_expression
2444 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2447 | relational_expression GTE_TK shift_expression
2449 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2452 | relational_expression INSTANCEOF_TK reference_type
2453 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2454 | relational_expression LT_TK error
2455 {yyerror ("Missing term"); RECOVER;}
2456 | relational_expression GT_TK error
2457 {yyerror ("Missing term"); RECOVER;}
2458 | relational_expression LTE_TK error
2459 {yyerror ("Missing term"); RECOVER;}
2460 | relational_expression GTE_TK error
2461 {yyerror ("Missing term"); RECOVER;}
2462 | relational_expression INSTANCEOF_TK error
2463 {yyerror ("Invalid reference type"); RECOVER;}
2466 equality_expression:
2467 relational_expression
2468 | equality_expression EQ_TK relational_expression
2470 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2473 | equality_expression NEQ_TK relational_expression
2475 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2478 | equality_expression EQ_TK error
2479 {yyerror ("Missing term"); RECOVER;}
2480 | equality_expression NEQ_TK error
2481 {yyerror ("Missing term"); RECOVER;}
2486 | and_expression AND_TK equality_expression
2488 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2491 | and_expression AND_TK error
2492 {yyerror ("Missing term"); RECOVER;}
2495 exclusive_or_expression:
2497 | exclusive_or_expression XOR_TK and_expression
2499 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2502 | exclusive_or_expression XOR_TK error
2503 {yyerror ("Missing term"); RECOVER;}
2506 inclusive_or_expression:
2507 exclusive_or_expression
2508 | inclusive_or_expression OR_TK exclusive_or_expression
2510 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2513 | inclusive_or_expression OR_TK error
2514 {yyerror ("Missing term"); RECOVER;}
2517 conditional_and_expression:
2518 inclusive_or_expression
2519 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2521 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2524 | conditional_and_expression BOOL_AND_TK error
2525 {yyerror ("Missing term"); RECOVER;}
2528 conditional_or_expression:
2529 conditional_and_expression
2530 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2532 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2535 | conditional_or_expression BOOL_OR_TK error
2536 {yyerror ("Missing term"); RECOVER;}
2539 conditional_expression: /* Error handling here is weak */
2540 conditional_or_expression
2541 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2543 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2544 EXPR_WFL_LINECOL ($$) = $2.location;
2546 | conditional_or_expression REL_QM_TK REL_CL_TK error
2549 yyerror ("Missing term");
2552 | conditional_or_expression REL_QM_TK error
2553 {yyerror ("Missing term"); DRECOVER (2);}
2554 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2555 {yyerror ("Missing term"); DRECOVER (3);}
2558 assignment_expression:
2559 conditional_expression
2564 left_hand_side assignment_operator assignment_expression
2565 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2566 | left_hand_side assignment_operator error
2568 YYNOT_TWICE yyerror ("Missing term");
2579 assignment_operator:
2585 assignment_expression
2588 constant_expression:
2594 /* Helper function to retrieve an OSB count. Should be used when the
2595 `dims:' rule is being used. */
2598 pop_current_osb (ctxp)
2599 struct parser_ctxt *ctxp;
2603 if (ctxp->osb_depth < 0)
2606 to_return = CURRENT_OSB (ctxp);
2614 /* This section of the code deal with save/restoring parser contexts.
2615 Add mode documentation here. FIXME */
2617 /* Helper function. Create a new parser context. With
2618 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2619 context is copied, otherwise, the new context is zeroed. The newly
2620 created context becomes the current one. */
2623 create_new_parser_context (copy_from_previous)
2624 int copy_from_previous;
2626 struct parser_ctxt *new;
2628 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2629 if (copy_from_previous)
2631 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2632 new->saved_data_ctx = 1;
2635 memset ((PTR) new, 0, sizeof (struct parser_ctxt));
2641 /* Create a new parser context and make it the current one. */
2644 java_push_parser_context ()
2646 create_new_parser_context (0);
2650 java_pop_parser_context (generate)
2654 struct parser_ctxt *toFree, *next;
2663 lineno = ctxp->lineno;
2664 current_class = ctxp->class_type;
2667 /* If the old and new lexers differ, then free the old one. */
2668 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2669 java_destroy_lexer (ctxp->lexer);
2671 /* Set the single import class file flag to 0 for the current list
2672 of imported things */
2673 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2674 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 0;
2676 /* And restore those of the previous context */
2677 if ((ctxp = next)) /* Assignment is really meant here */
2678 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2679 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_VALUE (current)) = 1;
2681 /* If we pushed a context to parse a class intended to be generated,
2682 we keep it so we can remember the class. What we could actually
2683 do is to just update a list of class names. */
2686 toFree->next = ctxp_for_generation;
2687 ctxp_for_generation = toFree;
2693 /* Create a parser context for the use of saving some global
2697 java_parser_context_save_global ()
2701 java_push_parser_context ();
2702 ctxp->saved_data_ctx = 1;
2705 /* If this context already stores data, create a new one suitable
2706 for data storage. */
2707 else if (ctxp->saved_data)
2708 create_new_parser_context (1);
2710 ctxp->lineno = lineno;
2711 ctxp->class_type = current_class;
2712 ctxp->filename = input_filename;
2713 ctxp->function_decl = current_function_decl;
2714 ctxp->saved_data = 1;
2717 /* Restore some global variables from the previous context. Make the
2718 previous context the current one. */
2721 java_parser_context_restore_global ()
2723 lineno = ctxp->lineno;
2724 current_class = ctxp->class_type;
2725 input_filename = ctxp->filename;
2729 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2730 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2732 current_function_decl = ctxp->function_decl;
2733 ctxp->saved_data = 0;
2734 if (ctxp->saved_data_ctx)
2735 java_pop_parser_context (0);
2738 /* Suspend vital data for the current class/function being parsed so
2739 that an other class can be parsed. Used to let local/anonymous
2740 classes be parsed. */
2743 java_parser_context_suspend ()
2745 /* This makes debugging through java_debug_context easier */
2746 static const char *name = "<inner buffer context>";
2748 /* Duplicate the previous context, use it to save the globals we're
2750 create_new_parser_context (1);
2751 ctxp->function_decl = current_function_decl;
2752 ctxp->class_type = current_class;
2754 /* Then create a new context which inherits all data from the
2755 previous one. This will be the new current context */
2756 create_new_parser_context (1);
2758 /* Help debugging */
2759 ctxp->next->filename = name;
2762 /* Resume vital data for the current class/function being parsed so
2763 that an other class can be parsed. Used to let local/anonymous
2764 classes be parsed. The trick is the data storing file position
2765 informations must be restored to their current value, so parsing
2766 can resume as if no context was ever saved. */
2769 java_parser_context_resume ()
2771 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2772 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2773 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2775 /* We need to inherit the list of classes to complete/generate */
2776 restored->classd_list = old->classd_list;
2777 restored->class_list = old->class_list;
2779 /* Restore the current class and function from the saver */
2780 current_class = saver->class_type;
2781 current_function_decl = saver->function_decl;
2783 /* Retrive the restored context */
2786 /* Re-installed the data for the parsing to carry on */
2787 memcpy (&ctxp->marker_begining, &old->marker_begining,
2788 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2790 /* Buffer context can now be discarded */
2795 /* Add a new anchor node to which all statement(s) initializing static
2796 and non static initialized upon declaration field(s) will be
2800 java_parser_context_push_initialized_field ()
2804 node = build_tree_list (NULL_TREE, NULL_TREE);
2805 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2806 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2808 node = build_tree_list (NULL_TREE, NULL_TREE);
2809 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2810 CPC_INITIALIZER_LIST (ctxp) = node;
2812 node = build_tree_list (NULL_TREE, NULL_TREE);
2813 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2814 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2817 /* Pop the lists of initialized field. If this lists aren't empty,
2818 remember them so we can use it to create and populate the finit$
2819 or <clinit> functions. */
2822 java_parser_context_pop_initialized_field ()
2825 tree class_type = TREE_TYPE (GET_CPC ());
2827 if (CPC_INITIALIZER_LIST (ctxp))
2829 stmts = CPC_INITIALIZER_STMT (ctxp);
2830 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2831 if (stmts && !java_error_count)
2832 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2835 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2837 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2838 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2839 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2840 /* Keep initialization in order to enforce 8.5 */
2841 if (stmts && !java_error_count)
2842 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2845 /* JDK 1.1 instance initializers */
2846 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2848 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2849 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2850 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2851 if (stmts && !java_error_count)
2852 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2857 reorder_static_initialized (list)
2860 /* We have to keep things in order. The alias initializer have to
2861 come first, then the initialized regular field, in reverse to
2862 keep them in lexical order. */
2863 tree marker, previous = NULL_TREE;
2864 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2865 if (TREE_CODE (marker) == TREE_LIST
2866 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2869 /* No static initialized, the list is fine as is */
2871 list = TREE_CHAIN (marker);
2873 /* No marker? reverse the whole list */
2875 list = nreverse (list);
2877 /* Otherwise, reverse what's after the marker and the new reordered
2878 sublist will replace the marker. */
2881 TREE_CHAIN (previous) = NULL_TREE;
2882 list = nreverse (list);
2883 list = chainon (TREE_CHAIN (marker), list);
2888 /* Helper functions to dump the parser context stack. */
2890 #define TAB_CONTEXT(C) \
2891 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2894 java_debug_context_do (tab)
2897 struct parser_ctxt *copy = ctxp;
2901 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2903 fprintf (stderr, "filename: %s\n", copy->filename);
2905 fprintf (stderr, "lineno: %d\n", copy->lineno);
2907 fprintf (stderr, "package: %s\n",
2909 IDENTIFIER_POINTER (copy->package) : "<none>"));
2911 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2913 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2919 /* Dump the stacked up parser contexts. Intended to be called from a
2923 java_debug_context ()
2925 java_debug_context_do (0);
2930 /* Flag for the error report routine to issue the error the first time
2931 it's called (overriding the default behavior which is to drop the
2932 first invocation and honor the second one, taking advantage of a
2934 static int force_error = 0;
2936 /* Reporting an constructor invocation error. */
2938 parse_ctor_invocation_error ()
2940 if (DECL_CONSTRUCTOR_P (current_function_decl))
2941 yyerror ("Constructor invocation must be first thing in a constructor");
2943 yyerror ("Only constructors can invoke constructors");
2946 /* Reporting JDK1.1 features not implemented. */
2949 parse_jdk1_1_error (msg)
2952 sorry (": `%s' JDK1.1(TM) feature", msg);
2954 return empty_stmt_node;
2957 static int do_warning = 0;
2964 static int prev_lineno;
2965 static const char *prev_msg;
2968 char *remainder, *code_from_source;
2970 if (!force_error && prev_lineno == lineno)
2973 /* Save current error location but report latter, when the context is
2975 if (ctxp->java_error_flag == 0)
2977 ctxp->java_error_flag = 1;
2979 /* Do something to use the previous line if we're reaching the
2980 end of the file... */
2981 #ifdef VERBOSE_SKELETON
2982 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2987 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2988 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2991 ctxp->java_error_flag = 0;
2993 java_warning_count++;
2997 if (elc.col == 0 && msg && msg[1] == ';')
2999 elc.col = ctxp->p_line->char_col-1;
3000 elc.line = ctxp->p_line->lineno;
3003 save_lineno = lineno;
3004 prev_lineno = lineno = elc.line;
3007 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3008 obstack_grow0 (&temporary_obstack,
3009 code_from_source, strlen (code_from_source));
3010 remainder = obstack_finish (&temporary_obstack);
3012 warning ("%s.\n%s", msg, remainder);
3014 error ("%s.\n%s", msg, remainder);
3016 /* This allow us to cheaply avoid an extra 'Invalid expression
3017 statement' error report when errors have been already reported on
3018 the same line. This occurs when we report an error but don't have
3019 a synchronization point other than ';', which
3020 expression_statement is the only one to take care of. */
3021 ctxp->prevent_ese = lineno = save_lineno;
3025 issue_warning_error_from_context (cl, msg, ap)
3030 const char *saved, *saved_input_filename;
3032 vsprintf (buffer, msg, ap);
3035 ctxp->elc.line = EXPR_WFL_LINENO (cl);
3036 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3037 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3039 /* We have a CL, that's a good reason for using it if it contains data */
3040 saved = ctxp->filename;
3041 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3042 ctxp->filename = EXPR_WFL_FILENAME (cl);
3043 saved_input_filename = input_filename;
3044 input_filename = ctxp->filename;
3046 java_error (buffer);
3047 ctxp->filename = saved;
3048 input_filename = saved_input_filename;
3052 /* Issue an error message at a current source line CL */
3055 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3057 #ifndef ANSI_PROTOTYPES
3064 #ifndef ANSI_PROTOTYPES
3065 cl = va_arg (ap, tree);
3066 msg = va_arg (ap, const char *);
3068 issue_warning_error_from_context (cl, msg, ap);
3072 /* Issue a warning at a current source line CL */
3075 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3077 #ifndef ANSI_PROTOTYPES
3084 #ifndef ANSI_PROTOTYPES
3085 cl = va_arg (ap, tree);
3086 msg = va_arg (ap, const char *);
3089 force_error = do_warning = 1;
3090 issue_warning_error_from_context (cl, msg, ap);
3091 do_warning = force_error = 0;
3096 find_expr_with_wfl (node)
3104 switch (TREE_CODE (node))
3107 node = BLOCK_EXPR_BODY (node);
3111 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3114 node = TREE_OPERAND (node, 1);
3118 node = TREE_OPERAND (node, 0);
3121 case LABELED_BLOCK_EXPR:
3122 node = TREE_OPERAND (node, 1);
3126 code = TREE_CODE_CLASS (TREE_CODE (node));
3127 if (((code == '1') || (code == '2') || (code == 'e'))
3128 && EXPR_WFL_LINECOL (node))
3136 /* Issue a missing return statement error. Uses METHOD to figure the
3137 last line of the method the error occurs in. */
3140 missing_return_error (method)
3143 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3144 parse_error_context (wfl_operator, "Missing return statement");
3147 /* Issue an unreachable statement error. From NODE, find the next
3148 statement to report appropriately. */
3150 unreachable_stmt_error (node)
3153 /* Browse node to find the next expression node that has a WFL. Use
3154 the location to report the error */
3155 if (TREE_CODE (node) == COMPOUND_EXPR)
3156 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3158 node = find_expr_with_wfl (node);
3162 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3163 parse_error_context (wfl_operator, "Unreachable statement");
3170 java_report_errors ()
3172 if (java_error_count)
3173 fprintf (stderr, "%d error%s",
3174 java_error_count, (java_error_count == 1 ? "" : "s"));
3175 if (java_warning_count)
3176 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3177 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3178 if (java_error_count || java_warning_count)
3179 putc ('\n', stderr);
3180 return java_error_count;
3184 java_accstring_lookup (flags)
3187 static char buffer [80];
3188 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3190 /* Access modifier looked-up first for easier report on forbidden
3192 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3193 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3194 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3195 if (flags & ACC_STATIC) COPY_RETURN ("static");
3196 if (flags & ACC_FINAL) COPY_RETURN ("final");
3197 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3198 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3199 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3200 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3201 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3202 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3209 /* Issuing error messages upon redefinition of classes, interfaces or
3213 classitf_redefinition_error (context, id, decl, cl)
3214 const char *context;
3217 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3218 context, IDENTIFIER_POINTER (id),
3219 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3220 /* Here we should point out where its redefined. It's a unicode. FIXME */
3224 variable_redefinition_error (context, name, type, line)
3225 tree context, name, type;
3228 const char *type_name;
3230 /* Figure a proper name for type. We might haven't resolved it */
3231 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3232 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3234 type_name = lang_printable_name (type, 0);
3236 parse_error_context (context,
3237 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3238 IDENTIFIER_POINTER (name),
3239 type_name, IDENTIFIER_POINTER (name), line);
3243 build_array_from_name (type, type_wfl, name, ret_name)
3244 tree type, type_wfl, name, *ret_name;
3249 /* Eventually get more dims */
3250 string = IDENTIFIER_POINTER (name);
3251 while (string [more_dims] == '[')
3254 /* If we have, then craft a new type for this variable */
3259 name = get_identifier (&string [more_dims]);
3261 /* If we have a pointer, use its type */
3262 if (TREE_CODE (type) == POINTER_TYPE)
3263 type = TREE_TYPE (type);
3265 /* Building the first dimension of a primitive type uses this
3267 if (JPRIMITIVE_TYPE_P (type))
3269 type = build_java_array_type (type, -1);
3272 /* Otherwise, if we have a WFL for this type, use it (the type
3273 is already an array on an unresolved type, and we just keep
3274 on adding dimensions) */
3279 string = IDENTIFIER_POINTER (TYPE_NAME (save));
3280 while (string[i] == '[')
3285 /* Add all the dimensions */
3287 type = build_unresolved_array_type (type);
3289 /* The type may have been incomplete in the first place */
3291 type = obtain_incomplete_type (type);
3299 /* Build something that the type identifier resolver will identify as
3300 being an array to an unresolved type. TYPE_WFL is a WFL on a
3304 build_unresolved_array_type (type_or_wfl)
3310 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3311 just create a array type */
3312 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3313 return build_java_array_type (type_or_wfl, -1);
3315 obstack_1grow (&temporary_obstack, '[');
3316 obstack_grow0 (&temporary_obstack,
3317 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3318 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3319 ptr = obstack_finish (&temporary_obstack);
3320 wfl = build_expr_wfl (get_identifier (ptr),
3321 EXPR_WFL_FILENAME (type_or_wfl),
3322 EXPR_WFL_LINENO (type_or_wfl),
3323 EXPR_WFL_COLNO (type_or_wfl));
3324 /* Re-install the existing qualifications so that the type can be
3325 resolved properly. */
3326 EXPR_WFL_QUALIFICATION (wfl) = EXPR_WFL_QUALIFICATION (type_or_wfl);
3331 parser_add_interface (class_decl, interface_decl, wfl)
3332 tree class_decl, interface_decl, wfl;
3334 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3335 parse_error_context (wfl, "Interface `%s' repeated",
3336 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3339 /* Bulk of common class/interface checks. Return 1 if an error was
3340 encountered. TAG is 0 for a class, 1 for an interface. */
3343 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3344 int is_interface, flags;
3345 tree raw_name, qualified_name, decl, cl;
3348 int sca = 0; /* Static class allowed */
3349 int icaf = 0; /* Inner class allowed flags */
3350 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3353 fprintf (stderr, " %s%s %s",
3354 (CPC_INNER_P () ? "inner" : ""),
3355 (is_interface ? "interface" : "class"),
3356 IDENTIFIER_POINTER (qualified_name));
3358 /* Scope of an interface/class type name:
3359 - Can't be imported by a single type import
3360 - Can't already exists in the package */
3361 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3362 && (node = find_name_in_single_imports (raw_name))
3366 (cl, "%s name `%s' clashes with imported type `%s'",
3367 (is_interface ? "Interface" : "Class"),
3368 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3371 if (decl && CLASS_COMPLETE_P (decl))
3373 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3374 qualified_name, decl, cl);
3378 if (check_inner_class_redefinition (raw_name, cl))
3381 /* If public, file name should match class/interface name, except
3382 when dealing with an inner class */
3383 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3387 /* Contains OS dependent assumption on path separator. FIXME */
3388 for (f = &input_filename [strlen (input_filename)];
3389 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3392 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
3394 if (strncmp (IDENTIFIER_POINTER (raw_name),
3395 f , IDENTIFIER_LENGTH (raw_name)) ||
3396 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3398 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3399 (is_interface ? "interface" : "class"),
3400 IDENTIFIER_POINTER (qualified_name),
3401 IDENTIFIER_POINTER (raw_name));
3404 /* Static classes can be declared only in top level classes. Note:
3405 once static, a inner class is a top level class. */
3406 if (flags & ACC_STATIC)
3408 /* Catch the specific error of declaring an class inner class
3409 with no toplevel enclosing class. Prevent check_modifiers from
3410 complaining a second time */
3411 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3413 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3414 IDENTIFIER_POINTER (qualified_name));
3417 /* Else, in the context of a top-level class declaration, let
3418 `check_modifiers' do its job, otherwise, give it a go */
3420 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3423 /* Inner classes can be declared private or protected
3424 within their enclosing classes. */
3427 /* A class which is local to a block can't be public, private,
3428 protected or static. But it is created final, so allow this
3430 if (current_function_decl)
3431 icaf = sca = uaaf = ACC_FINAL;
3434 check_modifiers_consistency (flags);
3435 icaf = ACC_PRIVATE|ACC_PROTECTED;
3442 uaaf = INTERFACE_INNER_MODIFIERS;
3444 uaaf = INTERFACE_MODIFIERS;
3446 check_modifiers ("Illegal modifier `%s' for interface declaration",
3450 check_modifiers ((current_function_decl ?
3451 "Illegal modifier `%s' for local class declaration" :
3452 "Illegal modifier `%s' for class declaration"),
3453 flags, uaaf|sca|icaf);
3458 make_nested_class_name (cpc_list)
3466 make_nested_class_name (TREE_CHAIN (cpc_list));
3468 /* Pick the qualified name when dealing with the first upmost
3470 name = (TREE_CHAIN (cpc_list) ?
3471 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3472 obstack_grow (&temporary_obstack,
3473 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3474 /* Why is NO_DOLLAR_IN_LABEL defined? */
3476 #ifdef NO_DOLLAR_IN_LABEL
3477 internal_error ("Can't use '$' as a separator for inner classes");
3480 obstack_1grow (&temporary_obstack, '$');
3483 /* Can't redefine a class already defined in an earlier scope. */
3486 check_inner_class_redefinition (raw_name, cl)
3491 for (scope_list = GET_CPC_LIST (); scope_list;
3492 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3493 if (raw_name == GET_CPC_UN_NODE (scope_list))
3496 (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",
3497 IDENTIFIER_POINTER (raw_name));
3504 find_as_inner_class (enclosing, name, cl)
3505 tree enclosing, name, cl;
3507 tree qual, to_return;
3511 name = TYPE_NAME (name);
3513 /* First search: within the scope of `enclosing', search for name */
3514 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3515 qual = EXPR_WFL_QUALIFICATION (cl);
3517 qual = build_tree_list (cl, NULL_TREE);
3519 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3521 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3524 /* We're dealing with a qualified name. Try to resolve thing until
3525 we get something that is an enclosing class. */
3526 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3528 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3530 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3531 qual = TREE_CHAIN (qual))
3533 acc = merge_qualified_name (acc,
3534 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3535 BUILD_PTR_FROM_NAME (ptr, acc);
3537 /* Don't try to resolve ACC as a class name if it follows
3538 the current package name. We don't want to pick something
3539 that's accidentally there: for example `a.b.c' in package
3540 `a.b' shouldn't trigger loading `a' if it's there by
3543 && strstr (IDENTIFIER_POINTER (ctxp->package),
3544 IDENTIFIER_POINTER (acc)))
3547 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3550 /* A NULL qual and a decl means that the search ended
3551 successfully?!? We have to do something then. FIXME */
3556 qual = EXPR_WFL_QUALIFICATION (cl);
3558 /* Otherwise, create a qual for the other part of the resolution. */
3560 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3562 return find_as_inner_class_do (qual, enclosing);
3565 /* We go inside the list of sub classes and try to find a way
3569 find_as_inner_class_do (qual, enclosing)
3570 tree qual, enclosing;
3575 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3577 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3578 tree next_enclosing = NULL_TREE;
3581 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3582 inner_list; inner_list = TREE_CHAIN (inner_list))
3584 if (TREE_VALUE (inner_list) == name_to_match)
3586 next_enclosing = TREE_PURPOSE (inner_list);
3590 enclosing = next_enclosing;
3593 return (!qual && enclosing ? enclosing : NULL_TREE);
3596 /* Reach all inner classes and tie their unqualified name to a
3600 set_nested_class_simple_name_value (outer, set)
3606 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3607 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3608 TREE_PURPOSE (l) : NULL_TREE);
3612 link_nested_class_to_enclosing ()
3614 if (GET_ENCLOSING_CPC ())
3616 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3617 DECL_INNER_CLASS_LIST (enclosing) =
3618 tree_cons (GET_CPC (), GET_CPC_UN (),
3619 DECL_INNER_CLASS_LIST (enclosing));
3620 enclosing = enclosing;
3625 maybe_make_nested_class_name (name)
3628 tree id = NULL_TREE;
3632 make_nested_class_name (GET_CPC_LIST ());
3633 obstack_grow0 (&temporary_obstack,
3634 IDENTIFIER_POINTER (name),
3635 IDENTIFIER_LENGTH (name));
3636 id = get_identifier (obstack_finish (&temporary_obstack));
3638 QUALIFIED_P (id) = 1;
3643 /* If DECL is NULL, create and push a new DECL, record the current
3644 line CL and do other maintenance things. */
3647 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3648 tree decl, raw_name, qualified_name, cl;
3651 decl = push_class (make_class (), qualified_name);
3653 /* Take care of the file and line business */
3654 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3655 /* If we're emiting xrefs, store the line/col number information */
3657 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3659 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3660 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3661 CLASS_PARSED_P (TREE_TYPE (decl)) = 1;
3662 CLASS_FROM_CURRENTLY_COMPILED_P (TREE_TYPE (decl)) =
3663 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3665 PUSH_CPC (decl, raw_name);
3666 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3668 /* Link the declaration to the already seen ones */
3669 TREE_CHAIN (decl) = ctxp->class_list;
3670 ctxp->class_list = decl;
3672 /* Create a new nodes in the global lists */
3673 gclass_list = tree_cons (NULL_TREE, decl, gclass_list);
3674 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3676 /* Install a new dependency list element */
3677 create_jdep_list (ctxp);
3679 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3680 IDENTIFIER_POINTER (qualified_name)));
3685 add_superinterfaces (decl, interface_list)
3686 tree decl, interface_list;
3689 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3690 takes care of ensuring that:
3691 - This is an accessible interface type,
3692 - Circularity detection.
3693 parser_add_interface is then called. If present but not defined,
3694 the check operation is delayed until the super interface gets
3696 for (node = interface_list; node; node = TREE_CHAIN (node))
3698 tree current = TREE_PURPOSE (node);
3699 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3700 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3702 if (!parser_check_super_interface (idecl, decl, current))
3703 parser_add_interface (decl, idecl, current);
3706 register_incomplete_type (JDEP_INTERFACE,
3707 current, decl, NULL_TREE);
3711 /* Create an interface in pass1 and return its decl. Return the
3712 interface's decl in pass 2. */
3715 create_interface (flags, id, super)
3719 tree raw_name = EXPR_WFL_NODE (id);
3720 tree q_name = parser_qualified_classname (raw_name);
3721 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3723 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3725 /* Basic checks: scope, redefinition, modifiers */
3726 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3732 /* Suspend the current parsing context if we're parsing an inner
3735 java_parser_context_suspend ();
3737 /* Push a new context for (static) initialized upon declaration fields */
3738 java_parser_context_push_initialized_field ();
3740 /* Interface modifiers check
3741 - public/abstract allowed (already done at that point)
3742 - abstract is obsolete (comes first, it's a warning, or should be)
3743 - Can't use twice the same (checked in the modifier rule) */
3744 if ((flags & ACC_ABSTRACT) && flag_redundant)
3745 parse_warning_context
3746 (MODIFIER_WFL (ABSTRACT_TK),
3747 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
3749 /* Create a new decl if DECL is NULL, otherwise fix it */
3750 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3752 /* Set super info and mark the class a complete */
3753 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3754 object_type_node, ctxp->interface_number);
3755 ctxp->interface_number = 0;
3756 CLASS_COMPLETE_P (decl) = 1;
3757 add_superinterfaces (decl, super);
3762 /* Anonymous class counter. Will be reset to 1 every time a non
3763 anonymous class gets created. */
3764 static int anonymous_class_counter = 1;
3766 /* Patch anonymous class CLASS, by either extending or implementing
3770 patch_anonymous_class (type_decl, class_decl, wfl)
3771 tree type_decl, class_decl, wfl;
3773 tree class = TREE_TYPE (class_decl);
3774 tree type = TREE_TYPE (type_decl);
3775 tree binfo = TYPE_BINFO (class);
3777 /* If it's an interface, implement it */
3778 if (CLASS_INTERFACE (type_decl))
3783 if (parser_check_super_interface (type_decl, class_decl, wfl))
3786 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3787 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3788 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3789 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3790 /* And add the interface */
3791 parser_add_interface (class_decl, type_decl, wfl);
3793 /* Otherwise, it's a type we want to extend */
3796 if (parser_check_super (type_decl, class_decl, wfl))
3798 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3803 create_anonymous_class (location, type_name)
3808 tree super = NULL_TREE, itf = NULL_TREE;
3809 tree id, type_decl, class;
3811 /* The unqualified name of the anonymous class. It's just a number. */
3812 sprintf (buffer, "%d", anonymous_class_counter++);
3813 id = build_wfl_node (get_identifier (buffer));
3814 EXPR_WFL_LINECOL (id) = location;
3816 /* We know about the type to extend/implement. We go ahead */
3817 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3819 /* Create a class which either implements on extends the designated
3820 class. The class bears an innacessible name. */
3821 if (CLASS_INTERFACE (type_decl))
3823 /* It's OK to modify it here. It's been already used and
3824 shouldn't be reused */
3825 ctxp->interface_number = 1;
3826 /* Interfaces should presented as a list of WFLs */
3827 itf = build_tree_list (type_name, NULL_TREE);
3833 class = create_class (ACC_FINAL, id, super, itf);
3835 /* We didn't know anything about the stuff. We register a dependence. */
3837 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3839 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3843 /* Create a class in pass1 and return its decl. Return class
3844 interface's decl in pass 2. */
3847 create_class (flags, id, super, interfaces)
3849 tree id, super, interfaces;
3851 tree raw_name = EXPR_WFL_NODE (id);
3852 tree class_id, decl;
3853 tree super_decl_type;
3855 class_id = parser_qualified_classname (raw_name);
3856 decl = IDENTIFIER_CLASS_VALUE (class_id);
3857 EXPR_WFL_NODE (id) = class_id;
3859 /* Basic check: scope, redefinition, modifiers */
3860 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3866 /* Suspend the current parsing context if we're parsing an inner
3867 class or an anonymous class. */
3869 java_parser_context_suspend ();
3870 /* Push a new context for (static) initialized upon declaration fields */
3871 java_parser_context_push_initialized_field ();
3873 /* Class modifier check:
3874 - Allowed modifier (already done at that point)
3875 - abstract AND final forbidden
3876 - Public classes defined in the correct file */
3877 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3879 (id, "Class `%s' can't be declared both abstract and final",
3880 IDENTIFIER_POINTER (raw_name));
3882 /* Create a new decl if DECL is NULL, otherwise fix it */
3883 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
3885 /* If SUPER exists, use it, otherwise use Object */
3888 /* Can't extend java.lang.Object */
3889 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3891 parse_error_context (id, "Can't extend `java.lang.Object'");
3896 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
3898 else if (TREE_TYPE (decl) != object_type_node)
3899 super_decl_type = object_type_node;
3900 /* We're defining java.lang.Object */
3902 super_decl_type = NULL_TREE;
3904 /* A class nested in an interface is implicitly static. */
3905 if (INNER_CLASS_DECL_P (decl)
3906 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
3908 flags |= ACC_STATIC;
3911 /* Set super info and mark the class as complete. */
3912 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3913 ctxp->interface_number);
3914 ctxp->interface_number = 0;
3915 CLASS_COMPLETE_P (decl) = 1;
3916 add_superinterfaces (decl, interfaces);
3918 /* Add the private this$<n> field, Replicate final locals still in
3919 scope as private final fields mangled like val$<local_name>.
3920 This doesn't not occur for top level (static) inner classes. */
3921 if (PURE_INNER_CLASS_DECL_P (decl))
3922 add_inner_class_fields (decl, current_function_decl);
3924 /* If doing xref, store the location at which the inherited class
3925 (if any) was seen. */
3926 if (flag_emit_xref && super)
3927 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3929 /* Eventually sets the @deprecated tag flag */
3930 CHECK_DEPRECATED (decl);
3932 /* Reset the anonymous class counter when declaring non inner classes */
3933 if (!INNER_CLASS_DECL_P (decl))
3934 anonymous_class_counter = 1;
3939 /* End a class declaration: register the statements used to create
3940 finit$ and <clinit>, pop the current class and resume the prior
3941 parser context if necessary. */
3944 end_class_declaration (resume)
3947 /* If an error occured, context weren't pushed and won't need to be
3948 popped by a resume. */
3949 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3951 java_parser_context_pop_initialized_field ();
3953 if (resume && no_error_occured)
3954 java_parser_context_resume ();
3956 /* We're ending a class declaration, this is a good time to reset
3957 the interface cout. Note that might have been already done in
3958 create_interface, but if at that time an inner class was being
3959 dealt with, the interface count was reset in a context created
3960 for the sake of handling inner classes declaration. */
3961 ctxp->interface_number = 0;
3965 add_inner_class_fields (class_decl, fct_decl)
3969 tree block, marker, f;
3971 f = add_field (TREE_TYPE (class_decl),
3972 build_current_thisn (TREE_TYPE (class_decl)),
3973 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3975 FIELD_THISN (f) = 1;
3980 for (block = GET_CURRENT_BLOCK (fct_decl);
3981 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3984 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3987 tree wfl, init, list;
3989 /* Avoid non final arguments. */
3990 if (!LOCAL_FINAL_P (decl))
3993 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3994 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3995 wfl = build_wfl_node (name);
3996 init = build_wfl_node (pname);
3997 /* Build an initialization for the field: it will be
3998 initialized by a parameter added to finit$, bearing a
3999 mangled name of the field itself (param$<n>.) The
4000 parameter is provided to finit$ by the constructor
4001 invoking it (hence the constructor will also feature a
4002 hidden parameter, set to the value of the outer context
4003 local at the time the inner class is created.)
4005 Note: we take into account all possible locals that can
4006 be accessed by the inner class. It's actually not trivial
4007 to minimize these aliases down to the ones really
4008 used. One way to do that would be to expand all regular
4009 methods first, then finit$ to get a picture of what's
4010 used. It works with the exception that we would have to
4011 go back on all constructor invoked in regular methods to
4012 have their invokation reworked (to include the right amount
4013 of alias initializer parameters.)
4015 The only real way around, I think, is a first pass to
4016 identify locals really used in the inner class. We leave
4017 the flag FIELD_LOCAL_ALIAS_USED around for that future
4020 On the other hand, it only affect local inner classes,
4021 whose constructors (and finit$ call) will be featuring
4022 unecessary arguments. It's easy for a developper to keep
4023 this number of parameter down by using the `final'
4024 keyword only when necessary. For the time being, we can
4025 issue a warning on unecessary finals. FIXME */
4026 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4029 /* Register the field. The TREE_LIST holding the part
4030 initialized/initializer will be marked ARG_FINAL_P so
4031 that the created field can be marked
4032 FIELD_LOCAL_ALIAS. */
4033 list = build_tree_list (wfl, init);
4034 ARG_FINAL_P (list) = 1;
4035 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4039 if (!CPC_INITIALIZER_STMT (ctxp))
4042 /* If we ever registered an alias field, insert and marker to
4043 remeber where the list ends. The second part of the list (the one
4044 featuring initialized fields) so it can be later reversed to
4045 enforce 8.5. The marker will be removed during that operation. */
4046 marker = build_tree_list (NULL_TREE, NULL_TREE);
4047 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4048 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4051 /* Can't use lookup_field () since we don't want to load the class and
4052 can't set the CLASS_LOADED_P flag */
4055 find_field (class, name)
4060 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4062 if (DECL_NAME (decl) == name)
4068 /* Wrap around lookup_field that doesn't potentially upset the value
4072 lookup_field_wrapper (class, name)
4076 tree decl = NULL_TREE;
4077 java_parser_context_save_global ();
4079 /* Last chance: if we're within the context of an inner class, we
4080 might be trying to access a local variable defined in an outer
4081 context. We try to look for it now. */
4082 if (INNER_CLASS_TYPE_P (class))
4085 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4086 decl = lookup_field (&type, new_name);
4087 if (decl && decl != error_mark_node)
4088 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4090 if (!decl || decl == error_mark_node)
4093 decl = lookup_field (&type, name);
4096 java_parser_context_restore_global ();
4097 return decl == error_mark_node ? NULL : decl;
4100 /* Find duplicate field within the same class declarations and report
4101 the error. Returns 1 if a duplicated field was found, 0
4105 duplicate_declaration_error_p (new_field_name, new_type, cl)
4106 tree new_field_name, new_type, cl;
4108 /* This might be modified to work with method decl as well */
4109 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4112 char *t1 = xstrdup (purify_type_name
4113 ((TREE_CODE (new_type) == POINTER_TYPE
4114 && TREE_TYPE (new_type) == NULL_TREE) ?
4115 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4116 lang_printable_name (new_type, 1)));
4117 /* The type may not have been completed by the time we report
4119 char *t2 = xstrdup (purify_type_name
4120 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4121 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4122 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4123 lang_printable_name (TREE_TYPE (decl), 1)));
4125 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4126 t1, IDENTIFIER_POINTER (new_field_name),
4127 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4128 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4136 /* Field registration routine. If TYPE doesn't exist, field
4137 declarations are linked to the undefined TYPE dependency list, to
4138 be later resolved in java_complete_class () */
4141 register_fields (flags, type, variable_list)
4143 tree type, variable_list;
4145 tree current, saved_type;
4146 tree class_type = NULL_TREE;
4147 int saved_lineno = lineno;
4149 tree wfl = NULL_TREE;
4152 class_type = TREE_TYPE (GET_CPC ());
4154 if (!class_type || class_type == error_mark_node)
4157 /* If we're adding fields to interfaces, those fields are public,
4159 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4161 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4162 flags, ACC_PUBLIC, "interface field(s)");
4163 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4164 flags, ACC_STATIC, "interface field(s)");
4165 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4166 flags, ACC_FINAL, "interface field(s)");
4167 check_modifiers ("Illegal interface member modifier `%s'", flags,
4168 INTERFACE_FIELD_MODIFIERS);
4169 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4172 /* Obtain a suitable type for resolution, if necessary */
4173 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4175 /* If TYPE is fully resolved and we don't have a reference, make one */
4176 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4178 for (current = variable_list, saved_type = type; current;
4179 current = TREE_CHAIN (current), type = saved_type)
4183 tree cl = TREE_PURPOSE (current);
4184 tree init = TREE_VALUE (current);
4185 tree current_name = EXPR_WFL_NODE (cl);
4187 /* Can't declare non-final static fields in inner classes */
4188 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4189 && !(flags & ACC_FINAL))
4191 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4192 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4193 lang_printable_name (class_type, 0));
4195 /* Process NAME, as it may specify extra dimension(s) for it */
4196 type = build_array_from_name (type, wfl, current_name, ¤t_name);
4198 /* Type adjustment. We may have just readjusted TYPE because
4199 the variable specified more dimensions. Make sure we have
4200 a reference if we can and don't have one already. Also
4201 change the name if we have an init. */
4202 if (type != saved_type)
4204 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4206 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4209 real_type = GET_REAL_TYPE (type);
4210 /* Check for redeclarations */
4211 if (duplicate_declaration_error_p (current_name, real_type, cl))
4214 /* Set lineno to the line the field was found and create a
4215 declaration for it. Eventually sets the @deprecated tag flag. */
4217 lineno = EXPR_WFL_LINECOL (cl);
4219 lineno = EXPR_WFL_LINENO (cl);
4220 field_decl = add_field (class_type, current_name, real_type, flags);
4221 CHECK_DEPRECATED (field_decl);
4223 /* If the field denotes a final instance variable, then we
4224 allocate a LANG_DECL_SPECIFIC part to keep track of its
4225 initialization. We also mark whether the field was
4226 initialized upon it's declaration. We don't do that if the
4227 created field is an alias to a final local. */
4228 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4230 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4231 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4232 if ((flags & ACC_STATIC) && init)
4233 DECL_FIELD_FINAL_IUD (field_decl) = 1;
4236 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4237 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4238 hide parameters to this inner class finit$ and
4239 constructors. It also means that the field isn't final per
4241 if (ARG_FINAL_P (current))
4243 FIELD_LOCAL_ALIAS (field_decl) = 1;
4244 FIELD_FINAL (field_decl) = 0;
4247 /* Check if we must chain. */
4249 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4251 /* If we have an initialization value tied to the field */
4254 /* The field is declared static */
4255 if (flags & ACC_STATIC)
4257 /* We include the field and its initialization part into
4258 a list used to generate <clinit>. After <clinit> is
4259 walked, field initializations will be processed and
4260 fields initialized with known constants will be taken
4261 out of <clinit> and have their DECL_INITIAL set
4263 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4264 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4265 if (TREE_OPERAND (init, 1)
4266 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4267 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4269 /* A non-static field declared with an immediate initialization is
4270 to be initialized in <init>, if any. This field is remembered
4271 to be processed at the time of the generation of <init>. */
4274 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4275 SET_CPC_INITIALIZER_STMT (ctxp, init);
4277 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4278 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4281 lineno = saved_lineno;
4284 /* Generate finit$, using the list of initialized fields to populate
4285 its body. finit$'s parameter(s) list is adjusted to include the
4286 one(s) used to initialized the field(s) caching outer context
4290 generate_finit (class_type)
4294 tree list = TYPE_FINIT_STMT_LIST (class_type);
4295 tree mdecl, current, parms;
4297 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4298 class_type, NULL_TREE,
4300 CRAFTED_PARAM_LIST_FIXUP (parms);
4301 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4302 finit_identifier_node, parms);
4303 fix_method_argument_names (parms, mdecl);
4304 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4306 DECL_FUNCTION_NAP (mdecl) = count;
4307 start_artificial_method_body (mdecl);
4309 for (current = list; current; current = TREE_CHAIN (current))
4310 java_method_add_stmt (mdecl,
4311 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4313 end_artificial_method_body (mdecl);
4318 build_instance_initializer (mdecl)
4321 tree compound = NULL_TREE;
4322 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4325 for (current = stmt_list; current; current = TREE_CHAIN (current))
4326 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4332 add_instance_initializer (mdecl)
4335 tree compound = build_instance_initializer (mdecl);
4338 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4339 NULL_TREE, compound));
4342 /* Shared accros method_declarator and method_header to remember the
4343 patch stage that was reached during the declaration of the method.
4344 A method DECL is built differently is there is no patch
4345 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4346 pending on the currently defined method. */
4348 static int patch_stage;
4350 /* Check the method declaration and add the method to its current
4351 class. If the argument list is known to contain incomplete types,
4352 the method is partially added and the registration will be resume
4353 once the method arguments resolved. If TYPE is NULL, we're dealing
4354 with a constructor. */
4357 method_header (flags, type, mdecl, throws)
4359 tree type, mdecl, throws;
4361 tree type_wfl = NULL_TREE;
4362 tree meth_name = NULL_TREE;
4363 tree current, orig_arg, this_class = NULL;
4366 int constructor_ok = 0, must_chain;
4369 if (mdecl == error_mark_node)
4370 return error_mark_node;
4371 meth = TREE_VALUE (mdecl);
4372 id = TREE_PURPOSE (mdecl);
4374 check_modifiers_consistency (flags);
4377 this_class = TREE_TYPE (GET_CPC ());
4379 if (!this_class || this_class == error_mark_node)
4382 /* There are some forbidden modifiers for an abstract method and its
4383 class must be abstract as well. */
4384 if (type && (flags & ACC_ABSTRACT))
4386 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4387 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4388 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4389 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4390 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
4391 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4392 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4394 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4395 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4396 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4399 /* Things to be checked when declaring a constructor */
4402 int ec = java_error_count;
4403 /* 8.6: Constructor declarations: we might be trying to define a
4404 method without specifying a return type. */
4405 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4407 (id, "Invalid method declaration, return type required");
4408 /* 8.6.3: Constructor modifiers */
4411 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4412 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4413 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4414 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4415 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4417 /* If we found error here, we don't consider it's OK to tread
4418 the method definition as a constructor, for the rest of this
4420 if (ec == java_error_count)
4424 /* Method declared within the scope of an interface are implicitly
4425 abstract and public. Conflicts with other erroneously provided
4426 modifiers are checked right after. */
4428 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4430 /* If FLAGS isn't set because of a modifier, turn the
4431 corresponding modifier WFL to NULL so we issue a warning on
4432 the obsolete use of the modifier */
4433 if (!(flags & ACC_PUBLIC))
4434 MODIFIER_WFL (PUBLIC_TK) = NULL;
4435 if (!(flags & ACC_ABSTRACT))
4436 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4437 flags |= ACC_PUBLIC;
4438 flags |= ACC_ABSTRACT;
4441 /* Inner class can't declare static methods */
4442 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4445 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4446 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4447 lang_printable_name (this_class, 0));
4450 /* Modifiers context reset moved up, so abstract method declaration
4451 modifiers can be later checked. */
4453 /* Set constructor returned type to void and method name to <init>,
4454 unless we found an error identifier the constructor (in which
4455 case we retain the original name) */
4458 type = void_type_node;
4460 meth_name = init_identifier_node;
4463 meth_name = EXPR_WFL_NODE (id);
4465 /* Do the returned type resolution and registration if necessary */
4466 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4469 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4470 EXPR_WFL_NODE (id) = meth_name;
4471 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4475 patch_stage = JDEP_METHOD_RETURN;
4476 register_incomplete_type (patch_stage, type_wfl, id, type);
4477 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4480 TREE_TYPE (meth) = type;
4482 saved_lineno = lineno;
4483 /* When defining an abstract or interface method, the curly
4484 bracket at level 1 doesn't exist because there is no function
4486 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4487 EXPR_WFL_LINENO (id));
4489 /* Remember the original argument list */
4490 orig_arg = TYPE_ARG_TYPES (meth);
4492 if (patch_stage) /* includes ret type and/or all args */
4495 meth = add_method_1 (this_class, flags, meth_name, meth);
4496 /* Patch for the return type */
4497 if (patch_stage == JDEP_METHOD_RETURN)
4499 jdep = CLASSD_LAST (ctxp->classd_list);
4500 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4502 /* This is the stop JDEP. METH allows the function's signature
4504 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4507 meth = add_method (this_class, flags, meth_name,
4508 build_java_signature (meth));
4510 /* Remember final parameters */
4511 MARK_FINAL_PARMS (meth, orig_arg);
4513 /* Fix the method argument list so we have the argument name
4515 fix_method_argument_names (orig_arg, meth);
4517 /* Register the parameter number and re-install the current line
4519 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4520 lineno = saved_lineno;
4522 /* Register exception specified by the `throws' keyword for
4523 resolution and set the method decl appropriate field to the list.
4524 Note: the grammar ensures that what we get here are class
4528 throws = nreverse (throws);
4529 for (current = throws; current; current = TREE_CHAIN (current))
4531 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4532 NULL_TREE, NULL_TREE);
4533 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4534 &TREE_VALUE (current);
4536 DECL_FUNCTION_THROWS (meth) = throws;
4539 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4540 DECL_FUNCTION_WFL (meth) = id;
4542 /* Set the flag if we correctly processed a constructor */
4545 DECL_CONSTRUCTOR_P (meth) = 1;
4546 /* Compute and store the number of artificial parameters declared
4547 for this constructor */
4548 for (count = 0, current = TYPE_FIELDS (this_class); current;
4549 current = TREE_CHAIN (current))
4550 if (FIELD_LOCAL_ALIAS (current))
4552 DECL_FUNCTION_NAP (meth) = count;
4555 /* Eventually set the @deprecated tag flag */
4556 CHECK_DEPRECATED (meth);
4558 /* If doing xref, store column and line number information instead
4559 of the line number only. */
4561 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4567 fix_method_argument_names (orig_arg, meth)
4568 tree orig_arg, meth;
4570 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4571 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4573 TREE_PURPOSE (arg) = this_identifier_node;
4574 arg = TREE_CHAIN (arg);
4576 while (orig_arg != end_params_node)
4578 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4579 orig_arg = TREE_CHAIN (orig_arg);
4580 arg = TREE_CHAIN (arg);
4584 /* Complete the method declaration with METHOD_BODY. */
4587 finish_method_declaration (method_body)
4592 if (!current_function_decl)
4595 flags = get_access_flags_from_decl (current_function_decl);
4597 /* 8.4.5 Method Body */
4598 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4600 tree name = DECL_NAME (current_function_decl);
4601 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4602 "%s method `%s' can't have a body defined",
4603 (METHOD_NATIVE (current_function_decl) ?
4604 "Native" : "Abstract"),
4605 IDENTIFIER_POINTER (name));
4606 method_body = NULL_TREE;
4608 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4610 tree name = DECL_NAME (current_function_decl);
4612 (DECL_FUNCTION_WFL (current_function_decl),
4613 "Non native and non abstract method `%s' must have a body defined",
4614 IDENTIFIER_POINTER (name));
4615 method_body = NULL_TREE;
4618 if (flag_emit_class_files && method_body
4619 && TREE_CODE (method_body) == NOP_EXPR
4620 && TREE_TYPE (current_function_decl)
4621 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4622 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4624 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4625 maybe_absorb_scoping_blocks ();
4626 /* Exit function's body */
4628 /* Merge last line of the function with first line, directly in the
4629 function decl. It will be used to emit correct debug info. */
4630 if (!flag_emit_xref)
4631 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4633 /* Since function's argument's list are shared, reset the
4634 ARG_FINAL_P parameter that might have been set on some of this
4635 function parameters. */
4636 UNMARK_FINAL_PARMS (current_function_decl);
4638 /* So we don't have an irrelevant function declaration context for
4639 the next static block we'll see. */
4640 current_function_decl = NULL_TREE;
4643 /* Build a an error message for constructor circularity errors. */
4646 constructor_circularity_msg (from, to)
4649 static char string [4096];
4650 char *t = xstrdup (lang_printable_name (from, 0));
4651 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4656 /* Verify a circular call to METH. Return 1 if an error is found, 0
4660 verify_constructor_circularity (meth, current)
4663 static tree list = NULL_TREE;
4664 static int initialized_p;
4667 /* If we haven't already registered LIST with the garbage collector,
4671 ggc_add_tree_root (&list, 1);
4675 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4677 if (TREE_VALUE (c) == meth)
4683 list = nreverse (list);
4684 for (liste = list; liste; liste = TREE_CHAIN (liste))
4687 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4688 constructor_circularity_msg
4689 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4693 t = xstrdup (lang_printable_name (meth, 0));
4694 parse_error_context (TREE_PURPOSE (c),
4695 "%s: recursive invocation of constructor `%s'",
4696 constructor_circularity_msg (current, meth), t);
4702 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4704 list = tree_cons (c, current, list);
4705 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4707 list = TREE_CHAIN (list);
4712 /* Check modifiers that can be declared but exclusively */
4715 check_modifiers_consistency (flags)
4719 tree cl = NULL_TREE;
4721 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4722 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4723 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4726 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4730 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4731 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4733 parse_error_context (cl,
4734 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
4737 /* Check the methode header METH for abstract specifics features */
4740 check_abstract_method_header (meth)
4743 int flags = get_access_flags_from_decl (meth);
4745 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4746 ACC_ABSTRACT, "abstract method",
4747 IDENTIFIER_POINTER (DECL_NAME (meth)));
4748 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4749 ACC_PUBLIC, "abstract method",
4750 IDENTIFIER_POINTER (DECL_NAME (meth)));
4752 check_modifiers ("Illegal modifier `%s' for interface method",
4753 flags, INTERFACE_METHOD_MODIFIERS);
4756 /* Create a FUNCTION_TYPE node and start augmenting it with the
4757 declared function arguments. Arguments type that can't be resolved
4758 are left as they are, but the returned node is marked as containing
4759 incomplete types. */
4762 method_declarator (id, list)
4765 tree arg_types = NULL_TREE, current, node;
4766 tree meth = make_node (FUNCTION_TYPE);
4769 patch_stage = JDEP_NO_PATCH;
4771 if (GET_CPC () == error_mark_node)
4772 return error_mark_node;
4774 /* If we're dealing with an inner class constructor, we hide the
4775 this$<n> decl in the name field of its parameter declaration. We
4776 also might have to hide the outer context local alias
4777 initializers. Not done when the class is a toplevel class. */
4778 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4779 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4781 tree aliases_list, type, thisn;
4782 /* First the aliases, linked to the regular parameters */
4784 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4785 TREE_TYPE (GET_CPC ()),
4787 list = chainon (nreverse (aliases_list), list);
4790 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4791 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4792 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4796 for (current = list; current; current = TREE_CHAIN (current))
4799 tree wfl_name = TREE_PURPOSE (current);
4800 tree type = TREE_VALUE (current);
4801 tree name = EXPR_WFL_NODE (wfl_name);
4802 tree already, arg_node;
4803 tree type_wfl = NULL_TREE;
4806 /* Obtain a suitable type for resolution, if necessary */
4807 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4809 /* Process NAME, as it may specify extra dimension(s) for it */
4810 type = build_array_from_name (type, type_wfl, name, &name);
4811 EXPR_WFL_NODE (wfl_name) = name;
4813 real_type = GET_REAL_TYPE (type);
4814 if (TREE_CODE (real_type) == RECORD_TYPE)
4816 real_type = promote_type (real_type);
4817 if (TREE_CODE (type) == TREE_LIST)
4818 TREE_PURPOSE (type) = real_type;
4821 /* Check redefinition */
4822 for (already = arg_types; already; already = TREE_CHAIN (already))
4823 if (TREE_PURPOSE (already) == name)
4826 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4827 IDENTIFIER_POINTER (name),
4828 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4832 /* If we've an incomplete argument type, we know there is a location
4833 to patch when the type get resolved, later. */
4837 patch_stage = JDEP_METHOD;
4838 type = register_incomplete_type (patch_stage,
4839 type_wfl, wfl_name, type);
4840 jdep = CLASSD_LAST (ctxp->classd_list);
4841 JDEP_MISC (jdep) = id;
4844 /* The argument node: a name and a (possibly) incomplete type. */
4845 arg_node = build_tree_list (name, real_type);
4846 /* Remeber arguments declared final. */
4847 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4850 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4851 TREE_CHAIN (arg_node) = arg_types;
4852 arg_types = arg_node;
4854 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
4855 node = build_tree_list (id, meth);
4860 unresolved_type_p (wfl, returned)
4865 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4869 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4870 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4871 *returned = TREE_TYPE (decl);
4872 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4873 *returned = TREE_TYPE (GET_CPC ());
4875 *returned = NULL_TREE;
4884 /* From NAME, build a qualified identifier node using the
4885 qualification from the current package definition. */
4888 parser_qualified_classname (name)
4891 tree nested_class_name;
4893 if ((nested_class_name = maybe_make_nested_class_name (name)))
4894 return nested_class_name;
4897 return merge_qualified_name (ctxp->package, name);
4902 /* Called once the type a interface extends is resolved. Returns 0 if
4903 everything is OK. */
4906 parser_check_super_interface (super_decl, this_decl, this_wfl)
4907 tree super_decl, this_decl, this_wfl;
4909 tree super_type = TREE_TYPE (super_decl);
4911 /* Has to be an interface */
4912 if (!CLASS_INTERFACE (super_decl))
4915 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4916 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4917 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4918 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4919 "interface" : "class"),
4920 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4924 /* Check top-level interface access. Inner classes are subject to member
4925 access rules (6.6.1). */
4926 if (! INNER_CLASS_P (super_type)
4927 && check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4930 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4931 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4932 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4936 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4937 0 if everthing is OK. */
4940 parser_check_super (super_decl, this_decl, wfl)
4941 tree super_decl, this_decl, wfl;
4943 tree super_type = TREE_TYPE (super_decl);
4945 /* SUPER should be a CLASS (neither an array nor an interface) */
4946 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4949 (wfl, "Class `%s' can't subclass %s `%s'",
4950 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4951 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4952 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4956 if (CLASS_FINAL (TYPE_NAME (super_type)))
4958 parse_error_context (wfl, "Can't subclass final classes: %s",
4959 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4963 /* Check top-level class scope. Inner classes are subject to member access
4965 if (! INNER_CLASS_P (super_type)
4966 && (check_pkg_class_access (DECL_NAME (super_decl), wfl)))
4969 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4970 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4971 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4975 /* Create a new dependency list and link it (in a LIFO manner) to the
4976 CTXP list of type dependency list. */
4979 create_jdep_list (ctxp)
4980 struct parser_ctxt *ctxp;
4982 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
4983 new->first = new->last = NULL;
4984 new->next = ctxp->classd_list;
4985 ctxp->classd_list = new;
4989 reverse_jdep_list (ctxp)
4990 struct parser_ctxt *ctxp;
4992 register jdeplist *prev = NULL, *current, *next;
4993 for (current = ctxp->classd_list; current; current = next)
4995 next = current->next;
4996 current->next = prev;
5002 /* Create a fake pointer based on the ID stored in
5003 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
5004 registered again. */
5007 obtain_incomplete_type (type_name)
5010 tree ptr = NULL_TREE, name;
5012 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
5013 name = EXPR_WFL_NODE (type_name);
5014 else if (INCOMPLETE_TYPE_P (type_name))
5015 name = TYPE_NAME (type_name);
5019 BUILD_PTR_FROM_NAME (ptr, name);
5025 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5026 non NULL instead of computing a new fake type based on WFL. The new
5027 dependency is inserted in the current type dependency list, in FIFO
5031 register_incomplete_type (kind, wfl, decl, ptr)
5033 tree wfl, decl, ptr;
5035 jdep *new = (jdep *)xmalloc (sizeof (jdep));
5037 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5038 ptr = obtain_incomplete_type (wfl);
5040 JDEP_KIND (new) = kind;
5041 JDEP_DECL (new) = decl;
5042 JDEP_TO_RESOLVE (new) = ptr;
5043 JDEP_WFL (new) = wfl;
5044 JDEP_CHAIN (new) = NULL;
5045 JDEP_MISC (new) = NULL_TREE;
5046 /* For some dependencies, set the enclosing class of the current
5047 class to be the enclosing context */
5048 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE
5049 || kind == JDEP_ANONYMOUS || kind == JDEP_FIELD)
5050 && GET_ENCLOSING_CPC ())
5051 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5053 JDEP_ENCLOSING (new) = GET_CPC ();
5054 JDEP_GET_PATCH (new) = (tree *)NULL;
5056 JDEP_INSERT (ctxp->classd_list, new);
5062 java_check_circular_reference ()
5065 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5067 tree type = TREE_TYPE (current);
5068 if (CLASS_INTERFACE (current))
5070 /* Check all interfaces this class extends */
5071 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5076 n = TREE_VEC_LENGTH (basetype_vec);
5077 for (i = 0; i < n; i++)
5079 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5080 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5081 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5082 parse_error_context (lookup_cl (current),
5083 "Cyclic interface inheritance");
5087 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5088 parse_error_context (lookup_cl (current),
5089 "Cyclic class inheritance%s",
5090 (cyclic_inheritance_report ?
5091 cyclic_inheritance_report : ""));
5095 /* Augment the parameter list PARM with parameters crafted to
5096 initialize outer context locals aliases. Through ARTIFICIAL, a
5097 count is kept of the number of crafted parameters. MODE governs
5098 what eventually gets created: something suitable for a function
5099 creation or a function invocation, either the constructor or
5103 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5105 tree class_type, parm;
5109 tree additional_parms = NULL_TREE;
5111 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5112 if (FIELD_LOCAL_ALIAS (field))
5114 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5115 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5120 case AIPL_FUNCTION_DECLARATION:
5121 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5123 purpose = build_wfl_node (mangled_id);
5124 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5125 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5127 value = TREE_TYPE (field);
5130 case AIPL_FUNCTION_CREATION:
5131 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5133 value = TREE_TYPE (field);
5136 case AIPL_FUNCTION_FINIT_INVOCATION:
5137 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5139 /* Now, this is wrong. purpose should always be the NAME
5140 of something and value its matching value (decl, type,
5141 etc...) FIXME -- but there is a lot to fix. */
5143 /* When invoked for this kind of operation, we already
5144 know whether a field is used or not. */
5145 purpose = TREE_TYPE (field);
5146 value = build_wfl_node (mangled_id);
5149 case AIPL_FUNCTION_CTOR_INVOCATION:
5150 /* There are two case: the constructor invokation happends
5151 outside the local inner, in which case, locales from the outer
5152 context are directly used.
5154 Otherwise, we fold to using the alias directly. */
5155 if (class_type == current_class)
5159 name = get_identifier (&buffer[4]);
5160 value = IDENTIFIER_LOCAL_VALUE (name);
5164 additional_parms = tree_cons (purpose, value, additional_parms);
5168 if (additional_parms)
5170 if (ANONYMOUS_CLASS_P (class_type)
5171 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5172 additional_parms = nreverse (additional_parms);
5173 parm = chainon (additional_parms, parm);
5179 /* Craft a constructor for CLASS_DECL -- what we should do when none
5180 where found. ARGS is non NULL when a special signature must be
5181 enforced. This is the case for anonymous classes. */
5184 craft_constructor (class_decl, args)
5185 tree class_decl, args;
5187 tree class_type = TREE_TYPE (class_decl);
5188 tree parm = NULL_TREE;
5189 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5191 int i = 0, artificial = 0;
5192 tree decl, ctor_name;
5195 /* The constructor name is <init> unless we're dealing with an
5196 anonymous class, in which case the name will be fixed after having
5198 if (ANONYMOUS_CLASS_P (class_type))
5199 ctor_name = DECL_NAME (class_decl);
5201 ctor_name = init_identifier_node;
5203 /* If we're dealing with an inner class constructor, we hide the
5204 this$<n> decl in the name field of its parameter declaration. */
5205 if (PURE_INNER_CLASS_TYPE_P (class_type))
5207 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5208 parm = tree_cons (build_current_thisn (class_type),
5209 build_pointer_type (type), parm);
5211 /* Some more arguments to be hidden here. The values of the local
5212 variables of the outer context that the inner class needs to see. */
5213 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5218 /* Then if there are any args to be enforced, enforce them now */
5219 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5221 sprintf (buffer, "parm%d", i++);
5222 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5225 CRAFTED_PARAM_LIST_FIXUP (parm);
5226 decl = create_artificial_method (class_type, flags, void_type_node,
5228 fix_method_argument_names (parm, decl);
5229 /* Now, mark the artificial parameters. */
5230 DECL_FUNCTION_NAP (decl) = artificial;
5231 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5235 /* Fix the constructors. This will be called right after circular
5236 references have been checked. It is necessary to fix constructors
5237 early even if no code generation will take place for that class:
5238 some generated constructor might be required by the class whose
5239 compilation triggered this one to be simply loaded. */
5242 java_fix_constructors ()
5246 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5248 tree class_type = TREE_TYPE (current);
5252 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5255 current_class = class_type;
5256 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5258 if (DECL_CONSTRUCTOR_P (decl))
5260 fix_constructors (decl);
5265 /* Anonymous class constructor can't be generated that early. */
5266 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5267 craft_constructor (current, NULL_TREE);
5271 /* safe_layout_class just makes sure that we can load a class without
5272 disrupting the current_class, input_file, lineno, etc, information
5273 about the class processed currently. */
5276 safe_layout_class (class)
5279 tree save_current_class = current_class;
5280 const char *save_input_filename = input_filename;
5281 int save_lineno = lineno;
5283 layout_class (class);
5285 current_class = save_current_class;
5286 input_filename = save_input_filename;
5287 lineno = save_lineno;
5291 jdep_resolve_class (dep)
5296 if (JDEP_RESOLVED_P (dep))
5297 decl = JDEP_RESOLVED_DECL (dep);
5300 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5301 JDEP_DECL (dep), JDEP_WFL (dep));
5302 JDEP_RESOLVED (dep, decl);
5306 complete_class_report_errors (dep);
5307 else if (PURE_INNER_CLASS_DECL_P (decl))
5309 tree inner = TREE_TYPE (decl);
5310 if (! CLASS_LOADED_P (inner))
5312 safe_layout_class (inner);
5313 if (TYPE_SIZE (inner) == error_mark_node)
5314 TYPE_SIZE (inner) = NULL_TREE;
5316 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5321 /* Complete unsatisfied class declaration and their dependencies */
5324 java_complete_class ()
5331 /* Process imports */
5334 /* Rever things so we have the right order */
5335 ctxp->class_list = nreverse (ctxp->class_list);
5336 ctxp->classd_list = reverse_jdep_list (ctxp);
5338 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5340 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5343 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5346 if (!(decl = jdep_resolve_class (dep)))
5349 /* Now it's time to patch */
5350 switch (JDEP_KIND (dep))
5353 /* Simply patch super */
5354 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5356 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5357 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5362 /* We do part of the job done in add_field */
5363 tree field_decl = JDEP_DECL (dep);
5364 tree field_type = TREE_TYPE (decl);
5365 if (TREE_CODE (field_type) == RECORD_TYPE)
5366 field_type = promote_type (field_type);
5367 TREE_TYPE (field_decl) = field_type;
5368 DECL_ALIGN (field_decl) = 0;
5369 DECL_USER_ALIGN (field_decl) = 0;
5370 layout_decl (field_decl, 0);
5371 SOURCE_FRONTEND_DEBUG
5372 (("Completed field/var decl `%s' with `%s'",
5373 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5374 IDENTIFIER_POINTER (DECL_NAME (decl))));
5377 case JDEP_METHOD: /* We start patching a method */
5378 case JDEP_METHOD_RETURN:
5384 type = TREE_TYPE(decl);
5385 if (TREE_CODE (type) == RECORD_TYPE)
5386 type = promote_type (type);
5387 JDEP_APPLY_PATCH (dep, type);
5388 SOURCE_FRONTEND_DEBUG
5389 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5390 "Completing fct `%s' with ret type `%s'":
5391 "Completing arg `%s' with type `%s'"),
5392 IDENTIFIER_POINTER (EXPR_WFL_NODE
5393 (JDEP_DECL_WFL (dep))),
5394 IDENTIFIER_POINTER (DECL_NAME (decl))));
5398 dep = JDEP_CHAIN (dep);
5399 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5402 decl = jdep_resolve_class (dep);
5406 tree mdecl = JDEP_DECL (dep), signature;
5407 /* Recompute and reset the signature, check first that
5408 all types are now defined. If they're not,
5409 dont build the signature. */
5410 if (check_method_types_complete (mdecl))
5412 signature = build_java_signature (TREE_TYPE (mdecl));
5413 set_java_signature (TREE_TYPE (mdecl), signature);
5420 case JDEP_INTERFACE:
5421 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5424 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5429 type = TREE_TYPE(decl);
5430 if (TREE_CODE (type) == RECORD_TYPE)
5431 type = promote_type (type);
5432 JDEP_APPLY_PATCH (dep, type);
5436 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5437 SOURCE_FRONTEND_DEBUG
5438 (("Completing a random type dependency on a '%s' node",
5439 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5442 case JDEP_EXCEPTION:
5443 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5444 SOURCE_FRONTEND_DEBUG
5445 (("Completing `%s' `throws' argument node",
5446 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5449 case JDEP_ANONYMOUS:
5450 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5461 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5465 resolve_class (enclosing, class_type, decl, cl)
5466 tree enclosing, class_type, decl, cl;
5468 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5469 const char *base = name;
5470 tree resolved_type = TREE_TYPE (class_type);
5471 tree resolved_type_decl;
5473 if (resolved_type != NULL_TREE)
5475 tree resolved_type_decl = TYPE_NAME (resolved_type);
5476 if (resolved_type_decl == NULL_TREE
5477 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5479 resolved_type_decl = build_decl (TYPE_DECL,
5480 TYPE_NAME (class_type),
5483 return resolved_type_decl;
5486 /* 1- Check to see if we have an array. If true, find what we really
5488 while (name[0] == '[')
5492 TYPE_NAME (class_type) = get_identifier (name);
5493 WFL_STRIP_BRACKET (cl, cl);
5496 /* 2- Resolve the bare type */
5497 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5500 resolved_type = TREE_TYPE (resolved_type_decl);
5502 /* 3- If we have and array, reconstruct the array down to its nesting */
5505 while (base != name)
5507 resolved_type = build_java_array_type (resolved_type, -1);
5510 /* A TYPE_NAME that is a TYPE_DECL was set in
5511 build_java_array_type, return it. */
5512 resolved_type_decl = TYPE_NAME (resolved_type);
5514 TREE_TYPE (class_type) = resolved_type;
5515 return resolved_type_decl;
5518 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5519 are used to report error messages. */
5522 do_resolve_class (enclosing, class_type, decl, cl)
5523 tree enclosing, class_type, decl, cl;
5525 tree new_class_decl, super, start;
5527 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
5528 it is changed by find_in_imports{_on_demand} and (but it doesn't
5529 really matter) qualify_and_find */
5531 /* 0- Search in the current class as an inner class */
5534 /* Maybe some code here should be added to load the class or
5535 something, at least if the class isn't an inner class and ended
5536 being loaded from class file. FIXME. */
5541 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5542 return new_class_decl;
5544 intermediate = enclosing;
5545 /* Explore enclosing contexts. */
5546 while (INNER_CLASS_DECL_P (intermediate))
5548 intermediate = DECL_CONTEXT (intermediate);
5549 if ((new_class_decl = find_as_inner_class (intermediate,
5551 return new_class_decl;
5554 /* Now go to the upper classes, bail out if necessary. */
5555 super = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5556 if (!super || super == object_type_node)
5559 if (TREE_CODE (super) == POINTER_TYPE)
5560 super = do_resolve_class (NULL, super, NULL, NULL);
5562 super = TYPE_NAME (super);
5564 /* We may not have checked for circular inheritance yet, so do so
5565 here to prevent an infinite loop. */
5569 cl = lookup_cl (decl);
5572 (cl, "Cyclic inheritance involving %s",
5573 IDENTIFIER_POINTER (DECL_NAME (enclosing)));
5579 /* 1- Check for the type in single imports. This will change
5580 TYPE_NAME() if something relevant is found */
5581 find_in_imports (class_type);
5583 /* 2- And check for the type in the current compilation unit */
5584 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5586 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5587 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5588 load_class (TYPE_NAME (class_type), 0);
5589 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5592 /* 3- Search according to the current package definition */
5593 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5595 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5596 TYPE_NAME (class_type))))
5597 return new_class_decl;
5600 /* 4- Check the import on demands. Don't allow bar.baz to be
5601 imported from foo.* */
5602 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5603 if (find_in_imports_on_demand (class_type))
5606 /* If found in find_in_imports_on_demant, the type has already been
5608 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5609 return new_class_decl;
5611 /* 5- Try with a name qualified with the package name we've seen so far */
5612 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5616 /* If there is a current package (ctxp->package), it's the first
5617 element of package_list and we can skip it. */
5618 for (package = (ctxp->package ?
5619 TREE_CHAIN (package_list) : package_list);
5620 package; package = TREE_CHAIN (package))
5621 if ((new_class_decl = qualify_and_find (class_type,
5622 TREE_PURPOSE (package),
5623 TYPE_NAME (class_type))))
5624 return new_class_decl;
5627 /* 5- Check an other compilation unit that bears the name of type */
5628 load_class (TYPE_NAME (class_type), 0);
5631 cl = lookup_cl (decl);
5633 /* If we don't have a value for CL, then we're being called recursively.
5634 We can't check package access just yet, but it will be taken care of
5638 if (check_pkg_class_access (TYPE_NAME (class_type), cl))
5642 /* 6- Last call for a resolution */
5643 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5647 qualify_and_find (class_type, package, name)
5648 tree class_type, package, name;
5650 tree new_qualified = merge_qualified_name (package, name);
5651 tree new_class_decl;
5653 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5654 load_class (new_qualified, 0);
5655 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5657 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5658 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5659 load_class (new_qualified, 0);
5660 TYPE_NAME (class_type) = new_qualified;
5661 return IDENTIFIER_CLASS_VALUE (new_qualified);
5666 /* Resolve NAME and lay it out (if not done and if not the current
5667 parsed class). Return a decl node. This function is meant to be
5668 called when type resolution is necessary during the walk pass. */
5671 resolve_and_layout (something, cl)
5675 tree decl, decl_type;
5677 /* Don't do that on the current class */
5678 if (something == current_class)
5679 return TYPE_NAME (current_class);
5681 /* Don't do anything for void and other primitive types */
5682 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5685 /* Pointer types can be reall pointer types or fake pointers. When
5686 finding a real pointer, recheck for primitive types */
5687 if (TREE_CODE (something) == POINTER_TYPE)
5689 if (TREE_TYPE (something))
5691 something = TREE_TYPE (something);
5692 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5696 something = TYPE_NAME (something);
5699 /* Don't do anything for arrays of primitive types */
5700 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5701 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5704 /* Something might be a WFL */
5705 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5706 something = EXPR_WFL_NODE (something);
5708 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5709 TYPE_DECL or a real TYPE */
5710 else if (TREE_CODE (something) != IDENTIFIER_NODE)
5711 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5712 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5714 if (!(decl = resolve_no_layout (something, cl)))
5717 /* Resolve and layout if necessary */
5718 decl_type = TREE_TYPE (decl);
5719 layout_class_methods (decl_type);
5721 if (CLASS_FROM_SOURCE_P (decl_type))
5722 java_check_methods (decl);
5723 /* Layout the type if necessary */
5724 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5725 safe_layout_class (decl_type);
5730 /* Resolve a class, returns its decl but doesn't perform any
5731 layout. The current parsing context is saved and restored */
5734 resolve_no_layout (name, cl)
5738 BUILD_PTR_FROM_NAME (ptr, name);
5739 java_parser_context_save_global ();
5740 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5741 java_parser_context_restore_global ();
5746 /* Called when reporting errors. Skip leader '[' in a complex array
5747 type description that failed to be resolved. */
5750 purify_type_name (name)
5753 while (*name && *name == '[')
5758 /* The type CURRENT refers to can't be found. We print error messages. */
5761 complete_class_report_errors (dep)
5766 if (!JDEP_WFL (dep))
5769 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
5770 switch (JDEP_KIND (dep))
5774 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
5775 purify_type_name (name),
5776 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5780 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
5781 purify_type_name (name),
5782 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5784 case JDEP_METHOD: /* Covers arguments */
5786 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
5787 purify_type_name (name),
5788 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5789 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5791 case JDEP_METHOD_RETURN: /* Covers return type */
5793 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
5794 purify_type_name (name),
5795 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5797 case JDEP_INTERFACE:
5799 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5800 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5801 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5802 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5806 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
5807 purify_type_name (IDENTIFIER_POINTER
5808 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
5809 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5811 case JDEP_EXCEPTION: /* As specified by `throws' */
5813 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5814 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5817 /* Fix for -Wall. Just break doing nothing. The error will be
5823 /* Return a static string containing the DECL prototype string. If
5824 DECL is a constructor, use the class name instead of the form
5828 get_printable_method_name (decl)
5831 const char *to_return;
5832 tree name = NULL_TREE;
5834 if (DECL_CONSTRUCTOR_P (decl))
5836 name = DECL_NAME (decl);
5837 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
5840 to_return = lang_printable_name (decl, 0);
5841 if (DECL_CONSTRUCTOR_P (decl))
5842 DECL_NAME (decl) = name;
5847 /* Track method being redefined inside the same class. As a side
5848 effect, set DECL_NAME to an IDENTIFIER (prior entering this
5849 function it's a FWL, so we can track errors more accurately.) */
5852 check_method_redefinition (class, method)
5857 /* There's no need to verify <clinit> and finit$ */
5858 if (DECL_CLINIT_P (method) || DECL_FINIT_P (method))
5861 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
5862 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5864 if (redef == method)
5866 if (DECL_NAME (redef) == DECL_NAME (method)
5867 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
5868 && !DECL_ARTIFICIAL (method))
5871 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
5872 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5873 get_printable_method_name (redef));
5880 /* Return 1 if check went ok, 0 otherwise. */
5882 check_abstract_method_definitions (do_interface, class_decl, type)
5884 tree class_decl, type;
5886 tree class = TREE_TYPE (class_decl);
5887 tree method, end_type;
5890 end_type = (do_interface ? object_type_node : type);
5891 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5893 tree other_super, other_method, method_sig, method_name;
5895 int end_type_reached = 0;
5897 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5900 /* Now verify that somewhere in between TYPE and CLASS,
5901 abstract method METHOD gets a non abstract definition
5902 that is inherited by CLASS. */
5904 method_sig = build_java_signature (TREE_TYPE (method));
5905 method_name = DECL_NAME (method);
5906 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5907 method_name = EXPR_WFL_NODE (method_name);
5909 other_super = class;
5911 if (other_super == end_type)
5912 end_type_reached = 1;
5915 for (other_method = TYPE_METHODS (other_super); other_method;
5916 other_method = TREE_CHAIN (other_method))
5918 tree s = build_java_signature (TREE_TYPE (other_method));
5919 tree other_name = DECL_NAME (other_method);
5921 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5922 other_name = EXPR_WFL_NODE (other_name);
5923 if (!DECL_CLINIT_P (other_method)
5924 && !DECL_CONSTRUCTOR_P (other_method)
5925 && method_name == other_name
5927 && !METHOD_ABSTRACT (other_method))
5933 other_super = CLASSTYPE_SUPER (other_super);
5934 } while (!end_type_reached);
5936 /* Report that abstract METHOD didn't find an implementation
5937 that CLASS can use. */
5940 char *t = xstrdup (lang_printable_name
5941 (TREE_TYPE (TREE_TYPE (method)), 0));
5942 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5945 (lookup_cl (class_decl),
5946 "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",
5947 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5948 t, lang_printable_name (method, 0),
5949 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5950 "interface" : "class"),
5951 IDENTIFIER_POINTER (ccn),
5952 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5953 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
5959 if (ok && do_interface)
5961 /* Check for implemented interfaces. */
5963 tree vector = TYPE_BINFO_BASETYPES (type);
5964 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5966 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5967 ok = check_abstract_method_definitions (1, class_decl, super);
5974 /* Check that CLASS_DECL somehow implements all inherited abstract
5978 java_check_abstract_method_definitions (class_decl)
5981 tree class = TREE_TYPE (class_decl);
5985 if (CLASS_ABSTRACT (class_decl))
5988 /* Check for inherited types */
5991 super = CLASSTYPE_SUPER (super);
5992 check_abstract_method_definitions (0, class_decl, super);
5993 } while (super != object_type_node);
5995 /* Check for implemented interfaces. */
5996 vector = TYPE_BINFO_BASETYPES (class);
5997 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5999 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
6000 check_abstract_method_definitions (1, class_decl, super);
6004 /* Check all the types method DECL uses and return 1 if all of them
6005 are now complete, 0 otherwise. This is used to check whether its
6006 safe to build a method signature or not. */
6009 check_method_types_complete (decl)
6012 tree type = TREE_TYPE (decl);
6015 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6018 args = TYPE_ARG_TYPES (type);
6019 if (TREE_CODE (type) == METHOD_TYPE)
6020 args = TREE_CHAIN (args);
6021 for (; args != end_params_node; args = TREE_CHAIN (args))
6022 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6028 /* Visible interface to check methods contained in CLASS_DECL */
6031 java_check_methods (class_decl)
6034 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6037 if (CLASS_INTERFACE (class_decl))
6038 java_check_abstract_methods (class_decl);
6040 java_check_regular_methods (class_decl);
6042 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6045 /* Check all the methods of CLASS_DECL. Methods are first completed
6046 then checked according to regular method existance rules. If no
6047 constructor for CLASS_DECL were encountered, then build its
6051 java_check_regular_methods (class_decl)
6054 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6056 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
6057 tree found = NULL_TREE;
6060 /* It is not necessary to check methods defined in java.lang.Object */
6061 if (class == object_type_node)
6064 if (!TYPE_NVIRTUALS (class))
6065 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6067 /* Should take interfaces into account. FIXME */
6068 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6071 tree method_wfl = DECL_FUNCTION_WFL (method);
6074 /* Check for redefinitions */
6075 if (check_method_redefinition (class, method))
6078 /* If we see one constructor a mark so we don't generate the
6079 default one. Also skip other verifications: constructors
6080 can't be inherited hence hiden or overriden */
6081 if (DECL_CONSTRUCTOR_P (method))
6083 saw_constructor = 1;
6087 /* We verify things thrown by the method. They must inherits from
6088 java.lang.Throwable */
6089 for (mthrows = DECL_FUNCTION_THROWS (method);
6090 mthrows; mthrows = TREE_CHAIN (mthrows))
6092 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6094 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6096 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6099 sig = build_java_argument_signature (TREE_TYPE (method));
6100 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6102 /* Inner class can't declare static methods */
6103 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6105 char *t = xstrdup (lang_printable_name (class, 0));
6107 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6108 lang_printable_name (method, 0), t);
6112 /* Nothing overrides or it's a private method. */
6115 if (METHOD_PRIVATE (found))
6121 /* If `found' is declared in an interface, make sure the
6122 modifier matches. */
6123 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6124 && clinit_identifier_node != DECL_NAME (found)
6125 && !METHOD_PUBLIC (method))
6127 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6128 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6129 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6130 lang_printable_name (method, 0),
6131 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6134 /* Can't override a method with the same name and different return
6136 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6139 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6142 "Method `%s' was defined with return type `%s' in class `%s'",
6143 lang_printable_name (found, 0), t,
6145 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6149 aflags = get_access_flags_from_decl (found);
6151 /* Can't override final. Can't override static. */
6152 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6154 /* Static *can* override static */
6155 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6159 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6160 (METHOD_FINAL (found) ? "Final" : "Static"),
6161 lang_printable_name (found, 0),
6162 (METHOD_FINAL (found) ? "final" : "static"),
6164 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6168 /* Static method can't override instance method. */
6169 if (METHOD_STATIC (method))
6173 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6174 lang_printable_name (found, 0),
6176 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6180 /* - Overriding/hiding public must be public
6181 - Overriding/hiding protected must be protected or public
6182 - If the overriden or hidden method has default (package)
6183 access, then the overriding or hiding method must not be
6184 private; otherwise, a compile-time error occurs. If
6185 `found' belongs to an interface, things have been already
6187 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6188 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6189 || (METHOD_PROTECTED (found)
6190 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6191 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6192 && METHOD_PRIVATE (method))))
6196 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6197 (METHOD_PUBLIC (method) ? "public" :
6198 (METHOD_PRIVATE (method) ? "private" : "protected")),
6199 IDENTIFIER_POINTER (DECL_NAME
6200 (TYPE_NAME (DECL_CONTEXT (found)))));
6204 /* Overriding methods must have compatible `throws' clauses on checked
6205 exceptions, if any */
6206 check_throws_clauses (method, method_wfl, found);
6208 /* Inheriting multiple methods with the same signature. FIXME */
6211 if (!TYPE_NVIRTUALS (class))
6212 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6214 /* Search for inherited abstract method not yet implemented in this
6216 java_check_abstract_method_definitions (class_decl);
6218 if (!saw_constructor)
6222 /* Return a non zero value if the `throws' clause of METHOD (if any)
6223 is incompatible with the `throws' clause of FOUND (if any). */
6226 check_throws_clauses (method, method_wfl, found)
6227 tree method, method_wfl, found;
6229 tree mthrows, fthrows;
6231 /* Can't check these things with class loaded from bytecode. FIXME */
6232 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6235 for (mthrows = DECL_FUNCTION_THROWS (method);
6236 mthrows; mthrows = TREE_CHAIN (mthrows))
6238 /* We don't verify unchecked expressions */
6239 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6241 /* Checked expression must be compatible */
6242 for (fthrows = DECL_FUNCTION_THROWS (found);
6243 fthrows; fthrows = TREE_CHAIN (fthrows))
6244 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6249 (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'",
6250 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6251 lang_printable_name (found, 0),
6253 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6258 /* Check abstract method of interface INTERFACE */
6261 java_check_abstract_methods (interface_decl)
6262 tree interface_decl;
6265 tree method, basetype_vec, found;
6266 tree interface = TREE_TYPE (interface_decl);
6268 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6270 /* 2- Check for double definition inside the defining interface */
6271 if (check_method_redefinition (interface, method))
6274 /* 3- Overriding is OK as far as we preserve the return type and
6275 the thrown exceptions (FIXME) */
6276 found = lookup_java_interface_method2 (interface, method);
6280 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6282 (DECL_FUNCTION_WFL (found),
6283 "Method `%s' was defined with return type `%s' in class `%s'",
6284 lang_printable_name (found, 0), t,
6286 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6292 /* 4- Inherited methods can't differ by their returned types */
6293 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6295 n = TREE_VEC_LENGTH (basetype_vec);
6296 for (i = 0; i < n; i++)
6298 tree sub_interface_method, sub_interface;
6299 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6302 sub_interface = BINFO_TYPE (vec_elt);
6303 for (sub_interface_method = TYPE_METHODS (sub_interface);
6304 sub_interface_method;
6305 sub_interface_method = TREE_CHAIN (sub_interface_method))
6307 found = lookup_java_interface_method2 (interface,
6308 sub_interface_method);
6309 if (found && (found != sub_interface_method))
6312 (lookup_cl (sub_interface_method),
6313 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6314 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6315 lang_printable_name (found, 0),
6317 (DECL_NAME (TYPE_NAME
6318 (DECL_CONTEXT (sub_interface_method)))),
6320 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6326 /* Lookup methods in interfaces using their name and partial
6327 signature. Return a matching method only if their types differ. */
6330 lookup_java_interface_method2 (class, method_decl)
6331 tree class, method_decl;
6334 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6339 n = TREE_VEC_LENGTH (basetype_vec);
6340 for (i = 0; i < n; i++)
6342 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6343 if ((BINFO_TYPE (vec_elt) != object_type_node)
6345 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6348 for (i = 0; i < n; i++)
6350 to_return = lookup_java_interface_method2
6351 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6359 /* Lookup method using their name and partial signature. Return a
6360 matching method only if their types differ. */
6363 lookup_java_method2 (clas, method_decl, do_interface)
6364 tree clas, method_decl;
6367 tree method, method_signature, method_name, method_type, name;
6369 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6370 name = DECL_NAME (method_decl);
6371 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6372 EXPR_WFL_NODE (name) : name);
6373 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6375 while (clas != NULL_TREE)
6377 for (method = TYPE_METHODS (clas);
6378 method != NULL_TREE; method = TREE_CHAIN (method))
6380 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6381 tree name = DECL_NAME (method);
6382 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6383 EXPR_WFL_NODE (name) : name) == method_name
6384 && method_sig == method_signature
6385 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6388 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6393 /* Return the line that matches DECL line number, and try its best to
6394 position the column number. Used during error reports. */
6400 static tree cl = NULL_TREE;
6406 if (cl == NULL_TREE)
6408 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6409 ggc_add_tree_root (&cl, 1);
6412 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6413 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6415 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
6416 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6418 found = strstr ((const char *)line,
6419 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6421 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6426 /* Look for a simple name in the single-type import list */
6429 find_name_in_single_imports (name)
6434 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6435 if (TREE_VALUE (node) == name)
6436 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6441 /* Process all single-type import. */
6449 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6451 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6452 char *original_name;
6454 obstack_grow0 (&temporary_obstack,
6455 IDENTIFIER_POINTER (to_be_found),
6456 IDENTIFIER_LENGTH (to_be_found));
6457 original_name = obstack_finish (&temporary_obstack);
6459 /* Don't load twice something already defined. */
6460 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6467 QUALIFIED_P (to_be_found) = 1;
6468 load_class (to_be_found, 0);
6470 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6472 /* We found it, we can bail out */
6473 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6476 /* We haven't found it. Maybe we're trying to access an
6477 inner class. The only way for us to know is to try again
6478 after having dropped a qualifier. If we can't break it further,
6479 we have an error. */
6480 if (breakdown_qualified (&left, NULL, to_be_found))
6485 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6487 parse_error_context (TREE_PURPOSE (import),
6488 "Class or interface `%s' not found in import",
6493 obstack_free (&temporary_obstack, original_name);
6500 /* Possibly find and mark a class imported by a single-type import
6504 find_in_imports (class_type)
6509 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6510 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6512 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6513 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6518 note_possible_classname (name, len)
6523 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6525 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6529 node = ident_subst (name, len, "", '/', '.', "");
6530 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6531 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6535 /* Read a import directory, gathering potential match for further type
6536 references. Indifferently reads a filesystem or a ZIP archive
6540 read_import_dir (wfl)
6543 tree package_id = EXPR_WFL_NODE (wfl);
6544 const char *package_name = IDENTIFIER_POINTER (package_id);
6545 int package_length = IDENTIFIER_LENGTH (package_id);
6547 JCF *saved_jcf = current_jcf;
6552 struct buffer filename[1];
6555 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6557 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6559 BUFFER_INIT (filename);
6560 buffer_grow (filename, package_length + 100);
6562 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6564 const char *entry_name = jcf_path_name (entry);
6565 int entry_length = strlen (entry_name);
6566 if (jcf_path_is_zipfile (entry))
6569 buffer_grow (filename, entry_length);
6570 memcpy (filename->data, entry_name, entry_length - 1);
6571 filename->data[entry_length-1] = '\0';
6572 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6574 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6577 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6578 BUFFER_RESET (filename);
6579 for (k = 0; k < package_length; k++)
6581 char ch = package_name[k];
6582 *filename->ptr++ = ch == '.' ? '/' : ch;
6584 *filename->ptr++ = '/';
6586 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
6588 const char *current_entry = ZIPDIR_FILENAME (zipd);
6589 int current_entry_len = zipd->filename_length;
6591 if (current_entry_len >= BUFFER_LENGTH (filename)
6592 && strncmp (filename->data, current_entry,
6593 BUFFER_LENGTH (filename)) != 0)
6595 found |= note_possible_classname (current_entry,
6602 BUFFER_RESET (filename);
6603 buffer_grow (filename, entry_length + package_length + 4);
6604 strcpy (filename->data, entry_name);
6605 filename->ptr = filename->data + entry_length;
6606 for (k = 0; k < package_length; k++)
6608 char ch = package_name[k];
6609 *filename->ptr++ = ch == '.' ? '/' : ch;
6611 *filename->ptr = '\0';
6613 dirp = opendir (filename->data);
6616 *filename->ptr++ = '/';
6621 struct dirent *direntp = readdir (dirp);
6624 d_name = direntp->d_name;
6625 len = strlen (direntp->d_name);
6626 buffer_grow (filename, len+1);
6627 strcpy (filename->ptr, d_name);
6628 found |= note_possible_classname (filename->data + entry_length,
6629 package_length+len+1);
6636 free (filename->data);
6638 /* Here we should have a unified way of retrieving an entry, to be
6642 static int first = 1;
6645 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
6650 parse_error_context (wfl, "Package `%s' not found in import",
6652 current_jcf = saved_jcf;
6655 current_jcf = saved_jcf;
6658 /* Possibly find a type in the import on demands specified
6659 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6660 entire list, to detected potential double definitions. */
6663 find_in_imports_on_demand (class_type)
6666 tree node, import, node_to_use = NULL_TREE;
6668 tree cl = NULL_TREE;
6670 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6672 const char *id_name;
6673 obstack_grow (&temporary_obstack,
6674 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6675 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6676 obstack_1grow (&temporary_obstack, '.');
6677 obstack_grow0 (&temporary_obstack,
6678 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6679 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6680 id_name = obstack_finish (&temporary_obstack);
6682 node = maybe_get_identifier (id_name);
6683 if (node && IS_A_CLASSFILE_NAME (node))
6687 cl = TREE_PURPOSE (import);
6695 (TREE_PURPOSE (import),
6696 "Type `%s' also potentially defined in package `%s'",
6697 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6698 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6705 /* Setup lineno so that it refers to the line of the import (in
6706 case we parse a class file and encounter errors */
6708 int saved_lineno = lineno;
6709 lineno = EXPR_WFL_LINENO (cl);
6710 TYPE_NAME (class_type) = node_to_use;
6711 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6712 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6713 /* If there is no DECL set for the class or if the class isn't
6714 loaded and not seen in source yet, the load */
6715 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6716 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6718 load_class (node_to_use, 0);
6719 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6721 lineno = saved_lineno;
6722 if (! INNER_CLASS_P (TREE_TYPE (decl)))
6723 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6725 /* 6.6.1: Inner classes are subject to member access rules. */
6729 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6732 /* Add package NAME to the list of package encountered so far. To
6733 speed up class lookup in do_resolve_class, we make sure a
6734 particular package is added only once. */
6737 register_package (name)
6740 static struct hash_table _pht, *pht = NULL;
6744 hash_table_init (&_pht, hash_newfunc,
6745 java_hash_hash_tree_node, java_hash_compare_tree_node);
6749 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6751 package_list = chainon (package_list, build_tree_list (name, NULL));
6752 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6757 resolve_package (pkg, next)
6761 tree type_name = NULL_TREE;
6762 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
6764 /* The trick is to determine when the package name stops and were
6765 the name of something contained in the package starts. Then we
6766 return a fully qualified name of what we want to get. */
6768 /* Do a quick search on well known package names */
6769 if (!strncmp (name, "java.lang.reflect", 17))
6772 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6773 type_name = lookup_package_type (name, 17);
6775 else if (!strncmp (name, "java.lang", 9))
6777 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6778 type_name = lookup_package_type (name, 9);
6781 /* If we found something here, return */
6785 *next = EXPR_WFL_QUALIFICATION (pkg);
6787 /* Try to progressively construct a type name */
6788 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6789 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6790 current; current = TREE_CHAIN (current))
6792 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6793 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6796 /* resolve_package should be used in a loop, hence we
6797 point at this one to naturally process the next one at
6798 the next iteration. */
6807 lookup_package_type (name, from)
6812 const char *sub = &name[from+1];
6813 while (*sub != '.' && *sub)
6815 strncpy (subname, name, sub-name);
6816 subname [sub-name] = '\0';
6817 return get_identifier (subname);
6820 /* Check accessibility of inner classes according to member access rules.
6821 DECL is the inner class, ENCLOSING_DECL is the class from which the
6822 access is being attempted. */
6825 check_inner_class_access (decl, enclosing_decl, cl)
6826 tree decl, enclosing_decl, cl;
6829 tree enclosing_decl_type;
6831 /* We don't issue an error message when CL is null. CL can be null
6832 as a result of processing a JDEP crafted by source_start_java_method
6833 for the purpose of patching its parm decl. But the error would
6834 have been already trapped when fixing the method's signature.
6835 DECL can also be NULL in case of earlier errors. */
6839 enclosing_decl_type = TREE_TYPE (enclosing_decl);
6841 if (CLASS_PRIVATE (decl))
6843 /* Access is permitted only within the body of the top-level
6844 class in which DECL is declared. */
6845 tree top_level = decl;
6846 while (DECL_CONTEXT (top_level))
6847 top_level = DECL_CONTEXT (top_level);
6848 while (DECL_CONTEXT (enclosing_decl))
6849 enclosing_decl = DECL_CONTEXT (enclosing_decl);
6850 if (top_level == enclosing_decl)
6854 else if (CLASS_PROTECTED (decl))
6857 /* Access is permitted from within the same package... */
6858 if (in_same_package (decl, enclosing_decl))
6861 /* ... or from within the body of a subtype of the context in which
6862 DECL is declared. */
6863 decl_context = DECL_CONTEXT (decl);
6864 while (enclosing_decl)
6866 if (CLASS_INTERFACE (decl))
6868 if (interface_of_p (TREE_TYPE (decl_context),
6869 enclosing_decl_type))
6874 /* Eww. The order of the arguments is different!! */
6875 if (inherits_from_p (enclosing_decl_type,
6876 TREE_TYPE (decl_context)))
6879 enclosing_decl = DECL_CONTEXT (enclosing_decl);
6881 access = "protected";
6883 else if (! CLASS_PUBLIC (decl))
6885 /* Access is permitted only from within the same package as DECL. */
6886 if (in_same_package (decl, enclosing_decl))
6888 access = "non-public";
6891 /* Class is public. */
6894 parse_error_context (cl, "Nested %s %s is %s; cannot be accessed from here",
6895 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6896 lang_printable_name (decl, 0), access);
6899 /* Accessibility check for top-level classes. If CLASS_NAME is in a foreign
6900 package, it must be PUBLIC. Return 0 if no access violations were found,
6904 check_pkg_class_access (class_name, cl)
6910 if (!IDENTIFIER_CLASS_VALUE (class_name))
6913 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6916 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6918 /* Access to a private class within the same package is
6921 breakdown_qualified (&l, &r, class_name);
6922 if (!QUALIFIED_P (class_name) && !ctxp->package)
6923 /* Both in the empty package. */
6925 if (l == ctxp->package)
6926 /* Both in the same package. */
6930 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
6931 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6932 IDENTIFIER_POINTER (class_name));
6938 /* Local variable declaration. */
6941 declare_local_variables (modifier, type, vlist)
6946 tree decl, current, saved_type;
6947 tree type_wfl = NULL_TREE;
6951 /* Push a new block if statements were seen between the last time we
6952 pushed a block and now. Keep a count of blocks to close */
6953 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
6955 tree b = enter_block ();
6956 BLOCK_IS_IMPLICIT (b) = 1;
6962 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
6963 if (modifier == ACC_FINAL)
6968 (ctxp->modifier_ctx [i],
6969 "Only `final' is allowed as a local variables modifier");
6974 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6975 hold the TYPE value if a new incomplete has to be created (as
6976 opposed to being found already existing and reused). */
6977 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6979 /* If TYPE is fully resolved and we don't have a reference, make one */
6980 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6982 /* Go through all the declared variables */
6983 for (current = vlist, saved_type = type; current;
6984 current = TREE_CHAIN (current), type = saved_type)
6986 tree other, real_type;
6987 tree wfl = TREE_PURPOSE (current);
6988 tree name = EXPR_WFL_NODE (wfl);
6989 tree init = TREE_VALUE (current);
6991 /* Process NAME, as it may specify extra dimension(s) for it */
6992 type = build_array_from_name (type, type_wfl, name, &name);
6994 /* Variable redefinition check */
6995 if ((other = lookup_name_in_blocks (name)))
6997 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6998 DECL_SOURCE_LINE (other));
7002 /* Type adjustment. We may have just readjusted TYPE because
7003 the variable specified more dimensions. Make sure we have
7004 a reference if we can and don't have one already. */
7005 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7007 real_type = GET_REAL_TYPE (type);
7008 /* Never layout this decl. This will be done when its scope
7010 decl = build_decl (VAR_DECL, name, real_type);
7011 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7012 LOCAL_FINAL (decl) = final_p;
7013 BLOCK_CHAIN_DECL (decl);
7015 /* If doing xreferencing, replace the line number with the WFL
7018 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7020 /* Don't try to use an INIT statement when an error was found */
7021 if (init && java_error_count)
7024 /* Add the initialization function to the current function's code */
7027 /* Name might have been readjusted */
7028 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7029 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7030 java_method_add_stmt (current_function_decl,
7031 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7035 /* Setup dependency the type of the decl */
7039 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7040 dep = CLASSD_LAST (ctxp->classd_list);
7041 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7044 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7047 /* Called during parsing. Build decls from argument list. */
7050 source_start_java_method (fndecl)
7060 current_function_decl = fndecl;
7062 /* New scope for the function */
7064 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7065 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7067 tree type = TREE_VALUE (tem);
7068 tree name = TREE_PURPOSE (tem);
7070 /* If type is incomplete. Create an incomplete decl and ask for
7071 the decl to be patched later */
7072 if (INCOMPLETE_TYPE_P (type))
7075 tree real_type = GET_REAL_TYPE (type);
7076 parm_decl = build_decl (PARM_DECL, name, real_type);
7077 type = obtain_incomplete_type (type);
7078 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7079 jdep = CLASSD_LAST (ctxp->classd_list);
7080 JDEP_MISC (jdep) = name;
7081 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7084 parm_decl = build_decl (PARM_DECL, name, type);
7086 /* Remember if a local variable was declared final (via its
7087 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7088 if (ARG_FINAL_P (tem))
7090 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
7091 LOCAL_FINAL (parm_decl) = 1;
7094 BLOCK_CHAIN_DECL (parm_decl);
7096 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7097 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7099 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7100 DECL_MAX_LOCALS (current_function_decl) = i;
7103 /* Called during parsing. Creates an artificial method declaration. */
7106 create_artificial_method (class, flags, type, name, args)
7109 tree type, name, args;
7113 java_parser_context_save_global ();
7115 mdecl = make_node (FUNCTION_TYPE);
7116 TREE_TYPE (mdecl) = type;
7117 TYPE_ARG_TYPES (mdecl) = args;
7118 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7119 java_parser_context_restore_global ();
7120 DECL_ARTIFICIAL (mdecl) = 1;
7124 /* Starts the body if an artifical method. */
7127 start_artificial_method_body (mdecl)
7130 DECL_SOURCE_LINE (mdecl) = 1;
7131 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7132 source_start_java_method (mdecl);
7137 end_artificial_method_body (mdecl)
7140 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7141 It has to be evaluated first. (if mdecl is current_function_decl,
7142 we have an undefined behavior if no temporary variable is used.) */
7143 tree b = exit_block ();
7144 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7148 /* Terminate a function and expand its body. */
7151 source_end_java_method ()
7153 tree fndecl = current_function_decl;
7158 java_parser_context_save_global ();
7159 lineno = ctxp->last_ccb_indent1;
7161 /* Turn function bodies with only a NOP expr null, so they don't get
7162 generated at all and we won't get warnings when using the -W
7164 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7165 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7167 /* Generate function's code */
7168 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7169 && ! flag_emit_class_files
7170 && ! flag_emit_xref)
7171 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7173 /* pop out of its parameters */
7174 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7176 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7178 /* Generate rtl for function exit. */
7179 if (! flag_emit_class_files && ! flag_emit_xref)
7181 lineno = DECL_SOURCE_LINE_LAST (fndecl);
7182 expand_function_end (input_filename, lineno, 0);
7184 /* Run the optimizers and output assembler code for this function. */
7185 rest_of_compilation (fndecl);
7188 current_function_decl = NULL_TREE;
7189 java_parser_context_restore_global ();
7192 /* Record EXPR in the current function block. Complements compound
7193 expression second operand if necessary. */
7196 java_method_add_stmt (fndecl, expr)
7199 if (!GET_CURRENT_BLOCK (fndecl))
7201 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7205 add_stmt_to_block (b, type, stmt)
7208 tree body = BLOCK_EXPR_BODY (b), c;
7210 if (java_error_count)
7213 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7216 BLOCK_EXPR_BODY (b) = c;
7217 TREE_SIDE_EFFECTS (c) = 1;
7221 /* Add STMT to EXISTING if possible, otherwise create a new
7222 COMPOUND_EXPR and add STMT to it. */
7225 add_stmt_to_compound (existing, type, stmt)
7226 tree existing, type, stmt;
7229 return build (COMPOUND_EXPR, type, existing, stmt);
7234 void java_layout_seen_class_methods ()
7236 tree previous_list = all_class_list;
7237 tree end = NULL_TREE;
7242 for (current = previous_list;
7243 current != end; current = TREE_CHAIN (current))
7244 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7246 if (previous_list != all_class_list)
7248 end = previous_list;
7249 previous_list = all_class_list;
7257 java_reorder_fields ()
7259 static tree stop_reordering = NULL_TREE;
7260 static int initialized_p;
7263 /* Register STOP_REORDERING with the garbage collector. */
7266 ggc_add_tree_root (&stop_reordering, 1);
7270 for (current = gclass_list; current; current = TREE_CHAIN (current))
7272 current_class = TREE_TYPE (TREE_VALUE (current));
7274 if (current_class == stop_reordering)
7277 /* Reverse the fields, but leave the dummy field in front.
7278 Fields are already ordered for Object and Class */
7279 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7280 && current_class != class_type_node)
7282 /* If the dummy field is there, reverse the right fields and
7283 just layout the type for proper fields offset */
7284 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7286 tree fields = TYPE_FIELDS (current_class);
7287 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7288 TYPE_SIZE (current_class) = NULL_TREE;
7290 /* We don't have a dummy field, we need to layout the class,
7291 after having reversed the fields */
7294 TYPE_FIELDS (current_class) =
7295 nreverse (TYPE_FIELDS (current_class));
7296 TYPE_SIZE (current_class) = NULL_TREE;
7300 stop_reordering = TREE_TYPE (TREE_VALUE (gclass_list));
7303 /* Layout the methods of all classes loaded in one way or another.
7304 Check methods of source parsed classes. Then reorder the
7305 fields and layout the classes or the type of all source parsed
7309 java_layout_classes ()
7312 int save_error_count = java_error_count;
7314 /* Layout the methods of all classes seen so far */
7315 java_layout_seen_class_methods ();
7316 java_parse_abort_on_error ();
7317 all_class_list = NULL_TREE;
7319 /* Then check the methods of all parsed classes */
7320 for (current = gclass_list; current; current = TREE_CHAIN (current))
7321 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7322 java_check_methods (TREE_VALUE (current));
7323 java_parse_abort_on_error ();
7325 for (current = gclass_list; current; current = TREE_CHAIN (current))
7327 current_class = TREE_TYPE (TREE_VALUE (current));
7328 layout_class (current_class);
7330 /* Error reported by the caller */
7331 if (java_error_count)
7335 /* We might have reloaded classes durign the process of laying out
7336 classes for code generation. We must layout the methods of those
7337 late additions, as constructor checks might use them */
7338 java_layout_seen_class_methods ();
7339 java_parse_abort_on_error ();
7342 /* Expand methods in the current set of classes rememebered for
7346 java_complete_expand_classes ()
7350 do_not_fold = flag_emit_xref;
7352 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7353 if (!INNER_CLASS_DECL_P (current))
7354 java_complete_expand_class (current);
7357 /* Expand the methods found in OUTER, starting first by OUTER's inner
7361 java_complete_expand_class (outer)
7366 set_nested_class_simple_name_value (outer, 1); /* Set */
7368 /* We need to go after all inner classes and start expanding them,
7369 starting with most nested ones. We have to do that because nested
7370 classes might add functions to outer classes */
7372 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7373 inner_list; inner_list = TREE_CHAIN (inner_list))
7374 java_complete_expand_class (TREE_PURPOSE (inner_list));
7376 java_complete_expand_methods (outer);
7377 set_nested_class_simple_name_value (outer, 0); /* Reset */
7380 /* Expand methods registered in CLASS_DECL. The general idea is that
7381 we expand regular methods first. This allows us get an estimate on
7382 how outer context local alias fields are really used so we can add
7383 to the constructor just enough code to initialize them properly (it
7384 also lets us generate finit$ correctly.) Then we expand the
7385 constructors and then <clinit>. */
7388 java_complete_expand_methods (class_decl)
7391 tree clinit, finit, decl, first_decl;
7393 current_class = TREE_TYPE (class_decl);
7395 /* Find whether the class has final variables */
7396 for (decl = TYPE_FIELDS (current_class); decl; decl = TREE_CHAIN (decl))
7397 if (FIELD_FINAL (decl))
7399 TYPE_HAS_FINAL_VARIABLE (current_class) = 1;
7403 /* Initialize a new constant pool */
7404 init_outgoing_cpool ();
7406 /* Pre-expand <clinit> to figure whether we really need it or
7407 not. If we do need it, we pre-expand the static fields so they're
7408 ready to be used somewhere else. <clinit> will be fully expanded
7409 after we processed the constructors. */
7410 first_decl = TYPE_METHODS (current_class);
7411 clinit = maybe_generate_pre_expand_clinit (current_class);
7413 /* Then generate finit$ (if we need to) because constructor will
7415 if (TYPE_FINIT_STMT_LIST (current_class))
7417 finit = generate_finit (current_class);
7418 java_complete_expand_method (finit);
7421 /* Now do the constructors */
7422 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7426 if (!DECL_CONSTRUCTOR_P (decl))
7429 no_body = !DECL_FUNCTION_BODY (decl);
7430 /* Don't generate debug info on line zero when expanding a
7431 generated constructor. */
7433 restore_line_number_status (1);
7435 /* Reset the final local variable assignment flags */
7436 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7437 reset_final_variable_local_assignment_flag (current_class);
7439 java_complete_expand_method (decl);
7441 /* Check for missed out final variable assignment */
7442 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7443 check_final_variable_local_assignment_flag (current_class, decl);
7446 restore_line_number_status (0);
7449 /* First, do the ordinary methods. */
7450 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7452 /* Skip abstract or native methods -- but do handle native
7453 methods when generating JNI stubs. */
7454 if (METHOD_ABSTRACT (decl)
7455 || (! flag_jni && METHOD_NATIVE (decl))
7456 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7459 if (METHOD_NATIVE (decl))
7461 tree body = build_jni_stub (decl);
7462 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7465 java_complete_expand_method (decl);
7468 /* If there is indeed a <clinit>, fully expand it now */
7471 /* Reset the final local variable assignment flags */
7472 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7473 reset_static_final_variable_assignment_flag (current_class);
7474 /* Prevent the use of `this' inside <clinit> */
7475 ctxp->explicit_constructor_p = 1;
7476 java_complete_expand_method (clinit);
7477 ctxp->explicit_constructor_p = 0;
7478 /* Check for missed out static final variable assignment */
7479 if (TYPE_HAS_FINAL_VARIABLE (current_class)
7480 && !CLASS_INTERFACE (class_decl))
7481 check_static_final_variable_assignment_flag (current_class);
7484 /* We might have generated a class$ that we now want to expand */
7485 if (TYPE_DOT_CLASS (current_class))
7486 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7488 /* Now verify constructor circularity (stop after the first one we
7490 if (!CLASS_INTERFACE (class_decl))
7491 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7492 if (DECL_CONSTRUCTOR_P (decl)
7493 && verify_constructor_circularity (decl, decl))
7496 /* Final check on the initialization of final variables. */
7497 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7499 check_final_variable_global_assignment_flag (current_class);
7500 /* If we have an interface, check for uninitialized fields. */
7501 if (CLASS_INTERFACE (class_decl))
7502 check_static_final_variable_assignment_flag (current_class);
7505 /* Save the constant pool. We'll need to restore it later. */
7506 TYPE_CPOOL (current_class) = outgoing_cpool;
7509 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7510 safely used in some other methods/constructors. */
7513 maybe_generate_pre_expand_clinit (class_type)
7516 tree current, mdecl;
7518 if (!TYPE_CLINIT_STMT_LIST (class_type))
7521 /* Go through all static fields and pre expand them */
7522 for (current = TYPE_FIELDS (class_type); current;
7523 current = TREE_CHAIN (current))
7524 if (FIELD_STATIC (current))
7525 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7527 /* Then build the <clinit> method */
7528 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7529 clinit_identifier_node, end_params_node);
7530 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7532 start_artificial_method_body (mdecl);
7534 /* We process the list of assignment we produced as the result of
7535 the declaration of initialized static field and add them as
7536 statement to the <clinit> method. */
7537 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7538 current = TREE_CHAIN (current))
7540 tree stmt = current;
7541 /* We build the assignment expression that will initialize the
7542 field to its value. There are strict rules on static
7543 initializers (8.5). FIXME */
7544 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7545 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7546 java_method_add_stmt (mdecl, stmt);
7549 end_artificial_method_body (mdecl);
7551 /* Now we want to place <clinit> as the last method (because we need
7552 it at least for interface so that it doesn't interfere with the
7553 dispatch table based lookup. */
7554 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7556 current = TREE_CHAIN (TYPE_METHODS (class_type));
7557 TYPE_METHODS (class_type) = current;
7559 while (TREE_CHAIN (current))
7560 current = TREE_CHAIN (current);
7562 TREE_CHAIN (current) = mdecl;
7563 TREE_CHAIN (mdecl) = NULL_TREE;
7569 /* Analyzes a method body and look for something that isn't a
7570 MODIFY_EXPR with a constant value. */
7573 analyze_clinit_body (bbody)
7577 switch (TREE_CODE (bbody))
7580 bbody = BLOCK_EXPR_BODY (bbody);
7583 case EXPR_WITH_FILE_LOCATION:
7584 bbody = EXPR_WFL_NODE (bbody);
7588 if (analyze_clinit_body (TREE_OPERAND (bbody, 0)))
7590 bbody = TREE_OPERAND (bbody, 1);
7594 /* Return 0 if the operand is constant, 1 otherwise. */
7595 return ! TREE_CONSTANT (TREE_OPERAND (bbody, 1));
7604 /* See whether we could get rid of <clinit>. Criteria are: all static
7605 final fields have constant initial values and the body of <clinit>
7606 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7609 maybe_yank_clinit (mdecl)
7616 if (!DECL_CLINIT_P (mdecl))
7619 /* If the body isn't empty, then we keep <clinit>. Note that if
7620 we're emitting classfiles, this isn't enough not to rule it
7622 fbody = DECL_FUNCTION_BODY (mdecl);
7623 bbody = BLOCK_EXPR_BODY (fbody);
7624 if (bbody && bbody != error_mark_node)
7625 bbody = BLOCK_EXPR_BODY (bbody);
7628 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7631 type = DECL_CONTEXT (mdecl);
7632 current = TYPE_FIELDS (type);
7634 for (current = (current ? TREE_CHAIN (current) : current);
7635 current; current = TREE_CHAIN (current))
7639 /* We're not interested in non static field */
7640 if (!FIELD_STATIC (current))
7643 /* Anything that isn't String or a basic type is ruled out -- or
7644 if we know how to deal with it (when doing things natively) we
7645 should generated an empty <clinit> so that SUID are computed
7647 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7648 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7651 f_init = DECL_INITIAL (current);
7652 /* If we're emitting native code, we want static final fields to
7653 have constant initializers. If we don't meet these
7654 conditions, we keep <clinit> */
7655 if (!flag_emit_class_files
7656 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7658 /* If we're emitting bytecode, we want static fields to have
7659 constant initializers or no initializer. If we don't meet
7660 these conditions, we keep <clinit> */
7661 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7665 /* Now we analyze the method body and look for something that
7666 isn't a MODIFY_EXPR */
7667 if (bbody == empty_stmt_node)
7670 found = analyze_clinit_body (bbody);
7672 if (current || found)
7675 /* Get rid of <clinit> in the class' list of methods */
7676 if (TYPE_METHODS (type) == mdecl)
7677 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7679 for (current = TYPE_METHODS (type); current;
7680 current = TREE_CHAIN (current))
7681 if (TREE_CHAIN (current) == mdecl)
7683 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7691 /* Complete and expand a method. */
7694 java_complete_expand_method (mdecl)
7697 int yank_clinit = 0;
7699 current_function_decl = mdecl;
7700 /* Fix constructors before expanding them */
7701 if (DECL_CONSTRUCTOR_P (mdecl))
7702 fix_constructors (mdecl);
7704 /* Expand functions that have a body */
7705 if (DECL_FUNCTION_BODY (mdecl))
7707 tree fbody = DECL_FUNCTION_BODY (mdecl);
7708 tree block_body = BLOCK_EXPR_BODY (fbody);
7709 tree exception_copy = NULL_TREE;
7712 current_function_decl = mdecl;
7715 fprintf (stderr, " [%s.",
7716 lang_printable_name (DECL_CONTEXT (mdecl), 0));
7717 announce_function (mdecl);
7719 fprintf (stderr, "]");
7721 pushlevel (1); /* Prepare for a parameter push */
7722 ptr = &DECL_ARGUMENTS (mdecl);
7723 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7726 tree next = TREE_CHAIN (tem);
7727 tree type = TREE_TYPE (tem);
7728 if (PROMOTE_PROTOTYPES
7729 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7730 && INTEGRAL_TYPE_P (type))
7731 type = integer_type_node;
7732 DECL_ARG_TYPE (tem) = type;
7733 layout_decl (tem, 0);
7736 ptr = &TREE_CHAIN (tem);
7740 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7741 lineno = DECL_SOURCE_LINE_FIRST (mdecl);
7743 build_result_decl (mdecl);
7746 = (!METHOD_STATIC (mdecl) ?
7747 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7749 /* Purge the `throws' list of unchecked exceptions. If we're
7750 doing xref, save a copy of the list and re-install it
7753 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7755 purge_unchecked_exceptions (mdecl);
7757 /* Install exceptions thrown with `throws' */
7758 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7760 if (block_body != NULL_TREE)
7762 block_body = java_complete_tree (block_body);
7764 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
7765 check_for_initialization (block_body);
7766 ctxp->explicit_constructor_p = 0;
7769 BLOCK_EXPR_BODY (fbody) = block_body;
7771 /* If we saw a return but couldn't evaluate it properly, we'll
7772 have an error_mark_node here. */
7773 if (block_body != error_mark_node
7774 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
7775 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7777 missing_return_error (current_function_decl);
7779 /* Check wether we could just get rid of clinit, now the picture
7781 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7782 complete_start_java_method (mdecl);
7784 /* Don't go any further if we've found error(s) during the
7786 if (!java_error_count && !yank_clinit)
7787 source_end_java_method ();
7790 if (java_error_count)
7791 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7795 /* Pop the exceptions and sanity check */
7797 if (currently_caught_type_list)
7801 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
7807 /* This section of the code deals with accessing enclosing context
7808 fields either directly by using the relevant access to this$<n> or
7809 by invoking an access method crafted for that purpose. */
7811 /* Build the necessary access from an inner class to an outer
7812 class. This routine could be optimized to cache previous result
7813 (decl, current_class and returned access). When an access method
7814 needs to be generated, it always takes the form of a read. It might
7815 be later turned into a write by calling outer_field_access_fix. */
7818 build_outer_field_access (id, decl)
7821 tree access = NULL_TREE;
7822 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7823 tree decl_ctx = DECL_CONTEXT (decl);
7825 /* If the immediate enclosing context of the current class is the
7826 field decl's class or inherits from it; build the access as
7827 `this$<n>.<field>'. Note that we will break the `private' barrier
7828 if we're not emitting bytecodes. */
7829 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
7830 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7832 tree thisn = build_current_thisn (current_class);
7833 access = make_qualified_primary (build_wfl_node (thisn),
7834 id, EXPR_WFL_LINECOL (id));
7836 /* Otherwise, generate access methods to outer this and access the
7837 field (either using an access method or by direct access.) */
7840 int lc = EXPR_WFL_LINECOL (id);
7842 /* Now we chain the required number of calls to the access$0 to
7843 get a hold to the enclosing instance we need, and then we
7844 build the field access. */
7845 access = build_access_to_thisn (current_class, decl_ctx, lc);
7847 /* If the field is private and we're generating bytecode, then
7848 we generate an access method */
7849 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7851 tree name = build_outer_field_access_methods (decl);
7852 access = build_outer_field_access_expr (lc, decl_ctx,
7853 name, access, NULL_TREE);
7855 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7856 Once again we break the `private' access rule from a foreign
7859 access = make_qualified_primary (access, id, lc);
7861 return resolve_expression_name (access, NULL);
7864 /* Return a non zero value if NODE describes an outer field inner
7868 outer_field_access_p (type, decl)
7871 if (!INNER_CLASS_TYPE_P (type)
7872 || TREE_CODE (decl) != FIELD_DECL
7873 || DECL_CONTEXT (decl) == type)
7876 /* If the inner class extends the declaration context of the field
7877 we're try to acces, then this isn't an outer field access */
7878 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7881 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7882 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7884 if (type == DECL_CONTEXT (decl))
7887 if (!DECL_CONTEXT (TYPE_NAME (type)))
7889 /* Before we give up, see whether the field is inherited from
7890 the enclosing context we're considering. */
7891 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7900 /* Return a non zero value if NODE represents an outer field inner
7901 access that was been already expanded. As a side effect, it returns
7902 the name of the field being accessed and the argument passed to the
7903 access function, suitable for a regeneration of the access method
7904 call if necessary. */
7907 outer_field_expanded_access_p (node, name, arg_type, arg)
7908 tree node, *name, *arg_type, *arg;
7912 if (TREE_CODE (node) != CALL_EXPR)
7915 /* Well, gcj generates slightly different tree nodes when compiling
7916 to native or bytecodes. It's the case for function calls. */
7918 if (flag_emit_class_files
7919 && TREE_CODE (node) == CALL_EXPR
7920 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7922 else if (!flag_emit_class_files)
7924 node = TREE_OPERAND (node, 0);
7926 if (node && TREE_OPERAND (node, 0)
7927 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7929 node = TREE_OPERAND (node, 0);
7930 if (TREE_OPERAND (node, 0)
7931 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7932 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7933 (DECL_NAME (TREE_OPERAND (node, 0)))))
7938 if (identified && name && arg_type && arg)
7940 tree argument = TREE_OPERAND (node, 1);
7941 *name = DECL_NAME (TREE_OPERAND (node, 0));
7942 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7943 *arg = TREE_VALUE (argument);
7948 /* Detect in NODE an outer field read access from an inner class and
7949 transform it into a write with RHS as an argument. This function is
7950 called from the java_complete_lhs when an assignment to a LHS can
7954 outer_field_access_fix (wfl, node, rhs)
7955 tree wfl, node, rhs;
7957 tree name, arg_type, arg;
7959 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7961 /* At any rate, check whether we're trying to assign a value to
7963 tree accessed = (JDECL_P (node) ? node :
7964 (TREE_CODE (node) == COMPONENT_REF ?
7965 TREE_OPERAND (node, 1) : node));
7966 if (check_final_assignment (accessed, wfl))
7967 return error_mark_node;
7969 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7970 arg_type, name, arg, rhs);
7971 return java_complete_tree (node);
7976 /* Construct the expression that calls an access method:
7977 <type>.access$<n>(<arg1> [, <arg2>]);
7979 ARG2 can be NULL and will be omitted in that case. It will denote a
7983 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7985 tree type, access_method_name, arg1, arg2;
7987 tree args, cn, access;
7989 args = arg1 ? arg1 :
7990 build_wfl_node (build_current_thisn (current_class));
7991 args = build_tree_list (NULL_TREE, args);
7994 args = tree_cons (NULL_TREE, arg2, args);
7996 access = build_method_invocation (build_wfl_node (access_method_name), args);
7997 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7998 return make_qualified_primary (cn, access, lc);
8002 build_new_access_id ()
8004 static int access_n_counter = 1;
8007 sprintf (buffer, "access$%d", access_n_counter++);
8008 return get_identifier (buffer);
8011 /* Create the static access functions for the outer field DECL. We define a
8013 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
8017 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
8018 TREE_TYPE (<field>) value$) {
8019 return inst$.field = value$;
8021 We should have a usage flags on the DECL so we can lazily turn the ones
8022 we're using for code generation. FIXME.
8026 build_outer_field_access_methods (decl)
8029 tree id, args, stmt, mdecl;
8031 if (FIELD_INNER_ACCESS_P (decl))
8032 return FIELD_INNER_ACCESS (decl);
8034 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
8036 /* Create the identifier and a function named after it. */
8037 id = build_new_access_id ();
8039 /* The identifier is marked as bearing the name of a generated write
8040 access function for outer field accessed from inner classes. */
8041 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8043 /* Create the read access */
8044 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8045 TREE_CHAIN (args) = end_params_node;
8046 stmt = make_qualified_primary (build_wfl_node (inst_id),
8047 build_wfl_node (DECL_NAME (decl)), 0);
8048 stmt = build_return (0, stmt);
8049 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8050 TREE_TYPE (decl), id, args, stmt);
8051 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8053 /* Create the write access method. No write access for final variable */
8054 if (!FIELD_FINAL (decl))
8056 args = build_tree_list (inst_id,
8057 build_pointer_type (DECL_CONTEXT (decl)));
8058 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8059 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8060 stmt = make_qualified_primary (build_wfl_node (inst_id),
8061 build_wfl_node (DECL_NAME (decl)), 0);
8062 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8063 build_wfl_node (wpv_id)));
8064 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8065 TREE_TYPE (decl), id,
8068 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8070 /* Return the access name */
8071 return FIELD_INNER_ACCESS (decl) = id;
8074 /* Build an field access method NAME. */
8077 build_outer_field_access_method (class, type, name, args, body)
8078 tree class, type, name, args, body;
8080 tree saved_current_function_decl, mdecl;
8082 /* Create the method */
8083 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8084 fix_method_argument_names (args, mdecl);
8085 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8087 /* Attach the method body. */
8088 saved_current_function_decl = current_function_decl;
8089 start_artificial_method_body (mdecl);
8090 java_method_add_stmt (mdecl, body);
8091 end_artificial_method_body (mdecl);
8092 current_function_decl = saved_current_function_decl;
8098 /* This section deals with building access function necessary for
8099 certain kinds of method invocation from inner classes. */
8102 build_outer_method_access_method (decl)
8105 tree saved_current_function_decl, mdecl;
8106 tree args = NULL_TREE, call_args = NULL_TREE;
8107 tree carg, id, body, class;
8109 int parm_id_count = 0;
8111 /* Test this abort with an access to a private field */
8112 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8115 /* Check the cache first */
8116 if (DECL_FUNCTION_INNER_ACCESS (decl))
8117 return DECL_FUNCTION_INNER_ACCESS (decl);
8119 class = DECL_CONTEXT (decl);
8121 /* Obtain an access identifier and mark it */
8122 id = build_new_access_id ();
8123 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8125 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8126 /* Create the arguments, as much as the original */
8127 for (; carg && carg != end_params_node;
8128 carg = TREE_CHAIN (carg))
8130 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8131 args = chainon (args, build_tree_list (get_identifier (buffer),
8132 TREE_VALUE (carg)));
8134 args = chainon (args, end_params_node);
8136 /* Create the method */
8137 mdecl = create_artificial_method (class, ACC_STATIC,
8138 TREE_TYPE (TREE_TYPE (decl)), id, args);
8139 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8140 /* There is a potential bug here. We should be able to use
8141 fix_method_argument_names, but then arg names get mixed up and
8142 eventually a constructor will have its this$0 altered and the
8143 outer context won't be assignment properly. The test case is
8145 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8147 /* Attach the method body. */
8148 saved_current_function_decl = current_function_decl;
8149 start_artificial_method_body (mdecl);
8151 /* The actual method invocation uses the same args. When invoking a
8152 static methods that way, we don't want to skip the first
8155 if (!METHOD_STATIC (decl))
8156 carg = TREE_CHAIN (carg);
8157 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8158 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8161 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8163 if (!METHOD_STATIC (decl))
8164 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8166 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8167 body = build_return (0, body);
8168 java_method_add_stmt (mdecl,body);
8169 end_artificial_method_body (mdecl);
8170 current_function_decl = saved_current_function_decl;
8172 /* Back tag the access function so it know what it accesses */
8173 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8175 /* Tag the current method so it knows it has an access generated */
8176 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8180 /* This section of the code deals with building expressions to access
8181 the enclosing instance of an inner class. The enclosing instance is
8182 kept in a generated field called this$<n>, with <n> being the
8183 inner class nesting level (starting from 0.) */
8185 /* Build an access to a given this$<n>, always chaining access call to
8186 others. Access methods to this$<n> are build on the fly if
8187 necessary. This CAN'T be used to solely access this$<n-1> from
8188 this$<n> (which alway yield to special cases and optimization, see
8189 for example build_outer_field_access). */
8192 build_access_to_thisn (from, to, lc)
8196 tree access = NULL_TREE;
8202 access = build_current_thisn (from);
8203 access = build_wfl_node (access);
8207 tree access0_wfl, cn;
8209 maybe_build_thisn_access_method (from);
8210 access0_wfl = build_wfl_node (access0_identifier_node);
8211 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8212 EXPR_WFL_LINECOL (access0_wfl) = lc;
8213 access = build_tree_list (NULL_TREE, access);
8214 access = build_method_invocation (access0_wfl, access);
8215 access = make_qualified_primary (cn, access, lc);
8218 /* if FROM isn't an inter class, that's fine, we've done
8219 enough. What we're looking for can be accessed from there. */
8220 from = DECL_CONTEXT (TYPE_NAME (from));
8223 from = TREE_TYPE (from);
8228 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8229 is returned if nothing needs to be generated. Otherwise, the method
8230 generated and a method decl is returned.
8232 NOTE: These generated methods should be declared in a class file
8233 attribute so that they can't be referred to directly. */
8236 maybe_build_thisn_access_method (type)
8239 tree mdecl, args, stmt, rtype;
8240 tree saved_current_function_decl;
8242 /* If TYPE is a top-level class, no access method is required.
8243 If there already is such an access method, bail out. */
8244 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8247 /* We generate the method. The method looks like:
8248 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8250 args = build_tree_list (inst_id, build_pointer_type (type));
8251 TREE_CHAIN (args) = end_params_node;
8252 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8253 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8254 access0_identifier_node, args);
8255 fix_method_argument_names (args, mdecl);
8256 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8257 stmt = build_current_thisn (type);
8258 stmt = make_qualified_primary (build_wfl_node (inst_id),
8259 build_wfl_node (stmt), 0);
8260 stmt = build_return (0, stmt);
8262 saved_current_function_decl = current_function_decl;
8263 start_artificial_method_body (mdecl);
8264 java_method_add_stmt (mdecl, stmt);
8265 end_artificial_method_body (mdecl);
8266 current_function_decl = saved_current_function_decl;
8268 CLASS_ACCESS0_GENERATED_P (type) = 1;
8273 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8274 the first level of innerclassing. this$1 for the next one, etc...
8275 This function can be invoked with TYPE to NULL, available and then
8276 has to count the parser context. */
8279 build_current_thisn (type)
8282 static int saved_i = -1;
8283 static tree saved_thisn = NULL_TREE;
8284 static tree saved_type = NULL_TREE;
8285 static int saved_type_i = 0;
8286 static int initialized_p;
8291 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8294 ggc_add_tree_root (&saved_thisn, 1);
8295 ggc_add_tree_root (&saved_type, 1);
8301 if (type == saved_type)
8305 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8306 decl; decl = DECL_CONTEXT (decl), i++)
8314 i = list_length (GET_CPC_LIST ())-2;
8319 sprintf (buffer, "this$%d", i);
8321 saved_thisn = get_identifier (buffer);
8325 /* Return the assignement to the hidden enclosing context `this$<n>'
8326 by the second incoming parameter to the innerclass constructor. The
8327 form used is `this.this$<n> = this$<n>;'. */
8330 build_thisn_assign ()
8332 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8334 tree thisn = build_current_thisn (current_class);
8335 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8336 build_wfl_node (thisn), 0);
8337 tree rhs = build_wfl_node (thisn);
8338 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8339 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8345 /* Building the synthetic `class$' used to implement the `.class' 1.1
8346 extension for non primitive types. This method looks like:
8348 static Class class$(String type) throws NoClassDefFoundError
8350 try {return (java.lang.Class.forName (String));}
8351 catch (ClassNotFoundException e) {
8352 throw new NoClassDefFoundError(e.getMessage());}
8356 build_dot_class_method (class)
8359 #define BWF(S) build_wfl_node (get_identifier ((S)))
8360 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8361 tree args, tmp, saved_current_function_decl, mdecl;
8362 tree stmt, throw_stmt, catch, catch_block, try_block;
8363 tree catch_clause_param;
8364 tree class_not_found_exception, no_class_def_found_error;
8366 static tree get_message_wfl, type_parm_wfl;
8368 if (!get_message_wfl)
8370 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8371 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8372 ggc_add_tree_root (&get_message_wfl, 1);
8373 ggc_add_tree_root (&type_parm_wfl, 1);
8376 /* Build the arguments */
8377 args = build_tree_list (get_identifier ("type$"),
8378 build_pointer_type (string_type_node));
8379 TREE_CHAIN (args) = end_params_node;
8381 /* Build the qualified name java.lang.Class.forName */
8382 tmp = MQN (MQN (MQN (BWF ("java"),
8383 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8385 /* For things we have to catch and throw */
8386 class_not_found_exception =
8387 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8388 no_class_def_found_error =
8389 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8390 load_class (class_not_found_exception, 1);
8391 load_class (no_class_def_found_error, 1);
8393 /* Create the "class$" function */
8394 mdecl = create_artificial_method (class, ACC_STATIC,
8395 build_pointer_type (class_type_node),
8396 classdollar_identifier_node, args);
8397 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8398 no_class_def_found_error);
8400 /* We start by building the try block. We need to build:
8401 return (java.lang.Class.forName (type)); */
8402 stmt = build_method_invocation (tmp,
8403 build_tree_list (NULL_TREE, type_parm_wfl));
8404 stmt = build_return (0, stmt);
8405 /* Put it in a block. That's the try block */
8406 try_block = build_expr_block (stmt, NULL_TREE);
8408 /* Now onto the catch block. We start by building the expression
8409 throwing a new exception:
8410 throw new NoClassDefFoundError (_.getMessage); */
8411 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8412 get_message_wfl, 0);
8413 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8415 /* Build new NoClassDefFoundError (_.getMessage) */
8416 throw_stmt = build_new_invocation
8417 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8418 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8420 /* Build the throw, (it's too early to use BUILD_THROW) */
8421 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8423 /* Build the catch block to encapsulate all this. We begin by
8424 building an decl for the catch clause parameter and link it to
8425 newly created block, the catch block. */
8426 catch_clause_param =
8427 build_decl (VAR_DECL, wpv_id,
8428 build_pointer_type (class_not_found_exception));
8429 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8431 /* We initialize the variable with the exception handler. */
8432 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8433 build (JAVA_EXC_OBJ_EXPR, ptr_type_node));
8434 add_stmt_to_block (catch_block, NULL_TREE, catch);
8436 /* We add the statement throwing the new exception */
8437 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8439 /* Build a catch expression for all this */
8440 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8442 /* Build the try/catch sequence */
8443 stmt = build_try_statement (0, try_block, catch_block);
8445 fix_method_argument_names (args, mdecl);
8446 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8447 saved_current_function_decl = current_function_decl;
8448 start_artificial_method_body (mdecl);
8449 java_method_add_stmt (mdecl, stmt);
8450 end_artificial_method_body (mdecl);
8451 current_function_decl = saved_current_function_decl;
8452 TYPE_DOT_CLASS (class) = mdecl;
8458 build_dot_class_method_invocation (type)
8463 if (TYPE_ARRAY_P (type))
8464 sig_id = build_java_signature (type);
8466 sig_id = DECL_NAME (TYPE_NAME (type));
8468 s = build_string (IDENTIFIER_LENGTH (sig_id),
8469 IDENTIFIER_POINTER (sig_id));
8470 return build_method_invocation (build_wfl_node (classdollar_identifier_node),
8471 build_tree_list (NULL_TREE, s));
8474 /* This section of the code deals with constructor. */
8476 /* Craft a body for default constructor. Patch existing constructor
8477 bodies with call to super() and field initialization statements if
8481 fix_constructors (mdecl)
8484 tree body = DECL_FUNCTION_BODY (mdecl);
8485 tree thisn_assign, compound = NULL_TREE;
8486 tree class_type = DECL_CONTEXT (mdecl);
8488 if (DECL_FIXED_CONSTRUCTOR_P (mdecl))
8490 DECL_FIXED_CONSTRUCTOR_P (mdecl) = 1;
8494 /* It is an error for the compiler to generate a default
8495 constructor if the superclass doesn't have a constructor that
8496 takes no argument, or the same args for an anonymous class */
8497 if (verify_constructor_super (mdecl))
8499 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8500 tree save = DECL_NAME (mdecl);
8501 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8502 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8504 (lookup_cl (TYPE_NAME (class_type)),
8505 "No constructor matching `%s' found in class `%s'",
8506 lang_printable_name (mdecl, 0), n);
8507 DECL_NAME (mdecl) = save;
8510 /* The constructor body must be crafted by hand. It's the
8511 constructor we defined when we realize we didn't have the
8512 CLASSNAME() constructor */
8513 start_artificial_method_body (mdecl);
8515 /* Insert an assignment to the this$<n> hidden field, if
8517 if ((thisn_assign = build_thisn_assign ()))
8518 java_method_add_stmt (mdecl, thisn_assign);
8520 /* We don't generate a super constructor invocation if we're
8521 compiling java.lang.Object. build_super_invocation takes care
8523 java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8525 /* Insert the instance initializer block right here, after the
8526 super invocation. */
8527 add_instance_initializer (mdecl);
8529 end_artificial_method_body (mdecl);
8531 /* Search for an explicit constructor invocation */
8535 tree found_call = NULL_TREE;
8536 tree main_block = BLOCK_EXPR_BODY (body);
8537 tree ii; /* Instance Initializer */
8540 switch (TREE_CODE (body))
8543 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8547 case EXPR_WITH_FILE_LOCATION:
8549 body = TREE_OPERAND (body, 0);
8553 body = BLOCK_EXPR_BODY (body);
8560 /* Generate the assignment to this$<n>, if necessary */
8561 if ((thisn_assign = build_thisn_assign ()))
8562 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8564 /* The constructor is missing an invocation of super() */
8566 compound = add_stmt_to_compound (compound, NULL_TREE,
8567 build_super_invocation (mdecl));
8568 /* Explicit super() invokation should take place before the
8569 instance initializer blocks. */
8572 compound = add_stmt_to_compound (compound, NULL_TREE,
8573 TREE_OPERAND (found_call, 0));
8574 TREE_OPERAND (found_call, 0) = empty_stmt_node;
8577 /* Insert the instance initializer block right after. */
8578 if ((ii = build_instance_initializer (mdecl)))
8579 compound = add_stmt_to_compound (compound, NULL_TREE, ii);
8581 /* Fix the constructor main block if we're adding extra stmts */
8584 compound = add_stmt_to_compound (compound, NULL_TREE,
8585 BLOCK_EXPR_BODY (main_block));
8586 BLOCK_EXPR_BODY (main_block) = compound;
8591 /* Browse constructors in the super class, searching for a constructor
8592 that doesn't take any argument. Return 0 if one is found, 1
8593 otherwise. If the current class is an anonymous inner class, look
8594 for something that has the same signature. */
8597 verify_constructor_super (mdecl)
8600 tree class = CLASSTYPE_SUPER (current_class);
8601 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8607 if (ANONYMOUS_CLASS_P (current_class))
8609 tree mdecl_arg_type;
8610 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8611 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8612 if (DECL_CONSTRUCTOR_P (sdecl))
8615 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8617 arg_type = TREE_CHAIN (arg_type);
8618 for (m_arg_type = mdecl_arg_type;
8619 (arg_type != end_params_node
8620 && m_arg_type != end_params_node);
8621 arg_type = TREE_CHAIN (arg_type),
8622 m_arg_type = TREE_CHAIN (m_arg_type))
8623 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
8626 if (arg_type == end_params_node && m_arg_type == end_params_node)
8632 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8634 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8636 arg = TREE_CHAIN (arg);
8637 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8644 /* Generate code for all context remembered for code generation. */
8647 java_expand_classes ()
8649 int save_error_count = 0;
8650 static struct parser_ctxt *cur_ctxp = NULL;
8652 java_parse_abort_on_error ();
8653 if (!(ctxp = ctxp_for_generation))
8655 java_layout_classes ();
8656 java_parse_abort_on_error ();
8658 cur_ctxp = ctxp_for_generation;
8659 for (; cur_ctxp; cur_ctxp = cur_ctxp->next)
8662 input_filename = ctxp->filename;
8663 lang_init_source (2); /* Error msgs have method prototypes */
8664 java_complete_expand_classes (); /* Complete and expand classes */
8665 java_parse_abort_on_error ();
8667 input_filename = main_input_filename;
8669 /* Find anonymous classes and expand their constructor, now they
8671 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
8675 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8677 current_class = TREE_TYPE (current);
8678 if (ANONYMOUS_CLASS_P (current_class))
8681 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8683 if (DECL_CONSTRUCTOR_P (d))
8685 restore_line_number_status (1);
8686 java_complete_expand_method (d);
8687 restore_line_number_status (0);
8688 break; /* We now there are no other ones */
8695 /* If we've found error at that stage, don't try to generate
8696 anything, unless we're emitting xrefs or checking the syntax only
8697 (but not using -fsyntax-only for the purpose of generating
8699 if (java_error_count && !flag_emit_xref
8700 && (!flag_syntax_only && !flag_emit_class_files))
8703 /* Now things are stable, go for generation of the class data. */
8704 for (cur_ctxp = ctxp_for_generation; cur_ctxp; cur_ctxp = cur_ctxp->next)
8708 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8710 current_class = TREE_TYPE (current);
8711 outgoing_cpool = TYPE_CPOOL (current_class);
8712 if (flag_emit_class_files)
8713 write_classfile (current_class);
8715 expand_xref (current_class);
8716 else if (! flag_syntax_only)
8722 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8723 a tree list node containing RIGHT. Fore coming RIGHTs will be
8724 chained to this hook. LOCATION contains the location of the
8725 separating `.' operator. */
8728 make_qualified_primary (primary, right, location)
8729 tree primary, right;
8734 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8735 wfl = build_wfl_wrap (primary, location);
8739 /* If wfl wasn't qualified, we build a first anchor */
8740 if (!EXPR_WFL_QUALIFICATION (wfl))
8741 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
8744 /* And chain them */
8745 EXPR_WFL_LINECOL (right) = location;
8746 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8747 PRIMARY_P (wfl) = 1;
8751 /* Simple merge of two name separated by a `.' */
8754 merge_qualified_name (left, right)
8758 if (!left && !right)
8767 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8768 IDENTIFIER_LENGTH (left));
8769 obstack_1grow (&temporary_obstack, '.');
8770 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8771 IDENTIFIER_LENGTH (right));
8772 node = get_identifier (obstack_base (&temporary_obstack));
8773 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8774 QUALIFIED_P (node) = 1;
8778 /* Merge the two parts of a qualified name into LEFT. Set the
8779 location information of the resulting node to LOCATION, usually
8780 inherited from the location information of the `.' operator. */
8783 make_qualified_name (left, right, location)
8787 #ifdef USE_COMPONENT_REF
8788 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8789 EXPR_WFL_LINECOL (node) = location;
8792 tree left_id = EXPR_WFL_NODE (left);
8793 tree right_id = EXPR_WFL_NODE (right);
8796 merge = merge_qualified_name (left_id, right_id);
8798 /* Left wasn't qualified and is now qualified */
8799 if (!QUALIFIED_P (left_id))
8801 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8802 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8803 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8806 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8807 EXPR_WFL_LINECOL (wfl) = location;
8808 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8810 EXPR_WFL_NODE (left) = merge;
8815 /* Extract the last identifier component of the qualified in WFL. The
8816 last identifier is removed from the linked list */
8819 cut_identifier_in_qualified (wfl)
8823 tree previous = NULL_TREE;
8824 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8825 if (!TREE_CHAIN (q))
8828 /* Operating on a non qualified qualified WFL. */
8831 TREE_CHAIN (previous) = NULL_TREE;
8832 return TREE_PURPOSE (q);
8836 /* Resolve the expression name NAME. Return its decl. */
8839 resolve_expression_name (id, orig)
8843 tree name = EXPR_WFL_NODE (id);
8846 /* 6.5.5.1: Simple expression names */
8847 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8849 /* 15.13.1: NAME can appear within the scope of a local variable
8851 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8854 /* 15.13.1: NAME can appear within a class declaration */
8857 decl = lookup_field_wrapper (current_class, name);
8860 tree access = NULL_TREE;
8861 int fs = FIELD_STATIC (decl);
8863 /* If we're accessing an outer scope local alias, make
8864 sure we change the name of the field we're going to
8866 if (FIELD_LOCAL_ALIAS_USED (decl))
8867 name = DECL_NAME (decl);
8869 /* Instance variable (8.3.1.1) can't appear within
8870 static method, static initializer or initializer for
8871 a static variable. */
8872 if (!fs && METHOD_STATIC (current_function_decl))
8874 static_ref_err (id, name, current_class);
8875 return error_mark_node;
8877 /* Instance variables can't appear as an argument of
8878 an explicit constructor invocation */
8879 if (!fs && ctxp->explicit_constructor_p
8880 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
8883 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
8884 return error_mark_node;
8887 /* If we're processing an inner class and we're trying
8888 to access a field belonging to an outer class, build
8889 the access to the field */
8890 if (!fs && outer_field_access_p (current_class, decl))
8892 if (CLASS_STATIC (TYPE_NAME (current_class)))
8894 static_ref_err (id, DECL_NAME (decl), current_class);
8895 return error_mark_node;
8897 return build_outer_field_access (id, decl);
8900 /* Otherwise build what it takes to access the field */
8901 access = build_field_ref ((fs ? NULL_TREE : current_this),
8902 DECL_CONTEXT (decl), name);
8904 access = maybe_build_class_init_for_field (decl, access);
8905 /* We may be asked to save the real field access node */
8908 /* And we return what we got */
8911 /* Fall down to error report on undefined variable */
8914 /* 6.5.5.2 Qualified Expression Names */
8919 qualify_ambiguous_name (id);
8920 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8921 /* 15.10.2: Accessing Superclass Members using super */
8922 return resolve_field_access (id, orig, NULL);
8925 /* We've got an error here */
8926 parse_error_context (id, "Undefined variable `%s'",
8927 IDENTIFIER_POINTER (name));
8929 return error_mark_node;
8933 static_ref_err (wfl, field_id, class_type)
8934 tree wfl, field_id, class_type;
8938 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8939 IDENTIFIER_POINTER (field_id),
8940 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8943 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8944 We return something suitable to generate the field access. We also
8945 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8946 recipient's address can be null. */
8949 resolve_field_access (qual_wfl, field_decl, field_type)
8951 tree *field_decl, *field_type;
8955 tree decl, where_found, type_found;
8957 if (resolve_qualified_expression_name (qual_wfl, &decl,
8958 &where_found, &type_found))
8959 return error_mark_node;
8961 /* Resolve the LENGTH field of an array here */
8962 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
8963 && type_found && TYPE_ARRAY_P (type_found)
8964 && ! flag_emit_class_files && ! flag_emit_xref)
8966 tree length = build_java_array_length_access (where_found);
8969 /* In case we're dealing with a static array, we need to
8970 initialize its class before the array length can be fetched.
8971 It's also a good time to create a DECL_RTL for the field if
8972 none already exists, otherwise if the field was declared in a
8973 class found in an external file and hasn't been (and won't
8974 be) accessed for its value, none will be created. */
8975 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8977 build_static_field_ref (where_found);
8978 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8981 /* We might have been trying to resolve field.method(). In which
8982 case, the resolution is over and decl is the answer */
8983 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
8985 else if (JDECL_P (decl))
8988 type_found = DECL_CONTEXT (decl);
8989 is_static = FIELD_STATIC (decl);
8990 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8991 NULL_TREE : where_found),
8992 type_found, DECL_NAME (decl));
8993 if (field_ref == error_mark_node)
8994 return error_mark_node;
8996 field_ref = maybe_build_class_init_for_field (decl, field_ref);
9004 *field_type = (QUAL_DECL_TYPE (decl) ?
9005 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
9009 /* If NODE is an access to f static field, strip out the class
9010 initialization part and return the field decl, otherwise, return
9014 strip_out_static_field_access_decl (node)
9017 if (TREE_CODE (node) == COMPOUND_EXPR)
9019 tree op1 = TREE_OPERAND (node, 1);
9020 if (TREE_CODE (op1) == COMPOUND_EXPR)
9022 tree call = TREE_OPERAND (op1, 0);
9023 if (TREE_CODE (call) == CALL_EXPR
9024 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
9025 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
9026 == soft_initclass_node)
9027 return TREE_OPERAND (op1, 1);
9029 else if (JDECL_P (op1))
9035 /* 6.5.5.2: Qualified Expression Names */
9038 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
9040 tree *found_decl, *type_found, *where_found;
9042 int from_type = 0; /* Field search initiated from a type */
9043 int from_super = 0, from_cast = 0, from_qualified_this = 0;
9044 int previous_call_static = 0;
9046 tree decl = NULL_TREE, type = NULL_TREE, q;
9047 /* For certain for of inner class instantiation */
9048 tree saved_current, saved_this;
9049 #define RESTORE_THIS_AND_CURRENT_CLASS \
9050 { current_class = saved_current; current_this = saved_this;}
9052 *type_found = *where_found = NULL_TREE;
9054 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
9056 tree qual_wfl = QUAL_WFL (q);
9057 tree ret_decl; /* for EH checking */
9058 int location; /* for EH checking */
9060 /* 15.10.1 Field Access Using a Primary */
9061 switch (TREE_CODE (qual_wfl))
9064 case NEW_CLASS_EXPR:
9065 /* If the access to the function call is a non static field,
9066 build the code to access it. */
9067 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9069 decl = maybe_access_field (decl, *where_found,
9070 DECL_CONTEXT (decl));
9071 if (decl == error_mark_node)
9075 /* And code for the function call */
9076 if (complete_function_arguments (qual_wfl))
9079 /* We might have to setup a new current class and a new this
9080 for the search of an inner class, relative to the type of
9081 a expression resolved as `decl'. The current values are
9082 saved and restored shortly after */
9083 saved_current = current_class;
9084 saved_this = current_this;
9086 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9087 || from_qualified_this))
9089 /* If we still have `from_qualified_this', we have the form
9090 <T>.this.f() and we need to build <T>.this */
9091 if (from_qualified_this)
9093 decl = build_access_to_thisn (current_class, type, 0);
9094 decl = java_complete_tree (decl);
9095 type = TREE_TYPE (TREE_TYPE (decl));
9097 current_class = type;
9098 current_this = decl;
9099 from_qualified_this = 0;
9102 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9103 CALL_USING_SUPER (qual_wfl) = 1;
9104 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9105 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9106 *where_found = patch_method_invocation (qual_wfl, decl, type,
9108 &is_static, &ret_decl);
9109 if (*where_found == error_mark_node)
9111 RESTORE_THIS_AND_CURRENT_CLASS;
9114 *type_found = type = QUAL_DECL_TYPE (*where_found);
9116 /* If we're creating an inner class instance, check for that
9117 an enclosing instance is in scope */
9118 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9119 && INNER_ENCLOSING_SCOPE_CHECK (type))
9122 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9123 lang_printable_name (type, 0),
9124 (!current_this ? "" :
9125 "; an explicit one must be provided when creating this inner class"));
9126 RESTORE_THIS_AND_CURRENT_CLASS;
9130 /* In case we had to change then to resolve a inner class
9131 instantiation using a primary qualified by a `new' */
9132 RESTORE_THIS_AND_CURRENT_CLASS;
9134 /* EH check. No check on access$<n> functions */
9136 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9137 (DECL_NAME (current_function_decl)))
9138 check_thrown_exceptions (location, ret_decl);
9140 /* If the previous call was static and this one is too,
9141 build a compound expression to hold the two (because in
9142 that case, previous function calls aren't transported as
9143 forcoming function's argument. */
9144 if (previous_call_static && is_static)
9146 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9147 TREE_SIDE_EFFECTS (decl) = 1;
9151 previous_call_static = is_static;
9152 decl = *where_found;
9157 case NEW_ARRAY_EXPR:
9158 case NEW_ANONYMOUS_ARRAY_EXPR:
9159 *where_found = decl = java_complete_tree (qual_wfl);
9160 if (decl == error_mark_node)
9162 *type_found = type = QUAL_DECL_TYPE (decl);
9166 *where_found = decl = java_complete_tree (qual_wfl);
9167 if (decl == error_mark_node)
9169 *type_found = type = QUAL_DECL_TYPE (decl);
9173 case CONDITIONAL_EXPR:
9176 *where_found = decl = java_complete_tree (qual_wfl);
9177 if (decl == error_mark_node)
9179 *type_found = type = QUAL_DECL_TYPE (decl);
9183 /* If the access to the function call is a non static field,
9184 build the code to access it. */
9185 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9187 decl = maybe_access_field (decl, *where_found, type);
9188 if (decl == error_mark_node)
9191 /* And code for the array reference expression */
9192 decl = java_complete_tree (qual_wfl);
9193 if (decl == error_mark_node)
9195 type = QUAL_DECL_TYPE (decl);
9199 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9201 if ((type = patch_string (decl)))
9203 *where_found = QUAL_RESOLUTION (q) = decl;
9204 *type_found = type = TREE_TYPE (decl);
9208 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9210 *where_found = QUAL_RESOLUTION (q) = decl;
9211 *type_found = type = TREE_TYPE (decl);
9215 /* Fix for -Wall Just go to the next statement. Don't
9220 /* If we fall here, we weren't processing a (static) function call. */
9221 previous_call_static = 0;
9223 /* It can be the keyword THIS */
9224 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9229 (wfl, "Keyword `this' used outside allowed context");
9232 if (ctxp->explicit_constructor_p
9233 && type == current_class)
9235 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9238 /* We have to generate code for intermediate acess */
9239 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9241 *where_found = decl = current_this;
9242 *type_found = type = QUAL_DECL_TYPE (decl);
9244 /* We're trying to access the this from somewhere else. Make sure
9245 it's allowed before doing so. */
9248 if (!enclosing_context_p (type, current_class))
9250 char *p = xstrdup (lang_printable_name (type, 0));
9251 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9253 lang_printable_name (current_class, 0));
9257 from_qualified_this = 1;
9258 /* If there's nothing else after that, we need to
9259 produce something now, otherwise, the section of the
9260 code that needs to produce <T>.this will generate
9261 what is necessary. */
9262 if (!TREE_CHAIN (q))
9264 decl = build_access_to_thisn (current_class, type, 0);
9265 *where_found = decl = java_complete_tree (decl);
9266 *type_found = type = TREE_TYPE (decl);
9274 /* 15.10.2 Accessing Superclass Members using SUPER */
9275 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9278 /* Check on the restricted use of SUPER */
9279 if (METHOD_STATIC (current_function_decl)
9280 || current_class == object_type_node)
9283 (wfl, "Keyword `super' used outside allowed context");
9286 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9287 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9288 CLASSTYPE_SUPER (current_class),
9289 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9290 *where_found = decl = java_complete_tree (node);
9291 if (decl == error_mark_node)
9293 *type_found = type = QUAL_DECL_TYPE (decl);
9294 from_super = from_type = 1;
9298 /* 15.13.1: Can't search for field name in packages, so we
9299 assume a variable/class name was meant. */
9300 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9302 tree name = resolve_package (wfl, &q);
9306 *where_found = decl = resolve_no_layout (name, qual_wfl);
9307 /* We want to be absolutely sure that the class is laid
9308 out. We're going to search something inside it. */
9309 *type_found = type = TREE_TYPE (decl);
9310 layout_class (type);
9313 /* Fix them all the way down, if any are left. */
9316 list = TREE_CHAIN (q);
9319 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9320 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9321 list = TREE_CHAIN (list);
9327 if (from_super || from_cast)
9329 ((from_cast ? qual_wfl : wfl),
9330 "No variable `%s' defined in class `%s'",
9331 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9332 lang_printable_name (type, 0));
9335 (qual_wfl, "Undefined variable or class name: `%s'",
9336 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9341 /* We have a type name. It's been already resolved when the
9342 expression was qualified. */
9343 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9345 if (!(decl = QUAL_RESOLUTION (q)))
9346 return 1; /* Error reported already */
9348 /* Sneak preview. If next we see a `new', we're facing a
9349 qualification with resulted in a type being selected
9350 instead of a field. Report the error */
9352 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9354 parse_error_context (qual_wfl, "Undefined variable `%s'",
9355 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9359 if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
9362 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9363 java_accstring_lookup (get_access_flags_from_decl (decl)),
9364 GET_TYPE_NAME (type),
9365 IDENTIFIER_POINTER (DECL_NAME (decl)),
9366 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9369 check_deprecation (qual_wfl, decl);
9371 type = TREE_TYPE (decl);
9374 /* We resolve and expression name */
9377 tree field_decl = NULL_TREE;
9379 /* If there exists an early resolution, use it. That occurs
9380 only once and we know that there are more things to
9381 come. Don't do that when processing something after SUPER
9382 (we need more thing to be put in place below */
9383 if (!from_super && QUAL_RESOLUTION (q))
9385 decl = QUAL_RESOLUTION (q);
9388 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9391 *where_found = current_this;
9394 static_ref_err (qual_wfl, DECL_NAME (decl),
9398 if (outer_field_access_p (current_class, decl))
9399 decl = build_outer_field_access (qual_wfl, decl);
9403 *where_found = TREE_TYPE (decl);
9404 if (TREE_CODE (*where_found) == POINTER_TYPE)
9405 *where_found = TREE_TYPE (*where_found);
9410 /* We have to search for a field, knowing the type of its
9411 container. The flag FROM_TYPE indicates that we resolved
9412 the last member of the expression as a type name, which
9413 means that for the resolution of this field, we'll look
9414 for other errors than if it was resolved as a member of
9419 tree field_decl_type; /* For layout */
9421 if (!from_type && !JREFERENCE_TYPE_P (type))
9424 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9425 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9426 lang_printable_name (type, 0),
9427 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9431 field_decl = lookup_field_wrapper (type,
9432 EXPR_WFL_NODE (qual_wfl));
9434 /* Maybe what we're trying to access to is an inner
9435 class, only if decl is a TYPE_DECL. */
9436 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
9438 tree ptr, inner_decl;
9440 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9441 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9444 check_inner_class_access (inner_decl, decl, qual_wfl);
9445 type = TREE_TYPE (inner_decl);
9452 if (field_decl == NULL_TREE)
9455 (qual_wfl, "No variable `%s' defined in type `%s'",
9456 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9457 GET_TYPE_NAME (type));
9460 if (field_decl == error_mark_node)
9463 /* Layout the type of field_decl, since we may need
9464 it. Don't do primitive types or loaded classes. The
9465 situation of non primitive arrays may not handled
9466 properly here. FIXME */
9467 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9468 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9470 field_decl_type = TREE_TYPE (field_decl);
9471 if (!JPRIMITIVE_TYPE_P (field_decl_type)
9472 && !CLASS_LOADED_P (field_decl_type)
9473 && !TYPE_ARRAY_P (field_decl_type))
9474 resolve_and_layout (field_decl_type, NULL_TREE);
9476 /* Check on accessibility here */
9477 if (not_accessible_p (current_class, field_decl,
9478 DECL_CONTEXT (field_decl), from_super))
9482 "Can't access %s field `%s.%s' from `%s'",
9483 java_accstring_lookup
9484 (get_access_flags_from_decl (field_decl)),
9485 GET_TYPE_NAME (type),
9486 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9488 (DECL_NAME (TYPE_NAME (current_class))));
9491 check_deprecation (qual_wfl, field_decl);
9493 /* There are things to check when fields are accessed
9494 from type. There are no restrictions on a static
9495 declaration of the field when it is accessed from an
9497 is_static = FIELD_STATIC (field_decl);
9498 if (!from_super && from_type
9499 && !TYPE_INTERFACE_P (type)
9501 && (current_function_decl
9502 && METHOD_STATIC (current_function_decl)))
9504 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9507 from_cast = from_super = 0;
9509 /* It's an access from a type but it isn't static, we
9510 make it relative to `this'. */
9511 if (!is_static && from_type)
9512 decl = current_this;
9514 /* If we need to generate something to get a proper
9515 handle on what this field is accessed from, do it
9519 decl = maybe_access_field (decl, *where_found, *type_found);
9520 if (decl == error_mark_node)
9524 /* We want to keep the location were found it, and the type
9526 *where_found = decl;
9529 /* Generate the correct expression for field access from
9531 if (from_qualified_this)
9533 field_decl = build_outer_field_access (qual_wfl, field_decl);
9534 from_qualified_this = 0;
9537 /* This is the decl found and eventually the next one to
9542 type = QUAL_DECL_TYPE (decl);
9544 /* Sneak preview. If decl is qualified by a `new', report
9545 the error here to be accurate on the peculiar construct */
9547 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9548 && !JREFERENCE_TYPE_P (type))
9550 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9551 lang_printable_name (type, 0));
9555 /* `q' might have changed due to a after package resolution
9564 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9565 can't be accessed from REFERENCE (a record type). If MEMBER
9566 features a protected access, we then use WHERE which, if non null,
9567 holds the type of MEMBER's access that is checked against
9568 6.6.2.1. This function should be used when decl is a field or a
9572 not_accessible_p (reference, member, where, from_super)
9573 tree reference, member;
9577 int access_flag = get_access_flags_from_decl (member);
9579 /* Inner classes are processed by check_inner_class_access */
9580 if (INNER_CLASS_TYPE_P (reference))
9583 /* Access always granted for members declared public */
9584 if (access_flag & ACC_PUBLIC)
9587 /* Check access on protected members */
9588 if (access_flag & ACC_PROTECTED)
9590 /* Access granted if it occurs from within the package
9591 containing the class in which the protected member is
9593 if (class_in_current_package (DECL_CONTEXT (member)))
9596 /* If accessed with the form `super.member', then access is granted */
9600 /* If where is active, access was made through a
9601 qualifier. Access is granted if the type of the qualifier is
9602 or is a sublass of the type the access made from (6.6.2.1.) */
9603 if (where && !inherits_from_p (reference, where))
9606 /* Otherwise, access is granted if occuring from the class where
9607 member is declared or a subclass of it. Find the right
9608 context to perform the check */
9609 if (PURE_INNER_CLASS_TYPE_P (reference))
9611 while (INNER_CLASS_TYPE_P (reference))
9613 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9615 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9618 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9623 /* Check access on private members. Access is granted only if it
9624 occurs from within the class in which it is declared -- that does
9625 it for innerclasses too. */
9626 if (access_flag & ACC_PRIVATE)
9628 if (reference == DECL_CONTEXT (member))
9630 if (enclosing_context_p (reference, DECL_CONTEXT (member)))
9635 /* Default access are permitted only when occuring within the
9636 package in which the type (REFERENCE) is declared. In other words,
9637 REFERENCE is defined in the current package */
9639 return !class_in_current_package (reference);
9641 /* Otherwise, access is granted */
9645 /* Test deprecated decl access. */
9647 check_deprecation (wfl, decl)
9650 const char *file = DECL_SOURCE_FILE (decl);
9651 /* Complain if the field is deprecated and the file it was defined
9652 in isn't compiled at the same time the file which contains its
9654 if (DECL_DEPRECATED (decl)
9655 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9658 switch (TREE_CODE (decl))
9661 strcpy (the, "method");
9664 strcpy (the, "field");
9667 strcpy (the, "class");
9672 parse_warning_context
9673 (wfl, "The %s `%s' in class `%s' has been deprecated",
9674 the, lang_printable_name (decl, 0),
9675 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9679 /* Returns 1 if class was declared in the current package, 0 otherwise */
9682 class_in_current_package (class)
9685 static tree cache = NULL_TREE;
9692 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9694 /* If the current package is empty and the name of CLASS is
9695 qualified, class isn't in the current package. If there is a
9696 current package and the name of the CLASS is not qualified, class
9697 isn't in the current package */
9698 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
9701 /* If there is not package and the name of CLASS isn't qualified,
9702 they belong to the same unnamed package */
9703 if (!ctxp->package && !qualified_flag)
9706 /* Compare the left part of the name of CLASS with the package name */
9707 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9708 if (ctxp->package == left)
9710 static int initialized_p;
9711 /* Register CACHE with the garbage collector. */
9714 ggc_add_tree_root (&cache, 1);
9724 /* This function may generate code to access DECL from WHERE. This is
9725 done only if certain conditions meet. */
9728 maybe_access_field (decl, where, type)
9729 tree decl, where, type;
9731 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9732 && !FIELD_STATIC (decl))
9733 decl = build_field_ref (where ? where : current_this,
9734 (type ? type : DECL_CONTEXT (decl)),
9739 /* Build a method invocation, by patching PATCH. If non NULL
9740 and according to the situation, PRIMARY and WHERE may be
9741 used. IS_STATIC is set to 1 if the invoked function is static. */
9744 patch_method_invocation (patch, primary, where, from_super,
9745 is_static, ret_decl)
9746 tree patch, primary, where;
9751 tree wfl = TREE_OPERAND (patch, 0);
9752 tree args = TREE_OPERAND (patch, 1);
9753 tree name = EXPR_WFL_NODE (wfl);
9755 int is_static_flag = 0;
9756 int is_super_init = 0;
9757 tree this_arg = NULL_TREE;
9758 int is_array_clone_call = 0;
9760 /* Should be overriden if everything goes well. Otherwise, if
9761 something fails, it should keep this value. It stop the
9762 evaluation of a bogus assignment. See java_complete_tree,
9763 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9764 evaluating an assignment */
9765 TREE_TYPE (patch) = error_mark_node;
9767 /* Since lookup functions are messing with line numbers, save the
9769 java_parser_context_save_global ();
9771 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9773 /* Resolution of qualified name, excluding constructors */
9774 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9776 tree identifier, identifier_wfl, type, resolved;
9777 /* Extract the last IDENTIFIER of the qualified
9778 expression. This is a wfl and we will use it's location
9779 data during error report. */
9780 identifier_wfl = cut_identifier_in_qualified (wfl);
9781 identifier = EXPR_WFL_NODE (identifier_wfl);
9783 /* Given the context, IDENTIFIER is syntactically qualified
9784 as a MethodName. We need to qualify what's before */
9785 qualify_ambiguous_name (wfl);
9786 resolved = resolve_field_access (wfl, NULL, NULL);
9788 if (resolved == error_mark_node)
9789 PATCH_METHOD_RETURN_ERROR ();
9791 type = GET_SKIP_TYPE (resolved);
9792 resolve_and_layout (type, NULL_TREE);
9794 if (JPRIMITIVE_TYPE_P (type))
9798 "Can't invoke a method on primitive type `%s'",
9799 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9800 PATCH_METHOD_RETURN_ERROR ();
9803 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9804 args = nreverse (args);
9806 /* We're resolving a call from a type */
9807 if (TREE_CODE (resolved) == TYPE_DECL)
9809 if (CLASS_INTERFACE (resolved))
9813 "Can't make static reference to method `%s' in interface `%s'",
9814 IDENTIFIER_POINTER (identifier),
9815 IDENTIFIER_POINTER (name));
9816 PATCH_METHOD_RETURN_ERROR ();
9818 if (list && !METHOD_STATIC (list))
9820 char *fct_name = xstrdup (lang_printable_name (list, 0));
9823 "Can't make static reference to method `%s %s' in class `%s'",
9824 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9825 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9827 PATCH_METHOD_RETURN_ERROR ();
9831 this_arg = primary = resolved;
9833 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
9834 is_array_clone_call = 1;
9836 /* IDENTIFIER_WFL will be used to report any problem further */
9837 wfl = identifier_wfl;
9839 /* Resolution of simple names, names generated after a primary: or
9843 tree class_to_search = NULL_TREE;
9844 int lc; /* Looking for Constructor */
9846 /* We search constructor in their target class */
9847 if (CALL_CONSTRUCTOR_P (patch))
9849 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9850 class_to_search = EXPR_WFL_NODE (wfl);
9851 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9852 this_identifier_node)
9853 class_to_search = NULL_TREE;
9854 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9855 super_identifier_node)
9858 if (CLASSTYPE_SUPER (current_class))
9860 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9863 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
9864 PATCH_METHOD_RETURN_ERROR ();
9868 /* Class to search is NULL if we're searching the current one */
9869 if (class_to_search)
9871 class_to_search = resolve_and_layout (class_to_search, wfl);
9873 if (!class_to_search)
9876 (wfl, "Class `%s' not found in type declaration",
9877 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9878 PATCH_METHOD_RETURN_ERROR ();
9881 /* Can't instantiate an abstract class, but we can
9882 invoke it's constructor. It's use within the `new'
9883 context is denied here. */
9884 if (CLASS_ABSTRACT (class_to_search)
9885 && TREE_CODE (patch) == NEW_CLASS_EXPR)
9888 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9889 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9890 PATCH_METHOD_RETURN_ERROR ();
9893 class_to_search = TREE_TYPE (class_to_search);
9896 class_to_search = current_class;
9899 /* This is a regular search in the local class, unless an
9900 alternate class is specified. */
9903 if (where != NULL_TREE)
9904 class_to_search = where;
9905 else if (QUALIFIED_P (name))
9906 class_to_search = current_class;
9909 class_to_search = current_class;
9913 if (has_method (class_to_search, name))
9915 if (! INNER_CLASS_TYPE_P (class_to_search))
9917 parse_error_context (wfl,
9918 "No method named `%s' in scope",
9919 IDENTIFIER_POINTER (name));
9920 PATCH_METHOD_RETURN_ERROR ();
9923 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_to_search)));
9929 /* NAME is a simple identifier or comes from a primary. Search
9930 in the class whose declaration contain the method being
9932 resolve_and_layout (class_to_search, NULL_TREE);
9934 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
9935 /* Don't continue if no method were found, as the next statement
9936 can't be executed then. */
9938 PATCH_METHOD_RETURN_ERROR ();
9940 if (TYPE_ARRAY_P (class_to_search)
9941 && DECL_NAME (list) == get_identifier ("clone"))
9942 is_array_clone_call = 1;
9944 /* Check for static reference if non static methods */
9945 if (check_for_static_method_reference (wfl, patch, list,
9946 class_to_search, primary))
9947 PATCH_METHOD_RETURN_ERROR ();
9949 /* Check for inner classes creation from illegal contexts */
9950 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9951 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9952 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9955 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9956 lang_printable_name (class_to_search, 0),
9957 (!current_this ? "" :
9958 "; an explicit one must be provided when creating this inner class"));
9959 PATCH_METHOD_RETURN_ERROR ();
9962 /* Non static methods are called with the current object extra
9963 argument. If patch a `new TYPE()', the argument is the value
9964 returned by the object allocator. If method is resolved as a
9965 primary, use the primary otherwise use the current THIS. */
9966 args = nreverse (args);
9967 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
9969 this_arg = primary ? primary : current_this;
9971 /* If we're using an access method, things are different.
9972 There are two familly of cases:
9974 1) We're not generating bytecodes:
9976 - LIST is non static. It's invocation is transformed from
9977 x(a1,...,an) into this$<n>.x(a1,....an).
9978 - LIST is static. It's invocation is transformed from
9979 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9981 2) We're generating bytecodes:
9983 - LIST is non static. It's invocation is transformed from
9984 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9985 - LIST is static. It's invocation is transformed from
9986 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9988 Of course, this$<n> can be abitrary complex, ranging from
9989 this$0 (the immediate outer context) to
9990 access$0(access$0(...(this$0))).
9992 maybe_use_access_method returns a non zero value if the
9993 this_arg has to be moved into the (then generated) stub
9994 argument list. In the meantime, the selected function
9995 might have be replaced by a generated stub. */
9996 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9998 args = tree_cons (NULL_TREE, this_arg, args);
9999 this_arg = NULL_TREE; /* So it doesn't get chained twice */
10004 /* Merge point of all resolution schemes. If we have nothing, this
10005 is an error, already signaled */
10007 PATCH_METHOD_RETURN_ERROR ();
10009 /* Check accessibility, position the is_static flag, build and
10011 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
10012 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
10013 NULL_TREE), from_super)
10014 /* Calls to clone() on array types are permitted as a special-case. */
10015 && !is_array_clone_call)
10017 const char *fct_name = IDENTIFIER_POINTER (DECL_NAME (list));
10018 const char *access =
10019 java_accstring_lookup (get_access_flags_from_decl (list));
10020 const char *klass =
10021 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
10022 const char *refklass =
10023 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
10024 const char *what = (DECL_CONSTRUCTOR_P (list)
10025 ? "constructor" : "method");
10026 /* FIXME: WFL yields the wrong message here but I don't know
10027 what else to use. */
10028 parse_error_context (wfl,
10029 "Can't access %s %s `%s.%s' from `%s'",
10030 access, what, klass, fct_name, refklass);
10031 PATCH_METHOD_RETURN_ERROR ();
10033 check_deprecation (wfl, list);
10035 /* If invoking a innerclass constructor, there are hidden parameters
10037 if (TREE_CODE (patch) == NEW_CLASS_EXPR
10038 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10040 /* And make sure we add the accessed local variables to be saved
10041 in field aliases. */
10042 args = build_alias_initializer_parameter_list
10043 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
10045 /* Secretly pass the current_this/primary as a second argument */
10046 if (primary || current_this)
10049 tree this_type = (current_this ?
10050 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
10051 /* Method's (list) enclosing context */
10052 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
10053 /* If we have a primary, use it. */
10055 extra_arg = primary;
10056 /* The current `this' is an inner class but isn't a direct
10057 enclosing context for the inner class we're trying to
10058 create. Build an access to the proper enclosing context
10060 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
10061 && this_type != TREE_TYPE (mec))
10064 extra_arg = build_access_to_thisn (current_class,
10065 TREE_TYPE (mec), 0);
10066 extra_arg = java_complete_tree (extra_arg);
10068 /* Otherwise, just use the current `this' as an enclosing
10071 extra_arg = current_this;
10072 args = tree_cons (NULL_TREE, extra_arg, args);
10075 args = tree_cons (NULL_TREE, integer_zero_node, args);
10078 /* This handles the situation where a constructor invocation needs
10079 to have an enclosing context passed as a second parameter (the
10080 constructor is one of an inner class. We extract it from the
10081 current function. */
10082 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
10084 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
10087 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
10089 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
10090 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
10094 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
10096 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
10097 extra_arg = java_complete_tree (extra_arg);
10099 args = tree_cons (NULL_TREE, extra_arg, args);
10102 is_static_flag = METHOD_STATIC (list);
10103 if (! is_static_flag && this_arg != NULL_TREE)
10104 args = tree_cons (NULL_TREE, this_arg, args);
10106 /* In the context of an explicit constructor invocation, we can't
10107 invoke any method relying on `this'. Exceptions are: we're
10108 invoking a static function, primary exists and is not the current
10109 this, we're creating a new object. */
10110 if (ctxp->explicit_constructor_p
10112 && (!primary || primary == current_this)
10113 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10115 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
10116 PATCH_METHOD_RETURN_ERROR ();
10118 java_parser_context_restore_global ();
10120 *is_static = is_static_flag;
10121 /* Sometimes, we want the decl of the selected method. Such as for
10125 patch = patch_invoke (patch, list, args);
10126 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10128 tree finit_parms, finit_call;
10130 /* Prepare to pass hidden parameters to finit$, if any. */
10131 finit_parms = build_alias_initializer_parameter_list
10132 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10135 build_method_invocation (build_wfl_node (finit_identifier_node),
10138 /* Generate the code used to initialize fields declared with an
10139 initialization statement and build a compound statement along
10140 with the super constructor invocation. */
10141 patch = build (COMPOUND_EXPR, void_type_node, patch,
10142 java_complete_tree (finit_call));
10143 CAN_COMPLETE_NORMALLY (patch) = 1;
10148 /* Check that we're not trying to do a static reference to a method in
10149 non static method. Return 1 if it's the case, 0 otherwise. */
10152 check_for_static_method_reference (wfl, node, method, where, primary)
10153 tree wfl, node, method, where, primary;
10155 if (METHOD_STATIC (current_function_decl)
10156 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10158 char *fct_name = xstrdup (lang_printable_name (method, 0));
10159 parse_error_context
10160 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
10161 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10162 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10169 /* Fix the invocation of *MDECL if necessary in the case of a
10170 invocation from an inner class. *THIS_ARG might be modified
10171 appropriately and an alternative access to *MDECL might be
10175 maybe_use_access_method (is_super_init, mdecl, this_arg)
10177 tree *mdecl, *this_arg;
10180 tree md = *mdecl, ta = *this_arg;
10182 int non_static_context = !METHOD_STATIC (md);
10185 || DECL_CONTEXT (md) == current_class
10186 || !PURE_INNER_CLASS_TYPE_P (current_class)
10187 || DECL_FINIT_P (md))
10190 /* If we're calling a method found in an enclosing class, generate
10191 what it takes to retrieve the right this. Don't do that if we're
10192 invoking a static method. Note that if MD's type is unrelated to
10193 CURRENT_CLASS, then the current this can be used. */
10195 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10197 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10198 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10200 ta = build_current_thisn (current_class);
10201 ta = build_wfl_node (ta);
10208 maybe_build_thisn_access_method (type);
10209 if (inherits_from_p (type, DECL_CONTEXT (md)))
10211 ta = build_access_to_thisn (ctx, type, 0);
10214 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10215 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10218 ta = java_complete_tree (ta);
10221 /* We might have to use an access method to get to MD. We can
10222 break the method access rule as far as we're not generating
10224 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10226 md = build_outer_method_access_method (md);
10233 /* Returnin a non zero value indicates we were doing a non static
10234 method invokation that is now a static invocation. It will have
10235 callee displace `this' to insert it in the regular argument
10237 return (non_static_context && to_return);
10240 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10244 patch_invoke (patch, method, args)
10245 tree patch, method, args;
10248 tree original_call, t, ta;
10249 tree check = NULL_TREE;
10251 /* Last step for args: convert build-in types. If we're dealing with
10252 a new TYPE() type call, the first argument to the constructor
10253 isn't found in the incoming argument list, but delivered by
10255 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10256 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10257 t = TREE_CHAIN (t);
10258 for (ta = args; t != end_params_node && ta;
10259 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10260 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10261 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10262 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10264 /* Resolve unresolved returned type isses */
10265 t = TREE_TYPE (TREE_TYPE (method));
10266 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10267 resolve_and_layout (TREE_TYPE (t), NULL);
10269 if (flag_emit_class_files || flag_emit_xref)
10273 tree signature = build_java_signature (TREE_TYPE (method));
10274 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10276 case INVOKE_VIRTUAL:
10277 dtable = invoke_build_dtable (0, args);
10278 func = build_invokevirtual (dtable, method);
10281 case INVOKE_NONVIRTUAL:
10282 /* If the object for the method call is null, we throw an
10283 exception. We don't do this if the object is the current
10284 method's `this'. In other cases we just rely on an
10285 optimization pass to eliminate redundant checks. */
10286 if (TREE_VALUE (args) != current_this)
10288 /* We use a save_expr here to make sure we only evaluate
10289 the new `self' expression once. */
10290 tree save_arg = save_expr (TREE_VALUE (args));
10291 TREE_VALUE (args) = save_arg;
10292 check = java_check_reference (save_arg, 1);
10294 /* Fall through. */
10297 case INVOKE_STATIC:
10298 func = build_known_method_ref (method, TREE_TYPE (method),
10299 DECL_CONTEXT (method),
10303 case INVOKE_INTERFACE:
10304 dtable = invoke_build_dtable (1, args);
10305 func = build_invokeinterface (dtable, method);
10312 /* Ensure self_type is initialized, (invokestatic). FIXME */
10313 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10316 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10317 TREE_OPERAND (patch, 0) = func;
10318 TREE_OPERAND (patch, 1) = args;
10319 original_call = patch;
10321 /* We're processing a `new TYPE ()' form. New is called and its
10322 returned value is the first argument to the constructor. We build
10323 a COMPOUND_EXPR and use saved expression so that the overall NEW
10324 expression value is a pointer to a newly created and initialized
10326 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10328 tree class = DECL_CONTEXT (method);
10329 tree c1, saved_new, size, new;
10330 if (flag_emit_class_files || flag_emit_xref)
10332 TREE_TYPE (patch) = build_pointer_type (class);
10335 if (!TYPE_SIZE (class))
10336 safe_layout_class (class);
10337 size = size_in_bytes (class);
10338 new = build (CALL_EXPR, promote_type (class),
10339 build_address_of (alloc_object_node),
10340 tree_cons (NULL_TREE, build_class_ref (class),
10341 build_tree_list (NULL_TREE,
10342 size_in_bytes (class))),
10344 saved_new = save_expr (new);
10345 c1 = build_tree_list (NULL_TREE, saved_new);
10346 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10347 TREE_OPERAND (original_call, 1) = c1;
10348 TREE_SET_CODE (original_call, CALL_EXPR);
10349 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10352 /* If CHECK is set, then we are building a check to see if the object
10354 if (check != NULL_TREE)
10356 patch = build (COMPOUND_EXPR, TREE_TYPE (patch), check, patch);
10357 TREE_SIDE_EFFECTS (patch) = 1;
10364 invocation_mode (method, super)
10368 int access = get_access_flags_from_decl (method);
10371 return INVOKE_SUPER;
10373 if (access & ACC_STATIC)
10374 return INVOKE_STATIC;
10376 /* We have to look for a constructor before we handle nonvirtual
10377 calls; otherwise the constructor will look nonvirtual. */
10378 if (DECL_CONSTRUCTOR_P (method))
10379 return INVOKE_STATIC;
10381 if (access & ACC_FINAL || access & ACC_PRIVATE)
10382 return INVOKE_NONVIRTUAL;
10384 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10385 return INVOKE_NONVIRTUAL;
10387 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10388 return INVOKE_INTERFACE;
10390 return INVOKE_VIRTUAL;
10393 /* Retrieve a refined list of matching methods. It covers the step
10394 15.11.2 (Compile-Time Step 2) */
10397 lookup_method_invoke (lc, cl, class, name, arg_list)
10400 tree class, name, arg_list;
10402 tree atl = end_params_node; /* Arg Type List */
10403 tree method, signature, list, node;
10404 const char *candidates; /* Used for error report */
10407 /* Fix the arguments */
10408 for (node = arg_list; node; node = TREE_CHAIN (node))
10410 tree current_arg = TREE_TYPE (TREE_VALUE (node));
10411 /* Non primitive type may have to be resolved */
10412 if (!JPRIMITIVE_TYPE_P (current_arg))
10413 resolve_and_layout (current_arg, NULL_TREE);
10415 if (TREE_CODE (current_arg) == RECORD_TYPE)
10416 current_arg = promote_type (current_arg);
10417 atl = tree_cons (NULL_TREE, current_arg, atl);
10420 /* Presto. If we're dealing with an anonymous class and a
10421 constructor call, generate the right constructor now, since we
10422 know the arguments' types. */
10424 if (lc && ANONYMOUS_CLASS_P (class))
10425 craft_constructor (TYPE_NAME (class), atl);
10427 /* Find all candidates and then refine the list, searching for the
10428 most specific method. */
10429 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10430 list = find_most_specific_methods_list (list);
10431 if (list && !TREE_CHAIN (list))
10432 return TREE_VALUE (list);
10434 /* Issue an error. List candidates if any. Candidates are listed
10435 only if accessible (non accessible methods may end-up here for
10436 the sake of a better error report). */
10441 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10442 for (current = list; current; current = TREE_CHAIN (current))
10444 tree cm = TREE_VALUE (current);
10445 char string [4096];
10446 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
10449 (string, " `%s' in `%s'%s",
10450 get_printable_method_name (cm),
10451 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10452 (TREE_CHAIN (current) ? "\n" : ""));
10453 obstack_grow (&temporary_obstack, string, strlen (string));
10455 obstack_1grow (&temporary_obstack, '\0');
10456 candidates = obstack_finish (&temporary_obstack);
10458 /* Issue the error message */
10459 method = make_node (FUNCTION_TYPE);
10460 TYPE_ARG_TYPES (method) = atl;
10461 signature = build_java_argument_signature (method);
10462 dup = xstrdup (lang_printable_name (class, 0));
10463 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10464 (lc ? "constructor" : "method"),
10465 (lc ? dup : IDENTIFIER_POINTER (name)),
10466 IDENTIFIER_POINTER (signature), dup,
10467 (candidates ? candidates : ""));
10472 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10473 when we're looking for a constructor. */
10476 find_applicable_accessible_methods_list (lc, class, name, arglist)
10478 tree class, name, arglist;
10480 static struct hash_table t, *searched_classes = NULL;
10481 static int search_not_done = 0;
10482 tree list = NULL_TREE, all_list = NULL_TREE;
10484 /* Check the hash table to determine if this class has been searched
10486 if (searched_classes)
10488 if (hash_lookup (searched_classes,
10489 (const hash_table_key) class, FALSE, NULL))
10494 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10495 java_hash_compare_tree_node);
10496 searched_classes = &t;
10500 hash_lookup (searched_classes,
10501 (const hash_table_key) class, TRUE, NULL);
10503 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10505 load_class (class, 1);
10506 safe_layout_class (class);
10509 /* Search interfaces */
10510 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10511 && CLASS_INTERFACE (TYPE_NAME (class)))
10514 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10515 search_applicable_methods_list (lc, TYPE_METHODS (class),
10516 name, arglist, &list, &all_list);
10517 n = TREE_VEC_LENGTH (basetype_vec);
10518 for (i = 1; i < n; i++)
10520 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10523 rlist = find_applicable_accessible_methods_list (lc, t, name,
10525 list = chainon (rlist, list);
10528 /* Search classes */
10531 search_applicable_methods_list (lc, TYPE_METHODS (class),
10532 name, arglist, &list, &all_list);
10534 /* When looking finit$ or class$, we turn LC to 1 so that we
10535 only search in class. Note that we should have found
10536 something at this point. */
10537 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name))
10544 /* We must search all interfaces of this class */
10547 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10548 int n = TREE_VEC_LENGTH (basetype_vec), i;
10549 for (i = 1; i < n; i++)
10551 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10552 if (t != object_type_node)
10555 = find_applicable_accessible_methods_list (lc, t,
10557 list = chainon (rlist, list);
10562 /* Search superclass */
10563 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10566 class = CLASSTYPE_SUPER (class);
10567 rlist = find_applicable_accessible_methods_list (lc, class,
10569 list = chainon (rlist, list);
10575 /* We're done. Reset the searched classes list and finally search
10576 java.lang.Object if it wasn't searched already. */
10577 if (!search_not_done)
10580 && TYPE_METHODS (object_type_node)
10581 && !hash_lookup (searched_classes,
10582 (const hash_table_key) object_type_node,
10585 search_applicable_methods_list (lc,
10586 TYPE_METHODS (object_type_node),
10587 name, arglist, &list, &all_list);
10589 hash_table_free (searched_classes);
10590 searched_classes = NULL;
10593 /* Either return the list obtained or all selected (but
10594 inaccessible) methods for better error report. */
10595 return (!list ? all_list : list);
10598 /* Effectively search for the appropriate method in method */
10601 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
10603 tree method, name, arglist;
10604 tree *list, *all_list;
10606 for (; method; method = TREE_CHAIN (method))
10608 /* When dealing with constructor, stop here, otherwise search
10610 if (lc && !DECL_CONSTRUCTOR_P (method))
10612 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10613 || (DECL_NAME (method) != name)))
10616 if (argument_types_convertible (method, arglist))
10618 /* Retain accessible methods only */
10619 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10620 method, NULL_TREE, 0))
10621 *list = tree_cons (NULL_TREE, method, *list);
10623 /* Also retain all selected method here */
10624 *all_list = tree_cons (NULL_TREE, method, *list);
10629 /* 15.11.2.2 Choose the Most Specific Method */
10632 find_most_specific_methods_list (list)
10636 int abstract, candidates;
10637 tree current, new_list = NULL_TREE;
10638 for (current = list; current; current = TREE_CHAIN (current))
10641 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10643 for (method = list; method; method = TREE_CHAIN (method))
10645 tree method_v, current_v;
10646 /* Don't test a method against itself */
10647 if (method == current)
10650 method_v = TREE_VALUE (method);
10651 current_v = TREE_VALUE (current);
10653 /* Compare arguments and location where methods where declared */
10654 if (argument_types_convertible (method_v, current_v))
10656 if (valid_method_invocation_conversion_p
10657 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10658 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10659 && enclosing_context_p (DECL_CONTEXT (method_v),
10660 DECL_CONTEXT (current_v))))
10662 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10663 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10664 max = (v > max ? v : max);
10670 /* Review the list and select the maximally specific methods */
10671 for (current = list, abstract = -1, candidates = -1;
10672 current; current = TREE_CHAIN (current))
10673 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10675 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10676 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10680 /* If we have several and they're all abstract, just pick the
10682 if (candidates > 0 && (candidates == abstract))
10684 new_list = nreverse (new_list);
10685 TREE_CHAIN (new_list) = NULL_TREE;
10688 /* We have several (we couldn't find a most specific), all but one
10689 are abstract, we pick the only non abstract one. */
10690 if (candidates > 0 && (candidates == abstract+1))
10692 for (current = new_list; current; current = TREE_CHAIN (current))
10693 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10695 TREE_CHAIN (current) = NULL_TREE;
10696 new_list = current;
10700 /* If we can't find one, lower expectations and try to gather multiple
10701 maximally specific methods */
10702 while (!new_list && max)
10706 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10707 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10714 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
10715 converted by method invocation conversion (5.3) to the type of the
10716 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10717 to change less often than M1. */
10720 argument_types_convertible (m1, m2_or_arglist)
10721 tree m1, m2_or_arglist;
10723 static tree m2_arg_value = NULL_TREE;
10724 static tree m2_arg_cache = NULL_TREE;
10725 static int initialized_p;
10727 register tree m1_arg, m2_arg;
10729 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10731 if (!initialized_p)
10733 ggc_add_tree_root (&m2_arg_value, 1);
10734 ggc_add_tree_root (&m2_arg_cache, 1);
10738 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
10740 if (m2_arg_value == m2_or_arglist)
10741 m2_arg = m2_arg_cache;
10744 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10746 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10748 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10749 if (!METHOD_STATIC (m2_or_arglist))
10750 m2_arg = TREE_CHAIN (m2_arg);
10753 m2_arg = m2_or_arglist;
10755 m2_arg_value = m2_or_arglist;
10756 m2_arg_cache = m2_arg;
10759 while (m1_arg != end_params_node && m2_arg != end_params_node)
10761 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
10762 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10763 TREE_VALUE (m2_arg)))
10765 m1_arg = TREE_CHAIN (m1_arg);
10766 m2_arg = TREE_CHAIN (m2_arg);
10768 return m1_arg == end_params_node && m2_arg == end_params_node;
10771 /* Qualification routines */
10774 qualify_ambiguous_name (id)
10777 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10778 saved_current_class;
10779 int again, super_found = 0, this_found = 0, new_array_found = 0;
10782 /* We first qualify the first element, then derive qualification of
10783 others based on the first one. If the first element is qualified
10784 by a resolution (field or type), this resolution is stored in the
10785 QUAL_RESOLUTION of the qual element being examined. We need to
10786 save the current_class since the use of SUPER might change the
10788 saved_current_class = current_class;
10789 qual = EXPR_WFL_QUALIFICATION (id);
10792 /* Simple qualified expression feature a qual_wfl that is a
10793 WFL. Expression derived from a primary feature more complicated
10794 things like a CALL_EXPR. Expression from primary need to be
10795 worked out to extract the part on which the qualification will
10797 qual_wfl = QUAL_WFL (qual);
10798 switch (TREE_CODE (qual_wfl))
10801 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10802 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10804 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10805 qual_wfl = QUAL_WFL (qual);
10808 case NEW_ARRAY_EXPR:
10809 case NEW_ANONYMOUS_ARRAY_EXPR:
10810 qual = TREE_CHAIN (qual);
10811 again = new_array_found = 1;
10815 case NEW_CLASS_EXPR:
10816 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10819 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10820 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10823 qual = TREE_CHAIN (qual);
10824 qual_wfl = QUAL_WFL (qual);
10826 case CLASS_LITERAL:
10827 qual = TREE_CHAIN (qual);
10828 qual_wfl = QUAL_WFL (qual);
10831 /* Fix for -Wall. Just break doing nothing */
10835 ptr_type = current_class;
10837 code = TREE_CODE (qual_wfl);
10839 /* Pos evaluation: non WFL leading expression nodes */
10840 if (code == CONVERT_EXPR
10841 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10842 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10844 else if (code == INTEGER_CST)
10847 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
10848 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10849 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10851 else if (code == TREE_LIST)
10852 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10854 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10855 || code == PLUS_EXPR)
10857 qual = TREE_CHAIN (qual);
10858 qual_wfl = QUAL_WFL (qual);
10863 name = EXPR_WFL_NODE (qual_wfl);
10866 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10871 /* If we have a THIS (from a primary), we set the context accordingly */
10872 if (name == this_identifier_node)
10874 /* This isn't really elegant. One more added irregularity
10875 before I start using COMPONENT_REF (hopefully very soon.) */
10876 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10877 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10878 EXPR_WITH_FILE_LOCATION
10879 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10880 this_identifier_node)
10882 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10883 qual = EXPR_WFL_QUALIFICATION (qual);
10885 qual = TREE_CHAIN (qual);
10886 qual_wfl = QUAL_WFL (qual);
10887 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10890 name = EXPR_WFL_NODE (qual_wfl);
10893 /* If we have a SUPER, we set the context accordingly */
10894 if (name == super_identifier_node)
10896 current_class = CLASSTYPE_SUPER (ptr_type);
10897 /* Check that there is such a thing as a super class. If not,
10898 return. The error will be caught later on, during the
10900 if (!current_class)
10902 current_class = saved_current_class;
10905 qual = TREE_CHAIN (qual);
10906 /* Do one more interation to set things up */
10907 super_found = again = 1;
10911 /* If name appears within the scope of a local variable declaration
10912 or parameter declaration, then it is an expression name. We don't
10913 carry this test out if we're in the context of the use of SUPER
10915 if (!this_found && !super_found
10916 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10917 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
10919 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10920 QUAL_RESOLUTION (qual) = decl;
10923 /* If within the class/interface NAME was found to be used there
10924 exists a (possibly inherited) field named NAME, then this is an
10925 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10926 address length, it is OK. */
10927 else if ((decl = lookup_field_wrapper (ptr_type, name))
10928 || name == length_identifier_node)
10930 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10931 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
10934 /* We reclassify NAME as yielding to a type name resolution if:
10935 - NAME is a class/interface declared within the compilation
10936 unit containing NAME,
10937 - NAME is imported via a single-type-import declaration,
10938 - NAME is declared in an another compilation unit of the package
10939 of the compilation unit containing NAME,
10940 - NAME is declared by exactly on type-import-on-demand declaration
10941 of the compilation unit containing NAME.
10942 - NAME is actually a STRING_CST. */
10943 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10944 || (decl = resolve_and_layout (name, NULL_TREE)))
10946 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10947 QUAL_RESOLUTION (qual) = decl;
10950 /* Method call, array references and cast are expression name */
10951 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
10952 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10953 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR
10954 || TREE_CODE (QUAL_WFL (qual)) == MODIFY_EXPR)
10955 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10957 /* Check here that NAME isn't declared by more than one
10958 type-import-on-demand declaration of the compilation unit
10959 containing NAME. FIXME */
10961 /* Otherwise, NAME is reclassified as a package name */
10963 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10965 /* Propagate the qualification accross other components of the
10967 for (qual = TREE_CHAIN (qual); qual;
10968 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10970 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10971 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10973 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10976 /* Store the global qualification for the ambiguous part of ID back
10978 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10979 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10980 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10981 RESOLVE_TYPE_NAME_P (id) = 1;
10982 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10983 RESOLVE_PACKAGE_NAME_P (id) = 1;
10985 /* Restore the current class */
10986 current_class = saved_current_class;
10990 breakdown_qualified (left, right, source)
10991 tree *left, *right, source;
10994 int l = IDENTIFIER_LENGTH (source);
10996 base = alloca (l + 1);
10997 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10999 /* Breakdown NAME into REMAINDER . IDENTIFIER */
11001 while (*p != '.' && p != base)
11004 /* We didn't find a '.'. Return an error */
11010 *right = get_identifier (p+1);
11011 *left = get_identifier (base);
11016 /* Return TRUE if two classes are from the same package. */
11019 in_same_package (name1, name2)
11026 if (TREE_CODE (name1) == TYPE_DECL)
11027 name1 = DECL_NAME (name1);
11028 if (TREE_CODE (name2) == TYPE_DECL)
11029 name2 = DECL_NAME (name2);
11031 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
11032 /* One in empty package. */
11035 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
11036 /* Both in empty package. */
11039 breakdown_qualified (&pkg1, &tmp, name1);
11040 breakdown_qualified (&pkg2, &tmp, name2);
11042 return (pkg1 == pkg2);
11045 /* Patch tree nodes in a function body. When a BLOCK is found, push
11046 local variable decls if present.
11047 Same as java_complete_lhs, but does resolve static finals to values. */
11050 java_complete_tree (node)
11053 node = java_complete_lhs (node);
11054 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
11055 && DECL_INITIAL (node) != NULL_TREE
11056 && !flag_emit_xref)
11058 tree value = DECL_INITIAL (node);
11059 DECL_INITIAL (node) = NULL_TREE;
11060 value = fold_constant_for_init (value, node);
11061 DECL_INITIAL (node) = value;
11062 if (value != NULL_TREE)
11064 /* fold_constant_for_init sometimes widen the original type
11065 of the constant (i.e. byte to int.) It's not desirable,
11066 especially if NODE is a function argument. */
11067 if (TREE_CODE (value) == INTEGER_CST
11068 && TREE_TYPE (node) != TREE_TYPE (value))
11069 return convert (TREE_TYPE (node), value);
11074 DECL_FIELD_FINAL_IUD (node) = 0;
11080 java_stabilize_reference (node)
11083 if (TREE_CODE (node) == COMPOUND_EXPR)
11085 tree op0 = TREE_OPERAND (node, 0);
11086 tree op1 = TREE_OPERAND (node, 1);
11087 TREE_OPERAND (node, 0) = save_expr (op0);
11088 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
11091 return stabilize_reference (node);
11094 /* Patch tree nodes in a function body. When a BLOCK is found, push
11095 local variable decls if present.
11096 Same as java_complete_tree, but does not resolve static finals to values. */
11099 java_complete_lhs (node)
11102 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11105 /* CONVERT_EXPR always has its type set, even though it needs to be
11107 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11110 /* The switch block implements cases processing container nodes
11111 first. Contained nodes are always written back. Leaves come
11112 next and return a value. */
11113 switch (TREE_CODE (node))
11117 /* 1- Block section.
11118 Set the local values on decl names so we can identify them
11119 faster when they're referenced. At that stage, identifiers
11120 are legal so we don't check for declaration errors. */
11121 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11123 DECL_CONTEXT (cn) = current_function_decl;
11124 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11126 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11127 CAN_COMPLETE_NORMALLY (node) = 1;
11130 tree stmt = BLOCK_EXPR_BODY (node);
11132 int error_seen = 0;
11133 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11135 /* Re-order from (((A; B); C); ...; Z) to
11136 (A; (B; (C ; (...; Z)))).
11137 This makes it easier to scan the statements left-to-right
11138 without using recursion (which might overflow the stack
11139 if the block has many statements. */
11142 tree left = TREE_OPERAND (stmt, 0);
11143 if (TREE_CODE (left) != COMPOUND_EXPR)
11145 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11146 TREE_OPERAND (left, 1) = stmt;
11149 BLOCK_EXPR_BODY (node) = stmt;
11152 /* Now do the actual complete, without deep recursion for
11154 ptr = &BLOCK_EXPR_BODY (node);
11155 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11156 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
11158 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11159 tree *next = &TREE_OPERAND (*ptr, 1);
11160 TREE_OPERAND (*ptr, 0) = cur;
11161 if (cur == empty_stmt_node)
11163 /* Optimization; makes it easier to detect empty bodies.
11164 Most useful for <clinit> with all-constant initializer. */
11168 if (TREE_CODE (cur) == ERROR_MARK)
11170 else if (! CAN_COMPLETE_NORMALLY (cur))
11175 if (TREE_CODE (wfl_op2) == BLOCK)
11176 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11177 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11178 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11182 if (TREE_CODE (wfl_op2) != CASE_EXPR
11183 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11184 unreachable_stmt_error (*ptr);
11188 *ptr = java_complete_tree (*ptr);
11190 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11191 return error_mark_node;
11192 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11194 /* Turn local bindings to null */
11195 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11196 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11198 TREE_TYPE (node) = void_type_node;
11201 /* 2- They are expressions but ultimately deal with statements */
11204 wfl_op1 = TREE_OPERAND (node, 0);
11205 COMPLETE_CHECK_OP_0 (node);
11206 /* 14.19 A throw statement cannot complete normally. */
11207 CAN_COMPLETE_NORMALLY (node) = 0;
11208 return patch_throw_statement (node, wfl_op1);
11210 case SYNCHRONIZED_EXPR:
11211 wfl_op1 = TREE_OPERAND (node, 0);
11212 return patch_synchronized_statement (node, wfl_op1);
11215 return patch_try_statement (node);
11217 case TRY_FINALLY_EXPR:
11218 COMPLETE_CHECK_OP_0 (node);
11219 COMPLETE_CHECK_OP_1 (node);
11220 if (TREE_OPERAND (node, 0) == empty_stmt_node)
11221 return TREE_OPERAND (node, 1);
11222 if (TREE_OPERAND (node, 1) == empty_stmt_node)
11223 return TREE_OPERAND (node, 0);
11224 CAN_COMPLETE_NORMALLY (node)
11225 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11226 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11227 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11230 case CLEANUP_POINT_EXPR:
11231 COMPLETE_CHECK_OP_0 (node);
11232 TREE_TYPE (node) = void_type_node;
11233 CAN_COMPLETE_NORMALLY (node) =
11234 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11237 case WITH_CLEANUP_EXPR:
11238 COMPLETE_CHECK_OP_0 (node);
11239 COMPLETE_CHECK_OP_2 (node);
11240 CAN_COMPLETE_NORMALLY (node) =
11241 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11242 TREE_TYPE (node) = void_type_node;
11245 case LABELED_BLOCK_EXPR:
11246 PUSH_LABELED_BLOCK (node);
11247 if (LABELED_BLOCK_BODY (node))
11248 COMPLETE_CHECK_OP_1 (node);
11249 TREE_TYPE (node) = void_type_node;
11250 POP_LABELED_BLOCK ();
11252 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11254 LABELED_BLOCK_BODY (node) = NULL_TREE;
11255 CAN_COMPLETE_NORMALLY (node) = 1;
11257 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11258 CAN_COMPLETE_NORMALLY (node) = 1;
11261 case EXIT_BLOCK_EXPR:
11262 /* We don't complete operand 1, because it's the return value of
11263 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11264 return patch_bc_statement (node);
11267 cn = java_complete_tree (TREE_OPERAND (node, 0));
11268 if (cn == error_mark_node)
11271 /* First, the case expression must be constant. Values of final
11272 fields are accepted. */
11274 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11275 && JDECL_P (TREE_OPERAND (cn, 1))
11276 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11277 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11279 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11280 TREE_OPERAND (cn, 1));
11283 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11285 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11286 parse_error_context (node, "Constant expression required");
11287 return error_mark_node;
11290 nn = ctxp->current_loop;
11292 /* It must be assignable to the type of the switch expression. */
11293 if (!try_builtin_assignconv (NULL_TREE,
11294 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11296 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11297 parse_error_context
11299 "Incompatible type for case. Can't convert `%s' to `int'",
11300 lang_printable_name (TREE_TYPE (cn), 0));
11301 return error_mark_node;
11304 cn = fold (convert (int_type_node, cn));
11306 /* Multiple instance of a case label bearing the same
11307 value is checked during code generation. The case
11308 expression is allright so far. */
11309 if (TREE_CODE (cn) == VAR_DECL)
11310 cn = DECL_INITIAL (cn);
11311 TREE_OPERAND (node, 0) = cn;
11312 TREE_TYPE (node) = void_type_node;
11313 CAN_COMPLETE_NORMALLY (node) = 1;
11314 TREE_SIDE_EFFECTS (node) = 1;
11318 nn = ctxp->current_loop;
11319 /* Only one default label is allowed per switch statement */
11320 if (SWITCH_HAS_DEFAULT (nn))
11322 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11323 parse_error_context (wfl_operator,
11324 "Duplicate case label: `default'");
11325 return error_mark_node;
11328 SWITCH_HAS_DEFAULT (nn) = 1;
11329 TREE_TYPE (node) = void_type_node;
11330 TREE_SIDE_EFFECTS (node) = 1;
11331 CAN_COMPLETE_NORMALLY (node) = 1;
11337 /* Check whether the loop was enclosed in a labeled
11338 statement. If not, create one, insert the loop in it and
11340 nn = patch_loop_statement (node);
11342 /* Anyways, walk the body of the loop */
11343 if (TREE_CODE (node) == LOOP_EXPR)
11344 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11345 /* Switch statement: walk the switch expression and the cases */
11347 node = patch_switch_statement (node);
11349 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11350 nn = error_mark_node;
11353 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11354 /* If we returned something different, that's because we
11355 inserted a label. Pop the label too. */
11358 if (CAN_COMPLETE_NORMALLY (node))
11359 CAN_COMPLETE_NORMALLY (nn) = 1;
11360 POP_LABELED_BLOCK ();
11367 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11368 return patch_exit_expr (node);
11372 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11373 if (TREE_OPERAND (node, 0) == error_mark_node)
11374 return error_mark_node;
11375 /* then-else branches */
11376 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11377 if (TREE_OPERAND (node, 1) == error_mark_node)
11378 return error_mark_node;
11379 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11380 if (TREE_OPERAND (node, 2) == error_mark_node)
11381 return error_mark_node;
11382 return patch_if_else_statement (node);
11385 case CONDITIONAL_EXPR:
11387 wfl_op1 = TREE_OPERAND (node, 0);
11388 COMPLETE_CHECK_OP_0 (node);
11389 wfl_op2 = TREE_OPERAND (node, 1);
11390 COMPLETE_CHECK_OP_1 (node);
11391 wfl_op3 = TREE_OPERAND (node, 2);
11392 COMPLETE_CHECK_OP_2 (node);
11393 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11395 /* 3- Expression section */
11396 case COMPOUND_EXPR:
11397 wfl_op2 = TREE_OPERAND (node, 1);
11398 TREE_OPERAND (node, 0) = nn =
11399 java_complete_tree (TREE_OPERAND (node, 0));
11400 if (wfl_op2 == empty_stmt_node)
11401 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11404 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11406 /* An unreachable condition in a do-while statement
11407 is *not* (technically) an unreachable statement. */
11409 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11410 nn = EXPR_WFL_NODE (nn);
11411 if (TREE_CODE (nn) != EXIT_EXPR)
11413 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11414 parse_error_context (wfl_operator, "Unreachable statement");
11417 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11418 if (TREE_OPERAND (node, 1) == error_mark_node)
11419 return error_mark_node;
11420 CAN_COMPLETE_NORMALLY (node)
11421 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
11423 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11427 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11428 return patch_return (node);
11430 case EXPR_WITH_FILE_LOCATION:
11431 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11432 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11435 node = resolve_expression_name (node, NULL);
11436 if (node == error_mark_node)
11438 /* Keep line number information somewhere were it doesn't
11439 disrupt the completion process. */
11440 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11442 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11443 TREE_OPERAND (node, 1) = wfl;
11445 CAN_COMPLETE_NORMALLY (node) = 1;
11450 int save_lineno = lineno;
11451 lineno = EXPR_WFL_LINENO (node);
11452 body = java_complete_tree (EXPR_WFL_NODE (node));
11453 lineno = save_lineno;
11454 EXPR_WFL_NODE (node) = body;
11455 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11456 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11457 if (body == empty_stmt_node || TREE_CONSTANT (body))
11459 /* Makes it easier to constant fold, detect empty bodies. */
11462 if (body == error_mark_node)
11464 /* Its important for the evaluation of assignment that
11465 this mark on the TREE_TYPE is propagated. */
11466 TREE_TYPE (node) = error_mark_node;
11467 return error_mark_node;
11470 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11475 case NEW_ARRAY_EXPR:
11476 /* Patch all the dimensions */
11478 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11480 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11481 tree dim = convert (int_type_node,
11482 java_complete_tree (TREE_VALUE (cn)));
11483 if (dim == error_mark_node)
11490 TREE_VALUE (cn) = dim;
11491 /* Setup the location of the current dimension, for
11492 later error report. */
11493 TREE_PURPOSE (cn) =
11494 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11495 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11498 /* They complete the array creation expression, if no errors
11500 CAN_COMPLETE_NORMALLY (node) = 1;
11501 return (flag ? error_mark_node
11502 : force_evaluation_order (patch_newarray (node)));
11504 case NEW_ANONYMOUS_ARRAY_EXPR:
11505 /* Create the array type if necessary. */
11506 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11508 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11509 if (!(type = resolve_type_during_patch (type)))
11510 return error_mark_node;
11511 type = build_array_from_name (type, NULL_TREE,
11512 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11513 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11515 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11516 ANONYMOUS_ARRAY_INITIALIZER (node));
11517 if (node == error_mark_node)
11518 return error_mark_node;
11519 CAN_COMPLETE_NORMALLY (node) = 1;
11522 case NEW_CLASS_EXPR:
11524 /* Complete function's argument(s) first */
11525 if (complete_function_arguments (node))
11526 return error_mark_node;
11529 tree decl, wfl = TREE_OPERAND (node, 0);
11530 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11531 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
11532 super_identifier_node);
11534 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
11535 from_super, 0, &decl);
11536 if (node == error_mark_node)
11537 return error_mark_node;
11539 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11540 /* If we call this(...), register signature and positions */
11542 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11543 tree_cons (wfl, decl,
11544 DECL_CONSTRUCTOR_CALLS (current_function_decl));
11545 CAN_COMPLETE_NORMALLY (node) = 1;
11546 return force_evaluation_order (node);
11550 /* Save potential wfls */
11551 wfl_op1 = TREE_OPERAND (node, 0);
11552 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11554 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11555 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11556 && DECL_INITIAL (nn) != NULL_TREE)
11560 value = fold_constant_for_init (nn, nn);
11562 if (value != NULL_TREE)
11564 tree type = TREE_TYPE (value);
11565 if (JPRIMITIVE_TYPE_P (type) ||
11566 (type == string_ptr_type_node && ! flag_emit_class_files))
11567 return empty_stmt_node;
11569 if (! flag_emit_class_files)
11570 DECL_INITIAL (nn) = NULL_TREE;
11571 if (CLASS_FINAL_VARIABLE_P (nn))
11572 DECL_FIELD_FINAL_IUD (nn) = 0;
11574 wfl_op2 = TREE_OPERAND (node, 1);
11576 if (TREE_OPERAND (node, 0) == error_mark_node)
11577 return error_mark_node;
11579 flag = COMPOUND_ASSIGN_P (wfl_op2);
11582 /* This might break when accessing outer field from inner
11583 class. TESTME, FIXME */
11584 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
11586 /* Hand stabilize the lhs on both places */
11587 TREE_OPERAND (node, 0) = lvalue;
11588 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11589 (flag_emit_class_files ? lvalue : save_expr (lvalue));
11591 /* 15.25.2.a: Left hand is not an array access. FIXME */
11592 /* Now complete the RHS. We write it back later on. */
11593 nn = java_complete_tree (TREE_OPERAND (node, 1));
11595 if ((cn = patch_string (nn)))
11598 /* The last part of the rewrite for E1 op= E2 is to have
11599 E1 = (T)(E1 op E2), with T being the type of E1. */
11600 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11601 TREE_TYPE (lvalue), nn));
11603 /* If the assignment is compound and has reference type,
11604 then ensure the LHS has type String and nothing else. */
11605 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
11606 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
11607 parse_error_context (wfl_op2,
11608 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
11609 lang_printable_name (TREE_TYPE (lvalue), 0));
11611 /* 15.25.2.b: Left hand is an array access. FIXME */
11614 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
11615 function to complete this RHS. Note that a NEW_ARRAY_INIT
11616 might have been already fully expanded if created as a result
11617 of processing an anonymous array initializer. We avoid doing
11618 the operation twice by testing whether the node already bears
11620 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
11621 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
11622 TREE_OPERAND (node, 1));
11623 /* Otherwise we simply complete the RHS */
11625 nn = java_complete_tree (TREE_OPERAND (node, 1));
11627 if (nn == error_mark_node)
11628 return error_mark_node;
11630 /* Write back the RHS as we evaluated it. */
11631 TREE_OPERAND (node, 1) = nn;
11633 /* In case we're handling = with a String as a RHS, we need to
11634 produce a String out of the RHS (it might still be a
11635 STRING_CST or a StringBuffer at this stage */
11636 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11637 TREE_OPERAND (node, 1) = nn;
11639 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11640 TREE_OPERAND (node, 1))))
11642 /* We return error_mark_node if outer_field_access_fix
11643 detects we write into a final. */
11644 if (nn == error_mark_node)
11645 return error_mark_node;
11650 node = patch_assignment (node, wfl_op1, wfl_op2);
11651 /* Reorganize the tree if necessary. */
11652 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11653 || JSTRING_P (TREE_TYPE (node))))
11654 node = java_refold (node);
11657 CAN_COMPLETE_NORMALLY (node) = 1;
11669 case TRUNC_MOD_EXPR:
11670 case TRUNC_DIV_EXPR:
11672 case TRUTH_ANDIF_EXPR:
11673 case TRUTH_ORIF_EXPR:
11680 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11681 knows how to handle those cases. */
11682 wfl_op1 = TREE_OPERAND (node, 0);
11683 wfl_op2 = TREE_OPERAND (node, 1);
11685 CAN_COMPLETE_NORMALLY (node) = 1;
11686 /* Don't complete string nodes if dealing with the PLUS operand. */
11687 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
11689 nn = java_complete_tree (wfl_op1);
11690 if (nn == error_mark_node)
11691 return error_mark_node;
11693 TREE_OPERAND (node, 0) = nn;
11695 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
11697 nn = java_complete_tree (wfl_op2);
11698 if (nn == error_mark_node)
11699 return error_mark_node;
11701 TREE_OPERAND (node, 1) = nn;
11703 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
11705 case INSTANCEOF_EXPR:
11706 wfl_op1 = TREE_OPERAND (node, 0);
11707 COMPLETE_CHECK_OP_0 (node);
11708 if (flag_emit_xref)
11710 TREE_TYPE (node) = boolean_type_node;
11713 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11715 case UNARY_PLUS_EXPR:
11717 case TRUTH_NOT_EXPR:
11719 case PREDECREMENT_EXPR:
11720 case PREINCREMENT_EXPR:
11721 case POSTDECREMENT_EXPR:
11722 case POSTINCREMENT_EXPR:
11724 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11725 how to handle those cases. */
11726 wfl_op1 = TREE_OPERAND (node, 0);
11727 CAN_COMPLETE_NORMALLY (node) = 1;
11728 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11729 if (TREE_OPERAND (node, 0) == error_mark_node)
11730 return error_mark_node;
11731 node = patch_unaryop (node, wfl_op1);
11732 CAN_COMPLETE_NORMALLY (node) = 1;
11736 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11737 how to handle those cases. */
11738 wfl_op1 = TREE_OPERAND (node, 0);
11739 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11740 if (TREE_OPERAND (node, 0) == error_mark_node)
11741 return error_mark_node;
11742 if (!flag_emit_class_files && !flag_emit_xref)
11743 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
11744 /* The same applies to wfl_op2 */
11745 wfl_op2 = TREE_OPERAND (node, 1);
11746 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11747 if (TREE_OPERAND (node, 1) == error_mark_node)
11748 return error_mark_node;
11749 if (!flag_emit_class_files && !flag_emit_xref)
11750 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
11751 return patch_array_ref (node);
11756 case COMPONENT_REF:
11757 /* The first step in the re-write of qualified name handling. FIXME.
11758 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
11759 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11760 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11762 tree name = TREE_OPERAND (node, 1);
11763 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11764 if (field == NULL_TREE)
11766 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11767 return error_mark_node;
11769 if (! FIELD_STATIC (field))
11771 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11772 return error_mark_node;
11781 /* Can't use THIS in a static environment */
11784 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11785 parse_error_context (wfl_operator,
11786 "Keyword `this' used outside allowed context");
11787 TREE_TYPE (node) = error_mark_node;
11788 return error_mark_node;
11790 if (ctxp->explicit_constructor_p)
11792 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11793 parse_error_context
11794 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
11795 TREE_TYPE (node) = error_mark_node;
11796 return error_mark_node;
11798 return current_this;
11800 case CLASS_LITERAL:
11801 CAN_COMPLETE_NORMALLY (node) = 1;
11802 node = patch_incomplete_class_ref (node);
11803 if (node == error_mark_node)
11804 return error_mark_node;
11807 case INSTANCE_INITIALIZERS_EXPR:
11808 in_instance_initializer++;
11809 node = java_complete_tree (TREE_OPERAND (node, 0));
11810 in_instance_initializer--;
11811 if (node != error_mark_node)
11812 TREE_TYPE (node) = void_type_node;
11814 return error_mark_node;
11818 CAN_COMPLETE_NORMALLY (node) = 1;
11819 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
11820 and it's time to turn it into the appropriate String object */
11821 if ((nn = patch_string (node)))
11824 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
11829 /* Complete function call's argument. Return a non zero value is an
11830 error was found. */
11833 complete_function_arguments (node)
11839 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11840 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11842 tree wfl = TREE_VALUE (cn), parm, temp;
11843 parm = java_complete_tree (wfl);
11845 if (parm == error_mark_node)
11850 /* If have a string literal that we haven't transformed yet or a
11851 crafted string buffer, as a result of use of the the String
11852 `+' operator. Build `parm.toString()' and expand it. */
11853 if ((temp = patch_string (parm)))
11855 /* Inline PRIMTYPE.TYPE read access */
11856 parm = maybe_build_primttype_type_ref (parm, wfl);
11858 TREE_VALUE (cn) = parm;
11860 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11864 /* Sometimes (for loops and variable initialized during their
11865 declaration), we want to wrap a statement around a WFL and turn it
11869 build_debugable_stmt (location, stmt)
11873 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11875 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11876 EXPR_WFL_LINECOL (stmt) = location;
11878 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11883 build_expr_block (body, decls)
11886 tree node = make_node (BLOCK);
11887 BLOCK_EXPR_DECLS (node) = decls;
11888 BLOCK_EXPR_BODY (node) = body;
11890 TREE_TYPE (node) = TREE_TYPE (body);
11891 TREE_SIDE_EFFECTS (node) = 1;
11895 /* Create a new function block and link it approriately to current
11896 function block chain */
11901 tree b = build_expr_block (NULL_TREE, NULL_TREE);
11903 /* Link block B supercontext to the previous block. The current
11904 function DECL is used as supercontext when enter_a_block is called
11905 for the first time for a given function. The current function body
11906 (DECL_FUNCTION_BODY) is set to be block B. */
11908 tree fndecl = current_function_decl;
11911 BLOCK_SUPERCONTEXT (b) = current_static_block;
11912 current_static_block = b;
11915 else if (!DECL_FUNCTION_BODY (fndecl))
11917 BLOCK_SUPERCONTEXT (b) = fndecl;
11918 DECL_FUNCTION_BODY (fndecl) = b;
11922 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11923 DECL_FUNCTION_BODY (fndecl) = b;
11928 /* Exit a block by changing the current function body
11929 (DECL_FUNCTION_BODY) to the current block super context, only if
11930 the block being exited isn't the method's top level one. */
11936 if (current_function_decl)
11938 b = DECL_FUNCTION_BODY (current_function_decl);
11939 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11940 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11944 b = current_static_block;
11946 if (BLOCK_SUPERCONTEXT (b))
11947 current_static_block = BLOCK_SUPERCONTEXT (b);
11952 /* Lookup for NAME in the nested function's blocks, all the way up to
11953 the current toplevel one. It complies with Java's local variable
11957 lookup_name_in_blocks (name)
11960 tree b = GET_CURRENT_BLOCK (current_function_decl);
11962 while (b != current_function_decl)
11966 /* Paranoid sanity check. To be removed */
11967 if (TREE_CODE (b) != BLOCK)
11970 for (current = BLOCK_EXPR_DECLS (b); current;
11971 current = TREE_CHAIN (current))
11972 if (DECL_NAME (current) == name)
11974 b = BLOCK_SUPERCONTEXT (b);
11980 maybe_absorb_scoping_blocks ()
11982 while (BLOCK_IS_IMPLICIT (GET_CURRENT_BLOCK (current_function_decl)))
11984 tree b = exit_block ();
11985 java_method_add_stmt (current_function_decl, b);
11986 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11991 /* This section of the source is reserved to build_* functions that
11992 are building incomplete tree nodes and the patch_* functions that
11993 are completing them. */
11995 /* Wrap a non WFL node around a WFL. */
11998 build_wfl_wrap (node, location)
12002 tree wfl, node_to_insert = node;
12004 /* We want to process THIS . xxx symbolicaly, to keep it consistent
12005 with the way we're processing SUPER. A THIS from a primary as a
12006 different form than a SUPER. Turn THIS into something symbolic */
12007 if (TREE_CODE (node) == THIS_EXPR)
12008 node_to_insert = wfl = build_wfl_node (this_identifier_node);
12010 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
12012 EXPR_WFL_LINECOL (wfl) = location;
12013 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
12017 /* Build a super() constructor invocation. Returns empty_stmt_node if
12018 we're currently dealing with the class java.lang.Object. */
12021 build_super_invocation (mdecl)
12024 if (DECL_CONTEXT (mdecl) == object_type_node)
12025 return empty_stmt_node;
12028 tree super_wfl = build_wfl_node (super_identifier_node);
12029 tree a = NULL_TREE, t;
12030 /* If we're dealing with an anonymous class, pass the arguments
12031 of the crafted constructor along. */
12032 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
12034 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
12035 for (; t != end_params_node; t = TREE_CHAIN (t))
12036 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
12038 return build_method_invocation (super_wfl, a);
12042 /* Build a SUPER/THIS qualified method invocation. */
12045 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
12052 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
12053 EXPR_WFL_LINECOL (wfl) = lloc;
12054 invok = build_method_invocation (name, args);
12055 return make_qualified_primary (wfl, invok, rloc);
12058 /* Build an incomplete CALL_EXPR node. */
12061 build_method_invocation (name, args)
12065 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
12066 TREE_SIDE_EFFECTS (call) = 1;
12067 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12071 /* Build an incomplete new xxx(...) node. */
12074 build_new_invocation (name, args)
12077 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
12078 TREE_SIDE_EFFECTS (call) = 1;
12079 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
12083 /* Build an incomplete assignment expression. */
12086 build_assignment (op, op_location, lhs, rhs)
12087 int op, op_location;
12091 /* Build the corresponding binop if we deal with a Compound
12092 Assignment operator. Mark the binop sub-tree as part of a
12093 Compound Assignment expression */
12094 if (op != ASSIGN_TK)
12096 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12097 COMPOUND_ASSIGN_P (rhs) = 1;
12099 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12100 TREE_SIDE_EFFECTS (assignment) = 1;
12101 EXPR_WFL_LINECOL (assignment) = op_location;
12105 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
12108 print_int_node (node)
12111 static char buffer [80];
12112 if (TREE_CONSTANT_OVERFLOW (node))
12113 sprintf (buffer, "<overflow>");
12115 if (TREE_INT_CST_HIGH (node) == 0)
12116 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12117 TREE_INT_CST_LOW (node));
12118 else if (TREE_INT_CST_HIGH (node) == -1
12119 && TREE_INT_CST_LOW (node) != 0)
12122 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12123 -TREE_INT_CST_LOW (node));
12126 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12127 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12134 /* This section of the code handle assignment check with FINAL
12138 reset_static_final_variable_assignment_flag (class)
12142 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12143 if (CLASS_FINAL_VARIABLE_P (field))
12144 DECL_FIELD_FINAL_LIIC (field) = 0;
12147 /* Figure whether all final static variable have been initialized. */
12150 check_static_final_variable_assignment_flag (class)
12155 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12156 if (CLASS_FINAL_VARIABLE_P (field)
12157 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12158 parse_error_context
12159 (DECL_FIELD_FINAL_WFL (field),
12160 "Blank static final variable `%s' may not have been initialized",
12161 IDENTIFIER_POINTER (DECL_NAME (field)));
12164 /* This function marks all final variable locally unassigned. */
12167 reset_final_variable_local_assignment_flag (class)
12171 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12172 if (FINAL_VARIABLE_P (field))
12173 DECL_FIELD_FINAL_LIIC (field) = 0;
12176 /* Figure whether all final variables have beem initialized in MDECL
12177 and mark MDECL accordingly. */
12180 check_final_variable_local_assignment_flag (class, mdecl)
12185 int initialized = 0;
12186 int non_initialized = 0;
12188 if (DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12191 /* First find out whether all final variables or no final variable
12192 are initialized in this ctor. We don't take into account final
12193 variable that have been initialized upon declaration. */
12194 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12195 if (FINAL_VARIABLE_P (field) && !DECL_FIELD_FINAL_IUD (field))
12197 if (DECL_FIELD_FINAL_LIIC (field))
12203 /* There were no non initialized variable and no initialized variable.
12204 This ctor is fine. */
12205 if (!non_initialized && !initialized)
12206 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12207 /* If no variables have been initialized, that fine. We'll check
12208 later whether this ctor calls a constructor which initializes
12209 them. We mark the ctor as not initializing all its finals. */
12210 else if (initialized == 0)
12211 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12212 /* If we have a mixed bag, then we have a problem. We need to report
12213 all the variables we're not initializing. */
12214 else if (initialized && non_initialized)
12216 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12217 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12218 if (FIELD_FINAL (field)
12219 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12221 parse_error_context
12222 (lookup_cl (mdecl),
12223 "Blank final variable `%s' may not have been initialized in this constructor",
12224 IDENTIFIER_POINTER (DECL_NAME (field)));
12225 DECL_FIELD_FINAL_IERR (field) = 1;
12228 /* Otherwise we know this ctor is initializing all its final
12229 variable. We mark it so. */
12231 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12234 /* This function recurses in a simple what through STMT and stops when
12235 it finds a constructor call. It then verifies that the called
12236 constructor initialized its final properly. Return 1 upon success,
12237 0 or -1 otherwise. */
12240 check_final_variable_indirect_assignment (stmt)
12244 switch (TREE_CODE (stmt))
12246 case EXPR_WITH_FILE_LOCATION:
12247 return check_final_variable_indirect_assignment (EXPR_WFL_NODE (stmt));
12248 case COMPOUND_EXPR:
12249 res = check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12252 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 1));
12254 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12257 tree decl = TREE_OPERAND (stmt, 0);
12260 if (TREE_CODE (decl) != FUNCTION_DECL)
12261 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
12262 if (TREE_CODE (decl) != FUNCTION_DECL)
12264 if (DECL_FUNCTION_ALL_FINAL_INITIALIZED (decl))
12266 if (DECL_FINIT_P (decl) || DECL_CONTEXT (decl) != current_class)
12268 fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
12269 if (fbody == error_mark_node)
12271 fbody = BLOCK_EXPR_BODY (fbody);
12272 return check_final_variable_indirect_assignment (fbody);
12280 /* This is the last chance to catch a final variable initialization
12281 problem. This routine will report an error if a final variable was
12282 never (globally) initialized and never reported as not having been
12283 initialized properly. */
12286 check_final_variable_global_assignment_flag (class)
12292 /* We go through all natural ctors and see whether they're
12293 initializing all their final variables or not. */
12294 current_function_decl = NULL_TREE; /* For the error report. */
12295 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
12296 if (DECL_CONSTRUCTOR_P (mdecl) && ! DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12298 if (!DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl))
12300 /* It doesn't. Maybe it calls a constructor that initializes
12302 tree fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl));
12303 if (fbody == error_mark_node)
12305 fbody = BLOCK_EXPR_BODY (fbody);
12306 if (check_final_variable_indirect_assignment (fbody) == 1)
12308 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12312 parse_error_context
12313 (lookup_cl (mdecl),
12314 "Final variable initialization error in this constructor");
12320 /* Finally we catch final variables that never were initialized */
12321 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12322 if (FINAL_VARIABLE_P (field)
12323 /* If the field wasn't initialized upon declaration */
12324 && !DECL_FIELD_FINAL_IUD (field)
12325 /* There wasn't natural ctor in which the field could have been
12328 /* If we never reported a problem with this field */
12329 && !DECL_FIELD_FINAL_IERR (field))
12331 current_function_decl = NULL;
12332 parse_error_context
12333 (DECL_FIELD_FINAL_WFL (field),
12334 "Final variable `%s' hasn't been initialized upon its declaration",
12335 IDENTIFIER_POINTER (DECL_NAME (field)));
12340 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12344 check_final_assignment (lvalue, wfl)
12347 if (TREE_CODE (lvalue) != COMPONENT_REF && !JDECL_P (lvalue))
12350 if (TREE_CODE (lvalue) == COMPONENT_REF
12351 && JDECL_P (TREE_OPERAND (lvalue, 1)))
12352 lvalue = TREE_OPERAND (lvalue, 1);
12354 if (!FIELD_FINAL (lvalue))
12357 /* Now the logic. We can modify a final VARIABLE:
12358 1) in finit$, (its declaration was followed by an initialization,)
12359 2) consistently in each natural ctor, if it wasn't initialized in
12360 finit$ or once in <clinit>. In any other cases, an error should be
12362 if (DECL_FINIT_P (current_function_decl))
12364 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12368 if (!DECL_FUNCTION_SYNTHETIC_CTOR (current_function_decl)
12369 /* Only if it wasn't given a value upon initialization */
12370 && DECL_LANG_SPECIFIC (lvalue) && !DECL_FIELD_FINAL_IUD (lvalue)
12371 /* If it was never assigned a value in this constructor */
12372 && !DECL_FIELD_FINAL_LIIC (lvalue))
12374 /* Turn the locally assigned flag on, it will be checked later
12375 on to point out at discrepancies. */
12376 DECL_FIELD_FINAL_LIIC (lvalue) = 1;
12377 if (DECL_CLINIT_P (current_function_decl))
12378 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12382 /* Other problems should be reported right away. */
12383 parse_error_context
12384 (wfl, "Can't %sassign a value to the final variable `%s'",
12385 (FIELD_STATIC (lvalue) ? "re" : ""),
12386 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12388 /* Note that static field can be initialized once and only once. */
12389 if (FIELD_STATIC (lvalue))
12390 DECL_FIELD_FINAL_IERR (lvalue) = 1;
12395 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12396 read. This is needed to avoid circularities in the implementation
12397 of these fields in libjava. */
12400 maybe_build_primttype_type_ref (rhs, wfl)
12403 tree to_return = NULL_TREE;
12404 tree rhs_type = TREE_TYPE (rhs);
12405 if (TREE_CODE (rhs) == COMPOUND_EXPR)
12407 tree n = TREE_OPERAND (rhs, 1);
12408 if (TREE_CODE (n) == VAR_DECL
12409 && DECL_NAME (n) == TYPE_identifier_node
12410 && rhs_type == class_ptr_type
12411 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
12413 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
12414 if (!strncmp (self_name, "java.lang.", 10))
12415 to_return = build_primtype_type_ref (self_name);
12418 return (to_return ? to_return : rhs );
12421 /* 15.25 Assignment operators. */
12424 patch_assignment (node, wfl_op1, wfl_op2)
12429 tree rhs = TREE_OPERAND (node, 1);
12430 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12431 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12432 int error_found = 0;
12433 int lvalue_from_array = 0;
12435 /* Can't assign to a (blank) final. */
12436 if (check_final_assignment (lvalue, wfl_op1))
12439 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12441 /* Lhs can be a named variable */
12442 if (JDECL_P (lvalue))
12444 lhs_type = TREE_TYPE (lvalue);
12446 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12447 comment on reason why */
12448 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12450 lhs_type = TREE_TYPE (lvalue);
12451 lvalue_from_array = 1;
12453 /* Or a field access */
12454 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12455 lhs_type = TREE_TYPE (lvalue);
12456 /* Or a function return slot */
12457 else if (TREE_CODE (lvalue) == RESULT_DECL)
12458 lhs_type = TREE_TYPE (lvalue);
12459 /* Otherwise, we might want to try to write into an optimized static
12460 final, this is an of a different nature, reported further on. */
12461 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12462 && resolve_expression_name (wfl_op1, &llvalue))
12464 if (!error_found && check_final_assignment (llvalue, wfl_op1))
12466 /* What we should do instead is resetting the all the flags
12467 previously set, exchange lvalue for llvalue and continue. */
12469 return error_mark_node;
12472 lhs_type = TREE_TYPE (lvalue);
12476 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12480 rhs_type = TREE_TYPE (rhs);
12481 /* 5.1 Try the assignment conversion for builtin type. */
12482 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12484 /* 5.2 If it failed, try a reference conversion */
12485 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12486 lhs_type = promote_type (rhs_type);
12488 /* 15.25.2 If we have a compound assignment, convert RHS into the
12490 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12491 new_rhs = convert (lhs_type, rhs);
12493 /* Explicit cast required. This is an error */
12496 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12497 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12499 char operation [32]; /* Max size known */
12501 /* If the assignment is part of a declaration, we use the WFL of
12502 the declared variable to point out the error and call it a
12503 declaration problem. If the assignment is a genuine =
12504 operator, we call is a operator `=' problem, otherwise we
12505 call it an assignment problem. In both of these last cases,
12506 we use the WFL of the operator to indicate the error. */
12508 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12511 strcpy (operation, "declaration");
12515 wfl = wfl_operator;
12516 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12517 strcpy (operation, "assignment");
12518 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12519 strcpy (operation, "`return'");
12521 strcpy (operation, "`='");
12524 if (!valid_cast_to_p (rhs_type, lhs_type))
12525 parse_error_context
12526 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12527 operation, t1, t2);
12529 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12530 operation, t1, t2);
12531 free (t1); free (t2);
12535 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12537 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
12540 return error_mark_node;
12542 /* 10.10: Array Store Exception runtime check */
12543 if (!flag_emit_class_files
12545 && lvalue_from_array
12546 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12549 tree base = lvalue;
12551 /* We need to retrieve the right argument for
12552 _Jv_CheckArrayStore. This is somewhat complicated by bounds
12553 and null pointer checks, both of which wrap the operand in
12554 one layer of COMPOUND_EXPR. */
12555 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12556 base = TREE_OPERAND (lvalue, 0);
12559 base = TREE_OPERAND (base, 0);
12560 if (flag_bounds_check)
12561 base = TREE_OPERAND (base, 1);
12562 if (flag_check_references)
12563 base = TREE_OPERAND (base, 1);
12564 base = TREE_OPERAND (base, 0);
12567 /* Build the invocation of _Jv_CheckArrayStore */
12568 new_rhs = save_expr (new_rhs);
12569 check = build (CALL_EXPR, void_type_node,
12570 build_address_of (soft_checkarraystore_node),
12571 tree_cons (NULL_TREE, base,
12572 build_tree_list (NULL_TREE, new_rhs)),
12574 TREE_SIDE_EFFECTS (check) = 1;
12576 /* We have to decide on an insertion point */
12577 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12580 if (flag_bounds_check)
12582 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12583 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12584 build (COMPOUND_EXPR, void_type_node, t, check);
12587 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12588 check, TREE_OPERAND (lvalue, 1));
12592 /* Make sure the bound check will happen before the store check */
12593 if (flag_bounds_check)
12594 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12595 build (COMPOUND_EXPR, void_type_node,
12596 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12598 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12602 /* Final locals can be used as case values in switch
12603 statement. Prepare them for this eventuality. */
12604 if (TREE_CODE (lvalue) == VAR_DECL
12605 && LOCAL_FINAL_P (lvalue)
12606 && TREE_CONSTANT (new_rhs)
12607 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12608 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12611 TREE_CONSTANT (lvalue) = 1;
12612 DECL_INITIAL (lvalue) = new_rhs;
12615 TREE_OPERAND (node, 0) = lvalue;
12616 TREE_OPERAND (node, 1) = new_rhs;
12617 TREE_TYPE (node) = lhs_type;
12621 /* Check that type SOURCE can be cast into type DEST. If the cast
12622 can't occur at all, return 0 otherwise 1. This function is used to
12623 produce accurate error messages on the reasons why an assignment
12627 try_reference_assignconv (lhs_type, rhs)
12628 tree lhs_type, rhs;
12630 tree new_rhs = NULL_TREE;
12631 tree rhs_type = TREE_TYPE (rhs);
12633 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12635 /* `null' may be assigned to any reference type */
12636 if (rhs == null_pointer_node)
12637 new_rhs = null_pointer_node;
12638 /* Try the reference assignment conversion */
12639 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12641 /* This is a magic assignment that we process differently */
12642 else if (TREE_CODE (rhs) == JAVA_EXC_OBJ_EXPR)
12648 /* Check that RHS can be converted into LHS_TYPE by the assignment
12649 conversion (5.2), for the cases of RHS being a builtin type. Return
12650 NULL_TREE if the conversion fails or if because RHS isn't of a
12651 builtin type. Return a converted RHS if the conversion is possible. */
12654 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12655 tree wfl_op1, lhs_type, rhs;
12657 tree new_rhs = NULL_TREE;
12658 tree rhs_type = TREE_TYPE (rhs);
12660 /* Handle boolean specially. */
12661 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12662 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12664 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12665 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12669 /* Zero accepted everywhere */
12670 else if (TREE_CODE (rhs) == INTEGER_CST
12671 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12672 && JPRIMITIVE_TYPE_P (rhs_type))
12673 new_rhs = convert (lhs_type, rhs);
12675 /* 5.1.1 Try Identity Conversion,
12676 5.1.2 Try Widening Primitive Conversion */
12677 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12678 new_rhs = convert (lhs_type, rhs);
12680 /* Try a narrowing primitive conversion (5.1.3):
12681 - expression is a constant expression of type int AND
12682 - variable is byte, short or char AND
12683 - The value of the expression is representable in the type of the
12685 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12686 && (lhs_type == byte_type_node || lhs_type == char_type_node
12687 || lhs_type == short_type_node))
12689 if (int_fits_type_p (rhs, lhs_type))
12690 new_rhs = convert (lhs_type, rhs);
12691 else if (wfl_op1) /* Might be called with a NULL */
12692 parse_warning_context
12693 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
12694 print_int_node (rhs), lang_printable_name (lhs_type, 0));
12695 /* Reported a warning that will turn into an error further
12696 down, so we don't return */
12702 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12703 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
12704 0 is the conversion test fails. This implements parts the method
12705 invocation convertion (5.3). */
12708 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12709 tree lhs_type, rhs_type;
12711 /* 5.1.1: This is the identity conversion part. */
12712 if (lhs_type == rhs_type)
12715 /* Reject non primitive types and boolean conversions. */
12716 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
12719 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12720 than a char can't be converted into a char. Short can't too, but
12721 the < test below takes care of that */
12722 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12725 /* Accept all promoted type here. Note, we can't use <= in the test
12726 below, because we still need to bounce out assignments of short
12727 to char and the likes */
12728 if (lhs_type == int_type_node
12729 && (rhs_type == promoted_byte_type_node
12730 || rhs_type == promoted_short_type_node
12731 || rhs_type == promoted_char_type_node
12732 || rhs_type == promoted_boolean_type_node))
12735 /* From here, an integral is widened if its precision is smaller
12736 than the precision of the LHS or if the LHS is a floating point
12737 type, or the RHS is a float and the RHS a double. */
12738 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12739 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12740 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12741 || (rhs_type == float_type_node && lhs_type == double_type_node))
12747 /* Check that something of SOURCE type can be assigned or cast to
12748 something of DEST type at runtime. Return 1 if the operation is
12749 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12750 were SOURCE is cast into DEST, which borrows a lot of the
12751 assignment check. */
12754 valid_ref_assignconv_cast_p (source, dest, cast)
12759 /* SOURCE or DEST might be null if not from a declared entity. */
12760 if (!source || !dest)
12762 if (JNULLP_TYPE_P (source))
12764 if (TREE_CODE (source) == POINTER_TYPE)
12765 source = TREE_TYPE (source);
12766 if (TREE_CODE (dest) == POINTER_TYPE)
12767 dest = TREE_TYPE (dest);
12769 /* If source and dest are being compiled from bytecode, they may need to
12771 if (CLASS_P (source) && !CLASS_LOADED_P (source))
12773 load_class (source, 1);
12774 safe_layout_class (source);
12776 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12778 load_class (dest, 1);
12779 safe_layout_class (dest);
12782 /* Case where SOURCE is a class type */
12783 if (TYPE_CLASS_P (source))
12785 if (TYPE_CLASS_P (dest))
12786 return (source == dest
12787 || inherits_from_p (source, dest)
12788 || (cast && inherits_from_p (dest, source)));
12789 if (TYPE_INTERFACE_P (dest))
12791 /* If doing a cast and SOURCE is final, the operation is
12792 always correct a compile time (because even if SOURCE
12793 does not implement DEST, a subclass of SOURCE might). */
12794 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12796 /* Otherwise, SOURCE must implement DEST */
12797 return interface_of_p (dest, source);
12799 /* DEST is an array, cast permited if SOURCE is of Object type */
12800 return (cast && source == object_type_node ? 1 : 0);
12802 if (TYPE_INTERFACE_P (source))
12804 if (TYPE_CLASS_P (dest))
12806 /* If not casting, DEST must be the Object type */
12808 return dest == object_type_node;
12809 /* We're doing a cast. The cast is always valid is class
12810 DEST is not final, otherwise, DEST must implement SOURCE */
12811 else if (!CLASS_FINAL (TYPE_NAME (dest)))
12814 return interface_of_p (source, dest);
12816 if (TYPE_INTERFACE_P (dest))
12818 /* If doing a cast, then if SOURCE and DEST contain method
12819 with the same signature but different return type, then
12820 this is a (compile time) error */
12823 tree method_source, method_dest;
12827 for (method_source = TYPE_METHODS (source); method_source;
12828 method_source = TREE_CHAIN (method_source))
12831 build_java_argument_signature (TREE_TYPE (method_source));
12832 source_type = TREE_TYPE (TREE_TYPE (method_source));
12833 source_name = DECL_NAME (method_source);
12834 for (method_dest = TYPE_METHODS (dest);
12835 method_dest; method_dest = TREE_CHAIN (method_dest))
12837 build_java_argument_signature (TREE_TYPE (method_dest))
12838 && source_name == DECL_NAME (method_dest)
12839 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12845 return source == dest || interface_of_p (dest, source);
12851 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12852 || (DECL_NAME (TYPE_NAME (source))
12853 == java_io_serializable)));
12856 if (TYPE_ARRAY_P (source))
12858 if (TYPE_CLASS_P (dest))
12859 return dest == object_type_node;
12860 /* Can't cast an array to an interface unless the interface is
12861 java.lang.Cloneable or java.io.Serializable. */
12862 if (TYPE_INTERFACE_P (dest))
12863 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12864 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
12867 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12868 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12870 /* In case of severe errors, they turn out null */
12871 if (!dest_element_type || !source_element_type)
12873 if (source_element_type == dest_element_type)
12875 return valid_ref_assignconv_cast_p (source_element_type,
12876 dest_element_type, cast);
12884 valid_cast_to_p (source, dest)
12888 if (TREE_CODE (source) == POINTER_TYPE)
12889 source = TREE_TYPE (source);
12890 if (TREE_CODE (dest) == POINTER_TYPE)
12891 dest = TREE_TYPE (dest);
12893 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12894 return valid_ref_assignconv_cast_p (source, dest, 1);
12896 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12899 else if (TREE_CODE (source) == BOOLEAN_TYPE
12900 && TREE_CODE (dest) == BOOLEAN_TYPE)
12907 do_unary_numeric_promotion (arg)
12910 tree type = TREE_TYPE (arg);
12911 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12912 || TREE_CODE (type) == CHAR_TYPE)
12913 arg = convert (int_type_node, arg);
12917 /* Return a non zero value if SOURCE can be converted into DEST using
12918 the method invocation conversion rule (5.3). */
12920 valid_method_invocation_conversion_p (dest, source)
12923 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
12924 && valid_builtin_assignconv_identity_widening_p (dest, source))
12925 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12926 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12927 && valid_ref_assignconv_cast_p (source, dest, 0)));
12930 /* Build an incomplete binop expression. */
12933 build_binop (op, op_location, op1, op2)
12938 tree binop = build (op, NULL_TREE, op1, op2);
12939 TREE_SIDE_EFFECTS (binop) = 1;
12940 /* Store the location of the operator, for better error report. The
12941 string of the operator will be rebuild based on the OP value. */
12942 EXPR_WFL_LINECOL (binop) = op_location;
12946 /* Build the string of the operator retained by NODE. If NODE is part
12947 of a compound expression, add an '=' at the end of the string. This
12948 function is called when an error needs to be reported on an
12949 operator. The string is returned as a pointer to a static character
12953 operator_string (node)
12956 #define BUILD_OPERATOR_STRING(S) \
12958 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12962 static char buffer [10];
12963 switch (TREE_CODE (node))
12965 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12966 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12967 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12968 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12969 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12970 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12971 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12972 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12973 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12974 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12975 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12976 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12977 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12978 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12979 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12980 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12981 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12982 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12983 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
12984 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12985 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12986 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12987 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12988 case PREINCREMENT_EXPR: /* Fall through */
12989 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12990 case PREDECREMENT_EXPR: /* Fall through */
12991 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12993 internal_error ("unregistered operator %s",
12994 tree_code_name [TREE_CODE (node)]);
12997 #undef BUILD_OPERATOR_STRING
13000 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
13003 java_decl_equiv (var_acc1, var_acc2)
13004 tree var_acc1, var_acc2;
13006 if (JDECL_P (var_acc1))
13007 return (var_acc1 == var_acc2);
13009 return (TREE_CODE (var_acc1) == COMPONENT_REF
13010 && TREE_CODE (var_acc2) == COMPONENT_REF
13011 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
13012 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
13013 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
13016 /* Return a non zero value if CODE is one of the operators that can be
13017 used in conjunction with the `=' operator in a compound assignment. */
13020 binop_compound_p (code)
13021 enum tree_code code;
13024 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
13025 if (binop_lookup [i] == code)
13028 return i < BINOP_COMPOUND_CANDIDATES;
13031 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
13037 tree c, b, ns, decl;
13039 if (TREE_CODE (t) != MODIFY_EXPR)
13042 c = TREE_OPERAND (t, 1);
13043 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
13044 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
13045 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
13048 /* Now the left branch of the binary operator. */
13049 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
13050 if (! (b && TREE_CODE (b) == NOP_EXPR
13051 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
13054 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
13055 if (! (ns && TREE_CODE (ns) == NOP_EXPR
13056 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
13059 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
13060 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
13061 /* It's got to be the an equivalent decl */
13062 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
13064 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
13065 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
13066 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
13067 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
13068 /* Change the right part of the BINOP_EXPR */
13069 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
13075 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
13076 errors but we modify NODE so that it contains the type computed
13077 according to the expression, when it's fixed. Otherwise, we write
13078 error_mark_node as the type. It allows us to further the analysis
13079 of remaining nodes and detects more errors in certain cases. */
13082 patch_binop (node, wfl_op1, wfl_op2)
13087 tree op1 = TREE_OPERAND (node, 0);
13088 tree op2 = TREE_OPERAND (node, 1);
13089 tree op1_type = TREE_TYPE (op1);
13090 tree op2_type = TREE_TYPE (op2);
13091 tree prom_type = NULL_TREE, cn;
13092 int code = TREE_CODE (node);
13094 /* If 1, tell the routine that we have to return error_mark_node
13095 after checking for the initialization of the RHS */
13096 int error_found = 0;
13098 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13100 /* If either op<n>_type are NULL, this might be early signs of an
13101 error situation, unless it's too early to tell (in case we're
13102 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13103 correctly so the error can be later on reported accurately. */
13104 if (! (code == PLUS_EXPR || code == NE_EXPR
13105 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13110 n = java_complete_tree (op1);
13111 op1_type = TREE_TYPE (n);
13115 n = java_complete_tree (op2);
13116 op2_type = TREE_TYPE (n);
13122 /* 15.16 Multiplicative operators */
13123 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13124 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13125 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13126 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13127 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13129 if (!JNUMERIC_TYPE_P (op1_type))
13130 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13131 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13132 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13133 TREE_TYPE (node) = error_mark_node;
13137 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13138 /* Change the division operator if necessary */
13139 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13140 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13142 if (TREE_CODE (prom_type) == INTEGER_TYPE
13143 && flag_use_divide_subroutine
13144 && ! flag_emit_class_files
13145 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13146 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13148 /* This one is more complicated. FLOATs are processed by a
13149 function call to soft_fmod. Duplicate the value of the
13150 COMPOUND_ASSIGN_P flag. */
13151 if (code == TRUNC_MOD_EXPR)
13153 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13154 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13155 TREE_SIDE_EFFECTS (mod)
13156 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13161 /* 15.17 Additive Operators */
13162 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13164 /* Operation is valid if either one argument is a string
13165 constant, a String object or a StringBuffer crafted for the
13166 purpose of the a previous usage of the String concatenation
13169 if (TREE_CODE (op1) == STRING_CST
13170 || TREE_CODE (op2) == STRING_CST
13171 || JSTRING_TYPE_P (op1_type)
13172 || JSTRING_TYPE_P (op2_type)
13173 || IS_CRAFTED_STRING_BUFFER_P (op1)
13174 || IS_CRAFTED_STRING_BUFFER_P (op2))
13175 return build_string_concatenation (op1, op2);
13177 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13179 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13181 if (!JNUMERIC_TYPE_P (op1_type))
13182 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13183 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13184 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13185 TREE_TYPE (node) = error_mark_node;
13189 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13192 /* 15.18 Shift Operators */
13196 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13198 if (!JINTEGRAL_TYPE_P (op1_type))
13199 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13202 if (JNUMERIC_TYPE_P (op2_type))
13203 parse_error_context (wfl_operator,
13204 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
13205 operator_string (node),
13206 lang_printable_name (op2_type, 0));
13208 parse_error_context (wfl_operator,
13209 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
13210 operator_string (node),
13211 lang_printable_name (op2_type, 0));
13213 TREE_TYPE (node) = error_mark_node;
13218 /* Unary numeric promotion (5.6.1) is performed on each operand
13220 op1 = do_unary_numeric_promotion (op1);
13221 op2 = do_unary_numeric_promotion (op2);
13223 /* The type of the shift expression is the type of the promoted
13224 type of the left-hand operand */
13225 prom_type = TREE_TYPE (op1);
13227 /* Shift int only up to 0x1f and long up to 0x3f */
13228 if (prom_type == int_type_node)
13229 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13230 build_int_2 (0x1f, 0)));
13232 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13233 build_int_2 (0x3f, 0)));
13235 /* The >>> operator is a >> operating on unsigned quantities */
13236 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13239 tree utype = unsigned_type (prom_type);
13240 op1 = convert (utype, op1);
13241 TREE_SET_CODE (node, RSHIFT_EXPR);
13242 TREE_OPERAND (node, 0) = op1;
13243 TREE_OPERAND (node, 1) = op2;
13244 TREE_TYPE (node) = utype;
13245 to_return = convert (prom_type, node);
13246 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13247 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13248 TREE_SIDE_EFFECTS (to_return)
13249 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13254 /* 15.19.1 Type Comparison Operator instaceof */
13255 case INSTANCEOF_EXPR:
13257 TREE_TYPE (node) = boolean_type_node;
13259 if (!(op2_type = resolve_type_during_patch (op2)))
13260 return error_mark_node;
13262 /* The first operand must be a reference type or the null type */
13263 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13264 error_found = 1; /* Error reported further below */
13266 /* The second operand must be a reference type */
13267 if (!JREFERENCE_TYPE_P (op2_type))
13269 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13270 parse_error_context
13271 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13272 lang_printable_name (op2_type, 0));
13276 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13278 /* If the first operand is null, the result is always false */
13279 if (op1 == null_pointer_node)
13280 return boolean_false_node;
13281 else if (flag_emit_class_files)
13283 TREE_OPERAND (node, 1) = op2_type;
13284 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13287 /* Otherwise we have to invoke instance of to figure it out */
13289 return build_instanceof (op1, op2_type);
13291 /* There is no way the expression operand can be an instance of
13292 the type operand. This is a compile time error. */
13295 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13296 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13297 parse_error_context
13298 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13299 t1, lang_printable_name (op2_type, 0));
13306 /* 15.21 Bitwise and Logical Operators */
13310 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13311 /* Binary numeric promotion is performed on both operand and the
13312 expression retain that type */
13313 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13315 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13316 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13317 /* The type of the bitwise operator expression is BOOLEAN */
13318 prom_type = boolean_type_node;
13321 if (!JINTEGRAL_TYPE_P (op1_type))
13322 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13323 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13324 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13325 TREE_TYPE (node) = error_mark_node;
13327 /* Insert a break here if adding thing before the switch's
13328 break for this case */
13332 /* 15.22 Conditional-And Operator */
13333 case TRUTH_ANDIF_EXPR:
13334 /* 15.23 Conditional-Or Operator */
13335 case TRUTH_ORIF_EXPR:
13336 /* Operands must be of BOOLEAN type */
13337 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13338 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13340 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13341 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13342 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13343 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13344 TREE_TYPE (node) = boolean_type_node;
13348 /* The type of the conditional operators is BOOLEAN */
13349 prom_type = boolean_type_node;
13352 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13357 /* The type of each of the operands must be a primitive numeric
13359 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13361 if (!JNUMERIC_TYPE_P (op1_type))
13362 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13363 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13364 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13365 TREE_TYPE (node) = boolean_type_node;
13369 /* Binary numeric promotion is performed on the operands */
13370 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13371 /* The type of the relation expression is always BOOLEAN */
13372 prom_type = boolean_type_node;
13375 /* 15.20 Equality Operator */
13378 /* It's time for us to patch the strings. */
13379 if ((cn = patch_string (op1)))
13382 op1_type = TREE_TYPE (op1);
13384 if ((cn = patch_string (op2)))
13387 op2_type = TREE_TYPE (op2);
13390 /* 15.20.1 Numerical Equality Operators == and != */
13391 /* Binary numeric promotion is performed on the operands */
13392 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13393 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13395 /* 15.20.2 Boolean Equality Operators == and != */
13396 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13397 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13398 ; /* Nothing to do here */
13400 /* 15.20.3 Reference Equality Operators == and != */
13401 /* Types have to be either references or the null type. If
13402 they're references, it must be possible to convert either
13403 type to the other by casting conversion. */
13404 else if (op1 == null_pointer_node || op2 == null_pointer_node
13405 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13406 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13407 || valid_ref_assignconv_cast_p (op2_type,
13409 ; /* Nothing to do here */
13411 /* Else we have an error figure what can't be converted into
13412 what and report the error */
13416 t1 = xstrdup (lang_printable_name (op1_type, 0));
13417 parse_error_context
13419 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13420 operator_string (node), t1,
13421 lang_printable_name (op2_type, 0));
13423 TREE_TYPE (node) = boolean_type_node;
13427 prom_type = boolean_type_node;
13432 return error_mark_node;
13434 TREE_OPERAND (node, 0) = op1;
13435 TREE_OPERAND (node, 1) = op2;
13436 TREE_TYPE (node) = prom_type;
13437 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13439 if (flag_emit_xref)
13442 /* fold does not respect side-effect order as required for Java but not C.
13443 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13446 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13447 : ! TREE_SIDE_EFFECTS (node))
13448 node = fold (node);
13452 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13453 zero value, the value of CSTE comes after the valude of STRING */
13456 do_merge_string_cste (cste, string, string_len, after)
13458 const char *string;
13459 int string_len, after;
13461 const char *old = TREE_STRING_POINTER (cste);
13462 int old_len = TREE_STRING_LENGTH (cste);
13463 int len = old_len + string_len;
13464 char *new = alloca (len+1);
13468 memcpy (new, string, string_len);
13469 memcpy (&new [string_len], old, old_len);
13473 memcpy (new, old, old_len);
13474 memcpy (&new [old_len], string, string_len);
13477 return build_string (len, new);
13480 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13481 new STRING_CST on success, NULL_TREE on failure */
13484 merge_string_cste (op1, op2, after)
13488 /* Handle two string constants right away */
13489 if (TREE_CODE (op2) == STRING_CST)
13490 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13491 TREE_STRING_LENGTH (op2), after);
13493 /* Reasonable integer constant can be treated right away */
13494 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13496 static const char *boolean_true = "true";
13497 static const char *boolean_false = "false";
13498 static const char *null_pointer = "null";
13500 const char *string;
13502 if (op2 == boolean_true_node)
13503 string = boolean_true;
13504 else if (op2 == boolean_false_node)
13505 string = boolean_false;
13506 else if (op2 == null_pointer_node)
13507 string = null_pointer;
13508 else if (TREE_TYPE (op2) == char_type_node)
13510 ch[0] = (char )TREE_INT_CST_LOW (op2);
13515 string = print_int_node (op2);
13517 return do_merge_string_cste (op1, string, strlen (string), after);
13522 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13523 has to be a STRING_CST and the other part must be a STRING_CST or a
13524 INTEGRAL constant. Return a new STRING_CST if the operation
13525 succeed, NULL_TREE otherwise.
13527 If the case we want to optimize for space, we might want to return
13528 NULL_TREE for each invocation of this routine. FIXME */
13531 string_constant_concatenation (op1, op2)
13534 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13539 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13540 rest = (string == op1 ? op2 : op1);
13541 invert = (string == op1 ? 0 : 1 );
13543 /* Walk REST, only if it looks reasonable */
13544 if (TREE_CODE (rest) != STRING_CST
13545 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13546 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13547 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13549 rest = java_complete_tree (rest);
13550 if (rest == error_mark_node)
13551 return error_mark_node;
13552 rest = fold (rest);
13554 return merge_string_cste (string, rest, invert);
13559 /* Implement the `+' operator. Does static optimization if possible,
13560 otherwise create (if necessary) and append elements to a
13561 StringBuffer. The StringBuffer will be carried around until it is
13562 used for a function call or an assignment. Then toString() will be
13563 called on it to turn it into a String object. */
13566 build_string_concatenation (op1, op2)
13570 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13572 if (flag_emit_xref)
13573 return build (PLUS_EXPR, string_type_node, op1, op2);
13575 /* Try to do some static optimization */
13576 if ((result = string_constant_concatenation (op1, op2)))
13579 /* Discard empty strings on either side of the expression */
13580 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13585 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13588 /* If operands are string constant, turn then into object references */
13589 if (TREE_CODE (op1) == STRING_CST)
13590 op1 = patch_string_cst (op1);
13591 if (op2 && TREE_CODE (op2) == STRING_CST)
13592 op2 = patch_string_cst (op2);
13594 /* If either one of the constant is null and the other non null
13595 operand is a String object, return it. */
13596 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13599 /* If OP1 isn't already a StringBuffer, create and
13600 initialize a new one */
13601 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13603 /* Two solutions here:
13604 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13605 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13606 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13607 op1 = BUILD_STRING_BUFFER (op1);
13610 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13611 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13617 /* OP1 is no longer the last node holding a crafted StringBuffer */
13618 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13619 /* Create a node for `{new...,xxx}.append (op2)' */
13621 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13624 /* Mark the last node holding a crafted StringBuffer */
13625 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13627 TREE_SIDE_EFFECTS (op1) = side_effects;
13631 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13632 StringBuffer. If no string were found to be patched, return
13636 patch_string (node)
13639 if (node == error_mark_node)
13640 return error_mark_node;
13641 if (TREE_CODE (node) == STRING_CST)
13642 return patch_string_cst (node);
13643 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13645 int saved = ctxp->explicit_constructor_p;
13646 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13648 /* Temporary disable forbid the use of `this'. */
13649 ctxp->explicit_constructor_p = 0;
13650 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13651 /* String concatenation arguments must be evaluated in order too. */
13652 ret = force_evaluation_order (ret);
13653 /* Restore it at its previous value */
13654 ctxp->explicit_constructor_p = saved;
13660 /* Build the internal representation of a string constant. */
13663 patch_string_cst (node)
13667 if (! flag_emit_class_files)
13669 node = get_identifier (TREE_STRING_POINTER (node));
13670 location = alloc_name_constant (CONSTANT_String, node);
13671 node = build_ref_from_constant_pool (location);
13673 TREE_TYPE (node) = string_ptr_type_node;
13674 TREE_CONSTANT (node) = 1;
13678 /* Build an incomplete unary operator expression. */
13681 build_unaryop (op_token, op_location, op1)
13682 int op_token, op_location;
13689 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13690 case MINUS_TK: op = NEGATE_EXPR; break;
13691 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13692 case NOT_TK: op = BIT_NOT_EXPR; break;
13696 unaryop = build1 (op, NULL_TREE, op1);
13697 TREE_SIDE_EFFECTS (unaryop) = 1;
13698 /* Store the location of the operator, for better error report. The
13699 string of the operator will be rebuild based on the OP value. */
13700 EXPR_WFL_LINECOL (unaryop) = op_location;
13704 /* Special case for the ++/-- operators, since they require an extra
13705 argument to build, which is set to NULL and patched
13706 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13709 build_incdec (op_token, op_location, op1, is_post_p)
13710 int op_token, op_location;
13714 static enum tree_code lookup [2][2] =
13716 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13717 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13719 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13720 NULL_TREE, op1, NULL_TREE);
13721 TREE_SIDE_EFFECTS (node) = 1;
13722 /* Store the location of the operator, for better error report. The
13723 string of the operator will be rebuild based on the OP value. */
13724 EXPR_WFL_LINECOL (node) = op_location;
13728 /* Build an incomplete cast operator, based on the use of the
13729 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13730 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13731 though its type is already set. */
13734 build_cast (location, type, exp)
13738 tree node = build1 (CONVERT_EXPR, type, exp);
13739 EXPR_WFL_LINECOL (node) = location;
13743 /* Build an incomplete class reference operator. */
13745 build_incomplete_class_ref (location, class_name)
13749 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13750 EXPR_WFL_LINECOL (node) = location;
13754 /* Complete an incomplete class reference operator. */
13756 patch_incomplete_class_ref (node)
13759 tree type = TREE_OPERAND (node, 0);
13762 if (!(ref_type = resolve_type_during_patch (type)))
13763 return error_mark_node;
13765 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
13767 /* A class referenced by `foo.class' is initialized. */
13768 return build_class_init (ref_type, build_class_ref (ref_type));
13771 /* If we're emitting class files and we have to deal with non
13772 primitive types, we invoke (and consider generating) the
13773 synthetic static method `class$'. */
13774 if (!TYPE_DOT_CLASS (current_class))
13775 build_dot_class_method (current_class);
13776 ref_type = build_dot_class_method_invocation (ref_type);
13777 return java_complete_tree (ref_type);
13780 /* 15.14 Unary operators. We return error_mark_node in case of error,
13781 but preserve the type of NODE if the type is fixed. */
13784 patch_unaryop (node, wfl_op)
13788 tree op = TREE_OPERAND (node, 0);
13789 tree op_type = TREE_TYPE (op);
13790 tree prom_type = NULL_TREE, value, decl;
13791 int outer_field_flag = 0;
13792 int code = TREE_CODE (node);
13793 int error_found = 0;
13795 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13799 /* 15.13.2 Postfix Increment Operator ++ */
13800 case POSTINCREMENT_EXPR:
13801 /* 15.13.3 Postfix Increment Operator -- */
13802 case POSTDECREMENT_EXPR:
13803 /* 15.14.1 Prefix Increment Operator ++ */
13804 case PREINCREMENT_EXPR:
13805 /* 15.14.2 Prefix Decrement Operator -- */
13806 case PREDECREMENT_EXPR:
13807 op = decl = strip_out_static_field_access_decl (op);
13808 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13809 /* We might be trying to change an outer field accessed using
13811 if (outer_field_flag)
13813 /* Retrieve the decl of the field we're trying to access. We
13814 do that by first retrieving the function we would call to
13815 access the field. It has been already verified that this
13816 field isn't final */
13817 if (flag_emit_class_files)
13818 decl = TREE_OPERAND (op, 0);
13820 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13821 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13823 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
13824 else if (!JDECL_P (decl)
13825 && TREE_CODE (decl) != COMPONENT_REF
13826 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13827 && TREE_CODE (decl) != INDIRECT_REF
13828 && !(TREE_CODE (decl) == COMPOUND_EXPR
13829 && TREE_OPERAND (decl, 1)
13830 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
13833 /* Before screaming, check that we're not in fact trying to
13834 increment a optimized static final access, in which case
13835 we issue an different error message. */
13836 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13837 && resolve_expression_name (wfl_op, &lvalue)
13838 && check_final_assignment (lvalue, wfl_op)))
13839 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13840 operator_string (node));
13841 TREE_TYPE (node) = error_mark_node;
13845 if (check_final_assignment (op, wfl_op))
13848 /* From now on, we know that op if a variable and that it has a
13849 valid wfl. We use wfl_op to locate errors related to the
13851 else if (!JNUMERIC_TYPE_P (op_type))
13853 parse_error_context
13854 (wfl_op, "Invalid argument type `%s' to `%s'",
13855 lang_printable_name (op_type, 0), operator_string (node));
13856 TREE_TYPE (node) = error_mark_node;
13861 /* Before the addition, binary numeric promotion is performed on
13862 both operands, if really necessary */
13863 if (JINTEGRAL_TYPE_P (op_type))
13865 value = build_int_2 (1, 0);
13866 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13870 value = build_int_2 (1, 0);
13872 binary_numeric_promotion (op_type,
13873 TREE_TYPE (value), &op, &value);
13876 /* We remember we might be accessing an outer field */
13877 if (outer_field_flag)
13879 /* We re-generate an access to the field */
13880 value = build (PLUS_EXPR, TREE_TYPE (op),
13881 build_outer_field_access (wfl_op, decl), value);
13883 /* And we patch the original access$() into a write
13884 with plus_op as a rhs */
13885 return outer_field_access_fix (node, op, value);
13888 /* And write back into the node. */
13889 TREE_OPERAND (node, 0) = op;
13890 TREE_OPERAND (node, 1) = value;
13891 /* Convert the overall back into its original type, if
13892 necessary, and return */
13893 if (JINTEGRAL_TYPE_P (op_type))
13894 return fold (node);
13896 return fold (convert (op_type, node));
13900 /* 15.14.3 Unary Plus Operator + */
13901 case UNARY_PLUS_EXPR:
13902 /* 15.14.4 Unary Minus Operator - */
13904 if (!JNUMERIC_TYPE_P (op_type))
13906 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13907 TREE_TYPE (node) = error_mark_node;
13910 /* Unary numeric promotion is performed on operand */
13913 op = do_unary_numeric_promotion (op);
13914 prom_type = TREE_TYPE (op);
13915 if (code == UNARY_PLUS_EXPR)
13920 /* 15.14.5 Bitwise Complement Operator ~ */
13922 if (!JINTEGRAL_TYPE_P (op_type))
13924 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13925 TREE_TYPE (node) = error_mark_node;
13930 op = do_unary_numeric_promotion (op);
13931 prom_type = TREE_TYPE (op);
13935 /* 15.14.6 Logical Complement Operator ! */
13936 case TRUTH_NOT_EXPR:
13937 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13939 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
13940 /* But the type is known. We will report an error if further
13941 attempt of a assignment is made with this rhs */
13942 TREE_TYPE (node) = boolean_type_node;
13946 prom_type = boolean_type_node;
13949 /* 15.15 Cast Expression */
13951 value = patch_cast (node, wfl_operator);
13952 if (value == error_mark_node)
13954 /* If this cast is part of an assignment, we tell the code
13955 that deals with it not to complain about a mismatch,
13956 because things have been cast, anyways */
13957 TREE_TYPE (node) = error_mark_node;
13962 value = fold (value);
13963 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13970 return error_mark_node;
13972 /* There are cases where node has been replaced by something else
13973 and we don't end up returning here: UNARY_PLUS_EXPR,
13974 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
13975 TREE_OPERAND (node, 0) = fold (op);
13976 TREE_TYPE (node) = prom_type;
13977 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
13978 return fold (node);
13981 /* Generic type resolution that sometimes takes place during node
13982 patching. Returned the resolved type or generate an error
13983 message. Return the resolved type or NULL_TREE. */
13986 resolve_type_during_patch (type)
13989 if (unresolved_type_p (type, NULL))
13991 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
13994 parse_error_context (type,
13995 "Class `%s' not found in type declaration",
13996 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13999 return TREE_TYPE (type_decl);
14003 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
14004 found. Otherwise NODE or something meant to replace it is returned. */
14007 patch_cast (node, wfl_op)
14011 tree op = TREE_OPERAND (node, 0);
14012 tree op_type = TREE_TYPE (op);
14013 tree cast_type = TREE_TYPE (node);
14016 /* First resolve OP_TYPE if unresolved */
14017 if (!(cast_type = resolve_type_during_patch (cast_type)))
14018 return error_mark_node;
14020 /* Check on cast that are proven correct at compile time */
14021 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
14024 if (cast_type == op_type)
14027 /* float and double type are converted to the original type main
14028 variant and then to the target type. */
14029 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
14030 op = convert (integer_type_node, op);
14032 /* Try widening/narowwing convertion. Potentially, things need
14033 to be worked out in gcc so we implement the extreme cases
14034 correctly. fold_convert() needs to be fixed. */
14035 return convert (cast_type, op);
14038 /* It's also valid to cast a boolean into a boolean */
14039 if (op_type == boolean_type_node && cast_type == boolean_type_node)
14042 /* null can be casted to references */
14043 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
14044 return build_null_of_type (cast_type);
14046 /* The remaining legal casts involve conversion between reference
14047 types. Check for their compile time correctness. */
14048 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
14049 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
14051 TREE_TYPE (node) = promote_type (cast_type);
14052 /* Now, the case can be determined correct at compile time if
14053 OP_TYPE can be converted into CAST_TYPE by assignment
14054 conversion (5.2) */
14056 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
14058 TREE_SET_CODE (node, NOP_EXPR);
14062 if (flag_emit_class_files)
14064 TREE_SET_CODE (node, CONVERT_EXPR);
14068 /* The cast requires a run-time check */
14069 return build (CALL_EXPR, promote_type (cast_type),
14070 build_address_of (soft_checkcast_node),
14071 tree_cons (NULL_TREE, build_class_ref (cast_type),
14072 build_tree_list (NULL_TREE, op)),
14076 /* Any other casts are proven incorrect at compile time */
14077 t1 = xstrdup (lang_printable_name (op_type, 0));
14078 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
14079 t1, lang_printable_name (cast_type, 0));
14081 return error_mark_node;
14084 /* Build a null constant and give it the type TYPE. */
14087 build_null_of_type (type)
14090 tree node = build_int_2 (0, 0);
14091 TREE_TYPE (node) = promote_type (type);
14095 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14096 a list of indices. */
14098 build_array_ref (location, array, index)
14102 tree node = build (ARRAY_REF, NULL_TREE, array, index);
14103 EXPR_WFL_LINECOL (node) = location;
14107 /* 15.12 Array Access Expression */
14110 patch_array_ref (node)
14113 tree array = TREE_OPERAND (node, 0);
14114 tree array_type = TREE_TYPE (array);
14115 tree index = TREE_OPERAND (node, 1);
14116 tree index_type = TREE_TYPE (index);
14117 int error_found = 0;
14119 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14121 if (TREE_CODE (array_type) == POINTER_TYPE)
14122 array_type = TREE_TYPE (array_type);
14124 /* The array reference must be an array */
14125 if (!TYPE_ARRAY_P (array_type))
14127 parse_error_context
14129 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14130 lang_printable_name (array_type, 0));
14131 TREE_TYPE (node) = error_mark_node;
14135 /* The array index undergoes unary numeric promotion. The promoted
14136 type must be int */
14137 index = do_unary_numeric_promotion (index);
14138 if (TREE_TYPE (index) != int_type_node)
14140 if (valid_cast_to_p (index_type, int_type_node))
14141 parse_error_context (wfl_operator,
14142 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
14143 lang_printable_name (index_type, 0));
14145 parse_error_context (wfl_operator,
14146 "Incompatible type for `[]'. Can't convert `%s' to `int'",
14147 lang_printable_name (index_type, 0));
14148 TREE_TYPE (node) = error_mark_node;
14153 return error_mark_node;
14155 array_type = TYPE_ARRAY_ELEMENT (array_type);
14157 if (flag_emit_class_files || flag_emit_xref)
14159 TREE_OPERAND (node, 0) = array;
14160 TREE_OPERAND (node, 1) = index;
14164 /* The save_expr is for correct evaluation order. It would be cleaner
14165 to use force_evaluation_order (see comment there), but that is
14166 difficult when we also have to deal with bounds checking. */
14167 if (TREE_SIDE_EFFECTS (index))
14168 array = save_expr (array);
14169 node = build_java_arrayaccess (array, array_type, index);
14170 if (TREE_SIDE_EFFECTS (index))
14171 node = build (COMPOUND_EXPR, array_type, array, node);
14173 TREE_TYPE (node) = array_type;
14177 /* 15.9 Array Creation Expressions */
14180 build_newarray_node (type, dims, extra_dims)
14186 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
14187 build_int_2 (extra_dims, 0));
14192 patch_newarray (node)
14195 tree type = TREE_OPERAND (node, 0);
14196 tree dims = TREE_OPERAND (node, 1);
14197 tree cdim, array_type;
14198 int error_found = 0;
14200 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14202 /* Dimension types are verified. It's better for the types to be
14203 verified in order. */
14204 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14207 tree dim = TREE_VALUE (cdim);
14209 /* Dim might have been saved during its evaluation */
14210 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14212 /* The type of each specified dimension must be an integral type. */
14213 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14216 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14217 promoted type must be int. */
14220 dim = do_unary_numeric_promotion (dim);
14221 if (TREE_TYPE (dim) != int_type_node)
14225 /* Report errors on types here */
14228 parse_error_context
14229 (TREE_PURPOSE (cdim),
14230 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
14231 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14232 "Explicit cast needed to" : "Can't"),
14233 lang_printable_name (TREE_TYPE (dim), 0));
14237 TREE_PURPOSE (cdim) = NULL_TREE;
14240 /* Resolve array base type if unresolved */
14241 if (!(type = resolve_type_during_patch (type)))
14246 /* We don't want further evaluation of this bogus array creation
14248 TREE_TYPE (node) = error_mark_node;
14249 return error_mark_node;
14252 /* Set array_type to the actual (promoted) array type of the result. */
14253 if (TREE_CODE (type) == RECORD_TYPE)
14254 type = build_pointer_type (type);
14255 while (--xdims >= 0)
14257 type = promote_type (build_java_array_type (type, -1));
14259 dims = nreverse (dims);
14261 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14265 = build_java_array_type (type,
14266 TREE_CODE (cdim) == INTEGER_CST
14267 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14269 array_type = promote_type (array_type);
14271 dims = nreverse (dims);
14273 /* The node is transformed into a function call. Things are done
14274 differently according to the number of dimensions. If the number
14275 of dimension is equal to 1, then the nature of the base type
14276 (primitive or not) matters. */
14278 return build_new_array (type, TREE_VALUE (dims));
14280 /* Can't reuse what's already written in expr.c because it uses the
14281 JVM stack representation. Provide a build_multianewarray. FIXME */
14282 return build (CALL_EXPR, array_type,
14283 build_address_of (soft_multianewarray_node),
14284 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
14285 tree_cons (NULL_TREE,
14286 build_int_2 (ndims, 0), dims )),
14290 /* 10.6 Array initializer. */
14292 /* Build a wfl for array element that don't have one, so we can
14293 pin-point errors. */
14296 maybe_build_array_element_wfl (node)
14299 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14300 return build_expr_wfl (NULL_TREE, ctxp->filename,
14301 ctxp->elc.line, ctxp->elc.prev_col);
14306 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14307 identification of initialized arrays easier to detect during walk
14311 build_new_array_init (location, values)
14315 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14316 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14317 EXPR_WFL_LINECOL (to_return) = location;
14321 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14322 occurred. Otherwise return NODE after having set its type
14326 patch_new_array_init (type, node)
14329 int error_seen = 0;
14330 tree current, element_type;
14331 HOST_WIDE_INT length;
14332 int all_constant = 1;
14333 tree init = TREE_OPERAND (node, 0);
14335 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14337 parse_error_context (node,
14338 "Invalid array initializer for non-array type `%s'",
14339 lang_printable_name (type, 1));
14340 return error_mark_node;
14342 type = TREE_TYPE (type);
14343 element_type = TYPE_ARRAY_ELEMENT (type);
14345 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14347 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14348 current; length++, current = TREE_CHAIN (current))
14350 tree elt = TREE_VALUE (current);
14351 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14353 error_seen |= array_constructor_check_entry (element_type, current);
14354 elt = TREE_VALUE (current);
14355 /* When compiling to native code, STRING_CST is converted to
14356 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14357 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14362 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14363 TREE_PURPOSE (current) = NULL_TREE;
14366 if (elt && TREE_CODE (elt) == TREE_LIST
14367 && TREE_VALUE (elt) == error_mark_node)
14372 return error_mark_node;
14374 /* Create a new type. We can't reuse the one we have here by
14375 patching its dimension because it originally is of dimension -1
14376 hence reused by gcc. This would prevent triangular arrays. */
14377 type = build_java_array_type (element_type, length);
14378 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14379 TREE_TYPE (node) = promote_type (type);
14380 TREE_CONSTANT (init) = all_constant;
14381 TREE_CONSTANT (node) = all_constant;
14385 /* Verify that one entry of the initializer element list can be
14386 assigned to the array base type. Report 1 if an error occurred, 0
14390 array_constructor_check_entry (type, entry)
14393 char *array_type_string = NULL; /* For error reports */
14394 tree value, type_value, new_value, wfl_value, patched;
14395 int error_seen = 0;
14397 new_value = NULL_TREE;
14398 wfl_value = TREE_VALUE (entry);
14400 value = java_complete_tree (TREE_VALUE (entry));
14401 /* patch_string return error_mark_node if arg is error_mark_node */
14402 if ((patched = patch_string (value)))
14404 if (value == error_mark_node)
14407 type_value = TREE_TYPE (value);
14409 /* At anytime, try_builtin_assignconv can report a warning on
14410 constant overflow during narrowing. */
14411 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14412 new_value = try_builtin_assignconv (wfl_operator, type, value);
14413 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14414 type_value = promote_type (type);
14416 /* Check and report errors */
14419 const char *msg = (!valid_cast_to_p (type_value, type) ?
14420 "Can't" : "Explicit cast needed to");
14421 if (!array_type_string)
14422 array_type_string = xstrdup (lang_printable_name (type, 1));
14423 parse_error_context
14424 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14425 msg, lang_printable_name (type_value, 1), array_type_string);
14431 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
14432 TREE_VALUE (entry) = new_value;
14435 if (array_type_string)
14436 free (array_type_string);
14438 TREE_PURPOSE (entry) = NULL_TREE;
14443 build_this (location)
14446 tree node = build_wfl_node (this_identifier_node);
14447 TREE_SET_CODE (node, THIS_EXPR);
14448 EXPR_WFL_LINECOL (node) = location;
14452 /* 14.15 The return statement. It builds a modify expression that
14453 assigns the returned value to the RESULT_DECL that hold the value
14457 build_return (location, op)
14461 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14462 EXPR_WFL_LINECOL (node) = location;
14463 node = build_debugable_stmt (location, node);
14468 patch_return (node)
14471 tree return_exp = TREE_OPERAND (node, 0);
14472 tree meth = current_function_decl;
14473 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14474 int error_found = 0;
14476 TREE_TYPE (node) = error_mark_node;
14477 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14479 /* It's invalid to have a return value within a function that is
14480 declared with the keyword void or that is a constructor */
14481 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14484 /* It's invalid to use a return statement in a static block */
14485 if (DECL_CLINIT_P (current_function_decl))
14488 /* It's invalid to have a no return value within a function that
14489 isn't declared with the keyword `void' */
14490 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14493 if (in_instance_initializer)
14498 if (in_instance_initializer)
14499 parse_error_context (wfl_operator,
14500 "`return' inside instance initializer");
14502 else if (DECL_CLINIT_P (current_function_decl))
14503 parse_error_context (wfl_operator,
14504 "`return' inside static initializer");
14506 else if (!DECL_CONSTRUCTOR_P (meth))
14508 char *t = xstrdup (lang_printable_name (mtype, 0));
14509 parse_error_context (wfl_operator,
14510 "`return' with%s value from `%s %s'",
14511 (error_found == 1 ? "" : "out"),
14512 t, lang_printable_name (meth, 0));
14516 parse_error_context (wfl_operator,
14517 "`return' with value from constructor `%s'",
14518 lang_printable_name (meth, 0));
14519 return error_mark_node;
14522 /* If we have a return_exp, build a modify expression and expand
14523 it. Note: at that point, the assignment is declared valid, but we
14524 may want to carry some more hacks */
14527 tree exp = java_complete_tree (return_exp);
14528 tree modify, patched;
14530 /* If the function returned value and EXP are booleans, EXP has
14531 to be converted into the type of DECL_RESULT, which is integer
14532 (see complete_start_java_method) */
14533 if (TREE_TYPE (exp) == boolean_type_node &&
14534 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14535 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14537 /* `null' can be assigned to a function returning a reference */
14538 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14539 exp == null_pointer_node)
14540 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14542 if ((patched = patch_string (exp)))
14545 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14546 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14547 modify = java_complete_tree (modify);
14549 if (modify != error_mark_node)
14551 TREE_SIDE_EFFECTS (modify) = 1;
14552 TREE_OPERAND (node, 0) = modify;
14555 return error_mark_node;
14557 TREE_TYPE (node) = void_type_node;
14558 TREE_SIDE_EFFECTS (node) = 1;
14562 /* 14.8 The if Statement */
14565 build_if_else_statement (location, expression, if_body, else_body)
14567 tree expression, if_body, else_body;
14571 else_body = empty_stmt_node;
14572 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14573 EXPR_WFL_LINECOL (node) = location;
14574 node = build_debugable_stmt (location, node);
14579 patch_if_else_statement (node)
14582 tree expression = TREE_OPERAND (node, 0);
14584 TREE_TYPE (node) = error_mark_node;
14585 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14587 /* The type of expression must be boolean */
14588 if (TREE_TYPE (expression) != boolean_type_node
14589 && TREE_TYPE (expression) != promoted_boolean_type_node)
14591 parse_error_context
14593 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
14594 lang_printable_name (TREE_TYPE (expression), 0));
14595 return error_mark_node;
14598 TREE_TYPE (node) = void_type_node;
14599 TREE_SIDE_EFFECTS (node) = 1;
14600 CAN_COMPLETE_NORMALLY (node)
14601 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14602 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
14606 /* 14.6 Labeled Statements */
14608 /* Action taken when a lableled statement is parsed. a new
14609 LABELED_BLOCK_EXPR is created. No statement is attached to the
14610 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
14613 build_labeled_block (location, label)
14618 tree label_decl, node;
14619 if (label == NULL_TREE || label == continue_identifier_node)
14620 label_name = label;
14623 label_name = merge_qualified_name (label_id, label);
14624 /* Issue an error if we try to reuse a label that was previously
14626 if (IDENTIFIER_LOCAL_VALUE (label_name))
14628 EXPR_WFL_LINECOL (wfl_operator) = location;
14629 parse_error_context (wfl_operator,
14630 "Declaration of `%s' shadows a previous label declaration",
14631 IDENTIFIER_POINTER (label));
14632 EXPR_WFL_LINECOL (wfl_operator) =
14633 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14634 parse_error_context (wfl_operator,
14635 "This is the location of the previous declaration of label `%s'",
14636 IDENTIFIER_POINTER (label));
14637 java_error_count--;
14641 label_decl = create_label_decl (label_name);
14642 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14643 EXPR_WFL_LINECOL (node) = location;
14644 TREE_SIDE_EFFECTS (node) = 1;
14648 /* A labeled statement LBE is attached a statement. */
14651 finish_labeled_statement (lbe, statement)
14652 tree lbe; /* Labeled block expr */
14655 /* In anyways, tie the loop to its statement */
14656 LABELED_BLOCK_BODY (lbe) = statement;
14657 pop_labeled_block ();
14658 POP_LABELED_BLOCK ();
14662 /* 14.10, 14.11, 14.12 Loop Statements */
14664 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14668 build_new_loop (loop_body)
14671 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14672 TREE_SIDE_EFFECTS (loop) = 1;
14677 /* Create a loop body according to the following structure:
14679 COMPOUND_EXPR (loop main body)
14680 EXIT_EXPR (this order is for while/for loops.
14681 LABELED_BLOCK_EXPR the order is reversed for do loops)
14682 LABEL_DECL (a continue occuring here branches at the
14683 BODY end of this labeled block)
14686 REVERSED, if non zero, tells that the loop condition expr comes
14687 after the body, like in the do-while loop.
14689 To obtain a loop, the loop body structure described above is
14690 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14693 LABEL_DECL (use this label to exit the loop)
14695 <structure described above> */
14698 build_loop_body (location, condition, reversed)
14703 tree first, second, body;
14705 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14706 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14707 condition = build_debugable_stmt (location, condition);
14708 TREE_SIDE_EFFECTS (condition) = 1;
14710 body = build_labeled_block (0, continue_identifier_node);
14711 first = (reversed ? body : condition);
14712 second = (reversed ? condition : body);
14714 build (COMPOUND_EXPR, NULL_TREE,
14715 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14718 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14719 their order) on the current loop. Unlink the current loop from the
14723 finish_loop_body (location, condition, body, reversed)
14725 tree condition, body;
14728 tree to_return = ctxp->current_loop;
14729 tree loop_body = LOOP_EXPR_BODY (to_return);
14732 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14733 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14734 The real EXIT_EXPR is one operand further. */
14735 EXPR_WFL_LINECOL (cnode) = location;
14736 /* This one is for accurate error reports */
14737 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14738 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14740 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14745 /* Tailored version of finish_loop_body for FOR loops, when FOR
14746 loops feature the condition part */
14749 finish_for_loop (location, condition, update, body)
14751 tree condition, update, body;
14753 /* Put the condition and the loop body in place */
14754 tree loop = finish_loop_body (location, condition, body, 0);
14755 /* LOOP is the current loop which has been now popped of the loop
14756 stack. Install the update block */
14757 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14761 /* Try to find the loop a block might be related to. This comprises
14762 the case where the LOOP_EXPR is found as the second operand of a
14763 COMPOUND_EXPR, because the loop happens to have an initialization
14764 part, then expressed as the first operand of the COMPOUND_EXPR. If
14765 the search finds something, 1 is returned. Otherwise, 0 is
14766 returned. The search is assumed to start from a
14767 LABELED_BLOCK_EXPR's block. */
14770 search_loop (statement)
14773 if (TREE_CODE (statement) == LOOP_EXPR)
14776 if (TREE_CODE (statement) == BLOCK)
14777 statement = BLOCK_SUBBLOCKS (statement);
14781 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14782 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14783 statement = TREE_OPERAND (statement, 1);
14785 return (TREE_CODE (statement) == LOOP_EXPR
14786 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
14789 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14790 returned otherwise. */
14793 labeled_block_contains_loop_p (block, loop)
14799 if (LABELED_BLOCK_BODY (block) == loop)
14802 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
14808 /* If the loop isn't surrounded by a labeled statement, create one and
14809 insert LOOP as its body. */
14812 patch_loop_statement (loop)
14817 TREE_TYPE (loop) = void_type_node;
14818 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14821 loop_label = build_labeled_block (0, NULL_TREE);
14822 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14823 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
14824 LABELED_BLOCK_BODY (loop_label) = loop;
14825 PUSH_LABELED_BLOCK (loop_label);
14829 /* 14.13, 14.14: break and continue Statements */
14831 /* Build a break or a continue statement. a null NAME indicates an
14832 unlabeled break/continue statement. */
14835 build_bc_statement (location, is_break, name)
14836 int location, is_break;
14839 tree break_continue, label_block_expr = NULL_TREE;
14843 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14844 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14845 /* Null means that we don't have a target for this named
14846 break/continue. In this case, we make the target to be the
14847 label name, so that the error can be reported accuratly in
14848 patch_bc_statement. */
14849 label_block_expr = EXPR_WFL_NODE (name);
14851 /* Unlabeled break/continue will be handled during the
14852 break/continue patch operation */
14854 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14856 IS_BREAK_STMT_P (break_continue) = is_break;
14857 TREE_SIDE_EFFECTS (break_continue) = 1;
14858 EXPR_WFL_LINECOL (break_continue) = location;
14859 break_continue = build_debugable_stmt (location, break_continue);
14860 return break_continue;
14863 /* Verification of a break/continue statement. */
14866 patch_bc_statement (node)
14869 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
14870 tree labeled_block = ctxp->current_labeled_block;
14871 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14873 /* Having an identifier here means that the target is unknown. */
14874 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
14876 parse_error_context (wfl_operator, "No label definition found for `%s'",
14877 IDENTIFIER_POINTER (bc_label));
14878 return error_mark_node;
14880 if (! IS_BREAK_STMT_P (node))
14882 /* It's a continue statement. */
14883 for (;; labeled_block = TREE_CHAIN (labeled_block))
14885 if (labeled_block == NULL_TREE)
14887 if (bc_label == NULL_TREE)
14888 parse_error_context (wfl_operator,
14889 "`continue' must be in loop");
14891 parse_error_context
14892 (wfl_operator, "continue label `%s' does not name a loop",
14893 IDENTIFIER_POINTER (bc_label));
14894 return error_mark_node;
14896 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14897 == continue_identifier_node)
14898 && (bc_label == NULL_TREE
14899 || TREE_CHAIN (labeled_block) == bc_label))
14901 bc_label = labeled_block;
14906 else if (!bc_label)
14908 for (;; labeled_block = TREE_CHAIN (labeled_block))
14910 if (labeled_block == NULL_TREE)
14912 parse_error_context (wfl_operator,
14913 "`break' must be in loop or switch");
14914 return error_mark_node;
14916 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14917 if (TREE_CODE (target_stmt) == SWITCH_EXPR
14918 || search_loop (target_stmt))
14920 bc_label = labeled_block;
14926 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
14927 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14929 /* Our break/continue don't return values. */
14930 TREE_TYPE (node) = void_type_node;
14931 /* Encapsulate the break within a compound statement so that it's
14932 expanded all the times by expand_expr (and not clobbered
14933 sometimes, like after a if statement) */
14934 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14935 TREE_SIDE_EFFECTS (node) = 1;
14939 /* Process the exit expression belonging to a loop. Its type must be
14943 patch_exit_expr (node)
14946 tree expression = TREE_OPERAND (node, 0);
14947 TREE_TYPE (node) = error_mark_node;
14948 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14950 /* The type of expression must be boolean */
14951 if (TREE_TYPE (expression) != boolean_type_node)
14953 parse_error_context
14955 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
14956 lang_printable_name (TREE_TYPE (expression), 0));
14957 return error_mark_node;
14959 /* Now we know things are allright, invert the condition, fold and
14961 TREE_OPERAND (node, 0) =
14962 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
14964 if (! integer_zerop (TREE_OPERAND (node, 0))
14965 && ctxp->current_loop != NULL_TREE
14966 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14967 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14968 if (! integer_onep (TREE_OPERAND (node, 0)))
14969 CAN_COMPLETE_NORMALLY (node) = 1;
14972 TREE_TYPE (node) = void_type_node;
14976 /* 14.9 Switch statement */
14979 patch_switch_statement (node)
14982 tree se = TREE_OPERAND (node, 0), se_type;
14984 /* Complete the switch expression */
14985 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14986 se_type = TREE_TYPE (se);
14987 /* The type of the switch expression must be char, byte, short or
14989 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
14991 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14992 parse_error_context (wfl_operator,
14993 "Incompatible type for `switch'. Can't convert `%s' to `int'",
14994 lang_printable_name (se_type, 0));
14995 /* This is what java_complete_tree will check */
14996 TREE_OPERAND (node, 0) = error_mark_node;
14997 return error_mark_node;
15000 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
15002 /* Ready to return */
15003 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
15005 TREE_TYPE (node) = error_mark_node;
15006 return error_mark_node;
15008 TREE_TYPE (node) = void_type_node;
15009 TREE_SIDE_EFFECTS (node) = 1;
15010 CAN_COMPLETE_NORMALLY (node)
15011 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
15012 || ! SWITCH_HAS_DEFAULT (node);
15016 /* 14.18 The try/catch statements */
15019 build_try_statement (location, try_block, catches)
15021 tree try_block, catches;
15023 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
15024 EXPR_WFL_LINECOL (node) = location;
15029 build_try_finally_statement (location, try_block, finally)
15031 tree try_block, finally;
15033 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
15034 EXPR_WFL_LINECOL (node) = location;
15039 patch_try_statement (node)
15042 int error_found = 0;
15043 tree try = TREE_OPERAND (node, 0);
15044 /* Exception handlers are considered in left to right order */
15045 tree catch = nreverse (TREE_OPERAND (node, 1));
15046 tree current, caught_type_list = NULL_TREE;
15048 /* Check catch clauses, if any. Every time we find an error, we try
15049 to process the next catch clause. We process the catch clause before
15050 the try block so that when processing the try block we can check thrown
15051 exceptions againts the caught type list. */
15052 for (current = catch; current; current = TREE_CHAIN (current))
15054 tree carg_decl, carg_type;
15055 tree sub_current, catch_block, catch_clause;
15058 /* At this point, the structure of the catch clause is
15059 CATCH_EXPR (catch node)
15060 BLOCK (with the decl of the parameter)
15062 MODIFY_EXPR (assignment of the catch parameter)
15063 BLOCK (catch clause block)
15065 catch_clause = TREE_OPERAND (current, 0);
15066 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
15067 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
15069 /* Catch clauses can't have more than one parameter declared,
15070 but it's already enforced by the grammar. Make sure that the
15071 only parameter of the clause statement in of class Throwable
15072 or a subclass of Throwable, but that was done earlier. The
15073 catch clause parameter type has also been resolved. */
15075 /* Just make sure that the catch clause parameter type inherits
15076 from java.lang.Throwable */
15077 if (!inherits_from_p (carg_type, throwable_type_node))
15079 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15080 parse_error_context (wfl_operator,
15081 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
15082 lang_printable_name (carg_type, 0));
15087 /* Partial check for unreachable catch statement: The catch
15088 clause is reachable iff is no earlier catch block A in
15089 the try statement such that the type of the catch
15090 clause's parameter is the same as or a subclass of the
15091 type of A's parameter */
15093 for (sub_current = catch;
15094 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15096 tree sub_catch_clause, decl;
15097 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15098 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15100 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15102 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15103 parse_error_context
15105 "`catch' not reached because of the catch clause at line %d",
15106 EXPR_WFL_LINENO (sub_current));
15107 unreachable = error_found = 1;
15111 /* Complete the catch clause block */
15112 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15113 if (catch_block == error_mark_node)
15118 if (CAN_COMPLETE_NORMALLY (catch_block))
15119 CAN_COMPLETE_NORMALLY (node) = 1;
15120 TREE_OPERAND (current, 0) = catch_block;
15125 /* Things to do here: the exception must be thrown */
15127 /* Link this type to the caught type list */
15128 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15131 PUSH_EXCEPTIONS (caught_type_list);
15132 if ((try = java_complete_tree (try)) == error_mark_node)
15134 if (CAN_COMPLETE_NORMALLY (try))
15135 CAN_COMPLETE_NORMALLY (node) = 1;
15138 /* Verification ends here */
15140 return error_mark_node;
15142 TREE_OPERAND (node, 0) = try;
15143 TREE_OPERAND (node, 1) = catch;
15144 TREE_TYPE (node) = void_type_node;
15148 /* 14.17 The synchronized Statement */
15151 patch_synchronized_statement (node, wfl_op1)
15152 tree node, wfl_op1;
15154 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15155 tree block = TREE_OPERAND (node, 1);
15157 tree tmp, enter, exit, expr_decl, assignment;
15159 if (expr == error_mark_node)
15161 block = java_complete_tree (block);
15165 /* We might be trying to synchronize on a STRING_CST */
15166 if ((tmp = patch_string (expr)))
15169 /* The TYPE of expr must be a reference type */
15170 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15172 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15173 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
15174 lang_printable_name (TREE_TYPE (expr), 0));
15175 return error_mark_node;
15178 if (flag_emit_xref)
15180 TREE_OPERAND (node, 0) = expr;
15181 TREE_OPERAND (node, 1) = java_complete_tree (block);
15182 CAN_COMPLETE_NORMALLY (node) = 1;
15186 /* Generate a try-finally for the synchronized statement, except
15187 that the handler that catches all throw exception calls
15188 _Jv_MonitorExit and then rethrow the exception.
15189 The synchronized statement is then implemented as:
15192 _Jv_MonitorEnter (expression)
15194 _Jv_MonitorExit (expression)
15198 e = _Jv_exception_info ();
15199 _Jv_MonitorExit (expression)
15203 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15204 BUILD_MONITOR_ENTER (enter, expr_decl);
15205 BUILD_MONITOR_EXIT (exit, expr_decl);
15206 CAN_COMPLETE_NORMALLY (enter) = 1;
15207 CAN_COMPLETE_NORMALLY (exit) = 1;
15208 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15209 TREE_SIDE_EFFECTS (assignment) = 1;
15210 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
15211 build (COMPOUND_EXPR, NULL_TREE,
15212 build (WITH_CLEANUP_EXPR, NULL_TREE,
15213 build (COMPOUND_EXPR, NULL_TREE,
15214 assignment, enter),
15217 node = build_expr_block (node, expr_decl);
15219 return java_complete_tree (node);
15222 /* 14.16 The throw Statement */
15225 patch_throw_statement (node, wfl_op1)
15226 tree node, wfl_op1;
15228 tree expr = TREE_OPERAND (node, 0);
15229 tree type = TREE_TYPE (expr);
15230 int unchecked_ok = 0, tryblock_throws_ok = 0;
15232 /* Thrown expression must be assignable to java.lang.Throwable */
15233 if (!try_reference_assignconv (throwable_type_node, expr))
15235 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15236 parse_error_context (wfl_operator,
15237 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
15238 lang_printable_name (type, 0));
15239 /* If the thrown expression was a reference, we further the
15240 compile-time check. */
15241 if (!JREFERENCE_TYPE_P (type))
15242 return error_mark_node;
15245 /* At least one of the following must be true */
15247 /* The type of the throw expression is a not checked exception,
15248 i.e. is a unchecked expression. */
15249 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15251 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15252 /* An instance can't throw a checked excetion unless that exception
15253 is explicitely declared in the `throws' clause of each
15254 constructor. This doesn't apply to anonymous classes, since they
15255 don't have declared constructors. */
15257 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
15260 for (current = TYPE_METHODS (current_class); current;
15261 current = TREE_CHAIN (current))
15262 if (DECL_CONSTRUCTOR_P (current)
15263 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15265 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)",
15266 lang_printable_name (TREE_TYPE (expr), 0));
15267 return error_mark_node;
15271 /* Throw is contained in a try statement and at least one catch
15272 clause can receive the thrown expression or the current method is
15273 declared to throw such an exception. Or, the throw statement is
15274 contained in a method or constructor declaration and the type of
15275 the Expression is assignable to at least one type listed in the
15276 throws clause the declaration. */
15278 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15279 if (!(unchecked_ok || tryblock_throws_ok))
15281 /* If there is a surrounding try block that has no matching
15282 clatch clause, report it first. A surrounding try block exits
15283 only if there is something after the list of checked
15284 exception thrown by the current function (if any). */
15285 if (IN_TRY_BLOCK_P ())
15286 parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
15287 lang_printable_name (type, 0));
15288 /* If we have no surrounding try statement and the method doesn't have
15289 any throws, report it now. FIXME */
15291 /* We report that the exception can't be throw from a try block
15292 in all circumstances but when the `throw' is inside a static
15294 else if (!EXCEPTIONS_P (currently_caught_type_list)
15295 && !tryblock_throws_ok)
15297 if (DECL_CLINIT_P (current_function_decl))
15298 parse_error_context (wfl_operator,
15299 "Checked exception `%s' can't be thrown in initializer",
15300 lang_printable_name (type, 0));
15302 parse_error_context (wfl_operator,
15303 "Checked exception `%s' isn't thrown from a `try' block",
15304 lang_printable_name (type, 0));
15306 /* Otherwise, the current method doesn't have the appropriate
15307 throws declaration */
15309 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
15310 lang_printable_name (type, 0));
15311 return error_mark_node;
15314 if (! flag_emit_class_files && ! flag_emit_xref)
15315 BUILD_THROW (node, expr);
15317 /* If doing xrefs, keep the location where the `throw' was seen. */
15318 if (flag_emit_xref)
15319 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15323 /* Check that exception said to be thrown by method DECL can be
15324 effectively caught from where DECL is invoked. */
15327 check_thrown_exceptions (location, decl)
15332 /* For all the unchecked exceptions thrown by DECL */
15333 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15334 throws = TREE_CHAIN (throws))
15335 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15338 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15339 if (DECL_NAME (decl) == get_identifier ("clone"))
15342 EXPR_WFL_LINECOL (wfl_operator) = location;
15343 if (DECL_FINIT_P (current_function_decl))
15344 parse_error_context
15345 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15346 lang_printable_name (TREE_VALUE (throws), 0));
15349 parse_error_context
15350 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
15351 lang_printable_name (TREE_VALUE (throws), 0),
15352 (DECL_INIT_P (current_function_decl) ?
15353 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15354 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15359 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15360 try-catch blocks, OR is listed in the `throws' clause of the
15364 check_thrown_exceptions_do (exception)
15367 tree list = currently_caught_type_list;
15368 resolve_and_layout (exception, NULL_TREE);
15369 /* First, all the nested try-catch-finally at that stage. The
15370 last element contains `throws' clause exceptions, if any. */
15371 if (IS_UNCHECKED_EXCEPTION_P (exception))
15376 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15377 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15379 list = TREE_CHAIN (list);
15385 purge_unchecked_exceptions (mdecl)
15388 tree throws = DECL_FUNCTION_THROWS (mdecl);
15389 tree new = NULL_TREE;
15393 tree next = TREE_CHAIN (throws);
15394 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15396 TREE_CHAIN (throws) = new;
15401 /* List is inverted here, but it doesn't matter */
15402 DECL_FUNCTION_THROWS (mdecl) = new;
15405 /* 15.24 Conditional Operator ?: */
15408 patch_conditional_expr (node, wfl_cond, wfl_op1)
15409 tree node, wfl_cond, wfl_op1;
15411 tree cond = TREE_OPERAND (node, 0);
15412 tree op1 = TREE_OPERAND (node, 1);
15413 tree op2 = TREE_OPERAND (node, 2);
15414 tree resulting_type = NULL_TREE;
15415 tree t1, t2, patched;
15416 int error_found = 0;
15418 /* Operands of ?: might be StringBuffers crafted as a result of a
15419 string concatenation. Obtain a descent operand here. */
15420 if ((patched = patch_string (op1)))
15421 TREE_OPERAND (node, 1) = op1 = patched;
15422 if ((patched = patch_string (op2)))
15423 TREE_OPERAND (node, 2) = op2 = patched;
15425 t1 = TREE_TYPE (op1);
15426 t2 = TREE_TYPE (op2);
15428 /* The first expression must be a boolean */
15429 if (TREE_TYPE (cond) != boolean_type_node)
15431 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15432 parse_error_context (wfl_operator,
15433 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
15434 lang_printable_name (TREE_TYPE (cond), 0));
15438 /* Second and third can be numeric, boolean (i.e. primitive),
15439 references or null. Anything else results in an error */
15440 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15441 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15442 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15443 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15446 /* Determine the type of the conditional expression. Same types are
15447 easy to deal with */
15449 resulting_type = t1;
15451 /* There are different rules for numeric types */
15452 else if (JNUMERIC_TYPE_P (t1))
15454 /* if byte/short found, the resulting type is short */
15455 if ((t1 == byte_type_node && t2 == short_type_node)
15456 || (t1 == short_type_node && t2 == byte_type_node))
15457 resulting_type = short_type_node;
15459 /* If t1 is a constant int and t2 is of type byte, short or char
15460 and t1's value fits in t2, then the resulting type is t2 */
15461 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15462 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15463 resulting_type = t2;
15465 /* If t2 is a constant int and t1 is of type byte, short or char
15466 and t2's value fits in t1, then the resulting type is t1 */
15467 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15468 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15469 resulting_type = t1;
15471 /* Otherwise, binary numeric promotion is applied and the
15472 resulting type is the promoted type of operand 1 and 2 */
15474 resulting_type = binary_numeric_promotion (t1, t2,
15475 &TREE_OPERAND (node, 1),
15476 &TREE_OPERAND (node, 2));
15479 /* Cases of a reference and a null type */
15480 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15481 resulting_type = t1;
15483 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15484 resulting_type = t2;
15486 /* Last case: different reference types. If a type can be converted
15487 into the other one by assignment conversion, the latter
15488 determines the type of the expression */
15489 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15490 resulting_type = promote_type (t1);
15492 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15493 resulting_type = promote_type (t2);
15495 /* If we don't have any resulting type, we're in trouble */
15496 if (!resulting_type)
15498 char *t = xstrdup (lang_printable_name (t1, 0));
15499 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15500 parse_error_context (wfl_operator,
15501 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15502 t, lang_printable_name (t2, 0));
15509 TREE_TYPE (node) = error_mark_node;
15510 return error_mark_node;
15513 TREE_TYPE (node) = resulting_type;
15514 TREE_SET_CODE (node, COND_EXPR);
15515 CAN_COMPLETE_NORMALLY (node) = 1;
15519 /* Wrap EXPR with code to initialize DECL's class, if appropriate. */
15522 maybe_build_class_init_for_field (decl, expr)
15525 tree clas = DECL_CONTEXT (decl);
15526 if (flag_emit_class_files || flag_emit_xref)
15529 if (TREE_CODE (decl) == VAR_DECL && FIELD_STATIC (decl)
15530 && FIELD_FINAL (decl))
15532 tree init = DECL_INITIAL (decl);
15533 if (init != NULL_TREE)
15534 init = fold_constant_for_init (init, decl);
15535 if (init != NULL_TREE && CONSTANT_VALUE_P (init))
15539 return build_class_init (clas, expr);
15542 /* Try to constant fold NODE.
15543 If NODE is not a constant expression, return NULL_EXPR.
15544 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15547 fold_constant_for_init (node, context)
15551 tree op0, op1, val;
15552 enum tree_code code = TREE_CODE (node);
15564 case TRUNC_MOD_EXPR:
15572 case TRUTH_ANDIF_EXPR:
15573 case TRUTH_ORIF_EXPR:
15580 op0 = TREE_OPERAND (node, 0);
15581 op1 = TREE_OPERAND (node, 1);
15582 val = fold_constant_for_init (op0, context);
15583 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15585 TREE_OPERAND (node, 0) = val;
15586 val = fold_constant_for_init (op1, context);
15587 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15589 TREE_OPERAND (node, 1) = val;
15590 return patch_binop (node, op0, op1);
15592 case UNARY_PLUS_EXPR:
15594 case TRUTH_NOT_EXPR:
15597 op0 = TREE_OPERAND (node, 0);
15598 val = fold_constant_for_init (op0, context);
15599 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15601 TREE_OPERAND (node, 0) = val;
15602 return patch_unaryop (node, op0);
15606 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15607 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15609 TREE_OPERAND (node, 0) = val;
15610 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15611 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15613 TREE_OPERAND (node, 1) = val;
15614 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15615 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15617 TREE_OPERAND (node, 2) = val;
15618 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15619 : TREE_OPERAND (node, 2);
15623 if (! FIELD_FINAL (node)
15624 || DECL_INITIAL (node) == NULL_TREE)
15626 val = DECL_INITIAL (node);
15627 /* Guard against infinite recursion. */
15628 DECL_INITIAL (node) = NULL_TREE;
15629 val = fold_constant_for_init (val, node);
15630 DECL_INITIAL (node) = val;
15631 if (!val && CLASS_FINAL_VARIABLE_P (node))
15632 DECL_FIELD_FINAL_IUD (node) = 0;
15635 case EXPR_WITH_FILE_LOCATION:
15636 /* Compare java_complete_tree and resolve_expression_name. */
15637 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15638 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15640 tree name = EXPR_WFL_NODE (node);
15642 if (PRIMARY_P (node))
15644 else if (! QUALIFIED_P (name))
15646 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
15647 if (decl == NULL_TREE
15648 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
15650 return fold_constant_for_init (decl, decl);
15654 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15655 qualify_ambiguous_name (node);
15656 if (resolve_field_access (node, &decl, NULL)
15657 && decl != NULL_TREE)
15658 return fold_constant_for_init (decl, decl);
15664 op0 = TREE_OPERAND (node, 0);
15665 val = fold_constant_for_init (op0, context);
15666 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15668 TREE_OPERAND (node, 0) = val;
15672 #ifdef USE_COMPONENT_REF
15674 case COMPONENT_REF:
15683 #ifdef USE_COMPONENT_REF
15684 /* Context is 'T' for TypeName, 'P' for PackageName,
15685 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15688 resolve_simple_name (name, context)
15695 resolve_qualified_name (name, context)
15702 /* Mark P, which is really a `struct parser_ctxt **' for GC. */
15705 mark_parser_ctxt (p)
15708 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15715 for (i = 0; i < 11; ++i)
15716 ggc_mark_tree (pc->modifier_ctx[i]);
15717 ggc_mark_tree (pc->class_type);
15718 ggc_mark_tree (pc->function_decl);
15719 ggc_mark_tree (pc->package);
15720 ggc_mark_tree (pc->class_list);
15721 ggc_mark_tree (pc->current_parsed_class);
15722 ggc_mark_tree (pc->current_parsed_class_un);
15723 ggc_mark_tree (pc->non_static_initialized);
15724 ggc_mark_tree (pc->static_initialized);
15725 ggc_mark_tree (pc->instance_initializers);
15726 ggc_mark_tree (pc->import_list);
15727 ggc_mark_tree (pc->import_demand_list);
15728 ggc_mark_tree (pc->current_loop);
15729 ggc_mark_tree (pc->current_labeled_block);
15730 #endif /* JC1_LITE */
15733 mark_parser_ctxt (&pc->next);
15739 /* Register roots with the garbage collector. */
15740 ggc_add_tree_root (src_parse_roots, sizeof (src_parse_roots) / sizeof(tree));