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 void expand_start_java_method PARAMS ((tree));
120 static tree find_name_in_single_imports PARAMS ((tree));
121 static void check_abstract_method_header PARAMS ((tree));
122 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
123 static tree resolve_expression_name PARAMS ((tree, tree *));
124 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
125 static int check_class_interface_creation PARAMS ((int, int, tree,
127 static tree patch_method_invocation PARAMS ((tree, tree, tree, int,
129 static int breakdown_qualified PARAMS ((tree *, 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 enter_a_block PARAMS ((tree));
156 static tree exit_block PARAMS ((void));
157 static tree lookup_name_in_blocks PARAMS ((tree));
158 static void maybe_absorb_scoping_blocks PARAMS ((void));
159 static tree build_method_invocation PARAMS ((tree, tree));
160 static tree build_new_invocation PARAMS ((tree, tree));
161 static tree build_assignment PARAMS ((int, int, tree, tree));
162 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
163 static int check_final_assignment PARAMS ((tree ,tree));
164 static tree patch_assignment PARAMS ((tree, tree, tree ));
165 static tree patch_binop PARAMS ((tree, tree, tree));
166 static tree build_unaryop PARAMS ((int, int, tree));
167 static tree build_incdec PARAMS ((int, int, tree, int));
168 static tree patch_unaryop PARAMS ((tree, tree));
169 static tree build_cast PARAMS ((int, tree, tree));
170 static tree build_null_of_type PARAMS ((tree));
171 static tree patch_cast PARAMS ((tree, tree));
172 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
173 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
174 static int valid_cast_to_p PARAMS ((tree, tree));
175 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
176 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
177 static tree try_reference_assignconv PARAMS ((tree, tree));
178 static tree build_unresolved_array_type PARAMS ((tree));
179 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
180 static tree build_array_ref PARAMS ((int, tree, tree));
181 static tree patch_array_ref PARAMS ((tree));
182 static tree make_qualified_name PARAMS ((tree, tree, int));
183 static tree merge_qualified_name PARAMS ((tree, tree));
184 static tree make_qualified_primary PARAMS ((tree, tree, int));
185 static int resolve_qualified_expression_name PARAMS ((tree, tree *,
187 static void qualify_ambiguous_name PARAMS ((tree));
188 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
189 static tree build_newarray_node PARAMS ((tree, tree, int));
190 static tree patch_newarray PARAMS ((tree));
191 static tree resolve_type_during_patch PARAMS ((tree));
192 static tree build_this PARAMS ((int));
193 static tree build_wfl_wrap PARAMS ((tree, int));
194 static tree build_return PARAMS ((int, tree));
195 static tree patch_return PARAMS ((tree));
196 static tree maybe_access_field PARAMS ((tree, tree, tree));
197 static int complete_function_arguments PARAMS ((tree));
198 static int check_for_static_method_reference PARAMS ((tree, tree, tree,
200 static int not_accessible_p PARAMS ((tree, tree, tree, int));
201 static void check_deprecation PARAMS ((tree, tree));
202 static int class_in_current_package PARAMS ((tree));
203 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
204 static tree patch_if_else_statement PARAMS ((tree));
205 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
206 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
207 static tree patch_exit_expr PARAMS ((tree));
208 static tree build_labeled_block PARAMS ((int, tree));
209 static tree finish_labeled_statement PARAMS ((tree, tree));
210 static tree build_bc_statement PARAMS ((int, int, tree));
211 static tree patch_bc_statement PARAMS ((tree));
212 static tree patch_loop_statement PARAMS ((tree));
213 static tree build_new_loop PARAMS ((tree));
214 static tree build_loop_body PARAMS ((int, tree, int));
215 static tree finish_loop_body PARAMS ((int, tree, tree, int));
216 static tree build_debugable_stmt PARAMS ((int, tree));
217 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
218 static tree patch_switch_statement PARAMS ((tree));
219 static tree string_constant_concatenation PARAMS ((tree, tree));
220 static tree build_string_concatenation PARAMS ((tree, tree));
221 static tree patch_string_cst PARAMS ((tree));
222 static tree patch_string PARAMS ((tree));
223 static tree build_try_statement PARAMS ((int, tree, tree));
224 static tree build_try_finally_statement PARAMS ((int, tree, tree));
225 static tree patch_try_statement PARAMS ((tree));
226 static tree patch_synchronized_statement PARAMS ((tree, tree));
227 static tree patch_throw_statement PARAMS ((tree, tree));
228 static void check_thrown_exceptions PARAMS ((int, tree));
229 static int check_thrown_exceptions_do PARAMS ((tree));
230 static void purge_unchecked_exceptions PARAMS ((tree));
231 static void check_throws_clauses PARAMS ((tree, tree, tree));
232 static void finish_method_declaration PARAMS ((tree));
233 static tree build_super_invocation PARAMS ((tree));
234 static int verify_constructor_circularity PARAMS ((tree, tree));
235 static char *constructor_circularity_msg PARAMS ((tree, tree));
236 static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
238 static const char *get_printable_method_name PARAMS ((tree));
239 static tree patch_conditional_expr PARAMS ((tree, tree, tree));
240 static tree generate_finit PARAMS ((tree));
241 static void add_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 *));
334 /* Number of error found so far. */
335 int java_error_count;
336 /* Number of warning found so far. */
337 int java_warning_count;
338 /* Tell when not to fold, when doing xrefs */
340 /* Cyclic inheritance report, as it can be set by layout_class */
341 const char *cyclic_inheritance_report;
343 /* Tell when we're within an instance initializer */
344 static int in_instance_initializer;
346 /* The current parser context */
347 struct parser_ctxt *ctxp;
349 /* List of things that were analyzed for which code will be generated */
350 static struct parser_ctxt *ctxp_for_generation = NULL;
352 /* binop_lookup maps token to tree_code. It is used where binary
353 operations are involved and required by the parser. RDIV_EXPR
354 covers both integral/floating point division. The code is changed
355 once the type of both operator is worked out. */
357 static enum tree_code binop_lookup[19] =
359 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
360 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
361 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
362 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
363 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
365 #define BINOP_LOOKUP(VALUE) \
366 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
368 /* This is the end index for binary operators that can also be used
369 in compound assignements. */
370 #define BINOP_COMPOUND_CANDIDATES 11
372 /* The "$L" identifier we use to create labels. */
373 static tree label_id = NULL_TREE;
375 /* The "StringBuffer" identifier used for the String `+' operator. */
376 static tree wfl_string_buffer = NULL_TREE;
378 /* The "append" identifier used for String `+' operator. */
379 static tree wfl_append = NULL_TREE;
381 /* The "toString" identifier used for String `+' operator. */
382 static tree wfl_to_string = NULL_TREE;
384 /* The "java.lang" import qualified name. */
385 static tree java_lang_id = NULL_TREE;
387 /* The generated `inst$' identifier used for generated enclosing
388 instance/field access functions. */
389 static tree inst_id = NULL_TREE;
391 /* The "java.lang.Cloneable" qualified name. */
392 static tree java_lang_cloneable = NULL_TREE;
394 /* The "java.io.Serializable" qualified name. */
395 static tree java_io_serializable = NULL_TREE;
397 /* Context and flag for static blocks */
398 static tree current_static_block = NULL_TREE;
400 /* The generated `write_parm_value$' identifier. */
403 /* The list of all packages we've seen so far */
404 static tree package_list = NULL_TREE;
406 /* Hold THIS for the scope of the current public method decl. */
407 static tree current_this;
409 /* Hold a list of catch clauses list. The first element of this list is
410 the list of the catch clauses of the currently analysed try block. */
411 static tree currently_caught_type_list;
413 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
414 line and point it out. */
415 /* Should point out the one that don't fit. ASCII/unicode, going
418 #define check_modifiers(__message, __value, __mask) do { \
419 if ((__value) & ~(__mask)) \
421 int i, remainder = (__value) & ~(__mask); \
422 for (i = 0; i <= 10; i++) \
423 if ((1 << i) & remainder) \
424 parse_error_context (ctxp->modifier_ctx [i], (__message), \
425 java_accstring_lookup (1 << i)); \
447 /* Things defined here have to match the order of what's in the
448 binop_lookup table. */
450 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
451 %token LS_TK SRS_TK ZRS_TK
452 %token AND_TK XOR_TK OR_TK
453 %token BOOL_AND_TK BOOL_OR_TK
454 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
456 /* This maps to the same binop_lookup entry than the token above */
458 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
460 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
461 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
464 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
466 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
467 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
468 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
469 %token PAD_TK ABSTRACT_TK MODIFIER_TK
472 /* Keep those two in order, too */
473 %token DECR_TK INCR_TK
475 /* From now one, things can be in any order */
477 %token DEFAULT_TK IF_TK THROW_TK
478 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
479 %token THROWS_TK BREAK_TK IMPORT_TK
480 %token ELSE_TK INSTANCEOF_TK RETURN_TK
481 %token VOID_TK CATCH_TK INTERFACE_TK
482 %token CASE_TK EXTENDS_TK FINALLY_TK
483 %token SUPER_TK WHILE_TK CLASS_TK
484 %token SWITCH_TK CONST_TK TRY_TK
485 %token FOR_TK NEW_TK CONTINUE_TK
486 %token GOTO_TK PACKAGE_TK THIS_TK
488 %token BYTE_TK SHORT_TK INT_TK LONG_TK
489 %token CHAR_TK INTEGRAL_TK
491 %token FLOAT_TK DOUBLE_TK FP_TK
495 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
497 %token ASSIGN_ANY_TK ASSIGN_TK
498 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
500 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
501 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
503 %type <value> modifiers MODIFIER_TK final synchronized
505 %type <node> super ID_TK identifier
506 %type <node> name simple_name qualified_name
507 %type <node> type_declaration compilation_unit
508 field_declaration method_declaration extends_interfaces
509 interfaces interface_type_list
510 class_member_declaration
511 import_declarations package_declaration
512 type_declarations interface_body
513 interface_member_declaration constant_declaration
514 interface_member_declarations interface_type
515 abstract_method_declaration interface_type_list
516 %type <node> class_body_declaration class_member_declaration
517 static_initializer constructor_declaration block
518 %type <node> class_body_declarations constructor_header
519 %type <node> class_or_interface_type class_type class_type_list
520 constructor_declarator explicit_constructor_invocation
521 %type <node> dim_expr dim_exprs this_or_super throws
523 %type <node> variable_declarator_id variable_declarator
524 variable_declarators variable_initializer
525 variable_initializers constructor_body
528 %type <node> class_body block_end constructor_block_end
529 %type <node> statement statement_without_trailing_substatement
530 labeled_statement if_then_statement label_decl
531 if_then_else_statement while_statement for_statement
532 statement_nsi labeled_statement_nsi do_statement
533 if_then_else_statement_nsi while_statement_nsi
534 for_statement_nsi statement_expression_list for_init
535 for_update statement_expression expression_statement
536 primary_no_new_array expression primary
537 array_creation_expression array_type
538 class_instance_creation_expression field_access
539 method_invocation array_access something_dot_new
540 argument_list postfix_expression while_expression
541 post_increment_expression post_decrement_expression
542 unary_expression_not_plus_minus unary_expression
543 pre_increment_expression pre_decrement_expression
544 unary_expression_not_plus_minus cast_expression
545 multiplicative_expression additive_expression
546 shift_expression relational_expression
547 equality_expression and_expression
548 exclusive_or_expression inclusive_or_expression
549 conditional_and_expression conditional_or_expression
550 conditional_expression assignment_expression
551 left_hand_side assignment for_header for_begin
552 constant_expression do_statement_begin empty_statement
553 switch_statement synchronized_statement throw_statement
554 try_statement switch_expression switch_block
555 catches catch_clause catch_clause_parameter finally
556 anonymous_class_creation
557 %type <node> return_statement break_statement continue_statement
559 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
560 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
561 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
562 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
563 %type <operator> ASSIGN_ANY_TK assignment_operator
564 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
565 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
566 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
567 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
568 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
569 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
570 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
571 %type <operator> NEW_TK
573 %type <node> method_body
575 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
576 STRING_LIT_TK NULL_TK VOID_TK
578 %type <node> IF_TK WHILE_TK FOR_TK
580 %type <node> formal_parameter_list formal_parameter
581 method_declarator method_header
583 %type <node> primitive_type reference_type type
584 BOOLEAN_TK INTEGRAL_TK FP_TK
586 /* Added or modified JDK 1.1 rule types */
587 %type <node> type_literals
590 /* 19.2 Production from 2.3: The Syntactic Grammar */
593 /* Register static variables with the garbage
595 ggc_add_tree_root (&label_id, 1);
596 ggc_add_tree_root (&wfl_string_buffer, 1);
597 ggc_add_tree_root (&wfl_append, 1);
598 ggc_add_tree_root (&wfl_to_string, 1);
599 ggc_add_tree_root (&java_lang_id, 1);
600 ggc_add_tree_root (&inst_id, 1);
601 ggc_add_tree_root (&java_lang_cloneable, 1);
602 ggc_add_tree_root (&java_io_serializable, 1);
603 ggc_add_tree_root (¤t_static_block, 1);
604 ggc_add_tree_root (&wpv_id, 1);
605 ggc_add_tree_root (&package_list, 1);
606 ggc_add_tree_root (¤t_this, 1);
607 ggc_add_tree_root (¤tly_caught_type_list, 1);
608 ggc_add_root (&ctxp, 1,
609 sizeof (struct parser_ctxt *),
611 ggc_add_root (&ctxp_for_generation, 1,
612 sizeof (struct parser_ctxt *),
619 /* 19.3 Productions from 3: Lexical structure */
629 /* 19.4 Productions from 4: Types, Values and Variables */
642 class_or_interface_type
646 class_or_interface_type:
651 class_or_interface_type /* Default rule */
655 class_or_interface_type
661 int osb = pop_current_osb (ctxp);
662 tree t = build_java_array_type (($1), -1);
663 CLASS_LOADED_P (t) = 1;
665 t = build_unresolved_array_type (t);
670 int osb = pop_current_osb (ctxp);
673 t = build_unresolved_array_type (t);
678 /* 19.5 Productions from 6: Names */
680 simple_name /* Default rule */
681 | qualified_name /* Default rule */
685 identifier /* Default rule */
689 name DOT_TK identifier
690 { $$ = make_qualified_name ($1, $3, $2.location); }
697 /* 19.6: Production from 7: Packages */
700 | package_declaration
701 | import_declarations
703 | package_declaration import_declarations
704 | package_declaration type_declarations
705 | import_declarations type_declarations
706 | package_declaration import_declarations type_declarations
714 | import_declarations import_declaration
722 | type_declarations type_declaration
726 PACKAGE_TK name SC_TK
728 ctxp->package = EXPR_WFL_NODE ($2);
729 register_package (ctxp->package);
732 {yyerror ("Missing name"); RECOVER;}
733 | PACKAGE_TK name error
734 {yyerror ("';' expected"); RECOVER;}
738 single_type_import_declaration
739 | type_import_on_demand_declaration
742 single_type_import_declaration:
745 tree name = EXPR_WFL_NODE ($2), last_name;
746 int i = IDENTIFIER_LENGTH (name)-1;
747 const char *last = &IDENTIFIER_POINTER (name)[i];
748 while (last != IDENTIFIER_POINTER (name))
754 last_name = get_identifier (++last);
755 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
757 tree err = find_name_in_single_imports (last_name);
758 if (err && err != name)
760 ($2, "Ambiguous class: `%s' and `%s'",
761 IDENTIFIER_POINTER (name),
762 IDENTIFIER_POINTER (err));
764 REGISTER_IMPORT ($2, last_name);
767 REGISTER_IMPORT ($2, last_name);
770 {yyerror ("Missing name"); RECOVER;}
771 | IMPORT_TK name error
772 {yyerror ("';' expected"); RECOVER;}
775 type_import_on_demand_declaration:
776 IMPORT_TK name DOT_TK MULT_TK SC_TK
778 tree name = EXPR_WFL_NODE ($2);
779 /* Don't import java.lang.* twice. */
780 if (name != java_lang_id)
782 read_import_dir ($2);
783 ctxp->import_demand_list =
784 chainon (ctxp->import_demand_list,
785 build_tree_list ($2, NULL_TREE));
788 | IMPORT_TK name DOT_TK error
789 {yyerror ("'*' expected"); RECOVER;}
790 | IMPORT_TK name DOT_TK MULT_TK error
791 {yyerror ("';' expected"); RECOVER;}
796 { end_class_declaration (0); }
797 | interface_declaration
798 { end_class_declaration (0); }
803 yyerror ("Class or interface declaration expected");
807 /* 19.7 Shortened from the original:
808 modifiers: modifier | modifiers modifier
809 modifier: any of public... */
815 | modifiers MODIFIER_TK
820 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
821 java_accstring_lookup (acc));
829 /* 19.8.1 Production from $8.1: Class Declaration */
831 modifiers CLASS_TK identifier super interfaces
832 { create_class ($1, $3, $4, $5); }
834 | CLASS_TK identifier super interfaces
835 { create_class (0, $2, $3, $4); }
837 | modifiers CLASS_TK error
838 {yyerror ("Missing class name"); RECOVER;}
840 {yyerror ("Missing class name"); RECOVER;}
841 | CLASS_TK identifier error
843 if (!ctxp->class_err) yyerror ("'{' expected");
846 | modifiers CLASS_TK identifier error
847 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
852 | EXTENDS_TK class_type
854 | EXTENDS_TK class_type error
855 {yyerror ("'{' expected"); ctxp->class_err=1;}
857 {yyerror ("Missing super class name"); ctxp->class_err=1;}
862 | IMPLEMENTS_TK interface_type_list
864 | IMPLEMENTS_TK error
867 yyerror ("Missing interface name");
874 ctxp->interface_number = 1;
875 $$ = build_tree_list ($1, NULL_TREE);
877 | interface_type_list C_TK interface_type
879 ctxp->interface_number++;
880 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
882 | interface_type_list C_TK error
883 {yyerror ("Missing interface name"); RECOVER;}
889 /* Store the location of the `}' when doing xrefs */
891 DECL_END_SOURCE_LINE (GET_CPC ()) =
892 EXPR_WFL_ADD_COL ($2.location, 1);
895 | OCB_TK class_body_declarations CCB_TK
897 /* Store the location of the `}' when doing xrefs */
899 DECL_END_SOURCE_LINE (GET_CPC ()) =
900 EXPR_WFL_ADD_COL ($3.location, 1);
905 class_body_declarations:
906 class_body_declaration
907 | class_body_declarations class_body_declaration
910 class_body_declaration:
911 class_member_declaration
913 | constructor_declaration
914 | block /* Added, JDK1.1, instance initializer */
916 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
917 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
921 class_member_declaration:
924 | class_declaration /* Added, JDK1.1 inner classes */
925 { end_class_declaration (1); }
926 | interface_declaration /* Added, JDK1.1 inner interfaces */
927 { end_class_declaration (1); }
931 /* 19.8.2 Productions from 8.3: Field Declarations */
933 type variable_declarators SC_TK
934 { register_fields (0, $1, $2); }
935 | modifiers type variable_declarators SC_TK
938 ("Illegal modifier `%s' for field declaration",
939 $1, FIELD_MODIFIERS);
940 check_modifiers_consistency ($1);
941 register_fields ($1, $2, $3);
945 variable_declarators:
946 /* Should we use build_decl_list () instead ? FIXME */
947 variable_declarator /* Default rule */
948 | variable_declarators C_TK variable_declarator
949 { $$ = chainon ($1, $3); }
950 | variable_declarators C_TK error
951 {yyerror ("Missing term"); RECOVER;}
955 variable_declarator_id
956 { $$ = build_tree_list ($1, NULL_TREE); }
957 | variable_declarator_id ASSIGN_TK variable_initializer
959 if (java_error_count)
962 ($1, build_assignment ($2.token, $2.location, $1, $3));
964 | variable_declarator_id ASSIGN_TK error
966 yyerror ("Missing variable initializer");
967 $$ = build_tree_list ($1, NULL_TREE);
970 | variable_declarator_id ASSIGN_TK variable_initializer error
972 yyerror ("';' expected");
973 $$ = build_tree_list ($1, NULL_TREE);
978 variable_declarator_id:
980 | variable_declarator_id OSB_TK CSB_TK
981 { $$ = build_unresolved_array_type ($1); }
983 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
984 | variable_declarator_id OSB_TK error
986 tree node = java_lval.node;
987 if (node && (TREE_CODE (node) == INTEGER_CST
988 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
989 yyerror ("Can't specify array dimension in a declaration");
991 yyerror ("']' expected");
994 | variable_declarator_id CSB_TK error
995 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
998 variable_initializer:
1003 /* 19.8.3 Productions from 8.4: Method Declarations */
1007 current_function_decl = $1;
1008 if (current_function_decl
1009 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1010 source_start_java_method (current_function_decl);
1012 current_function_decl = NULL_TREE;
1015 { finish_method_declaration ($3); }
1016 | method_header error
1017 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1021 type method_declarator throws
1022 { $$ = method_header (0, $1, $2, $3); }
1023 | VOID_TK method_declarator throws
1024 { $$ = method_header (0, void_type_node, $2, $3); }
1025 | modifiers type method_declarator throws
1026 { $$ = method_header ($1, $2, $3, $4); }
1027 | modifiers VOID_TK method_declarator throws
1028 { $$ = method_header ($1, void_type_node, $3, $4); }
1031 yyerror ("Invalid method declaration, method name required");
1034 | modifiers type error
1037 {yyerror ("Identifier expected"); RECOVER;}
1038 | modifiers VOID_TK error
1039 {yyerror ("Identifier expected"); RECOVER;}
1042 yyerror ("Invalid method declaration, return type required");
1048 identifier OP_TK CP_TK
1050 ctxp->formal_parameter_number = 0;
1051 $$ = method_declarator ($1, NULL_TREE);
1053 | identifier OP_TK formal_parameter_list CP_TK
1054 { $$ = method_declarator ($1, $3); }
1055 | method_declarator OSB_TK CSB_TK
1057 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1059 build_unresolved_array_type (TREE_PURPOSE ($1));
1060 parse_warning_context
1062 "Discouraged form of returned type specification");
1064 | identifier OP_TK error
1065 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1066 | method_declarator OSB_TK error
1067 {yyerror ("']' expected"); RECOVER;}
1070 formal_parameter_list:
1073 ctxp->formal_parameter_number = 1;
1075 | formal_parameter_list C_TK formal_parameter
1077 ctxp->formal_parameter_number += 1;
1078 $$ = chainon ($1, $3);
1080 | formal_parameter_list C_TK error
1081 { yyerror ("Missing formal parameter term"); RECOVER; }
1085 type variable_declarator_id
1087 $$ = build_tree_list ($2, $1);
1089 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1091 $$ = build_tree_list ($3, $2);
1092 ARG_FINAL_P ($$) = 1;
1096 yyerror ("Missing identifier"); RECOVER;
1101 yyerror ("Missing identifier"); RECOVER;
1109 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1111 if ($1 != ACC_FINAL)
1112 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1118 | THROWS_TK class_type_list
1121 {yyerror ("Missing class type term"); RECOVER;}
1126 { $$ = build_tree_list ($1, $1); }
1127 | class_type_list C_TK class_type
1128 { $$ = tree_cons ($3, $3, $1); }
1129 | class_type_list C_TK error
1130 {yyerror ("Missing class type term"); RECOVER;}
1135 | SC_TK { $$ = NULL_TREE; }
1138 /* 19.8.4 Productions from 8.5: Static Initializers */
1142 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1143 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1144 current_static_block = NULL_TREE;
1148 static: /* Test lval.sub_token here */
1151 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1152 /* Can't have a static initializer in an innerclass */
1153 if ($1 | ACC_STATIC &&
1154 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1156 (MODIFIER_WFL (STATIC_TK),
1157 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1158 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1159 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1163 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1164 constructor_declaration:
1167 current_function_decl = $1;
1168 source_start_java_method (current_function_decl);
1171 { finish_method_declaration ($3); }
1175 constructor_declarator throws
1176 { $$ = method_header (0, NULL_TREE, $1, $2); }
1177 | modifiers constructor_declarator throws
1178 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1181 constructor_declarator:
1182 simple_name OP_TK CP_TK
1184 ctxp->formal_parameter_number = 0;
1185 $$ = method_declarator ($1, NULL_TREE);
1187 | simple_name OP_TK formal_parameter_list CP_TK
1188 { $$ = method_declarator ($1, $3); }
1192 /* Unlike regular method, we always need a complete (empty)
1193 body so we can safely perform all the required code
1194 addition (super invocation and field initialization) */
1195 block_begin constructor_block_end
1197 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1200 | block_begin explicit_constructor_invocation constructor_block_end
1202 | block_begin block_statements constructor_block_end
1204 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1208 constructor_block_end:
1212 /* Error recovery for that rule moved down expression_statement: rule. */
1213 explicit_constructor_invocation:
1214 this_or_super OP_TK CP_TK SC_TK
1216 $$ = build_method_invocation ($1, NULL_TREE);
1217 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1218 $$ = java_method_add_stmt (current_function_decl, $$);
1220 | this_or_super OP_TK argument_list CP_TK SC_TK
1222 $$ = build_method_invocation ($1, $3);
1223 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1224 $$ = java_method_add_stmt (current_function_decl, $$);
1226 /* Added, JDK1.1 inner classes. Modified because the rule
1227 'primary' couldn't work. */
1228 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1229 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1230 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1231 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1234 this_or_super: /* Added, simplifies error diagnostics */
1237 tree wfl = build_wfl_node (this_identifier_node);
1238 EXPR_WFL_LINECOL (wfl) = $1.location;
1243 tree wfl = build_wfl_node (super_identifier_node);
1244 EXPR_WFL_LINECOL (wfl) = $1.location;
1249 /* 19.9 Productions from 9: Interfaces */
1250 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1251 interface_declaration:
1252 INTERFACE_TK identifier
1253 { create_interface (0, $2, NULL_TREE); }
1255 | modifiers INTERFACE_TK identifier
1256 { create_interface ($1, $3, NULL_TREE); }
1258 | INTERFACE_TK identifier extends_interfaces
1259 { create_interface (0, $2, $3); }
1261 | modifiers INTERFACE_TK identifier extends_interfaces
1262 { create_interface ($1, $3, $4); }
1264 | INTERFACE_TK identifier error
1265 {yyerror ("'{' expected"); RECOVER;}
1266 | modifiers INTERFACE_TK identifier error
1267 {yyerror ("'{' expected"); RECOVER;}
1271 EXTENDS_TK interface_type
1273 ctxp->interface_number = 1;
1274 $$ = build_tree_list ($2, NULL_TREE);
1276 | extends_interfaces C_TK interface_type
1278 ctxp->interface_number++;
1279 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1282 {yyerror ("Invalid interface type"); RECOVER;}
1283 | extends_interfaces C_TK error
1284 {yyerror ("Missing term"); RECOVER;}
1290 | OCB_TK interface_member_declarations CCB_TK
1294 interface_member_declarations:
1295 interface_member_declaration
1296 | interface_member_declarations interface_member_declaration
1299 interface_member_declaration:
1300 constant_declaration
1301 | abstract_method_declaration
1302 | class_declaration /* Added, JDK1.1 inner classes */
1303 { end_class_declaration (1); }
1304 | interface_declaration /* Added, JDK1.1 inner interfaces */
1305 { end_class_declaration (1); }
1308 constant_declaration:
1312 abstract_method_declaration:
1315 check_abstract_method_header ($1);
1316 current_function_decl = NULL_TREE; /* FIXME ? */
1318 | method_header error
1319 {yyerror ("';' expected"); RECOVER;}
1322 /* 19.10 Productions from 10: Arrays */
1325 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1326 | OCB_TK variable_initializers CCB_TK
1327 { $$ = build_new_array_init ($1.location, $2); }
1328 | OCB_TK variable_initializers C_TK CCB_TK
1329 { $$ = build_new_array_init ($1.location, $2); }
1332 variable_initializers:
1333 variable_initializer
1335 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1338 | variable_initializers C_TK variable_initializer
1340 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1342 | variable_initializers C_TK error
1343 {yyerror ("Missing term"); RECOVER;}
1346 /* 19.11 Production from 14: Blocks and Statements */
1350 /* Store the location of the `}' when doing xrefs */
1351 if (current_function_decl && flag_emit_xref)
1352 DECL_END_SOURCE_LINE (current_function_decl) =
1353 EXPR_WFL_ADD_COL ($2.location, 1);
1354 $$ = empty_stmt_node;
1356 | block_begin block_statements block_end
1368 maybe_absorb_scoping_blocks ();
1369 /* Store the location of the `}' when doing xrefs */
1370 if (current_function_decl && flag_emit_xref)
1371 DECL_END_SOURCE_LINE (current_function_decl) =
1372 EXPR_WFL_ADD_COL ($1.location, 1);
1374 if (!BLOCK_SUBBLOCKS ($$))
1375 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1381 | block_statements block_statement
1385 local_variable_declaration_statement
1387 { java_method_add_stmt (current_function_decl, $1); }
1388 | class_declaration /* Added, JDK1.1 local classes */
1390 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1391 end_class_declaration (1);
1395 local_variable_declaration_statement:
1396 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1399 local_variable_declaration:
1400 type variable_declarators
1401 { declare_local_variables (0, $1, $2); }
1402 | final type variable_declarators /* Added, JDK1.1 final locals */
1403 { declare_local_variables ($1, $2, $3); }
1407 statement_without_trailing_substatement
1410 | if_then_else_statement
1413 { $$ = exit_block (); }
1417 statement_without_trailing_substatement
1418 | labeled_statement_nsi
1419 | if_then_else_statement_nsi
1420 | while_statement_nsi
1422 { $$ = exit_block (); }
1425 statement_without_trailing_substatement:
1428 | expression_statement
1432 | continue_statement
1434 | synchronized_statement
1442 if (flag_extraneous_semicolon)
1444 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1445 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1447 $$ = empty_stmt_node;
1452 identifier REL_CL_TK
1454 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1455 EXPR_WFL_NODE ($1));
1457 push_labeled_block ($$);
1458 PUSH_LABELED_BLOCK ($$);
1463 label_decl statement
1464 { $$ = finish_labeled_statement ($1, $2); }
1466 {yyerror ("':' expected"); RECOVER;}
1469 labeled_statement_nsi:
1470 label_decl statement_nsi
1471 { $$ = finish_labeled_statement ($1, $2); }
1474 /* We concentrate here a bunch of error handling rules that we couldn't write
1475 earlier, because expression_statement catches a missing ';'. */
1476 expression_statement:
1477 statement_expression SC_TK
1479 /* We have a statement. Generate a WFL around it so
1481 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1482 /* We know we have a statement, so set the debug
1483 info to be eventually generate here. */
1484 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1488 YYNOT_TWICE yyerror ("Invalid expression statement");
1489 DRECOVER (expr_stmt);
1493 YYNOT_TWICE yyerror ("Invalid expression statement");
1494 DRECOVER (expr_stmt);
1498 YYNOT_TWICE yyerror ("Invalid expression statement");
1499 DRECOVER (expr_stmt);
1501 | this_or_super OP_TK error
1502 {yyerror ("')' expected"); RECOVER;}
1503 | this_or_super OP_TK CP_TK error
1505 parse_ctor_invocation_error ();
1508 | this_or_super OP_TK argument_list error
1509 {yyerror ("')' expected"); RECOVER;}
1510 | this_or_super OP_TK argument_list CP_TK error
1512 parse_ctor_invocation_error ();
1515 | name DOT_TK SUPER_TK error
1516 {yyerror ("'(' expected"); RECOVER;}
1517 | name DOT_TK SUPER_TK OP_TK error
1518 {yyerror ("')' expected"); RECOVER;}
1519 | name DOT_TK SUPER_TK OP_TK argument_list error
1520 {yyerror ("')' expected"); RECOVER;}
1521 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1522 {yyerror ("';' expected"); RECOVER;}
1523 | name DOT_TK SUPER_TK OP_TK CP_TK error
1524 {yyerror ("';' expected"); RECOVER;}
1527 statement_expression:
1529 | pre_increment_expression
1530 | pre_decrement_expression
1531 | post_increment_expression
1532 | post_decrement_expression
1534 | class_instance_creation_expression
1538 IF_TK OP_TK expression CP_TK statement
1540 $$ = build_if_else_statement ($2.location, $3,
1544 {yyerror ("'(' expected"); RECOVER;}
1546 {yyerror ("Missing term"); RECOVER;}
1547 | IF_TK OP_TK expression error
1548 {yyerror ("')' expected"); RECOVER;}
1551 if_then_else_statement:
1552 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1553 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1556 if_then_else_statement_nsi:
1557 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1558 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1568 /* Make into "proper list" of COMPOUND_EXPRs.
1569 I.e. make the last statment also have its own
1571 maybe_absorb_scoping_blocks ();
1572 TREE_OPERAND ($1, 1) = exit_block ();
1573 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1578 SWITCH_TK OP_TK expression CP_TK
1580 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1581 EXPR_WFL_LINECOL ($$) = $2.location;
1584 {yyerror ("'(' expected"); RECOVER;}
1585 | SWITCH_TK OP_TK error
1586 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1587 | SWITCH_TK OP_TK expression CP_TK error
1588 {yyerror ("'{' expected"); RECOVER;}
1591 /* Default assignment is there to avoid type node on switch_block
1597 | OCB_TK switch_labels CCB_TK
1599 | OCB_TK switch_block_statement_groups CCB_TK
1601 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1605 switch_block_statement_groups:
1606 switch_block_statement_group
1607 | switch_block_statement_groups switch_block_statement_group
1610 switch_block_statement_group:
1611 switch_labels block_statements
1616 | switch_labels switch_label
1620 CASE_TK constant_expression REL_CL_TK
1622 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1623 EXPR_WFL_LINECOL (lab) = $1.location;
1624 java_method_add_stmt (current_function_decl, lab);
1626 | DEFAULT_TK REL_CL_TK
1628 tree lab = build (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1629 EXPR_WFL_LINECOL (lab) = $1.location;
1630 java_method_add_stmt (current_function_decl, lab);
1633 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1634 | CASE_TK constant_expression error
1635 {yyerror ("':' expected"); RECOVER;}
1637 {yyerror ("':' expected"); RECOVER;}
1641 WHILE_TK OP_TK expression CP_TK
1643 tree body = build_loop_body ($2.location, $3, 0);
1644 $$ = build_new_loop (body);
1649 while_expression statement
1650 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1652 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1653 | WHILE_TK OP_TK error
1654 {yyerror ("Missing term and ')' expected"); RECOVER;}
1655 | WHILE_TK OP_TK expression error
1656 {yyerror ("')' expected"); RECOVER;}
1659 while_statement_nsi:
1660 while_expression statement_nsi
1661 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1667 tree body = build_loop_body (0, NULL_TREE, 1);
1668 $$ = build_new_loop (body);
1670 /* Need error handing here. FIXME */
1674 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1675 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1679 for_begin SC_TK expression SC_TK for_update CP_TK statement
1681 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1682 $3 = build_wfl_node ($3);
1683 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1685 | for_begin SC_TK SC_TK for_update CP_TK statement
1687 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1688 /* We have not condition, so we get rid of the EXIT_EXPR */
1689 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1692 | for_begin SC_TK error
1693 {yyerror ("Invalid control expression"); RECOVER;}
1694 | for_begin SC_TK expression SC_TK error
1695 {yyerror ("Invalid update expression"); RECOVER;}
1696 | for_begin SC_TK SC_TK error
1697 {yyerror ("Invalid update expression"); RECOVER;}
1701 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1702 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1703 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1705 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1706 /* We have not condition, so we get rid of the EXIT_EXPR */
1707 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1715 /* This scope defined for local variable that may be
1716 defined within the scope of the for loop */
1720 {yyerror ("'(' expected"); DRECOVER(for_1);}
1721 | FOR_TK OP_TK error
1722 {yyerror ("Invalid init statement"); RECOVER;}
1728 /* We now declare the loop body. The loop is
1729 declared as a for loop. */
1730 tree body = build_loop_body (0, NULL_TREE, 0);
1731 $$ = build_new_loop (body);
1732 FOR_LOOP_P ($$) = 1;
1733 /* The loop is added to the current block the for
1734 statement is defined within */
1735 java_method_add_stmt (current_function_decl, $$);
1738 for_init: /* Can be empty */
1739 { $$ = empty_stmt_node; }
1740 | statement_expression_list
1742 /* Init statement recorded within the previously
1743 defined block scope */
1744 $$ = java_method_add_stmt (current_function_decl, $1);
1746 | local_variable_declaration
1748 /* Local variable are recorded within the previously
1749 defined block scope */
1752 | statement_expression_list error
1753 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1756 for_update: /* Can be empty */
1757 {$$ = empty_stmt_node;}
1758 | statement_expression_list
1759 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1762 statement_expression_list:
1763 statement_expression
1764 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1765 | statement_expression_list C_TK statement_expression
1766 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1767 | statement_expression_list C_TK error
1768 {yyerror ("Missing term"); RECOVER;}
1773 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1774 | BREAK_TK identifier SC_TK
1775 { $$ = build_bc_statement ($1.location, 1, $2); }
1777 {yyerror ("Missing term"); RECOVER;}
1778 | BREAK_TK identifier error
1779 {yyerror ("';' expected"); RECOVER;}
1784 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1785 | CONTINUE_TK identifier SC_TK
1786 { $$ = build_bc_statement ($1.location, 0, $2); }
1788 {yyerror ("Missing term"); RECOVER;}
1789 | CONTINUE_TK identifier error
1790 {yyerror ("';' expected"); RECOVER;}
1795 { $$ = build_return ($1.location, NULL_TREE); }
1796 | RETURN_TK expression SC_TK
1797 { $$ = build_return ($1.location, $2); }
1799 {yyerror ("Missing term"); RECOVER;}
1800 | RETURN_TK expression error
1801 {yyerror ("';' expected"); RECOVER;}
1805 THROW_TK expression SC_TK
1807 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1808 EXPR_WFL_LINECOL ($$) = $1.location;
1811 {yyerror ("Missing term"); RECOVER;}
1812 | THROW_TK expression error
1813 {yyerror ("';' expected"); RECOVER;}
1816 synchronized_statement:
1817 synchronized OP_TK expression CP_TK block
1819 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1820 EXPR_WFL_LINECOL ($$) =
1821 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1823 | synchronized OP_TK expression CP_TK error
1824 {yyerror ("'{' expected"); RECOVER;}
1825 | synchronized error
1826 {yyerror ("'(' expected"); RECOVER;}
1827 | synchronized OP_TK error CP_TK
1828 {yyerror ("Missing term"); RECOVER;}
1829 | synchronized OP_TK error
1830 {yyerror ("Missing term"); RECOVER;}
1837 "Illegal modifier `%s'. Only `synchronized' was expected here",
1838 $1, ACC_SYNCHRONIZED);
1839 if ($1 != ACC_SYNCHRONIZED)
1840 MODIFIER_WFL (SYNCHRONIZED_TK) =
1841 build_wfl_node (NULL_TREE);
1846 TRY_TK block catches
1847 { $$ = build_try_statement ($1.location, $2, $3); }
1848 | TRY_TK block finally
1849 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1850 | TRY_TK block catches finally
1851 { $$ = build_try_finally_statement
1852 ($1.location, build_try_statement ($1.location,
1856 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1861 | catches catch_clause
1863 TREE_CHAIN ($2) = $1;
1869 catch_clause_parameter block
1871 java_method_add_stmt (current_function_decl, $2);
1876 catch_clause_parameter:
1877 CATCH_TK OP_TK formal_parameter CP_TK
1879 /* We add a block to define a scope for
1880 formal_parameter (CCBP). The formal parameter is
1881 declared initialized by the appropriate function
1883 tree ccpb = enter_block ();
1884 tree init = build_assignment (ASSIGN_TK, $2.location,
1886 soft_exceptioninfo_call_node);
1887 declare_local_variables (0, TREE_VALUE ($3),
1888 build_tree_list (TREE_PURPOSE ($3),
1890 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1891 EXPR_WFL_LINECOL ($$) = $1.location;
1894 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1895 | CATCH_TK OP_TK error
1897 yyerror ("Missing term or ')' expected");
1898 RECOVER; $$ = NULL_TREE;
1900 | CATCH_TK OP_TK error CP_TK /* That's for () */
1901 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1908 {yyerror ("'{' expected"); RECOVER; }
1911 /* 19.12 Production from 15: Expressions */
1913 primary_no_new_array
1914 | array_creation_expression
1917 primary_no_new_array:
1920 { $$ = build_this ($1.location); }
1921 | OP_TK expression CP_TK
1923 | class_instance_creation_expression
1928 /* Added, JDK1.1 inner classes. Documentation is wrong
1929 refering to a 'ClassName' (class_name) rule that doesn't
1930 exist. Used name: instead. */
1931 | name DOT_TK THIS_TK
1933 tree wfl = build_wfl_node (this_identifier_node);
1934 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1936 | OP_TK expression error
1937 {yyerror ("')' expected"); RECOVER;}
1939 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1940 | primitive_type DOT_TK error
1941 {yyerror ("'class' expected" ); RECOVER;}
1942 | VOID_TK DOT_TK error
1943 {yyerror ("'class' expected" ); RECOVER;}
1947 name DOT_TK CLASS_TK
1948 { $$ = build_incomplete_class_ref ($2.location, $1); }
1949 | array_type DOT_TK CLASS_TK
1950 { $$ = build_incomplete_class_ref ($2.location, $1); }
1951 | primitive_type DOT_TK CLASS_TK
1952 { $$ = build_class_ref ($1); }
1953 | VOID_TK DOT_TK CLASS_TK
1954 { $$ = build_class_ref (void_type_node); }
1957 class_instance_creation_expression:
1958 NEW_TK class_type OP_TK argument_list CP_TK
1959 { $$ = build_new_invocation ($2, $4); }
1960 | NEW_TK class_type OP_TK CP_TK
1961 { $$ = build_new_invocation ($2, NULL_TREE); }
1962 | anonymous_class_creation
1963 /* Added, JDK1.1 inner classes, modified to use name or
1964 primary instead of primary solely which couldn't work in
1966 | something_dot_new identifier OP_TK CP_TK
1968 tree ctor = build_new_invocation ($2, NULL_TREE);
1969 $$ = make_qualified_primary ($1, ctor,
1970 EXPR_WFL_LINECOL ($1));
1972 | something_dot_new identifier OP_TK CP_TK class_body
1973 | something_dot_new identifier OP_TK argument_list CP_TK
1975 tree ctor = build_new_invocation ($2, $4);
1976 $$ = make_qualified_primary ($1, ctor,
1977 EXPR_WFL_LINECOL ($1));
1979 | something_dot_new identifier OP_TK argument_list CP_TK class_body
1980 | NEW_TK error SC_TK
1981 {yyerror ("'(' expected"); DRECOVER(new_1);}
1982 | NEW_TK class_type error
1983 {yyerror ("'(' expected"); RECOVER;}
1984 | NEW_TK class_type OP_TK error
1985 {yyerror ("')' or term expected"); RECOVER;}
1986 | NEW_TK class_type OP_TK argument_list error
1987 {yyerror ("')' expected"); RECOVER;}
1988 | something_dot_new error
1989 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1990 | something_dot_new identifier error
1991 {yyerror ("'(' expected"); RECOVER;}
1994 /* Created after JDK1.1 rules originally added to
1995 class_instance_creation_expression, but modified to use
1996 'class_type' instead of 'TypeName' (type_name) which is mentionned
1997 in the documentation but doesn't exist. */
1999 anonymous_class_creation:
2000 NEW_TK class_type OP_TK argument_list CP_TK
2001 { create_anonymous_class ($1.location, $2); }
2004 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2005 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2007 end_class_declaration (1);
2009 /* Now we can craft the new expression */
2010 $$ = build_new_invocation (id, $4);
2012 /* Note that we can't possibly be here if
2013 `class_type' is an interface (in which case the
2014 anonymous class extends Object and implements
2015 `class_type', hence its constructor can't have
2018 /* Otherwise, the innerclass must feature a
2019 constructor matching `argument_list'. Anonymous
2020 classes are a bit special: it's impossible to
2021 define constructor for them, hence constructors
2022 must be generated following the hints provided by
2023 the `new' expression. Whether a super constructor
2024 of that nature exists or not is to be verified
2025 later on in verify_constructor_super.
2027 It's during the expansion of a `new' statement
2028 refering to an anonymous class that a ctor will
2029 be generated for the anonymous class, with the
2033 | NEW_TK class_type OP_TK CP_TK
2034 { create_anonymous_class ($1.location, $2); }
2037 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2038 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2040 end_class_declaration (1);
2042 /* Now we can craft the new expression. The
2043 statement doesn't need to be remember so that a
2044 constructor can be generated, since its signature
2045 is already known. */
2046 $$ = build_new_invocation (id, NULL_TREE);
2050 something_dot_new: /* Added, not part of the specs. */
2053 | primary DOT_TK NEW_TK
2060 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2061 ctxp->formal_parameter_number = 1;
2063 | argument_list C_TK expression
2065 ctxp->formal_parameter_number += 1;
2066 $$ = tree_cons (NULL_TREE, $3, $1);
2068 | argument_list C_TK error
2069 {yyerror ("Missing term"); RECOVER;}
2072 array_creation_expression:
2073 NEW_TK primitive_type dim_exprs
2074 { $$ = build_newarray_node ($2, $3, 0); }
2075 | NEW_TK class_or_interface_type dim_exprs
2076 { $$ = build_newarray_node ($2, $3, 0); }
2077 | NEW_TK primitive_type dim_exprs dims
2078 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2079 | NEW_TK class_or_interface_type dim_exprs dims
2080 { $$ = build_newarray_node ($2, $3, pop_current_osb (ctxp));}
2081 /* Added, JDK1.1 anonymous array. Initial documentation rule
2083 | NEW_TK class_or_interface_type dims array_initializer
2086 int osb = pop_current_osb (ctxp);
2088 obstack_1grow (&temporary_obstack, '[');
2089 sig = obstack_finish (&temporary_obstack);
2090 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2091 $2, get_identifier (sig), $4);
2093 | NEW_TK primitive_type dims array_initializer
2095 int osb = pop_current_osb (ctxp);
2098 type = build_java_array_type (type, -1);
2099 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2100 build_pointer_type (type), NULL_TREE, $4);
2102 | NEW_TK error CSB_TK
2103 {yyerror ("'[' expected"); DRECOVER ("]");}
2104 | NEW_TK error OSB_TK
2105 {yyerror ("']' expected"); RECOVER;}
2110 { $$ = build_tree_list (NULL_TREE, $1); }
2111 | dim_exprs dim_expr
2112 { $$ = tree_cons (NULL_TREE, $2, $$); }
2116 OSB_TK expression CSB_TK
2118 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2120 $2 = build_wfl_node ($2);
2121 TREE_TYPE ($2) = NULL_TREE;
2123 EXPR_WFL_LINECOL ($2) = $1.location;
2126 | OSB_TK expression error
2127 {yyerror ("']' expected"); RECOVER;}
2130 yyerror ("Missing term");
2131 yyerror ("']' expected");
2140 /* If not initialized, allocate memory for the osb
2142 if (!ctxp->osb_limit)
2144 allocate = ctxp->osb_limit = 32;
2145 ctxp->osb_depth = -1;
2147 /* If capacity overflown, reallocate a bigger chunk */
2148 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2149 allocate = ctxp->osb_limit << 1;
2153 allocate *= sizeof (int);
2154 if (ctxp->osb_number)
2155 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2158 ctxp->osb_number = (int *)xmalloc (allocate);
2161 CURRENT_OSB (ctxp) = 1;
2163 | dims OSB_TK CSB_TK
2164 { CURRENT_OSB (ctxp)++; }
2166 { yyerror ("']' expected"); RECOVER;}
2170 primary DOT_TK identifier
2171 { $$ = make_qualified_primary ($1, $3, $2.location); }
2172 /* FIXME - REWRITE TO:
2173 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2174 | SUPER_TK DOT_TK identifier
2176 tree super_wfl = build_wfl_node (super_identifier_node);
2177 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2178 $$ = make_qualified_name (super_wfl, $3, $2.location);
2181 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2186 { $$ = build_method_invocation ($1, NULL_TREE); }
2187 | name OP_TK argument_list CP_TK
2188 { $$ = build_method_invocation ($1, $3); }
2189 | primary DOT_TK identifier OP_TK CP_TK
2191 if (TREE_CODE ($1) == THIS_EXPR)
2192 $$ = build_this_super_qualified_invocation
2193 (1, $3, NULL_TREE, 0, $2.location);
2196 tree invok = build_method_invocation ($3, NULL_TREE);
2197 $$ = make_qualified_primary ($1, invok, $2.location);
2200 | primary DOT_TK identifier OP_TK argument_list CP_TK
2202 if (TREE_CODE ($1) == THIS_EXPR)
2203 $$ = build_this_super_qualified_invocation
2204 (1, $3, $5, 0, $2.location);
2207 tree invok = build_method_invocation ($3, $5);
2208 $$ = make_qualified_primary ($1, invok, $2.location);
2211 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2213 $$ = build_this_super_qualified_invocation
2214 (0, $3, NULL_TREE, $1.location, $2.location);
2216 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2218 $$ = build_this_super_qualified_invocation
2219 (0, $3, $5, $1.location, $2.location);
2221 /* Screws up thing. I let it here until I'm convinced it can
2223 | primary DOT_TK error
2224 {yyerror ("'(' expected"); DRECOVER(bad);} */
2225 | SUPER_TK DOT_TK error CP_TK
2226 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2227 | SUPER_TK DOT_TK error DOT_TK
2228 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2232 name OSB_TK expression CSB_TK
2233 { $$ = build_array_ref ($2.location, $1, $3); }
2234 | primary_no_new_array OSB_TK expression CSB_TK
2235 { $$ = build_array_ref ($2.location, $1, $3); }
2238 yyerror ("Missing term and ']' expected");
2239 DRECOVER(array_access);
2241 | name OSB_TK expression error
2243 yyerror ("']' expected");
2244 DRECOVER(array_access);
2246 | primary_no_new_array OSB_TK error
2248 yyerror ("Missing term and ']' expected");
2249 DRECOVER(array_access);
2251 | primary_no_new_array OSB_TK expression error
2253 yyerror ("']' expected");
2254 DRECOVER(array_access);
2261 | post_increment_expression
2262 | post_decrement_expression
2265 post_increment_expression:
2266 postfix_expression INCR_TK
2267 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2270 post_decrement_expression:
2271 postfix_expression DECR_TK
2272 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2276 pre_increment_expression
2277 | pre_decrement_expression
2278 | PLUS_TK unary_expression
2279 {$$ = build_unaryop ($1.token, $1.location, $2); }
2280 | MINUS_TK unary_expression
2281 {$$ = build_unaryop ($1.token, $1.location, $2); }
2282 | unary_expression_not_plus_minus
2284 {yyerror ("Missing term"); RECOVER}
2286 {yyerror ("Missing term"); RECOVER}
2289 pre_increment_expression:
2290 INCR_TK unary_expression
2291 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2293 {yyerror ("Missing term"); RECOVER}
2296 pre_decrement_expression:
2297 DECR_TK unary_expression
2298 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2300 {yyerror ("Missing term"); RECOVER}
2303 unary_expression_not_plus_minus:
2305 | NOT_TK unary_expression
2306 {$$ = build_unaryop ($1.token, $1.location, $2); }
2307 | NEG_TK unary_expression
2308 {$$ = build_unaryop ($1.token, $1.location, $2); }
2311 {yyerror ("Missing term"); RECOVER}
2313 {yyerror ("Missing term"); RECOVER}
2316 cast_expression: /* Error handling here is potentially weak */
2317 OP_TK primitive_type dims CP_TK unary_expression
2320 int osb = pop_current_osb (ctxp);
2322 type = build_java_array_type (type, -1);
2323 $$ = build_cast ($1.location, type, $5);
2325 | OP_TK primitive_type CP_TK unary_expression
2326 { $$ = build_cast ($1.location, $2, $4); }
2327 | OP_TK expression CP_TK unary_expression_not_plus_minus
2328 { $$ = build_cast ($1.location, $2, $4); }
2329 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2332 int osb = pop_current_osb (ctxp);
2334 obstack_1grow (&temporary_obstack, '[');
2335 obstack_grow0 (&temporary_obstack,
2336 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2337 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2338 ptr = obstack_finish (&temporary_obstack);
2339 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2340 $$ = build_cast ($1.location, $2, $5);
2342 | OP_TK primitive_type OSB_TK error
2343 {yyerror ("']' expected, invalid type expression");}
2346 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2349 | OP_TK primitive_type dims CP_TK error
2350 {yyerror ("Missing term"); RECOVER;}
2351 | OP_TK primitive_type CP_TK error
2352 {yyerror ("Missing term"); RECOVER;}
2353 | OP_TK name dims CP_TK error
2354 {yyerror ("Missing term"); RECOVER;}
2357 multiplicative_expression:
2359 | multiplicative_expression MULT_TK unary_expression
2361 $$ = build_binop (BINOP_LOOKUP ($2.token),
2362 $2.location, $1, $3);
2364 | multiplicative_expression DIV_TK unary_expression
2366 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2369 | multiplicative_expression REM_TK unary_expression
2371 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2374 | multiplicative_expression MULT_TK error
2375 {yyerror ("Missing term"); RECOVER;}
2376 | multiplicative_expression DIV_TK error
2377 {yyerror ("Missing term"); RECOVER;}
2378 | multiplicative_expression REM_TK error
2379 {yyerror ("Missing term"); RECOVER;}
2382 additive_expression:
2383 multiplicative_expression
2384 | additive_expression PLUS_TK multiplicative_expression
2386 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2389 | additive_expression MINUS_TK multiplicative_expression
2391 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2394 | additive_expression PLUS_TK error
2395 {yyerror ("Missing term"); RECOVER;}
2396 | additive_expression MINUS_TK error
2397 {yyerror ("Missing term"); RECOVER;}
2402 | shift_expression LS_TK additive_expression
2404 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2407 | shift_expression SRS_TK additive_expression
2409 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2412 | shift_expression ZRS_TK additive_expression
2414 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2417 | shift_expression LS_TK error
2418 {yyerror ("Missing term"); RECOVER;}
2419 | shift_expression SRS_TK error
2420 {yyerror ("Missing term"); RECOVER;}
2421 | shift_expression ZRS_TK error
2422 {yyerror ("Missing term"); RECOVER;}
2425 relational_expression:
2427 | relational_expression LT_TK shift_expression
2429 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2432 | relational_expression GT_TK shift_expression
2434 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2437 | relational_expression LTE_TK shift_expression
2439 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2442 | relational_expression GTE_TK shift_expression
2444 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2447 | relational_expression INSTANCEOF_TK reference_type
2448 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2449 | relational_expression LT_TK error
2450 {yyerror ("Missing term"); RECOVER;}
2451 | relational_expression GT_TK error
2452 {yyerror ("Missing term"); RECOVER;}
2453 | relational_expression LTE_TK error
2454 {yyerror ("Missing term"); RECOVER;}
2455 | relational_expression GTE_TK error
2456 {yyerror ("Missing term"); RECOVER;}
2457 | relational_expression INSTANCEOF_TK error
2458 {yyerror ("Invalid reference type"); RECOVER;}
2461 equality_expression:
2462 relational_expression
2463 | equality_expression EQ_TK relational_expression
2465 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2468 | equality_expression NEQ_TK relational_expression
2470 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2473 | equality_expression EQ_TK error
2474 {yyerror ("Missing term"); RECOVER;}
2475 | equality_expression NEQ_TK error
2476 {yyerror ("Missing term"); RECOVER;}
2481 | and_expression AND_TK equality_expression
2483 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2486 | and_expression AND_TK error
2487 {yyerror ("Missing term"); RECOVER;}
2490 exclusive_or_expression:
2492 | exclusive_or_expression XOR_TK and_expression
2494 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2497 | exclusive_or_expression XOR_TK error
2498 {yyerror ("Missing term"); RECOVER;}
2501 inclusive_or_expression:
2502 exclusive_or_expression
2503 | inclusive_or_expression OR_TK exclusive_or_expression
2505 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2508 | inclusive_or_expression OR_TK error
2509 {yyerror ("Missing term"); RECOVER;}
2512 conditional_and_expression:
2513 inclusive_or_expression
2514 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2516 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2519 | conditional_and_expression BOOL_AND_TK error
2520 {yyerror ("Missing term"); RECOVER;}
2523 conditional_or_expression:
2524 conditional_and_expression
2525 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2527 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2530 | conditional_or_expression BOOL_OR_TK error
2531 {yyerror ("Missing term"); RECOVER;}
2534 conditional_expression: /* Error handling here is weak */
2535 conditional_or_expression
2536 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2538 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2539 EXPR_WFL_LINECOL ($$) = $2.location;
2541 | conditional_or_expression REL_QM_TK REL_CL_TK error
2544 yyerror ("Missing term");
2547 | conditional_or_expression REL_QM_TK error
2548 {yyerror ("Missing term"); DRECOVER (2);}
2549 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2550 {yyerror ("Missing term"); DRECOVER (3);}
2553 assignment_expression:
2554 conditional_expression
2559 left_hand_side assignment_operator assignment_expression
2560 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2561 | left_hand_side assignment_operator error
2563 YYNOT_TWICE yyerror ("Missing term");
2574 assignment_operator:
2580 assignment_expression
2583 constant_expression:
2589 /* Helper function to retrieve an OSB count. Should be used when the
2590 `dims:' rule is being used. */
2593 pop_current_osb (ctxp)
2594 struct parser_ctxt *ctxp;
2598 if (ctxp->osb_depth < 0)
2601 to_return = CURRENT_OSB (ctxp);
2609 /* This section of the code deal with save/restoring parser contexts.
2610 Add mode documentation here. FIXME */
2612 /* Helper function. Create a new parser context. With
2613 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2614 context is copied, otherwise, the new context is zeroed. The newly
2615 created context becomes the current one. */
2618 create_new_parser_context (copy_from_previous)
2619 int copy_from_previous;
2621 struct parser_ctxt *new;
2623 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2624 if (copy_from_previous)
2626 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2627 new->saved_data_ctx = 1;
2630 memset ((PTR) new, 0, sizeof (struct parser_ctxt));
2636 /* Create a new parser context and make it the current one. */
2639 java_push_parser_context ()
2641 create_new_parser_context (0);
2644 ctxp->incomplete_class = ctxp->next->incomplete_class;
2645 ctxp->gclass_list = ctxp->next->gclass_list;
2650 java_pop_parser_context (generate)
2654 struct parser_ctxt *toFree, *next;
2663 next->incomplete_class = ctxp->incomplete_class;
2664 next->gclass_list = ctxp->gclass_list;
2665 lineno = ctxp->lineno;
2666 current_class = ctxp->class_type;
2669 /* If the old and new lexers differ, then free the old one. */
2670 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2671 java_destroy_lexer (ctxp->lexer);
2673 /* Set the single import class file flag to 0 for the current list
2674 of imported things */
2675 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2676 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2678 /* And restore those of the previous context */
2679 if ((ctxp = next)) /* Assignment is really meant here */
2680 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2681 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2683 /* If we pushed a context to parse a class intended to be generated,
2684 we keep it so we can remember the class. What we could actually
2685 do is to just update a list of class names. */
2688 toFree->next = ctxp_for_generation;
2689 ctxp_for_generation = toFree;
2695 /* Create a parser context for the use of saving some global
2699 java_parser_context_save_global ()
2703 java_push_parser_context ();
2704 ctxp->saved_data_ctx = 1;
2707 /* If this context already stores data, create a new one suitable
2708 for data storage. */
2709 else if (ctxp->saved_data)
2710 create_new_parser_context (1);
2712 ctxp->lineno = lineno;
2713 ctxp->class_type = current_class;
2714 ctxp->filename = input_filename;
2715 ctxp->function_decl = current_function_decl;
2716 ctxp->saved_data = 1;
2719 /* Restore some global variables from the previous context. Make the
2720 previous context the current one. */
2723 java_parser_context_restore_global ()
2725 lineno = ctxp->lineno;
2726 current_class = ctxp->class_type;
2727 input_filename = ctxp->filename;
2731 BUILD_FILENAME_IDENTIFIER_NODE (s, input_filename);
2732 EXPR_WFL_FILENAME_NODE (wfl_operator) = s;
2734 current_function_decl = ctxp->function_decl;
2735 ctxp->saved_data = 0;
2736 if (ctxp->saved_data_ctx)
2737 java_pop_parser_context (0);
2740 /* Suspend vital data for the current class/function being parsed so
2741 that an other class can be parsed. Used to let local/anonymous
2742 classes be parsed. */
2745 java_parser_context_suspend ()
2747 /* This makes debugging through java_debug_context easier */
2748 static const char *name = "<inner buffer context>";
2750 /* Duplicate the previous context, use it to save the globals we're
2752 create_new_parser_context (1);
2753 ctxp->function_decl = current_function_decl;
2754 ctxp->class_type = current_class;
2756 /* Then create a new context which inherits all data from the
2757 previous one. This will be the new current context */
2758 create_new_parser_context (1);
2760 /* Help debugging */
2761 ctxp->next->filename = name;
2764 /* Resume vital data for the current class/function being parsed so
2765 that an other class can be parsed. Used to let local/anonymous
2766 classes be parsed. The trick is the data storing file position
2767 informations must be restored to their current value, so parsing
2768 can resume as if no context was ever saved. */
2771 java_parser_context_resume ()
2773 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2774 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2775 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2777 /* We need to inherit the list of classes to complete/generate */
2778 restored->incomplete_class = old->incomplete_class;
2779 restored->gclass_list = old->gclass_list;
2780 restored->classd_list = old->classd_list;
2781 restored->class_list = old->class_list;
2783 /* Restore the current class and function from the saver */
2784 current_class = saver->class_type;
2785 current_function_decl = saver->function_decl;
2787 /* Retrive the restored context */
2790 /* Re-installed the data for the parsing to carry on */
2791 bcopy (&old->marker_begining, &ctxp->marker_begining,
2792 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2794 /* Buffer context can now be discarded */
2799 /* Add a new anchor node to which all statement(s) initializing static
2800 and non static initialized upon declaration field(s) will be
2804 java_parser_context_push_initialized_field ()
2808 node = build_tree_list (NULL_TREE, NULL_TREE);
2809 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2810 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2812 node = build_tree_list (NULL_TREE, NULL_TREE);
2813 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2814 CPC_INITIALIZER_LIST (ctxp) = node;
2816 node = build_tree_list (NULL_TREE, NULL_TREE);
2817 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2818 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2821 /* Pop the lists of initialized field. If this lists aren't empty,
2822 remember them so we can use it to create and populate the finit$
2823 or <clinit> functions. */
2826 java_parser_context_pop_initialized_field ()
2829 tree class_type = TREE_TYPE (GET_CPC ());
2831 if (CPC_INITIALIZER_LIST (ctxp))
2833 stmts = CPC_INITIALIZER_STMT (ctxp);
2834 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2835 if (stmts && !java_error_count)
2836 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2839 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2841 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2842 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2843 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2844 /* Keep initialization in order to enforce 8.5 */
2845 if (stmts && !java_error_count)
2846 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2849 /* JDK 1.1 instance initializers */
2850 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2852 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2853 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2854 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2855 if (stmts && !java_error_count)
2856 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2861 reorder_static_initialized (list)
2864 /* We have to keep things in order. The alias initializer have to
2865 come first, then the initialized regular field, in reverse to
2866 keep them in lexical order. */
2867 tree marker, previous = NULL_TREE;
2868 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2869 if (TREE_CODE (marker) == TREE_LIST
2870 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2873 /* No static initialized, the list is fine as is */
2875 list = TREE_CHAIN (marker);
2877 /* No marker? reverse the whole list */
2879 list = nreverse (list);
2881 /* Otherwise, reverse what's after the marker and the new reordered
2882 sublist will replace the marker. */
2885 TREE_CHAIN (previous) = NULL_TREE;
2886 list = nreverse (list);
2887 list = chainon (TREE_CHAIN (marker), list);
2892 /* Helper functions to dump the parser context stack. */
2894 #define TAB_CONTEXT(C) \
2895 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2898 java_debug_context_do (tab)
2901 struct parser_ctxt *copy = ctxp;
2905 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2907 fprintf (stderr, "filename: %s\n", copy->filename);
2909 fprintf (stderr, "lineno: %d\n", copy->lineno);
2911 fprintf (stderr, "package: %s\n",
2913 IDENTIFIER_POINTER (copy->package) : "<none>"));
2915 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2917 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2923 /* Dump the stacked up parser contexts. Intended to be called from a
2927 java_debug_context ()
2929 java_debug_context_do (0);
2934 /* Flag for the error report routine to issue the error the first time
2935 it's called (overriding the default behavior which is to drop the
2936 first invocation and honor the second one, taking advantage of a
2938 static int force_error = 0;
2940 /* Reporting an constructor invocation error. */
2942 parse_ctor_invocation_error ()
2944 if (DECL_CONSTRUCTOR_P (current_function_decl))
2945 yyerror ("Constructor invocation must be first thing in a constructor");
2947 yyerror ("Only constructors can invoke constructors");
2950 /* Reporting JDK1.1 features not implemented. */
2953 parse_jdk1_1_error (msg)
2956 sorry (": `%s' JDK1.1(TM) feature", msg);
2958 return empty_stmt_node;
2961 static int do_warning = 0;
2968 static int prev_lineno;
2969 static const char *prev_msg;
2972 char *remainder, *code_from_source;
2974 if (!force_error && prev_lineno == lineno)
2977 /* Save current error location but report latter, when the context is
2979 if (ctxp->java_error_flag == 0)
2981 ctxp->java_error_flag = 1;
2983 /* Do something to use the previous line if we're reaching the
2984 end of the file... */
2985 #ifdef VERBOSE_SKELETON
2986 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2991 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2992 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2995 ctxp->java_error_flag = 0;
2997 java_warning_count++;
3001 if (elc.col == 0 && msg && msg[1] == ';')
3003 elc.col = ctxp->p_line->char_col-1;
3004 elc.line = ctxp->p_line->lineno;
3007 save_lineno = lineno;
3008 prev_lineno = lineno = elc.line;
3011 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
3012 obstack_grow0 (&temporary_obstack,
3013 code_from_source, strlen (code_from_source));
3014 remainder = obstack_finish (&temporary_obstack);
3016 warning ("%s.\n%s", msg, remainder);
3018 error ("%s.\n%s", msg, remainder);
3020 /* This allow us to cheaply avoid an extra 'Invalid expression
3021 statement' error report when errors have been already reported on
3022 the same line. This occurs when we report an error but don't have
3023 a synchronization point other than ';', which
3024 expression_statement is the only one to take care of. */
3025 ctxp->prevent_ese = lineno = save_lineno;
3029 issue_warning_error_from_context (cl, msg, ap)
3034 const char *saved, *saved_input_filename;
3036 vsprintf (buffer, msg, ap);
3039 ctxp->elc.line = EXPR_WFL_LINENO (cl);
3040 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3041 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3043 /* We have a CL, that's a good reason for using it if it contains data */
3044 saved = ctxp->filename;
3045 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3046 ctxp->filename = EXPR_WFL_FILENAME (cl);
3047 saved_input_filename = input_filename;
3048 input_filename = ctxp->filename;
3050 java_error (buffer);
3051 ctxp->filename = saved;
3052 input_filename = saved_input_filename;
3056 /* Issue an error message at a current source line CL */
3059 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3061 #ifndef ANSI_PROTOTYPES
3068 #ifndef ANSI_PROTOTYPES
3069 cl = va_arg (ap, tree);
3070 msg = va_arg (ap, const char *);
3072 issue_warning_error_from_context (cl, msg, ap);
3076 /* Issue a warning at a current source line CL */
3079 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3081 #ifndef ANSI_PROTOTYPES
3088 #ifndef ANSI_PROTOTYPES
3089 cl = va_arg (ap, tree);
3090 msg = va_arg (ap, const char *);
3093 force_error = do_warning = 1;
3094 issue_warning_error_from_context (cl, msg, ap);
3095 do_warning = force_error = 0;
3100 find_expr_with_wfl (node)
3108 switch (TREE_CODE (node))
3111 node = BLOCK_EXPR_BODY (node);
3115 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3118 node = TREE_OPERAND (node, 1);
3122 node = TREE_OPERAND (node, 0);
3125 case LABELED_BLOCK_EXPR:
3126 node = TREE_OPERAND (node, 1);
3130 code = TREE_CODE_CLASS (TREE_CODE (node));
3131 if (((code == '1') || (code == '2') || (code == 'e'))
3132 && EXPR_WFL_LINECOL (node))
3140 /* Issue a missing return statement error. Uses METHOD to figure the
3141 last line of the method the error occurs in. */
3144 missing_return_error (method)
3147 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3148 parse_error_context (wfl_operator, "Missing return statement");
3151 /* Issue an unreachable statement error. From NODE, find the next
3152 statement to report appropriately. */
3154 unreachable_stmt_error (node)
3157 /* Browse node to find the next expression node that has a WFL. Use
3158 the location to report the error */
3159 if (TREE_CODE (node) == COMPOUND_EXPR)
3160 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3162 node = find_expr_with_wfl (node);
3166 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3167 parse_error_context (wfl_operator, "Unreachable statement");
3174 java_report_errors ()
3176 if (java_error_count)
3177 fprintf (stderr, "%d error%s",
3178 java_error_count, (java_error_count == 1 ? "" : "s"));
3179 if (java_warning_count)
3180 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3181 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3182 if (java_error_count || java_warning_count)
3183 putc ('\n', stderr);
3184 return java_error_count;
3188 java_accstring_lookup (flags)
3191 static char buffer [80];
3192 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3194 /* Access modifier looked-up first for easier report on forbidden
3196 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3197 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3198 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3199 if (flags & ACC_STATIC) COPY_RETURN ("static");
3200 if (flags & ACC_FINAL) COPY_RETURN ("final");
3201 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3202 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3203 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3204 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3205 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3206 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3213 /* Issuing error messages upon redefinition of classes, interfaces or
3217 classitf_redefinition_error (context, id, decl, cl)
3218 const char *context;
3221 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3222 context, IDENTIFIER_POINTER (id),
3223 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3224 /* Here we should point out where its redefined. It's a unicode. FIXME */
3228 variable_redefinition_error (context, name, type, line)
3229 tree context, name, type;
3232 const char *type_name;
3234 /* Figure a proper name for type. We might haven't resolved it */
3235 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3236 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3238 type_name = lang_printable_name (type, 0);
3240 parse_error_context (context,
3241 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3242 IDENTIFIER_POINTER (name),
3243 type_name, IDENTIFIER_POINTER (name), line);
3247 build_array_from_name (type, type_wfl, name, ret_name)
3248 tree type, type_wfl, name, *ret_name;
3253 /* Eventually get more dims */
3254 string = IDENTIFIER_POINTER (name);
3255 while (string [more_dims] == '[')
3258 /* If we have, then craft a new type for this variable */
3261 name = get_identifier (&string [more_dims]);
3263 /* If we have a pointer, use its type */
3264 if (TREE_CODE (type) == POINTER_TYPE)
3265 type = TREE_TYPE (type);
3267 /* Building the first dimension of a primitive type uses this
3269 if (JPRIMITIVE_TYPE_P (type))
3271 type = build_java_array_type (type, -1);
3272 CLASS_LOADED_P (type) = 1;
3275 /* Otherwise, if we have a WFL for this type, use it (the type
3276 is already an array on an unresolved type, and we just keep
3277 on adding dimensions) */
3281 /* Add all the dimensions */
3283 type = build_unresolved_array_type (type);
3285 /* The type may have been incomplete in the first place */
3287 type = obtain_incomplete_type (type);
3295 /* Build something that the type identifier resolver will identify as
3296 being an array to an unresolved type. TYPE_WFL is a WFL on a
3300 build_unresolved_array_type (type_or_wfl)
3305 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3306 just create a array type */
3307 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3309 tree type = build_java_array_type (type_or_wfl, -1);
3310 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3314 obstack_1grow (&temporary_obstack, '[');
3315 obstack_grow0 (&temporary_obstack,
3316 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3317 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3318 ptr = obstack_finish (&temporary_obstack);
3319 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3324 parser_add_interface (class_decl, interface_decl, wfl)
3325 tree class_decl, interface_decl, wfl;
3327 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3328 parse_error_context (wfl, "Interface `%s' repeated",
3329 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3332 /* Bulk of common class/interface checks. Return 1 if an error was
3333 encountered. TAG is 0 for a class, 1 for an interface. */
3336 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3337 int is_interface, flags;
3338 tree raw_name, qualified_name, decl, cl;
3341 int sca = 0; /* Static class allowed */
3342 int icaf = 0; /* Inner class allowed flags */
3343 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3346 fprintf (stderr, " %s%s %s",
3347 (CPC_INNER_P () ? "inner" : ""),
3348 (is_interface ? "interface" : "class"),
3349 IDENTIFIER_POINTER (qualified_name));
3351 /* Scope of an interface/class type name:
3352 - Can't be imported by a single type import
3353 - Can't already exists in the package */
3354 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3355 && (node = find_name_in_single_imports (raw_name))
3359 (cl, "%s name `%s' clashes with imported type `%s'",
3360 (is_interface ? "Interface" : "Class"),
3361 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3364 if (decl && CLASS_COMPLETE_P (decl))
3366 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3367 qualified_name, decl, cl);
3371 if (check_inner_class_redefinition (raw_name, cl))
3374 /* If public, file name should match class/interface name, except
3375 when dealing with an inner class */
3376 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3380 /* Contains OS dependent assumption on path separator. FIXME */
3381 for (f = &input_filename [strlen (input_filename)];
3382 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3385 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
3387 if (strncmp (IDENTIFIER_POINTER (raw_name),
3388 f , IDENTIFIER_LENGTH (raw_name)) ||
3389 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3391 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3392 (is_interface ? "interface" : "class"),
3393 IDENTIFIER_POINTER (qualified_name),
3394 IDENTIFIER_POINTER (raw_name));
3397 /* Static classes can be declared only in top level classes. Note:
3398 once static, a inner class is a top level class. */
3399 if (flags & ACC_STATIC)
3401 /* Catch the specific error of declaring an class inner class
3402 with no toplevel enclosing class. Prevent check_modifiers from
3403 complaining a second time */
3404 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3406 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3407 IDENTIFIER_POINTER (qualified_name));
3410 /* Else, in the context of a top-level class declaration, let
3411 `check_modifiers' do its job, otherwise, give it a go */
3413 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3416 /* Inner classes can be declared private or protected
3417 within their enclosing classes. */
3420 /* A class which is local to a block can't be public, private,
3421 protected or static. But it is created final, so allow this
3423 if (current_function_decl)
3424 icaf = sca = uaaf = ACC_FINAL;
3427 check_modifiers_consistency (flags);
3428 icaf = ACC_PRIVATE|ACC_PROTECTED;
3435 uaaf = INTERFACE_INNER_MODIFIERS;
3437 uaaf = INTERFACE_MODIFIERS;
3439 check_modifiers ("Illegal modifier `%s' for interface declaration",
3443 check_modifiers ((current_function_decl ?
3444 "Illegal modifier `%s' for local class declaration" :
3445 "Illegal modifier `%s' for class declaration"),
3446 flags, uaaf|sca|icaf);
3451 make_nested_class_name (cpc_list)
3459 make_nested_class_name (TREE_CHAIN (cpc_list));
3461 /* Pick the qualified name when dealing with the first upmost
3463 name = (TREE_CHAIN (cpc_list) ?
3464 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3465 obstack_grow (&temporary_obstack,
3466 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3467 /* Why is NO_DOLLAR_IN_LABEL defined? */
3469 #ifdef NO_DOLLAR_IN_LABEL
3470 internal_error ("Can't use '$' as a separator for inner classes");
3473 obstack_1grow (&temporary_obstack, '$');
3476 /* Can't redefine a class already defined in an earlier scope. */
3479 check_inner_class_redefinition (raw_name, cl)
3484 for (scope_list = GET_CPC_LIST (); scope_list;
3485 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3486 if (raw_name == GET_CPC_UN_NODE (scope_list))
3489 (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",
3490 IDENTIFIER_POINTER (raw_name));
3497 find_as_inner_class (enclosing, name, cl)
3498 tree enclosing, name, cl;
3500 tree qual, to_return;
3504 name = TYPE_NAME (name);
3506 /* First search: within the scope of `enclosing', search for name */
3507 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3508 qual = EXPR_WFL_QUALIFICATION (cl);
3510 qual = build_tree_list (cl, NULL_TREE);
3512 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3514 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3517 /* We're dealing with a qualified name. Try to resolve thing until
3518 we get something that is an enclosing class. */
3519 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3521 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3523 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3524 qual = TREE_CHAIN (qual))
3526 acc = merge_qualified_name (acc,
3527 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3528 BUILD_PTR_FROM_NAME (ptr, acc);
3529 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3532 /* A NULL qual and a decl means that the search ended
3533 successfully?!? We have to do something then. FIXME */
3538 qual = EXPR_WFL_QUALIFICATION (cl);
3540 /* Otherwise, create a qual for the other part of the resolution. */
3542 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3544 return find_as_inner_class_do (qual, enclosing);
3547 /* We go inside the list of sub classes and try to find a way
3551 find_as_inner_class_do (qual, enclosing)
3552 tree qual, enclosing;
3557 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3559 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3560 tree next_enclosing = NULL_TREE;
3563 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3564 inner_list; inner_list = TREE_CHAIN (inner_list))
3566 if (TREE_VALUE (inner_list) == name_to_match)
3568 next_enclosing = TREE_PURPOSE (inner_list);
3572 enclosing = next_enclosing;
3575 return (!qual && enclosing ? enclosing : NULL_TREE);
3578 /* Reach all inner classes and tie their unqualified name to a
3582 set_nested_class_simple_name_value (outer, set)
3588 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3589 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3590 TREE_PURPOSE (l) : NULL_TREE);
3594 link_nested_class_to_enclosing ()
3596 if (GET_ENCLOSING_CPC ())
3598 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3599 DECL_INNER_CLASS_LIST (enclosing) =
3600 tree_cons (GET_CPC (), GET_CPC_UN (),
3601 DECL_INNER_CLASS_LIST (enclosing));
3602 enclosing = enclosing;
3607 maybe_make_nested_class_name (name)
3610 tree id = NULL_TREE;
3614 make_nested_class_name (GET_CPC_LIST ());
3615 obstack_grow0 (&temporary_obstack,
3616 IDENTIFIER_POINTER (name),
3617 IDENTIFIER_LENGTH (name));
3618 id = get_identifier (obstack_finish (&temporary_obstack));
3620 QUALIFIED_P (id) = 1;
3625 /* If DECL is NULL, create and push a new DECL, record the current
3626 line CL and do other maintenance things. */
3629 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3630 tree decl, raw_name, qualified_name, cl;
3633 decl = push_class (make_class (), qualified_name);
3635 /* Take care of the file and line business */
3636 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3637 /* If we're emiting xrefs, store the line/col number information */
3639 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3641 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3642 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3643 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3644 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3646 PUSH_CPC (decl, raw_name);
3647 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3649 /* Link the declaration to the already seen ones */
3650 TREE_CHAIN (decl) = ctxp->class_list;
3651 ctxp->class_list = decl;
3653 /* Create a new nodes in the global lists */
3654 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
3655 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3657 /* Install a new dependency list element */
3658 create_jdep_list (ctxp);
3660 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3661 IDENTIFIER_POINTER (qualified_name)));
3666 add_superinterfaces (decl, interface_list)
3667 tree decl, interface_list;
3670 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3671 takes care of ensuring that:
3672 - This is an accessible interface type,
3673 - Circularity detection.
3674 parser_add_interface is then called. If present but not defined,
3675 the check operation is delayed until the super interface gets
3677 for (node = interface_list; node; node = TREE_CHAIN (node))
3679 tree current = TREE_PURPOSE (node);
3680 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3681 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3683 if (!parser_check_super_interface (idecl, decl, current))
3684 parser_add_interface (decl, idecl, current);
3687 register_incomplete_type (JDEP_INTERFACE,
3688 current, decl, NULL_TREE);
3692 /* Create an interface in pass1 and return its decl. Return the
3693 interface's decl in pass 2. */
3696 create_interface (flags, id, super)
3700 tree raw_name = EXPR_WFL_NODE (id);
3701 tree q_name = parser_qualified_classname (raw_name);
3702 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3704 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3706 /* Basic checks: scope, redefinition, modifiers */
3707 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3713 /* Suspend the current parsing context if we're parsing an inner
3716 java_parser_context_suspend ();
3718 /* Push a new context for (static) initialized upon declaration fields */
3719 java_parser_context_push_initialized_field ();
3721 /* Interface modifiers check
3722 - public/abstract allowed (already done at that point)
3723 - abstract is obsolete (comes first, it's a warning, or should be)
3724 - Can't use twice the same (checked in the modifier rule) */
3725 if ((flags & ACC_ABSTRACT) && flag_redundant)
3726 parse_warning_context
3727 (MODIFIER_WFL (ABSTRACT_TK),
3728 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
3730 /* Create a new decl if DECL is NULL, otherwise fix it */
3731 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3733 /* Set super info and mark the class a complete */
3734 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3735 object_type_node, ctxp->interface_number);
3736 ctxp->interface_number = 0;
3737 CLASS_COMPLETE_P (decl) = 1;
3738 add_superinterfaces (decl, super);
3743 /* Anonymous class counter. Will be reset to 1 every time a non
3744 anonymous class gets created. */
3745 static int anonymous_class_counter = 1;
3747 /* Patch anonymous class CLASS, by either extending or implementing
3751 patch_anonymous_class (type_decl, class_decl, wfl)
3752 tree type_decl, class_decl, wfl;
3754 tree class = TREE_TYPE (class_decl);
3755 tree type = TREE_TYPE (type_decl);
3756 tree binfo = TYPE_BINFO (class);
3758 /* If it's an interface, implement it */
3759 if (CLASS_INTERFACE (type_decl))
3764 if (parser_check_super_interface (type_decl, class_decl, wfl))
3767 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3768 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3769 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3770 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3771 /* And add the interface */
3772 parser_add_interface (class_decl, type_decl, wfl);
3774 /* Otherwise, it's a type we want to extend */
3777 if (parser_check_super (type_decl, class_decl, wfl))
3779 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3784 create_anonymous_class (location, type_name)
3789 tree super = NULL_TREE, itf = NULL_TREE;
3790 tree id, type_decl, class;
3792 /* The unqualified name of the anonymous class. It's just a number. */
3793 sprintf (buffer, "%d", anonymous_class_counter++);
3794 id = build_wfl_node (get_identifier (buffer));
3795 EXPR_WFL_LINECOL (id) = location;
3797 /* We know about the type to extend/implement. We go ahead */
3798 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3800 /* Create a class which either implements on extends the designated
3801 class. The class bears an innacessible name. */
3802 if (CLASS_INTERFACE (type_decl))
3804 /* It's OK to modify it here. It's been already used and
3805 shouldn't be reused */
3806 ctxp->interface_number = 1;
3807 /* Interfaces should presented as a list of WFLs */
3808 itf = build_tree_list (type_name, NULL_TREE);
3814 class = create_class (ACC_FINAL, id, super, itf);
3816 /* We didn't know anything about the stuff. We register a dependence. */
3818 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3820 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3824 /* Create a class in pass1 and return its decl. Return class
3825 interface's decl in pass 2. */
3828 create_class (flags, id, super, interfaces)
3830 tree id, super, interfaces;
3832 tree raw_name = EXPR_WFL_NODE (id);
3833 tree class_id, decl;
3834 tree super_decl_type;
3836 class_id = parser_qualified_classname (raw_name);
3837 decl = IDENTIFIER_CLASS_VALUE (class_id);
3838 EXPR_WFL_NODE (id) = class_id;
3840 /* Basic check: scope, redefinition, modifiers */
3841 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3847 /* Suspend the current parsing context if we're parsing an inner
3848 class or an anonymous class. */
3850 java_parser_context_suspend ();
3851 /* Push a new context for (static) initialized upon declaration fields */
3852 java_parser_context_push_initialized_field ();
3854 /* Class modifier check:
3855 - Allowed modifier (already done at that point)
3856 - abstract AND final forbidden
3857 - Public classes defined in the correct file */
3858 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3860 (id, "Class `%s' can't be declared both abstract and final",
3861 IDENTIFIER_POINTER (raw_name));
3863 /* Create a new decl if DECL is NULL, otherwise fix it */
3864 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
3866 /* If SUPER exists, use it, otherwise use Object */
3869 /* Can't extend java.lang.Object */
3870 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3872 parse_error_context (id, "Can't extend `java.lang.Object'");
3877 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
3879 else if (TREE_TYPE (decl) != object_type_node)
3880 super_decl_type = object_type_node;
3881 /* We're defining java.lang.Object */
3883 super_decl_type = NULL_TREE;
3885 /* A class nested in an interface is implicitly static. */
3886 if (INNER_CLASS_DECL_P (decl)
3887 && CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (DECL_CONTEXT (decl)))))
3889 flags |= ACC_STATIC;
3892 /* Set super info and mark the class as complete. */
3893 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3894 ctxp->interface_number);
3895 ctxp->interface_number = 0;
3896 CLASS_COMPLETE_P (decl) = 1;
3897 add_superinterfaces (decl, interfaces);
3899 /* Add the private this$<n> field, Replicate final locals still in
3900 scope as private final fields mangled like val$<local_name>.
3901 This doesn't not occur for top level (static) inner classes. */
3902 if (PURE_INNER_CLASS_DECL_P (decl))
3903 add_inner_class_fields (decl, current_function_decl);
3905 /* If doing xref, store the location at which the inherited class
3906 (if any) was seen. */
3907 if (flag_emit_xref && super)
3908 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3910 /* Eventually sets the @deprecated tag flag */
3911 CHECK_DEPRECATED (decl);
3913 /* Reset the anonymous class counter when declaring non inner classes */
3914 if (!INNER_CLASS_DECL_P (decl))
3915 anonymous_class_counter = 1;
3920 /* End a class declaration: register the statements used to create
3921 finit$ and <clinit>, pop the current class and resume the prior
3922 parser context if necessary. */
3925 end_class_declaration (resume)
3928 /* If an error occured, context weren't pushed and won't need to be
3929 popped by a resume. */
3930 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3932 java_parser_context_pop_initialized_field ();
3934 if (resume && no_error_occured)
3935 java_parser_context_resume ();
3937 /* We're ending a class declaration, this is a good time to reset
3938 the interface cout. Note that might have been already done in
3939 create_interface, but if at that time an inner class was being
3940 dealt with, the interface count was reset in a context created
3941 for the sake of handling inner classes declaration. */
3942 ctxp->interface_number = 0;
3946 add_inner_class_fields (class_decl, fct_decl)
3950 tree block, marker, f;
3952 f = add_field (TREE_TYPE (class_decl),
3953 build_current_thisn (TREE_TYPE (class_decl)),
3954 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3956 FIELD_THISN (f) = 1;
3961 for (block = GET_CURRENT_BLOCK (fct_decl);
3962 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3965 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3968 tree wfl, init, list;
3970 /* Avoid non final arguments. */
3971 if (!LOCAL_FINAL_P (decl))
3974 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3975 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3976 wfl = build_wfl_node (name);
3977 init = build_wfl_node (pname);
3978 /* Build an initialization for the field: it will be
3979 initialized by a parameter added to finit$, bearing a
3980 mangled name of the field itself (param$<n>.) The
3981 parameter is provided to finit$ by the constructor
3982 invoking it (hence the constructor will also feature a
3983 hidden parameter, set to the value of the outer context
3984 local at the time the inner class is created.)
3986 Note: we take into account all possible locals that can
3987 be accessed by the inner class. It's actually not trivial
3988 to minimize these aliases down to the ones really
3989 used. One way to do that would be to expand all regular
3990 methods first, then finit$ to get a picture of what's
3991 used. It works with the exception that we would have to
3992 go back on all constructor invoked in regular methods to
3993 have their invokation reworked (to include the right amount
3994 of alias initializer parameters.)
3996 The only real way around, I think, is a first pass to
3997 identify locals really used in the inner class. We leave
3998 the flag FIELD_LOCAL_ALIAS_USED around for that future
4001 On the other hand, it only affect local inner classes,
4002 whose constructors (and finit$ call) will be featuring
4003 unecessary arguments. It's easy for a developper to keep
4004 this number of parameter down by using the `final'
4005 keyword only when necessary. For the time being, we can
4006 issue a warning on unecessary finals. FIXME */
4007 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
4010 /* Register the field. The TREE_LIST holding the part
4011 initialized/initializer will be marked ARG_FINAL_P so
4012 that the created field can be marked
4013 FIELD_LOCAL_ALIAS. */
4014 list = build_tree_list (wfl, init);
4015 ARG_FINAL_P (list) = 1;
4016 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
4020 if (!CPC_INITIALIZER_STMT (ctxp))
4023 /* If we ever registered an alias field, insert and marker to
4024 remeber where the list ends. The second part of the list (the one
4025 featuring initialized fields) so it can be later reversed to
4026 enforce 8.5. The marker will be removed during that operation. */
4027 marker = build_tree_list (NULL_TREE, NULL_TREE);
4028 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4029 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4032 /* Can't use lookup_field () since we don't want to load the class and
4033 can't set the CLASS_LOADED_P flag */
4036 find_field (class, name)
4041 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4043 if (DECL_NAME (decl) == name)
4049 /* Wrap around lookup_field that doesn't potentially upset the value
4053 lookup_field_wrapper (class, name)
4057 tree decl = NULL_TREE;
4058 java_parser_context_save_global ();
4060 /* Last chance: if we're within the context of an inner class, we
4061 might be trying to access a local variable defined in an outer
4062 context. We try to look for it now. */
4063 if (INNER_CLASS_TYPE_P (class))
4066 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4067 decl = lookup_field (&type, new_name);
4068 if (decl && decl != error_mark_node)
4069 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4071 if (!decl || decl == error_mark_node)
4074 decl = lookup_field (&type, name);
4077 java_parser_context_restore_global ();
4078 return decl == error_mark_node ? NULL : decl;
4081 /* Find duplicate field within the same class declarations and report
4082 the error. Returns 1 if a duplicated field was found, 0
4086 duplicate_declaration_error_p (new_field_name, new_type, cl)
4087 tree new_field_name, new_type, cl;
4089 /* This might be modified to work with method decl as well */
4090 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4093 char *t1 = xstrdup (purify_type_name
4094 ((TREE_CODE (new_type) == POINTER_TYPE
4095 && TREE_TYPE (new_type) == NULL_TREE) ?
4096 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4097 lang_printable_name (new_type, 1)));
4098 /* The type may not have been completed by the time we report
4100 char *t2 = xstrdup (purify_type_name
4101 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4102 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4103 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4104 lang_printable_name (TREE_TYPE (decl), 1)));
4106 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4107 t1, IDENTIFIER_POINTER (new_field_name),
4108 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4109 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4117 /* Field registration routine. If TYPE doesn't exist, field
4118 declarations are linked to the undefined TYPE dependency list, to
4119 be later resolved in java_complete_class () */
4122 register_fields (flags, type, variable_list)
4124 tree type, variable_list;
4126 tree current, saved_type;
4127 tree class_type = NULL_TREE;
4128 int saved_lineno = lineno;
4130 tree wfl = NULL_TREE;
4133 class_type = TREE_TYPE (GET_CPC ());
4135 if (!class_type || class_type == error_mark_node)
4138 /* If we're adding fields to interfaces, those fields are public,
4140 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4142 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4143 flags, ACC_PUBLIC, "interface field(s)");
4144 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4145 flags, ACC_STATIC, "interface field(s)");
4146 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4147 flags, ACC_FINAL, "interface field(s)");
4148 check_modifiers ("Illegal interface member modifier `%s'", flags,
4149 INTERFACE_FIELD_MODIFIERS);
4150 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4153 /* Obtain a suitable type for resolution, if necessary */
4154 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4156 /* If TYPE is fully resolved and we don't have a reference, make one */
4157 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4159 for (current = variable_list, saved_type = type; current;
4160 current = TREE_CHAIN (current), type = saved_type)
4164 tree cl = TREE_PURPOSE (current);
4165 tree init = TREE_VALUE (current);
4166 tree current_name = EXPR_WFL_NODE (cl);
4168 /* Can't declare non-final static fields in inner classes */
4169 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4170 && !(flags & ACC_FINAL))
4172 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4173 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4174 lang_printable_name (class_type, 0));
4176 /* Process NAME, as it may specify extra dimension(s) for it */
4177 type = build_array_from_name (type, wfl, current_name, ¤t_name);
4179 /* Type adjustment. We may have just readjusted TYPE because
4180 the variable specified more dimensions. Make sure we have
4181 a reference if we can and don't have one already. Also
4182 change the name if we have an init. */
4183 if (type != saved_type)
4185 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4187 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4190 real_type = GET_REAL_TYPE (type);
4191 /* Check for redeclarations */
4192 if (duplicate_declaration_error_p (current_name, real_type, cl))
4195 /* Set lineno to the line the field was found and create a
4196 declaration for it. Eventually sets the @deprecated tag flag. */
4198 lineno = EXPR_WFL_LINECOL (cl);
4200 lineno = EXPR_WFL_LINENO (cl);
4201 field_decl = add_field (class_type, current_name, real_type, flags);
4202 CHECK_DEPRECATED (field_decl);
4204 /* If the field denotes a final instance variable, then we
4205 allocate a LANG_DECL_SPECIFIC part to keep track of its
4206 initialization. We also mark whether the field was
4207 initialized upon it's declaration. We don't do that if the
4208 created field is an alias to a final local. */
4209 if (!ARG_FINAL_P (current) && (flags & ACC_FINAL))
4211 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (field_decl);
4212 DECL_FIELD_FINAL_WFL (field_decl) = cl;
4213 if ((flags & ACC_STATIC) && init)
4214 DECL_FIELD_FINAL_IUD (field_decl) = 1;
4217 /* If the couple initializer/initialized is marked ARG_FINAL_P,
4218 we mark the created field FIELD_LOCAL_ALIAS, so that we can
4219 hide parameters to this inner class finit$ and
4220 constructors. It also means that the field isn't final per
4222 if (ARG_FINAL_P (current))
4224 FIELD_LOCAL_ALIAS (field_decl) = 1;
4225 FIELD_FINAL (field_decl) = 0;
4228 /* Check if we must chain. */
4230 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4232 /* If we have an initialization value tied to the field */
4235 /* The field is declared static */
4236 if (flags & ACC_STATIC)
4238 /* We include the field and its initialization part into
4239 a list used to generate <clinit>. After <clinit> is
4240 walked, field initializations will be processed and
4241 fields initialized with known constants will be taken
4242 out of <clinit> and have their DECL_INITIAL set
4244 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4245 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4246 if (TREE_OPERAND (init, 1)
4247 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4248 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4250 /* A non-static field declared with an immediate initialization is
4251 to be initialized in <init>, if any. This field is remembered
4252 to be processed at the time of the generation of <init>. */
4255 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4256 SET_CPC_INITIALIZER_STMT (ctxp, init);
4258 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4259 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4262 lineno = saved_lineno;
4265 /* Generate finit$, using the list of initialized fields to populate
4266 its body. finit$'s parameter(s) list is adjusted to include the
4267 one(s) used to initialized the field(s) caching outer context
4271 generate_finit (class_type)
4275 tree list = TYPE_FINIT_STMT_LIST (class_type);
4276 tree mdecl, current, parms;
4278 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4279 class_type, NULL_TREE,
4281 CRAFTED_PARAM_LIST_FIXUP (parms);
4282 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4283 finit_identifier_node, parms);
4284 fix_method_argument_names (parms, mdecl);
4285 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4287 DECL_FUNCTION_NAP (mdecl) = count;
4288 start_artificial_method_body (mdecl);
4290 for (current = list; current; current = TREE_CHAIN (current))
4291 java_method_add_stmt (mdecl,
4292 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4294 end_artificial_method_body (mdecl);
4299 add_instance_initializer (mdecl)
4303 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4304 tree compound = NULL_TREE;
4308 for (current = stmt_list; current; current = TREE_CHAIN (current))
4309 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4311 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4312 NULL_TREE, compound));
4316 /* Shared accros method_declarator and method_header to remember the
4317 patch stage that was reached during the declaration of the method.
4318 A method DECL is built differently is there is no patch
4319 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4320 pending on the currently defined method. */
4322 static int patch_stage;
4324 /* Check the method declaration and add the method to its current
4325 class. If the argument list is known to contain incomplete types,
4326 the method is partially added and the registration will be resume
4327 once the method arguments resolved. If TYPE is NULL, we're dealing
4328 with a constructor. */
4331 method_header (flags, type, mdecl, throws)
4333 tree type, mdecl, throws;
4335 tree type_wfl = NULL_TREE;
4336 tree meth_name = NULL_TREE;
4337 tree current, orig_arg, this_class = NULL;
4340 int constructor_ok = 0, must_chain;
4343 if (mdecl == error_mark_node)
4344 return error_mark_node;
4345 meth = TREE_VALUE (mdecl);
4346 id = TREE_PURPOSE (mdecl);
4348 check_modifiers_consistency (flags);
4351 this_class = TREE_TYPE (GET_CPC ());
4353 if (!this_class || this_class == error_mark_node)
4356 /* There are some forbidden modifiers for an abstract method and its
4357 class must be abstract as well. */
4358 if (type && (flags & ACC_ABSTRACT))
4360 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4361 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4362 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4363 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4364 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
4365 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4366 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4368 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4369 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4370 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4373 /* Things to be checked when declaring a constructor */
4376 int ec = java_error_count;
4377 /* 8.6: Constructor declarations: we might be trying to define a
4378 method without specifying a return type. */
4379 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4381 (id, "Invalid method declaration, return type required");
4382 /* 8.6.3: Constructor modifiers */
4385 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4386 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4387 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4388 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4389 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4391 /* If we found error here, we don't consider it's OK to tread
4392 the method definition as a constructor, for the rest of this
4394 if (ec == java_error_count)
4398 /* Method declared within the scope of an interface are implicitly
4399 abstract and public. Conflicts with other erroneously provided
4400 modifiers are checked right after. */
4402 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4404 /* If FLAGS isn't set because of a modifier, turn the
4405 corresponding modifier WFL to NULL so we issue a warning on
4406 the obsolete use of the modifier */
4407 if (!(flags & ACC_PUBLIC))
4408 MODIFIER_WFL (PUBLIC_TK) = NULL;
4409 if (!(flags & ACC_ABSTRACT))
4410 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4411 flags |= ACC_PUBLIC;
4412 flags |= ACC_ABSTRACT;
4415 /* Inner class can't declare static methods */
4416 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4419 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4420 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4421 lang_printable_name (this_class, 0));
4424 /* Modifiers context reset moved up, so abstract method declaration
4425 modifiers can be later checked. */
4427 /* Set constructor returned type to void and method name to <init>,
4428 unless we found an error identifier the constructor (in which
4429 case we retain the original name) */
4432 type = void_type_node;
4434 meth_name = init_identifier_node;
4437 meth_name = EXPR_WFL_NODE (id);
4439 /* Do the returned type resolution and registration if necessary */
4440 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4443 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4444 EXPR_WFL_NODE (id) = meth_name;
4445 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4449 patch_stage = JDEP_METHOD_RETURN;
4450 register_incomplete_type (patch_stage, type_wfl, id, type);
4451 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4454 TREE_TYPE (meth) = type;
4456 saved_lineno = lineno;
4457 /* When defining an abstract or interface method, the curly
4458 bracket at level 1 doesn't exist because there is no function
4460 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4461 EXPR_WFL_LINENO (id));
4463 /* Remember the original argument list */
4464 orig_arg = TYPE_ARG_TYPES (meth);
4466 if (patch_stage) /* includes ret type and/or all args */
4469 meth = add_method_1 (this_class, flags, meth_name, meth);
4470 /* Patch for the return type */
4471 if (patch_stage == JDEP_METHOD_RETURN)
4473 jdep = CLASSD_LAST (ctxp->classd_list);
4474 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4476 /* This is the stop JDEP. METH allows the function's signature
4478 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4481 meth = add_method (this_class, flags, meth_name,
4482 build_java_signature (meth));
4484 /* Remember final parameters */
4485 MARK_FINAL_PARMS (meth, orig_arg);
4487 /* Fix the method argument list so we have the argument name
4489 fix_method_argument_names (orig_arg, meth);
4491 /* Register the parameter number and re-install the current line
4493 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4494 lineno = saved_lineno;
4496 /* Register exception specified by the `throws' keyword for
4497 resolution and set the method decl appropriate field to the list.
4498 Note: the grammar ensures that what we get here are class
4502 throws = nreverse (throws);
4503 for (current = throws; current; current = TREE_CHAIN (current))
4505 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4506 NULL_TREE, NULL_TREE);
4507 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4508 &TREE_VALUE (current);
4510 DECL_FUNCTION_THROWS (meth) = throws;
4513 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4514 DECL_FUNCTION_WFL (meth) = id;
4516 /* Set the flag if we correctly processed a constructor */
4519 DECL_CONSTRUCTOR_P (meth) = 1;
4520 /* Compute and store the number of artificial parameters declared
4521 for this constructor */
4522 for (count = 0, current = TYPE_FIELDS (this_class); current;
4523 current = TREE_CHAIN (current))
4524 if (FIELD_LOCAL_ALIAS (current))
4526 DECL_FUNCTION_NAP (meth) = count;
4529 /* Eventually set the @deprecated tag flag */
4530 CHECK_DEPRECATED (meth);
4532 /* If doing xref, store column and line number information instead
4533 of the line number only. */
4535 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4541 fix_method_argument_names (orig_arg, meth)
4542 tree orig_arg, meth;
4544 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4545 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4547 TREE_PURPOSE (arg) = this_identifier_node;
4548 arg = TREE_CHAIN (arg);
4550 while (orig_arg != end_params_node)
4552 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4553 orig_arg = TREE_CHAIN (orig_arg);
4554 arg = TREE_CHAIN (arg);
4558 /* Complete the method declaration with METHOD_BODY. */
4561 finish_method_declaration (method_body)
4566 if (!current_function_decl)
4569 flags = get_access_flags_from_decl (current_function_decl);
4571 /* 8.4.5 Method Body */
4572 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4574 tree name = DECL_NAME (current_function_decl);
4575 parse_error_context (DECL_FUNCTION_WFL (current_function_decl),
4576 "%s method `%s' can't have a body defined",
4577 (METHOD_NATIVE (current_function_decl) ?
4578 "Native" : "Abstract"),
4579 IDENTIFIER_POINTER (name));
4580 method_body = NULL_TREE;
4582 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4584 tree name = DECL_NAME (current_function_decl);
4586 (DECL_FUNCTION_WFL (current_function_decl),
4587 "Non native and non abstract method `%s' must have a body defined",
4588 IDENTIFIER_POINTER (name));
4589 method_body = NULL_TREE;
4592 if (flag_emit_class_files && method_body
4593 && TREE_CODE (method_body) == NOP_EXPR
4594 && TREE_TYPE (current_function_decl)
4595 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4596 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4598 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4599 maybe_absorb_scoping_blocks ();
4600 /* Exit function's body */
4602 /* Merge last line of the function with first line, directly in the
4603 function decl. It will be used to emit correct debug info. */
4604 if (!flag_emit_xref)
4605 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4607 /* Since function's argument's list are shared, reset the
4608 ARG_FINAL_P parameter that might have been set on some of this
4609 function parameters. */
4610 UNMARK_FINAL_PARMS (current_function_decl);
4612 /* So we don't have an irrelevant function declaration context for
4613 the next static block we'll see. */
4614 current_function_decl = NULL_TREE;
4617 /* Build a an error message for constructor circularity errors. */
4620 constructor_circularity_msg (from, to)
4623 static char string [4096];
4624 char *t = xstrdup (lang_printable_name (from, 0));
4625 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4630 /* Verify a circular call to METH. Return 1 if an error is found, 0
4634 verify_constructor_circularity (meth, current)
4637 static tree list = NULL_TREE;
4638 static int initialized_p;
4641 /* If we haven't already registered LIST with the garbage collector,
4645 ggc_add_tree_root (&list, 1);
4649 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4651 if (TREE_VALUE (c) == meth)
4657 list = nreverse (list);
4658 for (liste = list; liste; liste = TREE_CHAIN (liste))
4661 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4662 constructor_circularity_msg
4663 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4667 t = xstrdup (lang_printable_name (meth, 0));
4668 parse_error_context (TREE_PURPOSE (c),
4669 "%s: recursive invocation of constructor `%s'",
4670 constructor_circularity_msg (current, meth), t);
4676 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4678 list = tree_cons (c, current, list);
4679 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4681 list = TREE_CHAIN (list);
4686 /* Check modifiers that can be declared but exclusively */
4689 check_modifiers_consistency (flags)
4693 tree cl = NULL_TREE;
4695 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4696 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4697 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4700 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4704 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4705 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4707 parse_error_context (cl,
4708 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
4711 /* Check the methode header METH for abstract specifics features */
4714 check_abstract_method_header (meth)
4717 int flags = get_access_flags_from_decl (meth);
4719 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4720 ACC_ABSTRACT, "abstract method",
4721 IDENTIFIER_POINTER (DECL_NAME (meth)));
4722 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4723 ACC_PUBLIC, "abstract method",
4724 IDENTIFIER_POINTER (DECL_NAME (meth)));
4726 check_modifiers ("Illegal modifier `%s' for interface method",
4727 flags, INTERFACE_METHOD_MODIFIERS);
4730 /* Create a FUNCTION_TYPE node and start augmenting it with the
4731 declared function arguments. Arguments type that can't be resolved
4732 are left as they are, but the returned node is marked as containing
4733 incomplete types. */
4736 method_declarator (id, list)
4739 tree arg_types = NULL_TREE, current, node;
4740 tree meth = make_node (FUNCTION_TYPE);
4743 patch_stage = JDEP_NO_PATCH;
4745 if (GET_CPC () == error_mark_node)
4746 return error_mark_node;
4748 /* If we're dealing with an inner class constructor, we hide the
4749 this$<n> decl in the name field of its parameter declaration. We
4750 also might have to hide the outer context local alias
4751 initializers. Not done when the class is a toplevel class. */
4752 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4753 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4755 tree aliases_list, type, thisn;
4756 /* First the aliases, linked to the regular parameters */
4758 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4759 TREE_TYPE (GET_CPC ()),
4761 list = chainon (nreverse (aliases_list), list);
4764 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4765 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4766 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4770 for (current = list; current; current = TREE_CHAIN (current))
4773 tree wfl_name = TREE_PURPOSE (current);
4774 tree type = TREE_VALUE (current);
4775 tree name = EXPR_WFL_NODE (wfl_name);
4776 tree already, arg_node;
4777 tree type_wfl = NULL_TREE;
4780 /* Obtain a suitable type for resolution, if necessary */
4781 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4783 /* Process NAME, as it may specify extra dimension(s) for it */
4784 type = build_array_from_name (type, type_wfl, name, &name);
4785 EXPR_WFL_NODE (wfl_name) = name;
4787 real_type = GET_REAL_TYPE (type);
4788 if (TREE_CODE (real_type) == RECORD_TYPE)
4790 real_type = promote_type (real_type);
4791 if (TREE_CODE (type) == TREE_LIST)
4792 TREE_PURPOSE (type) = real_type;
4795 /* Check redefinition */
4796 for (already = arg_types; already; already = TREE_CHAIN (already))
4797 if (TREE_PURPOSE (already) == name)
4800 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4801 IDENTIFIER_POINTER (name),
4802 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4806 /* If we've an incomplete argument type, we know there is a location
4807 to patch when the type get resolved, later. */
4811 patch_stage = JDEP_METHOD;
4812 type = register_incomplete_type (patch_stage,
4813 type_wfl, wfl_name, type);
4814 jdep = CLASSD_LAST (ctxp->classd_list);
4815 JDEP_MISC (jdep) = id;
4818 /* The argument node: a name and a (possibly) incomplete type. */
4819 arg_node = build_tree_list (name, real_type);
4820 /* Remeber arguments declared final. */
4821 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4824 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4825 TREE_CHAIN (arg_node) = arg_types;
4826 arg_types = arg_node;
4828 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
4829 node = build_tree_list (id, meth);
4834 unresolved_type_p (wfl, returned)
4839 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4843 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4844 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4845 *returned = TREE_TYPE (decl);
4846 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4847 *returned = TREE_TYPE (GET_CPC ());
4849 *returned = NULL_TREE;
4858 /* From NAME, build a qualified identifier node using the
4859 qualification from the current package definition. */
4862 parser_qualified_classname (name)
4865 tree nested_class_name;
4867 if ((nested_class_name = maybe_make_nested_class_name (name)))
4868 return nested_class_name;
4871 return merge_qualified_name (ctxp->package, name);
4876 /* Called once the type a interface extends is resolved. Returns 0 if
4877 everything is OK. */
4880 parser_check_super_interface (super_decl, this_decl, this_wfl)
4881 tree super_decl, this_decl, this_wfl;
4883 tree super_type = TREE_TYPE (super_decl);
4885 /* Has to be an interface */
4886 if (!CLASS_INTERFACE (super_decl))
4889 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4890 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4891 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4892 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4893 "interface" : "class"),
4894 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4898 /* Check scope: same package OK, other package: OK if public */
4899 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4902 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4903 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4904 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4908 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4909 0 if everthing is OK. */
4912 parser_check_super (super_decl, this_decl, wfl)
4913 tree super_decl, this_decl, wfl;
4915 tree super_type = TREE_TYPE (super_decl);
4917 /* SUPER should be a CLASS (neither an array nor an interface) */
4918 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4921 (wfl, "Class `%s' can't subclass %s `%s'",
4922 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4923 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4924 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4928 if (CLASS_FINAL (TYPE_NAME (super_type)))
4930 parse_error_context (wfl, "Can't subclass final classes: %s",
4931 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4935 /* Check scope: same package OK, other package: OK if public */
4936 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4939 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4940 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4941 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4945 /* Create a new dependency list and link it (in a LIFO manner) to the
4946 CTXP list of type dependency list. */
4949 create_jdep_list (ctxp)
4950 struct parser_ctxt *ctxp;
4952 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
4953 new->first = new->last = NULL;
4954 new->next = ctxp->classd_list;
4955 ctxp->classd_list = new;
4959 reverse_jdep_list (ctxp)
4960 struct parser_ctxt *ctxp;
4962 register jdeplist *prev = NULL, *current, *next;
4963 for (current = ctxp->classd_list; current; current = next)
4965 next = current->next;
4966 current->next = prev;
4972 /* Create a fake pointer based on the ID stored in
4973 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4974 registered again. */
4977 obtain_incomplete_type (type_name)
4982 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4983 name = EXPR_WFL_NODE (type_name);
4984 else if (INCOMPLETE_TYPE_P (type_name))
4985 name = TYPE_NAME (type_name);
4989 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
4990 if (TYPE_NAME (ptr) == name)
4995 BUILD_PTR_FROM_NAME (ptr, name);
4997 TREE_CHAIN (ptr) = ctxp->incomplete_class;
4998 ctxp->incomplete_class = ptr;
5004 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
5005 non NULL instead of computing a new fake type based on WFL. The new
5006 dependency is inserted in the current type dependency list, in FIFO
5010 register_incomplete_type (kind, wfl, decl, ptr)
5012 tree wfl, decl, ptr;
5014 jdep *new = (jdep *)xmalloc (sizeof (jdep));
5016 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
5017 ptr = obtain_incomplete_type (wfl);
5019 JDEP_KIND (new) = kind;
5020 JDEP_DECL (new) = decl;
5021 JDEP_SOLV (new) = ptr;
5022 JDEP_WFL (new) = wfl;
5023 JDEP_CHAIN (new) = NULL;
5024 JDEP_MISC (new) = NULL_TREE;
5025 /* For some dependencies, set the enclosing class of the current
5026 class to be the enclosing context */
5027 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE
5028 || kind == JDEP_ANONYMOUS || kind == JDEP_FIELD)
5029 && GET_ENCLOSING_CPC ())
5030 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
5032 JDEP_ENCLOSING (new) = GET_CPC ();
5033 JDEP_GET_PATCH (new) = (tree *)NULL;
5035 JDEP_INSERT (ctxp->classd_list, new);
5041 java_check_circular_reference ()
5044 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5046 tree type = TREE_TYPE (current);
5047 if (CLASS_INTERFACE (current))
5049 /* Check all interfaces this class extends */
5050 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5055 n = TREE_VEC_LENGTH (basetype_vec);
5056 for (i = 0; i < n; i++)
5058 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5059 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5060 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5061 parse_error_context (lookup_cl (current),
5062 "Cyclic interface inheritance");
5066 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5067 parse_error_context (lookup_cl (current),
5068 "Cyclic class inheritance%s",
5069 (cyclic_inheritance_report ?
5070 cyclic_inheritance_report : ""));
5074 /* Augment the parameter list PARM with parameters crafted to
5075 initialize outer context locals aliases. Through ARTIFICIAL, a
5076 count is kept of the number of crafted parameters. MODE governs
5077 what eventually gets created: something suitable for a function
5078 creation or a function invocation, either the constructor or
5082 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5084 tree class_type, parm;
5088 tree additional_parms = NULL_TREE;
5090 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5091 if (FIELD_LOCAL_ALIAS (field))
5093 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5094 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5099 case AIPL_FUNCTION_DECLARATION:
5100 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5102 purpose = build_wfl_node (mangled_id);
5103 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5104 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5106 value = TREE_TYPE (field);
5109 case AIPL_FUNCTION_CREATION:
5110 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5112 value = TREE_TYPE (field);
5115 case AIPL_FUNCTION_FINIT_INVOCATION:
5116 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5118 /* Now, this is wrong. purpose should always be the NAME
5119 of something and value its matching value (decl, type,
5120 etc...) FIXME -- but there is a lot to fix. */
5122 /* When invoked for this kind of operation, we already
5123 know whether a field is used or not. */
5124 purpose = TREE_TYPE (field);
5125 value = build_wfl_node (mangled_id);
5128 case AIPL_FUNCTION_CTOR_INVOCATION:
5129 /* There are two case: the constructor invokation happends
5130 outside the local inner, in which case, locales from the outer
5131 context are directly used.
5133 Otherwise, we fold to using the alias directly. */
5134 if (class_type == current_class)
5138 name = get_identifier (&buffer[4]);
5139 value = IDENTIFIER_LOCAL_VALUE (name);
5143 additional_parms = tree_cons (purpose, value, additional_parms);
5147 if (additional_parms)
5149 if (ANONYMOUS_CLASS_P (class_type)
5150 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5151 additional_parms = nreverse (additional_parms);
5152 parm = chainon (additional_parms, parm);
5158 /* Craft a constructor for CLASS_DECL -- what we should do when none
5159 where found. ARGS is non NULL when a special signature must be
5160 enforced. This is the case for anonymous classes. */
5163 craft_constructor (class_decl, args)
5164 tree class_decl, args;
5166 tree class_type = TREE_TYPE (class_decl);
5167 tree parm = NULL_TREE;
5168 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5170 int i = 0, artificial = 0;
5171 tree decl, ctor_name;
5174 /* The constructor name is <init> unless we're dealing with an
5175 anonymous class, in which case the name will be fixed after having
5177 if (ANONYMOUS_CLASS_P (class_type))
5178 ctor_name = DECL_NAME (class_decl);
5180 ctor_name = init_identifier_node;
5182 /* If we're dealing with an inner class constructor, we hide the
5183 this$<n> decl in the name field of its parameter declaration. */
5184 if (PURE_INNER_CLASS_TYPE_P (class_type))
5186 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5187 parm = tree_cons (build_current_thisn (class_type),
5188 build_pointer_type (type), parm);
5190 /* Some more arguments to be hidden here. The values of the local
5191 variables of the outer context that the inner class needs to see. */
5192 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5197 /* Then if there are any args to be enforced, enforce them now */
5198 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5200 sprintf (buffer, "parm%d", i++);
5201 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5204 CRAFTED_PARAM_LIST_FIXUP (parm);
5205 decl = create_artificial_method (class_type, flags, void_type_node,
5207 fix_method_argument_names (parm, decl);
5208 /* Now, mark the artificial parameters. */
5209 DECL_FUNCTION_NAP (decl) = artificial;
5210 DECL_FUNCTION_SYNTHETIC_CTOR (decl) = DECL_CONSTRUCTOR_P (decl) = 1;
5214 /* Fix the constructors. This will be called right after circular
5215 references have been checked. It is necessary to fix constructors
5216 early even if no code generation will take place for that class:
5217 some generated constructor might be required by the class whose
5218 compilation triggered this one to be simply loaded. */
5221 java_fix_constructors ()
5225 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5227 tree class_type = TREE_TYPE (current);
5231 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5234 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5236 if (DECL_CONSTRUCTOR_P (decl))
5238 fix_constructors (decl);
5243 /* Anonymous class constructor can't be generated that early. */
5244 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5245 craft_constructor (current, NULL_TREE);
5249 /* safe_layout_class just makes sure that we can load a class without
5250 disrupting the current_class, input_file, lineno, etc, information
5251 about the class processed currently. */
5254 safe_layout_class (class)
5257 tree save_current_class = current_class;
5258 const char *save_input_filename = input_filename;
5259 int save_lineno = lineno;
5261 layout_class (class);
5263 current_class = save_current_class;
5264 input_filename = save_input_filename;
5265 lineno = save_lineno;
5266 CLASS_LOADED_P (class) = 1;
5270 jdep_resolve_class (dep)
5275 if (JDEP_RESOLVED_P (dep))
5276 decl = JDEP_RESOLVED_DECL (dep);
5279 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5280 JDEP_DECL (dep), JDEP_WFL (dep));
5281 JDEP_RESOLVED (dep, decl);
5285 complete_class_report_errors (dep);
5286 else if (PURE_INNER_CLASS_DECL_P (decl))
5287 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5291 /* Complete unsatisfied class declaration and their dependencies */
5294 java_complete_class ()
5301 /* Process imports */
5304 /* Rever things so we have the right order */
5305 ctxp->class_list = nreverse (ctxp->class_list);
5306 ctxp->classd_list = reverse_jdep_list (ctxp);
5308 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5310 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5313 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5316 if (!(decl = jdep_resolve_class (dep)))
5319 /* Now it's time to patch */
5320 switch (JDEP_KIND (dep))
5323 /* Simply patch super */
5324 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5326 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5327 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5332 /* We do part of the job done in add_field */
5333 tree field_decl = JDEP_DECL (dep);
5334 tree field_type = TREE_TYPE (decl);
5335 if (TREE_CODE (field_type) == RECORD_TYPE)
5336 field_type = promote_type (field_type);
5337 TREE_TYPE (field_decl) = field_type;
5338 DECL_ALIGN (field_decl) = 0;
5339 DECL_USER_ALIGN (field_decl) = 0;
5340 layout_decl (field_decl, 0);
5341 SOURCE_FRONTEND_DEBUG
5342 (("Completed field/var decl `%s' with `%s'",
5343 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5344 IDENTIFIER_POINTER (DECL_NAME (decl))));
5347 case JDEP_METHOD: /* We start patching a method */
5348 case JDEP_METHOD_RETURN:
5354 type = TREE_TYPE(decl);
5355 if (TREE_CODE (type) == RECORD_TYPE)
5356 type = promote_type (type);
5357 JDEP_APPLY_PATCH (dep, type);
5358 SOURCE_FRONTEND_DEBUG
5359 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5360 "Completing fct `%s' with ret type `%s'":
5361 "Completing arg `%s' with type `%s'"),
5362 IDENTIFIER_POINTER (EXPR_WFL_NODE
5363 (JDEP_DECL_WFL (dep))),
5364 IDENTIFIER_POINTER (DECL_NAME (decl))));
5368 dep = JDEP_CHAIN (dep);
5369 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5372 decl = jdep_resolve_class (dep);
5376 tree mdecl = JDEP_DECL (dep), signature;
5377 /* Recompute and reset the signature, check first that
5378 all types are now defined. If they're not,
5379 dont build the signature. */
5380 if (check_method_types_complete (mdecl))
5382 signature = build_java_signature (TREE_TYPE (mdecl));
5383 set_java_signature (TREE_TYPE (mdecl), signature);
5390 case JDEP_INTERFACE:
5391 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5394 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5399 type = TREE_TYPE(decl);
5400 if (TREE_CODE (type) == RECORD_TYPE)
5401 type = promote_type (type);
5402 JDEP_APPLY_PATCH (dep, type);
5406 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5407 SOURCE_FRONTEND_DEBUG
5408 (("Completing a random type dependency on a '%s' node",
5409 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5412 case JDEP_EXCEPTION:
5413 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5414 SOURCE_FRONTEND_DEBUG
5415 (("Completing `%s' `throws' argument node",
5416 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5419 case JDEP_ANONYMOUS:
5420 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5431 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5435 resolve_class (enclosing, class_type, decl, cl)
5436 tree enclosing, class_type, decl, cl;
5438 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5439 const char *base = name;
5440 tree resolved_type = TREE_TYPE (class_type);
5441 tree resolved_type_decl;
5443 if (resolved_type != NULL_TREE)
5445 tree resolved_type_decl = TYPE_NAME (resolved_type);
5446 if (resolved_type_decl == NULL_TREE
5447 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5449 resolved_type_decl = build_decl (TYPE_DECL,
5450 TYPE_NAME (class_type),
5453 return resolved_type_decl;
5456 /* 1- Check to see if we have an array. If true, find what we really
5458 while (name[0] == '[')
5462 TYPE_NAME (class_type) = get_identifier (name);
5463 WFL_STRIP_BRACKET (cl, cl);
5466 /* 2- Resolve the bare type */
5467 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5470 resolved_type = TREE_TYPE (resolved_type_decl);
5472 /* 3- If we have and array, reconstruct the array down to its nesting */
5475 while (base != name)
5477 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5478 resolved_type = promote_type (resolved_type);
5479 resolved_type = build_java_array_type (resolved_type, -1);
5480 CLASS_LOADED_P (resolved_type) = 1;
5483 /* A TYPE_NAME that is a TYPE_DECL was set in
5484 build_java_array_type, return it. */
5485 resolved_type_decl = TYPE_NAME (resolved_type);
5487 TREE_TYPE (class_type) = resolved_type;
5488 return resolved_type_decl;
5491 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5492 are used to report error messages. */
5495 do_resolve_class (enclosing, class_type, decl, cl)
5496 tree enclosing, class_type, decl, cl;
5498 tree new_class_decl;
5500 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
5501 it is changed by find_in_imports{_on_demand} and (but it doesn't
5502 really matter) qualify_and_find */
5504 /* 0- Search in the current class as an inner class */
5506 /* Maybe some code here should be added to load the class or
5507 something, at least if the class isn't an inner class and ended
5508 being loaded from class file. FIXME. */
5514 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5515 return new_class_decl;
5517 intermediate = enclosing;
5518 /* Explore enclosing contexts. */
5519 while (INNER_CLASS_DECL_P (intermediate))
5521 intermediate = DECL_CONTEXT (intermediate);
5522 if ((new_class_decl = find_as_inner_class (intermediate,
5524 return new_class_decl;
5527 /* Now go to the upper classes, bail out if necessary. */
5528 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5529 if (!enclosing || enclosing == object_type_node)
5532 if (TREE_CODE (enclosing) == RECORD_TYPE)
5534 enclosing = TYPE_NAME (enclosing);
5538 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
5539 BUILD_PTR_FROM_NAME (name, enclosing);
5542 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5545 /* 1- Check for the type in single imports. This will change
5546 TYPE_NAME() if something relevant is found */
5547 find_in_imports (class_type);
5549 /* 2- And check for the type in the current compilation unit */
5550 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5552 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5553 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5554 load_class (TYPE_NAME (class_type), 0);
5555 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5558 /* 3- Search according to the current package definition */
5559 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5561 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5562 TYPE_NAME (class_type))))
5563 return new_class_decl;
5566 /* 4- Check the import on demands. Don't allow bar.baz to be
5567 imported from foo.* */
5568 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5569 if (find_in_imports_on_demand (class_type))
5572 /* If found in find_in_imports_on_demant, the type has already been
5574 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5575 return new_class_decl;
5577 /* 5- Try with a name qualified with the package name we've seen so far */
5578 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5582 /* If there is a current package (ctxp->package), it's the first
5583 element of package_list and we can skip it. */
5584 for (package = (ctxp->package ?
5585 TREE_CHAIN (package_list) : package_list);
5586 package; package = TREE_CHAIN (package))
5587 if ((new_class_decl = qualify_and_find (class_type,
5588 TREE_PURPOSE (package),
5589 TYPE_NAME (class_type))))
5590 return new_class_decl;
5593 /* 5- Check an other compilation unit that bears the name of type */
5594 load_class (TYPE_NAME (class_type), 0);
5595 if (check_pkg_class_access (TYPE_NAME (class_type),
5596 (cl ? cl : lookup_cl (decl))))
5599 /* 6- Last call for a resolution */
5600 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5604 qualify_and_find (class_type, package, name)
5605 tree class_type, package, name;
5607 tree new_qualified = merge_qualified_name (package, name);
5608 tree new_class_decl;
5610 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5611 load_class (new_qualified, 0);
5612 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5614 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5615 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5616 load_class (new_qualified, 0);
5617 TYPE_NAME (class_type) = new_qualified;
5618 return IDENTIFIER_CLASS_VALUE (new_qualified);
5623 /* Resolve NAME and lay it out (if not done and if not the current
5624 parsed class). Return a decl node. This function is meant to be
5625 called when type resolution is necessary during the walk pass. */
5628 resolve_and_layout (something, cl)
5632 tree decl, decl_type;
5634 /* Don't do that on the current class */
5635 if (something == current_class)
5636 return TYPE_NAME (current_class);
5638 /* Don't do anything for void and other primitive types */
5639 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5642 /* Pointer types can be reall pointer types or fake pointers. When
5643 finding a real pointer, recheck for primitive types */
5644 if (TREE_CODE (something) == POINTER_TYPE)
5646 if (TREE_TYPE (something))
5648 something = TREE_TYPE (something);
5649 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5653 something = TYPE_NAME (something);
5656 /* Don't do anything for arrays of primitive types */
5657 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5658 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5661 /* Something might be a WFL */
5662 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5663 something = EXPR_WFL_NODE (something);
5665 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5666 TYPE_DECL or a real TYPE */
5667 else if (TREE_CODE (something) != IDENTIFIER_NODE)
5668 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5669 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5671 if (!(decl = resolve_no_layout (something, cl)))
5674 /* Resolve and layout if necessary */
5675 decl_type = TREE_TYPE (decl);
5676 layout_class_methods (decl_type);
5678 if (CLASS_FROM_SOURCE_P (decl_type))
5679 java_check_methods (decl);
5680 /* Layout the type if necessary */
5681 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5682 safe_layout_class (decl_type);
5687 /* Resolve a class, returns its decl but doesn't perform any
5688 layout. The current parsing context is saved and restored */
5691 resolve_no_layout (name, cl)
5695 BUILD_PTR_FROM_NAME (ptr, name);
5696 java_parser_context_save_global ();
5697 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5698 java_parser_context_restore_global ();
5703 /* Called when reporting errors. Skip leader '[' in a complex array
5704 type description that failed to be resolved. */
5707 purify_type_name (name)
5710 while (*name && *name == '[')
5715 /* The type CURRENT refers to can't be found. We print error messages. */
5718 complete_class_report_errors (dep)
5723 if (!JDEP_WFL (dep))
5726 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
5727 switch (JDEP_KIND (dep))
5731 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
5732 purify_type_name (name),
5733 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5737 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
5738 purify_type_name (name),
5739 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5741 case JDEP_METHOD: /* Covers arguments */
5743 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
5744 purify_type_name (name),
5745 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5746 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5748 case JDEP_METHOD_RETURN: /* Covers return type */
5750 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
5751 purify_type_name (name),
5752 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5754 case JDEP_INTERFACE:
5756 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5757 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5758 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5759 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5763 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
5764 purify_type_name (IDENTIFIER_POINTER
5765 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
5766 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5768 case JDEP_EXCEPTION: /* As specified by `throws' */
5770 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5771 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5774 /* Fix for -Wall. Just break doing nothing. The error will be
5780 /* Return a static string containing the DECL prototype string. If
5781 DECL is a constructor, use the class name instead of the form
5785 get_printable_method_name (decl)
5788 const char *to_return;
5789 tree name = NULL_TREE;
5791 if (DECL_CONSTRUCTOR_P (decl))
5793 name = DECL_NAME (decl);
5794 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
5797 to_return = lang_printable_name (decl, 0);
5798 if (DECL_CONSTRUCTOR_P (decl))
5799 DECL_NAME (decl) = name;
5804 /* Track method being redefined inside the same class. As a side
5805 effect, set DECL_NAME to an IDENTIFIER (prior entering this
5806 function it's a FWL, so we can track errors more accurately.) */
5809 check_method_redefinition (class, method)
5814 /* There's no need to verify <clinit> and finit$ */
5815 if (DECL_CLINIT_P (method) || DECL_FINIT_P (method))
5818 sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
5819 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5821 if (redef == method)
5823 if (DECL_NAME (redef) == DECL_NAME (method)
5824 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef))
5825 && !DECL_ARTIFICIAL (method))
5828 (DECL_FUNCTION_WFL (method), "Duplicate %s declaration `%s'",
5829 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5830 get_printable_method_name (redef));
5837 /* Return 1 if check went ok, 0 otherwise. */
5839 check_abstract_method_definitions (do_interface, class_decl, type)
5841 tree class_decl, type;
5843 tree class = TREE_TYPE (class_decl);
5844 tree method, end_type;
5847 end_type = (do_interface ? object_type_node : type);
5848 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5850 tree other_super, other_method, method_sig, method_name;
5852 int end_type_reached = 0;
5854 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5857 /* Now verify that somewhere in between TYPE and CLASS,
5858 abstract method METHOD gets a non abstract definition
5859 that is inherited by CLASS. */
5861 method_sig = build_java_signature (TREE_TYPE (method));
5862 method_name = DECL_NAME (method);
5863 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5864 method_name = EXPR_WFL_NODE (method_name);
5866 other_super = class;
5868 if (other_super == end_type)
5869 end_type_reached = 1;
5872 for (other_method = TYPE_METHODS (other_super); other_method;
5873 other_method = TREE_CHAIN (other_method))
5875 tree s = build_java_signature (TREE_TYPE (other_method));
5876 tree other_name = DECL_NAME (other_method);
5878 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5879 other_name = EXPR_WFL_NODE (other_name);
5880 if (!DECL_CLINIT_P (other_method)
5881 && !DECL_CONSTRUCTOR_P (other_method)
5882 && method_name == other_name
5884 && !METHOD_ABSTRACT (other_method))
5890 other_super = CLASSTYPE_SUPER (other_super);
5891 } while (!end_type_reached);
5893 /* Report that abstract METHOD didn't find an implementation
5894 that CLASS can use. */
5897 char *t = xstrdup (lang_printable_name
5898 (TREE_TYPE (TREE_TYPE (method)), 0));
5899 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5902 (lookup_cl (class_decl),
5903 "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",
5904 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5905 t, lang_printable_name (method, 0),
5906 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5907 "interface" : "class"),
5908 IDENTIFIER_POINTER (ccn),
5909 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5910 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
5916 if (ok && do_interface)
5918 /* Check for implemented interfaces. */
5920 tree vector = TYPE_BINFO_BASETYPES (type);
5921 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5923 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5924 ok = check_abstract_method_definitions (1, class_decl, super);
5931 /* Check that CLASS_DECL somehow implements all inherited abstract
5935 java_check_abstract_method_definitions (class_decl)
5938 tree class = TREE_TYPE (class_decl);
5942 if (CLASS_ABSTRACT (class_decl))
5945 /* Check for inherited types */
5948 super = CLASSTYPE_SUPER (super);
5949 check_abstract_method_definitions (0, class_decl, super);
5950 } while (super != object_type_node);
5952 /* Check for implemented interfaces. */
5953 vector = TYPE_BINFO_BASETYPES (class);
5954 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5956 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5957 check_abstract_method_definitions (1, class_decl, super);
5961 /* Check all the types method DECL uses and return 1 if all of them
5962 are now complete, 0 otherwise. This is used to check whether its
5963 safe to build a method signature or not. */
5966 check_method_types_complete (decl)
5969 tree type = TREE_TYPE (decl);
5972 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
5975 args = TYPE_ARG_TYPES (type);
5976 if (TREE_CODE (type) == METHOD_TYPE)
5977 args = TREE_CHAIN (args);
5978 for (; args != end_params_node; args = TREE_CHAIN (args))
5979 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
5985 /* Visible interface to check methods contained in CLASS_DECL */
5988 java_check_methods (class_decl)
5991 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
5994 if (CLASS_INTERFACE (class_decl))
5995 java_check_abstract_methods (class_decl);
5997 java_check_regular_methods (class_decl);
5999 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6002 /* Check all the methods of CLASS_DECL. Methods are first completed
6003 then checked according to regular method existance rules. If no
6004 constructor for CLASS_DECL were encountered, then build its
6008 java_check_regular_methods (class_decl)
6011 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6013 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
6014 tree found = NULL_TREE;
6017 /* It is not necessary to check methods defined in java.lang.Object */
6018 if (class == object_type_node)
6021 if (!TYPE_NVIRTUALS (class))
6022 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6024 /* Should take interfaces into account. FIXME */
6025 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6028 tree method_wfl = DECL_FUNCTION_WFL (method);
6031 /* Check for redefinitions */
6032 if (check_method_redefinition (class, method))
6035 /* If we see one constructor a mark so we don't generate the
6036 default one. Also skip other verifications: constructors
6037 can't be inherited hence hiden or overriden */
6038 if (DECL_CONSTRUCTOR_P (method))
6040 saw_constructor = 1;
6044 /* We verify things thrown by the method. They must inherits from
6045 java.lang.Throwable */
6046 for (mthrows = DECL_FUNCTION_THROWS (method);
6047 mthrows; mthrows = TREE_CHAIN (mthrows))
6049 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6051 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6053 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6056 sig = build_java_argument_signature (TREE_TYPE (method));
6057 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6059 /* Inner class can't declare static methods */
6060 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6062 char *t = xstrdup (lang_printable_name (class, 0));
6064 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6065 lang_printable_name (method, 0), t);
6069 /* Nothing overrides or it's a private method. */
6072 if (METHOD_PRIVATE (found))
6078 /* If `found' is declared in an interface, make sure the
6079 modifier matches. */
6080 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6081 && clinit_identifier_node != DECL_NAME (found)
6082 && !METHOD_PUBLIC (method))
6084 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6085 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6086 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6087 lang_printable_name (method, 0),
6088 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6091 /* Can't override a method with the same name and different return
6093 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6096 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6099 "Method `%s' was defined with return type `%s' in class `%s'",
6100 lang_printable_name (found, 0), t,
6102 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6106 aflags = get_access_flags_from_decl (found);
6108 /* Can't override final. Can't override static. */
6109 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6111 /* Static *can* override static */
6112 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6116 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6117 (METHOD_FINAL (found) ? "Final" : "Static"),
6118 lang_printable_name (found, 0),
6119 (METHOD_FINAL (found) ? "final" : "static"),
6121 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6125 /* Static method can't override instance method. */
6126 if (METHOD_STATIC (method))
6130 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6131 lang_printable_name (found, 0),
6133 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6137 /* - Overriding/hiding public must be public
6138 - Overriding/hiding protected must be protected or public
6139 - If the overriden or hidden method has default (package)
6140 access, then the overriding or hiding method must not be
6141 private; otherwise, a compile-time error occurs. If
6142 `found' belongs to an interface, things have been already
6144 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6145 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6146 || (METHOD_PROTECTED (found)
6147 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6148 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6149 && METHOD_PRIVATE (method))))
6153 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6154 (METHOD_PUBLIC (method) ? "public" :
6155 (METHOD_PRIVATE (method) ? "private" : "protected")),
6156 IDENTIFIER_POINTER (DECL_NAME
6157 (TYPE_NAME (DECL_CONTEXT (found)))));
6161 /* Overriding methods must have compatible `throws' clauses on checked
6162 exceptions, if any */
6163 check_throws_clauses (method, method_wfl, found);
6165 /* Inheriting multiple methods with the same signature. FIXME */
6168 if (!TYPE_NVIRTUALS (class))
6169 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6171 /* Search for inherited abstract method not yet implemented in this
6173 java_check_abstract_method_definitions (class_decl);
6175 if (!saw_constructor)
6179 /* Return a non zero value if the `throws' clause of METHOD (if any)
6180 is incompatible with the `throws' clause of FOUND (if any). */
6183 check_throws_clauses (method, method_wfl, found)
6184 tree method, method_wfl, found;
6186 tree mthrows, fthrows;
6188 /* Can't check these things with class loaded from bytecode. FIXME */
6189 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6192 for (mthrows = DECL_FUNCTION_THROWS (method);
6193 mthrows; mthrows = TREE_CHAIN (mthrows))
6195 /* We don't verify unchecked expressions */
6196 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6198 /* Checked expression must be compatible */
6199 for (fthrows = DECL_FUNCTION_THROWS (found);
6200 fthrows; fthrows = TREE_CHAIN (fthrows))
6201 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6206 (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'",
6207 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6208 lang_printable_name (found, 0),
6210 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6215 /* Check abstract method of interface INTERFACE */
6218 java_check_abstract_methods (interface_decl)
6219 tree interface_decl;
6222 tree method, basetype_vec, found;
6223 tree interface = TREE_TYPE (interface_decl);
6225 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6227 /* 2- Check for double definition inside the defining interface */
6228 if (check_method_redefinition (interface, method))
6231 /* 3- Overriding is OK as far as we preserve the return type and
6232 the thrown exceptions (FIXME) */
6233 found = lookup_java_interface_method2 (interface, method);
6237 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6239 (DECL_FUNCTION_WFL (found),
6240 "Method `%s' was defined with return type `%s' in class `%s'",
6241 lang_printable_name (found, 0), t,
6243 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6249 /* 4- Inherited methods can't differ by their returned types */
6250 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6252 n = TREE_VEC_LENGTH (basetype_vec);
6253 for (i = 0; i < n; i++)
6255 tree sub_interface_method, sub_interface;
6256 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6259 sub_interface = BINFO_TYPE (vec_elt);
6260 for (sub_interface_method = TYPE_METHODS (sub_interface);
6261 sub_interface_method;
6262 sub_interface_method = TREE_CHAIN (sub_interface_method))
6264 found = lookup_java_interface_method2 (interface,
6265 sub_interface_method);
6266 if (found && (found != sub_interface_method))
6269 (lookup_cl (sub_interface_method),
6270 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6271 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6272 lang_printable_name (found, 0),
6274 (DECL_NAME (TYPE_NAME
6275 (DECL_CONTEXT (sub_interface_method)))),
6277 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6283 /* Lookup methods in interfaces using their name and partial
6284 signature. Return a matching method only if their types differ. */
6287 lookup_java_interface_method2 (class, method_decl)
6288 tree class, method_decl;
6291 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6296 n = TREE_VEC_LENGTH (basetype_vec);
6297 for (i = 0; i < n; i++)
6299 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6300 if ((BINFO_TYPE (vec_elt) != object_type_node)
6302 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6305 for (i = 0; i < n; i++)
6307 to_return = lookup_java_interface_method2
6308 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6316 /* Lookup method using their name and partial signature. Return a
6317 matching method only if their types differ. */
6320 lookup_java_method2 (clas, method_decl, do_interface)
6321 tree clas, method_decl;
6324 tree method, method_signature, method_name, method_type, name;
6326 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6327 name = DECL_NAME (method_decl);
6328 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6329 EXPR_WFL_NODE (name) : name);
6330 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6332 while (clas != NULL_TREE)
6334 for (method = TYPE_METHODS (clas);
6335 method != NULL_TREE; method = TREE_CHAIN (method))
6337 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6338 tree name = DECL_NAME (method);
6339 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6340 EXPR_WFL_NODE (name) : name) == method_name
6341 && method_sig == method_signature
6342 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6345 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6350 /* Return the line that matches DECL line number, and try its best to
6351 position the column number. Used during error reports. */
6357 static tree cl = NULL_TREE;
6363 if (cl == NULL_TREE)
6365 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6366 ggc_add_tree_root (&cl, 1);
6369 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6370 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6372 line = java_get_line_col (EXPR_WFL_FILENAME (cl),
6373 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6375 found = strstr ((const char *)line,
6376 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6378 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6383 /* Look for a simple name in the single-type import list */
6386 find_name_in_single_imports (name)
6391 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6392 if (TREE_VALUE (node) == name)
6393 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6398 /* Process all single-type import. */
6406 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6408 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6409 char *original_name;
6411 obstack_grow0 (&temporary_obstack,
6412 IDENTIFIER_POINTER (to_be_found),
6413 IDENTIFIER_LENGTH (to_be_found));
6414 original_name = obstack_finish (&temporary_obstack);
6416 /* Don't load twice something already defined. */
6417 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6424 QUALIFIED_P (to_be_found) = 1;
6425 load_class (to_be_found, 0);
6427 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6429 /* We found it, we can bail out */
6430 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6433 /* We haven't found it. Maybe we're trying to access an
6434 inner class. The only way for us to know is to try again
6435 after having dropped a qualifier. If we can't break it further,
6436 we have an error. */
6437 if (breakdown_qualified (&left, NULL, to_be_found))
6442 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6444 parse_error_context (TREE_PURPOSE (import),
6445 "Class or interface `%s' not found in import",
6450 obstack_free (&temporary_obstack, original_name);
6457 /* Possibly find and mark a class imported by a single-type import
6461 find_in_imports (class_type)
6466 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6467 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6469 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6470 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6475 note_possible_classname (name, len)
6480 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6482 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6486 node = ident_subst (name, len, "", '/', '.', "");
6487 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6488 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6492 /* Read a import directory, gathering potential match for further type
6493 references. Indifferently reads a filesystem or a ZIP archive
6497 read_import_dir (wfl)
6500 tree package_id = EXPR_WFL_NODE (wfl);
6501 const char *package_name = IDENTIFIER_POINTER (package_id);
6502 int package_length = IDENTIFIER_LENGTH (package_id);
6504 JCF *saved_jcf = current_jcf;
6509 struct buffer filename[1];
6512 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6514 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6516 BUFFER_INIT (filename);
6517 buffer_grow (filename, package_length + 100);
6519 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6521 const char *entry_name = jcf_path_name (entry);
6522 int entry_length = strlen (entry_name);
6523 if (jcf_path_is_zipfile (entry))
6526 buffer_grow (filename, entry_length);
6527 memcpy (filename->data, entry_name, entry_length - 1);
6528 filename->data[entry_length-1] = '\0';
6529 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6531 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6534 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6535 BUFFER_RESET (filename);
6536 for (k = 0; k < package_length; k++)
6538 char ch = package_name[k];
6539 *filename->ptr++ = ch == '.' ? '/' : ch;
6541 *filename->ptr++ = '/';
6543 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
6545 const char *current_entry = ZIPDIR_FILENAME (zipd);
6546 int current_entry_len = zipd->filename_length;
6548 if (current_entry_len >= BUFFER_LENGTH (filename)
6549 && strncmp (filename->data, current_entry,
6550 BUFFER_LENGTH (filename)) != 0)
6552 found |= note_possible_classname (current_entry,
6559 BUFFER_RESET (filename);
6560 buffer_grow (filename, entry_length + package_length + 4);
6561 strcpy (filename->data, entry_name);
6562 filename->ptr = filename->data + entry_length;
6563 for (k = 0; k < package_length; k++)
6565 char ch = package_name[k];
6566 *filename->ptr++ = ch == '.' ? '/' : ch;
6568 *filename->ptr = '\0';
6570 dirp = opendir (filename->data);
6573 *filename->ptr++ = '/';
6578 struct dirent *direntp = readdir (dirp);
6581 d_name = direntp->d_name;
6582 len = strlen (direntp->d_name);
6583 buffer_grow (filename, len+1);
6584 strcpy (filename->ptr, d_name);
6585 found |= note_possible_classname (filename->data + entry_length,
6586 package_length+len+1);
6593 free (filename->data);
6595 /* Here we should have a unified way of retrieving an entry, to be
6599 static int first = 1;
6602 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
6607 parse_error_context (wfl, "Package `%s' not found in import",
6609 current_jcf = saved_jcf;
6612 current_jcf = saved_jcf;
6615 /* Possibly find a type in the import on demands specified
6616 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6617 entire list, to detected potential double definitions. */
6620 find_in_imports_on_demand (class_type)
6623 tree node, import, node_to_use = NULL_TREE;
6625 tree cl = NULL_TREE;
6627 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6629 const char *id_name;
6630 obstack_grow (&temporary_obstack,
6631 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6632 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6633 obstack_1grow (&temporary_obstack, '.');
6634 obstack_grow0 (&temporary_obstack,
6635 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6636 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6637 id_name = obstack_finish (&temporary_obstack);
6639 node = maybe_get_identifier (id_name);
6640 if (node && IS_A_CLASSFILE_NAME (node))
6644 cl = TREE_PURPOSE (import);
6652 (TREE_PURPOSE (import),
6653 "Type `%s' also potentially defined in package `%s'",
6654 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6655 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6662 /* Setup lineno so that it refers to the line of the import (in
6663 case we parse a class file and encounter errors */
6665 int saved_lineno = lineno;
6666 lineno = EXPR_WFL_LINENO (cl);
6667 TYPE_NAME (class_type) = node_to_use;
6668 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6669 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6670 /* If there is no DECL set for the class or if the class isn't
6671 loaded and not seen in source yet, the load */
6672 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6673 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6674 load_class (node_to_use, 0);
6675 lineno = saved_lineno;
6676 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6679 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6682 /* Add package NAME to the list of package encountered so far. To
6683 speed up class lookup in do_resolve_class, we make sure a
6684 particular package is added only once. */
6687 register_package (name)
6690 static struct hash_table _pht, *pht = NULL;
6694 hash_table_init (&_pht, hash_newfunc,
6695 java_hash_hash_tree_node, java_hash_compare_tree_node);
6699 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6701 package_list = chainon (package_list, build_tree_list (name, NULL));
6702 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6707 resolve_package (pkg, next)
6711 tree type_name = NULL_TREE;
6712 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
6714 /* The trick is to determine when the package name stops and were
6715 the name of something contained in the package starts. Then we
6716 return a fully qualified name of what we want to get. */
6718 /* Do a quick search on well known package names */
6719 if (!strncmp (name, "java.lang.reflect", 17))
6722 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6723 type_name = lookup_package_type (name, 17);
6725 else if (!strncmp (name, "java.lang", 9))
6727 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6728 type_name = lookup_package_type (name, 9);
6731 /* If we found something here, return */
6735 *next = EXPR_WFL_QUALIFICATION (pkg);
6737 /* Try to progressively construct a type name */
6738 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6739 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6740 current; current = TREE_CHAIN (current))
6742 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6743 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6746 /* resolve_package should be used in a loop, hence we
6747 point at this one to naturally process the next one at
6748 the next iteration. */
6757 lookup_package_type (name, from)
6762 const char *sub = &name[from+1];
6763 while (*sub != '.' && *sub)
6765 strncpy (subname, name, sub-name);
6766 subname [sub-name] = '\0';
6767 return get_identifier (subname);
6771 check_inner_class_access (decl, enclosing_decl, cl)
6772 tree decl, enclosing_decl, cl;
6776 /* We don't issue an error message when CL is null. CL can be null
6777 as a result of processing a JDEP crafted by source_start_java_method
6778 for the purpose of patching its parm decl. But the error would
6779 have been already trapped when fixing the method's signature.
6780 DECL can also be NULL in case of earlier errors. */
6784 /* We grant access to private and protected inner classes if the
6785 location from where we're trying to access DECL is an enclosing
6786 context for DECL or if both have a common enclosing context. */
6787 if (CLASS_PRIVATE (decl))
6789 if (CLASS_PROTECTED (decl))
6794 if (common_enclosing_context_p (TREE_TYPE (enclosing_decl),
6796 || enclosing_context_p (TREE_TYPE (enclosing_decl),
6800 parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed",
6801 (access == 1 ? "private" : "protected"),
6802 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6803 lang_printable_name (decl, 0));
6806 /* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6807 access violations were found, 1 otherwise. */
6810 check_pkg_class_access (class_name, cl)
6816 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6819 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6822 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6824 /* Access to a private class within the same package is
6827 breakdown_qualified (&l, &r, class_name);
6828 if (l == ctxp->package)
6832 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
6833 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6834 IDENTIFIER_POINTER (class_name));
6840 /* Local variable declaration. */
6843 declare_local_variables (modifier, type, vlist)
6848 tree decl, current, saved_type;
6849 tree type_wfl = NULL_TREE;
6853 /* Push a new block if statements were seen between the last time we
6854 pushed a block and now. Keep a cound of block to close */
6855 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
6857 tree body = GET_CURRENT_BLOCK (current_function_decl);
6858 tree b = enter_block ();
6859 BLOCK_EXPR_ORIGIN (b) = body;
6865 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
6866 if (modifier == ACC_FINAL)
6871 (ctxp->modifier_ctx [i],
6872 "Only `final' is allowed as a local variables modifier");
6877 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6878 hold the TYPE value if a new incomplete has to be created (as
6879 opposed to being found already existing and reused). */
6880 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6882 /* If TYPE is fully resolved and we don't have a reference, make one */
6883 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6885 /* Go through all the declared variables */
6886 for (current = vlist, saved_type = type; current;
6887 current = TREE_CHAIN (current), type = saved_type)
6889 tree other, real_type;
6890 tree wfl = TREE_PURPOSE (current);
6891 tree name = EXPR_WFL_NODE (wfl);
6892 tree init = TREE_VALUE (current);
6894 /* Process NAME, as it may specify extra dimension(s) for it */
6895 type = build_array_from_name (type, type_wfl, name, &name);
6897 /* Variable redefinition check */
6898 if ((other = lookup_name_in_blocks (name)))
6900 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6901 DECL_SOURCE_LINE (other));
6905 /* Type adjustment. We may have just readjusted TYPE because
6906 the variable specified more dimensions. Make sure we have
6907 a reference if we can and don't have one already. */
6908 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6910 real_type = GET_REAL_TYPE (type);
6911 /* Never layout this decl. This will be done when its scope
6913 decl = build_decl (VAR_DECL, name, real_type);
6914 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
6915 LOCAL_FINAL (decl) = final_p;
6916 BLOCK_CHAIN_DECL (decl);
6918 /* If doing xreferencing, replace the line number with the WFL
6921 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
6923 /* Don't try to use an INIT statement when an error was found */
6924 if (init && java_error_count)
6927 /* Add the initialization function to the current function's code */
6930 /* Name might have been readjusted */
6931 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
6932 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
6933 java_method_add_stmt (current_function_decl,
6934 build_debugable_stmt (EXPR_WFL_LINECOL (init),
6938 /* Setup dependency the type of the decl */
6942 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
6943 dep = CLASSD_LAST (ctxp->classd_list);
6944 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
6947 SOURCE_FRONTEND_DEBUG (("Defined locals"));
6950 /* Called during parsing. Build decls from argument list. */
6953 source_start_java_method (fndecl)
6963 current_function_decl = fndecl;
6965 /* New scope for the function */
6967 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
6968 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
6970 tree type = TREE_VALUE (tem);
6971 tree name = TREE_PURPOSE (tem);
6973 /* If type is incomplete. Create an incomplete decl and ask for
6974 the decl to be patched later */
6975 if (INCOMPLETE_TYPE_P (type))
6978 tree real_type = GET_REAL_TYPE (type);
6979 parm_decl = build_decl (PARM_DECL, name, real_type);
6980 type = obtain_incomplete_type (type);
6981 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
6982 jdep = CLASSD_LAST (ctxp->classd_list);
6983 JDEP_MISC (jdep) = name;
6984 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
6987 parm_decl = build_decl (PARM_DECL, name, type);
6989 /* Remember if a local variable was declared final (via its
6990 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
6991 if (ARG_FINAL_P (tem))
6993 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (parm_decl);
6994 LOCAL_FINAL (parm_decl) = 1;
6997 BLOCK_CHAIN_DECL (parm_decl);
6999 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7000 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7002 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7003 DECL_MAX_LOCALS (current_function_decl) = i;
7006 /* Called during parsing. Creates an artificial method declaration. */
7009 create_artificial_method (class, flags, type, name, args)
7012 tree type, name, args;
7016 java_parser_context_save_global ();
7018 mdecl = make_node (FUNCTION_TYPE);
7019 TREE_TYPE (mdecl) = type;
7020 TYPE_ARG_TYPES (mdecl) = args;
7021 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7022 java_parser_context_restore_global ();
7023 DECL_ARTIFICIAL (mdecl) = 1;
7027 /* Starts the body if an artifical method. */
7030 start_artificial_method_body (mdecl)
7033 DECL_SOURCE_LINE (mdecl) = 1;
7034 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7035 source_start_java_method (mdecl);
7040 end_artificial_method_body (mdecl)
7043 /* exit_block modifies DECL_FUNCTION_BODY (current_function_decl).
7044 It has to be evaluated first. (if mdecl is current_function_decl,
7045 we have an undefined behavior if no temporary variable is used.) */
7046 tree b = exit_block ();
7047 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = b;
7051 /* Called during expansion. Push decls formerly built from argument
7052 list so they're usable during expansion. */
7055 expand_start_java_method (fndecl)
7060 current_function_decl = fndecl;
7063 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
7064 announce_function (fndecl);
7066 fprintf (stderr, "]");
7068 pushlevel (1); /* Prepare for a parameter push */
7069 ptr = &DECL_ARGUMENTS (fndecl);
7070 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7073 tree next = TREE_CHAIN (tem);
7074 tree type = TREE_TYPE (tem);
7075 if (PROMOTE_PROTOTYPES
7076 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7077 && INTEGRAL_TYPE_P (type))
7078 type = integer_type_node;
7079 DECL_ARG_TYPE (tem) = type;
7080 layout_decl (tem, 0);
7083 ptr = &TREE_CHAIN (tem);
7087 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7088 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
7091 /* Terminate a function and expand its body. */
7094 source_end_java_method ()
7096 tree fndecl = current_function_decl;
7097 int flag_asynchronous_exceptions = asynchronous_exceptions;
7102 java_parser_context_save_global ();
7103 lineno = ctxp->last_ccb_indent1;
7105 /* Set EH language codes */
7106 java_set_exception_lang_code ();
7108 /* Turn function bodies with only a NOP expr null, so they don't get
7109 generated at all and we won't get warnings when using the -W
7111 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7112 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7114 /* Generate function's code */
7115 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7116 && ! flag_emit_class_files
7117 && ! flag_emit_xref)
7118 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7120 /* pop out of its parameters */
7121 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7123 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7125 /* Generate rtl for function exit. */
7126 if (! flag_emit_class_files && ! flag_emit_xref)
7128 lineno = DECL_SOURCE_LINE_LAST (fndecl);
7129 /* Emit catch-finally clauses */
7131 expand_function_end (input_filename, lineno, 0);
7133 /* FIXME: If the current method contains any exception handlers,
7134 force asynchronous_exceptions: this is necessary because signal
7135 handlers in libjava may throw exceptions. This is far from being
7136 a perfect solution, but it's better than doing nothing at all.*/
7138 asynchronous_exceptions = 1;
7140 /* Run the optimizers and output assembler code for this function. */
7141 rest_of_compilation (fndecl);
7144 current_function_decl = NULL_TREE;
7145 java_parser_context_restore_global ();
7146 asynchronous_exceptions = flag_asynchronous_exceptions;
7149 /* Record EXPR in the current function block. Complements compound
7150 expression second operand if necessary. */
7153 java_method_add_stmt (fndecl, expr)
7156 if (!GET_CURRENT_BLOCK (fndecl))
7158 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7162 add_stmt_to_block (b, type, stmt)
7165 tree body = BLOCK_EXPR_BODY (b), c;
7167 if (java_error_count)
7170 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7173 BLOCK_EXPR_BODY (b) = c;
7174 TREE_SIDE_EFFECTS (c) = 1;
7178 /* Add STMT to EXISTING if possible, otherwise create a new
7179 COMPOUND_EXPR and add STMT to it. */
7182 add_stmt_to_compound (existing, type, stmt)
7183 tree existing, type, stmt;
7186 return build (COMPOUND_EXPR, type, existing, stmt);
7191 void java_layout_seen_class_methods ()
7193 tree previous_list = all_class_list;
7194 tree end = NULL_TREE;
7199 for (current = previous_list;
7200 current != end; current = TREE_CHAIN (current))
7201 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7203 if (previous_list != all_class_list)
7205 end = previous_list;
7206 previous_list = all_class_list;
7214 java_reorder_fields ()
7216 static tree stop_reordering = NULL_TREE;
7217 static int initialized_p;
7220 /* Register STOP_REORDERING with the garbage collector. */
7223 ggc_add_tree_root (&stop_reordering, 1);
7227 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7229 current_class = TREE_TYPE (TREE_VALUE (current));
7231 if (current_class == stop_reordering)
7234 /* Reverse the fields, but leave the dummy field in front.
7235 Fields are already ordered for Object and Class */
7236 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7237 && current_class != class_type_node)
7239 /* If the dummy field is there, reverse the right fields and
7240 just layout the type for proper fields offset */
7241 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7243 tree fields = TYPE_FIELDS (current_class);
7244 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7245 TYPE_SIZE (current_class) = NULL_TREE;
7247 /* We don't have a dummy field, we need to layout the class,
7248 after having reversed the fields */
7251 TYPE_FIELDS (current_class) =
7252 nreverse (TYPE_FIELDS (current_class));
7253 TYPE_SIZE (current_class) = NULL_TREE;
7257 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7260 /* Layout the methods of all classes loaded in one way on an
7261 other. Check methods of source parsed classes. Then reorder the
7262 fields and layout the classes or the type of all source parsed
7266 java_layout_classes ()
7269 int save_error_count = java_error_count;
7271 /* Layout the methods of all classes seen so far */
7272 java_layout_seen_class_methods ();
7273 java_parse_abort_on_error ();
7274 all_class_list = NULL_TREE;
7276 /* Then check the methods of all parsed classes */
7277 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7278 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7279 java_check_methods (TREE_VALUE (current));
7280 java_parse_abort_on_error ();
7282 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7284 current_class = TREE_TYPE (TREE_VALUE (current));
7285 layout_class (current_class);
7287 /* From now on, the class is considered completely loaded */
7288 CLASS_LOADED_P (current_class) = 1;
7290 /* Error reported by the caller */
7291 if (java_error_count)
7295 /* We might have reloaded classes durign the process of laying out
7296 classes for code generation. We must layout the methods of those
7297 late additions, as constructor checks might use them */
7298 java_layout_seen_class_methods ();
7299 java_parse_abort_on_error ();
7302 /* Expand methods in the current set of classes rememebered for
7306 java_complete_expand_classes ()
7310 do_not_fold = flag_emit_xref;
7312 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7313 if (!INNER_CLASS_DECL_P (current))
7314 java_complete_expand_class (current);
7317 /* Expand the methods found in OUTER, starting first by OUTER's inner
7321 java_complete_expand_class (outer)
7326 set_nested_class_simple_name_value (outer, 1); /* Set */
7328 /* We need to go after all inner classes and start expanding them,
7329 starting with most nested ones. We have to do that because nested
7330 classes might add functions to outer classes */
7332 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7333 inner_list; inner_list = TREE_CHAIN (inner_list))
7334 java_complete_expand_class (TREE_PURPOSE (inner_list));
7336 java_complete_expand_methods (outer);
7337 set_nested_class_simple_name_value (outer, 0); /* Reset */
7340 /* Expand methods registered in CLASS_DECL. The general idea is that
7341 we expand regular methods first. This allows us get an estimate on
7342 how outer context local alias fields are really used so we can add
7343 to the constructor just enough code to initialize them properly (it
7344 also lets us generate finit$ correctly.) Then we expand the
7345 constructors and then <clinit>. */
7348 java_complete_expand_methods (class_decl)
7351 tree clinit, finit, decl, first_decl;
7353 current_class = TREE_TYPE (class_decl);
7355 /* Find whether the class has final variables */
7356 for (decl = TYPE_FIELDS (current_class); decl; decl = TREE_CHAIN (decl))
7357 if (FIELD_FINAL (decl))
7359 TYPE_HAS_FINAL_VARIABLE (current_class) = 1;
7363 /* Initialize a new constant pool */
7364 init_outgoing_cpool ();
7366 /* Pre-expand <clinit> to figure whether we really need it or
7367 not. If we do need it, we pre-expand the static fields so they're
7368 ready to be used somewhere else. <clinit> will be fully expanded
7369 after we processed the constructors. */
7370 first_decl = TYPE_METHODS (current_class);
7371 clinit = maybe_generate_pre_expand_clinit (current_class);
7373 /* Then generate finit$ (if we need to) because constructor will
7375 if (TYPE_FINIT_STMT_LIST (current_class))
7377 finit = generate_finit (current_class);
7378 java_complete_expand_method (finit);
7381 /* Now do the constructors */
7382 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7386 if (!DECL_CONSTRUCTOR_P (decl))
7389 no_body = !DECL_FUNCTION_BODY (decl);
7390 /* Don't generate debug info on line zero when expanding a
7391 generated constructor. */
7393 restore_line_number_status (1);
7395 /* Reset the final local variable assignment flags */
7396 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7397 reset_final_variable_local_assignment_flag (current_class);
7399 java_complete_expand_method (decl);
7401 /* Check for missed out final variable assignment */
7402 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7403 check_final_variable_local_assignment_flag (current_class, decl);
7406 restore_line_number_status (0);
7409 /* First, do the ordinary methods. */
7410 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7412 /* Skip abstract or native methods -- but do handle native
7413 methods when generating JNI stubs. */
7414 if (METHOD_ABSTRACT (decl)
7415 || (! flag_jni && METHOD_NATIVE (decl))
7416 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7419 if (METHOD_NATIVE (decl))
7421 tree body = build_jni_stub (decl);
7422 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7425 java_complete_expand_method (decl);
7428 /* If there is indeed a <clinit>, fully expand it now */
7431 /* Reset the final local variable assignment flags */
7432 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7433 reset_static_final_variable_assignment_flag (current_class);
7434 /* Prevent the use of `this' inside <clinit> */
7435 ctxp->explicit_constructor_p = 1;
7436 java_complete_expand_method (clinit);
7437 ctxp->explicit_constructor_p = 0;
7438 /* Check for missed out static final variable assignment */
7439 if (TYPE_HAS_FINAL_VARIABLE (current_class)
7440 && !CLASS_INTERFACE (class_decl))
7441 check_static_final_variable_assignment_flag (current_class);
7444 /* We might have generated a class$ that we now want to expand */
7445 if (TYPE_DOT_CLASS (current_class))
7446 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7448 /* Now verify constructor circularity (stop after the first one we
7450 if (!CLASS_INTERFACE (class_decl))
7451 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7452 if (DECL_CONSTRUCTOR_P (decl)
7453 && verify_constructor_circularity (decl, decl))
7456 /* Final check on the initialization of final variables. */
7457 if (TYPE_HAS_FINAL_VARIABLE (current_class))
7459 check_final_variable_global_assignment_flag (current_class);
7460 /* If we have an interface, check for uninitialized fields. */
7461 if (CLASS_INTERFACE (class_decl))
7462 check_static_final_variable_assignment_flag (current_class);
7465 /* Save the constant pool. We'll need to restore it later. */
7466 TYPE_CPOOL (current_class) = outgoing_cpool;
7469 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7470 safely used in some other methods/constructors. */
7473 maybe_generate_pre_expand_clinit (class_type)
7476 tree current, mdecl;
7478 if (!TYPE_CLINIT_STMT_LIST (class_type))
7481 /* Go through all static fields and pre expand them */
7482 for (current = TYPE_FIELDS (class_type); current;
7483 current = TREE_CHAIN (current))
7484 if (FIELD_STATIC (current))
7485 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7487 /* Then build the <clinit> method */
7488 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7489 clinit_identifier_node, end_params_node);
7490 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7492 start_artificial_method_body (mdecl);
7494 /* We process the list of assignment we produced as the result of
7495 the declaration of initialized static field and add them as
7496 statement to the <clinit> method. */
7497 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7498 current = TREE_CHAIN (current))
7500 tree stmt = current;
7501 /* We build the assignment expression that will initialize the
7502 field to its value. There are strict rules on static
7503 initializers (8.5). FIXME */
7504 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7505 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7506 java_method_add_stmt (mdecl, stmt);
7509 end_artificial_method_body (mdecl);
7511 /* Now we want to place <clinit> as the last method (because we need
7512 it at least for interface so that it doesn't interfere with the
7513 dispatch table based lookup. */
7514 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7516 current = TREE_CHAIN (TYPE_METHODS (class_type));
7517 TYPE_METHODS (class_type) = current;
7519 while (TREE_CHAIN (current))
7520 current = TREE_CHAIN (current);
7522 TREE_CHAIN (current) = mdecl;
7523 TREE_CHAIN (mdecl) = NULL_TREE;
7529 /* Analyzes a method body and look for something that isn't a
7533 analyze_clinit_body (bbody)
7537 switch (TREE_CODE (bbody))
7540 bbody = BLOCK_EXPR_BODY (bbody);
7543 case EXPR_WITH_FILE_LOCATION:
7544 bbody = EXPR_WFL_NODE (bbody);
7548 if (analyze_clinit_body (TREE_OPERAND (bbody, 0)))
7550 bbody = TREE_OPERAND (bbody, 1);
7565 /* See whether we could get rid of <clinit>. Criteria are: all static
7566 final fields have constant initial values and the body of <clinit>
7567 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7570 maybe_yank_clinit (mdecl)
7577 if (!DECL_CLINIT_P (mdecl))
7580 /* If the body isn't empty, then we keep <clinit>. Note that if
7581 we're emitting classfiles, this isn't enough not to rule it
7583 fbody = DECL_FUNCTION_BODY (mdecl);
7584 bbody = BLOCK_EXPR_BODY (fbody);
7585 if (bbody && bbody != error_mark_node)
7586 bbody = BLOCK_EXPR_BODY (bbody);
7589 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7592 type = DECL_CONTEXT (mdecl);
7593 current = TYPE_FIELDS (type);
7595 for (current = (current ? TREE_CHAIN (current) : current);
7596 current; current = TREE_CHAIN (current))
7600 /* We're not interested in non static field */
7601 if (!FIELD_STATIC (current))
7604 /* Anything that isn't String or a basic type is ruled out -- or
7605 if we know how to deal with it (when doing things natively) we
7606 should generated an empty <clinit> so that SUID are computed
7608 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7609 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7612 f_init = DECL_INITIAL (current);
7613 /* If we're emitting native code, we want static final fields to
7614 have constant initializers. If we don't meet these
7615 conditions, we keep <clinit> */
7616 if (!flag_emit_class_files
7617 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7619 /* If we're emitting bytecode, we want static fields to have
7620 constant initializers or no initializer. If we don't meet
7621 these conditions, we keep <clinit> */
7622 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7626 /* Now we analyze the method body and look for something that
7627 isn't a MODIFY_EXPR */
7628 if (bbody == empty_stmt_node)
7631 found = analyze_clinit_body (bbody);
7633 if (current || found)
7636 /* Get rid of <clinit> in the class' list of methods */
7637 if (TYPE_METHODS (type) == mdecl)
7638 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7640 for (current = TYPE_METHODS (type); current;
7641 current = TREE_CHAIN (current))
7642 if (TREE_CHAIN (current) == mdecl)
7644 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7652 /* Complete and expand a method. */
7655 java_complete_expand_method (mdecl)
7658 int yank_clinit = 0;
7660 current_function_decl = mdecl;
7661 /* Fix constructors before expanding them */
7662 if (DECL_CONSTRUCTOR_P (mdecl))
7663 fix_constructors (mdecl);
7665 /* Expand functions that have a body */
7666 if (DECL_FUNCTION_BODY (mdecl))
7668 tree fbody = DECL_FUNCTION_BODY (mdecl);
7669 tree block_body = BLOCK_EXPR_BODY (fbody);
7670 tree exception_copy = NULL_TREE;
7671 expand_start_java_method (mdecl);
7672 build_result_decl (mdecl);
7675 = (!METHOD_STATIC (mdecl) ?
7676 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7678 /* Purge the `throws' list of unchecked exceptions. If we're
7679 doing xref, save a copy of the list and re-install it
7682 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7684 purge_unchecked_exceptions (mdecl);
7686 /* Install exceptions thrown with `throws' */
7687 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7689 if (block_body != NULL_TREE)
7691 block_body = java_complete_tree (block_body);
7693 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
7694 check_for_initialization (block_body);
7695 ctxp->explicit_constructor_p = 0;
7698 BLOCK_EXPR_BODY (fbody) = block_body;
7700 /* If we saw a return but couldn't evaluate it properly, we'll
7701 have an error_mark_node here. */
7702 if (block_body != error_mark_node
7703 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
7704 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7706 missing_return_error (current_function_decl);
7708 /* Check wether we could just get rid of clinit, now the picture
7710 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7711 complete_start_java_method (mdecl);
7713 /* Don't go any further if we've found error(s) during the
7715 if (!java_error_count && !yank_clinit)
7716 source_end_java_method ();
7719 if (java_error_count)
7720 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7724 /* Pop the exceptions and sanity check */
7726 if (currently_caught_type_list)
7730 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
7736 /* This section of the code deals with accessing enclosing context
7737 fields either directly by using the relevant access to this$<n> or
7738 by invoking an access method crafted for that purpose. */
7740 /* Build the necessary access from an inner class to an outer
7741 class. This routine could be optimized to cache previous result
7742 (decl, current_class and returned access). When an access method
7743 needs to be generated, it always takes the form of a read. It might
7744 be later turned into a write by calling outer_field_access_fix. */
7747 build_outer_field_access (id, decl)
7750 tree access = NULL_TREE;
7751 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7752 tree decl_ctx = DECL_CONTEXT (decl);
7754 /* If the immediate enclosing context of the current class is the
7755 field decl's class or inherits from it; build the access as
7756 `this$<n>.<field>'. Note that we will break the `private' barrier
7757 if we're not emitting bytecodes. */
7758 if ((ctx == decl_ctx || inherits_from_p (ctx, decl_ctx))
7759 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7761 tree thisn = build_current_thisn (current_class);
7762 access = make_qualified_primary (build_wfl_node (thisn),
7763 id, EXPR_WFL_LINECOL (id));
7765 /* Otherwise, generate access methods to outer this and access the
7766 field (either using an access method or by direct access.) */
7769 int lc = EXPR_WFL_LINECOL (id);
7771 /* Now we chain the required number of calls to the access$0 to
7772 get a hold to the enclosing instance we need, and then we
7773 build the field access. */
7774 access = build_access_to_thisn (current_class, decl_ctx, lc);
7776 /* If the field is private and we're generating bytecode, then
7777 we generate an access method */
7778 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7780 tree name = build_outer_field_access_methods (decl);
7781 access = build_outer_field_access_expr (lc, decl_ctx,
7782 name, access, NULL_TREE);
7784 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7785 Once again we break the `private' access rule from a foreign
7788 access = make_qualified_primary (access, id, lc);
7790 return resolve_expression_name (access, NULL);
7793 /* Return a non zero value if NODE describes an outer field inner
7797 outer_field_access_p (type, decl)
7800 if (!INNER_CLASS_TYPE_P (type)
7801 || TREE_CODE (decl) != FIELD_DECL
7802 || DECL_CONTEXT (decl) == type)
7805 /* If the inner class extends the declaration context of the field
7806 we're try to acces, then this isn't an outer field access */
7807 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7810 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7811 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7813 if (type == DECL_CONTEXT (decl))
7816 if (!DECL_CONTEXT (TYPE_NAME (type)))
7818 /* Before we give up, see whether the field is inherited from
7819 the enclosing context we're considering. */
7820 if (inherits_from_p (type, DECL_CONTEXT (decl)))
7829 /* Return a non zero value if NODE represents an outer field inner
7830 access that was been already expanded. As a side effect, it returns
7831 the name of the field being accessed and the argument passed to the
7832 access function, suitable for a regeneration of the access method
7833 call if necessary. */
7836 outer_field_expanded_access_p (node, name, arg_type, arg)
7837 tree node, *name, *arg_type, *arg;
7841 if (TREE_CODE (node) != CALL_EXPR)
7844 /* Well, gcj generates slightly different tree nodes when compiling
7845 to native or bytecodes. It's the case for function calls. */
7847 if (flag_emit_class_files
7848 && TREE_CODE (node) == CALL_EXPR
7849 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7851 else if (!flag_emit_class_files)
7853 node = TREE_OPERAND (node, 0);
7855 if (node && TREE_OPERAND (node, 0)
7856 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7858 node = TREE_OPERAND (node, 0);
7859 if (TREE_OPERAND (node, 0)
7860 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7861 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7862 (DECL_NAME (TREE_OPERAND (node, 0)))))
7867 if (identified && name && arg_type && arg)
7869 tree argument = TREE_OPERAND (node, 1);
7870 *name = DECL_NAME (TREE_OPERAND (node, 0));
7871 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7872 *arg = TREE_VALUE (argument);
7877 /* Detect in NODE an outer field read access from an inner class and
7878 transform it into a write with RHS as an argument. This function is
7879 called from the java_complete_lhs when an assignment to a LHS can
7883 outer_field_access_fix (wfl, node, rhs)
7884 tree wfl, node, rhs;
7886 tree name, arg_type, arg;
7888 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7890 /* At any rate, check whether we're trying to assign a value to
7892 tree accessed = (JDECL_P (node) ? node :
7893 (TREE_CODE (node) == COMPONENT_REF ?
7894 TREE_OPERAND (node, 1) : node));
7895 if (check_final_assignment (accessed, wfl))
7896 return error_mark_node;
7898 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7899 arg_type, name, arg, rhs);
7900 return java_complete_tree (node);
7905 /* Construct the expression that calls an access method:
7906 <type>.access$<n>(<arg1> [, <arg2>]);
7908 ARG2 can be NULL and will be omitted in that case. It will denote a
7912 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7914 tree type, access_method_name, arg1, arg2;
7916 tree args, cn, access;
7918 args = arg1 ? arg1 :
7919 build_wfl_node (build_current_thisn (current_class));
7920 args = build_tree_list (NULL_TREE, args);
7923 args = tree_cons (NULL_TREE, arg2, args);
7925 access = build_method_invocation (build_wfl_node (access_method_name), args);
7926 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7927 return make_qualified_primary (cn, access, lc);
7931 build_new_access_id ()
7933 static int access_n_counter = 1;
7936 sprintf (buffer, "access$%d", access_n_counter++);
7937 return get_identifier (buffer);
7940 /* Create the static access functions for the outer field DECL. We define a
7942 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7946 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7947 TREE_TYPE (<field>) value$) {
7948 return inst$.field = value$;
7950 We should have a usage flags on the DECL so we can lazily turn the ones
7951 we're using for code generation. FIXME.
7955 build_outer_field_access_methods (decl)
7958 tree id, args, stmt, mdecl;
7960 if (FIELD_INNER_ACCESS_P (decl))
7961 return FIELD_INNER_ACCESS (decl);
7963 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
7965 /* Create the identifier and a function named after it. */
7966 id = build_new_access_id ();
7968 /* The identifier is marked as bearing the name of a generated write
7969 access function for outer field accessed from inner classes. */
7970 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7972 /* Create the read access */
7973 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7974 TREE_CHAIN (args) = end_params_node;
7975 stmt = make_qualified_primary (build_wfl_node (inst_id),
7976 build_wfl_node (DECL_NAME (decl)), 0);
7977 stmt = build_return (0, stmt);
7978 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7979 TREE_TYPE (decl), id, args, stmt);
7980 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7982 /* Create the write access method. No write access for final variable */
7983 if (!FIELD_FINAL (decl))
7985 args = build_tree_list (inst_id,
7986 build_pointer_type (DECL_CONTEXT (decl)));
7987 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
7988 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
7989 stmt = make_qualified_primary (build_wfl_node (inst_id),
7990 build_wfl_node (DECL_NAME (decl)), 0);
7991 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
7992 build_wfl_node (wpv_id)));
7993 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7994 TREE_TYPE (decl), id,
7997 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7999 /* Return the access name */
8000 return FIELD_INNER_ACCESS (decl) = id;
8003 /* Build an field access method NAME. */
8006 build_outer_field_access_method (class, type, name, args, body)
8007 tree class, type, name, args, body;
8009 tree saved_current_function_decl, mdecl;
8011 /* Create the method */
8012 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8013 fix_method_argument_names (args, mdecl);
8014 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8016 /* Attach the method body. */
8017 saved_current_function_decl = current_function_decl;
8018 start_artificial_method_body (mdecl);
8019 java_method_add_stmt (mdecl, body);
8020 end_artificial_method_body (mdecl);
8021 current_function_decl = saved_current_function_decl;
8027 /* This section deals with building access function necessary for
8028 certain kinds of method invocation from inner classes. */
8031 build_outer_method_access_method (decl)
8034 tree saved_current_function_decl, mdecl;
8035 tree args = NULL_TREE, call_args = NULL_TREE;
8036 tree carg, id, body, class;
8038 int parm_id_count = 0;
8040 /* Test this abort with an access to a private field */
8041 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8044 /* Check the cache first */
8045 if (DECL_FUNCTION_INNER_ACCESS (decl))
8046 return DECL_FUNCTION_INNER_ACCESS (decl);
8048 class = DECL_CONTEXT (decl);
8050 /* Obtain an access identifier and mark it */
8051 id = build_new_access_id ();
8052 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8054 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8055 /* Create the arguments, as much as the original */
8056 for (; carg && carg != end_params_node;
8057 carg = TREE_CHAIN (carg))
8059 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8060 args = chainon (args, build_tree_list (get_identifier (buffer),
8061 TREE_VALUE (carg)));
8063 args = chainon (args, end_params_node);
8065 /* Create the method */
8066 mdecl = create_artificial_method (class, ACC_STATIC,
8067 TREE_TYPE (TREE_TYPE (decl)), id, args);
8068 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8069 /* There is a potential bug here. We should be able to use
8070 fix_method_argument_names, but then arg names get mixed up and
8071 eventually a constructor will have its this$0 altered and the
8072 outer context won't be assignment properly. The test case is
8074 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8076 /* Attach the method body. */
8077 saved_current_function_decl = current_function_decl;
8078 start_artificial_method_body (mdecl);
8080 /* The actual method invocation uses the same args. When invoking a
8081 static methods that way, we don't want to skip the first
8084 if (!METHOD_STATIC (decl))
8085 carg = TREE_CHAIN (carg);
8086 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8087 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8090 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8092 if (!METHOD_STATIC (decl))
8093 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8095 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8096 body = build_return (0, body);
8097 java_method_add_stmt (mdecl,body);
8098 end_artificial_method_body (mdecl);
8099 current_function_decl = saved_current_function_decl;
8101 /* Back tag the access function so it know what it accesses */
8102 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8104 /* Tag the current method so it knows it has an access generated */
8105 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8109 /* This section of the code deals with building expressions to access
8110 the enclosing instance of an inner class. The enclosing instance is
8111 kept in a generated field called this$<n>, with <n> being the
8112 inner class nesting level (starting from 0.) */
8114 /* Build an access to a given this$<n>, always chaining access call to
8115 others. Access methods to this$<n> are build on the fly if
8116 necessary. This CAN'T be used to solely access this$<n-1> from
8117 this$<n> (which alway yield to special cases and optimization, see
8118 for example build_outer_field_access). */
8121 build_access_to_thisn (from, to, lc)
8125 tree access = NULL_TREE;
8131 access = build_current_thisn (from);
8132 access = build_wfl_node (access);
8136 tree access0_wfl, cn;
8138 maybe_build_thisn_access_method (from);
8139 access0_wfl = build_wfl_node (access0_identifier_node);
8140 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8141 EXPR_WFL_LINECOL (access0_wfl) = lc;
8142 access = build_tree_list (NULL_TREE, access);
8143 access = build_method_invocation (access0_wfl, access);
8144 access = make_qualified_primary (cn, access, lc);
8147 /* if FROM isn't an inter class, that's fine, we've done
8148 enough. What we're looking for can be accessed from there. */
8149 from = DECL_CONTEXT (TYPE_NAME (from));
8152 from = TREE_TYPE (from);
8157 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8158 is returned if nothing needs to be generated. Otherwise, the method
8159 generated and a method decl is returned.
8161 NOTE: These generated methods should be declared in a class file
8162 attribute so that they can't be referred to directly. */
8165 maybe_build_thisn_access_method (type)
8168 tree mdecl, args, stmt, rtype;
8169 tree saved_current_function_decl;
8171 /* If TYPE is a top-level class, no access method is required.
8172 If there already is such an access method, bail out. */
8173 if (CLASS_ACCESS0_GENERATED_P (type) || !PURE_INNER_CLASS_TYPE_P (type))
8176 /* We generate the method. The method looks like:
8177 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8179 args = build_tree_list (inst_id, build_pointer_type (type));
8180 TREE_CHAIN (args) = end_params_node;
8181 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8182 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8183 access0_identifier_node, args);
8184 fix_method_argument_names (args, mdecl);
8185 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8186 stmt = build_current_thisn (type);
8187 stmt = make_qualified_primary (build_wfl_node (inst_id),
8188 build_wfl_node (stmt), 0);
8189 stmt = build_return (0, stmt);
8191 saved_current_function_decl = current_function_decl;
8192 start_artificial_method_body (mdecl);
8193 java_method_add_stmt (mdecl, stmt);
8194 end_artificial_method_body (mdecl);
8195 current_function_decl = saved_current_function_decl;
8197 CLASS_ACCESS0_GENERATED_P (type) = 1;
8202 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8203 the first level of innerclassing. this$1 for the next one, etc...
8204 This function can be invoked with TYPE to NULL, available and then
8205 has to count the parser context. */
8208 build_current_thisn (type)
8211 static int saved_i = -1;
8212 static tree saved_thisn = NULL_TREE;
8213 static tree saved_type = NULL_TREE;
8214 static int saved_type_i = 0;
8215 static int initialized_p;
8220 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8223 ggc_add_tree_root (&saved_thisn, 1);
8224 ggc_add_tree_root (&saved_type, 1);
8230 if (type == saved_type)
8234 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8235 decl; decl = DECL_CONTEXT (decl), i++)
8243 i = list_length (GET_CPC_LIST ())-2;
8248 sprintf (buffer, "this$%d", i);
8250 saved_thisn = get_identifier (buffer);
8254 /* Return the assignement to the hidden enclosing context `this$<n>'
8255 by the second incoming parameter to the innerclass constructor. The
8256 form used is `this.this$<n> = this$<n>;'. */
8259 build_thisn_assign ()
8261 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8263 tree thisn = build_current_thisn (current_class);
8264 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8265 build_wfl_node (thisn), 0);
8266 tree rhs = build_wfl_node (thisn);
8267 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8268 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8274 /* Building the synthetic `class$' used to implement the `.class' 1.1
8275 extension for non primitive types. This method looks like:
8277 static Class class$(String type) throws NoClassDefFoundError
8279 try {return (java.lang.Class.forName (String));}
8280 catch (ClassNotFoundException e) {
8281 throw new NoClassDefFoundError(e.getMessage());}
8285 build_dot_class_method (class)
8288 #define BWF(S) build_wfl_node (get_identifier ((S)))
8289 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8290 tree args, tmp, saved_current_function_decl, mdecl;
8291 tree stmt, throw_stmt, catch, catch_block, try_block;
8292 tree catch_clause_param;
8293 tree class_not_found_exception, no_class_def_found_error;
8295 static tree get_message_wfl, type_parm_wfl;
8297 if (!get_message_wfl)
8299 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8300 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8301 ggc_add_tree_root (&get_message_wfl, 1);
8302 ggc_add_tree_root (&type_parm_wfl, 1);
8305 /* Build the arguments */
8306 args = build_tree_list (get_identifier ("type$"),
8307 build_pointer_type (string_type_node));
8308 TREE_CHAIN (args) = end_params_node;
8310 /* Build the qualified name java.lang.Class.forName */
8311 tmp = MQN (MQN (MQN (BWF ("java"),
8312 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8314 /* For things we have to catch and throw */
8315 class_not_found_exception =
8316 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8317 no_class_def_found_error =
8318 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8319 load_class (class_not_found_exception, 1);
8320 load_class (no_class_def_found_error, 1);
8322 /* Create the "class$" function */
8323 mdecl = create_artificial_method (class, ACC_STATIC,
8324 build_pointer_type (class_type_node),
8325 classdollar_identifier_node, args);
8326 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8327 no_class_def_found_error);
8329 /* We start by building the try block. We need to build:
8330 return (java.lang.Class.forName (type)); */
8331 stmt = build_method_invocation (tmp,
8332 build_tree_list (NULL_TREE, type_parm_wfl));
8333 stmt = build_return (0, stmt);
8334 /* Put it in a block. That's the try block */
8335 try_block = build_expr_block (stmt, NULL_TREE);
8337 /* Now onto the catch block. We start by building the expression
8338 throwing a new exception:
8339 throw new NoClassDefFoundError (_.getMessage); */
8340 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8341 get_message_wfl, 0);
8342 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8344 /* Build new NoClassDefFoundError (_.getMessage) */
8345 throw_stmt = build_new_invocation
8346 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8347 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8349 /* Build the throw, (it's too early to use BUILD_THROW) */
8350 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8352 /* Build the catch block to encapsulate all this. We begin by
8353 building an decl for the catch clause parameter and link it to
8354 newly created block, the catch block. */
8355 catch_clause_param =
8356 build_decl (VAR_DECL, wpv_id,
8357 build_pointer_type (class_not_found_exception));
8358 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8360 /* We initialize the variable with the exception handler. */
8361 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8362 soft_exceptioninfo_call_node);
8363 add_stmt_to_block (catch_block, NULL_TREE, catch);
8365 /* We add the statement throwing the new exception */
8366 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8368 /* Build a catch expression for all this */
8369 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8371 /* Build the try/catch sequence */
8372 stmt = build_try_statement (0, try_block, catch_block);
8374 fix_method_argument_names (args, mdecl);
8375 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8376 saved_current_function_decl = current_function_decl;
8377 start_artificial_method_body (mdecl);
8378 java_method_add_stmt (mdecl, stmt);
8379 end_artificial_method_body (mdecl);
8380 current_function_decl = saved_current_function_decl;
8381 TYPE_DOT_CLASS (class) = mdecl;
8387 build_dot_class_method_invocation (type)
8392 if (TYPE_ARRAY_P (type))
8393 sig_id = build_java_signature (type);
8395 sig_id = DECL_NAME (TYPE_NAME (type));
8397 s = build_string (IDENTIFIER_LENGTH (sig_id),
8398 IDENTIFIER_POINTER (sig_id));
8399 return build_method_invocation (build_wfl_node (classdollar_identifier_node),
8400 build_tree_list (NULL_TREE, s));
8403 /* This section of the code deals with constructor. */
8405 /* Craft a body for default constructor. Patch existing constructor
8406 bodies with call to super() and field initialization statements if
8410 fix_constructors (mdecl)
8413 tree body = DECL_FUNCTION_BODY (mdecl);
8414 tree thisn_assign, compound = NULL_TREE;
8415 tree class_type = DECL_CONTEXT (mdecl);
8419 /* It is an error for the compiler to generate a default
8420 constructor if the superclass doesn't have a constructor that
8421 takes no argument, or the same args for an anonymous class */
8422 if (verify_constructor_super (mdecl))
8424 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8425 tree save = DECL_NAME (mdecl);
8426 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8427 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8429 (lookup_cl (TYPE_NAME (class_type)),
8430 "No constructor matching `%s' found in class `%s'",
8431 lang_printable_name (mdecl, 0), n);
8432 DECL_NAME (mdecl) = save;
8435 /* The constructor body must be crafted by hand. It's the
8436 constructor we defined when we realize we didn't have the
8437 CLASSNAME() constructor */
8438 start_artificial_method_body (mdecl);
8440 /* Insert an assignment to the this$<n> hidden field, if
8442 if ((thisn_assign = build_thisn_assign ()))
8443 java_method_add_stmt (mdecl, thisn_assign);
8445 /* We don't generate a super constructor invocation if we're
8446 compiling java.lang.Object. build_super_invocation takes care
8448 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8450 /* Insert the instance initializer block right here, after the
8451 super invocation. */
8452 add_instance_initializer (mdecl);
8454 end_artificial_method_body (mdecl);
8456 /* Search for an explicit constructor invocation */
8460 tree main_block = BLOCK_EXPR_BODY (body);
8463 switch (TREE_CODE (body))
8466 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8470 case EXPR_WITH_FILE_LOCATION:
8471 body = TREE_OPERAND (body, 0);
8474 body = BLOCK_EXPR_BODY (body);
8480 /* The constructor is missing an invocation of super() */
8482 compound = add_stmt_to_compound (compound, NULL_TREE,
8483 build_super_invocation (mdecl));
8485 /* Generate the assignment to this$<n>, if necessary */
8486 if ((thisn_assign = build_thisn_assign ()))
8487 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8489 /* Insert the instance initializer block right here, after the
8490 super invocation. */
8491 add_instance_initializer (mdecl);
8493 /* Fix the constructor main block if we're adding extra stmts */
8496 compound = add_stmt_to_compound (compound, NULL_TREE,
8497 BLOCK_EXPR_BODY (main_block));
8498 BLOCK_EXPR_BODY (main_block) = compound;
8503 /* Browse constructors in the super class, searching for a constructor
8504 that doesn't take any argument. Return 0 if one is found, 1
8505 otherwise. If the current class is an anonymous inner class, look
8506 for something that has the same signature. */
8509 verify_constructor_super (mdecl)
8512 tree class = CLASSTYPE_SUPER (current_class);
8513 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8519 if (ANONYMOUS_CLASS_P (current_class))
8521 tree mdecl_arg_type;
8522 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8523 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8524 if (DECL_CONSTRUCTOR_P (sdecl))
8527 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8529 arg_type = TREE_CHAIN (arg_type);
8530 for (m_arg_type = mdecl_arg_type;
8531 (arg_type != end_params_node
8532 && m_arg_type != end_params_node);
8533 arg_type = TREE_CHAIN (arg_type),
8534 m_arg_type = TREE_CHAIN (m_arg_type))
8535 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
8538 if (arg_type == end_params_node && m_arg_type == end_params_node)
8544 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8546 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8548 arg = TREE_CHAIN (arg);
8549 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8556 /* Generate code for all context remembered for code generation. */
8559 java_expand_classes ()
8561 int save_error_count = 0;
8562 static struct parser_ctxt *saved_ctxp = NULL;
8564 java_parse_abort_on_error ();
8565 if (!(ctxp = ctxp_for_generation))
8567 java_layout_classes ();
8568 java_parse_abort_on_error ();
8570 saved_ctxp = ctxp_for_generation;
8571 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8573 ctxp = ctxp_for_generation;
8574 lang_init_source (2); /* Error msgs have method prototypes */
8575 java_complete_expand_classes (); /* Complete and expand classes */
8576 java_parse_abort_on_error ();
8579 /* Find anonymous classes and expand their constructor, now they
8581 for (ctxp_for_generation = saved_ctxp;
8582 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8585 ctxp = ctxp_for_generation;
8586 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8588 current_class = TREE_TYPE (current);
8589 if (ANONYMOUS_CLASS_P (current_class))
8592 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8594 if (DECL_CONSTRUCTOR_P (d))
8596 restore_line_number_status (1);
8597 java_complete_expand_method (d);
8598 restore_line_number_status (0);
8599 break; /* We now there are no other ones */
8606 /* If we've found error at that stage, don't try to generate
8607 anything, unless we're emitting xrefs or checking the syntax only
8608 (but not using -fsyntax-only for the purpose of generating
8610 if (java_error_count && !flag_emit_xref
8611 && (!flag_syntax_only && !flag_emit_class_files))
8614 /* Now things are stable, go for generation of the class data. */
8615 for (ctxp_for_generation = saved_ctxp;
8616 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8619 ctxp = ctxp_for_generation;
8620 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8622 current_class = TREE_TYPE (current);
8623 outgoing_cpool = TYPE_CPOOL (current_class);
8624 if (flag_emit_class_files)
8625 write_classfile (current_class);
8627 expand_xref (current_class);
8628 else if (! flag_syntax_only)
8634 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8635 a tree list node containing RIGHT. Fore coming RIGHTs will be
8636 chained to this hook. LOCATION contains the location of the
8637 separating `.' operator. */
8640 make_qualified_primary (primary, right, location)
8641 tree primary, right;
8646 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8647 wfl = build_wfl_wrap (primary, location);
8651 /* If wfl wasn't qualified, we build a first anchor */
8652 if (!EXPR_WFL_QUALIFICATION (wfl))
8653 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
8656 /* And chain them */
8657 EXPR_WFL_LINECOL (right) = location;
8658 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8659 PRIMARY_P (wfl) = 1;
8663 /* Simple merge of two name separated by a `.' */
8666 merge_qualified_name (left, right)
8670 if (!left && !right)
8679 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8680 IDENTIFIER_LENGTH (left));
8681 obstack_1grow (&temporary_obstack, '.');
8682 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8683 IDENTIFIER_LENGTH (right));
8684 node = get_identifier (obstack_base (&temporary_obstack));
8685 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8686 QUALIFIED_P (node) = 1;
8690 /* Merge the two parts of a qualified name into LEFT. Set the
8691 location information of the resulting node to LOCATION, usually
8692 inherited from the location information of the `.' operator. */
8695 make_qualified_name (left, right, location)
8699 #ifdef USE_COMPONENT_REF
8700 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8701 EXPR_WFL_LINECOL (node) = location;
8704 tree left_id = EXPR_WFL_NODE (left);
8705 tree right_id = EXPR_WFL_NODE (right);
8708 merge = merge_qualified_name (left_id, right_id);
8710 /* Left wasn't qualified and is now qualified */
8711 if (!QUALIFIED_P (left_id))
8713 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8714 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8715 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8718 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8719 EXPR_WFL_LINECOL (wfl) = location;
8720 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8722 EXPR_WFL_NODE (left) = merge;
8727 /* Extract the last identifier component of the qualified in WFL. The
8728 last identifier is removed from the linked list */
8731 cut_identifier_in_qualified (wfl)
8735 tree previous = NULL_TREE;
8736 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8737 if (!TREE_CHAIN (q))
8740 /* Operating on a non qualified qualified WFL. */
8743 TREE_CHAIN (previous) = NULL_TREE;
8744 return TREE_PURPOSE (q);
8748 /* Resolve the expression name NAME. Return its decl. */
8751 resolve_expression_name (id, orig)
8755 tree name = EXPR_WFL_NODE (id);
8758 /* 6.5.5.1: Simple expression names */
8759 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8761 /* 15.13.1: NAME can appear within the scope of a local variable
8763 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8766 /* 15.13.1: NAME can appear within a class declaration */
8769 decl = lookup_field_wrapper (current_class, name);
8772 tree access = NULL_TREE;
8773 int fs = FIELD_STATIC (decl);
8775 /* If we're accessing an outer scope local alias, make
8776 sure we change the name of the field we're going to
8778 if (FIELD_LOCAL_ALIAS_USED (decl))
8779 name = DECL_NAME (decl);
8781 /* Instance variable (8.3.1.1) can't appear within
8782 static method, static initializer or initializer for
8783 a static variable. */
8784 if (!fs && METHOD_STATIC (current_function_decl))
8786 static_ref_err (id, name, current_class);
8787 return error_mark_node;
8789 /* Instance variables can't appear as an argument of
8790 an explicit constructor invocation */
8791 if (!fs && ctxp->explicit_constructor_p
8792 && !enclosing_context_p (DECL_CONTEXT (decl), current_class))
8795 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
8796 return error_mark_node;
8799 /* If we're processing an inner class and we're trying
8800 to access a field belonging to an outer class, build
8801 the access to the field */
8802 if (!fs && outer_field_access_p (current_class, decl))
8804 if (CLASS_STATIC (TYPE_NAME (current_class)))
8806 static_ref_err (id, DECL_NAME (decl), current_class);
8807 return error_mark_node;
8809 return build_outer_field_access (id, decl);
8812 /* Otherwise build what it takes to access the field */
8813 access = build_field_ref ((fs ? NULL_TREE : current_this),
8814 DECL_CONTEXT (decl), name);
8815 if (fs && !flag_emit_class_files && !flag_emit_xref)
8816 access = build_class_init (DECL_CONTEXT (access), access);
8817 /* We may be asked to save the real field access node */
8820 /* And we return what we got */
8823 /* Fall down to error report on undefined variable */
8826 /* 6.5.5.2 Qualified Expression Names */
8831 qualify_ambiguous_name (id);
8832 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8833 /* 15.10.2: Accessing Superclass Members using super */
8834 return resolve_field_access (id, orig, NULL);
8837 /* We've got an error here */
8838 parse_error_context (id, "Undefined variable `%s'",
8839 IDENTIFIER_POINTER (name));
8841 return error_mark_node;
8845 static_ref_err (wfl, field_id, class_type)
8846 tree wfl, field_id, class_type;
8850 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8851 IDENTIFIER_POINTER (field_id),
8852 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8855 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8856 We return something suitable to generate the field access. We also
8857 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8858 recipient's address can be null. */
8861 resolve_field_access (qual_wfl, field_decl, field_type)
8863 tree *field_decl, *field_type;
8867 tree decl, where_found, type_found;
8869 if (resolve_qualified_expression_name (qual_wfl, &decl,
8870 &where_found, &type_found))
8871 return error_mark_node;
8873 /* Resolve the LENGTH field of an array here */
8874 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
8875 && type_found && TYPE_ARRAY_P (type_found)
8876 && ! flag_emit_class_files && ! flag_emit_xref)
8878 tree length = build_java_array_length_access (where_found);
8880 build_java_arraynull_check (type_found, length, int_type_node);
8882 /* In case we're dealing with a static array, we need to
8883 initialize its class before the array length can be fetched.
8884 It's also a good time to create a DECL_RTL for the field if
8885 none already exists, otherwise if the field was declared in a
8886 class found in an external file and hasn't been (and won't
8887 be) accessed for its value, none will be created. */
8888 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8890 build_static_field_ref (where_found);
8891 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8894 /* We might have been trying to resolve field.method(). In which
8895 case, the resolution is over and decl is the answer */
8896 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
8898 else if (JDECL_P (decl))
8900 int static_final_found = 0;
8902 type_found = DECL_CONTEXT (decl);
8903 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
8904 if (CLASS_FINAL_VARIABLE_P (decl)
8905 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
8906 && DECL_INITIAL (decl))
8908 /* When called on a FIELD_DECL of the right (primitive)
8909 type, java_complete_tree will try to substitue the decl
8910 for it's initial value. */
8911 field_ref = java_complete_tree (decl);
8912 static_final_found = 1;
8915 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8916 NULL_TREE : where_found),
8917 type_found, DECL_NAME (decl));
8918 if (field_ref == error_mark_node)
8919 return error_mark_node;
8920 if (is_static && !static_final_found
8921 && !flag_emit_class_files && !flag_emit_xref)
8922 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
8930 *field_type = (QUAL_DECL_TYPE (decl) ?
8931 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
8935 /* If NODE is an access to f static field, strip out the class
8936 initialization part and return the field decl, otherwise, return
8940 strip_out_static_field_access_decl (node)
8943 if (TREE_CODE (node) == COMPOUND_EXPR)
8945 tree op1 = TREE_OPERAND (node, 1);
8946 if (TREE_CODE (op1) == COMPOUND_EXPR)
8948 tree call = TREE_OPERAND (op1, 0);
8949 if (TREE_CODE (call) == CALL_EXPR
8950 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8951 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8952 == soft_initclass_node)
8953 return TREE_OPERAND (op1, 1);
8955 else if (JDECL_P (op1))
8961 /* 6.5.5.2: Qualified Expression Names */
8964 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
8966 tree *found_decl, *type_found, *where_found;
8968 int from_type = 0; /* Field search initiated from a type */
8969 int from_super = 0, from_cast = 0, from_qualified_this = 0;
8970 int previous_call_static = 0;
8972 tree decl = NULL_TREE, type = NULL_TREE, q;
8973 /* For certain for of inner class instantiation */
8974 tree saved_current, saved_this;
8975 #define RESTORE_THIS_AND_CURRENT_CLASS \
8976 { current_class = saved_current; current_this = saved_this;}
8978 *type_found = *where_found = NULL_TREE;
8980 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
8982 tree qual_wfl = QUAL_WFL (q);
8983 tree ret_decl; /* for EH checking */
8984 int location; /* for EH checking */
8986 /* 15.10.1 Field Access Using a Primary */
8987 switch (TREE_CODE (qual_wfl))
8990 case NEW_CLASS_EXPR:
8991 /* If the access to the function call is a non static field,
8992 build the code to access it. */
8993 if (JDECL_P (decl) && !FIELD_STATIC (decl))
8995 decl = maybe_access_field (decl, *where_found,
8996 DECL_CONTEXT (decl));
8997 if (decl == error_mark_node)
9001 /* And code for the function call */
9002 if (complete_function_arguments (qual_wfl))
9005 /* We might have to setup a new current class and a new this
9006 for the search of an inner class, relative to the type of
9007 a expression resolved as `decl'. The current values are
9008 saved and restored shortly after */
9009 saved_current = current_class;
9010 saved_this = current_this;
9012 && (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9013 || from_qualified_this))
9015 /* If we still have `from_qualified_this', we have the form
9016 <T>.this.f() and we need to build <T>.this */
9017 if (from_qualified_this)
9019 decl = build_access_to_thisn (current_class, type, 0);
9020 decl = java_complete_tree (decl);
9021 type = TREE_TYPE (TREE_TYPE (decl));
9023 current_class = type;
9024 current_this = decl;
9025 from_qualified_this = 0;
9028 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9029 CALL_USING_SUPER (qual_wfl) = 1;
9030 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9031 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9032 *where_found = patch_method_invocation (qual_wfl, decl, type,
9034 &is_static, &ret_decl);
9035 if (*where_found == error_mark_node)
9037 RESTORE_THIS_AND_CURRENT_CLASS;
9040 *type_found = type = QUAL_DECL_TYPE (*where_found);
9042 /* If we're creating an inner class instance, check for that
9043 an enclosing instance is in scope */
9044 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9045 && INNER_ENCLOSING_SCOPE_CHECK (type))
9048 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9049 lang_printable_name (type, 0),
9050 (!current_this ? "" :
9051 "; an explicit one must be provided when creating this inner class"));
9052 RESTORE_THIS_AND_CURRENT_CLASS;
9056 /* In case we had to change then to resolve a inner class
9057 instantiation using a primary qualified by a `new' */
9058 RESTORE_THIS_AND_CURRENT_CLASS;
9060 /* EH check. No check on access$<n> functions */
9062 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9063 (DECL_NAME (current_function_decl)))
9064 check_thrown_exceptions (location, ret_decl);
9066 /* If the previous call was static and this one is too,
9067 build a compound expression to hold the two (because in
9068 that case, previous function calls aren't transported as
9069 forcoming function's argument. */
9070 if (previous_call_static && is_static)
9072 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9073 TREE_SIDE_EFFECTS (decl) = 1;
9077 previous_call_static = is_static;
9078 decl = *where_found;
9083 case NEW_ARRAY_EXPR:
9084 case NEW_ANONYMOUS_ARRAY_EXPR:
9085 *where_found = decl = java_complete_tree (qual_wfl);
9086 if (decl == error_mark_node)
9088 *type_found = type = QUAL_DECL_TYPE (decl);
9089 CLASS_LOADED_P (type) = 1;
9093 *where_found = decl = java_complete_tree (qual_wfl);
9094 if (decl == error_mark_node)
9096 *type_found = type = QUAL_DECL_TYPE (decl);
9100 case CONDITIONAL_EXPR:
9103 *where_found = decl = java_complete_tree (qual_wfl);
9104 if (decl == error_mark_node)
9106 *type_found = type = QUAL_DECL_TYPE (decl);
9110 /* If the access to the function call is a non static field,
9111 build the code to access it. */
9112 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9114 decl = maybe_access_field (decl, *where_found, type);
9115 if (decl == error_mark_node)
9118 /* And code for the array reference expression */
9119 decl = java_complete_tree (qual_wfl);
9120 if (decl == error_mark_node)
9122 type = QUAL_DECL_TYPE (decl);
9126 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9128 if ((type = patch_string (decl)))
9130 *where_found = QUAL_RESOLUTION (q) = decl;
9131 *type_found = type = TREE_TYPE (decl);
9135 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9137 *where_found = QUAL_RESOLUTION (q) = decl;
9138 *type_found = type = TREE_TYPE (decl);
9142 /* Fix for -Wall Just go to the next statement. Don't
9147 /* If we fall here, we weren't processing a (static) function call. */
9148 previous_call_static = 0;
9150 /* It can be the keyword THIS */
9151 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9156 (wfl, "Keyword `this' used outside allowed context");
9159 if (ctxp->explicit_constructor_p
9160 && type == current_class)
9162 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9165 /* We have to generate code for intermediate acess */
9166 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9168 *where_found = decl = current_this;
9169 *type_found = type = QUAL_DECL_TYPE (decl);
9171 /* We're trying to access the this from somewhere else. Make sure
9172 it's allowed before doing so. */
9175 if (!enclosing_context_p (type, current_class))
9177 char *p = xstrdup (lang_printable_name (type, 0));
9178 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9180 lang_printable_name (current_class, 0));
9184 from_qualified_this = 1;
9185 /* If there's nothing else after that, we need to
9186 produce something now, otherwise, the section of the
9187 code that needs to produce <T>.this will generate
9188 what is necessary. */
9189 if (!TREE_CHAIN (q))
9191 decl = build_access_to_thisn (current_class, type, 0);
9192 *where_found = decl = java_complete_tree (decl);
9193 *type_found = type = TREE_TYPE (decl);
9201 /* 15.10.2 Accessing Superclass Members using SUPER */
9202 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9205 /* Check on the restricted use of SUPER */
9206 if (METHOD_STATIC (current_function_decl)
9207 || current_class == object_type_node)
9210 (wfl, "Keyword `super' used outside allowed context");
9213 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9214 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9215 CLASSTYPE_SUPER (current_class),
9216 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9217 *where_found = decl = java_complete_tree (node);
9218 if (decl == error_mark_node)
9220 *type_found = type = QUAL_DECL_TYPE (decl);
9221 from_super = from_type = 1;
9225 /* 15.13.1: Can't search for field name in packages, so we
9226 assume a variable/class name was meant. */
9227 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9229 tree name = resolve_package (wfl, &q);
9233 *where_found = decl = resolve_no_layout (name, qual_wfl);
9234 /* We want to be absolutely sure that the class is laid
9235 out. We're going to search something inside it. */
9236 *type_found = type = TREE_TYPE (decl);
9237 layout_class (type);
9240 /* Fix them all the way down, if any are left. */
9243 list = TREE_CHAIN (q);
9246 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9247 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9248 list = TREE_CHAIN (list);
9254 if (from_super || from_cast)
9256 ((from_cast ? qual_wfl : wfl),
9257 "No variable `%s' defined in class `%s'",
9258 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9259 lang_printable_name (type, 0));
9262 (qual_wfl, "Undefined variable or class name: `%s'",
9263 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9268 /* We have a type name. It's been already resolved when the
9269 expression was qualified. */
9270 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9272 if (!(decl = QUAL_RESOLUTION (q)))
9273 return 1; /* Error reported already */
9275 /* Sneak preview. If next we see a `new', we're facing a
9276 qualification with resulted in a type being selected
9277 instead of a field. Report the error */
9279 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9281 parse_error_context (qual_wfl, "Undefined variable `%s'",
9282 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9286 if (not_accessible_p (TREE_TYPE (decl), decl, type, 0))
9289 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9290 java_accstring_lookup (get_access_flags_from_decl (decl)),
9291 GET_TYPE_NAME (type),
9292 IDENTIFIER_POINTER (DECL_NAME (decl)),
9293 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9296 check_deprecation (qual_wfl, decl);
9298 type = TREE_TYPE (decl);
9301 /* We resolve and expression name */
9304 tree field_decl = NULL_TREE;
9306 /* If there exists an early resolution, use it. That occurs
9307 only once and we know that there are more things to
9308 come. Don't do that when processing something after SUPER
9309 (we need more thing to be put in place below */
9310 if (!from_super && QUAL_RESOLUTION (q))
9312 decl = QUAL_RESOLUTION (q);
9315 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9318 *where_found = current_this;
9321 static_ref_err (qual_wfl, DECL_NAME (decl),
9325 if (outer_field_access_p (current_class, decl))
9326 decl = build_outer_field_access (qual_wfl, decl);
9330 *where_found = TREE_TYPE (decl);
9331 if (TREE_CODE (*where_found) == POINTER_TYPE)
9332 *where_found = TREE_TYPE (*where_found);
9337 /* We have to search for a field, knowing the type of its
9338 container. The flag FROM_TYPE indicates that we resolved
9339 the last member of the expression as a type name, which
9340 means that for the resolution of this field, we'll look
9341 for other errors than if it was resolved as a member of
9346 tree field_decl_type; /* For layout */
9348 if (!from_type && !JREFERENCE_TYPE_P (type))
9351 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9352 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9353 lang_printable_name (type, 0),
9354 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9358 field_decl = lookup_field_wrapper (type,
9359 EXPR_WFL_NODE (qual_wfl));
9361 /* Maybe what we're trying to access to is an inner
9362 class, only if decl is a TYPE_DECL. */
9363 if (!field_decl && TREE_CODE (decl) == TYPE_DECL)
9365 tree ptr, inner_decl;
9367 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9368 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9371 check_inner_class_access (inner_decl, decl, qual_wfl);
9372 type = TREE_TYPE (inner_decl);
9379 if (field_decl == NULL_TREE)
9382 (qual_wfl, "No variable `%s' defined in type `%s'",
9383 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9384 GET_TYPE_NAME (type));
9387 if (field_decl == error_mark_node)
9390 /* Layout the type of field_decl, since we may need
9391 it. Don't do primitive types or loaded classes. The
9392 situation of non primitive arrays may not handled
9393 properly here. FIXME */
9394 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9395 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9397 field_decl_type = TREE_TYPE (field_decl);
9398 if (!JPRIMITIVE_TYPE_P (field_decl_type)
9399 && !CLASS_LOADED_P (field_decl_type)
9400 && !TYPE_ARRAY_P (field_decl_type))
9401 resolve_and_layout (field_decl_type, NULL_TREE);
9402 if (TYPE_ARRAY_P (field_decl_type))
9403 CLASS_LOADED_P (field_decl_type) = 1;
9405 /* Check on accessibility here */
9406 if (not_accessible_p (current_class, field_decl,
9407 TREE_TYPE (decl), from_super))
9411 "Can't access %s field `%s.%s' from `%s'",
9412 java_accstring_lookup
9413 (get_access_flags_from_decl (field_decl)),
9414 GET_TYPE_NAME (type),
9415 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9417 (DECL_NAME (TYPE_NAME (current_class))));
9420 check_deprecation (qual_wfl, field_decl);
9422 /* There are things to check when fields are accessed
9423 from type. There are no restrictions on a static
9424 declaration of the field when it is accessed from an
9426 is_static = FIELD_STATIC (field_decl);
9427 if (!from_super && from_type
9428 && !TYPE_INTERFACE_P (type)
9430 && (current_function_decl
9431 && METHOD_STATIC (current_function_decl)))
9433 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9436 from_cast = from_super = 0;
9438 /* It's an access from a type but it isn't static, we
9439 make it relative to `this'. */
9440 if (!is_static && from_type)
9441 decl = current_this;
9443 /* If we need to generate something to get a proper
9444 handle on what this field is accessed from, do it
9448 decl = maybe_access_field (decl, *where_found, *type_found);
9449 if (decl == error_mark_node)
9453 /* We want to keep the location were found it, and the type
9455 *where_found = decl;
9458 /* Generate the correct expression for field access from
9460 if (from_qualified_this)
9462 field_decl = build_outer_field_access (qual_wfl, field_decl);
9463 from_qualified_this = 0;
9466 /* This is the decl found and eventually the next one to
9471 type = QUAL_DECL_TYPE (decl);
9473 /* Sneak preview. If decl is qualified by a `new', report
9474 the error here to be accurate on the peculiar construct */
9476 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9477 && !JREFERENCE_TYPE_P (type))
9479 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9480 lang_printable_name (type, 0));
9484 /* `q' might have changed due to a after package resolution
9493 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9494 can't be accessed from REFERENCE (a record type). If MEMBER
9495 features a protected access, we then use WHERE which, if non null,
9496 holds the type of MEMBER's access that is checked against
9497 6.6.2.1. This function should be used when decl is a field or a
9501 not_accessible_p (reference, member, where, from_super)
9502 tree reference, member;
9506 int access_flag = get_access_flags_from_decl (member);
9508 /* Inner classes are processed by check_inner_class_access */
9509 if (INNER_CLASS_TYPE_P (reference))
9512 /* Access always granted for members declared public */
9513 if (access_flag & ACC_PUBLIC)
9516 /* Check access on protected members */
9517 if (access_flag & ACC_PROTECTED)
9519 /* Access granted if it occurs from within the package
9520 containing the class in which the protected member is
9522 if (class_in_current_package (DECL_CONTEXT (member)))
9525 /* If accessed with the form `super.member', then access is granted */
9529 /* If where is active, access was made through a
9530 qualifier. Access is granted if the type of the qualifier is
9531 or is a sublass of the type the access made from (6.6.2.1.) */
9532 if (where && !inherits_from_p (where, reference))
9535 /* Otherwise, access is granted if occuring from the class where
9536 member is declared or a subclass of it. Find the right
9537 context to perform the check */
9538 if (PURE_INNER_CLASS_TYPE_P (reference))
9540 while (INNER_CLASS_TYPE_P (reference))
9542 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9544 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9547 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9552 /* Check access on private members. Access is granted only if it
9553 occurs from within the class in which it is declared -- that does
9554 it for innerclasses too. */
9555 if (access_flag & ACC_PRIVATE)
9557 if (reference == DECL_CONTEXT (member))
9559 if (enclosing_context_p (reference, DECL_CONTEXT (member)))
9564 /* Default access are permitted only when occuring within the
9565 package in which the type (REFERENCE) is declared. In other words,
9566 REFERENCE is defined in the current package */
9568 return !class_in_current_package (reference);
9570 /* Otherwise, access is granted */
9574 /* Test deprecated decl access. */
9576 check_deprecation (wfl, decl)
9579 const char *file = DECL_SOURCE_FILE (decl);
9580 /* Complain if the field is deprecated and the file it was defined
9581 in isn't compiled at the same time the file which contains its
9583 if (DECL_DEPRECATED (decl)
9584 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9587 switch (TREE_CODE (decl))
9590 strcpy (the, "method");
9593 strcpy (the, "field");
9596 strcpy (the, "class");
9601 parse_warning_context
9602 (wfl, "The %s `%s' in class `%s' has been deprecated",
9603 the, lang_printable_name (decl, 0),
9604 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9608 /* Returns 1 if class was declared in the current package, 0 otherwise */
9611 class_in_current_package (class)
9614 static tree cache = NULL_TREE;
9621 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9623 /* If the current package is empty and the name of CLASS is
9624 qualified, class isn't in the current package. If there is a
9625 current package and the name of the CLASS is not qualified, class
9626 isn't in the current package */
9627 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
9630 /* If there is not package and the name of CLASS isn't qualified,
9631 they belong to the same unnamed package */
9632 if (!ctxp->package && !qualified_flag)
9635 /* Compare the left part of the name of CLASS with the package name */
9636 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9637 if (ctxp->package == left)
9639 static int initialized_p;
9640 /* Register CACHE with the garbage collector. */
9643 ggc_add_tree_root (&cache, 1);
9653 /* This function may generate code to access DECL from WHERE. This is
9654 done only if certain conditions meet. */
9657 maybe_access_field (decl, where, type)
9658 tree decl, where, type;
9660 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9661 && !FIELD_STATIC (decl))
9662 decl = build_field_ref (where ? where : current_this,
9663 (type ? type : DECL_CONTEXT (decl)),
9668 /* Build a method invocation, by patching PATCH. If non NULL
9669 and according to the situation, PRIMARY and WHERE may be
9670 used. IS_STATIC is set to 1 if the invoked function is static. */
9673 patch_method_invocation (patch, primary, where, from_super,
9674 is_static, ret_decl)
9675 tree patch, primary, where;
9680 tree wfl = TREE_OPERAND (patch, 0);
9681 tree args = TREE_OPERAND (patch, 1);
9682 tree name = EXPR_WFL_NODE (wfl);
9684 int is_static_flag = 0;
9685 int is_super_init = 0;
9686 tree this_arg = NULL_TREE;
9687 int is_array_clone_call = 0;
9689 /* Should be overriden if everything goes well. Otherwise, if
9690 something fails, it should keep this value. It stop the
9691 evaluation of a bogus assignment. See java_complete_tree,
9692 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9693 evaluating an assignment */
9694 TREE_TYPE (patch) = error_mark_node;
9696 /* Since lookup functions are messing with line numbers, save the
9698 java_parser_context_save_global ();
9700 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9702 /* Resolution of qualified name, excluding constructors */
9703 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9705 tree identifier, identifier_wfl, type, resolved;
9706 /* Extract the last IDENTIFIER of the qualified
9707 expression. This is a wfl and we will use it's location
9708 data during error report. */
9709 identifier_wfl = cut_identifier_in_qualified (wfl);
9710 identifier = EXPR_WFL_NODE (identifier_wfl);
9712 /* Given the context, IDENTIFIER is syntactically qualified
9713 as a MethodName. We need to qualify what's before */
9714 qualify_ambiguous_name (wfl);
9715 resolved = resolve_field_access (wfl, NULL, NULL);
9717 if (resolved == error_mark_node)
9718 PATCH_METHOD_RETURN_ERROR ();
9720 type = GET_SKIP_TYPE (resolved);
9721 resolve_and_layout (type, NULL_TREE);
9723 if (JPRIMITIVE_TYPE_P (type))
9727 "Can't invoke a method on primitive type `%s'",
9728 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9729 PATCH_METHOD_RETURN_ERROR ();
9732 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9733 args = nreverse (args);
9735 /* We're resolving a call from a type */
9736 if (TREE_CODE (resolved) == TYPE_DECL)
9738 if (CLASS_INTERFACE (resolved))
9742 "Can't make static reference to method `%s' in interface `%s'",
9743 IDENTIFIER_POINTER (identifier),
9744 IDENTIFIER_POINTER (name));
9745 PATCH_METHOD_RETURN_ERROR ();
9747 if (list && !METHOD_STATIC (list))
9749 char *fct_name = xstrdup (lang_printable_name (list, 0));
9752 "Can't make static reference to method `%s %s' in class `%s'",
9753 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9754 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9756 PATCH_METHOD_RETURN_ERROR ();
9760 this_arg = primary = resolved;
9762 if (TYPE_ARRAY_P (type) && identifier == get_identifier ("clone"))
9763 is_array_clone_call = 1;
9765 /* IDENTIFIER_WFL will be used to report any problem further */
9766 wfl = identifier_wfl;
9768 /* Resolution of simple names, names generated after a primary: or
9772 tree class_to_search = NULL_TREE;
9773 int lc; /* Looking for Constructor */
9775 /* We search constructor in their target class */
9776 if (CALL_CONSTRUCTOR_P (patch))
9778 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9779 class_to_search = EXPR_WFL_NODE (wfl);
9780 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9781 this_identifier_node)
9782 class_to_search = NULL_TREE;
9783 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9784 super_identifier_node)
9787 if (CLASSTYPE_SUPER (current_class))
9789 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9792 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
9793 PATCH_METHOD_RETURN_ERROR ();
9797 /* Class to search is NULL if we're searching the current one */
9798 if (class_to_search)
9800 class_to_search = resolve_and_layout (class_to_search, wfl);
9802 if (!class_to_search)
9805 (wfl, "Class `%s' not found in type declaration",
9806 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9807 PATCH_METHOD_RETURN_ERROR ();
9810 /* Can't instantiate an abstract class, but we can
9811 invoke it's constructor. It's use within the `new'
9812 context is denied here. */
9813 if (CLASS_ABSTRACT (class_to_search)
9814 && TREE_CODE (patch) == NEW_CLASS_EXPR)
9817 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9818 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9819 PATCH_METHOD_RETURN_ERROR ();
9822 class_to_search = TREE_TYPE (class_to_search);
9825 class_to_search = current_class;
9828 /* This is a regular search in the local class, unless an
9829 alternate class is specified. */
9832 class_to_search = (where ? where : current_class);
9836 /* NAME is a simple identifier or comes from a primary. Search
9837 in the class whose declaration contain the method being
9839 resolve_and_layout (class_to_search, NULL_TREE);
9841 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
9842 /* Don't continue if no method were found, as the next statement
9843 can't be executed then. */
9845 PATCH_METHOD_RETURN_ERROR ();
9847 if (TYPE_ARRAY_P (class_to_search)
9848 && DECL_NAME (list) == get_identifier ("clone"))
9849 is_array_clone_call = 1;
9851 /* Check for static reference if non static methods */
9852 if (check_for_static_method_reference (wfl, patch, list,
9853 class_to_search, primary))
9854 PATCH_METHOD_RETURN_ERROR ();
9856 /* Check for inner classes creation from illegal contexts */
9857 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9858 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9859 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9862 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9863 lang_printable_name (class_to_search, 0),
9864 (!current_this ? "" :
9865 "; an explicit one must be provided when creating this inner class"));
9866 PATCH_METHOD_RETURN_ERROR ();
9869 /* Non static methods are called with the current object extra
9870 argument. If patch a `new TYPE()', the argument is the value
9871 returned by the object allocator. If method is resolved as a
9872 primary, use the primary otherwise use the current THIS. */
9873 args = nreverse (args);
9874 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
9876 this_arg = primary ? primary : current_this;
9878 /* If we're using an access method, things are different.
9879 There are two familly of cases:
9881 1) We're not generating bytecodes:
9883 - LIST is non static. It's invocation is transformed from
9884 x(a1,...,an) into this$<n>.x(a1,....an).
9885 - LIST is static. It's invocation is transformed from
9886 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9888 2) We're generating bytecodes:
9890 - LIST is non static. It's invocation is transformed from
9891 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9892 - LIST is static. It's invocation is transformed from
9893 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9895 Of course, this$<n> can be abitrary complex, ranging from
9896 this$0 (the immediate outer context) to
9897 access$0(access$0(...(this$0))).
9899 maybe_use_access_method returns a non zero value if the
9900 this_arg has to be moved into the (then generated) stub
9901 argument list. In the meantime, the selected function
9902 might have be replaced by a generated stub. */
9903 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9905 args = tree_cons (NULL_TREE, this_arg, args);
9906 this_arg = NULL_TREE; /* So it doesn't get chained twice */
9911 /* Merge point of all resolution schemes. If we have nothing, this
9912 is an error, already signaled */
9914 PATCH_METHOD_RETURN_ERROR ();
9916 /* Check accessibility, position the is_static flag, build and
9918 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list,
9919 (primary ? TREE_TYPE (TREE_TYPE (primary)) :
9920 NULL_TREE), from_super)
9921 /* Calls to clone() on array types are permitted as a special-case. */
9922 && !is_array_clone_call)
9924 char *fct_name = (char *) IDENTIFIER_POINTER (DECL_NAME (list));
9925 char *access = java_accstring_lookup (get_access_flags_from_decl (list));
9926 char *klass = (char *) IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list))));
9927 char *refklass = (char *) IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
9928 char *what = (char *) (DECL_CONSTRUCTOR_P (list)
9929 ? "constructor" : "method");
9930 /* FIXME: WFL yields the wrong message here but I don't know
9931 what else to use. */
9932 parse_error_context (wfl,
9933 "Can't access %s %s `%s.%s' from `%s'",
9934 access, what, klass, fct_name, refklass);
9935 PATCH_METHOD_RETURN_ERROR ();
9937 check_deprecation (wfl, list);
9939 /* If invoking a innerclass constructor, there are hidden parameters
9941 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9942 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9944 /* And make sure we add the accessed local variables to be saved
9945 in field aliases. */
9946 args = build_alias_initializer_parameter_list
9947 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9949 /* Secretly pass the current_this/primary as a second argument */
9950 if (primary || current_this)
9953 tree this_type = (current_this ?
9954 TREE_TYPE (TREE_TYPE (current_this)) : NULL_TREE);
9955 /* Method's (list) enclosing context */
9956 tree mec = DECL_CONTEXT (TYPE_NAME (DECL_CONTEXT (list)));
9957 /* If we have a primary, use it. */
9959 extra_arg = primary;
9960 /* The current `this' is an inner class but isn't a direct
9961 enclosing context for the inner class we're trying to
9962 create. Build an access to the proper enclosing context
9964 else if (current_this && PURE_INNER_CLASS_TYPE_P (this_type)
9965 && this_type != TREE_TYPE (mec))
9968 extra_arg = build_access_to_thisn (current_class,
9969 TREE_TYPE (mec), 0);
9970 extra_arg = java_complete_tree (extra_arg);
9972 /* Otherwise, just use the current `this' as an enclosing
9975 extra_arg = current_this;
9976 args = tree_cons (NULL_TREE, extra_arg, args);
9979 args = tree_cons (NULL_TREE, integer_zero_node, args);
9982 /* This handles the situation where a constructor invocation needs
9983 to have an enclosing context passed as a second parameter (the
9984 constructor is one of an inner class. We extract it from the
9985 current function. */
9986 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9988 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
9991 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
9993 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
9994 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
9998 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
10000 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
10001 extra_arg = java_complete_tree (extra_arg);
10003 args = tree_cons (NULL_TREE, extra_arg, args);
10006 is_static_flag = METHOD_STATIC (list);
10007 if (! is_static_flag && this_arg != NULL_TREE)
10008 args = tree_cons (NULL_TREE, this_arg, args);
10010 /* In the context of an explicit constructor invocation, we can't
10011 invoke any method relying on `this'. Exceptions are: we're
10012 invoking a static function, primary exists and is not the current
10013 this, we're creating a new object. */
10014 if (ctxp->explicit_constructor_p
10016 && (!primary || primary == current_this)
10017 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
10019 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
10020 PATCH_METHOD_RETURN_ERROR ();
10022 java_parser_context_restore_global ();
10024 *is_static = is_static_flag;
10025 /* Sometimes, we want the decl of the selected method. Such as for
10029 patch = patch_invoke (patch, list, args);
10030 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
10032 tree finit_parms, finit_call;
10034 /* Prepare to pass hidden parameters to finit$, if any. */
10035 finit_parms = build_alias_initializer_parameter_list
10036 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
10039 build_method_invocation (build_wfl_node (finit_identifier_node),
10042 /* Generate the code used to initialize fields declared with an
10043 initialization statement and build a compound statement along
10044 with the super constructor invocation. */
10045 patch = build (COMPOUND_EXPR, void_type_node, patch,
10046 java_complete_tree (finit_call));
10047 CAN_COMPLETE_NORMALLY (patch) = 1;
10052 /* Check that we're not trying to do a static reference to a method in
10053 non static method. Return 1 if it's the case, 0 otherwise. */
10056 check_for_static_method_reference (wfl, node, method, where, primary)
10057 tree wfl, node, method, where, primary;
10059 if (METHOD_STATIC (current_function_decl)
10060 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
10062 char *fct_name = xstrdup (lang_printable_name (method, 0));
10063 parse_error_context
10064 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
10065 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
10066 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
10073 /* Fix the invocation of *MDECL if necessary in the case of a
10074 invocation from an inner class. *THIS_ARG might be modified
10075 appropriately and an alternative access to *MDECL might be
10079 maybe_use_access_method (is_super_init, mdecl, this_arg)
10081 tree *mdecl, *this_arg;
10084 tree md = *mdecl, ta = *this_arg;
10086 int non_static_context = !METHOD_STATIC (md);
10089 || DECL_CONTEXT (md) == current_class
10090 || !PURE_INNER_CLASS_TYPE_P (current_class)
10091 || DECL_FINIT_P (md))
10094 /* If we're calling a method found in an enclosing class, generate
10095 what it takes to retrieve the right this. Don't do that if we're
10096 invoking a static method. Note that if MD's type is unrelated to
10097 CURRENT_CLASS, then the current this can be used. */
10099 if (non_static_context && DECL_CONTEXT (md) != object_type_node)
10101 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10102 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10104 ta = build_current_thisn (current_class);
10105 ta = build_wfl_node (ta);
10112 maybe_build_thisn_access_method (type);
10113 if (inherits_from_p (type, DECL_CONTEXT (md)))
10115 ta = build_access_to_thisn (ctx, type, 0);
10118 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10119 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10122 ta = java_complete_tree (ta);
10125 /* We might have to use an access method to get to MD. We can
10126 break the method access rule as far as we're not generating
10128 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10130 md = build_outer_method_access_method (md);
10137 /* Returnin a non zero value indicates we were doing a non static
10138 method invokation that is now a static invocation. It will have
10139 callee displace `this' to insert it in the regular argument
10141 return (non_static_context && to_return);
10144 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10148 patch_invoke (patch, method, args)
10149 tree patch, method, args;
10152 tree original_call, t, ta;
10153 tree cond = NULL_TREE;
10155 /* Last step for args: convert build-in types. If we're dealing with
10156 a new TYPE() type call, the first argument to the constructor
10157 isn't found in the incoming argument list, but delivered by
10159 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10160 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10161 t = TREE_CHAIN (t);
10162 for (ta = args; t != end_params_node && ta;
10163 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10164 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10165 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10166 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10168 /* Resolve unresolved returned type isses */
10169 t = TREE_TYPE (TREE_TYPE (method));
10170 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10171 resolve_and_layout (TREE_TYPE (t), NULL);
10173 if (flag_emit_class_files || flag_emit_xref)
10177 tree signature = build_java_signature (TREE_TYPE (method));
10178 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10180 case INVOKE_VIRTUAL:
10181 dtable = invoke_build_dtable (0, args);
10182 func = build_invokevirtual (dtable, method);
10185 case INVOKE_NONVIRTUAL:
10186 /* If the object for the method call is null, we throw an
10187 exception. We don't do this if the object is the current
10188 method's `this'. In other cases we just rely on an
10189 optimization pass to eliminate redundant checks. */
10190 if (TREE_VALUE (args) != current_this)
10192 /* We use a SAVE_EXPR here to make sure we only evaluate
10193 the new `self' expression once. */
10194 tree save_arg = save_expr (TREE_VALUE (args));
10195 TREE_VALUE (args) = save_arg;
10196 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10197 null_pointer_node);
10199 /* Fall through. */
10202 case INVOKE_STATIC:
10203 func = build_known_method_ref (method, TREE_TYPE (method),
10204 DECL_CONTEXT (method),
10208 case INVOKE_INTERFACE:
10209 dtable = invoke_build_dtable (1, args);
10210 func = build_invokeinterface (dtable, method);
10217 /* Ensure self_type is initialized, (invokestatic). FIXME */
10218 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10221 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10222 TREE_OPERAND (patch, 0) = func;
10223 TREE_OPERAND (patch, 1) = args;
10224 original_call = patch;
10226 /* We're processing a `new TYPE ()' form. New is called and its
10227 returned value is the first argument to the constructor. We build
10228 a COMPOUND_EXPR and use saved expression so that the overall NEW
10229 expression value is a pointer to a newly created and initialized
10231 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10233 tree class = DECL_CONTEXT (method);
10234 tree c1, saved_new, size, new;
10235 if (flag_emit_class_files || flag_emit_xref)
10237 TREE_TYPE (patch) = build_pointer_type (class);
10240 if (!TYPE_SIZE (class))
10241 safe_layout_class (class);
10242 size = size_in_bytes (class);
10243 new = build (CALL_EXPR, promote_type (class),
10244 build_address_of (alloc_object_node),
10245 tree_cons (NULL_TREE, build_class_ref (class),
10246 build_tree_list (NULL_TREE,
10247 size_in_bytes (class))),
10249 saved_new = save_expr (new);
10250 c1 = build_tree_list (NULL_TREE, saved_new);
10251 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10252 TREE_OPERAND (original_call, 1) = c1;
10253 TREE_SET_CODE (original_call, CALL_EXPR);
10254 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10257 /* If COND is set, then we are building a check to see if the object
10259 if (cond != NULL_TREE)
10261 /* We have to make the `then' branch a compound expression to
10262 make the types turn out right. This seems bizarre. */
10263 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10264 build (COMPOUND_EXPR, TREE_TYPE (patch),
10265 build (CALL_EXPR, void_type_node,
10266 build_address_of (soft_nullpointer_node),
10267 NULL_TREE, NULL_TREE),
10268 (FLOAT_TYPE_P (TREE_TYPE (patch))
10269 ? build_real (TREE_TYPE (patch), dconst0)
10270 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10271 integer_zero_node))),
10273 TREE_SIDE_EFFECTS (patch) = 1;
10280 invocation_mode (method, super)
10284 int access = get_access_flags_from_decl (method);
10287 return INVOKE_SUPER;
10289 if (access & ACC_STATIC)
10290 return INVOKE_STATIC;
10292 /* We have to look for a constructor before we handle nonvirtual
10293 calls; otherwise the constructor will look nonvirtual. */
10294 if (DECL_CONSTRUCTOR_P (method))
10295 return INVOKE_STATIC;
10297 if (access & ACC_FINAL || access & ACC_PRIVATE)
10298 return INVOKE_NONVIRTUAL;
10300 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10301 return INVOKE_NONVIRTUAL;
10303 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10304 return INVOKE_INTERFACE;
10306 return INVOKE_VIRTUAL;
10309 /* Retrieve a refined list of matching methods. It covers the step
10310 15.11.2 (Compile-Time Step 2) */
10313 lookup_method_invoke (lc, cl, class, name, arg_list)
10316 tree class, name, arg_list;
10318 tree atl = end_params_node; /* Arg Type List */
10319 tree method, signature, list, node;
10320 const char *candidates; /* Used for error report */
10323 /* Fix the arguments */
10324 for (node = arg_list; node; node = TREE_CHAIN (node))
10326 tree current_arg = TREE_TYPE (TREE_VALUE (node));
10327 /* Non primitive type may have to be resolved */
10328 if (!JPRIMITIVE_TYPE_P (current_arg))
10329 resolve_and_layout (current_arg, NULL_TREE);
10331 if (TREE_CODE (current_arg) == RECORD_TYPE)
10332 current_arg = promote_type (current_arg);
10333 atl = tree_cons (NULL_TREE, current_arg, atl);
10336 /* Presto. If we're dealing with an anonymous class and a
10337 constructor call, generate the right constructor now, since we
10338 know the arguments' types. */
10340 if (lc && ANONYMOUS_CLASS_P (class))
10341 craft_constructor (TYPE_NAME (class), atl);
10343 /* Find all candidates and then refine the list, searching for the
10344 most specific method. */
10345 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10346 list = find_most_specific_methods_list (list);
10347 if (list && !TREE_CHAIN (list))
10348 return TREE_VALUE (list);
10350 /* Issue an error. List candidates if any. Candidates are listed
10351 only if accessible (non accessible methods may end-up here for
10352 the sake of a better error report). */
10357 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10358 for (current = list; current; current = TREE_CHAIN (current))
10360 tree cm = TREE_VALUE (current);
10361 char string [4096];
10362 if (!cm || not_accessible_p (class, cm, NULL_TREE, 0))
10365 (string, " `%s' in `%s'%s",
10366 get_printable_method_name (cm),
10367 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10368 (TREE_CHAIN (current) ? "\n" : ""));
10369 obstack_grow (&temporary_obstack, string, strlen (string));
10371 obstack_1grow (&temporary_obstack, '\0');
10372 candidates = obstack_finish (&temporary_obstack);
10374 /* Issue the error message */
10375 method = make_node (FUNCTION_TYPE);
10376 TYPE_ARG_TYPES (method) = atl;
10377 signature = build_java_argument_signature (method);
10378 dup = xstrdup (lang_printable_name (class, 0));
10379 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10380 (lc ? "constructor" : "method"),
10381 (lc ? dup : IDENTIFIER_POINTER (name)),
10382 IDENTIFIER_POINTER (signature), dup,
10383 (candidates ? candidates : ""));
10388 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10389 when we're looking for a constructor. */
10392 find_applicable_accessible_methods_list (lc, class, name, arglist)
10394 tree class, name, arglist;
10396 static struct hash_table t, *searched_classes = NULL;
10397 static int search_not_done = 0;
10398 tree list = NULL_TREE, all_list = NULL_TREE;
10400 /* Check the hash table to determine if this class has been searched
10402 if (searched_classes)
10404 if (hash_lookup (searched_classes,
10405 (const hash_table_key) class, FALSE, NULL))
10410 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10411 java_hash_compare_tree_node);
10412 searched_classes = &t;
10416 hash_lookup (searched_classes,
10417 (const hash_table_key) class, TRUE, NULL);
10419 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10421 load_class (class, 1);
10422 safe_layout_class (class);
10425 /* Search interfaces */
10426 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10427 && CLASS_INTERFACE (TYPE_NAME (class)))
10430 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10431 search_applicable_methods_list (lc, TYPE_METHODS (class),
10432 name, arglist, &list, &all_list);
10433 n = TREE_VEC_LENGTH (basetype_vec);
10434 for (i = 1; i < n; i++)
10436 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10439 rlist = find_applicable_accessible_methods_list (lc, t, name,
10441 list = chainon (rlist, list);
10444 /* Search classes */
10448 int seen_inner_class = 0;
10449 search_applicable_methods_list (lc, TYPE_METHODS (class),
10450 name, arglist, &list, &all_list);
10452 /* When looking finit$ or class$, we turn LC to 1 so that we
10453 only search in class. Note that we should have found
10454 something at this point. */
10455 if (ID_FINIT_P (name) || ID_CLASSDOLLAR_P (name))
10462 /* We must search all interfaces of this class */
10465 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10466 int n = TREE_VEC_LENGTH (basetype_vec), i;
10467 for (i = 1; i < n; i++)
10469 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10470 if (t != object_type_node)
10473 = find_applicable_accessible_methods_list (lc, t,
10475 list = chainon (rlist, list);
10480 /* Search enclosing context of inner classes before looking
10482 while (!lc && INNER_CLASS_TYPE_P (class))
10485 seen_inner_class = 1;
10486 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
10487 rlist = find_applicable_accessible_methods_list (lc, class,
10489 list = chainon (rlist, list);
10492 if (!lc && seen_inner_class
10493 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10494 class = CLASSTYPE_SUPER (sc);
10498 /* Search superclass */
10499 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10502 class = CLASSTYPE_SUPER (class);
10503 rlist = find_applicable_accessible_methods_list (lc, class,
10505 list = chainon (rlist, list);
10511 /* We're done. Reset the searched classes list and finally search
10512 java.lang.Object if it wasn't searched already. */
10513 if (!search_not_done)
10516 && TYPE_METHODS (object_type_node)
10517 && !hash_lookup (searched_classes,
10518 (const hash_table_key) object_type_node,
10521 search_applicable_methods_list (lc,
10522 TYPE_METHODS (object_type_node),
10523 name, arglist, &list, &all_list);
10525 hash_table_free (searched_classes);
10526 searched_classes = NULL;
10529 /* Either return the list obtained or all selected (but
10530 inaccessible) methods for better error report. */
10531 return (!list ? all_list : list);
10534 /* Effectively search for the appropriate method in method */
10537 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
10539 tree method, name, arglist;
10540 tree *list, *all_list;
10542 for (; method; method = TREE_CHAIN (method))
10544 /* When dealing with constructor, stop here, otherwise search
10546 if (lc && !DECL_CONSTRUCTOR_P (method))
10548 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10549 || (DECL_NAME (method) != name)))
10552 if (argument_types_convertible (method, arglist))
10554 /* Retain accessible methods only */
10555 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10556 method, NULL_TREE, 0))
10557 *list = tree_cons (NULL_TREE, method, *list);
10559 /* Also retain all selected method here */
10560 *all_list = tree_cons (NULL_TREE, method, *list);
10565 /* 15.11.2.2 Choose the Most Specific Method */
10568 find_most_specific_methods_list (list)
10572 int abstract, candidates;
10573 tree current, new_list = NULL_TREE;
10574 for (current = list; current; current = TREE_CHAIN (current))
10577 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10579 for (method = list; method; method = TREE_CHAIN (method))
10581 tree method_v, current_v;
10582 /* Don't test a method against itself */
10583 if (method == current)
10586 method_v = TREE_VALUE (method);
10587 current_v = TREE_VALUE (current);
10589 /* Compare arguments and location where methods where declared */
10590 if (argument_types_convertible (method_v, current_v))
10592 if (valid_method_invocation_conversion_p
10593 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10594 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10595 && enclosing_context_p (DECL_CONTEXT (method_v),
10596 DECL_CONTEXT (current_v))))
10598 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10599 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10600 max = (v > max ? v : max);
10606 /* Review the list and select the maximally specific methods */
10607 for (current = list, abstract = -1, candidates = -1;
10608 current; current = TREE_CHAIN (current))
10609 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10611 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10612 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10616 /* If we have several and they're all abstract, just pick the
10618 if (candidates > 0 && (candidates == abstract))
10620 new_list = nreverse (new_list);
10621 TREE_CHAIN (new_list) = NULL_TREE;
10624 /* We have several (we couldn't find a most specific), all but one
10625 are abstract, we pick the only non abstract one. */
10626 if (candidates > 0 && (candidates == abstract+1))
10628 for (current = new_list; current; current = TREE_CHAIN (current))
10629 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10631 TREE_CHAIN (current) = NULL_TREE;
10632 new_list = current;
10636 /* If we can't find one, lower expectations and try to gather multiple
10637 maximally specific methods */
10638 while (!new_list && max)
10642 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10643 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10650 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
10651 converted by method invocation conversion (5.3) to the type of the
10652 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10653 to change less often than M1. */
10656 argument_types_convertible (m1, m2_or_arglist)
10657 tree m1, m2_or_arglist;
10659 static tree m2_arg_value = NULL_TREE;
10660 static tree m2_arg_cache = NULL_TREE;
10661 static int initialized_p;
10663 register tree m1_arg, m2_arg;
10665 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10667 if (!initialized_p)
10669 ggc_add_tree_root (&m2_arg_value, 1);
10670 ggc_add_tree_root (&m2_arg_cache, 1);
10674 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
10676 if (m2_arg_value == m2_or_arglist)
10677 m2_arg = m2_arg_cache;
10680 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10682 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10684 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10685 if (!METHOD_STATIC (m2_or_arglist))
10686 m2_arg = TREE_CHAIN (m2_arg);
10689 m2_arg = m2_or_arglist;
10691 m2_arg_value = m2_or_arglist;
10692 m2_arg_cache = m2_arg;
10695 while (m1_arg != end_params_node && m2_arg != end_params_node)
10697 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
10698 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10699 TREE_VALUE (m2_arg)))
10701 m1_arg = TREE_CHAIN (m1_arg);
10702 m2_arg = TREE_CHAIN (m2_arg);
10704 return m1_arg == end_params_node && m2_arg == end_params_node;
10707 /* Qualification routines */
10710 qualify_ambiguous_name (id)
10713 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10714 saved_current_class;
10715 int again, super_found = 0, this_found = 0, new_array_found = 0;
10718 /* We first qualify the first element, then derive qualification of
10719 others based on the first one. If the first element is qualified
10720 by a resolution (field or type), this resolution is stored in the
10721 QUAL_RESOLUTION of the qual element being examined. We need to
10722 save the current_class since the use of SUPER might change the
10724 saved_current_class = current_class;
10725 qual = EXPR_WFL_QUALIFICATION (id);
10728 /* Simple qualified expression feature a qual_wfl that is a
10729 WFL. Expression derived from a primary feature more complicated
10730 things like a CALL_EXPR. Expression from primary need to be
10731 worked out to extract the part on which the qualification will
10733 qual_wfl = QUAL_WFL (qual);
10734 switch (TREE_CODE (qual_wfl))
10737 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10738 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10740 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10741 qual_wfl = QUAL_WFL (qual);
10744 case NEW_ARRAY_EXPR:
10745 case NEW_ANONYMOUS_ARRAY_EXPR:
10746 qual = TREE_CHAIN (qual);
10747 again = new_array_found = 1;
10751 case NEW_CLASS_EXPR:
10752 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10755 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10756 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10759 qual = TREE_CHAIN (qual);
10760 qual_wfl = QUAL_WFL (qual);
10762 case CLASS_LITERAL:
10763 qual = TREE_CHAIN (qual);
10764 qual_wfl = QUAL_WFL (qual);
10767 /* Fix for -Wall. Just break doing nothing */
10771 ptr_type = current_class;
10773 code = TREE_CODE (qual_wfl);
10775 /* Pos evaluation: non WFL leading expression nodes */
10776 if (code == CONVERT_EXPR
10777 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10778 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10780 else if (code == INTEGER_CST)
10783 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
10784 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10785 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10787 else if (code == TREE_LIST)
10788 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10790 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10791 || code == PLUS_EXPR)
10793 qual = TREE_CHAIN (qual);
10794 qual_wfl = QUAL_WFL (qual);
10799 name = EXPR_WFL_NODE (qual_wfl);
10802 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10807 /* If we have a THIS (from a primary), we set the context accordingly */
10808 if (name == this_identifier_node)
10810 /* This isn't really elegant. One more added irregularity
10811 before I start using COMPONENT_REF (hopefully very soon.) */
10812 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10813 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10814 EXPR_WITH_FILE_LOCATION
10815 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10816 this_identifier_node)
10818 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10819 qual = EXPR_WFL_QUALIFICATION (qual);
10821 qual = TREE_CHAIN (qual);
10822 qual_wfl = QUAL_WFL (qual);
10823 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10826 name = EXPR_WFL_NODE (qual_wfl);
10829 /* If we have a SUPER, we set the context accordingly */
10830 if (name == super_identifier_node)
10832 current_class = CLASSTYPE_SUPER (ptr_type);
10833 /* Check that there is such a thing as a super class. If not,
10834 return. The error will be caught later on, during the
10836 if (!current_class)
10838 current_class = saved_current_class;
10841 qual = TREE_CHAIN (qual);
10842 /* Do one more interation to set things up */
10843 super_found = again = 1;
10847 /* If name appears within the scope of a local variable declaration
10848 or parameter declaration, then it is an expression name. We don't
10849 carry this test out if we're in the context of the use of SUPER
10851 if (!this_found && !super_found
10852 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10853 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
10855 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10856 QUAL_RESOLUTION (qual) = decl;
10859 /* If within the class/interface NAME was found to be used there
10860 exists a (possibly inherited) field named NAME, then this is an
10861 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10862 address length, it is OK. */
10863 else if ((decl = lookup_field_wrapper (ptr_type, name))
10864 || (new_array_found && name == length_identifier_node))
10866 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10867 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
10870 /* We reclassify NAME as yielding to a type name resolution if:
10871 - NAME is a class/interface declared within the compilation
10872 unit containing NAME,
10873 - NAME is imported via a single-type-import declaration,
10874 - NAME is declared in an another compilation unit of the package
10875 of the compilation unit containing NAME,
10876 - NAME is declared by exactly on type-import-on-demand declaration
10877 of the compilation unit containing NAME.
10878 - NAME is actually a STRING_CST. */
10879 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10880 || (decl = resolve_and_layout (name, NULL_TREE)))
10882 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10883 QUAL_RESOLUTION (qual) = decl;
10886 /* Method call, array references and cast are expression name */
10887 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
10888 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10889 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
10890 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10892 /* Check here that NAME isn't declared by more than one
10893 type-import-on-demand declaration of the compilation unit
10894 containing NAME. FIXME */
10896 /* Otherwise, NAME is reclassified as a package name */
10898 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10900 /* Propagate the qualification accross other components of the
10902 for (qual = TREE_CHAIN (qual); qual;
10903 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10905 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10906 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10908 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10911 /* Store the global qualification for the ambiguous part of ID back
10913 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10914 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10915 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10916 RESOLVE_TYPE_NAME_P (id) = 1;
10917 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10918 RESOLVE_PACKAGE_NAME_P (id) = 1;
10920 /* Restore the current class */
10921 current_class = saved_current_class;
10925 breakdown_qualified (left, right, source)
10926 tree *left, *right, source;
10929 int l = IDENTIFIER_LENGTH (source);
10931 base = alloca (l + 1);
10932 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10934 /* Breakdown NAME into REMAINDER . IDENTIFIER */
10936 while (*p != '.' && p != base)
10939 /* We didn't find a '.'. Return an error */
10945 *right = get_identifier (p+1);
10946 *left = get_identifier (base);
10951 /* Patch tree nodes in a function body. When a BLOCK is found, push
10952 local variable decls if present.
10953 Same as java_complete_lhs, but does resolve static finals to values. */
10956 java_complete_tree (node)
10959 node = java_complete_lhs (node);
10960 if (JDECL_P (node) && CLASS_FINAL_VARIABLE_P (node)
10961 && DECL_INITIAL (node) != NULL_TREE
10962 && !flag_emit_xref)
10964 tree value = DECL_INITIAL (node);
10965 DECL_INITIAL (node) = NULL_TREE;
10966 value = fold_constant_for_init (value, node);
10967 DECL_INITIAL (node) = value;
10968 if (value != NULL_TREE)
10970 /* fold_constant_for_init sometimes widen the original type
10971 of the constant (i.e. byte to int.) It's not desirable,
10972 especially if NODE is a function argument. */
10973 if (TREE_CODE (value) == INTEGER_CST
10974 && TREE_TYPE (node) != TREE_TYPE (value))
10975 return convert (TREE_TYPE (node), value);
10980 DECL_FIELD_FINAL_IUD (node) = 0;
10986 java_stabilize_reference (node)
10989 if (TREE_CODE (node) == COMPOUND_EXPR)
10991 tree op0 = TREE_OPERAND (node, 0);
10992 tree op1 = TREE_OPERAND (node, 1);
10993 TREE_OPERAND (node, 0) = save_expr (op0);
10994 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
10997 return stabilize_reference (node);
11000 /* Patch tree nodes in a function body. When a BLOCK is found, push
11001 local variable decls if present.
11002 Same as java_complete_tree, but does not resolve static finals to values. */
11005 java_complete_lhs (node)
11008 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
11011 /* CONVERT_EXPR always has its type set, even though it needs to be
11013 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
11016 /* The switch block implements cases processing container nodes
11017 first. Contained nodes are always written back. Leaves come
11018 next and return a value. */
11019 switch (TREE_CODE (node))
11023 /* 1- Block section.
11024 Set the local values on decl names so we can identify them
11025 faster when they're referenced. At that stage, identifiers
11026 are legal so we don't check for declaration errors. */
11027 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11029 DECL_CONTEXT (cn) = current_function_decl;
11030 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
11032 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
11033 CAN_COMPLETE_NORMALLY (node) = 1;
11036 tree stmt = BLOCK_EXPR_BODY (node);
11038 int error_seen = 0;
11039 if (TREE_CODE (stmt) == COMPOUND_EXPR)
11041 /* Re-order from (((A; B); C); ...; Z) to
11042 (A; (B; (C ; (...; Z)))).
11043 This makes it easier to scan the statements left-to-right
11044 without using recursion (which might overflow the stack
11045 if the block has many statements. */
11048 tree left = TREE_OPERAND (stmt, 0);
11049 if (TREE_CODE (left) != COMPOUND_EXPR)
11051 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
11052 TREE_OPERAND (left, 1) = stmt;
11055 BLOCK_EXPR_BODY (node) = stmt;
11058 /* Now do the actual complete, without deep recursion for
11060 ptr = &BLOCK_EXPR_BODY (node);
11061 while (TREE_CODE (*ptr) == COMPOUND_EXPR
11062 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
11064 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
11065 tree *next = &TREE_OPERAND (*ptr, 1);
11066 TREE_OPERAND (*ptr, 0) = cur;
11067 if (cur == empty_stmt_node)
11069 /* Optimization; makes it easier to detect empty bodies.
11070 Most useful for <clinit> with all-constant initializer. */
11074 if (TREE_CODE (cur) == ERROR_MARK)
11076 else if (! CAN_COMPLETE_NORMALLY (cur))
11081 if (TREE_CODE (wfl_op2) == BLOCK)
11082 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
11083 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
11084 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
11088 if (TREE_CODE (wfl_op2) != CASE_EXPR
11089 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
11090 unreachable_stmt_error (*ptr);
11094 *ptr = java_complete_tree (*ptr);
11096 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11097 return error_mark_node;
11098 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11100 /* Turn local bindings to null */
11101 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11102 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11104 TREE_TYPE (node) = void_type_node;
11107 /* 2- They are expressions but ultimately deal with statements */
11110 wfl_op1 = TREE_OPERAND (node, 0);
11111 COMPLETE_CHECK_OP_0 (node);
11112 /* 14.19 A throw statement cannot complete normally. */
11113 CAN_COMPLETE_NORMALLY (node) = 0;
11114 return patch_throw_statement (node, wfl_op1);
11116 case SYNCHRONIZED_EXPR:
11117 wfl_op1 = TREE_OPERAND (node, 0);
11118 return patch_synchronized_statement (node, wfl_op1);
11121 return patch_try_statement (node);
11123 case TRY_FINALLY_EXPR:
11124 COMPLETE_CHECK_OP_0 (node);
11125 COMPLETE_CHECK_OP_1 (node);
11126 CAN_COMPLETE_NORMALLY (node)
11127 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11128 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11129 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11132 case CLEANUP_POINT_EXPR:
11133 COMPLETE_CHECK_OP_0 (node);
11134 TREE_TYPE (node) = void_type_node;
11135 CAN_COMPLETE_NORMALLY (node) =
11136 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11139 case WITH_CLEANUP_EXPR:
11140 COMPLETE_CHECK_OP_0 (node);
11141 COMPLETE_CHECK_OP_2 (node);
11142 CAN_COMPLETE_NORMALLY (node) =
11143 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11144 TREE_TYPE (node) = void_type_node;
11147 case LABELED_BLOCK_EXPR:
11148 PUSH_LABELED_BLOCK (node);
11149 if (LABELED_BLOCK_BODY (node))
11150 COMPLETE_CHECK_OP_1 (node);
11151 TREE_TYPE (node) = void_type_node;
11152 POP_LABELED_BLOCK ();
11154 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11156 LABELED_BLOCK_BODY (node) = NULL_TREE;
11157 CAN_COMPLETE_NORMALLY (node) = 1;
11159 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11160 CAN_COMPLETE_NORMALLY (node) = 1;
11163 case EXIT_BLOCK_EXPR:
11164 /* We don't complete operand 1, because it's the return value of
11165 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11166 return patch_bc_statement (node);
11169 cn = java_complete_tree (TREE_OPERAND (node, 0));
11170 if (cn == error_mark_node)
11173 /* First, the case expression must be constant. Values of final
11174 fields are accepted. */
11176 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11177 && JDECL_P (TREE_OPERAND (cn, 1))
11178 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11179 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11181 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11182 TREE_OPERAND (cn, 1));
11185 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11187 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11188 parse_error_context (node, "Constant expression required");
11189 return error_mark_node;
11192 nn = ctxp->current_loop;
11194 /* It must be assignable to the type of the switch expression. */
11195 if (!try_builtin_assignconv (NULL_TREE,
11196 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11198 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11199 parse_error_context
11201 "Incompatible type for case. Can't convert `%s' to `int'",
11202 lang_printable_name (TREE_TYPE (cn), 0));
11203 return error_mark_node;
11206 cn = fold (convert (int_type_node, cn));
11208 /* Multiple instance of a case label bearing the same
11209 value is checked during code generation. The case
11210 expression is allright so far. */
11211 if (TREE_CODE (cn) == VAR_DECL)
11212 cn = DECL_INITIAL (cn);
11213 TREE_OPERAND (node, 0) = cn;
11214 TREE_TYPE (node) = void_type_node;
11215 CAN_COMPLETE_NORMALLY (node) = 1;
11216 TREE_SIDE_EFFECTS (node) = 1;
11220 nn = ctxp->current_loop;
11221 /* Only one default label is allowed per switch statement */
11222 if (SWITCH_HAS_DEFAULT (nn))
11224 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11225 parse_error_context (wfl_operator,
11226 "Duplicate case label: `default'");
11227 return error_mark_node;
11230 SWITCH_HAS_DEFAULT (nn) = 1;
11231 TREE_TYPE (node) = void_type_node;
11232 TREE_SIDE_EFFECTS (node) = 1;
11233 CAN_COMPLETE_NORMALLY (node) = 1;
11239 /* Check whether the loop was enclosed in a labeled
11240 statement. If not, create one, insert the loop in it and
11242 nn = patch_loop_statement (node);
11244 /* Anyways, walk the body of the loop */
11245 if (TREE_CODE (node) == LOOP_EXPR)
11246 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11247 /* Switch statement: walk the switch expression and the cases */
11249 node = patch_switch_statement (node);
11251 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11252 nn = error_mark_node;
11255 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11256 /* If we returned something different, that's because we
11257 inserted a label. Pop the label too. */
11260 if (CAN_COMPLETE_NORMALLY (node))
11261 CAN_COMPLETE_NORMALLY (nn) = 1;
11262 POP_LABELED_BLOCK ();
11269 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11270 return patch_exit_expr (node);
11274 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11275 if (TREE_OPERAND (node, 0) == error_mark_node)
11276 return error_mark_node;
11277 /* then-else branches */
11278 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11279 if (TREE_OPERAND (node, 1) == error_mark_node)
11280 return error_mark_node;
11281 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11282 if (TREE_OPERAND (node, 2) == error_mark_node)
11283 return error_mark_node;
11284 return patch_if_else_statement (node);
11287 case CONDITIONAL_EXPR:
11289 wfl_op1 = TREE_OPERAND (node, 0);
11290 COMPLETE_CHECK_OP_0 (node);
11291 wfl_op2 = TREE_OPERAND (node, 1);
11292 COMPLETE_CHECK_OP_1 (node);
11293 wfl_op3 = TREE_OPERAND (node, 2);
11294 COMPLETE_CHECK_OP_2 (node);
11295 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11297 /* 3- Expression section */
11298 case COMPOUND_EXPR:
11299 wfl_op2 = TREE_OPERAND (node, 1);
11300 TREE_OPERAND (node, 0) = nn =
11301 java_complete_tree (TREE_OPERAND (node, 0));
11302 if (wfl_op2 == empty_stmt_node)
11303 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11306 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11308 /* An unreachable condition in a do-while statement
11309 is *not* (technically) an unreachable statement. */
11311 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11312 nn = EXPR_WFL_NODE (nn);
11313 if (TREE_CODE (nn) != EXIT_EXPR)
11315 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11316 parse_error_context (wfl_operator, "Unreachable statement");
11319 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11320 if (TREE_OPERAND (node, 1) == error_mark_node)
11321 return error_mark_node;
11322 CAN_COMPLETE_NORMALLY (node)
11323 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
11325 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11329 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11330 return patch_return (node);
11332 case EXPR_WITH_FILE_LOCATION:
11333 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11334 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11337 node = resolve_expression_name (node, NULL);
11338 if (node == error_mark_node)
11340 /* Keep line number information somewhere were it doesn't
11341 disrupt the completion process. */
11342 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11344 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11345 TREE_OPERAND (node, 1) = wfl;
11347 CAN_COMPLETE_NORMALLY (node) = 1;
11352 int save_lineno = lineno;
11353 lineno = EXPR_WFL_LINENO (node);
11354 body = java_complete_tree (EXPR_WFL_NODE (node));
11355 lineno = save_lineno;
11356 EXPR_WFL_NODE (node) = body;
11357 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11358 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11359 if (body == empty_stmt_node || TREE_CONSTANT (body))
11361 /* Makes it easier to constant fold, detect empty bodies. */
11364 if (body == error_mark_node)
11366 /* Its important for the evaluation of assignment that
11367 this mark on the TREE_TYPE is propagated. */
11368 TREE_TYPE (node) = error_mark_node;
11369 return error_mark_node;
11372 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11377 case NEW_ARRAY_EXPR:
11378 /* Patch all the dimensions */
11380 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11382 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11383 tree dim = convert (int_type_node,
11384 java_complete_tree (TREE_VALUE (cn)));
11385 if (dim == error_mark_node)
11392 TREE_VALUE (cn) = dim;
11393 /* Setup the location of the current dimension, for
11394 later error report. */
11395 TREE_PURPOSE (cn) =
11396 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11397 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11400 /* They complete the array creation expression, if no errors
11402 CAN_COMPLETE_NORMALLY (node) = 1;
11403 return (flag ? error_mark_node
11404 : force_evaluation_order (patch_newarray (node)));
11406 case NEW_ANONYMOUS_ARRAY_EXPR:
11407 /* Create the array type if necessary. */
11408 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11410 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11411 if (!(type = resolve_type_during_patch (type)))
11412 return error_mark_node;
11413 type = build_array_from_name (type, NULL_TREE,
11414 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11415 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11417 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11418 ANONYMOUS_ARRAY_INITIALIZER (node));
11419 if (node == error_mark_node)
11420 return error_mark_node;
11421 CAN_COMPLETE_NORMALLY (node) = 1;
11424 case NEW_CLASS_EXPR:
11426 /* Complete function's argument(s) first */
11427 if (complete_function_arguments (node))
11428 return error_mark_node;
11431 tree decl, wfl = TREE_OPERAND (node, 0);
11432 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11433 int from_super = (EXPR_WFL_NODE (TREE_OPERAND (node, 0)) ==
11434 super_identifier_node);
11436 node = patch_method_invocation (node, NULL_TREE, NULL_TREE,
11437 from_super, 0, &decl);
11438 if (node == error_mark_node)
11439 return error_mark_node;
11441 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11442 /* If we call this(...), register signature and positions */
11444 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11445 tree_cons (wfl, decl,
11446 DECL_CONSTRUCTOR_CALLS (current_function_decl));
11447 CAN_COMPLETE_NORMALLY (node) = 1;
11448 return force_evaluation_order (node);
11452 /* Save potential wfls */
11453 wfl_op1 = TREE_OPERAND (node, 0);
11454 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11456 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11457 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11458 && DECL_INITIAL (nn) != NULL_TREE)
11462 value = fold_constant_for_init (nn, nn);
11464 if (value != NULL_TREE)
11466 tree type = TREE_TYPE (value);
11467 if (JPRIMITIVE_TYPE_P (type) ||
11468 (type == string_ptr_type_node && ! flag_emit_class_files))
11469 return empty_stmt_node;
11471 if (! flag_emit_class_files)
11472 DECL_INITIAL (nn) = NULL_TREE;
11473 if (CLASS_FINAL_VARIABLE_P (nn))
11474 DECL_FIELD_FINAL_IUD (nn) = 0;
11476 wfl_op2 = TREE_OPERAND (node, 1);
11478 if (TREE_OPERAND (node, 0) == error_mark_node)
11479 return error_mark_node;
11481 flag = COMPOUND_ASSIGN_P (wfl_op2);
11484 /* This might break when accessing outer field from inner
11485 class. TESTME, FIXME */
11486 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
11488 /* Hand stabilize the lhs on both places */
11489 TREE_OPERAND (node, 0) = lvalue;
11490 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11491 (flag_emit_class_files ? lvalue : save_expr (lvalue));
11493 /* 15.25.2.a: Left hand is not an array access. FIXME */
11494 /* Now complete the RHS. We write it back later on. */
11495 nn = java_complete_tree (TREE_OPERAND (node, 1));
11497 if ((cn = patch_string (nn)))
11500 /* The last part of the rewrite for E1 op= E2 is to have
11501 E1 = (T)(E1 op E2), with T being the type of E1. */
11502 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11503 TREE_TYPE (lvalue), nn));
11505 /* If the assignment is compound and has reference type,
11506 then ensure the LHS has type String and nothing else. */
11507 if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
11508 && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
11509 parse_error_context (wfl_op2,
11510 "Incompatible type for `+='. Can't convert `%s' to `java.lang.String'",
11511 lang_printable_name (TREE_TYPE (lvalue), 0));
11513 /* 15.25.2.b: Left hand is an array access. FIXME */
11516 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
11517 function to complete this RHS. Note that a NEW_ARRAY_INIT
11518 might have been already fully expanded if created as a result
11519 of processing an anonymous array initializer. We avoid doing
11520 the operation twice by testing whether the node already bears
11522 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
11523 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
11524 TREE_OPERAND (node, 1));
11525 /* Otherwise we simply complete the RHS */
11527 nn = java_complete_tree (TREE_OPERAND (node, 1));
11529 if (nn == error_mark_node)
11530 return error_mark_node;
11532 /* Write back the RHS as we evaluated it. */
11533 TREE_OPERAND (node, 1) = nn;
11535 /* In case we're handling = with a String as a RHS, we need to
11536 produce a String out of the RHS (it might still be a
11537 STRING_CST or a StringBuffer at this stage */
11538 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11539 TREE_OPERAND (node, 1) = nn;
11541 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11542 TREE_OPERAND (node, 1))))
11544 /* We return error_mark_node if outer_field_access_fix
11545 detects we write into a final. */
11546 if (nn == error_mark_node)
11547 return error_mark_node;
11552 node = patch_assignment (node, wfl_op1, wfl_op2);
11553 /* Reorganize the tree if necessary. */
11554 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11555 || JSTRING_P (TREE_TYPE (node))))
11556 node = java_refold (node);
11559 CAN_COMPLETE_NORMALLY (node) = 1;
11571 case TRUNC_MOD_EXPR:
11572 case TRUNC_DIV_EXPR:
11574 case TRUTH_ANDIF_EXPR:
11575 case TRUTH_ORIF_EXPR:
11582 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11583 knows how to handle those cases. */
11584 wfl_op1 = TREE_OPERAND (node, 0);
11585 wfl_op2 = TREE_OPERAND (node, 1);
11587 CAN_COMPLETE_NORMALLY (node) = 1;
11588 /* Don't complete string nodes if dealing with the PLUS operand. */
11589 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
11591 nn = java_complete_tree (wfl_op1);
11592 if (nn == error_mark_node)
11593 return error_mark_node;
11595 TREE_OPERAND (node, 0) = nn;
11597 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
11599 nn = java_complete_tree (wfl_op2);
11600 if (nn == error_mark_node)
11601 return error_mark_node;
11603 TREE_OPERAND (node, 1) = nn;
11605 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
11607 case INSTANCEOF_EXPR:
11608 wfl_op1 = TREE_OPERAND (node, 0);
11609 COMPLETE_CHECK_OP_0 (node);
11610 if (flag_emit_xref)
11612 TREE_TYPE (node) = boolean_type_node;
11615 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11617 case UNARY_PLUS_EXPR:
11619 case TRUTH_NOT_EXPR:
11621 case PREDECREMENT_EXPR:
11622 case PREINCREMENT_EXPR:
11623 case POSTDECREMENT_EXPR:
11624 case POSTINCREMENT_EXPR:
11626 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11627 how to handle those cases. */
11628 wfl_op1 = TREE_OPERAND (node, 0);
11629 CAN_COMPLETE_NORMALLY (node) = 1;
11630 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11631 if (TREE_OPERAND (node, 0) == error_mark_node)
11632 return error_mark_node;
11633 node = patch_unaryop (node, wfl_op1);
11634 CAN_COMPLETE_NORMALLY (node) = 1;
11638 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11639 how to handle those cases. */
11640 wfl_op1 = TREE_OPERAND (node, 0);
11641 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11642 if (TREE_OPERAND (node, 0) == error_mark_node)
11643 return error_mark_node;
11644 if (!flag_emit_class_files && !flag_emit_xref)
11645 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
11646 /* The same applies to wfl_op2 */
11647 wfl_op2 = TREE_OPERAND (node, 1);
11648 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11649 if (TREE_OPERAND (node, 1) == error_mark_node)
11650 return error_mark_node;
11651 if (!flag_emit_class_files && !flag_emit_xref)
11652 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
11653 return patch_array_ref (node);
11658 case COMPONENT_REF:
11659 /* The first step in the re-write of qualified name handling. FIXME.
11660 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
11661 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11662 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11664 tree name = TREE_OPERAND (node, 1);
11665 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11666 if (field == NULL_TREE)
11668 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11669 return error_mark_node;
11671 if (! FIELD_STATIC (field))
11673 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11674 return error_mark_node;
11683 /* Can't use THIS in a static environment */
11686 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11687 parse_error_context (wfl_operator,
11688 "Keyword `this' used outside allowed context");
11689 TREE_TYPE (node) = error_mark_node;
11690 return error_mark_node;
11692 if (ctxp->explicit_constructor_p)
11694 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11695 parse_error_context
11696 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
11697 TREE_TYPE (node) = error_mark_node;
11698 return error_mark_node;
11700 return current_this;
11702 case CLASS_LITERAL:
11703 CAN_COMPLETE_NORMALLY (node) = 1;
11704 node = patch_incomplete_class_ref (node);
11705 if (node == error_mark_node)
11706 return error_mark_node;
11709 case INSTANCE_INITIALIZERS_EXPR:
11710 in_instance_initializer++;
11711 node = java_complete_tree (TREE_OPERAND (node, 0));
11712 in_instance_initializer--;
11713 if (node != error_mark_node)
11714 TREE_TYPE (node) = void_type_node;
11716 return error_mark_node;
11720 CAN_COMPLETE_NORMALLY (node) = 1;
11721 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
11722 and it's time to turn it into the appropriate String object */
11723 if ((nn = patch_string (node)))
11726 internal_error ("No case for %s", tree_code_name [TREE_CODE (node)]);
11731 /* Complete function call's argument. Return a non zero value is an
11732 error was found. */
11735 complete_function_arguments (node)
11741 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11742 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11744 tree wfl = TREE_VALUE (cn), parm, temp;
11745 parm = java_complete_tree (wfl);
11747 if (parm == error_mark_node)
11752 /* If have a string literal that we haven't transformed yet or a
11753 crafted string buffer, as a result of use of the the String
11754 `+' operator. Build `parm.toString()' and expand it. */
11755 if ((temp = patch_string (parm)))
11757 /* Inline PRIMTYPE.TYPE read access */
11758 parm = maybe_build_primttype_type_ref (parm, wfl);
11760 TREE_VALUE (cn) = parm;
11762 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11766 /* Sometimes (for loops and variable initialized during their
11767 declaration), we want to wrap a statement around a WFL and turn it
11771 build_debugable_stmt (location, stmt)
11775 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11777 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11778 EXPR_WFL_LINECOL (stmt) = location;
11780 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11785 build_expr_block (body, decls)
11788 tree node = make_node (BLOCK);
11789 BLOCK_EXPR_DECLS (node) = decls;
11790 BLOCK_EXPR_BODY (node) = body;
11792 TREE_TYPE (node) = TREE_TYPE (body);
11793 TREE_SIDE_EFFECTS (node) = 1;
11797 /* Create a new function block and link it approriately to current
11798 function block chain */
11803 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11806 /* Link block B supercontext to the previous block. The current
11807 function DECL is used as supercontext when enter_a_block is called
11808 for the first time for a given function. The current function body
11809 (DECL_FUNCTION_BODY) is set to be block B. */
11815 tree fndecl = current_function_decl;
11818 BLOCK_SUPERCONTEXT (b) = current_static_block;
11819 current_static_block = b;
11822 else if (!DECL_FUNCTION_BODY (fndecl))
11824 BLOCK_SUPERCONTEXT (b) = fndecl;
11825 DECL_FUNCTION_BODY (fndecl) = b;
11829 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11830 DECL_FUNCTION_BODY (fndecl) = b;
11835 /* Exit a block by changing the current function body
11836 (DECL_FUNCTION_BODY) to the current block super context, only if
11837 the block being exited isn't the method's top level one. */
11843 if (current_function_decl)
11845 b = DECL_FUNCTION_BODY (current_function_decl);
11846 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11847 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11851 b = current_static_block;
11853 if (BLOCK_SUPERCONTEXT (b))
11854 current_static_block = BLOCK_SUPERCONTEXT (b);
11859 /* Lookup for NAME in the nested function's blocks, all the way up to
11860 the current toplevel one. It complies with Java's local variable
11864 lookup_name_in_blocks (name)
11867 tree b = GET_CURRENT_BLOCK (current_function_decl);
11869 while (b != current_function_decl)
11873 /* Paranoid sanity check. To be removed */
11874 if (TREE_CODE (b) != BLOCK)
11877 for (current = BLOCK_EXPR_DECLS (b); current;
11878 current = TREE_CHAIN (current))
11879 if (DECL_NAME (current) == name)
11881 b = BLOCK_SUPERCONTEXT (b);
11887 maybe_absorb_scoping_blocks ()
11889 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
11891 tree b = exit_block ();
11892 java_method_add_stmt (current_function_decl, b);
11893 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11898 /* This section of the source is reserved to build_* functions that
11899 are building incomplete tree nodes and the patch_* functions that
11900 are completing them. */
11902 /* Wrap a non WFL node around a WFL. */
11904 build_wfl_wrap (node, location)
11908 tree wfl, node_to_insert = node;
11910 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11911 with the way we're processing SUPER. A THIS from a primary as a
11912 different form than a SUPER. Turn THIS into something symbolic */
11913 if (TREE_CODE (node) == THIS_EXPR)
11914 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11916 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11918 EXPR_WFL_LINECOL (wfl) = location;
11919 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11924 /* Build a super() constructor invocation. Returns empty_stmt_node if
11925 we're currently dealing with the class java.lang.Object. */
11928 build_super_invocation (mdecl)
11931 if (DECL_CONTEXT (mdecl) == object_type_node)
11932 return empty_stmt_node;
11935 tree super_wfl = build_wfl_node (super_identifier_node);
11936 tree a = NULL_TREE, t;
11937 /* If we're dealing with an anonymous class, pass the arguments
11938 of the crafted constructor along. */
11939 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11941 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11942 for (; t != end_params_node; t = TREE_CHAIN (t))
11943 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11945 return build_method_invocation (super_wfl, a);
11949 /* Build a SUPER/THIS qualified method invocation. */
11952 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11959 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
11960 EXPR_WFL_LINECOL (wfl) = lloc;
11961 invok = build_method_invocation (name, args);
11962 return make_qualified_primary (wfl, invok, rloc);
11965 /* Build an incomplete CALL_EXPR node. */
11968 build_method_invocation (name, args)
11972 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11973 TREE_SIDE_EFFECTS (call) = 1;
11974 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11978 /* Build an incomplete new xxx(...) node. */
11981 build_new_invocation (name, args)
11984 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
11985 TREE_SIDE_EFFECTS (call) = 1;
11986 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11990 /* Build an incomplete assignment expression. */
11993 build_assignment (op, op_location, lhs, rhs)
11994 int op, op_location;
11998 /* Build the corresponding binop if we deal with a Compound
11999 Assignment operator. Mark the binop sub-tree as part of a
12000 Compound Assignment expression */
12001 if (op != ASSIGN_TK)
12003 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
12004 COMPOUND_ASSIGN_P (rhs) = 1;
12006 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
12007 TREE_SIDE_EFFECTS (assignment) = 1;
12008 EXPR_WFL_LINECOL (assignment) = op_location;
12012 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
12015 print_int_node (node)
12018 static char buffer [80];
12019 if (TREE_CONSTANT_OVERFLOW (node))
12020 sprintf (buffer, "<overflow>");
12022 if (TREE_INT_CST_HIGH (node) == 0)
12023 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
12024 TREE_INT_CST_LOW (node));
12025 else if (TREE_INT_CST_HIGH (node) == -1
12026 && TREE_INT_CST_LOW (node) != 0)
12029 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
12030 -TREE_INT_CST_LOW (node));
12033 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
12034 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
12041 /* This section of the code handle assignment check with FINAL
12045 reset_static_final_variable_assignment_flag (class)
12049 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12050 if (CLASS_FINAL_VARIABLE_P (field))
12051 DECL_FIELD_FINAL_LIIC (field) = 0;
12054 /* Figure whether all final static variable have been initialized. */
12057 check_static_final_variable_assignment_flag (class)
12062 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12063 if (CLASS_FINAL_VARIABLE_P (field)
12064 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12065 parse_error_context
12066 (DECL_FIELD_FINAL_WFL (field),
12067 "Blank static final variable `%s' may not have been initialized",
12068 IDENTIFIER_POINTER (DECL_NAME (field)));
12071 /* This function marks all final variable locally unassigned. */
12074 reset_final_variable_local_assignment_flag (class)
12078 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12079 if (FINAL_VARIABLE_P (field))
12080 DECL_FIELD_FINAL_LIIC (field) = 0;
12083 /* Figure whether all final variables have beem initialized in MDECL
12084 and mark MDECL accordingly. */
12087 check_final_variable_local_assignment_flag (class, mdecl)
12092 int initialized = 0;
12093 int non_initialized = 0;
12095 if (DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12098 /* First find out whether all final variables or no final variable
12099 are initialized in this ctor. We don't take into account final
12100 variable that have been initialized upon declaration. */
12101 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12102 if (FINAL_VARIABLE_P (field) && !DECL_FIELD_FINAL_IUD (field))
12104 if (DECL_FIELD_FINAL_LIIC (field))
12110 /* There were no non initialized variable and no initialized variable.
12111 This ctor is fine. */
12112 if (!non_initialized && !initialized)
12113 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12114 /* If no variables have been initialized, that fine. We'll check
12115 later whether this ctor calls a constructor which initializes
12116 them. We mark the ctor as not initializing all its finals. */
12117 else if (initialized == 0)
12118 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12119 /* If we have a mixed bag, then we have a problem. We need to report
12120 all the variables we're not initializing. */
12121 else if (initialized && non_initialized)
12123 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 0;
12124 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12125 if (FIELD_FINAL (field)
12126 && !DECL_FIELD_FINAL_IUD (field) && !DECL_FIELD_FINAL_LIIC (field))
12128 parse_error_context
12129 (lookup_cl (mdecl),
12130 "Blank final variable `%s' may not have been initialized in this constructor",
12131 IDENTIFIER_POINTER (DECL_NAME (field)));
12132 DECL_FIELD_FINAL_IERR (field) = 1;
12135 /* Otherwise we know this ctor is initializing all its final
12136 variable. We mark it so. */
12138 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12141 /* This function recurses in a simple what through STMT and stops when
12142 it finds a constructor call. It then verifies that the called
12143 constructor initialized its final properly. Return 1 upon success,
12144 0 or -1 otherwise. */
12147 check_final_variable_indirect_assignment (stmt)
12151 switch (TREE_CODE (stmt))
12153 case EXPR_WITH_FILE_LOCATION:
12154 return check_final_variable_indirect_assignment (EXPR_WFL_NODE (stmt));
12155 case COMPOUND_EXPR:
12156 res = check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12159 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 1));
12161 return check_final_variable_indirect_assignment (TREE_OPERAND (stmt, 0));
12164 tree decl = TREE_OPERAND (stmt, 0);
12167 if (TREE_CODE (decl) != FUNCTION_DECL)
12168 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
12169 if (TREE_CODE (decl) != FUNCTION_DECL)
12171 if (DECL_FUNCTION_ALL_FINAL_INITIALIZED (decl))
12173 if (DECL_FINIT_P (decl) || DECL_CONTEXT (decl) != current_class)
12175 fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl));
12176 if (fbody == error_mark_node)
12178 fbody = BLOCK_EXPR_BODY (fbody);
12179 return check_final_variable_indirect_assignment (fbody);
12187 /* This is the last chance to catch a final variable initialization
12188 problem. This routine will report an error if a final variable was
12189 never (globally) initialized and never reported as not having been
12190 initialized properly. */
12193 check_final_variable_global_assignment_flag (class)
12199 /* We go through all natural ctors and see whether they're
12200 initializing all their final variables or not. */
12201 current_function_decl = NULL_TREE; /* For the error report. */
12202 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
12203 if (DECL_CONSTRUCTOR_P (mdecl) && ! DECL_FUNCTION_SYNTHETIC_CTOR (mdecl))
12205 if (!DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl))
12207 /* It doesn't. Maybe it calls a constructor that initializes
12209 tree fbody = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl));
12210 if (fbody == error_mark_node)
12212 fbody = BLOCK_EXPR_BODY (fbody);
12213 if (check_final_variable_indirect_assignment (fbody) == 1)
12215 DECL_FUNCTION_ALL_FINAL_INITIALIZED (mdecl) = 1;
12219 parse_error_context
12220 (lookup_cl (mdecl),
12221 "Final variable initialization error in this constructor");
12227 /* Finally we catch final variables that never were initialized */
12228 for (field = TYPE_FIELDS (class); field; field = TREE_CHAIN (field))
12229 if (FINAL_VARIABLE_P (field)
12230 /* If the field wasn't initialized upon declaration */
12231 && !DECL_FIELD_FINAL_IUD (field)
12232 /* There wasn't natural ctor in which the field could have been
12235 /* If we never reported a problem with this field */
12236 && !DECL_FIELD_FINAL_IERR (field))
12238 current_function_decl = NULL;
12239 parse_error_context
12240 (DECL_FIELD_FINAL_WFL (field),
12241 "Final variable `%s' hasn't been initialized upon its declaration",
12242 IDENTIFIER_POINTER (DECL_NAME (field)));
12247 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
12251 check_final_assignment (lvalue, wfl)
12254 if (TREE_CODE (lvalue) != COMPONENT_REF && !JDECL_P (lvalue))
12257 if (TREE_CODE (lvalue) == COMPONENT_REF
12258 && JDECL_P (TREE_OPERAND (lvalue, 1)))
12259 lvalue = TREE_OPERAND (lvalue, 1);
12261 if (!FIELD_FINAL (lvalue))
12264 /* Now the logic. We can modify a final VARIABLE:
12265 1) in finit$, (its declaration was followed by an initialization,)
12266 2) consistently in each natural ctor, if it wasn't initialized in
12267 finit$ or once in <clinit>. In any other cases, an error should be
12269 if (DECL_FINIT_P (current_function_decl))
12271 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12275 if (!DECL_FUNCTION_SYNTHETIC_CTOR (current_function_decl)
12276 /* Only if it wasn't given a value upon initialization */
12277 && DECL_LANG_SPECIFIC (lvalue) && !DECL_FIELD_FINAL_IUD (lvalue)
12278 /* If it was never assigned a value in this constructor */
12279 && !DECL_FIELD_FINAL_LIIC (lvalue))
12281 /* Turn the locally assigned flag on, it will be checked later
12282 on to point out at discrepancies. */
12283 DECL_FIELD_FINAL_LIIC (lvalue) = 1;
12284 if (DECL_CLINIT_P (current_function_decl))
12285 DECL_FIELD_FINAL_IUD (lvalue) = 1;
12289 /* Other problems should be reported right away. */
12290 parse_error_context
12291 (wfl, "Can't %sassign a value to the final variable `%s'",
12292 (FIELD_STATIC (lvalue) ? "re" : ""),
12293 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
12295 /* Note that static field can be initialized once and only once. */
12296 if (FIELD_STATIC (lvalue))
12297 DECL_FIELD_FINAL_IERR (lvalue) = 1;
12302 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
12303 read. This is needed to avoid circularities in the implementation
12304 of these fields in libjava. */
12307 maybe_build_primttype_type_ref (rhs, wfl)
12310 tree to_return = NULL_TREE;
12311 tree rhs_type = TREE_TYPE (rhs);
12312 if (TREE_CODE (rhs) == COMPOUND_EXPR)
12314 tree n = TREE_OPERAND (rhs, 1);
12315 if (TREE_CODE (n) == VAR_DECL
12316 && DECL_NAME (n) == TYPE_identifier_node
12317 && rhs_type == class_ptr_type
12318 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
12320 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
12321 if (!strncmp (self_name, "java.lang.", 10))
12322 to_return = build_primtype_type_ref (self_name);
12325 return (to_return ? to_return : rhs );
12328 /* 15.25 Assignment operators. */
12331 patch_assignment (node, wfl_op1, wfl_op2)
12336 tree rhs = TREE_OPERAND (node, 1);
12337 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12338 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12339 int error_found = 0;
12340 int lvalue_from_array = 0;
12342 /* Can't assign to a (blank) final. */
12343 if (check_final_assignment (lvalue, wfl_op1))
12346 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12348 /* Lhs can be a named variable */
12349 if (JDECL_P (lvalue))
12351 lhs_type = TREE_TYPE (lvalue);
12353 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12354 comment on reason why */
12355 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12357 lhs_type = TREE_TYPE (lvalue);
12358 lvalue_from_array = 1;
12360 /* Or a field access */
12361 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12362 lhs_type = TREE_TYPE (lvalue);
12363 /* Or a function return slot */
12364 else if (TREE_CODE (lvalue) == RESULT_DECL)
12365 lhs_type = TREE_TYPE (lvalue);
12366 /* Otherwise, we might want to try to write into an optimized static
12367 final, this is an of a different nature, reported further on. */
12368 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12369 && resolve_expression_name (wfl_op1, &llvalue))
12371 if (!error_found && check_final_assignment (llvalue, wfl_op1))
12373 /* What we should do instead is resetting the all the flags
12374 previously set, exchange lvalue for llvalue and continue. */
12376 return error_mark_node;
12379 lhs_type = TREE_TYPE (lvalue);
12383 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12387 rhs_type = TREE_TYPE (rhs);
12388 /* 5.1 Try the assignment conversion for builtin type. */
12389 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12391 /* 5.2 If it failed, try a reference conversion */
12392 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12393 lhs_type = promote_type (rhs_type);
12395 /* 15.25.2 If we have a compound assignment, convert RHS into the
12397 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12398 new_rhs = convert (lhs_type, rhs);
12400 /* Explicit cast required. This is an error */
12403 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12404 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12406 char operation [32]; /* Max size known */
12408 /* If the assignment is part of a declaration, we use the WFL of
12409 the declared variable to point out the error and call it a
12410 declaration problem. If the assignment is a genuine =
12411 operator, we call is a operator `=' problem, otherwise we
12412 call it an assignment problem. In both of these last cases,
12413 we use the WFL of the operator to indicate the error. */
12415 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12418 strcpy (operation, "declaration");
12422 wfl = wfl_operator;
12423 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12424 strcpy (operation, "assignment");
12425 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12426 strcpy (operation, "`return'");
12428 strcpy (operation, "`='");
12431 if (!valid_cast_to_p (rhs_type, lhs_type))
12432 parse_error_context
12433 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12434 operation, t1, t2);
12436 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12437 operation, t1, t2);
12438 free (t1); free (t2);
12442 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12444 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
12447 return error_mark_node;
12449 /* 10.10: Array Store Exception runtime check */
12450 if (!flag_emit_class_files
12452 && lvalue_from_array
12453 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12456 tree base = lvalue;
12458 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12459 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12460 base = TREE_OPERAND (lvalue, 0);
12463 if (flag_bounds_check)
12464 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12466 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12469 /* Build the invocation of _Jv_CheckArrayStore */
12470 new_rhs = save_expr (new_rhs);
12471 check = build (CALL_EXPR, void_type_node,
12472 build_address_of (soft_checkarraystore_node),
12473 tree_cons (NULL_TREE, base,
12474 build_tree_list (NULL_TREE, new_rhs)),
12476 TREE_SIDE_EFFECTS (check) = 1;
12478 /* We have to decide on an insertion point */
12479 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12482 if (flag_bounds_check)
12484 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12485 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12486 build (COMPOUND_EXPR, void_type_node, t, check);
12489 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12490 check, TREE_OPERAND (lvalue, 1));
12494 /* Make sure the bound check will happen before the store check */
12495 if (flag_bounds_check)
12496 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12497 build (COMPOUND_EXPR, void_type_node,
12498 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12500 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12504 /* Final locals can be used as case values in switch
12505 statement. Prepare them for this eventuality. */
12506 if (TREE_CODE (lvalue) == VAR_DECL
12507 && LOCAL_FINAL_P (lvalue)
12508 && TREE_CONSTANT (new_rhs)
12509 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12510 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12513 TREE_CONSTANT (lvalue) = 1;
12514 DECL_INITIAL (lvalue) = new_rhs;
12517 TREE_OPERAND (node, 0) = lvalue;
12518 TREE_OPERAND (node, 1) = new_rhs;
12519 TREE_TYPE (node) = lhs_type;
12523 /* Check that type SOURCE can be cast into type DEST. If the cast
12524 can't occur at all, return 0 otherwise 1. This function is used to
12525 produce accurate error messages on the reasons why an assignment
12529 try_reference_assignconv (lhs_type, rhs)
12530 tree lhs_type, rhs;
12532 tree new_rhs = NULL_TREE;
12533 tree rhs_type = TREE_TYPE (rhs);
12535 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12537 /* `null' may be assigned to any reference type */
12538 if (rhs == null_pointer_node)
12539 new_rhs = null_pointer_node;
12540 /* Try the reference assignment conversion */
12541 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12543 /* This is a magic assignment that we process differently */
12544 else if (rhs == soft_exceptioninfo_call_node)
12550 /* Check that RHS can be converted into LHS_TYPE by the assignment
12551 conversion (5.2), for the cases of RHS being a builtin type. Return
12552 NULL_TREE if the conversion fails or if because RHS isn't of a
12553 builtin type. Return a converted RHS if the conversion is possible. */
12556 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12557 tree wfl_op1, lhs_type, rhs;
12559 tree new_rhs = NULL_TREE;
12560 tree rhs_type = TREE_TYPE (rhs);
12562 /* Handle boolean specially. */
12563 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12564 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12566 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12567 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12571 /* Zero accepted everywhere */
12572 else if (TREE_CODE (rhs) == INTEGER_CST
12573 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12574 && JPRIMITIVE_TYPE_P (rhs_type))
12575 new_rhs = convert (lhs_type, rhs);
12577 /* 5.1.1 Try Identity Conversion,
12578 5.1.2 Try Widening Primitive Conversion */
12579 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12580 new_rhs = convert (lhs_type, rhs);
12582 /* Try a narrowing primitive conversion (5.1.3):
12583 - expression is a constant expression of type int AND
12584 - variable is byte, short or char AND
12585 - The value of the expression is representable in the type of the
12587 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12588 && (lhs_type == byte_type_node || lhs_type == char_type_node
12589 || lhs_type == short_type_node))
12591 if (int_fits_type_p (rhs, lhs_type))
12592 new_rhs = convert (lhs_type, rhs);
12593 else if (wfl_op1) /* Might be called with a NULL */
12594 parse_warning_context
12595 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
12596 print_int_node (rhs), lang_printable_name (lhs_type, 0));
12597 /* Reported a warning that will turn into an error further
12598 down, so we don't return */
12604 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12605 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
12606 0 is the conversion test fails. This implements parts the method
12607 invocation convertion (5.3). */
12610 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12611 tree lhs_type, rhs_type;
12613 /* 5.1.1: This is the identity conversion part. */
12614 if (lhs_type == rhs_type)
12617 /* Reject non primitive types and boolean conversions. */
12618 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
12621 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12622 than a char can't be converted into a char. Short can't too, but
12623 the < test below takes care of that */
12624 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12627 /* Accept all promoted type here. Note, we can't use <= in the test
12628 below, because we still need to bounce out assignments of short
12629 to char and the likes */
12630 if (lhs_type == int_type_node
12631 && (rhs_type == promoted_byte_type_node
12632 || rhs_type == promoted_short_type_node
12633 || rhs_type == promoted_char_type_node
12634 || rhs_type == promoted_boolean_type_node))
12637 /* From here, an integral is widened if its precision is smaller
12638 than the precision of the LHS or if the LHS is a floating point
12639 type, or the RHS is a float and the RHS a double. */
12640 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12641 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12642 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12643 || (rhs_type == float_type_node && lhs_type == double_type_node))
12649 /* Check that something of SOURCE type can be assigned or cast to
12650 something of DEST type at runtime. Return 1 if the operation is
12651 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12652 were SOURCE is cast into DEST, which borrows a lot of the
12653 assignment check. */
12656 valid_ref_assignconv_cast_p (source, dest, cast)
12661 /* SOURCE or DEST might be null if not from a declared entity. */
12662 if (!source || !dest)
12664 if (JNULLP_TYPE_P (source))
12666 if (TREE_CODE (source) == POINTER_TYPE)
12667 source = TREE_TYPE (source);
12668 if (TREE_CODE (dest) == POINTER_TYPE)
12669 dest = TREE_TYPE (dest);
12671 /* If source and dest are being compiled from bytecode, they may need to
12673 if (CLASS_P (source) && !CLASS_LOADED_P (source))
12675 load_class (source, 1);
12676 safe_layout_class (source);
12678 if (CLASS_P (dest) && !CLASS_LOADED_P (dest))
12680 load_class (dest, 1);
12681 safe_layout_class (dest);
12684 /* Case where SOURCE is a class type */
12685 if (TYPE_CLASS_P (source))
12687 if (TYPE_CLASS_P (dest))
12688 return (source == dest
12689 || inherits_from_p (source, dest)
12690 || (cast && inherits_from_p (dest, source)));
12691 if (TYPE_INTERFACE_P (dest))
12693 /* If doing a cast and SOURCE is final, the operation is
12694 always correct a compile time (because even if SOURCE
12695 does not implement DEST, a subclass of SOURCE might). */
12696 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12698 /* Otherwise, SOURCE must implement DEST */
12699 return interface_of_p (dest, source);
12701 /* DEST is an array, cast permited if SOURCE is of Object type */
12702 return (cast && source == object_type_node ? 1 : 0);
12704 if (TYPE_INTERFACE_P (source))
12706 if (TYPE_CLASS_P (dest))
12708 /* If not casting, DEST must be the Object type */
12710 return dest == object_type_node;
12711 /* We're doing a cast. The cast is always valid is class
12712 DEST is not final, otherwise, DEST must implement SOURCE */
12713 else if (!CLASS_FINAL (TYPE_NAME (dest)))
12716 return interface_of_p (source, dest);
12718 if (TYPE_INTERFACE_P (dest))
12720 /* If doing a cast, then if SOURCE and DEST contain method
12721 with the same signature but different return type, then
12722 this is a (compile time) error */
12725 tree method_source, method_dest;
12729 for (method_source = TYPE_METHODS (source); method_source;
12730 method_source = TREE_CHAIN (method_source))
12733 build_java_argument_signature (TREE_TYPE (method_source));
12734 source_type = TREE_TYPE (TREE_TYPE (method_source));
12735 source_name = DECL_NAME (method_source);
12736 for (method_dest = TYPE_METHODS (dest);
12737 method_dest; method_dest = TREE_CHAIN (method_dest))
12739 build_java_argument_signature (TREE_TYPE (method_dest))
12740 && source_name == DECL_NAME (method_dest)
12741 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12747 return source == dest || interface_of_p (dest, source);
12753 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12754 || (DECL_NAME (TYPE_NAME (source))
12755 == java_io_serializable)));
12758 if (TYPE_ARRAY_P (source))
12760 if (TYPE_CLASS_P (dest))
12761 return dest == object_type_node;
12762 /* Can't cast an array to an interface unless the interface is
12763 java.lang.Cloneable or java.io.Serializable. */
12764 if (TYPE_INTERFACE_P (dest))
12765 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12766 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
12769 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12770 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12772 /* In case of severe errors, they turn out null */
12773 if (!dest_element_type || !source_element_type)
12775 if (source_element_type == dest_element_type)
12777 return valid_ref_assignconv_cast_p (source_element_type,
12778 dest_element_type, cast);
12786 valid_cast_to_p (source, dest)
12790 if (TREE_CODE (source) == POINTER_TYPE)
12791 source = TREE_TYPE (source);
12792 if (TREE_CODE (dest) == POINTER_TYPE)
12793 dest = TREE_TYPE (dest);
12795 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12796 return valid_ref_assignconv_cast_p (source, dest, 1);
12798 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12801 else if (TREE_CODE (source) == BOOLEAN_TYPE
12802 && TREE_CODE (dest) == BOOLEAN_TYPE)
12809 do_unary_numeric_promotion (arg)
12812 tree type = TREE_TYPE (arg);
12813 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12814 || TREE_CODE (type) == CHAR_TYPE)
12815 arg = convert (int_type_node, arg);
12819 /* Return a non zero value if SOURCE can be converted into DEST using
12820 the method invocation conversion rule (5.3). */
12822 valid_method_invocation_conversion_p (dest, source)
12825 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
12826 && valid_builtin_assignconv_identity_widening_p (dest, source))
12827 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12828 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12829 && valid_ref_assignconv_cast_p (source, dest, 0)));
12832 /* Build an incomplete binop expression. */
12835 build_binop (op, op_location, op1, op2)
12840 tree binop = build (op, NULL_TREE, op1, op2);
12841 TREE_SIDE_EFFECTS (binop) = 1;
12842 /* Store the location of the operator, for better error report. The
12843 string of the operator will be rebuild based on the OP value. */
12844 EXPR_WFL_LINECOL (binop) = op_location;
12848 /* Build the string of the operator retained by NODE. If NODE is part
12849 of a compound expression, add an '=' at the end of the string. This
12850 function is called when an error needs to be reported on an
12851 operator. The string is returned as a pointer to a static character
12855 operator_string (node)
12858 #define BUILD_OPERATOR_STRING(S) \
12860 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12864 static char buffer [10];
12865 switch (TREE_CODE (node))
12867 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12868 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12869 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12870 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12871 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12872 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12873 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12874 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12875 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12876 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12877 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12878 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12879 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12880 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12881 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12882 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12883 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12884 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12885 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
12886 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12887 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12888 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12889 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12890 case PREINCREMENT_EXPR: /* Fall through */
12891 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12892 case PREDECREMENT_EXPR: /* Fall through */
12893 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12895 internal_error ("unregistered operator %s",
12896 tree_code_name [TREE_CODE (node)]);
12899 #undef BUILD_OPERATOR_STRING
12902 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12905 java_decl_equiv (var_acc1, var_acc2)
12906 tree var_acc1, var_acc2;
12908 if (JDECL_P (var_acc1))
12909 return (var_acc1 == var_acc2);
12911 return (TREE_CODE (var_acc1) == COMPONENT_REF
12912 && TREE_CODE (var_acc2) == COMPONENT_REF
12913 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12914 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12915 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12918 /* Return a non zero value if CODE is one of the operators that can be
12919 used in conjunction with the `=' operator in a compound assignment. */
12922 binop_compound_p (code)
12923 enum tree_code code;
12926 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12927 if (binop_lookup [i] == code)
12930 return i < BINOP_COMPOUND_CANDIDATES;
12933 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12939 tree c, b, ns, decl;
12941 if (TREE_CODE (t) != MODIFY_EXPR)
12944 c = TREE_OPERAND (t, 1);
12945 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12946 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12947 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12950 /* Now the left branch of the binary operator. */
12951 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12952 if (! (b && TREE_CODE (b) == NOP_EXPR
12953 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12956 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12957 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12958 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12961 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12962 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12963 /* It's got to be the an equivalent decl */
12964 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12966 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12967 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12968 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12969 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12970 /* Change the right part of the BINOP_EXPR */
12971 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12977 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
12978 errors but we modify NODE so that it contains the type computed
12979 according to the expression, when it's fixed. Otherwise, we write
12980 error_mark_node as the type. It allows us to further the analysis
12981 of remaining nodes and detects more errors in certain cases. */
12984 patch_binop (node, wfl_op1, wfl_op2)
12989 tree op1 = TREE_OPERAND (node, 0);
12990 tree op2 = TREE_OPERAND (node, 1);
12991 tree op1_type = TREE_TYPE (op1);
12992 tree op2_type = TREE_TYPE (op2);
12993 tree prom_type = NULL_TREE, cn;
12994 int code = TREE_CODE (node);
12996 /* If 1, tell the routine that we have to return error_mark_node
12997 after checking for the initialization of the RHS */
12998 int error_found = 0;
13000 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13002 /* If either op<n>_type are NULL, this might be early signs of an
13003 error situation, unless it's too early to tell (in case we're
13004 handling a `+', `==', `!=' or `instanceof'.) We want to set op<n>_type
13005 correctly so the error can be later on reported accurately. */
13006 if (! (code == PLUS_EXPR || code == NE_EXPR
13007 || code == EQ_EXPR || code == INSTANCEOF_EXPR))
13012 n = java_complete_tree (op1);
13013 op1_type = TREE_TYPE (n);
13017 n = java_complete_tree (op2);
13018 op2_type = TREE_TYPE (n);
13024 /* 15.16 Multiplicative operators */
13025 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
13026 case RDIV_EXPR: /* 15.16.2 Division Operator / */
13027 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
13028 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
13029 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13031 if (!JNUMERIC_TYPE_P (op1_type))
13032 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13033 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13034 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13035 TREE_TYPE (node) = error_mark_node;
13039 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13040 /* Change the division operator if necessary */
13041 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
13042 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
13044 if (TREE_CODE (prom_type) == INTEGER_TYPE
13045 && flag_use_divide_subroutine
13046 && ! flag_emit_class_files
13047 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
13048 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
13050 /* This one is more complicated. FLOATs are processed by a
13051 function call to soft_fmod. Duplicate the value of the
13052 COMPOUND_ASSIGN_P flag. */
13053 if (code == TRUNC_MOD_EXPR)
13055 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
13056 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
13057 TREE_SIDE_EFFECTS (mod)
13058 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13063 /* 15.17 Additive Operators */
13064 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
13066 /* Operation is valid if either one argument is a string
13067 constant, a String object or a StringBuffer crafted for the
13068 purpose of the a previous usage of the String concatenation
13071 if (TREE_CODE (op1) == STRING_CST
13072 || TREE_CODE (op2) == STRING_CST
13073 || JSTRING_TYPE_P (op1_type)
13074 || JSTRING_TYPE_P (op2_type)
13075 || IS_CRAFTED_STRING_BUFFER_P (op1)
13076 || IS_CRAFTED_STRING_BUFFER_P (op2))
13077 return build_string_concatenation (op1, op2);
13079 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
13081 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
13083 if (!JNUMERIC_TYPE_P (op1_type))
13084 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13085 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13086 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13087 TREE_TYPE (node) = error_mark_node;
13091 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13094 /* 15.18 Shift Operators */
13098 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
13100 if (!JINTEGRAL_TYPE_P (op1_type))
13101 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13104 if (JNUMERIC_TYPE_P (op2_type))
13105 parse_error_context (wfl_operator,
13106 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
13107 operator_string (node),
13108 lang_printable_name (op2_type, 0));
13110 parse_error_context (wfl_operator,
13111 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
13112 operator_string (node),
13113 lang_printable_name (op2_type, 0));
13115 TREE_TYPE (node) = error_mark_node;
13120 /* Unary numeric promotion (5.6.1) is performed on each operand
13122 op1 = do_unary_numeric_promotion (op1);
13123 op2 = do_unary_numeric_promotion (op2);
13125 /* The type of the shift expression is the type of the promoted
13126 type of the left-hand operand */
13127 prom_type = TREE_TYPE (op1);
13129 /* Shift int only up to 0x1f and long up to 0x3f */
13130 if (prom_type == int_type_node)
13131 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13132 build_int_2 (0x1f, 0)));
13134 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
13135 build_int_2 (0x3f, 0)));
13137 /* The >>> operator is a >> operating on unsigned quantities */
13138 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
13141 tree utype = unsigned_type (prom_type);
13142 op1 = convert (utype, op1);
13143 TREE_SET_CODE (node, RSHIFT_EXPR);
13144 TREE_OPERAND (node, 0) = op1;
13145 TREE_OPERAND (node, 1) = op2;
13146 TREE_TYPE (node) = utype;
13147 to_return = convert (prom_type, node);
13148 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
13149 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
13150 TREE_SIDE_EFFECTS (to_return)
13151 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13156 /* 15.19.1 Type Comparison Operator instaceof */
13157 case INSTANCEOF_EXPR:
13159 TREE_TYPE (node) = boolean_type_node;
13161 if (!(op2_type = resolve_type_during_patch (op2)))
13162 return error_mark_node;
13164 /* The first operand must be a reference type or the null type */
13165 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
13166 error_found = 1; /* Error reported further below */
13168 /* The second operand must be a reference type */
13169 if (!JREFERENCE_TYPE_P (op2_type))
13171 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
13172 parse_error_context
13173 (wfl_operator, "Invalid argument `%s' for `instanceof'",
13174 lang_printable_name (op2_type, 0));
13178 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
13180 /* If the first operand is null, the result is always false */
13181 if (op1 == null_pointer_node)
13182 return boolean_false_node;
13183 else if (flag_emit_class_files)
13185 TREE_OPERAND (node, 1) = op2_type;
13186 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
13189 /* Otherwise we have to invoke instance of to figure it out */
13191 return build_instanceof (op1, op2_type);
13193 /* There is no way the expression operand can be an instance of
13194 the type operand. This is a compile time error. */
13197 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
13198 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
13199 parse_error_context
13200 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
13201 t1, lang_printable_name (op2_type, 0));
13208 /* 15.21 Bitwise and Logical Operators */
13212 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
13213 /* Binary numeric promotion is performed on both operand and the
13214 expression retain that type */
13215 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13217 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
13218 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
13219 /* The type of the bitwise operator expression is BOOLEAN */
13220 prom_type = boolean_type_node;
13223 if (!JINTEGRAL_TYPE_P (op1_type))
13224 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
13225 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
13226 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
13227 TREE_TYPE (node) = error_mark_node;
13229 /* Insert a break here if adding thing before the switch's
13230 break for this case */
13234 /* 15.22 Conditional-And Operator */
13235 case TRUTH_ANDIF_EXPR:
13236 /* 15.23 Conditional-Or Operator */
13237 case TRUTH_ORIF_EXPR:
13238 /* Operands must be of BOOLEAN type */
13239 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
13240 TREE_CODE (op2_type) != BOOLEAN_TYPE)
13242 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
13243 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
13244 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
13245 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
13246 TREE_TYPE (node) = boolean_type_node;
13250 /* The type of the conditional operators is BOOLEAN */
13251 prom_type = boolean_type_node;
13254 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
13259 /* The type of each of the operands must be a primitive numeric
13261 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
13263 if (!JNUMERIC_TYPE_P (op1_type))
13264 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
13265 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
13266 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
13267 TREE_TYPE (node) = boolean_type_node;
13271 /* Binary numeric promotion is performed on the operands */
13272 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13273 /* The type of the relation expression is always BOOLEAN */
13274 prom_type = boolean_type_node;
13277 /* 15.20 Equality Operator */
13280 /* It's time for us to patch the strings. */
13281 if ((cn = patch_string (op1)))
13284 op1_type = TREE_TYPE (op1);
13286 if ((cn = patch_string (op2)))
13289 op2_type = TREE_TYPE (op2);
13292 /* 15.20.1 Numerical Equality Operators == and != */
13293 /* Binary numeric promotion is performed on the operands */
13294 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
13295 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
13297 /* 15.20.2 Boolean Equality Operators == and != */
13298 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
13299 TREE_CODE (op2_type) == BOOLEAN_TYPE)
13300 ; /* Nothing to do here */
13302 /* 15.20.3 Reference Equality Operators == and != */
13303 /* Types have to be either references or the null type. If
13304 they're references, it must be possible to convert either
13305 type to the other by casting conversion. */
13306 else if (op1 == null_pointer_node || op2 == null_pointer_node
13307 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
13308 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
13309 || valid_ref_assignconv_cast_p (op2_type,
13311 ; /* Nothing to do here */
13313 /* Else we have an error figure what can't be converted into
13314 what and report the error */
13318 t1 = xstrdup (lang_printable_name (op1_type, 0));
13319 parse_error_context
13321 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
13322 operator_string (node), t1,
13323 lang_printable_name (op2_type, 0));
13325 TREE_TYPE (node) = boolean_type_node;
13329 prom_type = boolean_type_node;
13334 return error_mark_node;
13336 TREE_OPERAND (node, 0) = op1;
13337 TREE_OPERAND (node, 1) = op2;
13338 TREE_TYPE (node) = prom_type;
13339 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13341 if (flag_emit_xref)
13344 /* fold does not respect side-effect order as required for Java but not C.
13345 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
13348 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
13349 : ! TREE_SIDE_EFFECTS (node))
13350 node = fold (node);
13354 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
13355 zero value, the value of CSTE comes after the valude of STRING */
13358 do_merge_string_cste (cste, string, string_len, after)
13360 const char *string;
13361 int string_len, after;
13363 const char *old = TREE_STRING_POINTER (cste);
13364 int old_len = TREE_STRING_LENGTH (cste);
13365 int len = old_len + string_len;
13366 char *new = alloca (len+1);
13370 memcpy (new, string, string_len);
13371 memcpy (&new [string_len], old, old_len);
13375 memcpy (new, old, old_len);
13376 memcpy (&new [old_len], string, string_len);
13379 return build_string (len, new);
13382 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13383 new STRING_CST on success, NULL_TREE on failure */
13386 merge_string_cste (op1, op2, after)
13390 /* Handle two string constants right away */
13391 if (TREE_CODE (op2) == STRING_CST)
13392 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13393 TREE_STRING_LENGTH (op2), after);
13395 /* Reasonable integer constant can be treated right away */
13396 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13398 static const char *boolean_true = "true";
13399 static const char *boolean_false = "false";
13400 static const char *null_pointer = "null";
13402 const char *string;
13404 if (op2 == boolean_true_node)
13405 string = boolean_true;
13406 else if (op2 == boolean_false_node)
13407 string = boolean_false;
13408 else if (op2 == null_pointer_node)
13409 string = null_pointer;
13410 else if (TREE_TYPE (op2) == char_type_node)
13412 ch[0] = (char )TREE_INT_CST_LOW (op2);
13417 string = print_int_node (op2);
13419 return do_merge_string_cste (op1, string, strlen (string), after);
13424 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13425 has to be a STRING_CST and the other part must be a STRING_CST or a
13426 INTEGRAL constant. Return a new STRING_CST if the operation
13427 succeed, NULL_TREE otherwise.
13429 If the case we want to optimize for space, we might want to return
13430 NULL_TREE for each invocation of this routine. FIXME */
13433 string_constant_concatenation (op1, op2)
13436 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13441 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13442 rest = (string == op1 ? op2 : op1);
13443 invert = (string == op1 ? 0 : 1 );
13445 /* Walk REST, only if it looks reasonable */
13446 if (TREE_CODE (rest) != STRING_CST
13447 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13448 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13449 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13451 rest = java_complete_tree (rest);
13452 if (rest == error_mark_node)
13453 return error_mark_node;
13454 rest = fold (rest);
13456 return merge_string_cste (string, rest, invert);
13461 /* Implement the `+' operator. Does static optimization if possible,
13462 otherwise create (if necessary) and append elements to a
13463 StringBuffer. The StringBuffer will be carried around until it is
13464 used for a function call or an assignment. Then toString() will be
13465 called on it to turn it into a String object. */
13468 build_string_concatenation (op1, op2)
13472 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13474 if (flag_emit_xref)
13475 return build (PLUS_EXPR, string_type_node, op1, op2);
13477 /* Try to do some static optimization */
13478 if ((result = string_constant_concatenation (op1, op2)))
13481 /* Discard empty strings on either side of the expression */
13482 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13487 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13490 /* If operands are string constant, turn then into object references */
13491 if (TREE_CODE (op1) == STRING_CST)
13492 op1 = patch_string_cst (op1);
13493 if (op2 && TREE_CODE (op2) == STRING_CST)
13494 op2 = patch_string_cst (op2);
13496 /* If either one of the constant is null and the other non null
13497 operand is a String object, return it. */
13498 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13501 /* If OP1 isn't already a StringBuffer, create and
13502 initialize a new one */
13503 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13505 /* Two solutions here:
13506 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13507 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13508 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13509 op1 = BUILD_STRING_BUFFER (op1);
13512 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13513 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13519 /* OP1 is no longer the last node holding a crafted StringBuffer */
13520 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13521 /* Create a node for `{new...,xxx}.append (op2)' */
13523 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13526 /* Mark the last node holding a crafted StringBuffer */
13527 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13529 TREE_SIDE_EFFECTS (op1) = side_effects;
13533 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13534 StringBuffer. If no string were found to be patched, return
13538 patch_string (node)
13541 if (node == error_mark_node)
13542 return error_mark_node;
13543 if (TREE_CODE (node) == STRING_CST)
13544 return patch_string_cst (node);
13545 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13547 int saved = ctxp->explicit_constructor_p;
13548 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13550 /* Temporary disable forbid the use of `this'. */
13551 ctxp->explicit_constructor_p = 0;
13552 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13553 /* String concatenation arguments must be evaluated in order too. */
13554 ret = force_evaluation_order (ret);
13555 /* Restore it at its previous value */
13556 ctxp->explicit_constructor_p = saved;
13562 /* Build the internal representation of a string constant. */
13565 patch_string_cst (node)
13569 if (! flag_emit_class_files)
13571 node = get_identifier (TREE_STRING_POINTER (node));
13572 location = alloc_name_constant (CONSTANT_String, node);
13573 node = build_ref_from_constant_pool (location);
13575 TREE_TYPE (node) = string_ptr_type_node;
13576 TREE_CONSTANT (node) = 1;
13580 /* Build an incomplete unary operator expression. */
13583 build_unaryop (op_token, op_location, op1)
13584 int op_token, op_location;
13591 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13592 case MINUS_TK: op = NEGATE_EXPR; break;
13593 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13594 case NOT_TK: op = BIT_NOT_EXPR; break;
13598 unaryop = build1 (op, NULL_TREE, op1);
13599 TREE_SIDE_EFFECTS (unaryop) = 1;
13600 /* Store the location of the operator, for better error report. The
13601 string of the operator will be rebuild based on the OP value. */
13602 EXPR_WFL_LINECOL (unaryop) = op_location;
13606 /* Special case for the ++/-- operators, since they require an extra
13607 argument to build, which is set to NULL and patched
13608 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13611 build_incdec (op_token, op_location, op1, is_post_p)
13612 int op_token, op_location;
13616 static enum tree_code lookup [2][2] =
13618 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13619 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13621 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13622 NULL_TREE, op1, NULL_TREE);
13623 TREE_SIDE_EFFECTS (node) = 1;
13624 /* Store the location of the operator, for better error report. The
13625 string of the operator will be rebuild based on the OP value. */
13626 EXPR_WFL_LINECOL (node) = op_location;
13630 /* Build an incomplete cast operator, based on the use of the
13631 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13632 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13633 though its type is already set. */
13636 build_cast (location, type, exp)
13640 tree node = build1 (CONVERT_EXPR, type, exp);
13641 EXPR_WFL_LINECOL (node) = location;
13645 /* Build an incomplete class reference operator. */
13647 build_incomplete_class_ref (location, class_name)
13651 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13652 EXPR_WFL_LINECOL (node) = location;
13656 /* Complete an incomplete class reference operator. */
13658 patch_incomplete_class_ref (node)
13661 tree type = TREE_OPERAND (node, 0);
13664 if (!(ref_type = resolve_type_during_patch (type)))
13665 return error_mark_node;
13667 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
13669 /* A class referenced by `foo.class' is initialized. */
13670 return build_class_init (ref_type, build_class_ref (ref_type));
13673 /* If we're emitting class files and we have to deal with non
13674 primitive types, we invoke (and consider generating) the
13675 synthetic static method `class$'. */
13676 if (!TYPE_DOT_CLASS (current_class))
13677 build_dot_class_method (current_class);
13678 ref_type = build_dot_class_method_invocation (ref_type);
13679 return java_complete_tree (ref_type);
13682 /* 15.14 Unary operators. We return error_mark_node in case of error,
13683 but preserve the type of NODE if the type is fixed. */
13686 patch_unaryop (node, wfl_op)
13690 tree op = TREE_OPERAND (node, 0);
13691 tree op_type = TREE_TYPE (op);
13692 tree prom_type = NULL_TREE, value, decl;
13693 int outer_field_flag = 0;
13694 int code = TREE_CODE (node);
13695 int error_found = 0;
13697 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13701 /* 15.13.2 Postfix Increment Operator ++ */
13702 case POSTINCREMENT_EXPR:
13703 /* 15.13.3 Postfix Increment Operator -- */
13704 case POSTDECREMENT_EXPR:
13705 /* 15.14.1 Prefix Increment Operator ++ */
13706 case PREINCREMENT_EXPR:
13707 /* 15.14.2 Prefix Decrement Operator -- */
13708 case PREDECREMENT_EXPR:
13709 op = decl = strip_out_static_field_access_decl (op);
13710 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13711 /* We might be trying to change an outer field accessed using
13713 if (outer_field_flag)
13715 /* Retrieve the decl of the field we're trying to access. We
13716 do that by first retrieving the function we would call to
13717 access the field. It has been already verified that this
13718 field isn't final */
13719 if (flag_emit_class_files)
13720 decl = TREE_OPERAND (op, 0);
13722 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13723 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13725 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
13726 else if (!JDECL_P (decl)
13727 && TREE_CODE (decl) != COMPONENT_REF
13728 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13729 && TREE_CODE (decl) != INDIRECT_REF
13730 && !(TREE_CODE (decl) == COMPOUND_EXPR
13731 && TREE_OPERAND (decl, 1)
13732 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
13735 /* Before screaming, check that we're not in fact trying to
13736 increment a optimized static final access, in which case
13737 we issue an different error message. */
13738 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13739 && resolve_expression_name (wfl_op, &lvalue)
13740 && check_final_assignment (lvalue, wfl_op)))
13741 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13742 operator_string (node));
13743 TREE_TYPE (node) = error_mark_node;
13747 if (check_final_assignment (op, wfl_op))
13750 /* From now on, we know that op if a variable and that it has a
13751 valid wfl. We use wfl_op to locate errors related to the
13753 else if (!JNUMERIC_TYPE_P (op_type))
13755 parse_error_context
13756 (wfl_op, "Invalid argument type `%s' to `%s'",
13757 lang_printable_name (op_type, 0), operator_string (node));
13758 TREE_TYPE (node) = error_mark_node;
13763 /* Before the addition, binary numeric promotion is performed on
13764 both operands, if really necessary */
13765 if (JINTEGRAL_TYPE_P (op_type))
13767 value = build_int_2 (1, 0);
13768 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13772 value = build_int_2 (1, 0);
13774 binary_numeric_promotion (op_type,
13775 TREE_TYPE (value), &op, &value);
13778 /* We remember we might be accessing an outer field */
13779 if (outer_field_flag)
13781 /* We re-generate an access to the field */
13782 value = build (PLUS_EXPR, TREE_TYPE (op),
13783 build_outer_field_access (wfl_op, decl), value);
13785 /* And we patch the original access$() into a write
13786 with plus_op as a rhs */
13787 return outer_field_access_fix (node, op, value);
13790 /* And write back into the node. */
13791 TREE_OPERAND (node, 0) = op;
13792 TREE_OPERAND (node, 1) = value;
13793 /* Convert the overall back into its original type, if
13794 necessary, and return */
13795 if (JINTEGRAL_TYPE_P (op_type))
13796 return fold (node);
13798 return fold (convert (op_type, node));
13802 /* 15.14.3 Unary Plus Operator + */
13803 case UNARY_PLUS_EXPR:
13804 /* 15.14.4 Unary Minus Operator - */
13806 if (!JNUMERIC_TYPE_P (op_type))
13808 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13809 TREE_TYPE (node) = error_mark_node;
13812 /* Unary numeric promotion is performed on operand */
13815 op = do_unary_numeric_promotion (op);
13816 prom_type = TREE_TYPE (op);
13817 if (code == UNARY_PLUS_EXPR)
13822 /* 15.14.5 Bitwise Complement Operator ~ */
13824 if (!JINTEGRAL_TYPE_P (op_type))
13826 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13827 TREE_TYPE (node) = error_mark_node;
13832 op = do_unary_numeric_promotion (op);
13833 prom_type = TREE_TYPE (op);
13837 /* 15.14.6 Logical Complement Operator ! */
13838 case TRUTH_NOT_EXPR:
13839 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13841 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
13842 /* But the type is known. We will report an error if further
13843 attempt of a assignment is made with this rhs */
13844 TREE_TYPE (node) = boolean_type_node;
13848 prom_type = boolean_type_node;
13851 /* 15.15 Cast Expression */
13853 value = patch_cast (node, wfl_operator);
13854 if (value == error_mark_node)
13856 /* If this cast is part of an assignment, we tell the code
13857 that deals with it not to complain about a mismatch,
13858 because things have been cast, anyways */
13859 TREE_TYPE (node) = error_mark_node;
13864 value = fold (value);
13865 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13872 return error_mark_node;
13874 /* There are cases where node has been replaced by something else
13875 and we don't end up returning here: UNARY_PLUS_EXPR,
13876 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
13877 TREE_OPERAND (node, 0) = fold (op);
13878 TREE_TYPE (node) = prom_type;
13879 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
13880 return fold (node);
13883 /* Generic type resolution that sometimes takes place during node
13884 patching. Returned the resolved type or generate an error
13885 message. Return the resolved type or NULL_TREE. */
13888 resolve_type_during_patch (type)
13891 if (unresolved_type_p (type, NULL))
13893 tree type_decl = resolve_and_layout (EXPR_WFL_NODE (type), type);
13896 parse_error_context (type,
13897 "Class `%s' not found in type declaration",
13898 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13903 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13904 return TREE_TYPE (type_decl);
13909 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
13910 found. Otherwise NODE or something meant to replace it is returned. */
13913 patch_cast (node, wfl_op)
13917 tree op = TREE_OPERAND (node, 0);
13918 tree op_type = TREE_TYPE (op);
13919 tree cast_type = TREE_TYPE (node);
13922 /* First resolve OP_TYPE if unresolved */
13923 if (!(cast_type = resolve_type_during_patch (cast_type)))
13924 return error_mark_node;
13926 /* Check on cast that are proven correct at compile time */
13927 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13930 if (cast_type == op_type)
13933 /* float and double type are converted to the original type main
13934 variant and then to the target type. */
13935 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13936 op = convert (integer_type_node, op);
13938 /* Try widening/narowwing convertion. Potentially, things need
13939 to be worked out in gcc so we implement the extreme cases
13940 correctly. fold_convert() needs to be fixed. */
13941 return convert (cast_type, op);
13944 /* It's also valid to cast a boolean into a boolean */
13945 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13948 /* null can be casted to references */
13949 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13950 return build_null_of_type (cast_type);
13952 /* The remaining legal casts involve conversion between reference
13953 types. Check for their compile time correctness. */
13954 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
13955 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
13957 TREE_TYPE (node) = promote_type (cast_type);
13958 /* Now, the case can be determined correct at compile time if
13959 OP_TYPE can be converted into CAST_TYPE by assignment
13960 conversion (5.2) */
13962 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
13964 TREE_SET_CODE (node, NOP_EXPR);
13968 if (flag_emit_class_files)
13970 TREE_SET_CODE (node, CONVERT_EXPR);
13974 /* The cast requires a run-time check */
13975 return build (CALL_EXPR, promote_type (cast_type),
13976 build_address_of (soft_checkcast_node),
13977 tree_cons (NULL_TREE, build_class_ref (cast_type),
13978 build_tree_list (NULL_TREE, op)),
13982 /* Any other casts are proven incorrect at compile time */
13983 t1 = xstrdup (lang_printable_name (op_type, 0));
13984 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
13985 t1, lang_printable_name (cast_type, 0));
13987 return error_mark_node;
13990 /* Build a null constant and give it the type TYPE. */
13993 build_null_of_type (type)
13996 tree node = build_int_2 (0, 0);
13997 TREE_TYPE (node) = promote_type (type);
14001 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
14002 a list of indices. */
14004 build_array_ref (location, array, index)
14008 tree node = build (ARRAY_REF, NULL_TREE, array, index);
14009 EXPR_WFL_LINECOL (node) = location;
14013 /* 15.12 Array Access Expression */
14016 patch_array_ref (node)
14019 tree array = TREE_OPERAND (node, 0);
14020 tree array_type = TREE_TYPE (array);
14021 tree index = TREE_OPERAND (node, 1);
14022 tree index_type = TREE_TYPE (index);
14023 int error_found = 0;
14025 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14027 if (TREE_CODE (array_type) == POINTER_TYPE)
14028 array_type = TREE_TYPE (array_type);
14030 /* The array reference must be an array */
14031 if (!TYPE_ARRAY_P (array_type))
14033 parse_error_context
14035 "`[]' can only be applied to arrays. It can't be applied to `%s'",
14036 lang_printable_name (array_type, 0));
14037 TREE_TYPE (node) = error_mark_node;
14041 /* The array index undergoes unary numeric promotion. The promoted
14042 type must be int */
14043 index = do_unary_numeric_promotion (index);
14044 if (TREE_TYPE (index) != int_type_node)
14046 if (valid_cast_to_p (index_type, int_type_node))
14047 parse_error_context (wfl_operator,
14048 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
14049 lang_printable_name (index_type, 0));
14051 parse_error_context (wfl_operator,
14052 "Incompatible type for `[]'. Can't convert `%s' to `int'",
14053 lang_printable_name (index_type, 0));
14054 TREE_TYPE (node) = error_mark_node;
14059 return error_mark_node;
14061 array_type = TYPE_ARRAY_ELEMENT (array_type);
14063 if (flag_emit_class_files || flag_emit_xref)
14065 TREE_OPERAND (node, 0) = array;
14066 TREE_OPERAND (node, 1) = index;
14070 /* The save_expr is for correct evaluation order. It would be cleaner
14071 to use force_evaluation_order (see comment there), but that is
14072 difficult when we also have to deal with bounds checking. */
14073 if (TREE_SIDE_EFFECTS (index))
14074 array = save_expr (array);
14075 node = build_java_arrayaccess (array, array_type, index);
14076 if (TREE_SIDE_EFFECTS (index))
14077 node = build (COMPOUND_EXPR, array_type, array, node);
14079 TREE_TYPE (node) = array_type;
14083 /* 15.9 Array Creation Expressions */
14086 build_newarray_node (type, dims, extra_dims)
14092 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
14093 build_int_2 (extra_dims, 0));
14098 patch_newarray (node)
14101 tree type = TREE_OPERAND (node, 0);
14102 tree dims = TREE_OPERAND (node, 1);
14103 tree cdim, array_type;
14104 int error_found = 0;
14106 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
14108 /* Dimension types are verified. It's better for the types to be
14109 verified in order. */
14110 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
14113 tree dim = TREE_VALUE (cdim);
14115 /* Dim might have been saved during its evaluation */
14116 dim = (TREE_CODE (dim) == SAVE_EXPR ? TREE_OPERAND (dim, 0) : dim);
14118 /* The type of each specified dimension must be an integral type. */
14119 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
14122 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
14123 promoted type must be int. */
14126 dim = do_unary_numeric_promotion (dim);
14127 if (TREE_TYPE (dim) != int_type_node)
14131 /* Report errors on types here */
14134 parse_error_context
14135 (TREE_PURPOSE (cdim),
14136 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
14137 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
14138 "Explicit cast needed to" : "Can't"),
14139 lang_printable_name (TREE_TYPE (dim), 0));
14143 TREE_PURPOSE (cdim) = NULL_TREE;
14146 /* Resolve array base type if unresolved */
14147 if (!(type = resolve_type_during_patch (type)))
14152 /* We don't want further evaluation of this bogus array creation
14154 TREE_TYPE (node) = error_mark_node;
14155 return error_mark_node;
14158 /* Set array_type to the actual (promoted) array type of the result. */
14159 if (TREE_CODE (type) == RECORD_TYPE)
14160 type = build_pointer_type (type);
14161 while (--xdims >= 0)
14163 type = promote_type (build_java_array_type (type, -1));
14165 dims = nreverse (dims);
14167 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
14171 = build_java_array_type (type,
14172 TREE_CODE (cdim) == INTEGER_CST
14173 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
14175 array_type = promote_type (array_type);
14177 dims = nreverse (dims);
14179 /* The node is transformed into a function call. Things are done
14180 differently according to the number of dimensions. If the number
14181 of dimension is equal to 1, then the nature of the base type
14182 (primitive or not) matters. */
14184 return build_new_array (type, TREE_VALUE (dims));
14186 /* Can't reuse what's already written in expr.c because it uses the
14187 JVM stack representation. Provide a build_multianewarray. FIXME */
14188 return build (CALL_EXPR, array_type,
14189 build_address_of (soft_multianewarray_node),
14190 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
14191 tree_cons (NULL_TREE,
14192 build_int_2 (ndims, 0), dims )),
14196 /* 10.6 Array initializer. */
14198 /* Build a wfl for array element that don't have one, so we can
14199 pin-point errors. */
14202 maybe_build_array_element_wfl (node)
14205 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
14206 return build_expr_wfl (NULL_TREE, ctxp->filename,
14207 ctxp->elc.line, ctxp->elc.prev_col);
14212 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
14213 identification of initialized arrays easier to detect during walk
14217 build_new_array_init (location, values)
14221 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
14222 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
14223 EXPR_WFL_LINECOL (to_return) = location;
14227 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
14228 occurred. Otherwise return NODE after having set its type
14232 patch_new_array_init (type, node)
14235 int error_seen = 0;
14236 tree current, element_type;
14237 HOST_WIDE_INT length;
14238 int all_constant = 1;
14239 tree init = TREE_OPERAND (node, 0);
14241 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
14243 parse_error_context (node,
14244 "Invalid array initializer for non-array type `%s'",
14245 lang_printable_name (type, 1));
14246 return error_mark_node;
14248 type = TREE_TYPE (type);
14249 element_type = TYPE_ARRAY_ELEMENT (type);
14251 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
14253 for (length = 0, current = CONSTRUCTOR_ELTS (init);
14254 current; length++, current = TREE_CHAIN (current))
14256 tree elt = TREE_VALUE (current);
14257 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
14259 error_seen |= array_constructor_check_entry (element_type, current);
14260 elt = TREE_VALUE (current);
14261 /* When compiling to native code, STRING_CST is converted to
14262 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
14263 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
14268 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
14269 TREE_PURPOSE (current) = NULL_TREE;
14272 if (elt && TREE_CODE (elt) == TREE_LIST
14273 && TREE_VALUE (elt) == error_mark_node)
14278 return error_mark_node;
14280 /* Create a new type. We can't reuse the one we have here by
14281 patching its dimension because it originally is of dimension -1
14282 hence reused by gcc. This would prevent triangular arrays. */
14283 type = build_java_array_type (element_type, length);
14284 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
14285 TREE_TYPE (node) = promote_type (type);
14286 TREE_CONSTANT (init) = all_constant;
14287 TREE_CONSTANT (node) = all_constant;
14291 /* Verify that one entry of the initializer element list can be
14292 assigned to the array base type. Report 1 if an error occurred, 0
14296 array_constructor_check_entry (type, entry)
14299 char *array_type_string = NULL; /* For error reports */
14300 tree value, type_value, new_value, wfl_value, patched;
14301 int error_seen = 0;
14303 new_value = NULL_TREE;
14304 wfl_value = TREE_VALUE (entry);
14306 value = java_complete_tree (TREE_VALUE (entry));
14307 /* patch_string return error_mark_node if arg is error_mark_node */
14308 if ((patched = patch_string (value)))
14310 if (value == error_mark_node)
14313 type_value = TREE_TYPE (value);
14315 /* At anytime, try_builtin_assignconv can report a warning on
14316 constant overflow during narrowing. */
14317 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
14318 new_value = try_builtin_assignconv (wfl_operator, type, value);
14319 if (!new_value && (new_value = try_reference_assignconv (type, value)))
14320 type_value = promote_type (type);
14322 /* Check and report errors */
14325 const char *msg = (!valid_cast_to_p (type_value, type) ?
14326 "Can't" : "Explicit cast needed to");
14327 if (!array_type_string)
14328 array_type_string = xstrdup (lang_printable_name (type, 1));
14329 parse_error_context
14330 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
14331 msg, lang_printable_name (type_value, 1), array_type_string);
14337 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
14338 TREE_VALUE (entry) = new_value;
14341 if (array_type_string)
14342 free (array_type_string);
14344 TREE_PURPOSE (entry) = NULL_TREE;
14349 build_this (location)
14352 tree node = build_wfl_node (this_identifier_node);
14353 TREE_SET_CODE (node, THIS_EXPR);
14354 EXPR_WFL_LINECOL (node) = location;
14358 /* 14.15 The return statement. It builds a modify expression that
14359 assigns the returned value to the RESULT_DECL that hold the value
14363 build_return (location, op)
14367 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14368 EXPR_WFL_LINECOL (node) = location;
14369 node = build_debugable_stmt (location, node);
14374 patch_return (node)
14377 tree return_exp = TREE_OPERAND (node, 0);
14378 tree meth = current_function_decl;
14379 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14380 int error_found = 0;
14382 TREE_TYPE (node) = error_mark_node;
14383 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14385 /* It's invalid to have a return value within a function that is
14386 declared with the keyword void or that is a constructor */
14387 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14390 /* It's invalid to use a return statement in a static block */
14391 if (DECL_CLINIT_P (current_function_decl))
14394 /* It's invalid to have a no return value within a function that
14395 isn't declared with the keyword `void' */
14396 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14399 if (in_instance_initializer)
14404 if (in_instance_initializer)
14405 parse_error_context (wfl_operator,
14406 "`return' inside instance initializer");
14408 else if (DECL_CLINIT_P (current_function_decl))
14409 parse_error_context (wfl_operator,
14410 "`return' inside static initializer");
14412 else if (!DECL_CONSTRUCTOR_P (meth))
14414 char *t = xstrdup (lang_printable_name (mtype, 0));
14415 parse_error_context (wfl_operator,
14416 "`return' with%s value from `%s %s'",
14417 (error_found == 1 ? "" : "out"),
14418 t, lang_printable_name (meth, 0));
14422 parse_error_context (wfl_operator,
14423 "`return' with value from constructor `%s'",
14424 lang_printable_name (meth, 0));
14425 return error_mark_node;
14428 /* If we have a return_exp, build a modify expression and expand
14429 it. Note: at that point, the assignment is declared valid, but we
14430 may want to carry some more hacks */
14433 tree exp = java_complete_tree (return_exp);
14434 tree modify, patched;
14436 /* If the function returned value and EXP are booleans, EXP has
14437 to be converted into the type of DECL_RESULT, which is integer
14438 (see complete_start_java_method) */
14439 if (TREE_TYPE (exp) == boolean_type_node &&
14440 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14441 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14443 /* `null' can be assigned to a function returning a reference */
14444 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14445 exp == null_pointer_node)
14446 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14448 if ((patched = patch_string (exp)))
14451 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14452 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14453 modify = java_complete_tree (modify);
14455 if (modify != error_mark_node)
14457 TREE_SIDE_EFFECTS (modify) = 1;
14458 TREE_OPERAND (node, 0) = modify;
14461 return error_mark_node;
14463 TREE_TYPE (node) = void_type_node;
14464 TREE_SIDE_EFFECTS (node) = 1;
14468 /* 14.8 The if Statement */
14471 build_if_else_statement (location, expression, if_body, else_body)
14473 tree expression, if_body, else_body;
14477 else_body = empty_stmt_node;
14478 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14479 EXPR_WFL_LINECOL (node) = location;
14480 node = build_debugable_stmt (location, node);
14485 patch_if_else_statement (node)
14488 tree expression = TREE_OPERAND (node, 0);
14490 TREE_TYPE (node) = error_mark_node;
14491 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14493 /* The type of expression must be boolean */
14494 if (TREE_TYPE (expression) != boolean_type_node
14495 && TREE_TYPE (expression) != promoted_boolean_type_node)
14497 parse_error_context
14499 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
14500 lang_printable_name (TREE_TYPE (expression), 0));
14501 return error_mark_node;
14504 TREE_TYPE (node) = void_type_node;
14505 TREE_SIDE_EFFECTS (node) = 1;
14506 CAN_COMPLETE_NORMALLY (node)
14507 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14508 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
14512 /* 14.6 Labeled Statements */
14514 /* Action taken when a lableled statement is parsed. a new
14515 LABELED_BLOCK_EXPR is created. No statement is attached to the
14516 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
14519 build_labeled_block (location, label)
14524 tree label_decl, node;
14525 if (label == NULL_TREE || label == continue_identifier_node)
14526 label_name = label;
14529 label_name = merge_qualified_name (label_id, label);
14530 /* Issue an error if we try to reuse a label that was previously
14532 if (IDENTIFIER_LOCAL_VALUE (label_name))
14534 EXPR_WFL_LINECOL (wfl_operator) = location;
14535 parse_error_context (wfl_operator,
14536 "Declaration of `%s' shadows a previous label declaration",
14537 IDENTIFIER_POINTER (label));
14538 EXPR_WFL_LINECOL (wfl_operator) =
14539 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14540 parse_error_context (wfl_operator,
14541 "This is the location of the previous declaration of label `%s'",
14542 IDENTIFIER_POINTER (label));
14543 java_error_count--;
14547 label_decl = create_label_decl (label_name);
14548 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14549 EXPR_WFL_LINECOL (node) = location;
14550 TREE_SIDE_EFFECTS (node) = 1;
14554 /* A labeled statement LBE is attached a statement. */
14557 finish_labeled_statement (lbe, statement)
14558 tree lbe; /* Labeled block expr */
14561 /* In anyways, tie the loop to its statement */
14562 LABELED_BLOCK_BODY (lbe) = statement;
14563 pop_labeled_block ();
14564 POP_LABELED_BLOCK ();
14568 /* 14.10, 14.11, 14.12 Loop Statements */
14570 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14574 build_new_loop (loop_body)
14577 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14578 TREE_SIDE_EFFECTS (loop) = 1;
14583 /* Create a loop body according to the following structure:
14585 COMPOUND_EXPR (loop main body)
14586 EXIT_EXPR (this order is for while/for loops.
14587 LABELED_BLOCK_EXPR the order is reversed for do loops)
14588 LABEL_DECL (a continue occuring here branches at the
14589 BODY end of this labeled block)
14592 REVERSED, if non zero, tells that the loop condition expr comes
14593 after the body, like in the do-while loop.
14595 To obtain a loop, the loop body structure described above is
14596 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14599 LABEL_DECL (use this label to exit the loop)
14601 <structure described above> */
14604 build_loop_body (location, condition, reversed)
14609 tree first, second, body;
14611 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14612 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14613 condition = build_debugable_stmt (location, condition);
14614 TREE_SIDE_EFFECTS (condition) = 1;
14616 body = build_labeled_block (0, continue_identifier_node);
14617 first = (reversed ? body : condition);
14618 second = (reversed ? condition : body);
14620 build (COMPOUND_EXPR, NULL_TREE,
14621 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14624 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14625 their order) on the current loop. Unlink the current loop from the
14629 finish_loop_body (location, condition, body, reversed)
14631 tree condition, body;
14634 tree to_return = ctxp->current_loop;
14635 tree loop_body = LOOP_EXPR_BODY (to_return);
14638 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14639 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14640 The real EXIT_EXPR is one operand further. */
14641 EXPR_WFL_LINECOL (cnode) = location;
14642 /* This one is for accurate error reports */
14643 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14644 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14646 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14651 /* Tailored version of finish_loop_body for FOR loops, when FOR
14652 loops feature the condition part */
14655 finish_for_loop (location, condition, update, body)
14657 tree condition, update, body;
14659 /* Put the condition and the loop body in place */
14660 tree loop = finish_loop_body (location, condition, body, 0);
14661 /* LOOP is the current loop which has been now popped of the loop
14662 stack. Install the update block */
14663 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14667 /* Try to find the loop a block might be related to. This comprises
14668 the case where the LOOP_EXPR is found as the second operand of a
14669 COMPOUND_EXPR, because the loop happens to have an initialization
14670 part, then expressed as the first operand of the COMPOUND_EXPR. If
14671 the search finds something, 1 is returned. Otherwise, 0 is
14672 returned. The search is assumed to start from a
14673 LABELED_BLOCK_EXPR's block. */
14676 search_loop (statement)
14679 if (TREE_CODE (statement) == LOOP_EXPR)
14682 if (TREE_CODE (statement) == BLOCK)
14683 statement = BLOCK_SUBBLOCKS (statement);
14687 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14688 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14689 statement = TREE_OPERAND (statement, 1);
14691 return (TREE_CODE (statement) == LOOP_EXPR
14692 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
14695 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14696 returned otherwise. */
14699 labeled_block_contains_loop_p (block, loop)
14705 if (LABELED_BLOCK_BODY (block) == loop)
14708 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
14714 /* If the loop isn't surrounded by a labeled statement, create one and
14715 insert LOOP as its body. */
14718 patch_loop_statement (loop)
14723 TREE_TYPE (loop) = void_type_node;
14724 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14727 loop_label = build_labeled_block (0, NULL_TREE);
14728 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14729 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
14730 LABELED_BLOCK_BODY (loop_label) = loop;
14731 PUSH_LABELED_BLOCK (loop_label);
14735 /* 14.13, 14.14: break and continue Statements */
14737 /* Build a break or a continue statement. a null NAME indicates an
14738 unlabeled break/continue statement. */
14741 build_bc_statement (location, is_break, name)
14742 int location, is_break;
14745 tree break_continue, label_block_expr = NULL_TREE;
14749 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14750 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14751 /* Null means that we don't have a target for this named
14752 break/continue. In this case, we make the target to be the
14753 label name, so that the error can be reported accuratly in
14754 patch_bc_statement. */
14755 label_block_expr = EXPR_WFL_NODE (name);
14757 /* Unlabeled break/continue will be handled during the
14758 break/continue patch operation */
14760 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14762 IS_BREAK_STMT_P (break_continue) = is_break;
14763 TREE_SIDE_EFFECTS (break_continue) = 1;
14764 EXPR_WFL_LINECOL (break_continue) = location;
14765 break_continue = build_debugable_stmt (location, break_continue);
14766 return break_continue;
14769 /* Verification of a break/continue statement. */
14772 patch_bc_statement (node)
14775 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
14776 tree labeled_block = ctxp->current_labeled_block;
14777 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14779 /* Having an identifier here means that the target is unknown. */
14780 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
14782 parse_error_context (wfl_operator, "No label definition found for `%s'",
14783 IDENTIFIER_POINTER (bc_label));
14784 return error_mark_node;
14786 if (! IS_BREAK_STMT_P (node))
14788 /* It's a continue statement. */
14789 for (;; labeled_block = TREE_CHAIN (labeled_block))
14791 if (labeled_block == NULL_TREE)
14793 if (bc_label == NULL_TREE)
14794 parse_error_context (wfl_operator,
14795 "`continue' must be in loop");
14797 parse_error_context
14798 (wfl_operator, "continue label `%s' does not name a loop",
14799 IDENTIFIER_POINTER (bc_label));
14800 return error_mark_node;
14802 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14803 == continue_identifier_node)
14804 && (bc_label == NULL_TREE
14805 || TREE_CHAIN (labeled_block) == bc_label))
14807 bc_label = labeled_block;
14812 else if (!bc_label)
14814 for (;; labeled_block = TREE_CHAIN (labeled_block))
14816 if (labeled_block == NULL_TREE)
14818 parse_error_context (wfl_operator,
14819 "`break' must be in loop or switch");
14820 return error_mark_node;
14822 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14823 if (TREE_CODE (target_stmt) == SWITCH_EXPR
14824 || search_loop (target_stmt))
14826 bc_label = labeled_block;
14832 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
14833 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14835 /* Our break/continue don't return values. */
14836 TREE_TYPE (node) = void_type_node;
14837 /* Encapsulate the break within a compound statement so that it's
14838 expanded all the times by expand_expr (and not clobbered
14839 sometimes, like after a if statement) */
14840 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14841 TREE_SIDE_EFFECTS (node) = 1;
14845 /* Process the exit expression belonging to a loop. Its type must be
14849 patch_exit_expr (node)
14852 tree expression = TREE_OPERAND (node, 0);
14853 TREE_TYPE (node) = error_mark_node;
14854 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14856 /* The type of expression must be boolean */
14857 if (TREE_TYPE (expression) != boolean_type_node)
14859 parse_error_context
14861 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
14862 lang_printable_name (TREE_TYPE (expression), 0));
14863 return error_mark_node;
14865 /* Now we know things are allright, invert the condition, fold and
14867 TREE_OPERAND (node, 0) =
14868 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
14870 if (! integer_zerop (TREE_OPERAND (node, 0))
14871 && ctxp->current_loop != NULL_TREE
14872 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14873 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14874 if (! integer_onep (TREE_OPERAND (node, 0)))
14875 CAN_COMPLETE_NORMALLY (node) = 1;
14878 TREE_TYPE (node) = void_type_node;
14882 /* 14.9 Switch statement */
14885 patch_switch_statement (node)
14888 tree se = TREE_OPERAND (node, 0), se_type;
14890 /* Complete the switch expression */
14891 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14892 se_type = TREE_TYPE (se);
14893 /* The type of the switch expression must be char, byte, short or
14895 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
14897 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14898 parse_error_context (wfl_operator,
14899 "Incompatible type for `switch'. Can't convert `%s' to `int'",
14900 lang_printable_name (se_type, 0));
14901 /* This is what java_complete_tree will check */
14902 TREE_OPERAND (node, 0) = error_mark_node;
14903 return error_mark_node;
14906 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
14908 /* Ready to return */
14909 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
14911 TREE_TYPE (node) = error_mark_node;
14912 return error_mark_node;
14914 TREE_TYPE (node) = void_type_node;
14915 TREE_SIDE_EFFECTS (node) = 1;
14916 CAN_COMPLETE_NORMALLY (node)
14917 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14918 || ! SWITCH_HAS_DEFAULT (node);
14922 /* 14.18 The try/catch statements */
14925 build_try_statement (location, try_block, catches)
14927 tree try_block, catches;
14929 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
14930 EXPR_WFL_LINECOL (node) = location;
14935 build_try_finally_statement (location, try_block, finally)
14937 tree try_block, finally;
14939 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14940 EXPR_WFL_LINECOL (node) = location;
14945 patch_try_statement (node)
14948 int error_found = 0;
14949 tree try = TREE_OPERAND (node, 0);
14950 /* Exception handlers are considered in left to right order */
14951 tree catch = nreverse (TREE_OPERAND (node, 1));
14952 tree current, caught_type_list = NULL_TREE;
14954 /* Check catch clauses, if any. Every time we find an error, we try
14955 to process the next catch clause. We process the catch clause before
14956 the try block so that when processing the try block we can check thrown
14957 exceptions againts the caught type list. */
14958 for (current = catch; current; current = TREE_CHAIN (current))
14960 tree carg_decl, carg_type;
14961 tree sub_current, catch_block, catch_clause;
14964 /* At this point, the structure of the catch clause is
14965 CATCH_EXPR (catch node)
14966 BLOCK (with the decl of the parameter)
14968 MODIFY_EXPR (assignment of the catch parameter)
14969 BLOCK (catch clause block)
14971 catch_clause = TREE_OPERAND (current, 0);
14972 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14973 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14975 /* Catch clauses can't have more than one parameter declared,
14976 but it's already enforced by the grammar. Make sure that the
14977 only parameter of the clause statement in of class Throwable
14978 or a subclass of Throwable, but that was done earlier. The
14979 catch clause parameter type has also been resolved. */
14981 /* Just make sure that the catch clause parameter type inherits
14982 from java.lang.Throwable */
14983 if (!inherits_from_p (carg_type, throwable_type_node))
14985 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14986 parse_error_context (wfl_operator,
14987 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
14988 lang_printable_name (carg_type, 0));
14993 /* Partial check for unreachable catch statement: The catch
14994 clause is reachable iff is no earlier catch block A in
14995 the try statement such that the type of the catch
14996 clause's parameter is the same as or a subclass of the
14997 type of A's parameter */
14999 for (sub_current = catch;
15000 sub_current != current; sub_current = TREE_CHAIN (sub_current))
15002 tree sub_catch_clause, decl;
15003 sub_catch_clause = TREE_OPERAND (sub_current, 0);
15004 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
15006 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
15008 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
15009 parse_error_context
15011 "`catch' not reached because of the catch clause at line %d",
15012 EXPR_WFL_LINENO (sub_current));
15013 unreachable = error_found = 1;
15017 /* Complete the catch clause block */
15018 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
15019 if (catch_block == error_mark_node)
15024 if (CAN_COMPLETE_NORMALLY (catch_block))
15025 CAN_COMPLETE_NORMALLY (node) = 1;
15026 TREE_OPERAND (current, 0) = catch_block;
15031 /* Things to do here: the exception must be thrown */
15033 /* Link this type to the caught type list */
15034 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
15037 PUSH_EXCEPTIONS (caught_type_list);
15038 if ((try = java_complete_tree (try)) == error_mark_node)
15040 if (CAN_COMPLETE_NORMALLY (try))
15041 CAN_COMPLETE_NORMALLY (node) = 1;
15044 /* Verification ends here */
15046 return error_mark_node;
15048 TREE_OPERAND (node, 0) = try;
15049 TREE_OPERAND (node, 1) = catch;
15050 TREE_TYPE (node) = void_type_node;
15054 /* 14.17 The synchronized Statement */
15057 patch_synchronized_statement (node, wfl_op1)
15058 tree node, wfl_op1;
15060 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
15061 tree block = TREE_OPERAND (node, 1);
15063 tree tmp, enter, exit, expr_decl, assignment;
15065 if (expr == error_mark_node)
15067 block = java_complete_tree (block);
15071 /* We might be trying to synchronize on a STRING_CST */
15072 if ((tmp = patch_string (expr)))
15075 /* The TYPE of expr must be a reference type */
15076 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
15078 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15079 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
15080 lang_printable_name (TREE_TYPE (expr), 0));
15081 return error_mark_node;
15084 if (flag_emit_xref)
15086 TREE_OPERAND (node, 0) = expr;
15087 TREE_OPERAND (node, 1) = java_complete_tree (block);
15088 CAN_COMPLETE_NORMALLY (node) = 1;
15092 /* Generate a try-finally for the synchronized statement, except
15093 that the handler that catches all throw exception calls
15094 _Jv_MonitorExit and then rethrow the exception.
15095 The synchronized statement is then implemented as:
15098 _Jv_MonitorEnter (expression)
15100 _Jv_MonitorExit (expression)
15104 e = _Jv_exception_info ();
15105 _Jv_MonitorExit (expression)
15109 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
15110 BUILD_MONITOR_ENTER (enter, expr_decl);
15111 BUILD_MONITOR_EXIT (exit, expr_decl);
15112 CAN_COMPLETE_NORMALLY (enter) = 1;
15113 CAN_COMPLETE_NORMALLY (exit) = 1;
15114 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
15115 TREE_SIDE_EFFECTS (assignment) = 1;
15116 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
15117 build (COMPOUND_EXPR, NULL_TREE,
15118 build (WITH_CLEANUP_EXPR, NULL_TREE,
15119 build (COMPOUND_EXPR, NULL_TREE,
15120 assignment, enter),
15123 node = build_expr_block (node, expr_decl);
15125 return java_complete_tree (node);
15128 /* 14.16 The throw Statement */
15131 patch_throw_statement (node, wfl_op1)
15132 tree node, wfl_op1;
15134 tree expr = TREE_OPERAND (node, 0);
15135 tree type = TREE_TYPE (expr);
15136 int unchecked_ok = 0, tryblock_throws_ok = 0;
15138 /* Thrown expression must be assignable to java.lang.Throwable */
15139 if (!try_reference_assignconv (throwable_type_node, expr))
15141 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15142 parse_error_context (wfl_operator,
15143 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
15144 lang_printable_name (type, 0));
15145 /* If the thrown expression was a reference, we further the
15146 compile-time check. */
15147 if (!JREFERENCE_TYPE_P (type))
15148 return error_mark_node;
15151 /* At least one of the following must be true */
15153 /* The type of the throw expression is a not checked exception,
15154 i.e. is a unchecked expression. */
15155 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
15157 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15158 /* An instance can't throw a checked excetion unless that exception
15159 is explicitely declared in the `throws' clause of each
15160 constructor. This doesn't apply to anonymous classes, since they
15161 don't have declared constructors. */
15163 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
15166 for (current = TYPE_METHODS (current_class); current;
15167 current = TREE_CHAIN (current))
15168 if (DECL_CONSTRUCTOR_P (current)
15169 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
15171 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)",
15172 lang_printable_name (TREE_TYPE (expr), 0));
15173 return error_mark_node;
15177 /* Throw is contained in a try statement and at least one catch
15178 clause can receive the thrown expression or the current method is
15179 declared to throw such an exception. Or, the throw statement is
15180 contained in a method or constructor declaration and the type of
15181 the Expression is assignable to at least one type listed in the
15182 throws clause the declaration. */
15184 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
15185 if (!(unchecked_ok || tryblock_throws_ok))
15187 /* If there is a surrounding try block that has no matching
15188 clatch clause, report it first. A surrounding try block exits
15189 only if there is something after the list of checked
15190 exception thrown by the current function (if any). */
15191 if (IN_TRY_BLOCK_P ())
15192 parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
15193 lang_printable_name (type, 0));
15194 /* If we have no surrounding try statement and the method doesn't have
15195 any throws, report it now. FIXME */
15197 /* We report that the exception can't be throw from a try block
15198 in all circumstances but when the `throw' is inside a static
15200 else if (!EXCEPTIONS_P (currently_caught_type_list)
15201 && !tryblock_throws_ok)
15203 if (DECL_CLINIT_P (current_function_decl))
15204 parse_error_context (wfl_operator,
15205 "Checked exception `%s' can't be thrown in initializer",
15206 lang_printable_name (type, 0));
15208 parse_error_context (wfl_operator,
15209 "Checked exception `%s' isn't thrown from a `try' block",
15210 lang_printable_name (type, 0));
15212 /* Otherwise, the current method doesn't have the appropriate
15213 throws declaration */
15215 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
15216 lang_printable_name (type, 0));
15217 return error_mark_node;
15220 if (! flag_emit_class_files && ! flag_emit_xref)
15221 BUILD_THROW (node, expr);
15223 /* If doing xrefs, keep the location where the `throw' was seen. */
15224 if (flag_emit_xref)
15225 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
15229 /* Check that exception said to be thrown by method DECL can be
15230 effectively caught from where DECL is invoked. */
15233 check_thrown_exceptions (location, decl)
15238 /* For all the unchecked exceptions thrown by DECL */
15239 for (throws = DECL_FUNCTION_THROWS (decl); throws;
15240 throws = TREE_CHAIN (throws))
15241 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
15244 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
15245 if (DECL_NAME (decl) == get_identifier ("clone"))
15248 EXPR_WFL_LINECOL (wfl_operator) = location;
15249 if (DECL_FINIT_P (current_function_decl))
15250 parse_error_context
15251 (wfl_operator, "Exception `%s' can't be thrown in initializer",
15252 lang_printable_name (TREE_VALUE (throws), 0));
15255 parse_error_context
15256 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
15257 lang_printable_name (TREE_VALUE (throws), 0),
15258 (DECL_INIT_P (current_function_decl) ?
15259 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
15260 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
15265 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
15266 try-catch blocks, OR is listed in the `throws' clause of the
15270 check_thrown_exceptions_do (exception)
15273 tree list = currently_caught_type_list;
15274 resolve_and_layout (exception, NULL_TREE);
15275 /* First, all the nested try-catch-finally at that stage. The
15276 last element contains `throws' clause exceptions, if any. */
15277 if (IS_UNCHECKED_EXCEPTION_P (exception))
15282 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
15283 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
15285 list = TREE_CHAIN (list);
15291 purge_unchecked_exceptions (mdecl)
15294 tree throws = DECL_FUNCTION_THROWS (mdecl);
15295 tree new = NULL_TREE;
15299 tree next = TREE_CHAIN (throws);
15300 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
15302 TREE_CHAIN (throws) = new;
15307 /* List is inverted here, but it doesn't matter */
15308 DECL_FUNCTION_THROWS (mdecl) = new;
15311 /* 15.24 Conditional Operator ?: */
15314 patch_conditional_expr (node, wfl_cond, wfl_op1)
15315 tree node, wfl_cond, wfl_op1;
15317 tree cond = TREE_OPERAND (node, 0);
15318 tree op1 = TREE_OPERAND (node, 1);
15319 tree op2 = TREE_OPERAND (node, 2);
15320 tree resulting_type = NULL_TREE;
15321 tree t1, t2, patched;
15322 int error_found = 0;
15324 /* Operands of ?: might be StringBuffers crafted as a result of a
15325 string concatenation. Obtain a descent operand here. */
15326 if ((patched = patch_string (op1)))
15327 TREE_OPERAND (node, 1) = op1 = patched;
15328 if ((patched = patch_string (op2)))
15329 TREE_OPERAND (node, 2) = op2 = patched;
15331 t1 = TREE_TYPE (op1);
15332 t2 = TREE_TYPE (op2);
15334 /* The first expression must be a boolean */
15335 if (TREE_TYPE (cond) != boolean_type_node)
15337 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
15338 parse_error_context (wfl_operator,
15339 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
15340 lang_printable_name (TREE_TYPE (cond), 0));
15344 /* Second and third can be numeric, boolean (i.e. primitive),
15345 references or null. Anything else results in an error */
15346 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
15347 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
15348 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
15349 || (t1 == boolean_type_node && t2 == boolean_type_node)))
15352 /* Determine the type of the conditional expression. Same types are
15353 easy to deal with */
15355 resulting_type = t1;
15357 /* There are different rules for numeric types */
15358 else if (JNUMERIC_TYPE_P (t1))
15360 /* if byte/short found, the resulting type is short */
15361 if ((t1 == byte_type_node && t2 == short_type_node)
15362 || (t1 == short_type_node && t2 == byte_type_node))
15363 resulting_type = short_type_node;
15365 /* If t1 is a constant int and t2 is of type byte, short or char
15366 and t1's value fits in t2, then the resulting type is t2 */
15367 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
15368 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15369 resulting_type = t2;
15371 /* If t2 is a constant int and t1 is of type byte, short or char
15372 and t2's value fits in t1, then the resulting type is t1 */
15373 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15374 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15375 resulting_type = t1;
15377 /* Otherwise, binary numeric promotion is applied and the
15378 resulting type is the promoted type of operand 1 and 2 */
15380 resulting_type = binary_numeric_promotion (t1, t2,
15381 &TREE_OPERAND (node, 1),
15382 &TREE_OPERAND (node, 2));
15385 /* Cases of a reference and a null type */
15386 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15387 resulting_type = t1;
15389 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15390 resulting_type = t2;
15392 /* Last case: different reference types. If a type can be converted
15393 into the other one by assignment conversion, the latter
15394 determines the type of the expression */
15395 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15396 resulting_type = promote_type (t1);
15398 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15399 resulting_type = promote_type (t2);
15401 /* If we don't have any resulting type, we're in trouble */
15402 if (!resulting_type)
15404 char *t = xstrdup (lang_printable_name (t1, 0));
15405 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15406 parse_error_context (wfl_operator,
15407 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15408 t, lang_printable_name (t2, 0));
15415 TREE_TYPE (node) = error_mark_node;
15416 return error_mark_node;
15419 TREE_TYPE (node) = resulting_type;
15420 TREE_SET_CODE (node, COND_EXPR);
15421 CAN_COMPLETE_NORMALLY (node) = 1;
15425 /* Try to constant fold NODE.
15426 If NODE is not a constant expression, return NULL_EXPR.
15427 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15430 fold_constant_for_init (node, context)
15434 tree op0, op1, val;
15435 enum tree_code code = TREE_CODE (node);
15437 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
15445 case TRUNC_MOD_EXPR:
15453 case TRUTH_ANDIF_EXPR:
15454 case TRUTH_ORIF_EXPR:
15461 op0 = TREE_OPERAND (node, 0);
15462 op1 = TREE_OPERAND (node, 1);
15463 val = fold_constant_for_init (op0, context);
15464 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15466 TREE_OPERAND (node, 0) = val;
15467 val = fold_constant_for_init (op1, context);
15468 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15470 TREE_OPERAND (node, 1) = val;
15471 return patch_binop (node, op0, op1);
15473 case UNARY_PLUS_EXPR:
15475 case TRUTH_NOT_EXPR:
15478 op0 = TREE_OPERAND (node, 0);
15479 val = fold_constant_for_init (op0, context);
15480 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15482 TREE_OPERAND (node, 0) = val;
15483 return patch_unaryop (node, op0);
15487 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15488 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15490 TREE_OPERAND (node, 0) = val;
15491 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15492 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15494 TREE_OPERAND (node, 1) = val;
15495 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15496 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15498 TREE_OPERAND (node, 2) = val;
15499 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15500 : TREE_OPERAND (node, 2);
15504 if (! FIELD_FINAL (node)
15505 || DECL_INITIAL (node) == NULL_TREE)
15507 val = DECL_INITIAL (node);
15508 /* Guard against infinite recursion. */
15509 DECL_INITIAL (node) = NULL_TREE;
15510 val = fold_constant_for_init (val, node);
15511 DECL_INITIAL (node) = val;
15512 if (!val && CLASS_FINAL_VARIABLE_P (node))
15513 DECL_FIELD_FINAL_IUD (node) = 0;
15516 case EXPR_WITH_FILE_LOCATION:
15517 /* Compare java_complete_tree and resolve_expression_name. */
15518 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15519 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15521 tree name = EXPR_WFL_NODE (node);
15523 if (PRIMARY_P (node))
15525 else if (! QUALIFIED_P (name))
15527 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
15528 if (decl == NULL_TREE
15529 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
15531 return fold_constant_for_init (decl, decl);
15535 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15536 qualify_ambiguous_name (node);
15537 if (resolve_field_access (node, &decl, NULL)
15538 && decl != NULL_TREE)
15539 return fold_constant_for_init (decl, decl);
15545 op0 = TREE_OPERAND (node, 0);
15546 val = fold_constant_for_init (op0, context);
15547 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15549 TREE_OPERAND (node, 0) = val;
15553 #ifdef USE_COMPONENT_REF
15555 case COMPONENT_REF:
15564 #ifdef USE_COMPONENT_REF
15565 /* Context is 'T' for TypeName, 'P' for PackageName,
15566 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15569 resolve_simple_name (name, context)
15576 resolve_qualified_name (name, context)
15583 /* Mark P, which is really a `struct parser_ctxt **' for GC. */
15586 mark_parser_ctxt (p)
15589 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15596 for (i = 0; i < 11; ++i)
15597 ggc_mark_tree (pc->modifier_ctx[i]);
15598 ggc_mark_tree (pc->class_type);
15599 ggc_mark_tree (pc->function_decl);
15600 ggc_mark_tree (pc->package);
15601 ggc_mark_tree (pc->incomplete_class);
15602 ggc_mark_tree (pc->gclass_list);
15603 ggc_mark_tree (pc->class_list);
15604 ggc_mark_tree (pc->current_parsed_class);
15605 ggc_mark_tree (pc->current_parsed_class_un);
15606 ggc_mark_tree (pc->non_static_initialized);
15607 ggc_mark_tree (pc->static_initialized);
15608 ggc_mark_tree (pc->instance_initializers);
15609 ggc_mark_tree (pc->import_list);
15610 ggc_mark_tree (pc->import_demand_list);
15611 ggc_mark_tree (pc->current_loop);
15612 ggc_mark_tree (pc->current_labeled_block);
15613 #endif /* JC1_LITE */
15616 mark_parser_ctxt (&pc->next);