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 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"
71 #define DIR_SEPARATOR '/'
74 /* Local function prototypes */
75 static char *java_accstring_lookup PARAMS ((int));
76 static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
77 static void variable_redefinition_error PARAMS ((tree, tree, tree, int));
78 static tree create_class PARAMS ((int, tree, tree, tree));
79 static tree create_interface PARAMS ((int, tree, tree));
80 static void end_class_declaration PARAMS ((int));
81 static tree find_field PARAMS ((tree, tree));
82 static tree lookup_field_wrapper PARAMS ((tree, tree));
83 static int duplicate_declaration_error_p PARAMS ((tree, tree, tree));
84 static void register_fields PARAMS ((int, tree, tree));
85 static tree parser_qualified_classname PARAMS ((tree));
86 static int parser_check_super PARAMS ((tree, tree, tree));
87 static int parser_check_super_interface PARAMS ((tree, tree, tree));
88 static void check_modifiers_consistency PARAMS ((int));
89 static tree lookup_cl PARAMS ((tree));
90 static tree lookup_java_method2 PARAMS ((tree, tree, int));
91 static tree method_header PARAMS ((int, tree, tree, tree));
92 static void fix_method_argument_names PARAMS ((tree ,tree));
93 static tree method_declarator PARAMS ((tree, tree));
94 static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
96 static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list));
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_inner_class_access PARAMS ((tree, tree, tree));
105 static int check_pkg_class_access PARAMS ((tree, tree));
106 static void register_package PARAMS ((tree));
107 static tree resolve_package PARAMS ((tree, tree *));
108 static tree lookup_package_type PARAMS ((const char *, int));
109 static tree lookup_package_type_and_set_next PARAMS ((const char *, int, tree *));
110 static tree resolve_class PARAMS ((tree, tree, tree, tree));
111 static void declare_local_variables PARAMS ((int, tree, tree));
112 static void source_start_java_method PARAMS ((tree));
113 static void source_end_java_method PARAMS ((void));
114 static void expand_start_java_method PARAMS ((tree));
115 static tree find_name_in_single_imports PARAMS ((tree));
116 static void check_abstract_method_header PARAMS ((tree));
117 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
118 static tree resolve_expression_name PARAMS ((tree, tree *));
119 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
120 static int check_class_interface_creation PARAMS ((int, int, tree,
122 static tree patch_method_invocation PARAMS ((tree, tree, tree,
124 static int breakdown_qualified PARAMS ((tree *, tree *, tree));
125 static tree resolve_and_layout PARAMS ((tree, tree));
126 static tree qualify_and_find PARAMS ((tree, tree, tree));
127 static tree resolve_no_layout PARAMS ((tree, tree));
128 static int invocation_mode PARAMS ((tree, int));
129 static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
131 static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
133 static tree find_most_specific_methods_list PARAMS ((tree));
134 static int argument_types_convertible PARAMS ((tree, tree));
135 static tree patch_invoke PARAMS ((tree, tree, tree));
136 static int maybe_use_access_method PARAMS ((int, tree *, tree *));
137 static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
138 static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
139 static tree obtain_incomplete_type PARAMS ((tree));
140 static tree java_complete_lhs PARAMS ((tree));
141 static tree java_complete_tree PARAMS ((tree));
142 static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
143 static int maybe_yank_clinit PARAMS ((tree));
144 static void java_complete_expand_method PARAMS ((tree));
145 static int unresolved_type_p PARAMS ((tree, tree *));
146 static void create_jdep_list PARAMS ((struct parser_ctxt *));
147 static tree build_expr_block PARAMS ((tree, tree));
148 static tree enter_block PARAMS ((void));
149 static tree enter_a_block PARAMS ((tree));
150 static tree exit_block PARAMS ((void));
151 static tree lookup_name_in_blocks PARAMS ((tree));
152 static void maybe_absorb_scoping_blocks PARAMS ((void));
153 static tree build_method_invocation PARAMS ((tree, tree));
154 static tree build_new_invocation PARAMS ((tree, tree));
155 static tree build_assignment PARAMS ((int, int, tree, tree));
156 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
157 static int check_final_assignment PARAMS ((tree ,tree));
158 static tree patch_assignment PARAMS ((tree, tree, tree ));
159 static tree patch_binop PARAMS ((tree, tree, tree));
160 static tree build_unaryop PARAMS ((int, int, tree));
161 static tree build_incdec PARAMS ((int, int, tree, int));
162 static tree patch_unaryop PARAMS ((tree, tree));
163 static tree build_cast PARAMS ((int, tree, tree));
164 static tree build_null_of_type PARAMS ((tree));
165 static tree patch_cast PARAMS ((tree, tree));
166 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
167 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
168 static int valid_cast_to_p PARAMS ((tree, tree));
169 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
170 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
171 static tree try_reference_assignconv PARAMS ((tree, tree));
172 static tree build_unresolved_array_type PARAMS ((tree));
173 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
174 static tree build_array_ref PARAMS ((int, tree, tree));
175 static tree patch_array_ref PARAMS ((tree));
176 static tree make_qualified_name PARAMS ((tree, tree, int));
177 static tree merge_qualified_name PARAMS ((tree, tree));
178 static tree make_qualified_primary PARAMS ((tree, tree, int));
179 static int resolve_qualified_expression_name PARAMS ((tree, tree *,
181 static void qualify_ambiguous_name PARAMS ((tree));
182 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
183 static tree build_newarray_node PARAMS ((tree, tree, int));
184 static tree patch_newarray PARAMS ((tree));
185 static tree resolve_type_during_patch PARAMS ((tree));
186 static tree build_this PARAMS ((int));
187 static tree build_wfl_wrap PARAMS ((tree, int));
188 static tree build_return PARAMS ((int, tree));
189 static tree patch_return PARAMS ((tree));
190 static tree maybe_access_field PARAMS ((tree, tree, tree));
191 static int complete_function_arguments PARAMS ((tree));
192 static int check_for_static_method_reference PARAMS ((tree, tree, tree,
194 static int not_accessible_p PARAMS ((tree, tree, int));
195 static void check_deprecation PARAMS ((tree, tree));
196 static int class_in_current_package PARAMS ((tree));
197 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
198 static tree patch_if_else_statement PARAMS ((tree));
199 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
200 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
201 static tree patch_exit_expr PARAMS ((tree));
202 static tree build_labeled_block PARAMS ((int, tree));
203 static tree finish_labeled_statement PARAMS ((tree, tree));
204 static tree build_bc_statement PARAMS ((int, int, tree));
205 static tree patch_bc_statement PARAMS ((tree));
206 static tree patch_loop_statement PARAMS ((tree));
207 static tree build_new_loop PARAMS ((tree));
208 static tree build_loop_body PARAMS ((int, tree, int));
209 static tree finish_loop_body PARAMS ((int, tree, tree, int));
210 static tree build_debugable_stmt PARAMS ((int, tree));
211 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
212 static tree patch_switch_statement PARAMS ((tree));
213 static tree string_constant_concatenation PARAMS ((tree, tree));
214 static tree build_string_concatenation PARAMS ((tree, tree));
215 static tree patch_string_cst PARAMS ((tree));
216 static tree patch_string PARAMS ((tree));
217 static tree build_try_statement PARAMS ((int, tree, tree));
218 static tree build_try_finally_statement PARAMS ((int, tree, tree));
219 static tree patch_try_statement PARAMS ((tree));
220 static tree patch_synchronized_statement PARAMS ((tree, tree));
221 static tree patch_throw_statement PARAMS ((tree, tree));
222 static void check_thrown_exceptions PARAMS ((int, tree));
223 static int check_thrown_exceptions_do PARAMS ((tree));
224 static void purge_unchecked_exceptions PARAMS ((tree));
225 static void check_throws_clauses PARAMS ((tree, tree, tree));
226 static void finish_method_declaration PARAMS ((tree));
227 static tree build_super_invocation PARAMS ((tree));
228 static int verify_constructor_circularity PARAMS ((tree, tree));
229 static char *constructor_circularity_msg PARAMS ((tree, tree));
230 static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
232 static const char *get_printable_method_name PARAMS ((tree));
233 static tree patch_conditional_expr PARAMS ((tree, tree, tree));
234 static tree generate_finit PARAMS ((tree));
235 static void add_instance_initializer PARAMS ((tree));
236 static void fix_constructors PARAMS ((tree));
237 static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
239 static void craft_constructor PARAMS ((tree, tree));
240 static int verify_constructor_super PARAMS ((tree));
241 static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
242 static void start_artificial_method_body PARAMS ((tree));
243 static void end_artificial_method_body PARAMS ((tree));
244 static int check_method_redefinition PARAMS ((tree, tree));
245 static int reset_method_name PARAMS ((tree));
246 static int check_method_types_complete PARAMS ((tree));
247 static void java_check_regular_methods PARAMS ((tree));
248 static void java_check_abstract_methods PARAMS ((tree));
249 static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
250 static void unreachable_stmt_error PARAMS ((tree));
251 static tree find_expr_with_wfl PARAMS ((tree));
252 static void missing_return_error PARAMS ((tree));
253 static tree build_new_array_init PARAMS ((int, tree));
254 static tree patch_new_array_init PARAMS ((tree, tree));
255 static tree maybe_build_array_element_wfl PARAMS ((tree));
256 static int array_constructor_check_entry PARAMS ((tree, tree));
257 static const char *purify_type_name PARAMS ((const char *));
258 static tree fold_constant_for_init PARAMS ((tree, tree));
259 static tree strip_out_static_field_access_decl PARAMS ((tree));
260 static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
261 static void static_ref_err PARAMS ((tree, tree, tree));
262 static void parser_add_interface PARAMS ((tree, tree, tree));
263 static void add_superinterfaces PARAMS ((tree, tree));
264 static tree jdep_resolve_class PARAMS ((jdep *));
265 static int note_possible_classname PARAMS ((const char *, int));
266 static void java_complete_expand_classes PARAMS ((void));
267 static void java_complete_expand_class PARAMS ((tree));
268 static void java_complete_expand_methods PARAMS ((tree));
269 static tree cut_identifier_in_qualified PARAMS ((tree));
270 static tree java_stabilize_reference PARAMS ((tree));
271 static tree do_unary_numeric_promotion PARAMS ((tree));
272 static char * operator_string PARAMS ((tree));
273 static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
274 static tree merge_string_cste PARAMS ((tree, tree, int));
275 static tree java_refold PARAMS ((tree));
276 static int java_decl_equiv PARAMS ((tree, tree));
277 static int binop_compound_p PARAMS ((enum tree_code));
278 static tree search_loop PARAMS ((tree));
279 static int labeled_block_contains_loop_p PARAMS ((tree, tree));
280 static int check_abstract_method_definitions PARAMS ((int, tree, tree));
281 static void java_check_abstract_method_definitions PARAMS ((tree));
282 static void java_debug_context_do PARAMS ((int));
283 static void java_parser_context_push_initialized_field PARAMS ((void));
284 static void java_parser_context_pop_initialized_field PARAMS ((void));
285 static tree reorder_static_initialized PARAMS ((tree));
286 static void java_parser_context_suspend PARAMS ((void));
287 static void java_parser_context_resume PARAMS ((void));
289 /* JDK 1.1 work. FIXME */
291 static tree maybe_make_nested_class_name PARAMS ((tree));
292 static void make_nested_class_name PARAMS ((tree));
293 static void set_nested_class_simple_name_value PARAMS ((tree, int));
294 static void link_nested_class_to_enclosing PARAMS ((void));
295 static tree find_as_inner_class PARAMS ((tree, tree, tree));
296 static tree find_as_inner_class_do PARAMS ((tree, tree));
297 static int check_inner_class_redefinition PARAMS ((tree, tree));
299 static tree build_thisn_assign PARAMS ((void));
300 static tree build_current_thisn PARAMS ((tree));
301 static tree build_access_to_thisn PARAMS ((tree, tree, int));
302 static tree maybe_build_thisn_access_method PARAMS ((tree));
304 static tree build_outer_field_access PARAMS ((tree, tree));
305 static tree build_outer_field_access_methods PARAMS ((tree));
306 static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
308 static tree build_outer_method_access_method PARAMS ((tree));
309 static tree build_new_access_id PARAMS ((void));
310 static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
313 static int outer_field_access_p PARAMS ((tree, tree));
314 static int outer_field_expanded_access_p PARAMS ((tree, tree *,
316 static tree outer_field_access_fix PARAMS ((tree, tree, tree));
317 static tree build_incomplete_class_ref PARAMS ((int, tree));
318 static tree patch_incomplete_class_ref PARAMS ((tree));
319 static tree create_anonymous_class PARAMS ((int, tree));
320 static void patch_anonymous_class PARAMS ((tree, tree, tree));
321 static void add_inner_class_fields PARAMS ((tree, tree));
323 static tree build_dot_class_method PARAMS ((tree));
324 static tree build_dot_class_method_invocation PARAMS ((tree));
325 static void create_new_parser_context PARAMS ((int));
326 static void mark_parser_ctxt PARAMS ((void *));
328 /* Number of error found so far. */
329 int java_error_count;
330 /* Number of warning found so far. */
331 int java_warning_count;
332 /* Tell when not to fold, when doing xrefs */
334 /* Cyclic inheritance report, as it can be set by layout_class */
335 char *cyclic_inheritance_report;
337 /* Tell when we're within an instance initializer */
338 static int in_instance_initializer;
340 /* The current parser context */
341 struct parser_ctxt *ctxp;
343 /* List of things that were analyzed for which code will be generated */
344 static struct parser_ctxt *ctxp_for_generation = NULL;
346 /* binop_lookup maps token to tree_code. It is used where binary
347 operations are involved and required by the parser. RDIV_EXPR
348 covers both integral/floating point division. The code is changed
349 once the type of both operator is worked out. */
351 static enum tree_code binop_lookup[19] =
353 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
354 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
355 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
356 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
357 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
359 #define BINOP_LOOKUP(VALUE) \
360 binop_lookup [((VALUE) - PLUS_TK) % ARRAY_SIZE (binop_lookup)]
362 /* This is the end index for binary operators that can also be used
363 in compound assignements. */
364 #define BINOP_COMPOUND_CANDIDATES 11
366 /* The "$L" identifier we use to create labels. */
367 static tree label_id = NULL_TREE;
369 /* The "StringBuffer" identifier used for the String `+' operator. */
370 static tree wfl_string_buffer = NULL_TREE;
372 /* The "append" identifier used for String `+' operator. */
373 static tree wfl_append = NULL_TREE;
375 /* The "toString" identifier used for String `+' operator. */
376 static tree wfl_to_string = NULL_TREE;
378 /* The "java.lang" import qualified name. */
379 static tree java_lang_id = NULL_TREE;
381 /* The generated `inst$' identifier used for generated enclosing
382 instance/field access functions. */
383 static tree inst_id = NULL_TREE;
385 /* The "java.lang.Cloneable" qualified name. */
386 static tree java_lang_cloneable = NULL_TREE;
388 /* The "java.io.Serializable" qualified name. */
389 static tree java_io_serializable = NULL_TREE;
391 /* Context and flag for static blocks */
392 static tree current_static_block = NULL_TREE;
394 /* The generated `write_parm_value$' identifier. */
397 /* The list of all packages we've seen so far */
398 static tree package_list = NULL_TREE;
400 /* Hold THIS for the scope of the current public method decl. */
401 static tree current_this;
403 /* Hold a list of catch clauses list. The first element of this list is
404 the list of the catch clauses of the currently analysed try block. */
405 static tree currently_caught_type_list;
407 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
408 line and point it out. */
409 /* Should point out the one that don't fit. ASCII/unicode, going
412 #define check_modifiers(__message, __value, __mask) do { \
413 if ((__value) & ~(__mask)) \
415 int i, remainder = (__value) & ~(__mask); \
416 for (i = 0; i <= 10; i++) \
417 if ((1 << i) & remainder) \
418 parse_error_context (ctxp->modifier_ctx [i], (__message), \
419 java_accstring_lookup (1 << i)); \
441 /* Things defined here have to match the order of what's in the
442 binop_lookup table. */
444 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
445 %token LS_TK SRS_TK ZRS_TK
446 %token AND_TK XOR_TK OR_TK
447 %token BOOL_AND_TK BOOL_OR_TK
448 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
450 /* This maps to the same binop_lookup entry than the token above */
452 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
454 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
455 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
458 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
460 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
461 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
462 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
463 %token PAD_TK ABSTRACT_TK MODIFIER_TK
465 /* Keep those two in order, too */
466 %token DECR_TK INCR_TK
468 /* From now one, things can be in any order */
470 %token DEFAULT_TK IF_TK THROW_TK
471 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
472 %token THROWS_TK BREAK_TK IMPORT_TK
473 %token ELSE_TK INSTANCEOF_TK RETURN_TK
474 %token VOID_TK CATCH_TK INTERFACE_TK
475 %token CASE_TK EXTENDS_TK FINALLY_TK
476 %token SUPER_TK WHILE_TK CLASS_TK
477 %token SWITCH_TK CONST_TK TRY_TK
478 %token FOR_TK NEW_TK CONTINUE_TK
479 %token GOTO_TK PACKAGE_TK THIS_TK
481 %token BYTE_TK SHORT_TK INT_TK LONG_TK
482 %token CHAR_TK INTEGRAL_TK
484 %token FLOAT_TK DOUBLE_TK FP_TK
488 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
490 %token ASSIGN_ANY_TK ASSIGN_TK
491 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
493 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
494 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
496 %type <value> modifiers MODIFIER_TK final synchronized
498 %type <node> super ID_TK identifier
499 %type <node> name simple_name qualified_name
500 %type <node> type_declaration compilation_unit
501 field_declaration method_declaration extends_interfaces
502 interfaces interface_type_list
503 class_member_declaration
504 import_declarations package_declaration
505 type_declarations interface_body
506 interface_member_declaration constant_declaration
507 interface_member_declarations interface_type
508 abstract_method_declaration interface_type_list
509 %type <node> class_body_declaration class_member_declaration
510 static_initializer constructor_declaration block
511 %type <node> class_body_declarations constructor_header
512 %type <node> class_or_interface_type class_type class_type_list
513 constructor_declarator explicit_constructor_invocation
514 %type <node> dim_expr dim_exprs this_or_super throws
516 %type <node> variable_declarator_id variable_declarator
517 variable_declarators variable_initializer
518 variable_initializers constructor_body
521 %type <node> class_body block_end constructor_block_end
522 %type <node> statement statement_without_trailing_substatement
523 labeled_statement if_then_statement label_decl
524 if_then_else_statement while_statement for_statement
525 statement_nsi labeled_statement_nsi do_statement
526 if_then_else_statement_nsi while_statement_nsi
527 for_statement_nsi statement_expression_list for_init
528 for_update statement_expression expression_statement
529 primary_no_new_array expression primary
530 array_creation_expression array_type
531 class_instance_creation_expression field_access
532 method_invocation array_access something_dot_new
533 argument_list postfix_expression while_expression
534 post_increment_expression post_decrement_expression
535 unary_expression_not_plus_minus unary_expression
536 pre_increment_expression pre_decrement_expression
537 unary_expression_not_plus_minus cast_expression
538 multiplicative_expression additive_expression
539 shift_expression relational_expression
540 equality_expression and_expression
541 exclusive_or_expression inclusive_or_expression
542 conditional_and_expression conditional_or_expression
543 conditional_expression assignment_expression
544 left_hand_side assignment for_header for_begin
545 constant_expression do_statement_begin empty_statement
546 switch_statement synchronized_statement throw_statement
547 try_statement switch_expression switch_block
548 catches catch_clause catch_clause_parameter finally
549 anonymous_class_creation
550 %type <node> return_statement break_statement continue_statement
552 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
553 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
554 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
555 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
556 %type <operator> ASSIGN_ANY_TK assignment_operator
557 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
558 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
559 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
560 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
561 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
562 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
563 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
564 %type <operator> NEW_TK
566 %type <node> method_body
568 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
569 STRING_LIT_TK NULL_TK VOID_TK
571 %type <node> IF_TK WHILE_TK FOR_TK
573 %type <node> formal_parameter_list formal_parameter
574 method_declarator method_header
576 %type <node> primitive_type reference_type type
577 BOOLEAN_TK INTEGRAL_TK FP_TK
579 /* Added or modified JDK 1.1 rule types */
580 %type <node> type_literals array_type_literal
583 /* 19.2 Production from 2.3: The Syntactic Grammar */
586 /* Register static variables with the garbage
588 ggc_add_tree_root (&label_id, 1);
589 ggc_add_tree_root (&wfl_string_buffer, 1);
590 ggc_add_tree_root (&wfl_append, 1);
591 ggc_add_tree_root (&wfl_to_string, 1);
592 ggc_add_tree_root (&java_lang_id, 1);
593 ggc_add_tree_root (&inst_id, 1);
594 ggc_add_tree_root (&java_lang_cloneable, 1);
595 ggc_add_tree_root (&java_io_serializable, 1);
596 ggc_add_tree_root (¤t_static_block, 1);
597 ggc_add_tree_root (&wpv_id, 1);
598 ggc_add_tree_root (&package_list, 1);
599 ggc_add_tree_root (¤t_this, 1);
600 ggc_add_tree_root (¤tly_caught_type_list, 1);
601 ggc_add_string_root (&cyclic_inheritance_report, 1);
602 ggc_add_root (&ctxp, 1,
603 sizeof (struct parser_ctxt *),
605 ggc_add_root (&ctxp_for_generation, 1,
606 sizeof (struct parser_ctxt *),
613 /* 19.3 Productions from 3: Lexical structure */
623 /* 19.4 Productions from 4: Types, Values and Variables */
636 class_or_interface_type
640 class_or_interface_type:
645 class_or_interface_type /* Default rule */
649 class_or_interface_type
653 primitive_type OSB_TK CSB_TK
655 $$ = build_java_array_type ($1, -1);
656 CLASS_LOADED_P ($$) = 1;
659 { $$ = build_unresolved_array_type ($1); }
660 | array_type OSB_TK CSB_TK
661 { $$ = build_unresolved_array_type ($1); }
662 | primitive_type OSB_TK error
663 {RULE ("']' expected"); RECOVER;}
664 | array_type OSB_TK error
665 {RULE ("']' expected"); RECOVER;}
668 /* 19.5 Productions from 6: Names */
670 simple_name /* Default rule */
671 | qualified_name /* Default rule */
675 identifier /* Default rule */
679 name DOT_TK identifier
680 { $$ = make_qualified_name ($1, $3, $2.location); }
687 /* 19.6: Production from 7: Packages */
690 | package_declaration
691 | import_declarations
693 | package_declaration import_declarations
694 | package_declaration type_declarations
695 | import_declarations type_declarations
696 | package_declaration import_declarations type_declarations
704 | import_declarations import_declaration
712 | type_declarations type_declaration
716 PACKAGE_TK name SC_TK
718 ctxp->package = EXPR_WFL_NODE ($2);
719 register_package (ctxp->package);
722 {yyerror ("Missing name"); RECOVER;}
723 | PACKAGE_TK name error
724 {yyerror ("';' expected"); RECOVER;}
728 single_type_import_declaration
729 | type_import_on_demand_declaration
732 single_type_import_declaration:
735 tree name = EXPR_WFL_NODE ($2), last_name;
736 int i = IDENTIFIER_LENGTH (name)-1;
737 const char *last = &IDENTIFIER_POINTER (name)[i];
738 while (last != IDENTIFIER_POINTER (name))
744 last_name = get_identifier (++last);
745 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
747 tree err = find_name_in_single_imports (last_name);
748 if (err && err != name)
750 ($2, "Ambiguous class: `%s' and `%s'",
751 IDENTIFIER_POINTER (name),
752 IDENTIFIER_POINTER (err));
754 REGISTER_IMPORT ($2, last_name);
757 REGISTER_IMPORT ($2, last_name);
760 {yyerror ("Missing name"); RECOVER;}
761 | IMPORT_TK name error
762 {yyerror ("';' expected"); RECOVER;}
765 type_import_on_demand_declaration:
766 IMPORT_TK name DOT_TK MULT_TK SC_TK
768 tree name = EXPR_WFL_NODE ($2);
769 /* Don't import java.lang.* twice. */
770 if (name != java_lang_id)
772 read_import_dir ($2);
773 ctxp->import_demand_list =
774 chainon (ctxp->import_demand_list,
775 build_tree_list ($2, NULL_TREE));
778 | IMPORT_TK name DOT_TK error
779 {yyerror ("'*' expected"); RECOVER;}
780 | IMPORT_TK name DOT_TK MULT_TK error
781 {yyerror ("';' expected"); RECOVER;}
786 { end_class_declaration (0); }
787 | interface_declaration
788 { end_class_declaration (0); }
793 yyerror ("Class or interface declaration expected");
797 /* 19.7 Shortened from the original:
798 modifiers: modifier | modifiers modifier
799 modifier: any of public... */
805 | modifiers MODIFIER_TK
810 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
811 java_accstring_lookup (acc));
819 /* 19.8.1 Production from $8.1: Class Declaration */
821 modifiers CLASS_TK identifier super interfaces
822 { create_class ($1, $3, $4, $5); }
824 | CLASS_TK identifier super interfaces
825 { create_class (0, $2, $3, $4); }
827 | modifiers CLASS_TK error
828 {yyerror ("Missing class name"); RECOVER;}
830 {yyerror ("Missing class name"); RECOVER;}
831 | CLASS_TK identifier error
833 if (!ctxp->class_err) yyerror ("'{' expected");
836 | modifiers CLASS_TK identifier error
837 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
842 | EXTENDS_TK class_type
844 | EXTENDS_TK class_type error
845 {yyerror ("'{' expected"); ctxp->class_err=1;}
847 {yyerror ("Missing super class name"); ctxp->class_err=1;}
852 | IMPLEMENTS_TK interface_type_list
854 | IMPLEMENTS_TK error
857 yyerror ("Missing interface name");
864 ctxp->interface_number = 1;
865 $$ = build_tree_list ($1, NULL_TREE);
867 | interface_type_list C_TK interface_type
869 ctxp->interface_number++;
870 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
872 | interface_type_list C_TK error
873 {yyerror ("Missing interface name"); RECOVER;}
879 /* Store the location of the `}' when doing xrefs */
881 DECL_END_SOURCE_LINE (GET_CPC ()) =
882 EXPR_WFL_ADD_COL ($2.location, 1);
885 | OCB_TK class_body_declarations CCB_TK
887 /* Store the location of the `}' when doing xrefs */
889 DECL_END_SOURCE_LINE (GET_CPC ()) =
890 EXPR_WFL_ADD_COL ($3.location, 1);
895 class_body_declarations:
896 class_body_declaration
897 | class_body_declarations class_body_declaration
900 class_body_declaration:
901 class_member_declaration
903 | constructor_declaration
904 | block /* Added, JDK1.1, instance initializer */
906 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
907 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
911 class_member_declaration:
914 | class_declaration /* Added, JDK1.1 inner classes */
915 { end_class_declaration (1); }
916 | interface_declaration /* Added, JDK1.1 inner interfaces */
917 { end_class_declaration (1); }
921 /* 19.8.2 Productions from 8.3: Field Declarations */
923 type variable_declarators SC_TK
924 { register_fields (0, $1, $2); }
925 | modifiers type variable_declarators SC_TK
928 ("Illegal modifier `%s' for field declaration",
929 $1, FIELD_MODIFIERS);
930 check_modifiers_consistency ($1);
931 register_fields ($1, $2, $3);
935 variable_declarators:
936 /* Should we use build_decl_list () instead ? FIXME */
937 variable_declarator /* Default rule */
938 | variable_declarators C_TK variable_declarator
939 { $$ = chainon ($1, $3); }
940 | variable_declarators C_TK error
941 {yyerror ("Missing term"); RECOVER;}
945 variable_declarator_id
946 { $$ = build_tree_list ($1, NULL_TREE); }
947 | variable_declarator_id ASSIGN_TK variable_initializer
949 if (java_error_count)
952 ($1, build_assignment ($2.token, $2.location, $1, $3));
954 | variable_declarator_id ASSIGN_TK error
956 yyerror ("Missing variable initializer");
957 $$ = build_tree_list ($1, NULL_TREE);
960 | variable_declarator_id ASSIGN_TK variable_initializer error
962 yyerror ("';' expected");
963 $$ = build_tree_list ($1, NULL_TREE);
968 variable_declarator_id:
970 | variable_declarator_id OSB_TK CSB_TK
971 { $$ = build_unresolved_array_type ($1); }
973 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
974 | variable_declarator_id OSB_TK error
976 tree node = java_lval.node;
977 if (node && (TREE_CODE (node) == INTEGER_CST
978 || TREE_CODE (node) == EXPR_WITH_FILE_LOCATION))
979 yyerror ("Can't specify array dimension in a declaration");
981 yyerror ("']' expected");
984 | variable_declarator_id CSB_TK error
985 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
988 variable_initializer:
993 /* 19.8.3 Productions from 8.4: Method Declarations */
997 current_function_decl = $1;
998 if (current_function_decl
999 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
1000 source_start_java_method (current_function_decl);
1002 current_function_decl = NULL_TREE;
1005 { finish_method_declaration ($3); }
1006 | method_header error
1007 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
1011 type method_declarator throws
1012 { $$ = method_header (0, $1, $2, $3); }
1013 | VOID_TK method_declarator throws
1014 { $$ = method_header (0, void_type_node, $2, $3); }
1015 | modifiers type method_declarator throws
1016 { $$ = method_header ($1, $2, $3, $4); }
1017 | modifiers VOID_TK method_declarator throws
1018 { $$ = method_header ($1, void_type_node, $3, $4); }
1021 yyerror ("Invalid method declaration, method name required");
1024 | modifiers type error
1027 {yyerror ("Identifier expected"); RECOVER;}
1028 | modifiers VOID_TK error
1029 {yyerror ("Identifier expected"); RECOVER;}
1032 yyerror ("Invalid method declaration, return type required");
1038 identifier OP_TK CP_TK
1040 ctxp->formal_parameter_number = 0;
1041 $$ = method_declarator ($1, NULL_TREE);
1043 | identifier OP_TK formal_parameter_list CP_TK
1044 { $$ = method_declarator ($1, $3); }
1045 | method_declarator OSB_TK CSB_TK
1047 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1049 build_unresolved_array_type (TREE_PURPOSE ($1));
1050 parse_warning_context
1052 "Discouraged form of returned type specification");
1054 | identifier OP_TK error
1055 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1056 | method_declarator OSB_TK error
1057 {yyerror ("']' expected"); RECOVER;}
1060 formal_parameter_list:
1063 ctxp->formal_parameter_number = 1;
1065 | formal_parameter_list C_TK formal_parameter
1067 ctxp->formal_parameter_number += 1;
1068 $$ = chainon ($1, $3);
1070 | formal_parameter_list C_TK error
1071 { yyerror ("Missing formal parameter term"); RECOVER; }
1075 type variable_declarator_id
1077 $$ = build_tree_list ($2, $1);
1079 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1081 $$ = build_tree_list ($3, $2);
1082 ARG_FINAL_P ($$) = 1;
1086 yyerror ("Missing identifier"); RECOVER;
1091 yyerror ("Missing identifier"); RECOVER;
1099 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1101 if ($1 != ACC_FINAL)
1102 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1108 | THROWS_TK class_type_list
1111 {yyerror ("Missing class type term"); RECOVER;}
1116 { $$ = build_tree_list ($1, $1); }
1117 | class_type_list C_TK class_type
1118 { $$ = tree_cons ($3, $3, $1); }
1119 | class_type_list C_TK error
1120 {yyerror ("Missing class type term"); RECOVER;}
1125 | SC_TK { $$ = NULL_TREE; }
1128 /* 19.8.4 Productions from 8.5: Static Initializers */
1132 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1133 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1137 static: /* Test lval.sub_token here */
1140 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1141 /* Can't have a static initializer in an innerclass */
1142 if ($1 | ACC_STATIC &&
1143 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1145 (MODIFIER_WFL (STATIC_TK),
1146 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1147 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1148 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1152 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1153 constructor_declaration:
1156 current_function_decl = $1;
1157 source_start_java_method (current_function_decl);
1160 { finish_method_declaration ($3); }
1164 constructor_declarator throws
1165 { $$ = method_header (0, NULL_TREE, $1, $2); }
1166 | modifiers constructor_declarator throws
1167 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1170 constructor_declarator:
1171 simple_name OP_TK CP_TK
1173 ctxp->formal_parameter_number = 0;
1174 $$ = method_declarator ($1, NULL_TREE);
1176 | simple_name OP_TK formal_parameter_list CP_TK
1177 { $$ = method_declarator ($1, $3); }
1181 /* Unlike regular method, we always need a complete (empty)
1182 body so we can safely perform all the required code
1183 addition (super invocation and field initialization) */
1184 block_begin constructor_block_end
1186 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1189 | block_begin explicit_constructor_invocation constructor_block_end
1191 | block_begin block_statements constructor_block_end
1193 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1197 constructor_block_end:
1201 /* Error recovery for that rule moved down expression_statement: rule. */
1202 explicit_constructor_invocation:
1203 this_or_super OP_TK CP_TK SC_TK
1205 $$ = build_method_invocation ($1, NULL_TREE);
1206 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1207 $$ = java_method_add_stmt (current_function_decl, $$);
1209 | this_or_super OP_TK argument_list CP_TK SC_TK
1211 $$ = build_method_invocation ($1, $3);
1212 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1213 $$ = java_method_add_stmt (current_function_decl, $$);
1215 /* Added, JDK1.1 inner classes. Modified because the rule
1216 'primary' couldn't work. */
1217 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1218 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1219 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1220 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1223 this_or_super: /* Added, simplifies error diagnostics */
1226 tree wfl = build_wfl_node (this_identifier_node);
1227 EXPR_WFL_LINECOL (wfl) = $1.location;
1232 tree wfl = build_wfl_node (super_identifier_node);
1233 EXPR_WFL_LINECOL (wfl) = $1.location;
1238 /* 19.9 Productions from 9: Interfaces */
1239 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1240 interface_declaration:
1241 INTERFACE_TK identifier
1242 { create_interface (0, $2, NULL_TREE); }
1244 | modifiers INTERFACE_TK identifier
1245 { create_interface ($1, $3, NULL_TREE); }
1247 | INTERFACE_TK identifier extends_interfaces
1248 { create_interface (0, $2, $3); }
1250 | modifiers INTERFACE_TK identifier extends_interfaces
1251 { create_interface ($1, $3, $4); }
1253 | INTERFACE_TK identifier error
1254 {yyerror ("'{' expected"); RECOVER;}
1255 | modifiers INTERFACE_TK identifier error
1256 {yyerror ("'{' expected"); RECOVER;}
1260 EXTENDS_TK interface_type
1262 ctxp->interface_number = 1;
1263 $$ = build_tree_list ($2, NULL_TREE);
1265 | extends_interfaces C_TK interface_type
1267 ctxp->interface_number++;
1268 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1271 {yyerror ("Invalid interface type"); RECOVER;}
1272 | extends_interfaces C_TK error
1273 {yyerror ("Missing term"); RECOVER;}
1279 | OCB_TK interface_member_declarations CCB_TK
1283 interface_member_declarations:
1284 interface_member_declaration
1285 | interface_member_declarations interface_member_declaration
1288 interface_member_declaration:
1289 constant_declaration
1290 | abstract_method_declaration
1291 | class_declaration /* Added, JDK1.1 inner classes */
1292 { end_class_declaration (1); }
1293 | interface_declaration /* Added, JDK1.1 inner interfaces */
1294 { end_class_declaration (1); }
1297 constant_declaration:
1301 abstract_method_declaration:
1304 check_abstract_method_header ($1);
1305 current_function_decl = NULL_TREE; /* FIXME ? */
1307 | method_header error
1308 {yyerror ("';' expected"); RECOVER;}
1311 /* 19.10 Productions from 10: Arrays */
1314 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1315 | OCB_TK variable_initializers CCB_TK
1316 { $$ = build_new_array_init ($1.location, $2); }
1317 | OCB_TK variable_initializers C_TK CCB_TK
1318 { $$ = build_new_array_init ($1.location, $2); }
1321 variable_initializers:
1322 variable_initializer
1324 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1327 | variable_initializers C_TK variable_initializer
1329 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1331 | variable_initializers C_TK error
1332 {yyerror ("Missing term"); RECOVER;}
1335 /* 19.11 Production from 14: Blocks and Statements */
1339 /* Store the location of the `}' when doing xrefs */
1340 if (current_function_decl && flag_emit_xref)
1341 DECL_END_SOURCE_LINE (current_function_decl) =
1342 EXPR_WFL_ADD_COL ($2.location, 1);
1343 $$ = empty_stmt_node;
1345 | block_begin block_statements block_end
1357 maybe_absorb_scoping_blocks ();
1358 /* Store the location of the `}' when doing xrefs */
1359 if (current_function_decl && flag_emit_xref)
1360 DECL_END_SOURCE_LINE (current_function_decl) =
1361 EXPR_WFL_ADD_COL ($1.location, 1);
1363 if (!BLOCK_SUBBLOCKS ($$))
1364 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1370 | block_statements block_statement
1374 local_variable_declaration_statement
1376 { java_method_add_stmt (current_function_decl, $1); }
1377 | class_declaration /* Added, JDK1.1 local classes */
1379 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1380 end_class_declaration (1);
1384 local_variable_declaration_statement:
1385 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1388 local_variable_declaration:
1389 type variable_declarators
1390 { declare_local_variables (0, $1, $2); }
1391 | final type variable_declarators /* Added, JDK1.1 final locals */
1392 { declare_local_variables ($1, $2, $3); }
1396 statement_without_trailing_substatement
1399 | if_then_else_statement
1402 { $$ = exit_block (); }
1406 statement_without_trailing_substatement
1407 | labeled_statement_nsi
1408 | if_then_else_statement_nsi
1409 | while_statement_nsi
1411 { $$ = exit_block (); }
1414 statement_without_trailing_substatement:
1417 | expression_statement
1421 | continue_statement
1423 | synchronized_statement
1431 if (flag_extraneous_semicolon)
1433 EXPR_WFL_SET_LINECOL (wfl_operator, lineno, -1);
1434 parse_warning_context (wfl_operator, "An empty declaration is a deprecated feature that should not be used");
1436 $$ = empty_stmt_node;
1441 identifier REL_CL_TK
1443 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1444 EXPR_WFL_NODE ($1));
1446 push_labeled_block ($$);
1447 PUSH_LABELED_BLOCK ($$);
1452 label_decl statement
1453 { $$ = finish_labeled_statement ($1, $2); }
1455 {yyerror ("':' expected"); RECOVER;}
1458 labeled_statement_nsi:
1459 label_decl statement_nsi
1460 { $$ = finish_labeled_statement ($1, $2); }
1463 /* We concentrate here a bunch of error handling rules that we couldn't write
1464 earlier, because expression_statement catches a missing ';'. */
1465 expression_statement:
1466 statement_expression SC_TK
1468 /* We have a statement. Generate a WFL around it so
1470 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1471 /* We know we have a statement, so set the debug
1472 info to be eventually generate here. */
1473 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1477 YYNOT_TWICE yyerror ("Invalid expression statement");
1478 DRECOVER (expr_stmt);
1482 YYNOT_TWICE yyerror ("Invalid expression statement");
1483 DRECOVER (expr_stmt);
1487 YYNOT_TWICE yyerror ("Invalid expression statement");
1488 DRECOVER (expr_stmt);
1490 | this_or_super OP_TK error
1491 {yyerror ("')' expected"); RECOVER;}
1492 | this_or_super OP_TK CP_TK error
1494 parse_ctor_invocation_error ();
1497 | this_or_super OP_TK argument_list error
1498 {yyerror ("')' expected"); RECOVER;}
1499 | this_or_super OP_TK argument_list CP_TK error
1501 parse_ctor_invocation_error ();
1504 | name DOT_TK SUPER_TK error
1505 {yyerror ("'(' expected"); RECOVER;}
1506 | name DOT_TK SUPER_TK OP_TK error
1507 {yyerror ("')' expected"); RECOVER;}
1508 | name DOT_TK SUPER_TK OP_TK argument_list error
1509 {yyerror ("')' expected"); RECOVER;}
1510 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1511 {yyerror ("';' expected"); RECOVER;}
1512 | name DOT_TK SUPER_TK OP_TK CP_TK error
1513 {yyerror ("';' expected"); RECOVER;}
1516 statement_expression:
1518 | pre_increment_expression
1519 | pre_decrement_expression
1520 | post_increment_expression
1521 | post_decrement_expression
1523 | class_instance_creation_expression
1527 IF_TK OP_TK expression CP_TK statement
1529 $$ = build_if_else_statement ($2.location, $3,
1533 {yyerror ("'(' expected"); RECOVER;}
1535 {yyerror ("Missing term"); RECOVER;}
1536 | IF_TK OP_TK expression error
1537 {yyerror ("')' expected"); RECOVER;}
1540 if_then_else_statement:
1541 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1542 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1545 if_then_else_statement_nsi:
1546 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1547 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1557 /* Make into "proper list" of COMPOUND_EXPRs.
1558 I.e. make the last statment also have its own
1560 maybe_absorb_scoping_blocks ();
1561 TREE_OPERAND ($1, 1) = exit_block ();
1562 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1567 SWITCH_TK OP_TK expression CP_TK
1569 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1570 EXPR_WFL_LINECOL ($$) = $2.location;
1573 {yyerror ("'(' expected"); RECOVER;}
1574 | SWITCH_TK OP_TK error
1575 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1576 | SWITCH_TK OP_TK expression CP_TK error
1577 {yyerror ("'{' expected"); RECOVER;}
1580 /* Default assignment is there to avoid type node on switch_block
1586 | OCB_TK switch_labels CCB_TK
1588 | OCB_TK switch_block_statement_groups CCB_TK
1590 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1594 switch_block_statement_groups:
1595 switch_block_statement_group
1596 | switch_block_statement_groups switch_block_statement_group
1599 switch_block_statement_group:
1600 switch_labels block_statements
1605 | switch_labels switch_label
1609 CASE_TK constant_expression REL_CL_TK
1611 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1612 EXPR_WFL_LINECOL (lab) = $1.location;
1613 java_method_add_stmt (current_function_decl, lab);
1615 | DEFAULT_TK REL_CL_TK
1617 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1618 EXPR_WFL_LINECOL (lab) = $1.location;
1619 java_method_add_stmt (current_function_decl, lab);
1622 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1623 | CASE_TK constant_expression error
1624 {yyerror ("':' expected"); RECOVER;}
1626 {yyerror ("':' expected"); RECOVER;}
1630 WHILE_TK OP_TK expression CP_TK
1632 tree body = build_loop_body ($2.location, $3, 0);
1633 $$ = build_new_loop (body);
1638 while_expression statement
1639 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1641 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1642 | WHILE_TK OP_TK error
1643 {yyerror ("Missing term and ')' expected"); RECOVER;}
1644 | WHILE_TK OP_TK expression error
1645 {yyerror ("')' expected"); RECOVER;}
1648 while_statement_nsi:
1649 while_expression statement_nsi
1650 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1656 tree body = build_loop_body (0, NULL_TREE, 1);
1657 $$ = build_new_loop (body);
1659 /* Need error handing here. FIXME */
1663 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1664 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1668 for_begin SC_TK expression SC_TK for_update CP_TK statement
1670 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1671 $3 = build_wfl_node ($3);
1672 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1674 | for_begin SC_TK SC_TK for_update CP_TK statement
1676 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1677 /* We have not condition, so we get rid of the EXIT_EXPR */
1678 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1681 | for_begin SC_TK error
1682 {yyerror ("Invalid control expression"); RECOVER;}
1683 | for_begin SC_TK expression SC_TK error
1684 {yyerror ("Invalid update expression"); RECOVER;}
1685 | for_begin SC_TK SC_TK error
1686 {yyerror ("Invalid update expression"); RECOVER;}
1690 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1691 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1692 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1694 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1695 /* We have not condition, so we get rid of the EXIT_EXPR */
1696 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1704 /* This scope defined for local variable that may be
1705 defined within the scope of the for loop */
1709 {yyerror ("'(' expected"); DRECOVER(for_1);}
1710 | FOR_TK OP_TK error
1711 {yyerror ("Invalid init statement"); RECOVER;}
1717 /* We now declare the loop body. The loop is
1718 declared as a for loop. */
1719 tree body = build_loop_body (0, NULL_TREE, 0);
1720 $$ = build_new_loop (body);
1721 FOR_LOOP_P ($$) = 1;
1722 /* The loop is added to the current block the for
1723 statement is defined within */
1724 java_method_add_stmt (current_function_decl, $$);
1727 for_init: /* Can be empty */
1728 { $$ = empty_stmt_node; }
1729 | statement_expression_list
1731 /* Init statement recorded within the previously
1732 defined block scope */
1733 $$ = java_method_add_stmt (current_function_decl, $1);
1735 | local_variable_declaration
1737 /* Local variable are recorded within the previously
1738 defined block scope */
1741 | statement_expression_list error
1742 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1745 for_update: /* Can be empty */
1746 {$$ = empty_stmt_node;}
1747 | statement_expression_list
1748 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1751 statement_expression_list:
1752 statement_expression
1753 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1754 | statement_expression_list C_TK statement_expression
1755 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1756 | statement_expression_list C_TK error
1757 {yyerror ("Missing term"); RECOVER;}
1762 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1763 | BREAK_TK identifier SC_TK
1764 { $$ = build_bc_statement ($1.location, 1, $2); }
1766 {yyerror ("Missing term"); RECOVER;}
1767 | BREAK_TK identifier error
1768 {yyerror ("';' expected"); RECOVER;}
1773 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1774 | CONTINUE_TK identifier SC_TK
1775 { $$ = build_bc_statement ($1.location, 0, $2); }
1777 {yyerror ("Missing term"); RECOVER;}
1778 | CONTINUE_TK identifier error
1779 {yyerror ("';' expected"); RECOVER;}
1784 { $$ = build_return ($1.location, NULL_TREE); }
1785 | RETURN_TK expression SC_TK
1786 { $$ = build_return ($1.location, $2); }
1788 {yyerror ("Missing term"); RECOVER;}
1789 | RETURN_TK expression error
1790 {yyerror ("';' expected"); RECOVER;}
1794 THROW_TK expression SC_TK
1796 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1797 EXPR_WFL_LINECOL ($$) = $1.location;
1800 {yyerror ("Missing term"); RECOVER;}
1801 | THROW_TK expression error
1802 {yyerror ("';' expected"); RECOVER;}
1805 synchronized_statement:
1806 synchronized OP_TK expression CP_TK block
1808 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1809 EXPR_WFL_LINECOL ($$) =
1810 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1812 | synchronized OP_TK expression CP_TK error
1813 {yyerror ("'{' expected"); RECOVER;}
1814 | synchronized error
1815 {yyerror ("'(' expected"); RECOVER;}
1816 | synchronized OP_TK error CP_TK
1817 {yyerror ("Missing term"); RECOVER;}
1818 | synchronized OP_TK error
1819 {yyerror ("Missing term"); RECOVER;}
1826 "Illegal modifier `%s'. Only `synchronized' was expected here",
1827 $1, ACC_SYNCHRONIZED);
1828 if ($1 != ACC_SYNCHRONIZED)
1829 MODIFIER_WFL (SYNCHRONIZED_TK) =
1830 build_wfl_node (NULL_TREE);
1835 TRY_TK block catches
1836 { $$ = build_try_statement ($1.location, $2, $3); }
1837 | TRY_TK block finally
1838 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1839 | TRY_TK block catches finally
1840 { $$ = build_try_finally_statement
1841 ($1.location, build_try_statement ($1.location,
1845 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1850 | catches catch_clause
1852 TREE_CHAIN ($2) = $1;
1858 catch_clause_parameter block
1860 java_method_add_stmt (current_function_decl, $2);
1865 catch_clause_parameter:
1866 CATCH_TK OP_TK formal_parameter CP_TK
1868 /* We add a block to define a scope for
1869 formal_parameter (CCBP). The formal parameter is
1870 declared initialized by the appropriate function
1872 tree ccpb = enter_block ();
1873 tree init = build_assignment (ASSIGN_TK, $2.location,
1875 soft_exceptioninfo_call_node);
1876 declare_local_variables (0, TREE_VALUE ($3),
1877 build_tree_list (TREE_PURPOSE ($3),
1879 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1880 EXPR_WFL_LINECOL ($$) = $1.location;
1883 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1884 | CATCH_TK OP_TK error
1886 yyerror ("Missing term or ')' expected");
1887 RECOVER; $$ = NULL_TREE;
1889 | CATCH_TK OP_TK error CP_TK /* That's for () */
1890 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1897 {yyerror ("'{' expected"); RECOVER; }
1900 /* 19.12 Production from 15: Expressions */
1902 primary_no_new_array
1903 | array_creation_expression
1906 primary_no_new_array:
1909 { $$ = build_this ($1.location); }
1910 | OP_TK expression CP_TK
1912 | class_instance_creation_expression
1917 /* Added, JDK1.1 inner classes. Documentation is wrong
1918 refering to a 'ClassName' (class_name) rule that doesn't
1919 exist. Used name: instead. */
1920 | name DOT_TK THIS_TK
1922 tree wfl = build_wfl_node (this_identifier_node);
1923 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1925 | OP_TK expression error
1926 {yyerror ("')' expected"); RECOVER;}
1928 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1929 | primitive_type DOT_TK error
1930 {yyerror ("'class' expected" ); RECOVER;}
1931 | VOID_TK DOT_TK error
1932 {yyerror ("'class' expected" ); RECOVER;}
1935 /* Added, JDK1.1 type literals. We can't use `type' directly, so we
1936 broke the rule down a bit. */
1939 primitive_type OSB_TK CSB_TK
1941 $$ = build_java_array_type ($1, -1);
1942 CLASS_LOADED_P ($$) = 1;
1944 | name OSB_TK CSB_TK
1945 { $$ = build_unresolved_array_type ($1); }
1946 /* This triggers two reduce/reduce conflict between array_type_literal and
1948 | array_type OSB_TK CSB_TK
1949 { $$ = build_unresolved_array_type ($1); }
1954 name DOT_TK CLASS_TK
1955 { $$ = build_incomplete_class_ref ($2.location, $1); }
1956 | array_type_literal DOT_TK CLASS_TK
1957 { $$ = build_incomplete_class_ref ($2.location, $1); }
1958 | primitive_type DOT_TK CLASS_TK
1959 { $$ = build_class_ref ($1); }
1960 | VOID_TK DOT_TK CLASS_TK
1961 { $$ = build_class_ref (void_type_node); }
1964 class_instance_creation_expression:
1965 NEW_TK class_type OP_TK argument_list CP_TK
1966 { $$ = build_new_invocation ($2, $4); }
1967 | NEW_TK class_type OP_TK CP_TK
1968 { $$ = build_new_invocation ($2, NULL_TREE); }
1969 | anonymous_class_creation
1970 /* Added, JDK1.1 inner classes, modified to use name or
1971 primary instead of primary solely which couldn't work in
1973 | something_dot_new identifier OP_TK CP_TK
1975 tree ctor = build_new_invocation ($2, NULL_TREE);
1976 $$ = make_qualified_primary ($1, ctor,
1977 EXPR_WFL_LINECOL ($1));
1979 | something_dot_new identifier OP_TK CP_TK class_body
1980 | something_dot_new identifier OP_TK argument_list CP_TK
1982 tree ctor = build_new_invocation ($2, $4);
1983 $$ = make_qualified_primary ($1, ctor,
1984 EXPR_WFL_LINECOL ($1));
1986 | something_dot_new identifier OP_TK argument_list CP_TK class_body
1987 | NEW_TK error SC_TK
1988 {yyerror ("'(' expected"); DRECOVER(new_1);}
1989 | NEW_TK class_type error
1990 {yyerror ("'(' expected"); RECOVER;}
1991 | NEW_TK class_type OP_TK error
1992 {yyerror ("')' or term expected"); RECOVER;}
1993 | NEW_TK class_type OP_TK argument_list error
1994 {yyerror ("')' expected"); RECOVER;}
1995 | something_dot_new error
1996 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1997 | something_dot_new identifier error
1998 {yyerror ("'(' expected"); RECOVER;}
2001 /* Created after JDK1.1 rules originally added to
2002 class_instance_creation_expression, but modified to use
2003 'class_type' instead of 'TypeName' (type_name) which is mentionned
2004 in the documentation but doesn't exist. */
2006 anonymous_class_creation:
2007 NEW_TK class_type OP_TK argument_list CP_TK
2008 { create_anonymous_class ($1.location, $2); }
2011 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2012 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2014 end_class_declaration (1);
2016 /* Now we can craft the new expression */
2017 $$ = build_new_invocation (id, $4);
2019 /* Note that we can't possibly be here if
2020 `class_type' is an interface (in which case the
2021 anonymous class extends Object and implements
2022 `class_type', hence its constructor can't have
2025 /* Otherwise, the innerclass must feature a
2026 constructor matching `argument_list'. Anonymous
2027 classes are a bit special: it's impossible to
2028 define constructor for them, hence constructors
2029 must be generated following the hints provided by
2030 the `new' expression. Whether a super constructor
2031 of that nature exists or not is to be verified
2032 later on in verify_constructor_super.
2034 It's during the expansion of a `new' statement
2035 refering to an anonymous class that a ctor will
2036 be generated for the anonymous class, with the
2040 | NEW_TK class_type OP_TK CP_TK
2041 { create_anonymous_class ($1.location, $2); }
2044 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2045 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2047 end_class_declaration (1);
2049 /* Now we can craft the new expression. The
2050 statement doesn't need to be remember so that a
2051 constructor can be generated, since its signature
2052 is already known. */
2053 $$ = build_new_invocation (id, NULL_TREE);
2057 something_dot_new: /* Added, not part of the specs. */
2060 | primary DOT_TK NEW_TK
2067 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2068 ctxp->formal_parameter_number = 1;
2070 | argument_list C_TK expression
2072 ctxp->formal_parameter_number += 1;
2073 $$ = tree_cons (NULL_TREE, $3, $1);
2075 | argument_list C_TK error
2076 {yyerror ("Missing term"); RECOVER;}
2079 array_creation_expression:
2080 NEW_TK primitive_type dim_exprs
2081 { $$ = build_newarray_node ($2, $3, 0); }
2082 | NEW_TK class_or_interface_type dim_exprs
2083 { $$ = build_newarray_node ($2, $3, 0); }
2084 | NEW_TK primitive_type dim_exprs dims
2085 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
2086 | NEW_TK class_or_interface_type dim_exprs dims
2087 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
2088 /* Added, JDK1.1 anonymous array. Initial documentation rule
2090 | NEW_TK class_or_interface_type dims array_initializer
2093 while (CURRENT_OSB (ctxp)--)
2094 obstack_1grow (&temporary_obstack, '[');
2095 sig = obstack_finish (&temporary_obstack);
2096 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2097 $2, get_identifier (sig), $4);
2099 | NEW_TK primitive_type dims array_initializer
2102 while (CURRENT_OSB (ctxp)--)
2103 type = build_java_array_type (type, -1);
2104 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2105 build_pointer_type (type), NULL_TREE, $4);
2107 | NEW_TK error CSB_TK
2108 {yyerror ("'[' expected"); DRECOVER ("]");}
2109 | NEW_TK error OSB_TK
2110 {yyerror ("']' expected"); RECOVER;}
2115 { $$ = build_tree_list (NULL_TREE, $1); }
2116 | dim_exprs dim_expr
2117 { $$ = tree_cons (NULL_TREE, $2, $$); }
2121 OSB_TK expression CSB_TK
2123 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2125 $2 = build_wfl_node ($2);
2126 TREE_TYPE ($2) = NULL_TREE;
2128 EXPR_WFL_LINECOL ($2) = $1.location;
2131 | OSB_TK expression error
2132 {yyerror ("']' expected"); RECOVER;}
2135 yyerror ("Missing term");
2136 yyerror ("']' expected");
2145 /* If not initialized, allocate memory for the osb
2147 if (!ctxp->osb_limit)
2149 allocate = ctxp->osb_limit = 32;
2150 ctxp->osb_depth = -1;
2152 /* If capacity overflown, reallocate a bigger chunk */
2153 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2154 allocate = ctxp->osb_limit << 1;
2158 allocate *= sizeof (int);
2159 if (ctxp->osb_number)
2160 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2163 ctxp->osb_number = (int *)xmalloc (allocate);
2166 CURRENT_OSB (ctxp) = 1;
2168 | dims OSB_TK CSB_TK
2169 { CURRENT_OSB (ctxp)++; }
2171 { yyerror ("']' expected"); RECOVER;}
2175 primary DOT_TK identifier
2176 { $$ = make_qualified_primary ($1, $3, $2.location); }
2177 /* FIXME - REWRITE TO:
2178 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2179 | SUPER_TK DOT_TK identifier
2181 tree super_wfl = build_wfl_node (super_identifier_node);
2182 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2183 $$ = make_qualified_name (super_wfl, $3, $2.location);
2186 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2191 { $$ = build_method_invocation ($1, NULL_TREE); }
2192 | name OP_TK argument_list CP_TK
2193 { $$ = build_method_invocation ($1, $3); }
2194 | primary DOT_TK identifier OP_TK CP_TK
2196 if (TREE_CODE ($1) == THIS_EXPR)
2197 $$ = build_this_super_qualified_invocation
2198 (1, $3, NULL_TREE, 0, $2.location);
2201 tree invok = build_method_invocation ($3, NULL_TREE);
2202 $$ = make_qualified_primary ($1, invok, $2.location);
2205 | primary DOT_TK identifier OP_TK argument_list CP_TK
2207 if (TREE_CODE ($1) == THIS_EXPR)
2208 $$ = build_this_super_qualified_invocation
2209 (1, $3, $5, 0, $2.location);
2212 tree invok = build_method_invocation ($3, $5);
2213 $$ = make_qualified_primary ($1, invok, $2.location);
2216 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2218 $$ = build_this_super_qualified_invocation
2219 (0, $3, NULL_TREE, $1.location, $2.location);
2221 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2223 $$ = build_this_super_qualified_invocation
2224 (0, $3, $5, $1.location, $2.location);
2226 /* Screws up thing. I let it here until I'm convinced it can
2228 | primary DOT_TK error
2229 {yyerror ("'(' expected"); DRECOVER(bad);} */
2230 | SUPER_TK DOT_TK error CP_TK
2231 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2232 | SUPER_TK DOT_TK error DOT_TK
2233 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2237 name OSB_TK expression CSB_TK
2238 { $$ = build_array_ref ($2.location, $1, $3); }
2239 | primary_no_new_array OSB_TK expression CSB_TK
2240 { $$ = build_array_ref ($2.location, $1, $3); }
2243 yyerror ("Missing term and ']' expected");
2244 DRECOVER(array_access);
2246 | name OSB_TK expression error
2248 yyerror ("']' expected");
2249 DRECOVER(array_access);
2251 | primary_no_new_array OSB_TK error
2253 yyerror ("Missing term and ']' expected");
2254 DRECOVER(array_access);
2256 | primary_no_new_array OSB_TK expression error
2258 yyerror ("']' expected");
2259 DRECOVER(array_access);
2266 | post_increment_expression
2267 | post_decrement_expression
2270 post_increment_expression:
2271 postfix_expression INCR_TK
2272 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2275 post_decrement_expression:
2276 postfix_expression DECR_TK
2277 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2281 pre_increment_expression
2282 | pre_decrement_expression
2283 | PLUS_TK unary_expression
2284 {$$ = build_unaryop ($1.token, $1.location, $2); }
2285 | MINUS_TK unary_expression
2286 {$$ = build_unaryop ($1.token, $1.location, $2); }
2287 | unary_expression_not_plus_minus
2289 {yyerror ("Missing term"); RECOVER}
2291 {yyerror ("Missing term"); RECOVER}
2294 pre_increment_expression:
2295 INCR_TK unary_expression
2296 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2298 {yyerror ("Missing term"); RECOVER}
2301 pre_decrement_expression:
2302 DECR_TK unary_expression
2303 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2305 {yyerror ("Missing term"); RECOVER}
2308 unary_expression_not_plus_minus:
2310 | NOT_TK unary_expression
2311 {$$ = build_unaryop ($1.token, $1.location, $2); }
2312 | NEG_TK unary_expression
2313 {$$ = build_unaryop ($1.token, $1.location, $2); }
2316 {yyerror ("Missing term"); RECOVER}
2318 {yyerror ("Missing term"); RECOVER}
2321 cast_expression: /* Error handling here is potentially weak */
2322 OP_TK primitive_type dims CP_TK unary_expression
2325 while (CURRENT_OSB (ctxp)--)
2326 type = build_java_array_type (type, -1);
2328 $$ = build_cast ($1.location, type, $5);
2330 | OP_TK primitive_type CP_TK unary_expression
2331 { $$ = build_cast ($1.location, $2, $4); }
2332 | OP_TK expression CP_TK unary_expression_not_plus_minus
2333 { $$ = build_cast ($1.location, $2, $4); }
2334 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2337 while (CURRENT_OSB (ctxp)--)
2338 obstack_1grow (&temporary_obstack, '[');
2340 obstack_grow0 (&temporary_obstack,
2341 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2342 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2343 ptr = obstack_finish (&temporary_obstack);
2344 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2345 $$ = build_cast ($1.location, $2, $5);
2347 | OP_TK primitive_type OSB_TK error
2348 {yyerror ("']' expected, invalid type expression");}
2351 YYNOT_TWICE yyerror ("Invalid type expression"); RECOVER;
2354 | OP_TK primitive_type dims CP_TK error
2355 {yyerror ("Missing term"); RECOVER;}
2356 | OP_TK primitive_type CP_TK error
2357 {yyerror ("Missing term"); RECOVER;}
2358 | OP_TK name dims CP_TK error
2359 {yyerror ("Missing term"); RECOVER;}
2362 multiplicative_expression:
2364 | multiplicative_expression MULT_TK unary_expression
2366 $$ = build_binop (BINOP_LOOKUP ($2.token),
2367 $2.location, $1, $3);
2369 | multiplicative_expression DIV_TK unary_expression
2371 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2374 | multiplicative_expression REM_TK unary_expression
2376 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2379 | multiplicative_expression MULT_TK error
2380 {yyerror ("Missing term"); RECOVER;}
2381 | multiplicative_expression DIV_TK error
2382 {yyerror ("Missing term"); RECOVER;}
2383 | multiplicative_expression REM_TK error
2384 {yyerror ("Missing term"); RECOVER;}
2387 additive_expression:
2388 multiplicative_expression
2389 | additive_expression PLUS_TK multiplicative_expression
2391 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2394 | additive_expression MINUS_TK multiplicative_expression
2396 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2399 | additive_expression PLUS_TK error
2400 {yyerror ("Missing term"); RECOVER;}
2401 | additive_expression MINUS_TK error
2402 {yyerror ("Missing term"); RECOVER;}
2407 | shift_expression LS_TK additive_expression
2409 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2412 | shift_expression SRS_TK additive_expression
2414 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2417 | shift_expression ZRS_TK additive_expression
2419 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2422 | shift_expression LS_TK error
2423 {yyerror ("Missing term"); RECOVER;}
2424 | shift_expression SRS_TK error
2425 {yyerror ("Missing term"); RECOVER;}
2426 | shift_expression ZRS_TK error
2427 {yyerror ("Missing term"); RECOVER;}
2430 relational_expression:
2432 | relational_expression LT_TK shift_expression
2434 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2437 | relational_expression GT_TK shift_expression
2439 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2442 | relational_expression LTE_TK shift_expression
2444 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2447 | relational_expression GTE_TK shift_expression
2449 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2452 | relational_expression INSTANCEOF_TK reference_type
2453 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2454 | relational_expression LT_TK error
2455 {yyerror ("Missing term"); RECOVER;}
2456 | relational_expression GT_TK error
2457 {yyerror ("Missing term"); RECOVER;}
2458 | relational_expression LTE_TK error
2459 {yyerror ("Missing term"); RECOVER;}
2460 | relational_expression GTE_TK error
2461 {yyerror ("Missing term"); RECOVER;}
2462 | relational_expression INSTANCEOF_TK error
2463 {yyerror ("Invalid reference type"); RECOVER;}
2466 equality_expression:
2467 relational_expression
2468 | equality_expression EQ_TK relational_expression
2470 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2473 | equality_expression NEQ_TK relational_expression
2475 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2478 | equality_expression EQ_TK error
2479 {yyerror ("Missing term"); RECOVER;}
2480 | equality_expression NEQ_TK error
2481 {yyerror ("Missing term"); RECOVER;}
2486 | and_expression AND_TK equality_expression
2488 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2491 | and_expression AND_TK error
2492 {yyerror ("Missing term"); RECOVER;}
2495 exclusive_or_expression:
2497 | exclusive_or_expression XOR_TK and_expression
2499 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2502 | exclusive_or_expression XOR_TK error
2503 {yyerror ("Missing term"); RECOVER;}
2506 inclusive_or_expression:
2507 exclusive_or_expression
2508 | inclusive_or_expression OR_TK exclusive_or_expression
2510 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2513 | inclusive_or_expression OR_TK error
2514 {yyerror ("Missing term"); RECOVER;}
2517 conditional_and_expression:
2518 inclusive_or_expression
2519 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2521 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2524 | conditional_and_expression BOOL_AND_TK error
2525 {yyerror ("Missing term"); RECOVER;}
2528 conditional_or_expression:
2529 conditional_and_expression
2530 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2532 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2535 | conditional_or_expression BOOL_OR_TK error
2536 {yyerror ("Missing term"); RECOVER;}
2539 conditional_expression: /* Error handling here is weak */
2540 conditional_or_expression
2541 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2543 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2544 EXPR_WFL_LINECOL ($$) = $2.location;
2546 | conditional_or_expression REL_QM_TK REL_CL_TK error
2549 yyerror ("Missing term");
2552 | conditional_or_expression REL_QM_TK error
2553 {yyerror ("Missing term"); DRECOVER (2);}
2554 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2555 {yyerror ("Missing term"); DRECOVER (3);}
2558 assignment_expression:
2559 conditional_expression
2564 left_hand_side assignment_operator assignment_expression
2565 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2566 | left_hand_side assignment_operator error
2568 YYNOT_TWICE yyerror ("Missing term");
2579 assignment_operator:
2585 assignment_expression
2588 constant_expression:
2595 /* This section of the code deal with save/restoring parser contexts.
2596 Add mode documentation here. FIXME */
2598 /* Helper function. Create a new parser context. With
2599 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2600 context is copied, otherwise, the new context is zeroed. The newly
2601 created context becomes the current one. */
2604 create_new_parser_context (copy_from_previous)
2605 int copy_from_previous;
2607 struct parser_ctxt *new;
2609 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2610 if (copy_from_previous)
2612 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2613 new->saved_data_ctx = 1;
2616 bzero ((PTR) new, sizeof (struct parser_ctxt));
2622 /* Create a new parser context and make it the current one. */
2625 java_push_parser_context ()
2627 create_new_parser_context (0);
2630 ctxp->incomplete_class = ctxp->next->incomplete_class;
2631 ctxp->gclass_list = ctxp->next->gclass_list;
2636 java_pop_parser_context (generate)
2640 struct parser_ctxt *toFree, *next;
2649 next->incomplete_class = ctxp->incomplete_class;
2650 next->gclass_list = ctxp->gclass_list;
2651 lineno = ctxp->lineno;
2652 current_class = ctxp->class_type;
2655 /* If the old and new lexers differ, then free the old one. */
2656 if (ctxp->lexer && next && ctxp->lexer != next->lexer)
2657 java_destroy_lexer (ctxp->lexer);
2659 /* Set the single import class file flag to 0 for the current list
2660 of imported things */
2661 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2662 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2664 /* And restore those of the previous context */
2665 if ((ctxp = next)) /* Assignment is really meant here */
2666 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2667 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2669 /* If we pushed a context to parse a class intended to be generated,
2670 we keep it so we can remember the class. What we could actually
2671 do is to just update a list of class names. */
2674 toFree->next = ctxp_for_generation;
2675 ctxp_for_generation = toFree;
2681 /* Create a parser context for the use of saving some global
2685 java_parser_context_save_global ()
2689 java_push_parser_context ();
2690 ctxp->saved_data_ctx = 1;
2693 /* If this context already stores data, create a new one suitable
2694 for data storage. */
2695 else if (ctxp->saved_data)
2696 create_new_parser_context (1);
2698 ctxp->lineno = lineno;
2699 ctxp->class_type = current_class;
2700 ctxp->filename = input_filename;
2701 ctxp->function_decl = current_function_decl;
2702 ctxp->saved_data = 1;
2705 /* Restore some global variables from the previous context. Make the
2706 previous context the current one. */
2709 java_parser_context_restore_global ()
2711 lineno = ctxp->lineno;
2712 current_class = ctxp->class_type;
2713 input_filename = ctxp->filename;
2714 current_function_decl = ctxp->function_decl;
2715 ctxp->saved_data = 0;
2716 if (ctxp->saved_data_ctx)
2717 java_pop_parser_context (0);
2720 /* Suspend vital data for the current class/function being parsed so
2721 that an other class can be parsed. Used to let local/anonymous
2722 classes be parsed. */
2725 java_parser_context_suspend ()
2727 /* This makes debugging through java_debug_context easier */
2728 static const char *name = "<inner buffer context>";
2730 /* Duplicate the previous context, use it to save the globals we're
2732 create_new_parser_context (1);
2733 ctxp->function_decl = current_function_decl;
2734 ctxp->class_type = current_class;
2736 /* Then create a new context which inherits all data from the
2737 previous one. This will be the new current context */
2738 create_new_parser_context (1);
2740 /* Help debugging */
2741 ctxp->next->filename = name;
2744 /* Resume vital data for the current class/function being parsed so
2745 that an other class can be parsed. Used to let local/anonymous
2746 classes be parsed. The trick is the data storing file position
2747 informations must be restored to their current value, so parsing
2748 can resume as if no context was ever saved. */
2751 java_parser_context_resume ()
2753 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2754 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2755 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2757 /* We need to inherit the list of classes to complete/generate */
2758 restored->incomplete_class = old->incomplete_class;
2759 restored->gclass_list = old->gclass_list;
2760 restored->classd_list = old->classd_list;
2761 restored->class_list = old->class_list;
2763 /* Restore the current class and function from the saver */
2764 current_class = saver->class_type;
2765 current_function_decl = saver->function_decl;
2767 /* Retrive the restored context */
2770 /* Re-installed the data for the parsing to carry on */
2771 bcopy (&old->marker_begining, &ctxp->marker_begining,
2772 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2774 /* Buffer context can now be discarded */
2779 /* Add a new anchor node to which all statement(s) initializing static
2780 and non static initialized upon declaration field(s) will be
2784 java_parser_context_push_initialized_field ()
2788 node = build_tree_list (NULL_TREE, NULL_TREE);
2789 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2790 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2792 node = build_tree_list (NULL_TREE, NULL_TREE);
2793 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2794 CPC_INITIALIZER_LIST (ctxp) = node;
2796 node = build_tree_list (NULL_TREE, NULL_TREE);
2797 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2798 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2801 /* Pop the lists of initialized field. If this lists aren't empty,
2802 remember them so we can use it to create and populate the finit$
2803 or <clinit> functions. */
2806 java_parser_context_pop_initialized_field ()
2809 tree class_type = TREE_TYPE (GET_CPC ());
2811 if (CPC_INITIALIZER_LIST (ctxp))
2813 stmts = CPC_INITIALIZER_STMT (ctxp);
2814 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2815 if (stmts && !java_error_count)
2816 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2819 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2821 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2822 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2823 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2824 /* Keep initialization in order to enforce 8.5 */
2825 if (stmts && !java_error_count)
2826 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2829 /* JDK 1.1 instance initializers */
2830 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2832 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2833 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2834 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2835 if (stmts && !java_error_count)
2836 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2841 reorder_static_initialized (list)
2844 /* We have to keep things in order. The alias initializer have to
2845 come first, then the initialized regular field, in reverse to
2846 keep them in lexical order. */
2847 tree marker, previous = NULL_TREE;
2848 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2849 if (TREE_CODE (marker) == TREE_LIST
2850 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2853 /* No static initialized, the list is fine as is */
2855 list = TREE_CHAIN (marker);
2857 /* No marker? reverse the whole list */
2859 list = nreverse (list);
2861 /* Otherwise, reverse what's after the marker and the new reordered
2862 sublist will replace the marker. */
2865 TREE_CHAIN (previous) = NULL_TREE;
2866 list = nreverse (list);
2867 list = chainon (TREE_CHAIN (marker), list);
2872 /* Helper functions to dump the parser context stack. */
2874 #define TAB_CONTEXT(C) \
2875 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2878 java_debug_context_do (tab)
2881 struct parser_ctxt *copy = ctxp;
2885 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2887 fprintf (stderr, "filename: %s\n", copy->filename);
2889 fprintf (stderr, "lineno: %d\n", copy->lineno);
2891 fprintf (stderr, "package: %s\n",
2893 IDENTIFIER_POINTER (copy->package) : "<none>"));
2895 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2897 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2903 /* Dump the stacked up parser contexts. Intended to be called from a
2907 java_debug_context ()
2909 java_debug_context_do (0);
2914 /* Flag for the error report routine to issue the error the first time
2915 it's called (overriding the default behavior which is to drop the
2916 first invocation and honor the second one, taking advantage of a
2918 static int force_error = 0;
2920 /* Reporting an constructor invocation error. */
2922 parse_ctor_invocation_error ()
2924 if (DECL_CONSTRUCTOR_P (current_function_decl))
2925 yyerror ("Constructor invocation must be first thing in a constructor");
2927 yyerror ("Only constructors can invoke constructors");
2930 /* Reporting JDK1.1 features not implemented. */
2933 parse_jdk1_1_error (msg)
2936 sorry (": `%s' JDK1.1(TM) feature", msg);
2938 return empty_stmt_node;
2941 static int do_warning = 0;
2948 static int prev_lineno;
2949 static const char *prev_msg;
2952 char *remainder, *code_from_source;
2953 extern struct obstack temporary_obstack;
2955 if (!force_error && prev_lineno == lineno)
2958 /* Save current error location but report latter, when the context is
2960 if (ctxp->java_error_flag == 0)
2962 ctxp->java_error_flag = 1;
2964 /* Do something to use the previous line if we're reaching the
2965 end of the file... */
2966 #ifdef VERBOSE_SKELETON
2967 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2972 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2973 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2976 ctxp->java_error_flag = 0;
2978 java_warning_count++;
2982 if (elc.col == 0 && msg && msg[1] == ';')
2984 elc.col = ctxp->p_line->char_col-1;
2985 elc.line = ctxp->p_line->lineno;
2988 save_lineno = lineno;
2989 prev_lineno = lineno = elc.line;
2992 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
2993 obstack_grow0 (&temporary_obstack,
2994 code_from_source, strlen (code_from_source));
2995 remainder = obstack_finish (&temporary_obstack);
2997 warning ("%s.\n%s", msg, remainder);
2999 error ("%s.\n%s", msg, remainder);
3001 /* This allow us to cheaply avoid an extra 'Invalid expression
3002 statement' error report when errors have been already reported on
3003 the same line. This occurs when we report an error but don't have
3004 a synchronization point other than ';', which
3005 expression_statement is the only one to take care of. */
3006 ctxp->prevent_ese = lineno = save_lineno;
3010 issue_warning_error_from_context (cl, msg, ap)
3015 const char *saved, *saved_input_filename;
3017 vsprintf (buffer, msg, ap);
3020 ctxp->elc.line = EXPR_WFL_LINENO (cl);
3021 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
3022 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
3024 /* We have a CL, that's a good reason for using it if it contains data */
3025 saved = ctxp->filename;
3026 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3027 ctxp->filename = EXPR_WFL_FILENAME (cl);
3028 saved_input_filename = input_filename;
3029 input_filename = ctxp->filename;
3031 java_error (buffer);
3032 ctxp->filename = saved;
3033 input_filename = saved_input_filename;
3037 /* Issue an error message at a current source line CL */
3040 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3042 #ifndef ANSI_PROTOTYPES
3049 #ifndef ANSI_PROTOTYPES
3050 cl = va_arg (ap, tree);
3051 msg = va_arg (ap, const char *);
3053 issue_warning_error_from_context (cl, msg, ap);
3057 /* Issue a warning at a current source line CL */
3060 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3062 #ifndef ANSI_PROTOTYPES
3069 #ifndef ANSI_PROTOTYPES
3070 cl = va_arg (ap, tree);
3071 msg = va_arg (ap, const char *);
3074 force_error = do_warning = 1;
3075 issue_warning_error_from_context (cl, msg, ap);
3076 do_warning = force_error = 0;
3081 find_expr_with_wfl (node)
3089 switch (TREE_CODE (node))
3092 node = BLOCK_EXPR_BODY (node);
3096 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3099 node = TREE_OPERAND (node, 1);
3103 node = TREE_OPERAND (node, 0);
3106 case LABELED_BLOCK_EXPR:
3107 node = TREE_OPERAND (node, 1);
3111 code = TREE_CODE_CLASS (TREE_CODE (node));
3112 if (((code == '1') || (code == '2') || (code == 'e'))
3113 && EXPR_WFL_LINECOL (node))
3121 /* Issue a missing return statement error. Uses METHOD to figure the
3122 last line of the method the error occurs in. */
3125 missing_return_error (method)
3128 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3129 parse_error_context (wfl_operator, "Missing return statement");
3132 /* Issue an unreachable statement error. From NODE, find the next
3133 statement to report appropriately. */
3135 unreachable_stmt_error (node)
3138 /* Browse node to find the next expression node that has a WFL. Use
3139 the location to report the error */
3140 if (TREE_CODE (node) == COMPOUND_EXPR)
3141 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3143 node = find_expr_with_wfl (node);
3147 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3148 parse_error_context (wfl_operator, "Unreachable statement");
3151 fatal ("Can't get valid statement - unreachable_stmt_error");
3155 java_report_errors ()
3157 if (java_error_count)
3158 fprintf (stderr, "%d error%s",
3159 java_error_count, (java_error_count == 1 ? "" : "s"));
3160 if (java_warning_count)
3161 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3162 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3163 if (java_error_count || java_warning_count)
3164 putc ('\n', stderr);
3165 return java_error_count;
3169 java_accstring_lookup (flags)
3172 static char buffer [80];
3173 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3175 /* Access modifier looked-up first for easier report on forbidden
3177 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3178 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3179 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3180 if (flags & ACC_STATIC) COPY_RETURN ("static");
3181 if (flags & ACC_FINAL) COPY_RETURN ("final");
3182 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3183 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3184 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3185 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3186 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3187 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3194 /* Issuing error messages upon redefinition of classes, interfaces or
3198 classitf_redefinition_error (context, id, decl, cl)
3199 const char *context;
3202 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3203 context, IDENTIFIER_POINTER (id),
3204 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3205 /* Here we should point out where its redefined. It's a unicode. FIXME */
3209 variable_redefinition_error (context, name, type, line)
3210 tree context, name, type;
3213 const char *type_name;
3215 /* Figure a proper name for type. We might haven't resolved it */
3216 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3217 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3219 type_name = lang_printable_name (type, 0);
3221 parse_error_context (context,
3222 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3223 IDENTIFIER_POINTER (name),
3224 type_name, IDENTIFIER_POINTER (name), line);
3228 build_array_from_name (type, type_wfl, name, ret_name)
3229 tree type, type_wfl, name, *ret_name;
3234 /* Eventually get more dims */
3235 string = IDENTIFIER_POINTER (name);
3236 while (string [more_dims] == '[')
3239 /* If we have, then craft a new type for this variable */
3242 name = get_identifier (&string [more_dims]);
3244 /* If we have a pointer, use its type */
3245 if (TREE_CODE (type) == POINTER_TYPE)
3246 type = TREE_TYPE (type);
3248 /* Building the first dimension of a primitive type uses this
3250 if (JPRIMITIVE_TYPE_P (type))
3252 type = build_java_array_type (type, -1);
3253 CLASS_LOADED_P (type) = 1;
3256 /* Otherwise, if we have a WFL for this type, use it (the type
3257 is already an array on an unresolved type, and we just keep
3258 on adding dimensions) */
3262 /* Add all the dimensions */
3264 type = build_unresolved_array_type (type);
3266 /* The type may have been incomplete in the first place */
3268 type = obtain_incomplete_type (type);
3276 /* Build something that the type identifier resolver will identify as
3277 being an array to an unresolved type. TYPE_WFL is a WFL on a
3281 build_unresolved_array_type (type_or_wfl)
3286 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3287 just create a array type */
3288 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3290 tree type = build_java_array_type (type_or_wfl, -1);
3291 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3295 obstack_1grow (&temporary_obstack, '[');
3296 obstack_grow0 (&temporary_obstack,
3297 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3298 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3299 ptr = obstack_finish (&temporary_obstack);
3300 EXPR_WFL_NODE (type_or_wfl) = get_identifier (ptr);
3305 parser_add_interface (class_decl, interface_decl, wfl)
3306 tree class_decl, interface_decl, wfl;
3308 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3309 parse_error_context (wfl, "Interface `%s' repeated",
3310 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3313 /* Bulk of common class/interface checks. Return 1 if an error was
3314 encountered. TAG is 0 for a class, 1 for an interface. */
3317 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3318 int is_interface, flags;
3319 tree raw_name, qualified_name, decl, cl;
3322 int sca = 0; /* Static class allowed */
3323 int icaf = 0; /* Inner class allowed flags */
3324 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3327 fprintf (stderr, " %s%s %s",
3328 (CPC_INNER_P () ? "inner" : ""),
3329 (is_interface ? "interface" : "class"),
3330 IDENTIFIER_POINTER (qualified_name));
3332 /* Scope of an interface/class type name:
3333 - Can't be imported by a single type import
3334 - Can't already exists in the package */
3335 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3336 && (node = find_name_in_single_imports (raw_name))
3340 (cl, "%s name `%s' clashes with imported type `%s'",
3341 (is_interface ? "Interface" : "Class"),
3342 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3345 if (decl && CLASS_COMPLETE_P (decl))
3347 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3348 qualified_name, decl, cl);
3352 if (check_inner_class_redefinition (raw_name, cl))
3355 /* If public, file name should match class/interface name, except
3356 when dealing with an inner class */
3357 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3361 /* Contains OS dependent assumption on path separator. FIXME */
3362 for (f = &input_filename [strlen (input_filename)];
3363 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3366 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
3368 if (strncmp (IDENTIFIER_POINTER (raw_name),
3369 f , IDENTIFIER_LENGTH (raw_name)) ||
3370 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3372 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3373 (is_interface ? "interface" : "class"),
3374 IDENTIFIER_POINTER (qualified_name),
3375 IDENTIFIER_POINTER (raw_name));
3378 /* Static classes can be declared only in top level classes. Note:
3379 once static, a inner class is a top level class. */
3380 if (flags & ACC_STATIC)
3382 /* Catch the specific error of declaring an class inner class
3383 with no toplevel enclosing class. Prevent check_modifiers from
3384 complaining a second time */
3385 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3387 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3388 IDENTIFIER_POINTER (qualified_name));
3391 /* Else, in the context of a top-level class declaration, let
3392 `check_modifiers' do its job, otherwise, give it a go */
3394 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3397 /* Inner classes can be declared private or protected
3398 within their enclosing classes. */
3401 /* A class which is local to a block can't be public, private,
3402 protected or static. But it is created final, so allow this
3404 if (current_function_decl)
3405 icaf = sca = uaaf = ACC_FINAL;
3408 check_modifiers_consistency (flags);
3409 icaf = ACC_PRIVATE|ACC_PROTECTED;
3416 uaaf = INTERFACE_INNER_MODIFIERS;
3418 uaaf = INTERFACE_MODIFIERS;
3420 check_modifiers ("Illegal modifier `%s' for interface declaration",
3424 check_modifiers ((current_function_decl ?
3425 "Illegal modifier `%s' for local class declaration" :
3426 "Illegal modifier `%s' for class declaration"),
3427 flags, uaaf|sca|icaf);
3432 make_nested_class_name (cpc_list)
3440 make_nested_class_name (TREE_CHAIN (cpc_list));
3442 /* Pick the qualified name when dealing with the first upmost
3444 name = (TREE_CHAIN (cpc_list) ?
3445 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3446 obstack_grow (&temporary_obstack,
3447 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3448 /* Why is NO_DOLLAR_IN_LABEL defined? */
3450 #ifdef NO_DOLLAR_IN_LABEL
3451 fatal ("make_nested_class_name: Can't use '$' as a separator "
3452 "for inner classes");
3455 obstack_1grow (&temporary_obstack, '$');
3458 /* Can't redefine a class already defined in an earlier scope. */
3461 check_inner_class_redefinition (raw_name, cl)
3466 for (scope_list = GET_CPC_LIST (); scope_list;
3467 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3468 if (raw_name == GET_CPC_UN_NODE (scope_list))
3471 (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",
3472 IDENTIFIER_POINTER (raw_name));
3479 find_as_inner_class (enclosing, name, cl)
3480 tree enclosing, name, cl;
3482 tree qual, to_return;
3486 name = TYPE_NAME (name);
3488 /* First search: within the scope of `enclosing', search for name */
3489 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3490 qual = EXPR_WFL_QUALIFICATION (cl);
3492 qual = build_tree_list (cl, NULL_TREE);
3494 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3496 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3499 /* We're dealing with a qualified name. Try to resolve thing until
3500 we get something that is an enclosing class. */
3501 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3503 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3505 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3506 qual = TREE_CHAIN (qual))
3508 acc = merge_qualified_name (acc,
3509 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3510 BUILD_PTR_FROM_NAME (ptr, acc);
3511 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3514 /* A NULL qual and a decl means that the search ended
3515 successfully?!? We have to do something then. FIXME */
3520 qual = EXPR_WFL_QUALIFICATION (cl);
3522 /* Otherwise, create a qual for the other part of the resolution. */
3524 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3526 return find_as_inner_class_do (qual, enclosing);
3529 /* We go inside the list of sub classes and try to find a way
3533 find_as_inner_class_do (qual, enclosing)
3534 tree qual, enclosing;
3539 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3541 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3542 tree next_enclosing = NULL_TREE;
3545 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3546 inner_list; inner_list = TREE_CHAIN (inner_list))
3548 if (TREE_VALUE (inner_list) == name_to_match)
3550 next_enclosing = TREE_PURPOSE (inner_list);
3554 enclosing = next_enclosing;
3557 return (!qual && enclosing ? enclosing : NULL_TREE);
3560 /* Reach all inner classes and tie their unqualified name to a
3564 set_nested_class_simple_name_value (outer, set)
3570 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3571 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3572 TREE_PURPOSE (l) : NULL_TREE);
3576 link_nested_class_to_enclosing ()
3578 if (GET_ENCLOSING_CPC ())
3580 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3581 DECL_INNER_CLASS_LIST (enclosing) =
3582 tree_cons (GET_CPC (), GET_CPC_UN (),
3583 DECL_INNER_CLASS_LIST (enclosing));
3584 enclosing = enclosing;
3589 maybe_make_nested_class_name (name)
3592 tree id = NULL_TREE;
3596 make_nested_class_name (GET_CPC_LIST ());
3597 obstack_grow0 (&temporary_obstack,
3598 IDENTIFIER_POINTER (name),
3599 IDENTIFIER_LENGTH (name));
3600 id = get_identifier (obstack_finish (&temporary_obstack));
3602 QUALIFIED_P (id) = 1;
3607 /* If DECL is NULL, create and push a new DECL, record the current
3608 line CL and do other maintenance things. */
3611 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3612 tree decl, raw_name, qualified_name, cl;
3615 decl = push_class (make_class (), qualified_name);
3617 /* Take care of the file and line business */
3618 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3619 /* If we're emiting xrefs, store the line/col number information */
3621 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3623 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3624 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3625 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3626 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3628 PUSH_CPC (decl, raw_name);
3629 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3631 /* Link the declaration to the already seen ones */
3632 TREE_CHAIN (decl) = ctxp->class_list;
3633 ctxp->class_list = decl;
3635 /* Create a new nodes in the global lists */
3636 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
3637 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3639 /* Install a new dependency list element */
3640 create_jdep_list (ctxp);
3642 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3643 IDENTIFIER_POINTER (qualified_name)));
3648 add_superinterfaces (decl, interface_list)
3649 tree decl, interface_list;
3652 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3653 takes care of ensuring that:
3654 - This is an accessible interface type,
3655 - Circularity detection.
3656 parser_add_interface is then called. If present but not defined,
3657 the check operation is delayed until the super interface gets
3659 for (node = interface_list; node; node = TREE_CHAIN (node))
3661 tree current = TREE_PURPOSE (node);
3662 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3663 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3665 if (!parser_check_super_interface (idecl, decl, current))
3666 parser_add_interface (decl, idecl, current);
3669 register_incomplete_type (JDEP_INTERFACE,
3670 current, decl, NULL_TREE);
3674 /* Create an interface in pass1 and return its decl. Return the
3675 interface's decl in pass 2. */
3678 create_interface (flags, id, super)
3682 tree raw_name = EXPR_WFL_NODE (id);
3683 tree q_name = parser_qualified_classname (raw_name);
3684 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3686 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3688 /* Basic checks: scope, redefinition, modifiers */
3689 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3695 /* Suspend the current parsing context if we're parsing an inner
3698 java_parser_context_suspend ();
3700 /* Push a new context for (static) initialized upon declaration fields */
3701 java_parser_context_push_initialized_field ();
3703 /* Interface modifiers check
3704 - public/abstract allowed (already done at that point)
3705 - abstract is obsolete (comes first, it's a warning, or should be)
3706 - Can't use twice the same (checked in the modifier rule) */
3707 if ((flags & ACC_ABSTRACT) && flag_redundant)
3708 parse_warning_context
3709 (MODIFIER_WFL (ABSTRACT_TK),
3710 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
3712 /* Create a new decl if DECL is NULL, otherwise fix it */
3713 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3715 /* Set super info and mark the class a complete */
3716 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3717 object_type_node, ctxp->interface_number);
3718 ctxp->interface_number = 0;
3719 CLASS_COMPLETE_P (decl) = 1;
3720 add_superinterfaces (decl, super);
3725 /* Anonymous class counter. Will be reset to 1 every time a non
3726 anonymous class gets created. */
3727 static int anonymous_class_counter = 1;
3729 /* Patch anonymous class CLASS, by either extending or implementing
3733 patch_anonymous_class (type_decl, class_decl, wfl)
3734 tree type_decl, class_decl, wfl;
3736 tree class = TREE_TYPE (class_decl);
3737 tree type = TREE_TYPE (type_decl);
3738 tree binfo = TYPE_BINFO (class);
3740 /* If it's an interface, implement it */
3741 if (CLASS_INTERFACE (type_decl))
3746 if (parser_check_super_interface (type_decl, class_decl, wfl))
3749 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3750 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3751 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3752 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3753 /* And add the interface */
3754 parser_add_interface (class_decl, type_decl, wfl);
3756 /* Otherwise, it's a type we want to extend */
3759 if (parser_check_super (type_decl, class_decl, wfl))
3761 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3766 create_anonymous_class (location, type_name)
3771 tree super = NULL_TREE, itf = NULL_TREE;
3772 tree id, type_decl, class;
3774 /* The unqualified name of the anonymous class. It's just a number. */
3775 sprintf (buffer, "%d", anonymous_class_counter++);
3776 id = build_wfl_node (get_identifier (buffer));
3777 EXPR_WFL_LINECOL (id) = location;
3779 /* We know about the type to extend/implement. We go ahead */
3780 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3782 /* Create a class which either implements on extends the designated
3783 class. The class bears an innacessible name. */
3784 if (CLASS_INTERFACE (type_decl))
3786 /* It's OK to modify it here. It's been already used and
3787 shouldn't be reused */
3788 ctxp->interface_number = 1;
3789 /* Interfaces should presented as a list of WFLs */
3790 itf = build_tree_list (type_name, NULL_TREE);
3796 class = create_class (ACC_FINAL, id, super, itf);
3798 /* We didn't know anything about the stuff. We register a dependence. */
3800 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3802 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3806 /* Create a class in pass1 and return its decl. Return class
3807 interface's decl in pass 2. */
3810 create_class (flags, id, super, interfaces)
3812 tree id, super, interfaces;
3814 tree raw_name = EXPR_WFL_NODE (id);
3815 tree class_id, decl;
3816 tree super_decl_type;
3818 class_id = parser_qualified_classname (raw_name);
3819 decl = IDENTIFIER_CLASS_VALUE (class_id);
3820 EXPR_WFL_NODE (id) = class_id;
3822 /* Basic check: scope, redefinition, modifiers */
3823 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3829 /* Suspend the current parsing context if we're parsing an inner
3830 class or an anonymous class. */
3832 java_parser_context_suspend ();
3833 /* Push a new context for (static) initialized upon declaration fields */
3834 java_parser_context_push_initialized_field ();
3836 /* Class modifier check:
3837 - Allowed modifier (already done at that point)
3838 - abstract AND final forbidden
3839 - Public classes defined in the correct file */
3840 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3842 (id, "Class `%s' can't be declared both abstract and final",
3843 IDENTIFIER_POINTER (raw_name));
3845 /* Create a new decl if DECL is NULL, otherwise fix it */
3846 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
3848 /* If SUPER exists, use it, otherwise use Object */
3851 /* Can't extend java.lang.Object */
3852 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3854 parse_error_context (id, "Can't extend `java.lang.Object'");
3859 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
3861 else if (TREE_TYPE (decl) != object_type_node)
3862 super_decl_type = object_type_node;
3863 /* We're defining java.lang.Object */
3865 super_decl_type = NULL_TREE;
3867 /* Set super info and mark the class a complete */
3868 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3869 ctxp->interface_number);
3870 ctxp->interface_number = 0;
3871 CLASS_COMPLETE_P (decl) = 1;
3872 add_superinterfaces (decl, interfaces);
3874 /* Add the private this$<n> field, Replicate final locals still in
3875 scope as private final fields mangled like val$<local_name>.
3876 This doesn't not occur for top level (static) inner classes. */
3877 if (PURE_INNER_CLASS_DECL_P (decl))
3878 add_inner_class_fields (decl, current_function_decl);
3880 /* If doing xref, store the location at which the inherited class
3881 (if any) was seen. */
3882 if (flag_emit_xref && super)
3883 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3885 /* Eventually sets the @deprecated tag flag */
3886 CHECK_DEPRECATED (decl);
3888 /* Reset the anonymous class counter when declaring non inner classes */
3889 if (!INNER_CLASS_DECL_P (decl))
3890 anonymous_class_counter = 1;
3895 /* End a class declaration: register the statements used to create
3896 finit$ and <clinit>, pop the current class and resume the prior
3897 parser context if necessary. */
3900 end_class_declaration (resume)
3903 /* If an error occured, context weren't pushed and won't need to be
3904 popped by a resume. */
3905 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3907 java_parser_context_pop_initialized_field ();
3909 if (resume && no_error_occured)
3910 java_parser_context_resume ();
3912 /* We're ending a class declaration, this is a good time to reset
3913 the interface cout. Note that might have been already done in
3914 create_interface, but if at that time an inner class was being
3915 dealt with, the interface count was reset in a context created
3916 for the sake of handling inner classes declaration. */
3917 ctxp->interface_number = 0;
3921 add_inner_class_fields (class_decl, fct_decl)
3925 tree block, marker, f;
3927 f = add_field (TREE_TYPE (class_decl),
3928 build_current_thisn (TREE_TYPE (class_decl)),
3929 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3931 FIELD_THISN (f) = 1;
3936 for (block = GET_CURRENT_BLOCK (fct_decl);
3937 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3940 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3943 tree wfl, init, list;
3945 /* Avoid non final arguments. */
3946 if (!LOCAL_FINAL (decl))
3949 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3950 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3951 wfl = build_wfl_node (name);
3952 init = build_wfl_node (pname);
3953 /* Build an initialization for the field: it will be
3954 initialized by a parameter added to finit$, bearing a
3955 mangled name of the field itself (param$<n>.) The
3956 parameter is provided to finit$ by the constructor
3957 invoking it (hence the constructor will also feature a
3958 hidden parameter, set to the value of the outer context
3959 local at the time the inner class is created.)
3961 Note: we take into account all possible locals that can
3962 be accessed by the inner class. It's actually not trivial
3963 to minimize these aliases down to the ones really
3964 used. One way to do that would be to expand all regular
3965 methods first, then finit$ to get a picture of what's
3966 used. It works with the exception that we would have to
3967 go back on all constructor invoked in regular methods to
3968 have their invokation reworked (to include the right amount
3969 of alias initializer parameters.)
3971 The only real way around, I think, is a first pass to
3972 identify locals really used in the inner class. We leave
3973 the flag FIELD_LOCAL_ALIAS_USED around for that future
3976 On the other hand, it only affect local inner classes,
3977 whose constructors (and finit$ call) will be featuring
3978 unecessary arguments. It's easy for a developper to keep
3979 this number of parameter down by using the `final'
3980 keyword only when necessary. For the time being, we can
3981 issue a warning on unecessary finals. FIXME */
3982 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
3985 /* Register the field. The TREE_LIST holding the part
3986 initialized/initializer will be marked ARG_FINAL_P so
3987 that the created field can be marked
3988 FIELD_LOCAL_ALIAS. */
3989 list = build_tree_list (wfl, init);
3990 ARG_FINAL_P (list) = 1;
3991 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
3995 if (!CPC_INITIALIZER_STMT (ctxp))
3998 /* If we ever registered an alias field, insert and marker to
3999 remeber where the list ends. The second part of the list (the one
4000 featuring initialized fields) so it can be later reversed to
4001 enforce 8.5. The marker will be removed during that operation. */
4002 marker = build_tree_list (NULL_TREE, NULL_TREE);
4003 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
4004 SET_CPC_INITIALIZER_STMT (ctxp, marker);
4007 /* Can't use lookup_field () since we don't want to load the class and
4008 can't set the CLASS_LOADED_P flag */
4011 find_field (class, name)
4016 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4018 if (DECL_NAME (decl) == name)
4024 /* Wrap around lookup_field that doesn't potentially upset the value
4028 lookup_field_wrapper (class, name)
4032 tree decl = NULL_TREE;
4033 java_parser_context_save_global ();
4035 /* Last chance: if we're within the context of an inner class, we
4036 might be trying to access a local variable defined in an outer
4037 context. We try to look for it now. */
4038 if (INNER_CLASS_TYPE_P (class))
4041 MANGLE_OUTER_LOCAL_VARIABLE_NAME (new_name, name);
4042 decl = lookup_field (&type, new_name);
4043 if (decl && decl != error_mark_node)
4044 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4046 if (!decl || decl == error_mark_node)
4049 decl = lookup_field (&type, name);
4052 java_parser_context_restore_global ();
4053 return decl == error_mark_node ? NULL : decl;
4056 /* Find duplicate field within the same class declarations and report
4057 the error. Returns 1 if a duplicated field was found, 0
4061 duplicate_declaration_error_p (new_field_name, new_type, cl)
4062 tree new_field_name, new_type, cl;
4064 /* This might be modified to work with method decl as well */
4065 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4068 char *t1 = xstrdup (purify_type_name
4069 ((TREE_CODE (new_type) == POINTER_TYPE
4070 && TREE_TYPE (new_type) == NULL_TREE) ?
4071 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4072 lang_printable_name (new_type, 1)));
4073 /* The type may not have been completed by the time we report
4075 char *t2 = xstrdup (purify_type_name
4076 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4077 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4078 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4079 lang_printable_name (TREE_TYPE (decl), 1)));
4081 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4082 t1, IDENTIFIER_POINTER (new_field_name),
4083 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4084 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4092 /* Field registration routine. If TYPE doesn't exist, field
4093 declarations are linked to the undefined TYPE dependency list, to
4094 be later resolved in java_complete_class () */
4097 register_fields (flags, type, variable_list)
4099 tree type, variable_list;
4101 tree current, saved_type;
4102 tree class_type = NULL_TREE;
4103 int saved_lineno = lineno;
4105 tree wfl = NULL_TREE;
4108 class_type = TREE_TYPE (GET_CPC ());
4110 if (!class_type || class_type == error_mark_node)
4113 /* If we're adding fields to interfaces, those fields are public,
4115 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4117 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4118 flags, ACC_PUBLIC, "interface field(s)");
4119 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4120 flags, ACC_STATIC, "interface field(s)");
4121 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4122 flags, ACC_FINAL, "interface field(s)");
4123 check_modifiers ("Illegal interface member modifier `%s'", flags,
4124 INTERFACE_FIELD_MODIFIERS);
4125 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4128 /* Obtain a suitable type for resolution, if necessary */
4129 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4131 /* If TYPE is fully resolved and we don't have a reference, make one */
4132 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4134 for (current = variable_list, saved_type = type; current;
4135 current = TREE_CHAIN (current), type = saved_type)
4139 tree cl = TREE_PURPOSE (current);
4140 tree init = TREE_VALUE (current);
4141 tree current_name = EXPR_WFL_NODE (cl);
4143 /* Can't declare non-final static fields in inner classes */
4144 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4145 && !(flags & ACC_FINAL))
4147 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4148 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4149 lang_printable_name (class_type, 0));
4151 /* Process NAME, as it may specify extra dimension(s) for it */
4152 type = build_array_from_name (type, wfl, current_name, ¤t_name);
4154 /* Type adjustment. We may have just readjusted TYPE because
4155 the variable specified more dimensions. Make sure we have
4156 a reference if we can and don't have one already. Also
4157 change the name if we have an init. */
4158 if (type != saved_type)
4160 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4162 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4165 real_type = GET_REAL_TYPE (type);
4166 /* Check for redeclarations */
4167 if (duplicate_declaration_error_p (current_name, real_type, cl))
4170 /* Set lineno to the line the field was found and create a
4171 declaration for it. Eventually sets the @deprecated tag flag. */
4173 lineno = EXPR_WFL_LINECOL (cl);
4175 lineno = EXPR_WFL_LINENO (cl);
4176 field_decl = add_field (class_type, current_name, real_type, flags);
4177 CHECK_DEPRECATED (field_decl);
4179 /* If the couple initializer/initialized is marked ARG_FINAL_P, we
4180 mark the created field FIELD_LOCAL_ALIAS, so that we can
4181 hide parameters to this inner class finit$ and constructors. */
4182 if (ARG_FINAL_P (current))
4183 FIELD_LOCAL_ALIAS (field_decl) = 1;
4185 /* Check if we must chain. */
4187 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4189 /* If we have an initialization value tied to the field */
4192 /* The field is declared static */
4193 if (flags & ACC_STATIC)
4195 /* We include the field and its initialization part into
4196 a list used to generate <clinit>. After <clinit> is
4197 walked, field initializations will be processed and
4198 fields initialized with known constants will be taken
4199 out of <clinit> and have their DECL_INITIAL set
4201 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4202 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4203 if (TREE_OPERAND (init, 1)
4204 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4205 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4207 /* A non-static field declared with an immediate initialization is
4208 to be initialized in <init>, if any. This field is remembered
4209 to be processed at the time of the generation of <init>. */
4212 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4213 SET_CPC_INITIALIZER_STMT (ctxp, init);
4215 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4216 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4219 lineno = saved_lineno;
4222 /* Generate finit$, using the list of initialized fields to populate
4223 its body. finit$'s parameter(s) list is adjusted to include the
4224 one(s) used to initialized the field(s) caching outer context
4228 generate_finit (class_type)
4232 tree list = TYPE_FINIT_STMT_LIST (class_type);
4233 tree mdecl, current, parms;
4235 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4236 class_type, NULL_TREE,
4238 CRAFTED_PARAM_LIST_FIXUP (parms);
4239 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4240 finit_identifier_node, parms);
4241 fix_method_argument_names (parms, mdecl);
4242 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4244 DECL_FUNCTION_NAP (mdecl) = count;
4245 start_artificial_method_body (mdecl);
4247 for (current = list; current; current = TREE_CHAIN (current))
4248 java_method_add_stmt (mdecl,
4249 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4251 end_artificial_method_body (mdecl);
4256 add_instance_initializer (mdecl)
4260 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4261 tree compound = NULL_TREE;
4265 for (current = stmt_list; current; current = TREE_CHAIN (current))
4266 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4268 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4269 NULL_TREE, compound));
4273 /* Shared accros method_declarator and method_header to remember the
4274 patch stage that was reached during the declaration of the method.
4275 A method DECL is built differently is there is no patch
4276 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4277 pending on the currently defined method. */
4279 static int patch_stage;
4281 /* Check the method declaration and add the method to its current
4282 class. If the argument list is known to contain incomplete types,
4283 the method is partially added and the registration will be resume
4284 once the method arguments resolved. If TYPE is NULL, we're dealing
4285 with a constructor. */
4288 method_header (flags, type, mdecl, throws)
4290 tree type, mdecl, throws;
4292 tree type_wfl = NULL_TREE;
4293 tree meth_name = NULL_TREE;
4294 tree current, orig_arg, this_class = NULL;
4297 int constructor_ok = 0, must_chain;
4300 if (mdecl == error_mark_node)
4301 return error_mark_node;
4302 meth = TREE_VALUE (mdecl);
4303 id = TREE_PURPOSE (mdecl);
4305 check_modifiers_consistency (flags);
4308 this_class = TREE_TYPE (GET_CPC ());
4310 if (!this_class || this_class == error_mark_node)
4313 /* There are some forbidden modifiers for an abstract method and its
4314 class must be abstract as well. */
4315 if (type && (flags & ACC_ABSTRACT))
4317 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4318 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4319 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4320 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4321 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
4322 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4323 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4325 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4326 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4327 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4330 /* Things to be checked when declaring a constructor */
4333 int ec = java_error_count;
4334 /* 8.6: Constructor declarations: we might be trying to define a
4335 method without specifying a return type. */
4336 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4338 (id, "Invalid method declaration, return type required");
4339 /* 8.6.3: Constructor modifiers */
4342 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4343 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4344 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4345 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4346 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4348 /* If we found error here, we don't consider it's OK to tread
4349 the method definition as a constructor, for the rest of this
4351 if (ec == java_error_count)
4355 /* Method declared within the scope of an interface are implicitly
4356 abstract and public. Conflicts with other erroneously provided
4357 modifiers are checked right after. */
4359 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4361 /* If FLAGS isn't set because of a modifier, turn the
4362 corresponding modifier WFL to NULL so we issue a warning on
4363 the obsolete use of the modifier */
4364 if (!(flags & ACC_PUBLIC))
4365 MODIFIER_WFL (PUBLIC_TK) = NULL;
4366 if (!(flags & ACC_ABSTRACT))
4367 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4368 flags |= ACC_PUBLIC;
4369 flags |= ACC_ABSTRACT;
4372 /* Inner class can't declare static methods */
4373 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4376 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4377 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4378 lang_printable_name (this_class, 0));
4381 /* Modifiers context reset moved up, so abstract method declaration
4382 modifiers can be later checked. */
4384 /* Set constructor returned type to void and method name to <init>,
4385 unless we found an error identifier the constructor (in which
4386 case we retain the original name) */
4389 type = void_type_node;
4391 meth_name = init_identifier_node;
4394 meth_name = EXPR_WFL_NODE (id);
4396 /* Do the returned type resolution and registration if necessary */
4397 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4400 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4401 EXPR_WFL_NODE (id) = meth_name;
4402 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4406 patch_stage = JDEP_METHOD_RETURN;
4407 register_incomplete_type (patch_stage, type_wfl, id, type);
4408 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4411 TREE_TYPE (meth) = type;
4413 saved_lineno = lineno;
4414 /* When defining an abstract or interface method, the curly
4415 bracket at level 1 doesn't exist because there is no function
4417 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4418 EXPR_WFL_LINENO (id));
4420 /* Remember the original argument list */
4421 orig_arg = TYPE_ARG_TYPES (meth);
4423 if (patch_stage) /* includes ret type and/or all args */
4426 meth = add_method_1 (this_class, flags, meth_name, meth);
4427 /* Patch for the return type */
4428 if (patch_stage == JDEP_METHOD_RETURN)
4430 jdep = CLASSD_LAST (ctxp->classd_list);
4431 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4433 /* This is the stop JDEP. METH allows the function's signature
4435 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4438 meth = add_method (this_class, flags, meth_name,
4439 build_java_signature (meth));
4441 /* Remember final parameters */
4442 MARK_FINAL_PARMS (meth, orig_arg);
4444 /* Fix the method argument list so we have the argument name
4446 fix_method_argument_names (orig_arg, meth);
4448 /* Register the parameter number and re-install the current line
4450 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4451 lineno = saved_lineno;
4453 /* Register exception specified by the `throws' keyword for
4454 resolution and set the method decl appropriate field to the list.
4455 Note: the grammar ensures that what we get here are class
4459 throws = nreverse (throws);
4460 for (current = throws; current; current = TREE_CHAIN (current))
4462 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4463 NULL_TREE, NULL_TREE);
4464 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4465 &TREE_VALUE (current);
4467 DECL_FUNCTION_THROWS (meth) = throws;
4470 /* We set the DECL_NAME to ID so we can track the location where
4471 the function was declared. This allow us to report
4472 redefinition error accurately. When method are verified,
4473 DECL_NAME is reinstalled properly (using the content of the
4474 WFL node ID) (see check_method_redefinition). We don't do that
4475 when Object is being defined. Constructor <init> names will be
4476 reinstalled the same way. */
4477 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4478 DECL_NAME (meth) = id;
4480 /* Set the flag if we correctly processed a constructor */
4483 DECL_CONSTRUCTOR_P (meth) = 1;
4484 /* Compute and store the number of artificial parameters declared
4485 for this constructor */
4486 for (count = 0, current = TYPE_FIELDS (this_class); current;
4487 current = TREE_CHAIN (current))
4488 if (FIELD_LOCAL_ALIAS (current))
4490 DECL_FUNCTION_NAP (meth) = count;
4493 /* Eventually set the @deprecated tag flag */
4494 CHECK_DEPRECATED (meth);
4496 /* If doing xref, store column and line number information instead
4497 of the line number only. */
4499 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4505 fix_method_argument_names (orig_arg, meth)
4506 tree orig_arg, meth;
4508 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4509 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4511 TREE_PURPOSE (arg) = this_identifier_node;
4512 arg = TREE_CHAIN (arg);
4514 while (orig_arg != end_params_node)
4516 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4517 orig_arg = TREE_CHAIN (orig_arg);
4518 arg = TREE_CHAIN (arg);
4522 /* Complete the method declaration with METHOD_BODY. */
4525 finish_method_declaration (method_body)
4530 if (!current_function_decl)
4533 flags = get_access_flags_from_decl (current_function_decl);
4535 /* 8.4.5 Method Body */
4536 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4538 tree wfl = DECL_NAME (current_function_decl);
4539 parse_error_context (wfl,
4540 "%s method `%s' can't have a body defined",
4541 (METHOD_NATIVE (current_function_decl) ?
4542 "Native" : "Abstract"),
4543 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4544 method_body = NULL_TREE;
4546 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4548 tree wfl = DECL_NAME (current_function_decl);
4551 "Non native and non abstract method `%s' must have a body defined",
4552 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4553 method_body = NULL_TREE;
4556 if (flag_emit_class_files && method_body
4557 && TREE_CODE (method_body) == NOP_EXPR
4558 && TREE_TYPE (current_function_decl)
4559 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4560 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4562 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4563 maybe_absorb_scoping_blocks ();
4564 /* Exit function's body */
4566 /* Merge last line of the function with first line, directly in the
4567 function decl. It will be used to emit correct debug info. */
4568 if (!flag_emit_xref)
4569 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4571 /* Since function's argument's list are shared, reset the
4572 ARG_FINAL_P parameter that might have been set on some of this
4573 function parameters. */
4574 UNMARK_FINAL_PARMS (current_function_decl);
4576 /* So we don't have an irrelevant function declaration context for
4577 the next static block we'll see. */
4578 current_function_decl = NULL_TREE;
4581 /* Build a an error message for constructor circularity errors. */
4584 constructor_circularity_msg (from, to)
4587 static char string [4096];
4588 char *t = xstrdup (lang_printable_name (from, 0));
4589 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4594 /* Verify a circular call to METH. Return 1 if an error is found, 0
4598 verify_constructor_circularity (meth, current)
4601 static tree list = NULL_TREE;
4602 static int initialized_p;
4605 /* If we haven't already registered LIST with the garbage collector,
4609 ggc_add_tree_root (&list, 1);
4613 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4615 if (TREE_VALUE (c) == meth)
4621 list = nreverse (list);
4622 for (liste = list; liste; liste = TREE_CHAIN (liste))
4625 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4626 constructor_circularity_msg
4627 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4631 t = xstrdup (lang_printable_name (meth, 0));
4632 parse_error_context (TREE_PURPOSE (c),
4633 "%s: recursive invocation of constructor `%s'",
4634 constructor_circularity_msg (current, meth), t);
4640 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4642 list = tree_cons (c, current, list);
4643 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4645 list = TREE_CHAIN (list);
4650 /* Check modifiers that can be declared but exclusively */
4653 check_modifiers_consistency (flags)
4657 tree cl = NULL_TREE;
4659 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4660 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4661 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4664 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4668 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4669 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4671 parse_error_context (cl,
4672 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
4675 /* Check the methode header METH for abstract specifics features */
4678 check_abstract_method_header (meth)
4681 int flags = get_access_flags_from_decl (meth);
4682 /* DECL_NAME might still be a WFL node */
4683 tree name = GET_METHOD_NAME (meth);
4685 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4686 ACC_ABSTRACT, "abstract method",
4687 IDENTIFIER_POINTER (name));
4688 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4689 ACC_PUBLIC, "abstract method",
4690 IDENTIFIER_POINTER (name));
4692 check_modifiers ("Illegal modifier `%s' for interface method",
4693 flags, INTERFACE_METHOD_MODIFIERS);
4696 /* Create a FUNCTION_TYPE node and start augmenting it with the
4697 declared function arguments. Arguments type that can't be resolved
4698 are left as they are, but the returned node is marked as containing
4699 incomplete types. */
4702 method_declarator (id, list)
4705 tree arg_types = NULL_TREE, current, node;
4706 tree meth = make_node (FUNCTION_TYPE);
4709 patch_stage = JDEP_NO_PATCH;
4711 if (GET_CPC () == error_mark_node)
4712 return error_mark_node;
4714 /* If we're dealing with an inner class constructor, we hide the
4715 this$<n> decl in the name field of its parameter declaration. We
4716 also might have to hide the outer context local alias
4717 initializers. Not done when the class is a toplevel class. */
4718 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4719 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4721 tree aliases_list, type, thisn;
4722 /* First the aliases, linked to the regular parameters */
4724 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4725 TREE_TYPE (GET_CPC ()),
4727 list = chainon (nreverse (aliases_list), list);
4730 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4731 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4732 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4736 for (current = list; current; current = TREE_CHAIN (current))
4739 tree wfl_name = TREE_PURPOSE (current);
4740 tree type = TREE_VALUE (current);
4741 tree name = EXPR_WFL_NODE (wfl_name);
4742 tree already, arg_node;
4743 tree type_wfl = NULL_TREE;
4746 /* Obtain a suitable type for resolution, if necessary */
4747 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4749 /* Process NAME, as it may specify extra dimension(s) for it */
4750 type = build_array_from_name (type, type_wfl, name, &name);
4751 EXPR_WFL_NODE (wfl_name) = name;
4753 real_type = GET_REAL_TYPE (type);
4754 if (TREE_CODE (real_type) == RECORD_TYPE)
4756 real_type = promote_type (real_type);
4757 if (TREE_CODE (type) == TREE_LIST)
4758 TREE_PURPOSE (type) = real_type;
4761 /* Check redefinition */
4762 for (already = arg_types; already; already = TREE_CHAIN (already))
4763 if (TREE_PURPOSE (already) == name)
4766 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4767 IDENTIFIER_POINTER (name),
4768 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4772 /* If we've an incomplete argument type, we know there is a location
4773 to patch when the type get resolved, later. */
4777 patch_stage = JDEP_METHOD;
4778 type = register_incomplete_type (patch_stage,
4779 type_wfl, wfl_name, type);
4780 jdep = CLASSD_LAST (ctxp->classd_list);
4781 JDEP_MISC (jdep) = id;
4784 /* The argument node: a name and a (possibly) incomplete type. */
4785 arg_node = build_tree_list (name, real_type);
4786 /* Remeber arguments declared final. */
4787 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4790 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4791 TREE_CHAIN (arg_node) = arg_types;
4792 arg_types = arg_node;
4794 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
4795 node = build_tree_list (id, meth);
4800 unresolved_type_p (wfl, returned)
4805 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4809 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4810 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4811 *returned = TREE_TYPE (decl);
4812 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4813 *returned = TREE_TYPE (GET_CPC ());
4815 *returned = NULL_TREE;
4824 /* From NAME, build a qualified identifier node using the
4825 qualification from the current package definition. */
4828 parser_qualified_classname (name)
4831 tree nested_class_name;
4833 if ((nested_class_name = maybe_make_nested_class_name (name)))
4834 return nested_class_name;
4837 return merge_qualified_name (ctxp->package, name);
4842 /* Called once the type a interface extends is resolved. Returns 0 if
4843 everything is OK. */
4846 parser_check_super_interface (super_decl, this_decl, this_wfl)
4847 tree super_decl, this_decl, this_wfl;
4849 tree super_type = TREE_TYPE (super_decl);
4851 /* Has to be an interface */
4852 if (!CLASS_INTERFACE (super_decl))
4855 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4856 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4857 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4858 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4859 "interface" : "class"),
4860 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4864 /* Check scope: same package OK, other package: OK if public */
4865 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4868 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4869 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4870 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4874 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4875 0 if everthing is OK. */
4878 parser_check_super (super_decl, this_decl, wfl)
4879 tree super_decl, this_decl, wfl;
4881 tree super_type = TREE_TYPE (super_decl);
4883 /* SUPER should be a CLASS (neither an array nor an interface) */
4884 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4887 (wfl, "Class `%s' can't subclass %s `%s'",
4888 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4889 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4890 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4894 if (CLASS_FINAL (TYPE_NAME (super_type)))
4896 parse_error_context (wfl, "Can't subclass final classes: %s",
4897 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4901 /* Check scope: same package OK, other package: OK if public */
4902 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4905 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4906 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4907 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4911 /* Create a new dependency list and link it (in a LIFO manner) to the
4912 CTXP list of type dependency list. */
4915 create_jdep_list (ctxp)
4916 struct parser_ctxt *ctxp;
4918 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
4919 new->first = new->last = NULL;
4920 new->next = ctxp->classd_list;
4921 ctxp->classd_list = new;
4925 reverse_jdep_list (ctxp)
4926 struct parser_ctxt *ctxp;
4928 register jdeplist *prev = NULL, *current, *next;
4929 for (current = ctxp->classd_list; current; current = next)
4931 next = current->next;
4932 current->next = prev;
4938 /* Create a fake pointer based on the ID stored in
4939 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4940 registered again. */
4943 obtain_incomplete_type (type_name)
4948 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4949 name = EXPR_WFL_NODE (type_name);
4950 else if (INCOMPLETE_TYPE_P (type_name))
4951 name = TYPE_NAME (type_name);
4953 fatal ("invalid type name - obtain_incomplete_type");
4955 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
4956 if (TYPE_NAME (ptr) == name)
4961 BUILD_PTR_FROM_NAME (ptr, name);
4963 TREE_CHAIN (ptr) = ctxp->incomplete_class;
4964 ctxp->incomplete_class = ptr;
4970 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
4971 non NULL instead of computing a new fake type based on WFL. The new
4972 dependency is inserted in the current type dependency list, in FIFO
4976 register_incomplete_type (kind, wfl, decl, ptr)
4978 tree wfl, decl, ptr;
4980 jdep *new = (jdep *)xmalloc (sizeof (jdep));
4982 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
4983 ptr = obtain_incomplete_type (wfl);
4985 JDEP_KIND (new) = kind;
4986 JDEP_DECL (new) = decl;
4987 JDEP_SOLV (new) = ptr;
4988 JDEP_WFL (new) = wfl;
4989 JDEP_CHAIN (new) = NULL;
4990 JDEP_MISC (new) = NULL_TREE;
4991 /* For some dependencies, set the enclosing class of the current
4992 class to be the enclosing context */
4993 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS)
4994 && GET_ENCLOSING_CPC ())
4995 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
4997 JDEP_ENCLOSING (new) = GET_CPC ();
4998 JDEP_GET_PATCH (new) = (tree *)NULL;
5000 JDEP_INSERT (ctxp->classd_list, new);
5006 java_check_circular_reference ()
5009 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5011 tree type = TREE_TYPE (current);
5012 if (CLASS_INTERFACE (current))
5014 /* Check all interfaces this class extends */
5015 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
5020 n = TREE_VEC_LENGTH (basetype_vec);
5021 for (i = 0; i < n; i++)
5023 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
5024 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
5025 && interface_of_p (type, BINFO_TYPE (vec_elt)))
5026 parse_error_context (lookup_cl (current),
5027 "Cyclic interface inheritance");
5031 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5032 parse_error_context (lookup_cl (current),
5033 "Cyclic class inheritance%s",
5034 (cyclic_inheritance_report ?
5035 cyclic_inheritance_report : ""));
5039 /* Augment the parameter list PARM with parameters crafted to
5040 initialize outer context locals aliases. Through ARTIFICIAL, a
5041 count is kept of the number of crafted parameters. MODE governs
5042 what eventually gets created: something suitable for a function
5043 creation or a function invocation, either the constructor or
5047 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5049 tree class_type, parm;
5053 tree additional_parms = NULL_TREE;
5055 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5056 if (FIELD_LOCAL_ALIAS (field))
5058 const char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5059 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5064 case AIPL_FUNCTION_DECLARATION:
5065 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5067 purpose = build_wfl_node (mangled_id);
5068 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5069 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5071 value = TREE_TYPE (field);
5074 case AIPL_FUNCTION_CREATION:
5075 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (purpose,
5077 value = TREE_TYPE (field);
5080 case AIPL_FUNCTION_FINIT_INVOCATION:
5081 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (mangled_id,
5083 /* Now, this is wrong. purpose should always be the NAME
5084 of something and value its matching value (decl, type,
5085 etc...) FIXME -- but there is a lot to fix. */
5087 /* When invoked for this kind of operation, we already
5088 know whether a field is used or not. */
5089 purpose = TREE_TYPE (field);
5090 value = build_wfl_node (mangled_id);
5093 case AIPL_FUNCTION_CTOR_INVOCATION:
5094 /* There are two case: the constructor invokation happends
5095 outside the local inner, in which case, locales from the outer
5096 context are directly used.
5098 Otherwise, we fold to using the alias directly. */
5099 if (class_type == current_class)
5103 name = get_identifier (&buffer[4]);
5104 value = IDENTIFIER_LOCAL_VALUE (name);
5108 additional_parms = tree_cons (purpose, value, additional_parms);
5112 if (additional_parms)
5114 if (ANONYMOUS_CLASS_P (class_type)
5115 && mode == AIPL_FUNCTION_CTOR_INVOCATION)
5116 additional_parms = nreverse (additional_parms);
5117 parm = chainon (additional_parms, parm);
5123 /* Craft a constructor for CLASS_DECL -- what we should do when none
5124 where found. ARGS is non NULL when a special signature must be
5125 enforced. This is the case for anonymous classes. */
5128 craft_constructor (class_decl, args)
5129 tree class_decl, args;
5131 tree class_type = TREE_TYPE (class_decl);
5132 tree parm = NULL_TREE;
5133 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5135 int i = 0, artificial = 0;
5136 tree decl, ctor_name;
5139 /* The constructor name is <init> unless we're dealing with an
5140 anonymous class, in which case the name will be fixed after having
5142 if (ANONYMOUS_CLASS_P (class_type))
5143 ctor_name = DECL_NAME (class_decl);
5145 ctor_name = init_identifier_node;
5147 /* If we're dealing with an inner class constructor, we hide the
5148 this$<n> decl in the name field of its parameter declaration. */
5149 if (PURE_INNER_CLASS_TYPE_P (class_type))
5151 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5152 parm = tree_cons (build_current_thisn (class_type),
5153 build_pointer_type (type), parm);
5155 /* Some more arguments to be hidden here. The values of the local
5156 variables of the outer context that the inner class needs to see. */
5157 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5162 /* Then if there are any args to be enforced, enforce them now */
5163 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5165 sprintf (buffer, "parm%d", i++);
5166 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5169 CRAFTED_PARAM_LIST_FIXUP (parm);
5170 decl = create_artificial_method (class_type, flags, void_type_node,
5172 fix_method_argument_names (parm, decl);
5173 /* Now, mark the artificial parameters. */
5174 DECL_FUNCTION_NAP (decl) = artificial;
5175 DECL_CONSTRUCTOR_P (decl) = 1;
5179 /* Fix the constructors. This will be called right after circular
5180 references have been checked. It is necessary to fix constructors
5181 early even if no code generation will take place for that class:
5182 some generated constructor might be required by the class whose
5183 compilation triggered this one to be simply loaded. */
5186 java_fix_constructors ()
5190 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5192 tree class_type = TREE_TYPE (current);
5196 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5199 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5201 if (DECL_CONSTRUCTOR_P (decl))
5203 fix_constructors (decl);
5208 /* Anonymous class constructor can't be generated that early. */
5209 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5210 craft_constructor (current, NULL_TREE);
5214 /* safe_layout_class just makes sure that we can load a class without
5215 disrupting the current_class, input_file, lineno, etc, information
5216 about the class processed currently. */
5219 safe_layout_class (class)
5222 tree save_current_class = current_class;
5223 const char *save_input_filename = input_filename;
5224 int save_lineno = lineno;
5226 layout_class (class);
5228 current_class = save_current_class;
5229 input_filename = save_input_filename;
5230 lineno = save_lineno;
5231 CLASS_LOADED_P (class) = 1;
5235 jdep_resolve_class (dep)
5240 if (JDEP_RESOLVED_P (dep))
5241 decl = JDEP_RESOLVED_DECL (dep);
5244 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5245 JDEP_DECL (dep), JDEP_WFL (dep));
5246 JDEP_RESOLVED (dep, decl);
5250 complete_class_report_errors (dep);
5251 else if (PURE_INNER_CLASS_DECL_P (decl))
5252 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5256 /* Complete unsatisfied class declaration and their dependencies */
5259 java_complete_class ()
5266 /* Process imports */
5269 /* Rever things so we have the right order */
5270 ctxp->class_list = nreverse (ctxp->class_list);
5271 ctxp->classd_list = reverse_jdep_list (ctxp);
5273 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5275 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5278 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5281 if (!(decl = jdep_resolve_class (dep)))
5284 /* Now it's time to patch */
5285 switch (JDEP_KIND (dep))
5288 /* Simply patch super */
5289 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5291 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5292 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5297 /* We do part of the job done in add_field */
5298 tree field_decl = JDEP_DECL (dep);
5299 tree field_type = TREE_TYPE (decl);
5300 if (TREE_CODE (field_type) == RECORD_TYPE)
5301 field_type = promote_type (field_type);
5302 TREE_TYPE (field_decl) = field_type;
5303 DECL_ALIGN (field_decl) = 0;
5304 DECL_USER_ALIGN (field_decl) = 0;
5305 layout_decl (field_decl, 0);
5306 SOURCE_FRONTEND_DEBUG
5307 (("Completed field/var decl `%s' with `%s'",
5308 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5309 IDENTIFIER_POINTER (DECL_NAME (decl))));
5312 case JDEP_METHOD: /* We start patching a method */
5313 case JDEP_METHOD_RETURN:
5319 type = TREE_TYPE(decl);
5320 if (TREE_CODE (type) == RECORD_TYPE)
5321 type = promote_type (type);
5322 JDEP_APPLY_PATCH (dep, type);
5323 SOURCE_FRONTEND_DEBUG
5324 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5325 "Completing fct `%s' with ret type `%s'":
5326 "Completing arg `%s' with type `%s'"),
5327 IDENTIFIER_POINTER (EXPR_WFL_NODE
5328 (JDEP_DECL_WFL (dep))),
5329 IDENTIFIER_POINTER (DECL_NAME (decl))));
5333 dep = JDEP_CHAIN (dep);
5334 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5337 decl = jdep_resolve_class (dep);
5341 tree mdecl = JDEP_DECL (dep), signature;
5342 /* Recompute and reset the signature, check first that
5343 all types are now defined. If they're not,
5344 dont build the signature. */
5345 if (check_method_types_complete (mdecl))
5347 signature = build_java_signature (TREE_TYPE (mdecl));
5348 set_java_signature (TREE_TYPE (mdecl), signature);
5355 case JDEP_INTERFACE:
5356 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5359 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5364 type = TREE_TYPE(decl);
5365 if (TREE_CODE (type) == RECORD_TYPE)
5366 type = promote_type (type);
5367 JDEP_APPLY_PATCH (dep, type);
5371 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5372 SOURCE_FRONTEND_DEBUG
5373 (("Completing a random type dependency on a '%s' node",
5374 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5377 case JDEP_EXCEPTION:
5378 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5379 SOURCE_FRONTEND_DEBUG
5380 (("Completing `%s' `throws' argument node",
5381 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5384 case JDEP_ANONYMOUS:
5385 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5389 fatal ("Can't handle patch code %d - java_complete_class",
5397 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5401 resolve_class (enclosing, class_type, decl, cl)
5402 tree enclosing, class_type, decl, cl;
5404 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5405 const char *base = name;
5406 tree resolved_type = TREE_TYPE (class_type);
5407 tree resolved_type_decl;
5409 if (resolved_type != NULL_TREE)
5411 tree resolved_type_decl = TYPE_NAME (resolved_type);
5412 if (resolved_type_decl == NULL_TREE
5413 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5415 resolved_type_decl = build_decl (TYPE_DECL,
5416 TYPE_NAME (class_type),
5419 return resolved_type_decl;
5422 /* 1- Check to see if we have an array. If true, find what we really
5424 while (name[0] == '[')
5428 TYPE_NAME (class_type) = get_identifier (name);
5429 WFL_STRIP_BRACKET (cl, cl);
5432 /* 2- Resolve the bare type */
5433 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5436 resolved_type = TREE_TYPE (resolved_type_decl);
5438 /* 3- If we have and array, reconstruct the array down to its nesting */
5441 while (base != name)
5443 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5444 resolved_type = promote_type (resolved_type);
5445 resolved_type = build_java_array_type (resolved_type, -1);
5446 CLASS_LOADED_P (resolved_type) = 1;
5449 /* Build a fake decl for this, since this is what is expected to
5451 resolved_type_decl =
5452 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5453 /* Figure how those two things are important for error report. FIXME */
5454 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5455 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
5456 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
5458 TREE_TYPE (class_type) = resolved_type;
5459 return resolved_type_decl;
5462 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5463 are used to report error messages. */
5466 do_resolve_class (enclosing, class_type, decl, cl)
5467 tree enclosing, class_type, decl, cl;
5469 tree new_class_decl;
5471 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
5472 it is changed by find_in_imports{_on_demand} and (but it doesn't
5473 really matter) qualify_and_find */
5475 /* 0- Search in the current class as an inner class */
5477 /* Maybe some code here should be added to load the class or
5478 something, at least if the class isn't an inner class and ended
5479 being loaded from class file. FIXME. */
5484 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5485 return new_class_decl;
5487 /* Explore enclosing contexts. */
5488 while (INNER_CLASS_DECL_P (enclosing))
5490 enclosing = DECL_CONTEXT (enclosing);
5491 if ((new_class_decl = find_as_inner_class (enclosing,
5493 return new_class_decl;
5496 /* Now go to the upper classes, bail out if necessary. */
5497 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5498 if (!enclosing || enclosing == object_type_node)
5501 if (TREE_CODE (enclosing) == RECORD_TYPE)
5503 enclosing = TYPE_NAME (enclosing);
5507 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
5508 BUILD_PTR_FROM_NAME (name, enclosing);
5511 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5514 /* 1- Check for the type in single imports. This will change
5515 TYPE_NAME() if something relevant is found */
5516 find_in_imports (class_type);
5518 /* 2- And check for the type in the current compilation unit */
5519 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5521 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5522 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5523 load_class (TYPE_NAME (class_type), 0);
5524 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5527 /* 3- Search according to the current package definition */
5528 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5530 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5531 TYPE_NAME (class_type))))
5532 return new_class_decl;
5535 /* 4- Check the import on demands. Don't allow bar.baz to be
5536 imported from foo.* */
5537 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5538 if (find_in_imports_on_demand (class_type))
5541 /* If found in find_in_imports_on_demant, the type has already been
5543 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5544 return new_class_decl;
5546 /* 5- Try with a name qualified with the package name we've seen so far */
5547 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5551 /* If there is a current package (ctxp->package), it's the first
5552 element of package_list and we can skip it. */
5553 for (package = (ctxp->package ?
5554 TREE_CHAIN (package_list) : package_list);
5555 package; package = TREE_CHAIN (package))
5556 if ((new_class_decl = qualify_and_find (class_type,
5557 TREE_PURPOSE (package),
5558 TYPE_NAME (class_type))))
5559 return new_class_decl;
5562 /* 5- Check an other compilation unit that bears the name of type */
5563 load_class (TYPE_NAME (class_type), 0);
5564 if (check_pkg_class_access (TYPE_NAME (class_type),
5565 (cl ? cl : lookup_cl (decl))))
5568 /* 6- Last call for a resolution */
5569 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5573 qualify_and_find (class_type, package, name)
5574 tree class_type, package, name;
5576 tree new_qualified = merge_qualified_name (package, name);
5577 tree new_class_decl;
5579 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5580 load_class (new_qualified, 0);
5581 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5583 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5584 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5585 load_class (new_qualified, 0);
5586 TYPE_NAME (class_type) = new_qualified;
5587 return IDENTIFIER_CLASS_VALUE (new_qualified);
5592 /* Resolve NAME and lay it out (if not done and if not the current
5593 parsed class). Return a decl node. This function is meant to be
5594 called when type resolution is necessary during the walk pass. */
5597 resolve_and_layout (something, cl)
5601 tree decl, decl_type;
5603 /* Don't do that on the current class */
5604 if (something == current_class)
5605 return TYPE_NAME (current_class);
5607 /* Don't do anything for void and other primitive types */
5608 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5611 /* Pointer types can be reall pointer types or fake pointers. When
5612 finding a real pointer, recheck for primitive types */
5613 if (TREE_CODE (something) == POINTER_TYPE)
5615 if (TREE_TYPE (something))
5617 something = TREE_TYPE (something);
5618 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5622 something = TYPE_NAME (something);
5625 /* Don't do anything for arrays of primitive types */
5626 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5627 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5630 /* Something might be a WFL */
5631 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5632 something = EXPR_WFL_NODE (something);
5634 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5635 TYPE_DECL or a real TYPE */
5636 else if (TREE_CODE (something) != IDENTIFIER_NODE)
5637 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5638 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5640 if (!(decl = resolve_no_layout (something, cl)))
5643 /* Resolve and layout if necessary */
5644 decl_type = TREE_TYPE (decl);
5645 layout_class_methods (decl_type);
5647 if (CLASS_FROM_SOURCE_P (decl_type))
5648 java_check_methods (decl);
5649 /* Layout the type if necessary */
5650 if (decl_type != current_class && !CLASS_LOADED_P (decl_type))
5651 safe_layout_class (decl_type);
5656 /* Resolve a class, returns its decl but doesn't perform any
5657 layout. The current parsing context is saved and restored */
5660 resolve_no_layout (name, cl)
5664 BUILD_PTR_FROM_NAME (ptr, name);
5665 java_parser_context_save_global ();
5666 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5667 java_parser_context_restore_global ();
5672 /* Called when reporting errors. Skip leader '[' in a complex array
5673 type description that failed to be resolved. */
5676 purify_type_name (name)
5679 while (*name && *name == '[')
5684 /* The type CURRENT refers to can't be found. We print error messages. */
5687 complete_class_report_errors (dep)
5692 if (!JDEP_WFL (dep))
5695 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
5696 switch (JDEP_KIND (dep))
5700 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
5701 purify_type_name (name),
5702 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5706 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
5707 purify_type_name (name),
5708 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5710 case JDEP_METHOD: /* Covers arguments */
5712 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
5713 purify_type_name (name),
5714 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5715 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5717 case JDEP_METHOD_RETURN: /* Covers return type */
5719 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
5720 purify_type_name (name),
5721 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5723 case JDEP_INTERFACE:
5725 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5726 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5727 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5728 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5732 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
5733 purify_type_name (IDENTIFIER_POINTER
5734 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
5735 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5737 case JDEP_EXCEPTION: /* As specified by `throws' */
5739 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5740 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5743 /* Fix for -Wall. Just break doing nothing. The error will be
5749 /* Return a static string containing the DECL prototype string. If
5750 DECL is a constructor, use the class name instead of the form
5754 get_printable_method_name (decl)
5757 const char *to_return;
5758 tree name = NULL_TREE;
5760 if (DECL_CONSTRUCTOR_P (decl))
5762 name = DECL_NAME (decl);
5763 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
5766 to_return = lang_printable_name (decl, 0);
5767 if (DECL_CONSTRUCTOR_P (decl))
5768 DECL_NAME (decl) = name;
5773 /* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
5774 nevertheless needs to be verfied, 1 otherwise. */
5777 reset_method_name (method)
5780 if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method))
5782 /* NAME is just the plain name when Object is being defined */
5783 if (DECL_CONTEXT (method) != object_type_node)
5784 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
5785 init_identifier_node : GET_METHOD_NAME (method));
5792 /* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
5795 java_get_real_method_name (method_decl)
5798 tree method_name = DECL_NAME (method_decl);
5799 if (DECL_CONSTRUCTOR_P (method_decl))
5800 return init_identifier_node;
5802 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
5803 and still can be a constructor. FIXME */
5805 /* Don't confuse method only bearing the name of their class as
5807 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
5809 && GET_CPC_UN () == EXPR_WFL_NODE (method_name)
5810 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
5811 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
5812 return init_identifier_node;
5814 return EXPR_WFL_NODE (method_name);
5817 /* Track method being redefined inside the same class. As a side
5818 effect, set DECL_NAME to an IDENTIFIER (prior entering this
5819 function it's a FWL, so we can track errors more accurately.) */
5822 check_method_redefinition (class, method)
5826 tree cl = DECL_NAME (method);
5827 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
5828 /* decl name of artificial <clinit> and finit$ doesn't need to be
5829 fixed and checked */
5831 /* Reset the method name before running the check. If it returns 1,
5832 the method doesn't need to be verified with respect to method
5833 redeclaration and we return 0 */
5834 if (reset_method_name (method))
5837 name = DECL_NAME (method);
5838 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5840 if (redef == method)
5842 if (DECL_NAME (redef) == name
5843 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
5846 (cl, "Duplicate %s declaration `%s'",
5847 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5848 get_printable_method_name (redef));
5855 /* Return 1 if check went ok, 0 otherwise. */
5857 check_abstract_method_definitions (do_interface, class_decl, type)
5859 tree class_decl, type;
5861 tree class = TREE_TYPE (class_decl);
5862 tree method, end_type;
5865 end_type = (do_interface ? object_type_node : type);
5866 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5868 tree other_super, other_method, method_sig, method_name;
5870 int end_type_reached = 0;
5872 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5875 /* Now verify that somewhere in between TYPE and CLASS,
5876 abstract method METHOD gets a non abstract definition
5877 that is inherited by CLASS. */
5879 method_sig = build_java_signature (TREE_TYPE (method));
5880 method_name = DECL_NAME (method);
5881 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5882 method_name = EXPR_WFL_NODE (method_name);
5884 other_super = class;
5886 if (other_super == end_type)
5887 end_type_reached = 1;
5890 for (other_method = TYPE_METHODS (other_super); other_method;
5891 other_method = TREE_CHAIN (other_method))
5893 tree s = build_java_signature (TREE_TYPE (other_method));
5894 tree other_name = DECL_NAME (other_method);
5896 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5897 other_name = EXPR_WFL_NODE (other_name);
5898 if (!DECL_CLINIT_P (other_method)
5899 && !DECL_CONSTRUCTOR_P (other_method)
5900 && method_name == other_name
5902 && !METHOD_ABSTRACT (other_method))
5908 other_super = CLASSTYPE_SUPER (other_super);
5909 } while (!end_type_reached);
5911 /* Report that abstract METHOD didn't find an implementation
5912 that CLASS can use. */
5915 char *t = xstrdup (lang_printable_name
5916 (TREE_TYPE (TREE_TYPE (method)), 0));
5917 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5918 tree saved_wfl = NULL_TREE;
5920 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5922 saved_wfl = DECL_NAME (method);
5923 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5927 (lookup_cl (class_decl),
5928 "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",
5929 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5930 t, lang_printable_name (method, 0),
5931 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5932 "interface" : "class"),
5933 IDENTIFIER_POINTER (ccn),
5934 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5935 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
5940 DECL_NAME (method) = saved_wfl;
5944 if (ok && do_interface)
5946 /* Check for implemented interfaces. */
5948 tree vector = TYPE_BINFO_BASETYPES (type);
5949 for (i = 1; ok && vector && i < TREE_VEC_LENGTH (vector); i++)
5951 tree super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5952 ok = check_abstract_method_definitions (1, class_decl, super);
5959 /* Check that CLASS_DECL somehow implements all inherited abstract
5963 java_check_abstract_method_definitions (class_decl)
5966 tree class = TREE_TYPE (class_decl);
5970 if (CLASS_ABSTRACT (class_decl))
5973 /* Check for inherited types */
5976 super = CLASSTYPE_SUPER (super);
5977 check_abstract_method_definitions (0, class_decl, super);
5978 } while (super != object_type_node);
5980 /* Check for implemented interfaces. */
5981 vector = TYPE_BINFO_BASETYPES (class);
5982 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5984 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5985 check_abstract_method_definitions (1, class_decl, super);
5989 /* Check all the types method DECL uses and return 1 if all of them
5990 are now complete, 0 otherwise. This is used to check whether its
5991 safe to build a method signature or not. */
5994 check_method_types_complete (decl)
5997 tree type = TREE_TYPE (decl);
6000 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
6003 args = TYPE_ARG_TYPES (type);
6004 if (TREE_CODE (type) == METHOD_TYPE)
6005 args = TREE_CHAIN (args);
6006 for (; args != end_params_node; args = TREE_CHAIN (args))
6007 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
6013 /* Visible interface to check methods contained in CLASS_DECL */
6016 java_check_methods (class_decl)
6019 if (CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)))
6022 if (CLASS_INTERFACE (class_decl))
6023 java_check_abstract_methods (class_decl);
6025 java_check_regular_methods (class_decl);
6027 CLASS_METHOD_CHECKED_P (TREE_TYPE (class_decl)) = 1;
6030 /* Check all the methods of CLASS_DECL. Methods are first completed
6031 then checked according to regular method existance rules. If no
6032 constructor for CLASS_DECL were encountered, then build its
6036 java_check_regular_methods (class_decl)
6039 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
6041 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
6042 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
6045 /* It is not necessary to check methods defined in java.lang.Object */
6046 if (class == object_type_node)
6049 if (!TYPE_NVIRTUALS (class))
6050 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6052 /* Should take interfaces into account. FIXME */
6053 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
6056 tree method_wfl = DECL_NAME (method);
6059 /* If we previously found something and its name was saved,
6061 if (found && saved_found_wfl)
6063 DECL_NAME (found) = saved_found_wfl;
6064 saved_found_wfl = NULL_TREE;
6067 /* Check for redefinitions */
6068 if (check_method_redefinition (class, method))
6071 /* If we see one constructor a mark so we don't generate the
6072 default one. Also skip other verifications: constructors
6073 can't be inherited hence hiden or overriden */
6074 if (DECL_CONSTRUCTOR_P (method))
6076 saw_constructor = 1;
6080 /* We verify things thrown by the method. They must inherits from
6081 java.lang.Throwable */
6082 for (mthrows = DECL_FUNCTION_THROWS (method);
6083 mthrows; mthrows = TREE_CHAIN (mthrows))
6085 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6087 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6089 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6092 sig = build_java_argument_signature (TREE_TYPE (method));
6093 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6095 /* Inner class can't declare static methods */
6096 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6098 char *t = xstrdup (lang_printable_name (class, 0));
6100 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6101 lang_printable_name (method, 0), t);
6105 /* Nothing overrides or it's a private method. */
6108 if (METHOD_PRIVATE (found))
6114 /* If found wasn't verified, it's DECL_NAME won't be set properly.
6115 We set it temporarily for the sake of the error report. */
6116 saved_found_wfl = DECL_NAME (found);
6117 reset_method_name (found);
6119 /* If `found' is declared in an interface, make sure the
6120 modifier matches. */
6121 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6122 && clinit_identifier_node != DECL_NAME (found)
6123 && !METHOD_PUBLIC (method))
6125 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6126 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6127 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6128 lang_printable_name (method, 0),
6129 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6132 /* Can't override a method with the same name and different return
6134 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6137 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6140 "Method `%s' was defined with return type `%s' in class `%s'",
6141 lang_printable_name (found, 0), t,
6143 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6147 aflags = get_access_flags_from_decl (found);
6148 /* If the method has default, access in an other package, then
6149 issue a warning that the current method doesn't override the
6150 one that was found elsewhere. Do not issue this warning when
6151 the match was found in java.lang.Object. */
6152 if (DECL_CONTEXT (found) != object_type_node
6153 && ((aflags & ACC_VISIBILITY) == 0)
6154 && !class_in_current_package (DECL_CONTEXT (found))
6155 && !DECL_CLINIT_P (found)
6156 && flag_not_overriding)
6158 parse_warning_context
6159 (method_wfl, "Method `%s' in class `%s' does not override the corresponding method in class `%s', which is private to a different package",
6160 lang_printable_name (found, 0),
6161 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6162 IDENTIFIER_POINTER (DECL_NAME
6163 (TYPE_NAME (DECL_CONTEXT (found)))));
6167 /* Can't override final. Can't override static. */
6168 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6170 /* Static *can* override static */
6171 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6175 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6176 (METHOD_FINAL (found) ? "Final" : "Static"),
6177 lang_printable_name (found, 0),
6178 (METHOD_FINAL (found) ? "final" : "static"),
6180 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6184 /* Static method can't override instance method. */
6185 if (METHOD_STATIC (method))
6189 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6190 lang_printable_name (found, 0),
6192 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6196 /* - Overriding/hiding public must be public
6197 - Overriding/hiding protected must be protected or public
6198 - If the overriden or hidden method has default (package)
6199 access, then the overriding or hiding method must not be
6200 private; otherwise, a compile-time error occurs. If
6201 `found' belongs to an interface, things have been already
6203 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6204 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6205 || (METHOD_PROTECTED (found)
6206 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6207 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6208 && METHOD_PRIVATE (method))))
6212 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6213 (METHOD_PUBLIC (method) ? "public" :
6214 (METHOD_PRIVATE (method) ? "private" : "protected")),
6215 IDENTIFIER_POINTER (DECL_NAME
6216 (TYPE_NAME (DECL_CONTEXT (found)))));
6220 /* Overriding methods must have compatible `throws' clauses on checked
6221 exceptions, if any */
6222 check_throws_clauses (method, method_wfl, found);
6224 /* Inheriting multiple methods with the same signature. FIXME */
6227 /* Don't forget eventual pending found and saved_found_wfl. Take
6228 into account that we might have exited because we saw an
6229 artificial method as the last entry. */
6231 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
6232 DECL_NAME (found) = saved_found_wfl;
6234 if (!TYPE_NVIRTUALS (class))
6235 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6237 /* Search for inherited abstract method not yet implemented in this
6239 java_check_abstract_method_definitions (class_decl);
6241 if (!saw_constructor)
6242 fatal ("No constructor found");
6245 /* Return a non zero value if the `throws' clause of METHOD (if any)
6246 is incompatible with the `throws' clause of FOUND (if any). */
6249 check_throws_clauses (method, method_wfl, found)
6250 tree method, method_wfl, found;
6252 tree mthrows, fthrows;
6254 /* Can't check these things with class loaded from bytecode. FIXME */
6255 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6258 for (mthrows = DECL_FUNCTION_THROWS (method);
6259 mthrows; mthrows = TREE_CHAIN (mthrows))
6261 /* We don't verify unchecked expressions */
6262 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6264 /* Checked expression must be compatible */
6265 for (fthrows = DECL_FUNCTION_THROWS (found);
6266 fthrows; fthrows = TREE_CHAIN (fthrows))
6267 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6272 (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'",
6273 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6274 lang_printable_name (found, 0),
6276 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6281 /* Check abstract method of interface INTERFACE */
6284 java_check_abstract_methods (interface_decl)
6285 tree interface_decl;
6288 tree method, basetype_vec, found;
6289 tree interface = TREE_TYPE (interface_decl);
6291 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6293 tree method_wfl = DECL_NAME (method);
6295 /* 2- Check for double definition inside the defining interface */
6296 if (check_method_redefinition (interface, method))
6299 /* 3- Overriding is OK as far as we preserve the return type and
6300 the thrown exceptions (FIXME) */
6301 found = lookup_java_interface_method2 (interface, method);
6305 tree saved_found_wfl = DECL_NAME (found);
6306 reset_method_name (found);
6307 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6310 "Method `%s' was defined with return type `%s' in class `%s'",
6311 lang_printable_name (found, 0), t,
6313 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6315 DECL_NAME (found) = saved_found_wfl;
6320 /* 4- Inherited methods can't differ by their returned types */
6321 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6323 n = TREE_VEC_LENGTH (basetype_vec);
6324 for (i = 0; i < n; i++)
6326 tree sub_interface_method, sub_interface;
6327 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6330 sub_interface = BINFO_TYPE (vec_elt);
6331 for (sub_interface_method = TYPE_METHODS (sub_interface);
6332 sub_interface_method;
6333 sub_interface_method = TREE_CHAIN (sub_interface_method))
6335 found = lookup_java_interface_method2 (interface,
6336 sub_interface_method);
6337 if (found && (found != sub_interface_method))
6339 tree saved_found_wfl = DECL_NAME (found);
6340 reset_method_name (found);
6342 (lookup_cl (sub_interface_method),
6343 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6344 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6345 lang_printable_name (found, 0),
6347 (DECL_NAME (TYPE_NAME
6348 (DECL_CONTEXT (sub_interface_method)))),
6350 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6351 DECL_NAME (found) = saved_found_wfl;
6357 /* Lookup methods in interfaces using their name and partial
6358 signature. Return a matching method only if their types differ. */
6361 lookup_java_interface_method2 (class, method_decl)
6362 tree class, method_decl;
6365 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6370 n = TREE_VEC_LENGTH (basetype_vec);
6371 for (i = 0; i < n; i++)
6373 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6374 if ((BINFO_TYPE (vec_elt) != object_type_node)
6376 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6379 for (i = 0; i < n; i++)
6381 to_return = lookup_java_interface_method2
6382 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6390 /* Lookup method using their name and partial signature. Return a
6391 matching method only if their types differ. */
6394 lookup_java_method2 (clas, method_decl, do_interface)
6395 tree clas, method_decl;
6398 tree method, method_signature, method_name, method_type, name;
6400 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6401 name = DECL_NAME (method_decl);
6402 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6403 EXPR_WFL_NODE (name) : name);
6404 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6406 while (clas != NULL_TREE)
6408 for (method = TYPE_METHODS (clas);
6409 method != NULL_TREE; method = TREE_CHAIN (method))
6411 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6412 tree name = DECL_NAME (method);
6413 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6414 EXPR_WFL_NODE (name) : name) == method_name
6415 && method_sig == method_signature
6416 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6419 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6424 /* Return the line that matches DECL line number, and try its best to
6425 position the column number. Used during error reports. */
6431 static tree cl = NULL_TREE;
6437 if (cl == NULL_TREE)
6439 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6440 ggc_add_tree_root (&cl, 1);
6443 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6444 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6446 line = java_get_line_col (IDENTIFIER_POINTER (EXPR_WFL_FILENAME_NODE (cl)),
6447 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6449 found = strstr ((const char *)line,
6450 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6452 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6457 /* Look for a simple name in the single-type import list */
6460 find_name_in_single_imports (name)
6465 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6466 if (TREE_VALUE (node) == name)
6467 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6472 /* Process all single-type import. */
6480 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6482 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6484 /* Don't load twice something already defined. */
6485 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6487 QUALIFIED_P (to_be_found) = 1;
6488 load_class (to_be_found, 0);
6490 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6491 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6493 parse_error_context (TREE_PURPOSE (import),
6494 "Class or interface `%s' not found in import",
6495 IDENTIFIER_POINTER (to_be_found));
6504 /* Possibly find and mark a class imported by a single-type import
6508 find_in_imports (class_type)
6513 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6514 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6516 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6517 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6522 note_possible_classname (name, len)
6527 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6529 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6533 node = ident_subst (name, len, "", '/', '.', "");
6534 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6535 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6539 /* Read a import directory, gathering potential match for further type
6540 references. Indifferently reads a filesystem or a ZIP archive
6544 read_import_dir (wfl)
6547 tree package_id = EXPR_WFL_NODE (wfl);
6548 const char *package_name = IDENTIFIER_POINTER (package_id);
6549 int package_length = IDENTIFIER_LENGTH (package_id);
6551 JCF *saved_jcf = current_jcf;
6556 struct buffer filename[1];
6559 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6561 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6563 BUFFER_INIT (filename);
6564 buffer_grow (filename, package_length + 100);
6566 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6568 const char *entry_name = jcf_path_name (entry);
6569 int entry_length = strlen (entry_name);
6570 if (jcf_path_is_zipfile (entry))
6573 buffer_grow (filename, entry_length);
6574 memcpy (filename->data, entry_name, entry_length - 1);
6575 filename->data[entry_length-1] = '\0';
6576 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6578 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6581 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6582 BUFFER_RESET (filename);
6583 for (k = 0; k < package_length; k++)
6585 char ch = package_name[k];
6586 *filename->ptr++ = ch == '.' ? '/' : ch;
6588 *filename->ptr++ = '/';
6590 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
6592 const char *current_entry = ZIPDIR_FILENAME (zipd);
6593 int current_entry_len = zipd->filename_length;
6595 if (current_entry_len >= BUFFER_LENGTH (filename)
6596 && strncmp (filename->data, current_entry,
6597 BUFFER_LENGTH (filename)) != 0)
6599 found |= note_possible_classname (current_entry,
6606 BUFFER_RESET (filename);
6607 buffer_grow (filename, entry_length + package_length + 4);
6608 strcpy (filename->data, entry_name);
6609 filename->ptr = filename->data + entry_length;
6610 for (k = 0; k < package_length; k++)
6612 char ch = package_name[k];
6613 *filename->ptr++ = ch == '.' ? '/' : ch;
6615 *filename->ptr = '\0';
6617 dirp = opendir (filename->data);
6620 *filename->ptr++ = '/';
6625 struct dirent *direntp = readdir (dirp);
6628 d_name = direntp->d_name;
6629 len = strlen (direntp->d_name);
6630 buffer_grow (filename, len+1);
6631 strcpy (filename->ptr, d_name);
6632 found |= note_possible_classname (filename->data + entry_length,
6633 package_length+len+1);
6640 free (filename->data);
6642 /* Here we should have a unified way of retrieving an entry, to be
6646 static int first = 1;
6649 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
6654 parse_error_context (wfl, "Package `%s' not found in import",
6656 current_jcf = saved_jcf;
6659 current_jcf = saved_jcf;
6662 /* Possibly find a type in the import on demands specified
6663 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6664 entire list, to detected potential double definitions. */
6667 find_in_imports_on_demand (class_type)
6670 tree node, import, node_to_use = NULL_TREE;
6672 tree cl = NULL_TREE;
6674 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6676 const char *id_name;
6677 obstack_grow (&temporary_obstack,
6678 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6679 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6680 obstack_1grow (&temporary_obstack, '.');
6681 obstack_grow0 (&temporary_obstack,
6682 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6683 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6684 id_name = obstack_finish (&temporary_obstack);
6686 node = maybe_get_identifier (id_name);
6687 if (node && IS_A_CLASSFILE_NAME (node))
6691 cl = TREE_PURPOSE (import);
6699 (TREE_PURPOSE (import),
6700 "Type `%s' also potentially defined in package `%s'",
6701 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6702 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6709 /* Setup lineno so that it refers to the line of the import (in
6710 case we parse a class file and encounter errors */
6712 int saved_lineno = lineno;
6713 lineno = EXPR_WFL_LINENO (cl);
6714 TYPE_NAME (class_type) = node_to_use;
6715 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6716 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6717 /* If there is no DECL set for the class or if the class isn't
6718 loaded and not seen in source yet, the load */
6719 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6720 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6721 load_class (node_to_use, 0);
6722 lineno = saved_lineno;
6723 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6726 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6729 /* Add package NAME to the list of package encountered so far. To
6730 speed up class lookup in do_resolve_class, we make sure a
6731 particular package is added only once. */
6734 register_package (name)
6737 static struct hash_table _pht, *pht = NULL;
6741 hash_table_init (&_pht, hash_newfunc,
6742 java_hash_hash_tree_node, java_hash_compare_tree_node);
6746 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6748 package_list = chainon (package_list, build_tree_list (name, NULL));
6749 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6754 resolve_package (pkg, next)
6758 tree type_name = NULL_TREE;
6759 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
6761 /* The trick is to determine when the package name stops and were
6762 the name of something contained in the package starts. Then we
6763 return a fully qualified name of what we want to get. */
6765 /* Do a quick search on well known package names */
6766 if (!strncmp (name, "java.lang.reflect", 17))
6769 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6770 type_name = lookup_package_type (name, 17);
6772 else if (!strncmp (name, "java.lang", 9))
6774 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6775 type_name = lookup_package_type (name, 9);
6778 /* If we found something here, return */
6782 *next = EXPR_WFL_QUALIFICATION (pkg);
6784 /* Try the current package. */
6785 if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package),
6786 IDENTIFIER_LENGTH (ctxp->package)))
6789 lookup_package_type_and_set_next (name,
6790 IDENTIFIER_LENGTH (ctxp->package),
6796 /* Search in imported package */
6797 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
6799 tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current));
6800 int len = IDENTIFIER_LENGTH (current_pkg_name);
6801 if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len))
6805 breakdown_qualified (&left, &dummy, current_pkg_name);
6806 len = IDENTIFIER_LENGTH (left);
6807 type_name = lookup_package_type_and_set_next (name, len, next);
6813 /* Try to progressively construct a type name */
6814 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6815 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6816 current; current = TREE_CHAIN (current))
6818 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6819 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6822 /* resolve_package should be used in a loop, hence we
6823 point at this one to naturally process the next one at
6824 the next iteration. */
6833 lookup_package_type_and_set_next (name, len, next)
6839 tree type_name = lookup_package_type (name, len);
6844 ptr = IDENTIFIER_POINTER (type_name);
6845 while (ptr && (ptr = strchr (ptr, '.')))
6847 *next = TREE_CHAIN (*next);
6854 lookup_package_type (name, from)
6859 const char *sub = &name[from+1];
6860 while (*sub != '.' && *sub)
6862 strncpy (subname, name, sub-name);
6863 subname [sub-name] = '\0';
6864 return get_identifier (subname);
6868 check_inner_class_access (decl, enclosing_decl, cl)
6869 tree decl, enclosing_decl, cl;
6873 /* We don't issue an error message when CL is null. CL can be null
6874 as a result of processing a JDEP crafted by source_start_java_method
6875 for the purpose of patching its parm decl. But the error would
6876 have been already trapped when fixing the method's signature.
6877 DECL can also be NULL in case of earlier errors. */
6881 /* We grant access to private and protected inner classes if the
6882 location from where we're trying to access DECL is an enclosing
6883 context for DECL or if both have a common enclosing context. */
6884 if (CLASS_PRIVATE (decl))
6886 if (CLASS_PROTECTED (decl))
6891 if (common_enclosing_context_p (TREE_TYPE (enclosing_decl),
6893 || enclosing_context_p (TREE_TYPE (enclosing_decl),
6897 parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed",
6898 (access == 1 ? "private" : "protected"),
6899 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6900 lang_printable_name (decl, 0));
6903 /* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6904 access violations were found, 1 otherwise. */
6907 check_pkg_class_access (class_name, cl)
6913 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6916 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6919 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6921 /* Access to a private class within the same package is
6924 breakdown_qualified (&l, &r, class_name);
6925 if (l == ctxp->package)
6929 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
6930 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6931 IDENTIFIER_POINTER (class_name));
6937 /* Local variable declaration. */
6940 declare_local_variables (modifier, type, vlist)
6945 tree decl, current, saved_type;
6946 tree type_wfl = NULL_TREE;
6950 /* Push a new block if statements were seen between the last time we
6951 pushed a block and now. Keep a cound of block to close */
6952 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
6954 tree body = GET_CURRENT_BLOCK (current_function_decl);
6955 tree b = enter_block ();
6956 BLOCK_EXPR_ORIGIN (b) = body;
6962 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
6963 if (modifier == ACC_FINAL)
6968 (ctxp->modifier_ctx [i],
6969 "Only `final' is allowed as a local variables modifier");
6974 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6975 hold the TYPE value if a new incomplete has to be created (as
6976 opposed to being found already existing and reused). */
6977 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6979 /* If TYPE is fully resolved and we don't have a reference, make one */
6980 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6982 /* Go through all the declared variables */
6983 for (current = vlist, saved_type = type; current;
6984 current = TREE_CHAIN (current), type = saved_type)
6986 tree other, real_type;
6987 tree wfl = TREE_PURPOSE (current);
6988 tree name = EXPR_WFL_NODE (wfl);
6989 tree init = TREE_VALUE (current);
6991 /* Process NAME, as it may specify extra dimension(s) for it */
6992 type = build_array_from_name (type, type_wfl, name, &name);
6994 /* Variable redefinition check */
6995 if ((other = lookup_name_in_blocks (name)))
6997 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6998 DECL_SOURCE_LINE (other));
7002 /* Type adjustment. We may have just readjusted TYPE because
7003 the variable specified more dimensions. Make sure we have
7004 a reference if we can and don't have one already. */
7005 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
7007 real_type = GET_REAL_TYPE (type);
7008 /* Never layout this decl. This will be done when its scope
7010 decl = build_decl (VAR_DECL, name, real_type);
7011 LOCAL_FINAL (decl) = final_p;
7012 BLOCK_CHAIN_DECL (decl);
7014 /* If doing xreferencing, replace the line number with the WFL
7017 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
7019 /* Don't try to use an INIT statement when an error was found */
7020 if (init && java_error_count)
7023 /* Add the initialization function to the current function's code */
7026 /* Name might have been readjusted */
7027 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
7028 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
7029 java_method_add_stmt (current_function_decl,
7030 build_debugable_stmt (EXPR_WFL_LINECOL (init),
7034 /* Setup dependency the type of the decl */
7038 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
7039 dep = CLASSD_LAST (ctxp->classd_list);
7040 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
7043 SOURCE_FRONTEND_DEBUG (("Defined locals"));
7046 /* Called during parsing. Build decls from argument list. */
7049 source_start_java_method (fndecl)
7059 current_function_decl = fndecl;
7061 /* New scope for the function */
7063 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
7064 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
7066 tree type = TREE_VALUE (tem);
7067 tree name = TREE_PURPOSE (tem);
7069 /* If type is incomplete. Create an incomplete decl and ask for
7070 the decl to be patched later */
7071 if (INCOMPLETE_TYPE_P (type))
7074 tree real_type = GET_REAL_TYPE (type);
7075 parm_decl = build_decl (PARM_DECL, name, real_type);
7076 type = obtain_incomplete_type (type);
7077 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7078 jdep = CLASSD_LAST (ctxp->classd_list);
7079 JDEP_MISC (jdep) = name;
7080 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7083 parm_decl = build_decl (PARM_DECL, name, type);
7085 /* Remember if a local variable was declared final (via its
7086 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7087 if (ARG_FINAL_P (tem))
7088 LOCAL_FINAL (parm_decl) = 1;
7090 BLOCK_CHAIN_DECL (parm_decl);
7092 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7093 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7095 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7096 DECL_MAX_LOCALS (current_function_decl) = i;
7099 /* Called during parsing. Creates an artificial method declaration. */
7102 create_artificial_method (class, flags, type, name, args)
7105 tree type, name, args;
7109 java_parser_context_save_global ();
7111 mdecl = make_node (FUNCTION_TYPE);
7112 TREE_TYPE (mdecl) = type;
7113 TYPE_ARG_TYPES (mdecl) = args;
7114 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7115 java_parser_context_restore_global ();
7116 DECL_ARTIFICIAL (mdecl) = 1;
7120 /* Starts the body if an artifical method. */
7123 start_artificial_method_body (mdecl)
7126 DECL_SOURCE_LINE (mdecl) = 1;
7127 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7128 source_start_java_method (mdecl);
7133 end_artificial_method_body (mdecl)
7136 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
7140 /* Called during expansion. Push decls formerly built from argument
7141 list so they're usable during expansion. */
7144 expand_start_java_method (fndecl)
7149 current_function_decl = fndecl;
7152 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
7153 announce_function (fndecl);
7155 fprintf (stderr, "]");
7157 pushlevel (1); /* Prepare for a parameter push */
7158 ptr = &DECL_ARGUMENTS (fndecl);
7159 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7162 tree next = TREE_CHAIN (tem);
7163 tree type = TREE_TYPE (tem);
7164 if (PROMOTE_PROTOTYPES
7165 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7166 && INTEGRAL_TYPE_P (type))
7167 type = integer_type_node;
7168 DECL_ARG_TYPE (tem) = type;
7169 layout_decl (tem, 0);
7172 ptr = &TREE_CHAIN (tem);
7176 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7177 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
7180 /* Terminate a function and expand its body. */
7183 source_end_java_method ()
7185 tree fndecl = current_function_decl;
7186 int flag_asynchronous_exceptions = asynchronous_exceptions;
7191 java_parser_context_save_global ();
7192 lineno = ctxp->last_ccb_indent1;
7194 /* Set EH language codes */
7195 java_set_exception_lang_code ();
7197 /* Turn function bodies with only a NOP expr null, so they don't get
7198 generated at all and we won't get warnings when using the -W
7200 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7201 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7203 /* Generate function's code */
7204 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7205 && ! flag_emit_class_files
7206 && ! flag_emit_xref)
7207 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7209 /* pop out of its parameters */
7210 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7212 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7214 /* Generate rtl for function exit. */
7215 if (! flag_emit_class_files && ! flag_emit_xref)
7217 lineno = DECL_SOURCE_LINE_LAST (fndecl);
7218 /* Emit catch-finally clauses */
7220 expand_function_end (input_filename, lineno, 0);
7222 /* FIXME: If the current method contains any exception handlers,
7223 force asynchronous_exceptions: this is necessary because signal
7224 handlers in libjava may throw exceptions. This is far from being
7225 a perfect solution, but it's better than doing nothing at all.*/
7227 asynchronous_exceptions = 1;
7229 /* Run the optimizers and output assembler code for this function. */
7230 rest_of_compilation (fndecl);
7233 current_function_decl = NULL_TREE;
7234 java_parser_context_restore_global ();
7235 asynchronous_exceptions = flag_asynchronous_exceptions;
7238 /* Record EXPR in the current function block. Complements compound
7239 expression second operand if necessary. */
7242 java_method_add_stmt (fndecl, expr)
7245 if (!GET_CURRENT_BLOCK (fndecl))
7247 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7251 add_stmt_to_block (b, type, stmt)
7254 tree body = BLOCK_EXPR_BODY (b), c;
7256 if (java_error_count)
7259 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7262 BLOCK_EXPR_BODY (b) = c;
7263 TREE_SIDE_EFFECTS (c) = 1;
7267 /* Add STMT to EXISTING if possible, otherwise create a new
7268 COMPOUND_EXPR and add STMT to it. */
7271 add_stmt_to_compound (existing, type, stmt)
7272 tree existing, type, stmt;
7275 return build (COMPOUND_EXPR, type, existing, stmt);
7280 void java_layout_seen_class_methods ()
7282 tree previous_list = all_class_list;
7283 tree end = NULL_TREE;
7288 for (current = previous_list;
7289 current != end; current = TREE_CHAIN (current))
7290 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7292 if (previous_list != all_class_list)
7294 end = previous_list;
7295 previous_list = all_class_list;
7303 java_reorder_fields ()
7305 static tree stop_reordering = NULL_TREE;
7306 static int initialized_p;
7309 /* Register STOP_REORDERING with the garbage collector. */
7312 ggc_add_tree_root (&stop_reordering, 1);
7316 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7318 current_class = TREE_TYPE (TREE_VALUE (current));
7320 if (current_class == stop_reordering)
7323 /* Reverse the fields, but leave the dummy field in front.
7324 Fields are already ordered for Object and Class */
7325 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7326 && current_class != class_type_node)
7328 /* If the dummy field is there, reverse the right fields and
7329 just layout the type for proper fields offset */
7330 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7332 tree fields = TYPE_FIELDS (current_class);
7333 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7334 TYPE_SIZE (current_class) = NULL_TREE;
7336 /* We don't have a dummy field, we need to layout the class,
7337 after having reversed the fields */
7340 TYPE_FIELDS (current_class) =
7341 nreverse (TYPE_FIELDS (current_class));
7342 TYPE_SIZE (current_class) = NULL_TREE;
7346 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7349 /* Layout the methods of all classes loaded in one way on an
7350 other. Check methods of source parsed classes. Then reorder the
7351 fields and layout the classes or the type of all source parsed
7355 java_layout_classes ()
7358 int save_error_count = java_error_count;
7360 /* Layout the methods of all classes seen so far */
7361 java_layout_seen_class_methods ();
7362 java_parse_abort_on_error ();
7363 all_class_list = NULL_TREE;
7365 /* Then check the methods of all parsed classes */
7366 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7367 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7368 java_check_methods (TREE_VALUE (current));
7369 java_parse_abort_on_error ();
7371 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7373 current_class = TREE_TYPE (TREE_VALUE (current));
7374 layout_class (current_class);
7376 /* From now on, the class is considered completely loaded */
7377 CLASS_LOADED_P (current_class) = 1;
7379 /* Error reported by the caller */
7380 if (java_error_count)
7384 /* We might have reloaded classes durign the process of laying out
7385 classes for code generation. We must layout the methods of those
7386 late additions, as constructor checks might use them */
7387 java_layout_seen_class_methods ();
7388 java_parse_abort_on_error ();
7391 /* Expand methods in the current set of classes rememebered for
7395 java_complete_expand_classes ()
7399 do_not_fold = flag_emit_xref;
7401 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7402 if (!INNER_CLASS_DECL_P (current))
7403 java_complete_expand_class (current);
7406 /* Expand the methods found in OUTER, starting first by OUTER's inner
7410 java_complete_expand_class (outer)
7415 set_nested_class_simple_name_value (outer, 1); /* Set */
7417 /* We need to go after all inner classes and start expanding them,
7418 starting with most nested ones. We have to do that because nested
7419 classes might add functions to outer classes */
7421 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7422 inner_list; inner_list = TREE_CHAIN (inner_list))
7423 java_complete_expand_class (TREE_PURPOSE (inner_list));
7425 java_complete_expand_methods (outer);
7426 set_nested_class_simple_name_value (outer, 0); /* Reset */
7429 /* Expand methods registered in CLASS_DECL. The general idea is that
7430 we expand regular methods first. This allows us get an estimate on
7431 how outer context local alias fields are really used so we can add
7432 to the constructor just enough code to initialize them properly (it
7433 also lets us generate finit$ correctly.) Then we expand the
7434 constructors and then <clinit>. */
7437 java_complete_expand_methods (class_decl)
7440 tree clinit, finit, decl, first_decl;
7442 current_class = TREE_TYPE (class_decl);
7444 /* Initialize a new constant pool */
7445 init_outgoing_cpool ();
7447 /* Pre-expand <clinit> to figure whether we really need it or
7448 not. If we do need it, we pre-expand the static fields so they're
7449 ready to be used somewhere else. <clinit> will be fully expanded
7450 after we processed the constructors. */
7451 first_decl = TYPE_METHODS (current_class);
7452 clinit = maybe_generate_pre_expand_clinit (current_class);
7454 /* Then generate finit$ (if we need to) because constructor will
7456 if (TYPE_FINIT_STMT_LIST (current_class))
7458 finit = generate_finit (current_class);
7459 java_complete_expand_method (finit);
7462 /* Now do the constructors */
7463 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7467 if (!DECL_CONSTRUCTOR_P (decl))
7470 no_body = !DECL_FUNCTION_BODY (decl);
7471 /* Don't generate debug info on line zero when expanding a
7472 generated constructor. */
7474 restore_line_number_status (1);
7476 java_complete_expand_method (decl);
7479 restore_line_number_status (0);
7482 /* First, do the ordinary methods. */
7483 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7485 /* Skip abstract or native methods -- but do handle native
7486 methods when generating JNI stubs. */
7487 if (METHOD_ABSTRACT (decl)
7488 || (! flag_jni && METHOD_NATIVE (decl))
7489 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7492 if (METHOD_NATIVE (decl))
7494 tree body = build_jni_stub (decl);
7495 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7498 java_complete_expand_method (decl);
7501 /* If there is indeed a <clinit>, fully expand it now */
7504 /* Prevent the use of `this' inside <clinit> */
7505 ctxp->explicit_constructor_p = 1;
7506 java_complete_expand_method (clinit);
7507 ctxp->explicit_constructor_p = 0;
7510 /* We might have generated a class$ that we now want to expand */
7511 if (TYPE_DOT_CLASS (current_class))
7512 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7514 /* Now verify constructor circularity (stop after the first one we
7516 if (!CLASS_INTERFACE (class_decl))
7517 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7518 if (DECL_CONSTRUCTOR_P (decl)
7519 && verify_constructor_circularity (decl, decl))
7522 /* Save the constant pool. We'll need to restore it later. */
7523 TYPE_CPOOL (current_class) = outgoing_cpool;
7526 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7527 safely used in some other methods/constructors. */
7530 maybe_generate_pre_expand_clinit (class_type)
7533 tree current, mdecl;
7535 if (!TYPE_CLINIT_STMT_LIST (class_type))
7538 /* Go through all static fields and pre expand them */
7539 for (current = TYPE_FIELDS (class_type); current;
7540 current = TREE_CHAIN (current))
7541 if (FIELD_STATIC (current))
7542 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7544 /* Then build the <clinit> method */
7545 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7546 clinit_identifier_node, end_params_node);
7547 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7549 start_artificial_method_body (mdecl);
7551 /* We process the list of assignment we produced as the result of
7552 the declaration of initialized static field and add them as
7553 statement to the <clinit> method. */
7554 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7555 current = TREE_CHAIN (current))
7557 tree stmt = current;
7558 /* We build the assignment expression that will initialize the
7559 field to its value. There are strict rules on static
7560 initializers (8.5). FIXME */
7561 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7562 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7563 java_method_add_stmt (mdecl, stmt);
7566 end_artificial_method_body (mdecl);
7568 /* Now we want to place <clinit> as the last method (because we need
7569 it at least for interface so that it doesn't interfere with the
7570 dispatch table based lookup. */
7571 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7573 current = TREE_CHAIN (TYPE_METHODS (class_type));
7574 TYPE_METHODS (class_type) = current;
7576 while (TREE_CHAIN (current))
7577 current = TREE_CHAIN (current);
7579 TREE_CHAIN (current) = mdecl;
7580 TREE_CHAIN (mdecl) = NULL_TREE;
7586 /* See whether we could get rid of <clinit>. Criteria are: all static
7587 final fields have constant initial values and the body of <clinit>
7588 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7591 maybe_yank_clinit (mdecl)
7598 if (!DECL_CLINIT_P (mdecl))
7601 /* If the body isn't empty, then we keep <clinit>. Note that if
7602 we're emitting classfiles, this isn't enough not to rule it
7604 fbody = DECL_FUNCTION_BODY (mdecl);
7605 if ((bbody = BLOCK_EXPR_BODY (fbody)))
7606 bbody = BLOCK_EXPR_BODY (bbody);
7607 if (bbody && ! flag_emit_class_files && bbody != empty_stmt_node)
7610 type = DECL_CONTEXT (mdecl);
7611 current = TYPE_FIELDS (type);
7613 for (current = (current ? TREE_CHAIN (current) : current);
7614 current; current = TREE_CHAIN (current))
7618 /* We're not interested in non static field */
7619 if (!FIELD_STATIC (current))
7622 /* Anything that isn't String or a basic type is ruled out -- or
7623 if we now how to deal with it (when doing things natively) we
7624 should generated an empty <clinit> so that SUID are computed
7626 if (! JSTRING_TYPE_P (TREE_TYPE (current))
7627 && ! JNUMERIC_TYPE_P (TREE_TYPE (current)))
7630 f_init = DECL_INITIAL (current);
7631 /* If we're emitting native code, we want static final fields to
7632 have constant initializers. If we don't meet these
7633 conditions, we keep <clinit> */
7634 if (!flag_emit_class_files
7635 && !(FIELD_FINAL (current) && f_init && TREE_CONSTANT (f_init)))
7637 /* If we're emitting bytecode, we want static fields to have
7638 constant initializers or no initializer. If we don't meet
7639 these conditions, we keep <clinit> */
7640 if (flag_emit_class_files && f_init && !TREE_CONSTANT (f_init))
7644 /* Now we analyze the method body and look for something that
7645 isn't a MODIFY_EXPR */
7646 if (bbody == empty_stmt_node)
7649 switch (TREE_CODE (bbody))
7652 bbody = BLOCK_EXPR_BODY (bbody);
7655 case EXPR_WITH_FILE_LOCATION:
7656 bbody = EXPR_WFL_NODE (bbody);
7660 bbody = TREE_OPERAND (bbody, 0);
7672 if (current || found)
7675 /* Get rid of <clinit> in the class' list of methods */
7676 if (TYPE_METHODS (type) == mdecl)
7677 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7679 for (current = TYPE_METHODS (type); current;
7680 current = TREE_CHAIN (current))
7681 if (TREE_CHAIN (current) == mdecl)
7683 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7691 /* Complete and expand a method. */
7694 java_complete_expand_method (mdecl)
7697 int yank_clinit = 0;
7699 current_function_decl = mdecl;
7700 /* Fix constructors before expanding them */
7701 if (DECL_CONSTRUCTOR_P (mdecl))
7702 fix_constructors (mdecl);
7704 /* Expand functions that have a body */
7705 if (DECL_FUNCTION_BODY (mdecl))
7707 tree fbody = DECL_FUNCTION_BODY (mdecl);
7708 tree block_body = BLOCK_EXPR_BODY (fbody);
7709 tree exception_copy = NULL_TREE;
7710 expand_start_java_method (mdecl);
7711 build_result_decl (mdecl);
7714 = (!METHOD_STATIC (mdecl) ?
7715 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7717 /* Purge the `throws' list of unchecked exceptions. If we're
7718 doing xref, save a copy of the list and re-install it
7721 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7723 purge_unchecked_exceptions (mdecl);
7725 /* Install exceptions thrown with `throws' */
7726 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7728 if (block_body != NULL_TREE)
7730 block_body = java_complete_tree (block_body);
7732 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
7733 check_for_initialization (block_body);
7734 ctxp->explicit_constructor_p = 0;
7737 BLOCK_EXPR_BODY (fbody) = block_body;
7739 /* If we saw a return but couldn't evaluate it properly, we'll
7740 have an error_mark_node here. */
7741 if (block_body != error_mark_node
7742 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
7743 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7745 missing_return_error (current_function_decl);
7747 /* Check wether we could just get rid of clinit, now the picture
7749 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7750 complete_start_java_method (mdecl);
7752 /* Don't go any further if we've found error(s) during the
7754 if (!java_error_count && !yank_clinit)
7755 source_end_java_method ();
7758 if (java_error_count)
7759 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7763 /* Pop the exceptions and sanity check */
7765 if (currently_caught_type_list)
7766 fatal ("Exception list non empty - java_complete_expand_method");
7769 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
7775 /* This section of the code deals with accessing enclosing context
7776 fields either directly by using the relevant access to this$<n> or
7777 by invoking an access method crafted for that purpose. */
7779 /* Build the necessary access from an inner class to an outer
7780 class. This routine could be optimized to cache previous result
7781 (decl, current_class and returned access). When an access method
7782 needs to be generated, it always takes the form of a read. It might
7783 be later turned into a write by calling outer_field_access_fix. */
7786 build_outer_field_access (id, decl)
7789 tree access = NULL_TREE;
7790 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7792 /* If decl's class is the direct outer class of the current_class,
7793 build the access as `this$<n>.<field>'. Note that we will break
7794 the `private' barrier if we're not emitting bytecodes. */
7795 if (ctx == DECL_CONTEXT (decl)
7796 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7798 tree thisn = build_current_thisn (current_class);
7799 access = make_qualified_primary (build_wfl_node (thisn),
7800 id, EXPR_WFL_LINECOL (id));
7802 /* Otherwise, generate access methods to outer this and access the
7803 field (either using an access method or by direct access.) */
7806 int lc = EXPR_WFL_LINECOL (id);
7808 /* Now we chain the required number of calls to the access$0 to
7809 get a hold to the enclosing instance we need, and then we
7810 build the field access. */
7811 access = build_access_to_thisn (ctx, DECL_CONTEXT (decl), lc);
7813 /* If the field is private and we're generating bytecode, then
7814 we generate an access method */
7815 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7817 tree name = build_outer_field_access_methods (decl);
7818 access = build_outer_field_access_expr (lc, DECL_CONTEXT (decl),
7819 name, access, NULL_TREE);
7821 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7822 Once again we break the `private' access rule from a foreign
7825 access = make_qualified_primary (access, id, lc);
7827 return resolve_expression_name (access, NULL);
7830 /* Return a non zero value if NODE describes an outer field inner
7834 outer_field_access_p (type, decl)
7837 if (!INNER_CLASS_TYPE_P (type)
7838 || TREE_CODE (decl) != FIELD_DECL
7839 || DECL_CONTEXT (decl) == type)
7842 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7843 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7845 if (type == DECL_CONTEXT (decl))
7847 if (!DECL_CONTEXT (TYPE_NAME (type)))
7854 /* Return a non zero value if NODE represents an outer field inner
7855 access that was been already expanded. As a side effect, it returns
7856 the name of the field being accessed and the argument passed to the
7857 access function, suitable for a regeneration of the access method
7858 call if necessary. */
7861 outer_field_expanded_access_p (node, name, arg_type, arg)
7862 tree node, *name, *arg_type, *arg;
7866 if (TREE_CODE (node) != CALL_EXPR)
7869 /* Well, gcj generates slightly different tree nodes when compiling
7870 to native or bytecodes. It's the case for function calls. */
7872 if (flag_emit_class_files
7873 && TREE_CODE (node) == CALL_EXPR
7874 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7876 else if (!flag_emit_class_files)
7878 node = TREE_OPERAND (node, 0);
7880 if (node && TREE_OPERAND (node, 0)
7881 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7883 node = TREE_OPERAND (node, 0);
7884 if (TREE_OPERAND (node, 0)
7885 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7886 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7887 (DECL_NAME (TREE_OPERAND (node, 0)))))
7892 if (identified && name && arg_type && arg)
7894 tree argument = TREE_OPERAND (node, 1);
7895 *name = DECL_NAME (TREE_OPERAND (node, 0));
7896 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7897 *arg = TREE_VALUE (argument);
7902 /* Detect in NODE an outer field read access from an inner class and
7903 transform it into a write with RHS as an argument. This function is
7904 called from the java_complete_lhs when an assignment to a LHS can
7908 outer_field_access_fix (wfl, node, rhs)
7909 tree wfl, node, rhs;
7911 tree name, arg_type, arg;
7913 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7915 /* At any rate, check whether we're trying to assign a value to
7917 tree accessed = (JDECL_P (node) ? node :
7918 (TREE_CODE (node) == COMPONENT_REF ?
7919 TREE_OPERAND (node, 1) : node));
7920 if (check_final_assignment (accessed, wfl))
7921 return error_mark_node;
7923 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7924 arg_type, name, arg, rhs);
7925 return java_complete_tree (node);
7930 /* Construct the expression that calls an access method:
7931 <type>.access$<n>(<arg1> [, <arg2>]);
7933 ARG2 can be NULL and will be omitted in that case. It will denote a
7937 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7939 tree type, access_method_name, arg1, arg2;
7941 tree args, cn, access;
7943 args = arg1 ? arg1 :
7944 build_wfl_node (build_current_thisn (current_class));
7945 args = build_tree_list (NULL_TREE, args);
7948 args = tree_cons (NULL_TREE, arg2, args);
7950 access = build_method_invocation (build_wfl_node (access_method_name), args);
7951 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7952 return make_qualified_primary (cn, access, lc);
7956 build_new_access_id ()
7958 static int access_n_counter = 1;
7961 sprintf (buffer, "access$%d", access_n_counter++);
7962 return get_identifier (buffer);
7965 /* Create the static access functions for the outer field DECL. We define a
7967 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7971 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7972 TREE_TYPE (<field>) value$) {
7973 return inst$.field = value$;
7975 We should have a usage flags on the DECL so we can lazily turn the ones
7976 we're using for code generation. FIXME.
7980 build_outer_field_access_methods (decl)
7983 tree id, args, stmt, mdecl;
7985 /* Check point, to be removed. FIXME */
7986 if (FIELD_INNER_ACCESS (decl)
7987 && TREE_CODE (FIELD_INNER_ACCESS (decl)) != IDENTIFIER_NODE)
7990 if (FIELD_INNER_ACCESS (decl))
7991 return FIELD_INNER_ACCESS (decl);
7993 /* Create the identifier and a function named after it. */
7994 id = build_new_access_id ();
7996 /* The identifier is marked as bearing the name of a generated write
7997 access function for outer field accessed from inner classes. */
7998 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8000 /* Create the read access */
8001 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8002 TREE_CHAIN (args) = end_params_node;
8003 stmt = make_qualified_primary (build_wfl_node (inst_id),
8004 build_wfl_node (DECL_NAME (decl)), 0);
8005 stmt = build_return (0, stmt);
8006 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8007 TREE_TYPE (decl), id, args, stmt);
8008 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8010 /* Create the write access method */
8011 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
8012 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
8013 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
8014 stmt = make_qualified_primary (build_wfl_node (inst_id),
8015 build_wfl_node (DECL_NAME (decl)), 0);
8016 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
8017 build_wfl_node (wpv_id)));
8019 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
8020 TREE_TYPE (decl), id, args, stmt);
8021 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
8023 /* Return the access name */
8024 return FIELD_INNER_ACCESS (decl) = id;
8027 /* Build an field access method NAME. */
8030 build_outer_field_access_method (class, type, name, args, body)
8031 tree class, type, name, args, body;
8033 tree saved_current_function_decl, mdecl;
8035 /* Create the method */
8036 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
8037 fix_method_argument_names (args, mdecl);
8038 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8040 /* Attach the method body. */
8041 saved_current_function_decl = current_function_decl;
8042 start_artificial_method_body (mdecl);
8043 java_method_add_stmt (mdecl, body);
8044 end_artificial_method_body (mdecl);
8045 current_function_decl = saved_current_function_decl;
8051 /* This section deals with building access function necessary for
8052 certain kinds of method invocation from inner classes. */
8055 build_outer_method_access_method (decl)
8058 tree saved_current_function_decl, mdecl;
8059 tree args = NULL_TREE, call_args = NULL_TREE;
8060 tree carg, id, body, class;
8062 int parm_id_count = 0;
8064 /* Test this abort with an access to a private field */
8065 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
8068 /* Check the cache first */
8069 if (DECL_FUNCTION_INNER_ACCESS (decl))
8070 return DECL_FUNCTION_INNER_ACCESS (decl);
8072 class = DECL_CONTEXT (decl);
8074 /* Obtain an access identifier and mark it */
8075 id = build_new_access_id ();
8076 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
8078 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
8079 /* Create the arguments, as much as the original */
8080 for (; carg && carg != end_params_node;
8081 carg = TREE_CHAIN (carg))
8083 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
8084 args = chainon (args, build_tree_list (get_identifier (buffer),
8085 TREE_VALUE (carg)));
8087 args = chainon (args, end_params_node);
8089 /* Create the method */
8090 mdecl = create_artificial_method (class, ACC_STATIC,
8091 TREE_TYPE (TREE_TYPE (decl)), id, args);
8092 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8093 /* There is a potential bug here. We should be able to use
8094 fix_method_argument_names, but then arg names get mixed up and
8095 eventually a constructor will have its this$0 altered and the
8096 outer context won't be assignment properly. The test case is
8098 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
8100 /* Attach the method body. */
8101 saved_current_function_decl = current_function_decl;
8102 start_artificial_method_body (mdecl);
8104 /* The actual method invocation uses the same args. When invoking a
8105 static methods that way, we don't want to skip the first
8108 if (!METHOD_STATIC (decl))
8109 carg = TREE_CHAIN (carg);
8110 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
8111 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
8114 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
8116 if (!METHOD_STATIC (decl))
8117 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8119 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8120 body = build_return (0, body);
8121 java_method_add_stmt (mdecl,body);
8122 end_artificial_method_body (mdecl);
8123 current_function_decl = saved_current_function_decl;
8125 /* Back tag the access function so it know what it accesses */
8126 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8128 /* Tag the current method so it knows it has an access generated */
8129 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8133 /* This section of the code deals with building expressions to access
8134 the enclosing instance of an inner class. The enclosing instance is
8135 kept in a generated field called this$<n>, with <n> being the
8136 inner class nesting level (starting from 0.) */
8138 /* Build an access to a given this$<n>, possibly by chaining access
8139 call to others. Access methods to this$<n> are build on the fly if
8143 build_access_to_thisn (from, to, lc)
8147 tree access = NULL_TREE;
8151 tree access0_wfl, cn;
8153 maybe_build_thisn_access_method (from);
8154 access0_wfl = build_wfl_node (access0_identifier_node);
8155 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8156 EXPR_WFL_LINECOL (access0_wfl) = lc;
8160 access = build_current_thisn (current_class);
8161 access = build_wfl_node (access);
8163 access = build_tree_list (NULL_TREE, access);
8164 access = build_method_invocation (access0_wfl, access);
8165 access = make_qualified_primary (cn, access, lc);
8167 from = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (from)));
8172 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8173 is returned if nothing needs to be generated. Otherwise, the method
8174 generated and a method decl is returned.
8176 NOTE: These generated methods should be declared in a class file
8177 attribute so that they can't be referred to directly. */
8180 maybe_build_thisn_access_method (type)
8183 tree mdecl, args, stmt, rtype;
8184 tree saved_current_function_decl;
8186 /* If TYPE is a top-level class, no access method is required.
8187 If there already is such an access method, bail out. */
8188 if (CLASS_ACCESS0_GENERATED_P (type) || !INNER_CLASS_TYPE_P (type))
8191 /* We generate the method. The method looks like:
8192 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8194 args = build_tree_list (inst_id, build_pointer_type (type));
8195 TREE_CHAIN (args) = end_params_node;
8196 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8197 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8198 access0_identifier_node, args);
8199 fix_method_argument_names (args, mdecl);
8200 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8201 stmt = build_current_thisn (type);
8202 stmt = make_qualified_primary (build_wfl_node (inst_id),
8203 build_wfl_node (stmt), 0);
8204 stmt = build_return (0, stmt);
8206 saved_current_function_decl = current_function_decl;
8207 start_artificial_method_body (mdecl);
8208 java_method_add_stmt (mdecl, stmt);
8209 end_artificial_method_body (mdecl);
8210 current_function_decl = saved_current_function_decl;
8212 CLASS_ACCESS0_GENERATED_P (type) = 1;
8217 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8218 the first level of innerclassing. this$1 for the next one, etc...
8219 This function can be invoked with TYPE to NULL, available and then
8220 has to count the parser context. */
8223 build_current_thisn (type)
8226 static int saved_i = -1;
8227 static tree saved_thisn = NULL_TREE;
8228 static tree saved_type = NULL_TREE;
8229 static int saved_type_i = 0;
8230 static int initialized_p;
8235 /* Register SAVED_THISN and SAVED_TYPE with the garbage collector. */
8238 ggc_add_tree_root (&saved_thisn, 1);
8239 ggc_add_tree_root (&saved_type, 1);
8245 if (type == saved_type)
8249 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8250 decl; decl = DECL_CONTEXT (decl), i++)
8258 i = list_length (GET_CPC_LIST ())-2;
8263 sprintf (buffer, "this$%d", i);
8265 saved_thisn = get_identifier (buffer);
8269 /* Return the assignement to the hidden enclosing context `this$<n>'
8270 by the second incoming parameter to the innerclass constructor. The
8271 form used is `this.this$<n> = this$<n>;'. */
8274 build_thisn_assign ()
8276 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8278 tree thisn = build_current_thisn (current_class);
8279 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8280 build_wfl_node (thisn), 0);
8281 tree rhs = build_wfl_node (thisn);
8282 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8283 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8289 /* Building the synthetic `class$' used to implement the `.class' 1.1
8290 extension for non primitive types. This method looks like:
8292 static Class class$(String type) throws NoClassDefFoundError
8294 try {return (java.lang.Class.forName (String));}
8295 catch (ClassNotFoundException e) {
8296 throw new NoClassDefFoundError(e.getMessage());}
8300 build_dot_class_method (class)
8303 #define BWF(S) build_wfl_node (get_identifier ((S)))
8304 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8305 tree args, tmp, saved_current_function_decl, mdecl;
8306 tree stmt, throw_stmt, catch, catch_block, try_block;
8307 tree catch_clause_param;
8308 tree class_not_found_exception, no_class_def_found_error;
8310 static tree get_message_wfl, type_parm_wfl;
8312 if (!get_message_wfl)
8314 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8315 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8316 ggc_add_tree_root (&get_message_wfl, 1);
8317 ggc_add_tree_root (&type_parm_wfl, 1);
8320 /* Build the arguments */
8321 args = build_tree_list (get_identifier ("type$"),
8322 build_pointer_type (string_type_node));
8323 TREE_CHAIN (args) = end_params_node;
8325 /* Build the qualified name java.lang.Class.forName */
8326 tmp = MQN (MQN (MQN (BWF ("java"),
8327 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8329 /* For things we have to catch and throw */
8330 class_not_found_exception =
8331 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8332 no_class_def_found_error =
8333 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8334 load_class (class_not_found_exception, 1);
8335 load_class (no_class_def_found_error, 1);
8337 /* Create the "class$" function */
8338 mdecl = create_artificial_method (class, ACC_STATIC,
8339 build_pointer_type (class_type_node),
8340 get_identifier ("class$"), args);
8341 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8342 no_class_def_found_error);
8344 /* We start by building the try block. We need to build:
8345 return (java.lang.Class.forName (type)); */
8346 stmt = build_method_invocation (tmp,
8347 build_tree_list (NULL_TREE, type_parm_wfl));
8348 stmt = build_return (0, stmt);
8349 /* Put it in a block. That's the try block */
8350 try_block = build_expr_block (stmt, NULL_TREE);
8352 /* Now onto the catch block. We start by building the expression
8353 throwing a new exception:
8354 throw new NoClassDefFoundError (_.getMessage); */
8355 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8356 get_message_wfl, 0);
8357 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8359 /* Build new NoClassDefFoundError (_.getMessage) */
8360 throw_stmt = build_new_invocation
8361 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8362 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8364 /* Build the throw, (it's too early to use BUILD_THROW) */
8365 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8367 /* Build the catch block to encapsulate all this. We begin by
8368 building an decl for the catch clause parameter and link it to
8369 newly created block, the catch block. */
8370 catch_clause_param =
8371 build_decl (VAR_DECL, wpv_id,
8372 build_pointer_type (class_not_found_exception));
8373 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8375 /* We initialize the variable with the exception handler. */
8376 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8377 soft_exceptioninfo_call_node);
8378 add_stmt_to_block (catch_block, NULL_TREE, catch);
8380 /* We add the statement throwing the new exception */
8381 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8383 /* Build a catch expression for all this */
8384 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8386 /* Build the try/catch sequence */
8387 stmt = build_try_statement (0, try_block, catch_block);
8389 fix_method_argument_names (args, mdecl);
8390 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8391 saved_current_function_decl = current_function_decl;
8392 start_artificial_method_body (mdecl);
8393 java_method_add_stmt (mdecl, stmt);
8394 end_artificial_method_body (mdecl);
8395 current_function_decl = saved_current_function_decl;
8396 TYPE_DOT_CLASS (class) = mdecl;
8402 build_dot_class_method_invocation (type)
8407 if (TYPE_ARRAY_P (type))
8408 sig_id = build_java_signature (type);
8410 sig_id = DECL_NAME (TYPE_NAME (type));
8412 s = build_string (IDENTIFIER_LENGTH (sig_id),
8413 IDENTIFIER_POINTER (sig_id));
8414 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8415 build_tree_list (NULL_TREE, s));
8418 /* This section of the code deals with constructor. */
8420 /* Craft a body for default constructor. Patch existing constructor
8421 bodies with call to super() and field initialization statements if
8425 fix_constructors (mdecl)
8428 tree body = DECL_FUNCTION_BODY (mdecl);
8429 tree thisn_assign, compound = NULL_TREE;
8430 tree class_type = DECL_CONTEXT (mdecl);
8434 /* It is an error for the compiler to generate a default
8435 constructor if the superclass doesn't have a constructor that
8436 takes no argument, or the same args for an anonymous class */
8437 if (verify_constructor_super (mdecl))
8439 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8440 tree save = DECL_NAME (mdecl);
8441 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8442 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8444 (lookup_cl (TYPE_NAME (class_type)),
8445 "No constructor matching `%s' found in class `%s'",
8446 lang_printable_name (mdecl, 0), n);
8447 DECL_NAME (mdecl) = save;
8450 /* The constructor body must be crafted by hand. It's the
8451 constructor we defined when we realize we didn't have the
8452 CLASSNAME() constructor */
8453 start_artificial_method_body (mdecl);
8455 /* Insert an assignment to the this$<n> hidden field, if
8457 if ((thisn_assign = build_thisn_assign ()))
8458 java_method_add_stmt (mdecl, thisn_assign);
8460 /* We don't generate a super constructor invocation if we're
8461 compiling java.lang.Object. build_super_invocation takes care
8463 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8465 /* Insert the instance initializer block right here, after the
8466 super invocation. */
8467 add_instance_initializer (mdecl);
8469 end_artificial_method_body (mdecl);
8471 /* Search for an explicit constructor invocation */
8475 tree main_block = BLOCK_EXPR_BODY (body);
8478 switch (TREE_CODE (body))
8481 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8485 case EXPR_WITH_FILE_LOCATION:
8486 body = TREE_OPERAND (body, 0);
8489 body = BLOCK_EXPR_BODY (body);
8495 /* The constructor is missing an invocation of super() */
8497 compound = add_stmt_to_compound (compound, NULL_TREE,
8498 build_super_invocation (mdecl));
8500 /* Generate the assignment to this$<n>, if necessary */
8501 if ((thisn_assign = build_thisn_assign ()))
8502 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8504 /* Insert the instance initializer block right here, after the
8505 super invocation. */
8506 add_instance_initializer (mdecl);
8508 /* Fix the constructor main block if we're adding extra stmts */
8511 compound = add_stmt_to_compound (compound, NULL_TREE,
8512 BLOCK_EXPR_BODY (main_block));
8513 BLOCK_EXPR_BODY (main_block) = compound;
8518 /* Browse constructors in the super class, searching for a constructor
8519 that doesn't take any argument. Return 0 if one is found, 1
8520 otherwise. If the current class is an anonymous inner class, look
8521 for something that has the same signature. */
8524 verify_constructor_super (mdecl)
8527 tree class = CLASSTYPE_SUPER (current_class);
8528 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8534 if (ANONYMOUS_CLASS_P (current_class))
8536 tree mdecl_arg_type;
8537 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8538 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8539 if (DECL_CONSTRUCTOR_P (sdecl))
8542 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8544 arg_type = TREE_CHAIN (arg_type);
8545 for (m_arg_type = mdecl_arg_type;
8546 (arg_type != end_params_node
8547 && m_arg_type != end_params_node);
8548 arg_type = TREE_CHAIN (arg_type),
8549 m_arg_type = TREE_CHAIN (m_arg_type))
8550 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
8553 if (arg_type == end_params_node && m_arg_type == end_params_node)
8559 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8561 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8563 arg = TREE_CHAIN (arg);
8564 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8571 /* Generate code for all context remembered for code generation. */
8574 java_expand_classes ()
8576 int save_error_count = 0;
8577 static struct parser_ctxt *saved_ctxp = NULL;
8579 java_parse_abort_on_error ();
8580 if (!(ctxp = ctxp_for_generation))
8582 java_layout_classes ();
8583 java_parse_abort_on_error ();
8585 saved_ctxp = ctxp_for_generation;
8586 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8588 ctxp = ctxp_for_generation;
8589 lang_init_source (2); /* Error msgs have method prototypes */
8590 java_complete_expand_classes (); /* Complete and expand classes */
8591 java_parse_abort_on_error ();
8594 /* Find anonymous classes and expand their constructor, now they
8596 for (ctxp_for_generation = saved_ctxp;
8597 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8600 ctxp = ctxp_for_generation;
8601 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8603 current_class = TREE_TYPE (current);
8604 if (ANONYMOUS_CLASS_P (current_class))
8607 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8609 if (DECL_CONSTRUCTOR_P (d))
8611 restore_line_number_status (1);
8612 reset_method_name (d);
8613 java_complete_expand_method (d);
8614 restore_line_number_status (0);
8615 break; /* We now there are no other ones */
8622 /* If we've found error at that stage, don't try to generate
8623 anything, unless we're emitting xrefs or checking the syntax only
8624 (but not using -fsyntax-only for the purpose of generating
8626 if (java_error_count && !flag_emit_xref
8627 && (!flag_syntax_only && !flag_emit_class_files))
8630 /* Now things are stable, go for generation of the class data. */
8631 for (ctxp_for_generation = saved_ctxp;
8632 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8635 ctxp = ctxp_for_generation;
8636 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8638 current_class = TREE_TYPE (current);
8639 outgoing_cpool = TYPE_CPOOL (current_class);
8640 if (flag_emit_class_files)
8641 write_classfile (current_class);
8643 expand_xref (current_class);
8644 else if (! flag_syntax_only)
8650 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8651 a tree list node containing RIGHT. Fore coming RIGHTs will be
8652 chained to this hook. LOCATION contains the location of the
8653 separating `.' operator. */
8656 make_qualified_primary (primary, right, location)
8657 tree primary, right;
8662 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8663 wfl = build_wfl_wrap (primary, location);
8667 /* If wfl wasn't qualified, we build a first anchor */
8668 if (!EXPR_WFL_QUALIFICATION (wfl))
8669 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
8672 /* And chain them */
8673 EXPR_WFL_LINECOL (right) = location;
8674 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8675 PRIMARY_P (wfl) = 1;
8679 /* Simple merge of two name separated by a `.' */
8682 merge_qualified_name (left, right)
8686 if (!left && !right)
8695 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8696 IDENTIFIER_LENGTH (left));
8697 obstack_1grow (&temporary_obstack, '.');
8698 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8699 IDENTIFIER_LENGTH (right));
8700 node = get_identifier (obstack_base (&temporary_obstack));
8701 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8702 QUALIFIED_P (node) = 1;
8706 /* Merge the two parts of a qualified name into LEFT. Set the
8707 location information of the resulting node to LOCATION, usually
8708 inherited from the location information of the `.' operator. */
8711 make_qualified_name (left, right, location)
8715 #ifdef USE_COMPONENT_REF
8716 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8717 EXPR_WFL_LINECOL (node) = location;
8720 tree left_id = EXPR_WFL_NODE (left);
8721 tree right_id = EXPR_WFL_NODE (right);
8724 merge = merge_qualified_name (left_id, right_id);
8726 /* Left wasn't qualified and is now qualified */
8727 if (!QUALIFIED_P (left_id))
8729 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8730 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8731 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8734 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8735 EXPR_WFL_LINECOL (wfl) = location;
8736 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8738 EXPR_WFL_NODE (left) = merge;
8743 /* Extract the last identifier component of the qualified in WFL. The
8744 last identifier is removed from the linked list */
8747 cut_identifier_in_qualified (wfl)
8751 tree previous = NULL_TREE;
8752 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8753 if (!TREE_CHAIN (q))
8756 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
8757 TREE_CHAIN (previous) = NULL_TREE;
8758 return TREE_PURPOSE (q);
8762 /* Resolve the expression name NAME. Return its decl. */
8765 resolve_expression_name (id, orig)
8769 tree name = EXPR_WFL_NODE (id);
8772 /* 6.5.5.1: Simple expression names */
8773 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8775 /* 15.13.1: NAME can appear within the scope of a local variable
8777 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8780 /* 15.13.1: NAME can appear within a class declaration */
8783 decl = lookup_field_wrapper (current_class, name);
8786 tree access = NULL_TREE;
8787 int fs = FIELD_STATIC (decl);
8789 /* If we're accessing an outer scope local alias, make
8790 sure we change the name of the field we're going to
8792 if (FIELD_LOCAL_ALIAS_USED (decl))
8793 name = DECL_NAME (decl);
8795 /* Instance variable (8.3.1.1) can't appear within
8796 static method, static initializer or initializer for
8797 a static variable. */
8798 if (!fs && METHOD_STATIC (current_function_decl))
8800 static_ref_err (id, name, current_class);
8801 return error_mark_node;
8803 /* Instance variables can't appear as an argument of
8804 an explicit constructor invocation */
8805 if (!fs && ctxp->explicit_constructor_p)
8808 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
8809 return error_mark_node;
8812 /* If we're processing an inner class and we're trying
8813 to access a field belonging to an outer class, build
8814 the access to the field */
8815 if (!fs && outer_field_access_p (current_class, decl))
8816 return build_outer_field_access (id, decl);
8818 /* Otherwise build what it takes to access the field */
8819 access = build_field_ref ((fs ? NULL_TREE : current_this),
8820 DECL_CONTEXT (decl), name);
8821 if (fs && !flag_emit_class_files && !flag_emit_xref)
8822 access = build_class_init (DECL_CONTEXT (access), access);
8823 /* We may be asked to save the real field access node */
8826 /* And we return what we got */
8829 /* Fall down to error report on undefined variable */
8832 /* 6.5.5.2 Qualified Expression Names */
8837 qualify_ambiguous_name (id);
8838 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8839 /* 15.10.2: Accessing Superclass Members using super */
8840 return resolve_field_access (id, orig, NULL);
8843 /* We've got an error here */
8844 parse_error_context (id, "Undefined variable `%s'",
8845 IDENTIFIER_POINTER (name));
8847 return error_mark_node;
8851 static_ref_err (wfl, field_id, class_type)
8852 tree wfl, field_id, class_type;
8856 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8857 IDENTIFIER_POINTER (field_id),
8858 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8861 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8862 We return something suitable to generate the field access. We also
8863 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8864 recipient's address can be null. */
8867 resolve_field_access (qual_wfl, field_decl, field_type)
8869 tree *field_decl, *field_type;
8873 tree decl, where_found, type_found;
8875 if (resolve_qualified_expression_name (qual_wfl, &decl,
8876 &where_found, &type_found))
8877 return error_mark_node;
8879 /* Resolve the LENGTH field of an array here */
8880 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
8881 && type_found && TYPE_ARRAY_P (type_found)
8882 && ! flag_emit_class_files && ! flag_emit_xref)
8884 tree length = build_java_array_length_access (where_found);
8886 build_java_arraynull_check (type_found, length, int_type_node);
8888 /* In case we're dealing with a static array, we need to
8889 initialize its class before the array length can be fetched.
8890 It's also a good time to create a DECL_RTL for the field if
8891 none already exists, otherwise if the field was declared in a
8892 class found in an external file and hasn't been (and won't
8893 be) accessed for its value, none will be created. */
8894 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8896 build_static_field_ref (where_found);
8897 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8900 /* We might have been trying to resolve field.method(). In which
8901 case, the resolution is over and decl is the answer */
8902 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
8904 else if (JDECL_P (decl))
8906 int static_final_found = 0;
8908 type_found = DECL_CONTEXT (decl);
8909 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
8910 if (FIELD_FINAL (decl) && FIELD_STATIC (decl)
8911 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
8912 && DECL_INITIAL (decl))
8914 /* When called on a FIELD_DECL of the right (primitive)
8915 type, java_complete_tree will try to substitue the decl
8916 for it's initial value. */
8917 field_ref = java_complete_tree (decl);
8918 static_final_found = 1;
8921 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8922 NULL_TREE : where_found),
8923 type_found, DECL_NAME (decl));
8924 if (field_ref == error_mark_node)
8925 return error_mark_node;
8926 if (is_static && !static_final_found
8927 && !flag_emit_class_files && !flag_emit_xref)
8928 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
8936 *field_type = (QUAL_DECL_TYPE (decl) ?
8937 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
8941 /* If NODE is an access to f static field, strip out the class
8942 initialization part and return the field decl, otherwise, return
8946 strip_out_static_field_access_decl (node)
8949 if (TREE_CODE (node) == COMPOUND_EXPR)
8951 tree op1 = TREE_OPERAND (node, 1);
8952 if (TREE_CODE (op1) == COMPOUND_EXPR)
8954 tree call = TREE_OPERAND (op1, 0);
8955 if (TREE_CODE (call) == CALL_EXPR
8956 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8957 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8958 == soft_initclass_node)
8959 return TREE_OPERAND (op1, 1);
8961 else if (JDECL_P (op1))
8967 /* 6.5.5.2: Qualified Expression Names */
8970 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
8972 tree *found_decl, *type_found, *where_found;
8974 int from_type = 0; /* Field search initiated from a type */
8975 int from_super = 0, from_cast = 0, from_qualified_this = 0;
8976 int previous_call_static = 0;
8978 tree decl = NULL_TREE, type = NULL_TREE, q;
8979 /* For certain for of inner class instantiation */
8980 tree saved_current, saved_this;
8981 #define RESTORE_THIS_AND_CURRENT_CLASS \
8982 { current_class = saved_current; current_this = saved_this;}
8984 *type_found = *where_found = NULL_TREE;
8986 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
8988 tree qual_wfl = QUAL_WFL (q);
8989 tree ret_decl; /* for EH checking */
8990 int location; /* for EH checking */
8992 /* 15.10.1 Field Access Using a Primary */
8993 switch (TREE_CODE (qual_wfl))
8996 case NEW_CLASS_EXPR:
8997 /* If the access to the function call is a non static field,
8998 build the code to access it. */
8999 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9001 decl = maybe_access_field (decl, *where_found,
9002 DECL_CONTEXT (decl));
9003 if (decl == error_mark_node)
9007 /* And code for the function call */
9008 if (complete_function_arguments (qual_wfl))
9011 /* We might have to setup a new current class and a new this
9012 for the search of an inner class, relative to the type of
9013 a expression resolved as `decl'. The current values are
9014 saved and restored shortly after */
9015 saved_current = current_class;
9016 saved_this = current_this;
9017 if (decl && TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
9019 current_class = type;
9020 current_this = decl;
9023 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
9024 CALL_USING_SUPER (qual_wfl) = 1;
9025 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
9026 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
9027 *where_found = patch_method_invocation (qual_wfl, decl, type,
9028 &is_static, &ret_decl);
9029 if (*where_found == error_mark_node)
9031 RESTORE_THIS_AND_CURRENT_CLASS;
9034 *type_found = type = QUAL_DECL_TYPE (*where_found);
9036 /* If we're creating an inner class instance, check for that
9037 an enclosing instance is in scope */
9038 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
9039 && INNER_ENCLOSING_SCOPE_CHECK (type))
9042 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
9043 lang_printable_name (type, 0),
9044 (!current_this ? "" :
9045 "; an explicit one must be provided when creating this inner class"));
9046 RESTORE_THIS_AND_CURRENT_CLASS;
9050 /* In case we had to change then to resolve a inner class
9051 instantiation using a primary qualified by a `new' */
9052 RESTORE_THIS_AND_CURRENT_CLASS;
9054 /* EH check. No check on access$<n> functions */
9056 && !OUTER_FIELD_ACCESS_IDENTIFIER_P
9057 (DECL_NAME (current_function_decl)))
9058 check_thrown_exceptions (location, ret_decl);
9060 /* If the previous call was static and this one is too,
9061 build a compound expression to hold the two (because in
9062 that case, previous function calls aren't transported as
9063 forcoming function's argument. */
9064 if (previous_call_static && is_static)
9066 decl = build (COMPOUND_EXPR, type, decl, *where_found);
9067 TREE_SIDE_EFFECTS (decl) = 1;
9071 previous_call_static = is_static;
9072 decl = *where_found;
9077 case NEW_ARRAY_EXPR:
9078 case NEW_ANONYMOUS_ARRAY_EXPR:
9079 *where_found = decl = java_complete_tree (qual_wfl);
9080 if (decl == error_mark_node)
9082 *type_found = type = QUAL_DECL_TYPE (decl);
9083 CLASS_LOADED_P (type) = 1;
9087 *where_found = decl = java_complete_tree (qual_wfl);
9088 if (decl == error_mark_node)
9090 *type_found = type = QUAL_DECL_TYPE (decl);
9094 case CONDITIONAL_EXPR:
9097 *where_found = decl = java_complete_tree (qual_wfl);
9098 if (decl == error_mark_node)
9100 *type_found = type = QUAL_DECL_TYPE (decl);
9104 /* If the access to the function call is a non static field,
9105 build the code to access it. */
9106 if (JDECL_P (decl) && !FIELD_STATIC (decl))
9108 decl = maybe_access_field (decl, *where_found, type);
9109 if (decl == error_mark_node)
9112 /* And code for the array reference expression */
9113 decl = java_complete_tree (qual_wfl);
9114 if (decl == error_mark_node)
9116 type = QUAL_DECL_TYPE (decl);
9120 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9122 if ((type = patch_string (decl)))
9124 *where_found = QUAL_RESOLUTION (q) = decl;
9125 *type_found = type = TREE_TYPE (decl);
9129 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9131 *where_found = QUAL_RESOLUTION (q) = decl;
9132 *type_found = type = TREE_TYPE (decl);
9136 /* Fix for -Wall Just go to the next statement. Don't
9141 /* If we fall here, we weren't processing a (static) function call. */
9142 previous_call_static = 0;
9144 /* It can be the keyword THIS */
9145 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9150 (wfl, "Keyword `this' used outside allowed context");
9153 if (ctxp->explicit_constructor_p)
9155 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9158 /* We have to generate code for intermediate acess */
9159 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9161 *where_found = decl = current_this;
9162 *type_found = type = QUAL_DECL_TYPE (decl);
9164 /* We're trying to access the this from somewhere else. Make sure
9165 it's allowed before doing so. */
9168 if (!enclosing_context_p (type, current_class))
9170 char *p = xstrdup (lang_printable_name (type, 0));
9171 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9173 lang_printable_name (current_class, 0));
9177 *where_found = decl = build_current_thisn (type);
9178 from_qualified_this = 1;
9185 /* 15.10.2 Accessing Superclass Members using SUPER */
9186 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9189 /* Check on the restricted use of SUPER */
9190 if (METHOD_STATIC (current_function_decl)
9191 || current_class == object_type_node)
9194 (wfl, "Keyword `super' used outside allowed context");
9197 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9198 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9199 CLASSTYPE_SUPER (current_class),
9200 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9201 *where_found = decl = java_complete_tree (node);
9202 if (decl == error_mark_node)
9204 *type_found = type = QUAL_DECL_TYPE (decl);
9205 from_super = from_type = 1;
9209 /* 15.13.1: Can't search for field name in packages, so we
9210 assume a variable/class name was meant. */
9211 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9213 tree name = resolve_package (wfl, &q);
9217 *where_found = decl = resolve_no_layout (name, qual_wfl);
9218 /* We want to be absolutely sure that the class is laid
9219 out. We're going to search something inside it. */
9220 *type_found = type = TREE_TYPE (decl);
9221 layout_class (type);
9224 /* Fix them all the way down, if any are left. */
9227 list = TREE_CHAIN (q);
9230 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9231 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9232 list = TREE_CHAIN (list);
9238 if (from_super || from_cast)
9240 ((from_cast ? qual_wfl : wfl),
9241 "No variable `%s' defined in class `%s'",
9242 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9243 lang_printable_name (type, 0));
9246 (qual_wfl, "Undefined variable or class name: `%s'",
9247 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9252 /* We have a type name. It's been already resolved when the
9253 expression was qualified. */
9254 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9256 if (!(decl = QUAL_RESOLUTION (q)))
9257 return 1; /* Error reported already */
9259 /* Sneak preview. If next we see a `new', we're facing a
9260 qualification with resulted in a type being selected
9261 instead of a field. Report the error */
9263 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9265 parse_error_context (qual_wfl, "Undefined variable `%s'",
9266 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9270 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9273 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9274 java_accstring_lookup (get_access_flags_from_decl (decl)),
9275 GET_TYPE_NAME (type),
9276 IDENTIFIER_POINTER (DECL_NAME (decl)),
9277 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9280 check_deprecation (qual_wfl, decl);
9282 type = TREE_TYPE (decl);
9285 /* We resolve and expression name */
9288 tree field_decl = NULL_TREE;
9290 /* If there exists an early resolution, use it. That occurs
9291 only once and we know that there are more things to
9292 come. Don't do that when processing something after SUPER
9293 (we need more thing to be put in place below */
9294 if (!from_super && QUAL_RESOLUTION (q))
9296 decl = QUAL_RESOLUTION (q);
9299 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9302 *where_found = current_this;
9305 static_ref_err (qual_wfl, DECL_NAME (decl),
9309 if (outer_field_access_p (current_class, decl))
9310 decl = build_outer_field_access (qual_wfl, decl);
9314 *where_found = TREE_TYPE (decl);
9315 if (TREE_CODE (*where_found) == POINTER_TYPE)
9316 *where_found = TREE_TYPE (*where_found);
9321 /* We have to search for a field, knowing the type of its
9322 container. The flag FROM_TYPE indicates that we resolved
9323 the last member of the expression as a type name, which
9324 means that for the resolution of this field, we'll look
9325 for other errors than if it was resolved as a member of
9330 tree field_decl_type; /* For layout */
9332 if (!from_type && !JREFERENCE_TYPE_P (type))
9335 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9336 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9337 lang_printable_name (type, 0),
9338 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9342 field_decl = lookup_field_wrapper (type,
9343 EXPR_WFL_NODE (qual_wfl));
9345 /* Maybe what we're trying to access an inner class. */
9348 tree ptr, inner_decl;
9350 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9351 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9354 check_inner_class_access (inner_decl, decl, qual_wfl);
9355 type = TREE_TYPE (inner_decl);
9362 if (field_decl == NULL_TREE)
9365 (qual_wfl, "No variable `%s' defined in type `%s'",
9366 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9367 GET_TYPE_NAME (type));
9370 if (field_decl == error_mark_node)
9373 /* Layout the type of field_decl, since we may need
9374 it. Don't do primitive types or loaded classes. The
9375 situation of non primitive arrays may not handled
9376 properly here. FIXME */
9377 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9378 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9380 field_decl_type = TREE_TYPE (field_decl);
9381 if (!JPRIMITIVE_TYPE_P (field_decl_type)
9382 && !CLASS_LOADED_P (field_decl_type)
9383 && !TYPE_ARRAY_P (field_decl_type))
9384 resolve_and_layout (field_decl_type, NULL_TREE);
9385 if (TYPE_ARRAY_P (field_decl_type))
9386 CLASS_LOADED_P (field_decl_type) = 1;
9388 /* Check on accessibility here */
9389 if (not_accessible_p (type, field_decl, from_super))
9393 "Can't access %s field `%s.%s' from `%s'",
9394 java_accstring_lookup
9395 (get_access_flags_from_decl (field_decl)),
9396 GET_TYPE_NAME (type),
9397 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9399 (DECL_NAME (TYPE_NAME (current_class))));
9402 check_deprecation (qual_wfl, field_decl);
9404 /* There are things to check when fields are accessed
9405 from type. There are no restrictions on a static
9406 declaration of the field when it is accessed from an
9408 is_static = FIELD_STATIC (field_decl);
9409 if (!from_super && from_type
9410 && !TYPE_INTERFACE_P (type)
9412 && (current_function_decl
9413 && METHOD_STATIC (current_function_decl)))
9415 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9418 from_cast = from_super = 0;
9420 /* It's an access from a type but it isn't static, we
9421 make it relative to `this'. */
9422 if (!is_static && from_type)
9423 decl = current_this;
9425 /* If we need to generate something to get a proper
9426 handle on what this field is accessed from, do it
9430 decl = maybe_access_field (decl, *where_found, *type_found);
9431 if (decl == error_mark_node)
9435 /* We want to keep the location were found it, and the type
9437 *where_found = decl;
9440 /* Generate the correct expression for field access from
9442 if (from_qualified_this)
9444 field_decl = build_outer_field_access (qual_wfl, field_decl);
9445 from_qualified_this = 0;
9448 /* This is the decl found and eventually the next one to
9453 type = QUAL_DECL_TYPE (decl);
9455 /* Sneak preview. If decl is qualified by a `new', report
9456 the error here to be accurate on the peculiar construct */
9458 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9459 && !JREFERENCE_TYPE_P (type))
9461 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9462 lang_printable_name (type, 0));
9466 /* `q' might have changed due to a after package resolution
9475 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9476 can't be accessed from REFERENCE (a record type). This should be
9477 used when decl is a field or a method.*/
9480 not_accessible_p (reference, member, from_super)
9481 tree reference, member;
9484 int access_flag = get_access_flags_from_decl (member);
9486 /* Inner classes are processed by check_inner_class_access */
9487 if (INNER_CLASS_TYPE_P (reference))
9490 /* Access always granted for members declared public */
9491 if (access_flag & ACC_PUBLIC)
9494 /* Check access on protected members */
9495 if (access_flag & ACC_PROTECTED)
9497 /* Access granted if it occurs from within the package
9498 containing the class in which the protected member is
9500 if (class_in_current_package (DECL_CONTEXT (member)))
9503 /* If accessed with the form `super.member', then access is granted */
9507 /* Otherwise, access is granted if occuring from the class where
9508 member is declared or a subclass of it. Find the right
9509 context to perform the check */
9510 if (PURE_INNER_CLASS_TYPE_P (reference))
9512 while (INNER_CLASS_TYPE_P (reference))
9514 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9516 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9519 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9524 /* Check access on private members. Access is granted only if it
9525 occurs from within the class in which it is declared. Exceptions
9526 are accesses from inner-classes. */
9527 if (access_flag & ACC_PRIVATE)
9528 return (current_class == DECL_CONTEXT (member) ? 0 :
9529 (INNER_CLASS_TYPE_P (current_class) ? 0 : 1));
9531 /* Default access are permitted only when occuring within the
9532 package in which the type (REFERENCE) is declared. In other words,
9533 REFERENCE is defined in the current package */
9535 return !class_in_current_package (reference);
9537 /* Otherwise, access is granted */
9541 /* Test deprecated decl access. */
9543 check_deprecation (wfl, decl)
9546 const char *file = DECL_SOURCE_FILE (decl);
9547 /* Complain if the field is deprecated and the file it was defined
9548 in isn't compiled at the same time the file which contains its
9550 if (DECL_DEPRECATED (decl)
9551 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9554 switch (TREE_CODE (decl))
9557 strcpy (the, "method");
9560 strcpy (the, "field");
9563 strcpy (the, "class");
9566 fatal ("unexpected DECL code - check_deprecation");
9568 parse_warning_context
9569 (wfl, "The %s `%s' in class `%s' has been deprecated",
9570 the, lang_printable_name (decl, 0),
9571 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9575 /* Returns 1 if class was declared in the current package, 0 otherwise */
9578 class_in_current_package (class)
9581 static tree cache = NULL_TREE;
9588 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9590 /* If the current package is empty and the name of CLASS is
9591 qualified, class isn't in the current package. If there is a
9592 current package and the name of the CLASS is not qualified, class
9593 isn't in the current package */
9594 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
9597 /* If there is not package and the name of CLASS isn't qualified,
9598 they belong to the same unnamed package */
9599 if (!ctxp->package && !qualified_flag)
9602 /* Compare the left part of the name of CLASS with the package name */
9603 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9604 if (ctxp->package == left)
9606 static int initialized_p;
9607 /* Register CACHE with the garbage collector. */
9610 ggc_add_tree_root (&cache, 1);
9620 /* This function may generate code to access DECL from WHERE. This is
9621 done only if certain conditions meet. */
9624 maybe_access_field (decl, where, type)
9625 tree decl, where, type;
9627 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9628 && !FIELD_STATIC (decl))
9629 decl = build_field_ref (where ? where : current_this,
9630 (type ? type : DECL_CONTEXT (decl)),
9635 /* Build a method invocation, by patching PATCH. If non NULL
9636 and according to the situation, PRIMARY and WHERE may be
9637 used. IS_STATIC is set to 1 if the invoked function is static. */
9640 patch_method_invocation (patch, primary, where, is_static, ret_decl)
9641 tree patch, primary, where;
9645 tree wfl = TREE_OPERAND (patch, 0);
9646 tree args = TREE_OPERAND (patch, 1);
9647 tree name = EXPR_WFL_NODE (wfl);
9649 int is_static_flag = 0;
9650 int is_super_init = 0;
9651 tree this_arg = NULL_TREE;
9653 /* Should be overriden if everything goes well. Otherwise, if
9654 something fails, it should keep this value. It stop the
9655 evaluation of a bogus assignment. See java_complete_tree,
9656 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9657 evaluating an assignment */
9658 TREE_TYPE (patch) = error_mark_node;
9660 /* Since lookup functions are messing with line numbers, save the
9662 java_parser_context_save_global ();
9664 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9666 /* Resolution of qualified name, excluding constructors */
9667 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9669 tree identifier, identifier_wfl, type, resolved;
9670 /* Extract the last IDENTIFIER of the qualified
9671 expression. This is a wfl and we will use it's location
9672 data during error report. */
9673 identifier_wfl = cut_identifier_in_qualified (wfl);
9674 identifier = EXPR_WFL_NODE (identifier_wfl);
9676 /* Given the context, IDENTIFIER is syntactically qualified
9677 as a MethodName. We need to qualify what's before */
9678 qualify_ambiguous_name (wfl);
9679 resolved = resolve_field_access (wfl, NULL, NULL);
9681 if (resolved == error_mark_node)
9682 PATCH_METHOD_RETURN_ERROR ();
9684 type = GET_SKIP_TYPE (resolved);
9685 resolve_and_layout (type, NULL_TREE);
9687 if (JPRIMITIVE_TYPE_P (type))
9691 "Can't invoke a method on primitive type `%s'",
9692 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9693 PATCH_METHOD_RETURN_ERROR ();
9696 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9697 args = nreverse (args);
9699 /* We're resolving a call from a type */
9700 if (TREE_CODE (resolved) == TYPE_DECL)
9702 if (CLASS_INTERFACE (resolved))
9706 "Can't make static reference to method `%s' in interface `%s'",
9707 IDENTIFIER_POINTER (identifier),
9708 IDENTIFIER_POINTER (name));
9709 PATCH_METHOD_RETURN_ERROR ();
9711 if (list && !METHOD_STATIC (list))
9713 char *fct_name = xstrdup (lang_printable_name (list, 0));
9716 "Can't make static reference to method `%s %s' in class `%s'",
9717 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9718 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9720 PATCH_METHOD_RETURN_ERROR ();
9724 this_arg = primary = resolved;
9726 /* IDENTIFIER_WFL will be used to report any problem further */
9727 wfl = identifier_wfl;
9729 /* Resolution of simple names, names generated after a primary: or
9733 tree class_to_search = NULL_TREE;
9734 int lc; /* Looking for Constructor */
9736 /* We search constructor in their target class */
9737 if (CALL_CONSTRUCTOR_P (patch))
9739 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9740 class_to_search = EXPR_WFL_NODE (wfl);
9741 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9742 this_identifier_node)
9743 class_to_search = NULL_TREE;
9744 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9745 super_identifier_node)
9748 if (CLASSTYPE_SUPER (current_class))
9750 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9753 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
9754 PATCH_METHOD_RETURN_ERROR ();
9758 /* Class to search is NULL if we're searching the current one */
9759 if (class_to_search)
9761 class_to_search = resolve_and_layout (class_to_search, wfl);
9763 if (!class_to_search)
9766 (wfl, "Class `%s' not found in type declaration",
9767 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9768 PATCH_METHOD_RETURN_ERROR ();
9771 /* Can't instantiate an abstract class, but we can
9772 invoke it's constructor. It's use within the `new'
9773 context is denied here. */
9774 if (CLASS_ABSTRACT (class_to_search)
9775 && TREE_CODE (patch) == NEW_CLASS_EXPR)
9778 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9779 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9780 PATCH_METHOD_RETURN_ERROR ();
9783 class_to_search = TREE_TYPE (class_to_search);
9786 class_to_search = current_class;
9789 /* This is a regular search in the local class, unless an
9790 alternate class is specified. */
9793 class_to_search = (where ? where : current_class);
9797 /* NAME is a simple identifier or comes from a primary. Search
9798 in the class whose declaration contain the method being
9800 resolve_and_layout (class_to_search, NULL_TREE);
9802 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
9803 /* Don't continue if no method were found, as the next statement
9804 can't be executed then. */
9806 PATCH_METHOD_RETURN_ERROR ();
9808 /* Check for static reference if non static methods */
9809 if (check_for_static_method_reference (wfl, patch, list,
9810 class_to_search, primary))
9811 PATCH_METHOD_RETURN_ERROR ();
9813 /* Check for inner classes creation from illegal contexts */
9814 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9815 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9816 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9819 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9820 lang_printable_name (class_to_search, 0),
9821 (!current_this ? "" :
9822 "; an explicit one must be provided when creating this inner class"));
9823 PATCH_METHOD_RETURN_ERROR ();
9826 /* Non static methods are called with the current object extra
9827 argument. If patch a `new TYPE()', the argument is the value
9828 returned by the object allocator. If method is resolved as a
9829 primary, use the primary otherwise use the current THIS. */
9830 args = nreverse (args);
9831 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
9833 this_arg = primary ? primary : current_this;
9835 /* If we're using an access method, things are different.
9836 There are two familly of cases:
9838 1) We're not generating bytecodes:
9840 - LIST is non static. It's invocation is transformed from
9841 x(a1,...,an) into this$<n>.x(a1,....an).
9842 - LIST is static. It's invocation is transformed from
9843 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9845 2) We're generating bytecodes:
9847 - LIST is non static. It's invocation is transformed from
9848 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9849 - LIST is static. It's invocation is transformed from
9850 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9852 Of course, this$<n> can be abitrary complex, ranging from
9853 this$0 (the immediate outer context) to
9854 access$0(access$0(...(this$0))).
9856 maybe_use_access_method returns a non zero value if the
9857 this_arg has to be moved into the (then generated) stub
9858 argument list. In the meantime, the selected function
9859 might have be replaced by a generated stub. */
9860 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9861 args = tree_cons (NULL_TREE, this_arg, args);
9865 /* Merge point of all resolution schemes. If we have nothing, this
9866 is an error, already signaled */
9868 PATCH_METHOD_RETURN_ERROR ();
9870 /* Check accessibility, position the is_static flag, build and
9872 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
9874 char *fct_name = xstrdup (lang_printable_name (list, 0));
9876 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
9877 java_accstring_lookup (get_access_flags_from_decl (list)),
9878 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9879 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
9880 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9882 PATCH_METHOD_RETURN_ERROR ();
9884 check_deprecation (wfl, list);
9886 /* If invoking a innerclass constructor, there are hidden parameters
9888 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9889 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9891 /* And make sure we add the accessed local variables to be saved
9892 in field aliases. */
9893 args = build_alias_initializer_parameter_list
9894 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9896 /* Secretly pass the current_this/primary as a second argument */
9897 if (primary || current_this)
9898 args = tree_cons (NULL_TREE, (primary ? primary : current_this), args);
9900 args = tree_cons (NULL_TREE, integer_zero_node, args);
9903 /* This handles the situation where a constructor invocation needs
9904 to have an enclosing context passed as a second parameter (the
9905 constructor is one of an inner class. We extract it from the
9906 current function. */
9907 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9909 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
9912 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
9914 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
9915 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
9919 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
9921 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
9922 extra_arg = java_complete_tree (extra_arg);
9924 args = tree_cons (NULL_TREE, extra_arg, args);
9927 is_static_flag = METHOD_STATIC (list);
9928 if (! METHOD_STATIC (list) && this_arg != NULL_TREE)
9929 args = tree_cons (NULL_TREE, this_arg, args);
9931 /* In the context of an explicit constructor invocation, we can't
9932 invoke any method relying on `this'. Exceptions are: we're
9933 invoking a static function, primary exists and is not the current
9934 this, we're creating a new object. */
9935 if (ctxp->explicit_constructor_p
9937 && (!primary || primary == current_this)
9938 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
9940 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9941 PATCH_METHOD_RETURN_ERROR ();
9943 java_parser_context_restore_global ();
9945 *is_static = is_static_flag;
9946 /* Sometimes, we want the decl of the selected method. Such as for
9950 patch = patch_invoke (patch, list, args);
9951 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
9953 tree finit_parms, finit_call;
9955 /* Prepare to pass hidden parameters to finit$, if any. */
9956 finit_parms = build_alias_initializer_parameter_list
9957 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
9960 build_method_invocation (build_wfl_node (finit_identifier_node),
9963 /* Generate the code used to initialize fields declared with an
9964 initialization statement and build a compound statement along
9965 with the super constructor invocation. */
9966 patch = build (COMPOUND_EXPR, void_type_node, patch,
9967 java_complete_tree (finit_call));
9968 CAN_COMPLETE_NORMALLY (patch) = 1;
9973 /* Check that we're not trying to do a static reference to a method in
9974 non static method. Return 1 if it's the case, 0 otherwise. */
9977 check_for_static_method_reference (wfl, node, method, where, primary)
9978 tree wfl, node, method, where, primary;
9980 if (METHOD_STATIC (current_function_decl)
9981 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
9983 char *fct_name = xstrdup (lang_printable_name (method, 0));
9985 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
9986 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
9987 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
9994 /* Fix the invocation of *MDECL if necessary in the case of a
9995 invocation from an inner class. *THIS_ARG might be modified
9996 appropriately and an alternative access to *MDECL might be
10000 maybe_use_access_method (is_super_init, mdecl, this_arg)
10002 tree *mdecl, *this_arg;
10005 tree md = *mdecl, ta = *this_arg;
10007 int non_static_context = !METHOD_STATIC (md);
10010 || DECL_CONTEXT (md) == current_class
10011 || !PURE_INNER_CLASS_TYPE_P (current_class)
10012 || DECL_FINIT_P (md))
10015 /* If we're calling a method found in an enclosing class, generate
10016 what it takes to retrieve the right this. Don't do that if we're
10017 invoking a static method. */
10019 if (non_static_context)
10021 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
10022 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
10024 ta = build_current_thisn (current_class);
10025 ta = build_wfl_node (ta);
10032 maybe_build_thisn_access_method (type);
10033 if (inherits_from_p (type, DECL_CONTEXT (md)))
10035 ta = build_access_to_thisn (ctx, type, 0);
10038 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
10039 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
10042 ta = java_complete_tree (ta);
10045 /* We might have to use an access method to get to MD. We can
10046 break the method access rule as far as we're not generating
10048 if (METHOD_PRIVATE (md) && flag_emit_class_files)
10050 md = build_outer_method_access_method (md);
10057 /* Returnin a non zero value indicates we were doing a non static
10058 method invokation that is now a static invocation. It will have
10059 callee displace `this' to insert it in the regular argument
10061 return (non_static_context && to_return);
10064 /* Patch an invoke expression METHOD and ARGS, based on its invocation
10068 patch_invoke (patch, method, args)
10069 tree patch, method, args;
10072 tree original_call, t, ta;
10073 tree cond = NULL_TREE;
10075 /* Last step for args: convert build-in types. If we're dealing with
10076 a new TYPE() type call, the first argument to the constructor
10077 isn't found in the incoming argument list, but delivered by
10079 t = TYPE_ARG_TYPES (TREE_TYPE (method));
10080 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
10081 t = TREE_CHAIN (t);
10082 for (ta = args; t != end_params_node && ta;
10083 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
10084 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
10085 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
10086 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
10088 /* Resolve unresolved returned type isses */
10089 t = TREE_TYPE (TREE_TYPE (method));
10090 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
10091 resolve_and_layout (TREE_TYPE (t), NULL);
10093 if (flag_emit_class_files || flag_emit_xref)
10097 tree signature = build_java_signature (TREE_TYPE (method));
10098 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
10100 case INVOKE_VIRTUAL:
10101 dtable = invoke_build_dtable (0, args);
10102 func = build_invokevirtual (dtable, method);
10105 case INVOKE_NONVIRTUAL:
10106 /* If the object for the method call is null, we throw an
10107 exception. We don't do this if the object is the current
10108 method's `this'. In other cases we just rely on an
10109 optimization pass to eliminate redundant checks. */
10110 if (TREE_VALUE (args) != current_this)
10112 /* We use a SAVE_EXPR here to make sure we only evaluate
10113 the new `self' expression once. */
10114 tree save_arg = save_expr (TREE_VALUE (args));
10115 TREE_VALUE (args) = save_arg;
10116 cond = build (EQ_EXPR, boolean_type_node, save_arg,
10117 null_pointer_node);
10119 /* Fall through. */
10122 case INVOKE_STATIC:
10123 func = build_known_method_ref (method, TREE_TYPE (method),
10124 DECL_CONTEXT (method),
10128 case INVOKE_INTERFACE:
10129 dtable = invoke_build_dtable (1, args);
10130 func = build_invokeinterface (dtable, method);
10134 fatal ("internal error - unknown invocation_mode result");
10137 /* Ensure self_type is initialized, (invokestatic). FIXME */
10138 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10141 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10142 TREE_OPERAND (patch, 0) = func;
10143 TREE_OPERAND (patch, 1) = args;
10144 original_call = patch;
10146 /* We're processing a `new TYPE ()' form. New is called and its
10147 returned value is the first argument to the constructor. We build
10148 a COMPOUND_EXPR and use saved expression so that the overall NEW
10149 expression value is a pointer to a newly created and initialized
10151 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10153 tree class = DECL_CONTEXT (method);
10154 tree c1, saved_new, size, new;
10155 if (flag_emit_class_files || flag_emit_xref)
10157 TREE_TYPE (patch) = build_pointer_type (class);
10160 if (!TYPE_SIZE (class))
10161 safe_layout_class (class);
10162 size = size_in_bytes (class);
10163 new = build (CALL_EXPR, promote_type (class),
10164 build_address_of (alloc_object_node),
10165 tree_cons (NULL_TREE, build_class_ref (class),
10166 build_tree_list (NULL_TREE,
10167 size_in_bytes (class))),
10169 saved_new = save_expr (new);
10170 c1 = build_tree_list (NULL_TREE, saved_new);
10171 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10172 TREE_OPERAND (original_call, 1) = c1;
10173 TREE_SET_CODE (original_call, CALL_EXPR);
10174 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10177 /* If COND is set, then we are building a check to see if the object
10179 if (cond != NULL_TREE)
10181 /* We have to make the `then' branch a compound expression to
10182 make the types turn out right. This seems bizarre. */
10183 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10184 build (COMPOUND_EXPR, TREE_TYPE (patch),
10185 build (CALL_EXPR, void_type_node,
10186 build_address_of (soft_nullpointer_node),
10187 NULL_TREE, NULL_TREE),
10188 (FLOAT_TYPE_P (TREE_TYPE (patch))
10189 ? build_real (TREE_TYPE (patch), dconst0)
10190 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10191 integer_zero_node))),
10193 TREE_SIDE_EFFECTS (patch) = 1;
10200 invocation_mode (method, super)
10204 int access = get_access_flags_from_decl (method);
10207 return INVOKE_SUPER;
10209 if (access & ACC_STATIC)
10210 return INVOKE_STATIC;
10212 /* We have to look for a constructor before we handle nonvirtual
10213 calls; otherwise the constructor will look nonvirtual. */
10214 if (DECL_CONSTRUCTOR_P (method))
10215 return INVOKE_STATIC;
10217 if (access & ACC_FINAL || access & ACC_PRIVATE)
10218 return INVOKE_NONVIRTUAL;
10220 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10221 return INVOKE_NONVIRTUAL;
10223 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10224 return INVOKE_INTERFACE;
10226 return INVOKE_VIRTUAL;
10229 /* Retrieve a refined list of matching methods. It covers the step
10230 15.11.2 (Compile-Time Step 2) */
10233 lookup_method_invoke (lc, cl, class, name, arg_list)
10236 tree class, name, arg_list;
10238 tree atl = end_params_node; /* Arg Type List */
10239 tree method, signature, list, node;
10240 const char *candidates; /* Used for error report */
10243 /* Fix the arguments */
10244 for (node = arg_list; node; node = TREE_CHAIN (node))
10246 tree current_arg = TREE_TYPE (TREE_VALUE (node));
10247 /* Non primitive type may have to be resolved */
10248 if (!JPRIMITIVE_TYPE_P (current_arg))
10249 resolve_and_layout (current_arg, NULL_TREE);
10251 if (TREE_CODE (current_arg) == RECORD_TYPE)
10252 current_arg = promote_type (current_arg);
10253 atl = tree_cons (NULL_TREE, current_arg, atl);
10256 /* Presto. If we're dealing with an anonymous class and a
10257 constructor call, generate the right constructor now, since we
10258 know the arguments' types. */
10260 if (lc && ANONYMOUS_CLASS_P (class))
10261 craft_constructor (TYPE_NAME (class), atl);
10263 /* Find all candidates and then refine the list, searching for the
10264 most specific method. */
10265 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10266 list = find_most_specific_methods_list (list);
10267 if (list && !TREE_CHAIN (list))
10268 return TREE_VALUE (list);
10270 /* Issue an error. List candidates if any. Candidates are listed
10271 only if accessible (non accessible methods may end-up here for
10272 the sake of a better error report). */
10277 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10278 for (current = list; current; current = TREE_CHAIN (current))
10280 tree cm = TREE_VALUE (current);
10281 char string [4096];
10282 if (!cm || not_accessible_p (class, cm, 0))
10285 (string, " `%s' in `%s'%s",
10286 get_printable_method_name (cm),
10287 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10288 (TREE_CHAIN (current) ? "\n" : ""));
10289 obstack_grow (&temporary_obstack, string, strlen (string));
10291 obstack_1grow (&temporary_obstack, '\0');
10292 candidates = obstack_finish (&temporary_obstack);
10294 /* Issue the error message */
10295 method = make_node (FUNCTION_TYPE);
10296 TYPE_ARG_TYPES (method) = atl;
10297 signature = build_java_argument_signature (method);
10298 dup = xstrdup (lang_printable_name (class, 0));
10299 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10300 (lc ? "constructor" : "method"),
10301 (lc ? dup : IDENTIFIER_POINTER (name)),
10302 IDENTIFIER_POINTER (signature), dup,
10303 (candidates ? candidates : ""));
10308 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10309 when we're looking for a constructor. */
10312 find_applicable_accessible_methods_list (lc, class, name, arglist)
10314 tree class, name, arglist;
10316 static struct hash_table t, *searched_classes = NULL;
10317 static int search_not_done = 0;
10318 tree list = NULL_TREE, all_list = NULL_TREE;
10320 /* Check the hash table to determine if this class has been searched
10322 if (searched_classes)
10324 if (hash_lookup (searched_classes,
10325 (const hash_table_key) class, FALSE, NULL))
10330 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10331 java_hash_compare_tree_node);
10332 searched_classes = &t;
10336 hash_lookup (searched_classes,
10337 (const hash_table_key) class, TRUE, NULL);
10339 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10341 load_class (class, 1);
10342 safe_layout_class (class);
10345 /* Search interfaces */
10346 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10347 && CLASS_INTERFACE (TYPE_NAME (class)))
10350 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10351 search_applicable_methods_list (lc, TYPE_METHODS (class),
10352 name, arglist, &list, &all_list);
10353 n = TREE_VEC_LENGTH (basetype_vec);
10354 for (i = 1; i < n; i++)
10356 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10359 rlist = find_applicable_accessible_methods_list (lc, t, name,
10361 list = chainon (rlist, list);
10364 /* Search classes */
10368 int seen_inner_class = 0;
10369 search_applicable_methods_list (lc, TYPE_METHODS (class),
10370 name, arglist, &list, &all_list);
10372 /* We must search all interfaces of this class */
10375 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10376 int n = TREE_VEC_LENGTH (basetype_vec), i;
10377 for (i = 1; i < n; i++)
10379 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10380 if (t != object_type_node)
10383 = find_applicable_accessible_methods_list (lc, t,
10385 list = chainon (rlist, list);
10390 /* Search enclosing context of inner classes before looking
10392 while (!lc && INNER_CLASS_TYPE_P (class))
10395 seen_inner_class = 1;
10396 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
10397 rlist = find_applicable_accessible_methods_list (lc, class,
10399 list = chainon (rlist, list);
10402 if (!lc && seen_inner_class
10403 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10404 class = CLASSTYPE_SUPER (sc);
10408 /* Search superclass */
10409 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10412 class = CLASSTYPE_SUPER (class);
10413 rlist = find_applicable_accessible_methods_list (lc, class,
10415 list = chainon (rlist, list);
10421 /* We're done. Reset the searched classes list and finally search
10422 java.lang.Object if it wasn't searched already. */
10423 if (!search_not_done)
10426 && TYPE_METHODS (object_type_node)
10427 && !hash_lookup (searched_classes,
10428 (const hash_table_key) object_type_node,
10431 search_applicable_methods_list (lc,
10432 TYPE_METHODS (object_type_node),
10433 name, arglist, &list, &all_list);
10435 hash_table_free (searched_classes);
10436 searched_classes = NULL;
10439 /* Either return the list obtained or all selected (but
10440 inaccessible) methods for better error report. */
10441 return (!list ? all_list : list);
10444 /* Effectively search for the appropriate method in method */
10447 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
10449 tree method, name, arglist;
10450 tree *list, *all_list;
10452 for (; method; method = TREE_CHAIN (method))
10454 /* When dealing with constructor, stop here, otherwise search
10456 if (lc && !DECL_CONSTRUCTOR_P (method))
10458 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10459 || (GET_METHOD_NAME (method) != name)))
10462 if (argument_types_convertible (method, arglist))
10464 /* Retain accessible methods only */
10465 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10467 *list = tree_cons (NULL_TREE, method, *list);
10469 /* Also retain all selected method here */
10470 *all_list = tree_cons (NULL_TREE, method, *list);
10475 /* 15.11.2.2 Choose the Most Specific Method */
10478 find_most_specific_methods_list (list)
10482 int abstract, candidates;
10483 tree current, new_list = NULL_TREE;
10484 for (current = list; current; current = TREE_CHAIN (current))
10487 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10489 for (method = list; method; method = TREE_CHAIN (method))
10491 tree method_v, current_v;
10492 /* Don't test a method against itself */
10493 if (method == current)
10496 method_v = TREE_VALUE (method);
10497 current_v = TREE_VALUE (current);
10499 /* Compare arguments and location where methods where declared */
10500 if (argument_types_convertible (method_v, current_v))
10502 if (valid_method_invocation_conversion_p
10503 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10504 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10505 && enclosing_context_p (DECL_CONTEXT (method_v),
10506 DECL_CONTEXT (current_v))))
10508 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10509 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10510 max = (v > max ? v : max);
10516 /* Review the list and select the maximally specific methods */
10517 for (current = list, abstract = -1, candidates = -1;
10518 current; current = TREE_CHAIN (current))
10519 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10521 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10522 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10526 /* If we have several and they're all abstract, just pick the
10528 if (candidates > 0 && (candidates == abstract))
10530 new_list = nreverse (new_list);
10531 TREE_CHAIN (new_list) = NULL_TREE;
10534 /* We have several, we couldn't find a most specific, all but one are
10535 abstract, we pick the only non abstract one. */
10536 if (candidates > 0 && !max && (candidates == abstract+1))
10538 for (current = new_list; current; current = TREE_CHAIN (current))
10539 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10541 TREE_CHAIN (current) = NULL_TREE;
10542 new_list = current;
10546 /* If we can't find one, lower expectations and try to gather multiple
10547 maximally specific methods */
10548 while (!new_list && max)
10552 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10553 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10560 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
10561 converted by method invocation conversion (5.3) to the type of the
10562 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10563 to change less often than M1. */
10566 argument_types_convertible (m1, m2_or_arglist)
10567 tree m1, m2_or_arglist;
10569 static tree m2_arg_value = NULL_TREE;
10570 static tree m2_arg_cache = NULL_TREE;
10571 static int initialized_p;
10573 register tree m1_arg, m2_arg;
10575 /* Register M2_ARG_VALUE and M2_ARG_CACHE with the garbage
10577 if (!initialized_p)
10579 ggc_add_tree_root (&m2_arg_value, 1);
10580 ggc_add_tree_root (&m2_arg_cache, 1);
10584 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
10586 if (m2_arg_value == m2_or_arglist)
10587 m2_arg = m2_arg_cache;
10590 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10592 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10594 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10595 if (!METHOD_STATIC (m2_or_arglist))
10596 m2_arg = TREE_CHAIN (m2_arg);
10599 m2_arg = m2_or_arglist;
10601 m2_arg_value = m2_or_arglist;
10602 m2_arg_cache = m2_arg;
10605 while (m1_arg != end_params_node && m2_arg != end_params_node)
10607 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
10608 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10609 TREE_VALUE (m2_arg)))
10611 m1_arg = TREE_CHAIN (m1_arg);
10612 m2_arg = TREE_CHAIN (m2_arg);
10614 return m1_arg == end_params_node && m2_arg == end_params_node;
10617 /* Qualification routines */
10620 qualify_ambiguous_name (id)
10623 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10624 saved_current_class;
10625 int again, super_found = 0, this_found = 0, new_array_found = 0;
10628 /* We first qualify the first element, then derive qualification of
10629 others based on the first one. If the first element is qualified
10630 by a resolution (field or type), this resolution is stored in the
10631 QUAL_RESOLUTION of the qual element being examined. We need to
10632 save the current_class since the use of SUPER might change the
10634 saved_current_class = current_class;
10635 qual = EXPR_WFL_QUALIFICATION (id);
10638 /* Simple qualified expression feature a qual_wfl that is a
10639 WFL. Expression derived from a primary feature more complicated
10640 things like a CALL_EXPR. Expression from primary need to be
10641 worked out to extract the part on which the qualification will
10643 qual_wfl = QUAL_WFL (qual);
10644 switch (TREE_CODE (qual_wfl))
10647 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10648 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10650 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10651 qual_wfl = QUAL_WFL (qual);
10654 case NEW_ARRAY_EXPR:
10655 case NEW_ANONYMOUS_ARRAY_EXPR:
10656 qual = TREE_CHAIN (qual);
10657 again = new_array_found = 1;
10661 case NEW_CLASS_EXPR:
10662 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10665 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10666 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10669 qual = TREE_CHAIN (qual);
10670 qual_wfl = QUAL_WFL (qual);
10672 case CLASS_LITERAL:
10673 qual = TREE_CHAIN (qual);
10674 qual_wfl = QUAL_WFL (qual);
10677 /* Fix for -Wall. Just break doing nothing */
10681 ptr_type = current_class;
10683 code = TREE_CODE (qual_wfl);
10685 /* Pos evaluation: non WFL leading expression nodes */
10686 if (code == CONVERT_EXPR
10687 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10688 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10690 else if (code == INTEGER_CST)
10693 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
10694 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10695 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10697 else if (code == TREE_LIST)
10698 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10700 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10701 || code == PLUS_EXPR)
10703 qual = TREE_CHAIN (qual);
10704 qual_wfl = QUAL_WFL (qual);
10709 name = EXPR_WFL_NODE (qual_wfl);
10712 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10717 /* If we have a THIS (from a primary), we set the context accordingly */
10718 if (name == this_identifier_node)
10720 /* This isn't really elegant. One more added irregularity
10721 before I start using COMPONENT_REF (hopefully very soon.) */
10722 if (TREE_CODE (TREE_PURPOSE (qual)) == ARRAY_REF
10723 && TREE_CODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10724 EXPR_WITH_FILE_LOCATION
10725 && EXPR_WFL_NODE (TREE_OPERAND (TREE_PURPOSE (qual), 0)) ==
10726 this_identifier_node)
10728 qual = TREE_OPERAND (TREE_PURPOSE (qual), 0);
10729 qual = EXPR_WFL_QUALIFICATION (qual);
10731 qual = TREE_CHAIN (qual);
10732 qual_wfl = QUAL_WFL (qual);
10733 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10736 name = EXPR_WFL_NODE (qual_wfl);
10739 /* If we have a SUPER, we set the context accordingly */
10740 if (name == super_identifier_node)
10742 current_class = CLASSTYPE_SUPER (ptr_type);
10743 /* Check that there is such a thing as a super class. If not,
10744 return. The error will be caught later on, during the
10746 if (!current_class)
10748 current_class = saved_current_class;
10751 qual = TREE_CHAIN (qual);
10752 /* Do one more interation to set things up */
10753 super_found = again = 1;
10757 /* If name appears within the scope of a local variable declaration
10758 or parameter declaration, then it is an expression name. We don't
10759 carry this test out if we're in the context of the use of SUPER
10761 if (!this_found && !super_found
10762 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10763 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
10765 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10766 QUAL_RESOLUTION (qual) = decl;
10769 /* If within the class/interface NAME was found to be used there
10770 exists a (possibly inherited) field named NAME, then this is an
10771 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10772 address length, it is OK. */
10773 else if ((decl = lookup_field_wrapper (ptr_type, name))
10774 || (new_array_found && name == length_identifier_node))
10776 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10777 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
10780 /* We reclassify NAME as yielding to a type name resolution if:
10781 - NAME is a class/interface declared within the compilation
10782 unit containing NAME,
10783 - NAME is imported via a single-type-import declaration,
10784 - NAME is declared in an another compilation unit of the package
10785 of the compilation unit containing NAME,
10786 - NAME is declared by exactly on type-import-on-demand declaration
10787 of the compilation unit containing NAME.
10788 - NAME is actually a STRING_CST. */
10789 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10790 || (decl = resolve_and_layout (name, NULL_TREE)))
10792 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10793 QUAL_RESOLUTION (qual) = decl;
10796 /* Method call, array references and cast are expression name */
10797 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
10798 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10799 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
10800 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10802 /* Check here that NAME isn't declared by more than one
10803 type-import-on-demand declaration of the compilation unit
10804 containing NAME. FIXME */
10806 /* Otherwise, NAME is reclassified as a package name */
10808 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10810 /* Propagate the qualification accross other components of the
10812 for (qual = TREE_CHAIN (qual); qual;
10813 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10815 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10816 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10818 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10821 /* Store the global qualification for the ambiguous part of ID back
10823 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10824 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10825 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10826 RESOLVE_TYPE_NAME_P (id) = 1;
10827 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10828 RESOLVE_PACKAGE_NAME_P (id) = 1;
10830 /* Restore the current class */
10831 current_class = saved_current_class;
10835 breakdown_qualified (left, right, source)
10836 tree *left, *right, source;
10839 int l = IDENTIFIER_LENGTH (source);
10841 base = alloca (l + 1);
10842 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
10844 /* Breakdown NAME into REMAINDER . IDENTIFIER */
10846 while (*p != '.' && p != base)
10849 /* We didn't find a '.'. Return an error */
10855 *right = get_identifier (p+1);
10856 *left = get_identifier (base);
10861 /* Patch tree nodes in a function body. When a BLOCK is found, push
10862 local variable decls if present.
10863 Same as java_complete_lhs, but does resolve static finals to values. */
10866 java_complete_tree (node)
10869 node = java_complete_lhs (node);
10870 if (JDECL_P (node) && FIELD_STATIC (node) && FIELD_FINAL (node)
10871 && DECL_INITIAL (node) != NULL_TREE
10872 && !flag_emit_xref)
10874 tree value = DECL_INITIAL (node);
10875 DECL_INITIAL (node) = NULL_TREE;
10876 value = fold_constant_for_init (value, node);
10877 DECL_INITIAL (node) = value;
10878 if (value != NULL_TREE)
10880 /* fold_constant_for_init sometimes widen the original type
10881 of the constant (i.e. byte to int.) It's not desirable,
10882 especially if NODE is a function argument. */
10883 if (TREE_CODE (value) == INTEGER_CST
10884 && TREE_TYPE (node) != TREE_TYPE (value))
10885 return convert (TREE_TYPE (node), value);
10894 java_stabilize_reference (node)
10897 if (TREE_CODE (node) == COMPOUND_EXPR)
10899 tree op0 = TREE_OPERAND (node, 0);
10900 tree op1 = TREE_OPERAND (node, 1);
10901 TREE_OPERAND (node, 0) = save_expr (op0);
10902 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
10905 return stabilize_reference (node);
10908 /* Patch tree nodes in a function body. When a BLOCK is found, push
10909 local variable decls if present.
10910 Same as java_complete_tree, but does not resolve static finals to values. */
10913 java_complete_lhs (node)
10916 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
10919 /* CONVERT_EXPR always has its type set, even though it needs to be
10921 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
10924 /* The switch block implements cases processing container nodes
10925 first. Contained nodes are always written back. Leaves come
10926 next and return a value. */
10927 switch (TREE_CODE (node))
10931 /* 1- Block section.
10932 Set the local values on decl names so we can identify them
10933 faster when they're referenced. At that stage, identifiers
10934 are legal so we don't check for declaration errors. */
10935 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10937 DECL_CONTEXT (cn) = current_function_decl;
10938 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
10940 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
10941 CAN_COMPLETE_NORMALLY (node) = 1;
10944 tree stmt = BLOCK_EXPR_BODY (node);
10946 int error_seen = 0;
10947 if (TREE_CODE (stmt) == COMPOUND_EXPR)
10949 /* Re-order from (((A; B); C); ...; Z) to
10950 (A; (B; (C ; (...; Z)))).
10951 This makes it easier to scan the statements left-to-right
10952 without using recursion (which might overflow the stack
10953 if the block has many statements. */
10956 tree left = TREE_OPERAND (stmt, 0);
10957 if (TREE_CODE (left) != COMPOUND_EXPR)
10959 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
10960 TREE_OPERAND (left, 1) = stmt;
10963 BLOCK_EXPR_BODY (node) = stmt;
10966 /* Now do the actual complete, without deep recursion for
10968 ptr = &BLOCK_EXPR_BODY (node);
10969 while (TREE_CODE (*ptr) == COMPOUND_EXPR
10970 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
10972 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
10973 tree *next = &TREE_OPERAND (*ptr, 1);
10974 TREE_OPERAND (*ptr, 0) = cur;
10975 if (cur == empty_stmt_node)
10977 /* Optimization; makes it easier to detect empty bodies.
10978 Most useful for <clinit> with all-constant initializer. */
10982 if (TREE_CODE (cur) == ERROR_MARK)
10984 else if (! CAN_COMPLETE_NORMALLY (cur))
10989 if (TREE_CODE (wfl_op2) == BLOCK)
10990 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
10991 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
10992 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
10996 if (TREE_CODE (wfl_op2) != CASE_EXPR
10997 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
10998 unreachable_stmt_error (*ptr);
11002 *ptr = java_complete_tree (*ptr);
11004 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
11005 return error_mark_node;
11006 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
11008 /* Turn local bindings to null */
11009 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
11010 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
11012 TREE_TYPE (node) = void_type_node;
11015 /* 2- They are expressions but ultimately deal with statements */
11018 wfl_op1 = TREE_OPERAND (node, 0);
11019 COMPLETE_CHECK_OP_0 (node);
11020 /* 14.19 A throw statement cannot complete normally. */
11021 CAN_COMPLETE_NORMALLY (node) = 0;
11022 return patch_throw_statement (node, wfl_op1);
11024 case SYNCHRONIZED_EXPR:
11025 wfl_op1 = TREE_OPERAND (node, 0);
11026 return patch_synchronized_statement (node, wfl_op1);
11029 return patch_try_statement (node);
11031 case TRY_FINALLY_EXPR:
11032 COMPLETE_CHECK_OP_0 (node);
11033 COMPLETE_CHECK_OP_1 (node);
11034 CAN_COMPLETE_NORMALLY (node)
11035 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
11036 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
11037 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
11040 case CLEANUP_POINT_EXPR:
11041 COMPLETE_CHECK_OP_0 (node);
11042 TREE_TYPE (node) = void_type_node;
11043 CAN_COMPLETE_NORMALLY (node) =
11044 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11047 case WITH_CLEANUP_EXPR:
11048 COMPLETE_CHECK_OP_0 (node);
11049 COMPLETE_CHECK_OP_2 (node);
11050 CAN_COMPLETE_NORMALLY (node) =
11051 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
11052 TREE_TYPE (node) = void_type_node;
11055 case LABELED_BLOCK_EXPR:
11056 PUSH_LABELED_BLOCK (node);
11057 if (LABELED_BLOCK_BODY (node))
11058 COMPLETE_CHECK_OP_1 (node);
11059 TREE_TYPE (node) = void_type_node;
11060 POP_LABELED_BLOCK ();
11062 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
11064 LABELED_BLOCK_BODY (node) = NULL_TREE;
11065 CAN_COMPLETE_NORMALLY (node) = 1;
11067 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
11068 CAN_COMPLETE_NORMALLY (node) = 1;
11071 case EXIT_BLOCK_EXPR:
11072 /* We don't complete operand 1, because it's the return value of
11073 the EXIT_BLOCK_EXPR which doesn't exist it Java */
11074 return patch_bc_statement (node);
11077 cn = java_complete_tree (TREE_OPERAND (node, 0));
11078 if (cn == error_mark_node)
11081 /* First, the case expression must be constant. Values of final
11082 fields are accepted. */
11084 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
11085 && JDECL_P (TREE_OPERAND (cn, 1))
11086 && FIELD_FINAL (TREE_OPERAND (cn, 1))
11087 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
11089 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
11090 TREE_OPERAND (cn, 1));
11093 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
11095 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11096 parse_error_context (node, "Constant expression required");
11097 return error_mark_node;
11100 nn = ctxp->current_loop;
11102 /* It must be assignable to the type of the switch expression. */
11103 if (!try_builtin_assignconv (NULL_TREE,
11104 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
11106 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11107 parse_error_context
11109 "Incompatible type for case. Can't convert `%s' to `int'",
11110 lang_printable_name (TREE_TYPE (cn), 0));
11111 return error_mark_node;
11114 cn = fold (convert (int_type_node, cn));
11116 /* Multiple instance of a case label bearing the same
11117 value is checked during code generation. The case
11118 expression is allright so far. */
11119 if (TREE_CODE (cn) == VAR_DECL)
11120 cn = DECL_INITIAL (cn);
11121 TREE_OPERAND (node, 0) = cn;
11122 TREE_TYPE (node) = void_type_node;
11123 CAN_COMPLETE_NORMALLY (node) = 1;
11124 TREE_SIDE_EFFECTS (node) = 1;
11128 nn = ctxp->current_loop;
11129 /* Only one default label is allowed per switch statement */
11130 if (SWITCH_HAS_DEFAULT (nn))
11132 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11133 parse_error_context (wfl_operator,
11134 "Duplicate case label: `default'");
11135 return error_mark_node;
11138 SWITCH_HAS_DEFAULT (nn) = 1;
11139 TREE_TYPE (node) = void_type_node;
11140 TREE_SIDE_EFFECTS (node) = 1;
11141 CAN_COMPLETE_NORMALLY (node) = 1;
11147 /* Check whether the loop was enclosed in a labeled
11148 statement. If not, create one, insert the loop in it and
11150 nn = patch_loop_statement (node);
11152 /* Anyways, walk the body of the loop */
11153 if (TREE_CODE (node) == LOOP_EXPR)
11154 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11155 /* Switch statement: walk the switch expression and the cases */
11157 node = patch_switch_statement (node);
11159 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11160 nn = error_mark_node;
11163 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11164 /* If we returned something different, that's because we
11165 inserted a label. Pop the label too. */
11168 if (CAN_COMPLETE_NORMALLY (node))
11169 CAN_COMPLETE_NORMALLY (nn) = 1;
11170 POP_LABELED_BLOCK ();
11177 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11178 return patch_exit_expr (node);
11182 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11183 if (TREE_OPERAND (node, 0) == error_mark_node)
11184 return error_mark_node;
11185 /* then-else branches */
11186 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11187 if (TREE_OPERAND (node, 1) == error_mark_node)
11188 return error_mark_node;
11189 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11190 if (TREE_OPERAND (node, 2) == error_mark_node)
11191 return error_mark_node;
11192 return patch_if_else_statement (node);
11195 case CONDITIONAL_EXPR:
11197 wfl_op1 = TREE_OPERAND (node, 0);
11198 COMPLETE_CHECK_OP_0 (node);
11199 wfl_op2 = TREE_OPERAND (node, 1);
11200 COMPLETE_CHECK_OP_1 (node);
11201 wfl_op3 = TREE_OPERAND (node, 2);
11202 COMPLETE_CHECK_OP_2 (node);
11203 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11205 /* 3- Expression section */
11206 case COMPOUND_EXPR:
11207 wfl_op2 = TREE_OPERAND (node, 1);
11208 TREE_OPERAND (node, 0) = nn =
11209 java_complete_tree (TREE_OPERAND (node, 0));
11210 if (wfl_op2 == empty_stmt_node)
11211 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11214 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11216 /* An unreachable condition in a do-while statement
11217 is *not* (technically) an unreachable statement. */
11219 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11220 nn = EXPR_WFL_NODE (nn);
11221 if (TREE_CODE (nn) != EXIT_EXPR)
11223 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11224 parse_error_context (wfl_operator, "Unreachable statement");
11227 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11228 if (TREE_OPERAND (node, 1) == error_mark_node)
11229 return error_mark_node;
11230 CAN_COMPLETE_NORMALLY (node)
11231 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
11233 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11237 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11238 return patch_return (node);
11240 case EXPR_WITH_FILE_LOCATION:
11241 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11242 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11245 node = resolve_expression_name (node, NULL);
11246 if (node == error_mark_node)
11248 /* Keep line number information somewhere were it doesn't
11249 disrupt the completion process. */
11250 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11252 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11253 TREE_OPERAND (node, 1) = wfl;
11255 CAN_COMPLETE_NORMALLY (node) = 1;
11260 int save_lineno = lineno;
11261 lineno = EXPR_WFL_LINENO (node);
11262 body = java_complete_tree (EXPR_WFL_NODE (node));
11263 lineno = save_lineno;
11264 EXPR_WFL_NODE (node) = body;
11265 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11266 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11267 if (body == empty_stmt_node)
11269 /* Optimization; makes it easier to detect empty bodies. */
11272 if (body == error_mark_node)
11274 /* Its important for the evaluation of assignment that
11275 this mark on the TREE_TYPE is propagated. */
11276 TREE_TYPE (node) = error_mark_node;
11277 return error_mark_node;
11280 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11285 case NEW_ARRAY_EXPR:
11286 /* Patch all the dimensions */
11288 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11290 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11291 tree dim = convert (int_type_node,
11292 java_complete_tree (TREE_VALUE (cn)));
11293 if (dim == error_mark_node)
11300 TREE_VALUE (cn) = dim;
11301 /* Setup the location of the current dimension, for
11302 later error report. */
11303 TREE_PURPOSE (cn) =
11304 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11305 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11308 /* They complete the array creation expression, if no errors
11310 CAN_COMPLETE_NORMALLY (node) = 1;
11311 return (flag ? error_mark_node
11312 : force_evaluation_order (patch_newarray (node)));
11314 case NEW_ANONYMOUS_ARRAY_EXPR:
11315 /* Create the array type if necessary. */
11316 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11318 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11319 if (!(type = resolve_type_during_patch (type)))
11320 return error_mark_node;
11321 type = build_array_from_name (type, NULL_TREE,
11322 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11323 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11325 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11326 ANONYMOUS_ARRAY_INITIALIZER (node));
11327 if (node == error_mark_node)
11328 return error_mark_node;
11329 CAN_COMPLETE_NORMALLY (node) = 1;
11332 case NEW_CLASS_EXPR:
11334 /* Complete function's argument(s) first */
11335 if (complete_function_arguments (node))
11336 return error_mark_node;
11339 tree decl, wfl = TREE_OPERAND (node, 0);
11340 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11342 node = patch_method_invocation (node, NULL_TREE,
11343 NULL_TREE, 0, &decl);
11344 if (node == error_mark_node)
11345 return error_mark_node;
11347 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11348 /* If we call this(...), register signature and positions */
11350 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11351 tree_cons (wfl, decl,
11352 DECL_CONSTRUCTOR_CALLS (current_function_decl));
11353 CAN_COMPLETE_NORMALLY (node) = 1;
11354 return force_evaluation_order (node);
11358 /* Save potential wfls */
11359 wfl_op1 = TREE_OPERAND (node, 0);
11360 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11362 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11363 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11364 && DECL_INITIAL (nn) != NULL_TREE)
11368 value = fold_constant_for_init (nn, nn);
11370 if (value != NULL_TREE)
11372 tree type = TREE_TYPE (value);
11373 if (JPRIMITIVE_TYPE_P (type) ||
11374 (type == string_ptr_type_node && ! flag_emit_class_files))
11375 return empty_stmt_node;
11377 if (! flag_emit_class_files)
11378 DECL_INITIAL (nn) = NULL_TREE;
11380 wfl_op2 = TREE_OPERAND (node, 1);
11382 if (TREE_OPERAND (node, 0) == error_mark_node)
11383 return error_mark_node;
11385 flag = COMPOUND_ASSIGN_P (wfl_op2);
11388 /* This might break when accessing outer field from inner
11389 class. TESTME, FIXME */
11390 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
11392 /* Hand stablize the lhs on both places */
11393 TREE_OPERAND (node, 0) = lvalue;
11394 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11395 (flag_emit_class_files ? lvalue : save_expr (lvalue));
11397 /* 15.25.2.a: Left hand is not an array access. FIXME */
11398 /* Now complete the RHS. We write it back later on. */
11399 nn = java_complete_tree (TREE_OPERAND (node, 1));
11401 if ((cn = patch_string (nn)))
11404 /* The last part of the rewrite for E1 op= E2 is to have
11405 E1 = (T)(E1 op E2), with T being the type of E1. */
11406 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11407 TREE_TYPE (lvalue), nn));
11409 /* 15.25.2.b: Left hand is an array access. FIXME */
11412 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
11413 function to complete this RHS. Note that a NEW_ARRAY_INIT
11414 might have been already fully expanded if created as a result
11415 of processing an anonymous array initializer. We avoid doing
11416 the operation twice by testing whether the node already bears
11418 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
11419 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
11420 TREE_OPERAND (node, 1));
11421 /* Otherwise we simply complete the RHS */
11423 nn = java_complete_tree (TREE_OPERAND (node, 1));
11425 if (nn == error_mark_node)
11426 return error_mark_node;
11428 /* Write back the RHS as we evaluated it. */
11429 TREE_OPERAND (node, 1) = nn;
11431 /* In case we're handling = with a String as a RHS, we need to
11432 produce a String out of the RHS (it might still be a
11433 STRING_CST or a StringBuffer at this stage */
11434 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11435 TREE_OPERAND (node, 1) = nn;
11437 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11438 TREE_OPERAND (node, 1))))
11440 /* We return error_mark_node if outer_field_access_fix
11441 detects we write into a final. */
11442 if (nn == error_mark_node)
11443 return error_mark_node;
11448 node = patch_assignment (node, wfl_op1, wfl_op2);
11449 /* Reorganize the tree if necessary. */
11450 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11451 || JSTRING_P (TREE_TYPE (node))))
11452 node = java_refold (node);
11455 CAN_COMPLETE_NORMALLY (node) = 1;
11467 case TRUNC_MOD_EXPR:
11468 case TRUNC_DIV_EXPR:
11470 case TRUTH_ANDIF_EXPR:
11471 case TRUTH_ORIF_EXPR:
11478 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11479 knows how to handle those cases. */
11480 wfl_op1 = TREE_OPERAND (node, 0);
11481 wfl_op2 = TREE_OPERAND (node, 1);
11483 CAN_COMPLETE_NORMALLY (node) = 1;
11484 /* Don't complete string nodes if dealing with the PLUS operand. */
11485 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
11487 nn = java_complete_tree (wfl_op1);
11488 if (nn == error_mark_node)
11489 return error_mark_node;
11491 TREE_OPERAND (node, 0) = nn;
11493 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
11495 nn = java_complete_tree (wfl_op2);
11496 if (nn == error_mark_node)
11497 return error_mark_node;
11499 TREE_OPERAND (node, 1) = nn;
11501 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
11503 case INSTANCEOF_EXPR:
11504 wfl_op1 = TREE_OPERAND (node, 0);
11505 COMPLETE_CHECK_OP_0 (node);
11506 if (flag_emit_xref)
11508 TREE_TYPE (node) = boolean_type_node;
11511 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11513 case UNARY_PLUS_EXPR:
11515 case TRUTH_NOT_EXPR:
11517 case PREDECREMENT_EXPR:
11518 case PREINCREMENT_EXPR:
11519 case POSTDECREMENT_EXPR:
11520 case POSTINCREMENT_EXPR:
11522 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11523 how to handle those cases. */
11524 wfl_op1 = TREE_OPERAND (node, 0);
11525 CAN_COMPLETE_NORMALLY (node) = 1;
11526 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11527 if (TREE_OPERAND (node, 0) == error_mark_node)
11528 return error_mark_node;
11529 node = patch_unaryop (node, wfl_op1);
11530 CAN_COMPLETE_NORMALLY (node) = 1;
11534 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11535 how to handle those cases. */
11536 wfl_op1 = TREE_OPERAND (node, 0);
11537 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11538 if (TREE_OPERAND (node, 0) == error_mark_node)
11539 return error_mark_node;
11540 if (!flag_emit_class_files && !flag_emit_xref)
11541 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
11542 /* The same applies to wfl_op2 */
11543 wfl_op2 = TREE_OPERAND (node, 1);
11544 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11545 if (TREE_OPERAND (node, 1) == error_mark_node)
11546 return error_mark_node;
11547 if (!flag_emit_class_files && !flag_emit_xref)
11548 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
11549 return patch_array_ref (node);
11554 case COMPONENT_REF:
11555 /* The first step in the re-write of qualified name handling. FIXME.
11556 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
11557 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11558 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11560 tree name = TREE_OPERAND (node, 1);
11561 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11562 if (field == NULL_TREE)
11564 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11565 return error_mark_node;
11567 if (! FIELD_STATIC (field))
11569 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11570 return error_mark_node;
11575 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
11579 /* Can't use THIS in a static environment */
11582 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11583 parse_error_context (wfl_operator,
11584 "Keyword `this' used outside allowed context");
11585 TREE_TYPE (node) = error_mark_node;
11586 return error_mark_node;
11588 if (ctxp->explicit_constructor_p)
11590 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11591 parse_error_context
11592 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
11593 TREE_TYPE (node) = error_mark_node;
11594 return error_mark_node;
11596 return current_this;
11598 case CLASS_LITERAL:
11599 CAN_COMPLETE_NORMALLY (node) = 1;
11600 node = patch_incomplete_class_ref (node);
11601 if (node == error_mark_node)
11602 return error_mark_node;
11605 case INSTANCE_INITIALIZERS_EXPR:
11606 in_instance_initializer++;
11607 node = java_complete_tree (TREE_OPERAND (node, 0));
11608 in_instance_initializer--;
11609 if (node != error_mark_node)
11610 TREE_TYPE (node) = void_type_node;
11612 return error_mark_node;
11616 CAN_COMPLETE_NORMALLY (node) = 1;
11617 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
11618 and it's time to turn it into the appropriate String object */
11619 if ((nn = patch_string (node)))
11622 fatal ("No case for tree code `%s' - java_complete_tree\n",
11623 tree_code_name [TREE_CODE (node)]);
11628 /* Complete function call's argument. Return a non zero value is an
11629 error was found. */
11632 complete_function_arguments (node)
11638 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11639 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11641 tree wfl = TREE_VALUE (cn), parm, temp;
11642 parm = java_complete_tree (wfl);
11644 if (parm == error_mark_node)
11649 /* If have a string literal that we haven't transformed yet or a
11650 crafted string buffer, as a result of use of the the String
11651 `+' operator. Build `parm.toString()' and expand it. */
11652 if ((temp = patch_string (parm)))
11654 /* Inline PRIMTYPE.TYPE read access */
11655 parm = maybe_build_primttype_type_ref (parm, wfl);
11657 TREE_VALUE (cn) = parm;
11659 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11663 /* Sometimes (for loops and variable initialized during their
11664 declaration), we want to wrap a statement around a WFL and turn it
11668 build_debugable_stmt (location, stmt)
11672 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11674 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11675 EXPR_WFL_LINECOL (stmt) = location;
11677 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11682 build_expr_block (body, decls)
11685 tree node = make_node (BLOCK);
11686 BLOCK_EXPR_DECLS (node) = decls;
11687 BLOCK_EXPR_BODY (node) = body;
11689 TREE_TYPE (node) = TREE_TYPE (body);
11690 TREE_SIDE_EFFECTS (node) = 1;
11694 /* Create a new function block and link it approriately to current
11695 function block chain */
11700 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11703 /* Link block B supercontext to the previous block. The current
11704 function DECL is used as supercontext when enter_a_block is called
11705 for the first time for a given function. The current function body
11706 (DECL_FUNCTION_BODY) is set to be block B. */
11712 tree fndecl = current_function_decl;
11715 BLOCK_SUPERCONTEXT (b) = current_static_block;
11716 current_static_block = b;
11719 else if (!DECL_FUNCTION_BODY (fndecl))
11721 BLOCK_SUPERCONTEXT (b) = fndecl;
11722 DECL_FUNCTION_BODY (fndecl) = b;
11726 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11727 DECL_FUNCTION_BODY (fndecl) = b;
11732 /* Exit a block by changing the current function body
11733 (DECL_FUNCTION_BODY) to the current block super context, only if
11734 the block being exited isn't the method's top level one. */
11740 if (current_function_decl)
11742 b = DECL_FUNCTION_BODY (current_function_decl);
11743 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11744 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11748 b = current_static_block;
11750 if (BLOCK_SUPERCONTEXT (b))
11751 current_static_block = BLOCK_SUPERCONTEXT (b);
11756 /* Lookup for NAME in the nested function's blocks, all the way up to
11757 the current toplevel one. It complies with Java's local variable
11761 lookup_name_in_blocks (name)
11764 tree b = GET_CURRENT_BLOCK (current_function_decl);
11766 while (b != current_function_decl)
11770 /* Paranoid sanity check. To be removed */
11771 if (TREE_CODE (b) != BLOCK)
11772 fatal ("non block expr function body - lookup_name_in_blocks");
11774 for (current = BLOCK_EXPR_DECLS (b); current;
11775 current = TREE_CHAIN (current))
11776 if (DECL_NAME (current) == name)
11778 b = BLOCK_SUPERCONTEXT (b);
11784 maybe_absorb_scoping_blocks ()
11786 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
11788 tree b = exit_block ();
11789 java_method_add_stmt (current_function_decl, b);
11790 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11795 /* This section of the source is reserved to build_* functions that
11796 are building incomplete tree nodes and the patch_* functions that
11797 are completing them. */
11799 /* Wrap a non WFL node around a WFL. */
11801 build_wfl_wrap (node, location)
11805 tree wfl, node_to_insert = node;
11807 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11808 with the way we're processing SUPER. A THIS from a primary as a
11809 different form than a SUPER. Turn THIS into something symbolic */
11810 if (TREE_CODE (node) == THIS_EXPR)
11811 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11813 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11815 EXPR_WFL_LINECOL (wfl) = location;
11816 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11821 /* Build a super() constructor invocation. Returns empty_stmt_node if
11822 we're currently dealing with the class java.lang.Object. */
11825 build_super_invocation (mdecl)
11828 if (DECL_CONTEXT (mdecl) == object_type_node)
11829 return empty_stmt_node;
11832 tree super_wfl = build_wfl_node (super_identifier_node);
11833 tree a = NULL_TREE, t;
11834 /* If we're dealing with an anonymous class, pass the arguments
11835 of the crafted constructor along. */
11836 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11838 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11839 for (; t != end_params_node; t = TREE_CHAIN (t))
11840 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11842 return build_method_invocation (super_wfl, a);
11846 /* Build a SUPER/THIS qualified method invocation. */
11849 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11856 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
11857 EXPR_WFL_LINECOL (wfl) = lloc;
11858 invok = build_method_invocation (name, args);
11859 return make_qualified_primary (wfl, invok, rloc);
11862 /* Build an incomplete CALL_EXPR node. */
11865 build_method_invocation (name, args)
11869 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11870 TREE_SIDE_EFFECTS (call) = 1;
11871 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11875 /* Build an incomplete new xxx(...) node. */
11878 build_new_invocation (name, args)
11881 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
11882 TREE_SIDE_EFFECTS (call) = 1;
11883 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11887 /* Build an incomplete assignment expression. */
11890 build_assignment (op, op_location, lhs, rhs)
11891 int op, op_location;
11895 /* Build the corresponding binop if we deal with a Compound
11896 Assignment operator. Mark the binop sub-tree as part of a
11897 Compound Assignment expression */
11898 if (op != ASSIGN_TK)
11900 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
11901 COMPOUND_ASSIGN_P (rhs) = 1;
11903 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
11904 TREE_SIDE_EFFECTS (assignment) = 1;
11905 EXPR_WFL_LINECOL (assignment) = op_location;
11909 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
11912 print_int_node (node)
11915 static char buffer [80];
11916 if (TREE_CONSTANT_OVERFLOW (node))
11917 sprintf (buffer, "<overflow>");
11919 if (TREE_INT_CST_HIGH (node) == 0)
11920 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
11921 TREE_INT_CST_LOW (node));
11922 else if (TREE_INT_CST_HIGH (node) == -1
11923 && TREE_INT_CST_LOW (node) != 0)
11926 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
11927 -TREE_INT_CST_LOW (node));
11930 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
11931 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
11936 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
11940 check_final_assignment (lvalue, wfl)
11943 if (TREE_CODE (lvalue) == COMPOUND_EXPR
11944 && JDECL_P (TREE_OPERAND (lvalue, 1)))
11945 lvalue = TREE_OPERAND (lvalue, 1);
11947 /* When generating class files, references to the `length' field
11948 look a bit different. */
11949 if ((flag_emit_class_files
11950 && TREE_CODE (lvalue) == COMPONENT_REF
11951 && TYPE_ARRAY_P (TREE_TYPE (TREE_OPERAND (lvalue, 0)))
11952 && FIELD_FINAL (TREE_OPERAND (lvalue, 1)))
11953 || (TREE_CODE (lvalue) == FIELD_DECL
11954 && FIELD_FINAL (lvalue)
11955 && !DECL_CLINIT_P (current_function_decl)
11956 && !DECL_FINIT_P (current_function_decl)))
11958 parse_error_context
11959 (wfl, "Can't assign a value to the final variable `%s'",
11960 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
11966 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
11967 read. This is needed to avoid circularities in the implementation
11968 of these fields in libjava. */
11971 maybe_build_primttype_type_ref (rhs, wfl)
11974 tree to_return = NULL_TREE;
11975 tree rhs_type = TREE_TYPE (rhs);
11976 if (TREE_CODE (rhs) == COMPOUND_EXPR)
11978 tree n = TREE_OPERAND (rhs, 1);
11979 if (TREE_CODE (n) == VAR_DECL
11980 && DECL_NAME (n) == TYPE_identifier_node
11981 && rhs_type == class_ptr_type
11982 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
11984 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
11985 if (!strncmp (self_name, "java.lang.", 10))
11986 to_return = build_primtype_type_ref (self_name);
11989 return (to_return ? to_return : rhs );
11992 /* 15.25 Assignment operators. */
11995 patch_assignment (node, wfl_op1, wfl_op2)
12000 tree rhs = TREE_OPERAND (node, 1);
12001 tree lvalue = TREE_OPERAND (node, 0), llvalue;
12002 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
12003 int error_found = 0;
12004 int lvalue_from_array = 0;
12006 /* Can't assign to a (blank) final. */
12007 if (check_final_assignment (lvalue, wfl_op1))
12010 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12012 /* Lhs can be a named variable */
12013 if (JDECL_P (lvalue))
12015 lhs_type = TREE_TYPE (lvalue);
12017 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
12018 comment on reason why */
12019 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
12021 lhs_type = TREE_TYPE (lvalue);
12022 lvalue_from_array = 1;
12024 /* Or a field access */
12025 else if (TREE_CODE (lvalue) == COMPONENT_REF)
12026 lhs_type = TREE_TYPE (lvalue);
12027 /* Or a function return slot */
12028 else if (TREE_CODE (lvalue) == RESULT_DECL)
12029 lhs_type = TREE_TYPE (lvalue);
12030 /* Otherwise, we might want to try to write into an optimized static
12031 final, this is an of a different nature, reported further on. */
12032 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
12033 && resolve_expression_name (wfl_op1, &llvalue))
12035 if (!error_found && check_final_assignment (llvalue, wfl_op1))
12037 /* What we should do instead is resetting the all the flags
12038 previously set, exchange lvalue for llvalue and continue. */
12040 return error_mark_node;
12043 lhs_type = TREE_TYPE (lvalue);
12047 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
12051 rhs_type = TREE_TYPE (rhs);
12052 /* 5.1 Try the assignment conversion for builtin type. */
12053 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
12055 /* 5.2 If it failed, try a reference conversion */
12056 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
12057 lhs_type = promote_type (rhs_type);
12059 /* 15.25.2 If we have a compound assignment, convert RHS into the
12061 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12062 new_rhs = convert (lhs_type, rhs);
12064 /* Explicit cast required. This is an error */
12067 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
12068 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
12070 char operation [32]; /* Max size known */
12072 /* If the assignment is part of a declaration, we use the WFL of
12073 the declared variable to point out the error and call it a
12074 declaration problem. If the assignment is a genuine =
12075 operator, we call is a operator `=' problem, otherwise we
12076 call it an assignment problem. In both of these last cases,
12077 we use the WFL of the operator to indicate the error. */
12079 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
12082 strcpy (operation, "declaration");
12086 wfl = wfl_operator;
12087 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
12088 strcpy (operation, "assignment");
12089 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
12090 strcpy (operation, "`return'");
12092 strcpy (operation, "`='");
12095 if (!valid_cast_to_p (rhs_type, lhs_type))
12096 parse_error_context
12097 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
12098 operation, t1, t2);
12100 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
12101 operation, t1, t2);
12102 free (t1); free (t2);
12106 /* Inline read access to java.lang.PRIMTYPE.TYPE */
12108 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
12111 return error_mark_node;
12113 /* 10.10: Array Store Exception runtime check */
12114 if (!flag_emit_class_files
12116 && lvalue_from_array
12117 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
12120 tree base = lvalue;
12122 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
12123 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12124 base = TREE_OPERAND (lvalue, 0);
12127 if (flag_bounds_check)
12128 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
12130 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
12133 /* Build the invocation of _Jv_CheckArrayStore */
12134 new_rhs = save_expr (new_rhs);
12135 check = build (CALL_EXPR, void_type_node,
12136 build_address_of (soft_checkarraystore_node),
12137 tree_cons (NULL_TREE, base,
12138 build_tree_list (NULL_TREE, new_rhs)),
12140 TREE_SIDE_EFFECTS (check) = 1;
12142 /* We have to decide on an insertion point */
12143 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
12146 if (flag_bounds_check)
12148 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
12149 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
12150 build (COMPOUND_EXPR, void_type_node, t, check);
12153 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12154 check, TREE_OPERAND (lvalue, 1));
12158 /* Make sure the bound check will happen before the store check */
12159 if (flag_bounds_check)
12160 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12161 build (COMPOUND_EXPR, void_type_node,
12162 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12164 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12168 /* Final locals can be used as case values in switch
12169 statement. Prepare them for this eventuality. */
12170 if (TREE_CODE (lvalue) == VAR_DECL
12171 && LOCAL_FINAL (lvalue)
12172 && TREE_CONSTANT (new_rhs)
12173 && IDENTIFIER_LOCAL_VALUE (DECL_NAME (lvalue))
12174 && JINTEGRAL_TYPE_P (TREE_TYPE (lvalue))
12177 TREE_CONSTANT (lvalue) = 1;
12178 DECL_INITIAL (lvalue) = new_rhs;
12181 TREE_OPERAND (node, 0) = lvalue;
12182 TREE_OPERAND (node, 1) = new_rhs;
12183 TREE_TYPE (node) = lhs_type;
12187 /* Check that type SOURCE can be cast into type DEST. If the cast
12188 can't occur at all, return 0 otherwise 1. This function is used to
12189 produce accurate error messages on the reasons why an assignment
12193 try_reference_assignconv (lhs_type, rhs)
12194 tree lhs_type, rhs;
12196 tree new_rhs = NULL_TREE;
12197 tree rhs_type = TREE_TYPE (rhs);
12199 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12201 /* `null' may be assigned to any reference type */
12202 if (rhs == null_pointer_node)
12203 new_rhs = null_pointer_node;
12204 /* Try the reference assignment conversion */
12205 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12207 /* This is a magic assignment that we process differently */
12208 else if (rhs == soft_exceptioninfo_call_node)
12214 /* Check that RHS can be converted into LHS_TYPE by the assignment
12215 conversion (5.2), for the cases of RHS being a builtin type. Return
12216 NULL_TREE if the conversion fails or if because RHS isn't of a
12217 builtin type. Return a converted RHS if the conversion is possible. */
12220 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12221 tree wfl_op1, lhs_type, rhs;
12223 tree new_rhs = NULL_TREE;
12224 tree rhs_type = TREE_TYPE (rhs);
12226 /* Handle boolean specially. */
12227 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12228 || TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12230 if (TREE_CODE (rhs_type) == BOOLEAN_TYPE
12231 && TREE_CODE (lhs_type) == BOOLEAN_TYPE)
12235 /* Zero accepted everywhere */
12236 else if (TREE_CODE (rhs) == INTEGER_CST
12237 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12238 && JPRIMITIVE_TYPE_P (rhs_type))
12239 new_rhs = convert (lhs_type, rhs);
12241 /* 5.1.1 Try Identity Conversion,
12242 5.1.2 Try Widening Primitive Conversion */
12243 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12244 new_rhs = convert (lhs_type, rhs);
12246 /* Try a narrowing primitive conversion (5.1.3):
12247 - expression is a constant expression of type int AND
12248 - variable is byte, short or char AND
12249 - The value of the expression is representable in the type of the
12251 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12252 && (lhs_type == byte_type_node || lhs_type == char_type_node
12253 || lhs_type == short_type_node))
12255 if (int_fits_type_p (rhs, lhs_type))
12256 new_rhs = convert (lhs_type, rhs);
12257 else if (wfl_op1) /* Might be called with a NULL */
12258 parse_warning_context
12259 (wfl_op1, "Constant expression `%s' too wide for narrowing primitive conversion to `%s'",
12260 print_int_node (rhs), lang_printable_name (lhs_type, 0));
12261 /* Reported a warning that will turn into an error further
12262 down, so we don't return */
12268 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12269 conversion (5.1.1) or widening primitive conversion (5.1.2). Return
12270 0 is the conversion test fails. This implements parts the method
12271 invocation convertion (5.3). */
12274 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12275 tree lhs_type, rhs_type;
12277 /* 5.1.1: This is the identity conversion part. */
12278 if (lhs_type == rhs_type)
12281 /* Reject non primitive types and boolean conversions. */
12282 if (!JNUMERIC_TYPE_P (lhs_type) || !JNUMERIC_TYPE_P (rhs_type))
12285 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12286 than a char can't be converted into a char. Short can't too, but
12287 the < test below takes care of that */
12288 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12291 /* Accept all promoted type here. Note, we can't use <= in the test
12292 below, because we still need to bounce out assignments of short
12293 to char and the likes */
12294 if (lhs_type == int_type_node
12295 && (rhs_type == promoted_byte_type_node
12296 || rhs_type == promoted_short_type_node
12297 || rhs_type == promoted_char_type_node
12298 || rhs_type == promoted_boolean_type_node))
12301 /* From here, an integral is widened if its precision is smaller
12302 than the precision of the LHS or if the LHS is a floating point
12303 type, or the RHS is a float and the RHS a double. */
12304 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12305 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12306 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12307 || (rhs_type == float_type_node && lhs_type == double_type_node))
12313 /* Check that something of SOURCE type can be assigned or cast to
12314 something of DEST type at runtime. Return 1 if the operation is
12315 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12316 were SOURCE is cast into DEST, which borrows a lot of the
12317 assignment check. */
12320 valid_ref_assignconv_cast_p (source, dest, cast)
12325 /* SOURCE or DEST might be null if not from a declared entity. */
12326 if (!source || !dest)
12328 if (JNULLP_TYPE_P (source))
12330 if (TREE_CODE (source) == POINTER_TYPE)
12331 source = TREE_TYPE (source);
12332 if (TREE_CODE (dest) == POINTER_TYPE)
12333 dest = TREE_TYPE (dest);
12334 /* Case where SOURCE is a class type */
12335 if (TYPE_CLASS_P (source))
12337 if (TYPE_CLASS_P (dest))
12338 return (source == dest
12339 || inherits_from_p (source, dest)
12340 || (cast && inherits_from_p (dest, source)));
12341 if (TYPE_INTERFACE_P (dest))
12343 /* If doing a cast and SOURCE is final, the operation is
12344 always correct a compile time (because even if SOURCE
12345 does not implement DEST, a subclass of SOURCE might). */
12346 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12348 /* Otherwise, SOURCE must implement DEST */
12349 return interface_of_p (dest, source);
12351 /* DEST is an array, cast permited if SOURCE is of Object type */
12352 return (cast && source == object_type_node ? 1 : 0);
12354 if (TYPE_INTERFACE_P (source))
12356 if (TYPE_CLASS_P (dest))
12358 /* If not casting, DEST must be the Object type */
12360 return dest == object_type_node;
12361 /* We're doing a cast. The cast is always valid is class
12362 DEST is not final, otherwise, DEST must implement SOURCE */
12363 else if (!CLASS_FINAL (TYPE_NAME (dest)))
12366 return interface_of_p (source, dest);
12368 if (TYPE_INTERFACE_P (dest))
12370 /* If doing a cast, then if SOURCE and DEST contain method
12371 with the same signature but different return type, then
12372 this is a (compile time) error */
12375 tree method_source, method_dest;
12379 for (method_source = TYPE_METHODS (source); method_source;
12380 method_source = TREE_CHAIN (method_source))
12383 build_java_argument_signature (TREE_TYPE (method_source));
12384 source_type = TREE_TYPE (TREE_TYPE (method_source));
12385 source_name = DECL_NAME (method_source);
12386 for (method_dest = TYPE_METHODS (dest);
12387 method_dest; method_dest = TREE_CHAIN (method_dest))
12389 build_java_argument_signature (TREE_TYPE (method_dest))
12390 && source_name == DECL_NAME (method_dest)
12391 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12397 return source == dest || interface_of_p (dest, source);
12403 && (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable
12404 || (DECL_NAME (TYPE_NAME (source))
12405 == java_io_serializable)));
12408 if (TYPE_ARRAY_P (source))
12410 if (TYPE_CLASS_P (dest))
12411 return dest == object_type_node;
12412 /* Can't cast an array to an interface unless the interface is
12413 java.lang.Cloneable or java.io.Serializable. */
12414 if (TYPE_INTERFACE_P (dest))
12415 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable
12416 || DECL_NAME (TYPE_NAME (dest)) == java_io_serializable);
12419 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12420 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12422 /* In case of severe errors, they turn out null */
12423 if (!dest_element_type || !source_element_type)
12425 if (source_element_type == dest_element_type)
12427 return valid_ref_assignconv_cast_p (source_element_type,
12428 dest_element_type, cast);
12436 valid_cast_to_p (source, dest)
12440 if (TREE_CODE (source) == POINTER_TYPE)
12441 source = TREE_TYPE (source);
12442 if (TREE_CODE (dest) == POINTER_TYPE)
12443 dest = TREE_TYPE (dest);
12445 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12446 return valid_ref_assignconv_cast_p (source, dest, 1);
12448 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12451 else if (TREE_CODE (source) == BOOLEAN_TYPE
12452 && TREE_CODE (dest) == BOOLEAN_TYPE)
12459 do_unary_numeric_promotion (arg)
12462 tree type = TREE_TYPE (arg);
12463 if ((TREE_CODE (type) == INTEGER_TYPE && TYPE_PRECISION (type) < 32)
12464 || TREE_CODE (type) == CHAR_TYPE)
12465 arg = convert (int_type_node, arg);
12469 /* Return a non zero value if SOURCE can be converted into DEST using
12470 the method invocation conversion rule (5.3). */
12472 valid_method_invocation_conversion_p (dest, source)
12475 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
12476 && valid_builtin_assignconv_identity_widening_p (dest, source))
12477 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12478 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12479 && valid_ref_assignconv_cast_p (source, dest, 0)));
12482 /* Build an incomplete binop expression. */
12485 build_binop (op, op_location, op1, op2)
12490 tree binop = build (op, NULL_TREE, op1, op2);
12491 TREE_SIDE_EFFECTS (binop) = 1;
12492 /* Store the location of the operator, for better error report. The
12493 string of the operator will be rebuild based on the OP value. */
12494 EXPR_WFL_LINECOL (binop) = op_location;
12498 /* Build the string of the operator retained by NODE. If NODE is part
12499 of a compound expression, add an '=' at the end of the string. This
12500 function is called when an error needs to be reported on an
12501 operator. The string is returned as a pointer to a static character
12505 operator_string (node)
12508 #define BUILD_OPERATOR_STRING(S) \
12510 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12514 static char buffer [10];
12515 switch (TREE_CODE (node))
12517 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12518 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12519 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12520 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12521 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12522 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12523 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12524 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12525 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12526 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12527 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12528 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12529 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12530 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12531 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12532 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12533 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12534 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12535 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
12536 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12537 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12538 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12539 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12540 case PREINCREMENT_EXPR: /* Fall through */
12541 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12542 case PREDECREMENT_EXPR: /* Fall through */
12543 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12545 fatal ("unregistered operator %s - operator_string",
12546 tree_code_name [TREE_CODE (node)]);
12549 #undef BUILD_OPERATOR_STRING
12552 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12555 java_decl_equiv (var_acc1, var_acc2)
12556 tree var_acc1, var_acc2;
12558 if (JDECL_P (var_acc1))
12559 return (var_acc1 == var_acc2);
12561 return (TREE_CODE (var_acc1) == COMPONENT_REF
12562 && TREE_CODE (var_acc2) == COMPONENT_REF
12563 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12564 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12565 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12568 /* Return a non zero value if CODE is one of the operators that can be
12569 used in conjunction with the `=' operator in a compound assignment. */
12572 binop_compound_p (code)
12573 enum tree_code code;
12576 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12577 if (binop_lookup [i] == code)
12580 return i < BINOP_COMPOUND_CANDIDATES;
12583 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12589 tree c, b, ns, decl;
12591 if (TREE_CODE (t) != MODIFY_EXPR)
12594 c = TREE_OPERAND (t, 1);
12595 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12596 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12597 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12600 /* Now the left branch of the binary operator. */
12601 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12602 if (! (b && TREE_CODE (b) == NOP_EXPR
12603 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12606 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12607 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12608 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12611 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12612 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12613 /* It's got to be the an equivalent decl */
12614 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12616 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12617 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12618 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12619 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12620 /* Change the right part of the BINOP_EXPR */
12621 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12627 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
12628 errors but we modify NODE so that it contains the type computed
12629 according to the expression, when it's fixed. Otherwise, we write
12630 error_mark_node as the type. It allows us to further the analysis
12631 of remaining nodes and detects more errors in certain cases. */
12634 patch_binop (node, wfl_op1, wfl_op2)
12639 tree op1 = TREE_OPERAND (node, 0);
12640 tree op2 = TREE_OPERAND (node, 1);
12641 tree op1_type = TREE_TYPE (op1);
12642 tree op2_type = TREE_TYPE (op2);
12643 tree prom_type = NULL_TREE, cn;
12644 int code = TREE_CODE (node);
12646 /* If 1, tell the routine that we have to return error_mark_node
12647 after checking for the initialization of the RHS */
12648 int error_found = 0;
12650 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12654 /* 15.16 Multiplicative operators */
12655 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
12656 case RDIV_EXPR: /* 15.16.2 Division Operator / */
12657 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
12658 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
12659 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
12661 if (!JNUMERIC_TYPE_P (op1_type))
12662 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12663 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
12664 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12665 TREE_TYPE (node) = error_mark_node;
12669 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12670 /* Change the division operator if necessary */
12671 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
12672 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
12674 if (TREE_CODE (prom_type) == INTEGER_TYPE
12675 && flag_use_divide_subroutine
12676 && ! flag_emit_class_files
12677 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
12678 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
12680 /* This one is more complicated. FLOATs are processed by a
12681 function call to soft_fmod. Duplicate the value of the
12682 COMPOUND_ASSIGN_P flag. */
12683 if (code == TRUNC_MOD_EXPR)
12685 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
12686 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
12687 TREE_SIDE_EFFECTS (mod)
12688 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12693 /* 15.17 Additive Operators */
12694 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
12696 /* Operation is valid if either one argument is a string
12697 constant, a String object or a StringBuffer crafted for the
12698 purpose of the a previous usage of the String concatenation
12701 if (TREE_CODE (op1) == STRING_CST
12702 || TREE_CODE (op2) == STRING_CST
12703 || JSTRING_TYPE_P (op1_type)
12704 || JSTRING_TYPE_P (op2_type)
12705 || IS_CRAFTED_STRING_BUFFER_P (op1)
12706 || IS_CRAFTED_STRING_BUFFER_P (op2))
12707 return build_string_concatenation (op1, op2);
12709 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
12711 if (!JNUMERIC_TYPE_P (op1_type) || !JNUMERIC_TYPE_P (op2_type))
12713 if (!JNUMERIC_TYPE_P (op1_type))
12714 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12715 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
12716 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12717 TREE_TYPE (node) = error_mark_node;
12721 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12724 /* 15.18 Shift Operators */
12728 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
12730 if (!JINTEGRAL_TYPE_P (op1_type))
12731 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12734 if (JNUMERIC_TYPE_P (op2_type))
12735 parse_error_context (wfl_operator,
12736 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
12737 operator_string (node),
12738 lang_printable_name (op2_type, 0));
12740 parse_error_context (wfl_operator,
12741 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
12742 operator_string (node),
12743 lang_printable_name (op2_type, 0));
12745 TREE_TYPE (node) = error_mark_node;
12750 /* Unary numeric promotion (5.6.1) is performed on each operand
12752 op1 = do_unary_numeric_promotion (op1);
12753 op2 = do_unary_numeric_promotion (op2);
12755 /* The type of the shift expression is the type of the promoted
12756 type of the left-hand operand */
12757 prom_type = TREE_TYPE (op1);
12759 /* Shift int only up to 0x1f and long up to 0x3f */
12760 if (prom_type == int_type_node)
12761 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12762 build_int_2 (0x1f, 0)));
12764 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12765 build_int_2 (0x3f, 0)));
12767 /* The >>> operator is a >> operating on unsigned quantities */
12768 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
12771 tree utype = unsigned_type (prom_type);
12772 op1 = convert (utype, op1);
12773 TREE_SET_CODE (node, RSHIFT_EXPR);
12774 TREE_OPERAND (node, 0) = op1;
12775 TREE_OPERAND (node, 1) = op2;
12776 TREE_TYPE (node) = utype;
12777 to_return = convert (prom_type, node);
12778 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
12779 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
12780 TREE_SIDE_EFFECTS (to_return)
12781 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12786 /* 15.19.1 Type Comparison Operator instaceof */
12787 case INSTANCEOF_EXPR:
12789 TREE_TYPE (node) = boolean_type_node;
12791 if (!(op2_type = resolve_type_during_patch (op2)))
12792 return error_mark_node;
12794 /* The first operand must be a reference type or the null type */
12795 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
12796 error_found = 1; /* Error reported further below */
12798 /* The second operand must be a reference type */
12799 if (!JREFERENCE_TYPE_P (op2_type))
12801 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
12802 parse_error_context
12803 (wfl_operator, "Invalid argument `%s' for `instanceof'",
12804 lang_printable_name (op2_type, 0));
12808 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
12810 /* If the first operand is null, the result is always false */
12811 if (op1 == null_pointer_node)
12812 return boolean_false_node;
12813 else if (flag_emit_class_files)
12815 TREE_OPERAND (node, 1) = op2_type;
12816 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
12819 /* Otherwise we have to invoke instance of to figure it out */
12821 return build_instanceof (op1, op2_type);
12823 /* There is no way the expression operand can be an instance of
12824 the type operand. This is a compile time error. */
12827 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
12828 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
12829 parse_error_context
12830 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
12831 t1, lang_printable_name (op2_type, 0));
12838 /* 15.21 Bitwise and Logical Operators */
12842 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
12843 /* Binary numeric promotion is performed on both operand and the
12844 expression retain that type */
12845 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12847 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
12848 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
12849 /* The type of the bitwise operator expression is BOOLEAN */
12850 prom_type = boolean_type_node;
12853 if (!JINTEGRAL_TYPE_P (op1_type))
12854 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12855 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
12856 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
12857 TREE_TYPE (node) = error_mark_node;
12859 /* Insert a break here if adding thing before the switch's
12860 break for this case */
12864 /* 15.22 Conditional-And Operator */
12865 case TRUTH_ANDIF_EXPR:
12866 /* 15.23 Conditional-Or Operator */
12867 case TRUTH_ORIF_EXPR:
12868 /* Operands must be of BOOLEAN type */
12869 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
12870 TREE_CODE (op2_type) != BOOLEAN_TYPE)
12872 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
12873 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
12874 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
12875 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
12876 TREE_TYPE (node) = boolean_type_node;
12880 /* The type of the conditional operators is BOOLEAN */
12881 prom_type = boolean_type_node;
12884 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
12889 /* The type of each of the operands must be a primitive numeric
12891 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
12893 if (!JNUMERIC_TYPE_P (op1_type))
12894 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12895 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
12896 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12897 TREE_TYPE (node) = boolean_type_node;
12901 /* Binary numeric promotion is performed on the operands */
12902 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12903 /* The type of the relation expression is always BOOLEAN */
12904 prom_type = boolean_type_node;
12907 /* 15.20 Equality Operator */
12910 /* It's time for us to patch the strings. */
12911 if ((cn = patch_string (op1)))
12914 op1_type = TREE_TYPE (op1);
12916 if ((cn = patch_string (op2)))
12919 op2_type = TREE_TYPE (op2);
12922 /* 15.20.1 Numerical Equality Operators == and != */
12923 /* Binary numeric promotion is performed on the operands */
12924 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
12925 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12927 /* 15.20.2 Boolean Equality Operators == and != */
12928 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
12929 TREE_CODE (op2_type) == BOOLEAN_TYPE)
12930 ; /* Nothing to do here */
12932 /* 15.20.3 Reference Equality Operators == and != */
12933 /* Types have to be either references or the null type. If
12934 they're references, it must be possible to convert either
12935 type to the other by casting conversion. */
12936 else if (op1 == null_pointer_node || op2 == null_pointer_node
12937 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
12938 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
12939 || valid_ref_assignconv_cast_p (op2_type,
12941 ; /* Nothing to do here */
12943 /* Else we have an error figure what can't be converted into
12944 what and report the error */
12948 t1 = xstrdup (lang_printable_name (op1_type, 0));
12949 parse_error_context
12951 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
12952 operator_string (node), t1,
12953 lang_printable_name (op2_type, 0));
12955 TREE_TYPE (node) = boolean_type_node;
12959 prom_type = boolean_type_node;
12964 return error_mark_node;
12966 TREE_OPERAND (node, 0) = op1;
12967 TREE_OPERAND (node, 1) = op2;
12968 TREE_TYPE (node) = prom_type;
12969 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12971 if (flag_emit_xref)
12974 /* fold does not respect side-effect order as required for Java but not C.
12975 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
12978 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
12979 : ! TREE_SIDE_EFFECTS (node))
12980 node = fold (node);
12984 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
12985 zero value, the value of CSTE comes after the valude of STRING */
12988 do_merge_string_cste (cste, string, string_len, after)
12990 const char *string;
12991 int string_len, after;
12993 const char *old = TREE_STRING_POINTER (cste);
12994 int old_len = TREE_STRING_LENGTH (cste);
12995 int len = old_len + string_len;
12998 cste = make_node (STRING_CST);
12999 TREE_STRING_LENGTH (cste) = len;
13000 new = TREE_STRING_POINTER (cste) = ggc_alloc (len+1);
13004 memcpy (new, string, string_len);
13005 memcpy (&new [string_len], old, old_len);
13009 memcpy (new, old, old_len);
13010 memcpy (&new [old_len], string, string_len);
13016 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
13017 new STRING_CST on success, NULL_TREE on failure */
13020 merge_string_cste (op1, op2, after)
13024 /* Handle two string constants right away */
13025 if (TREE_CODE (op2) == STRING_CST)
13026 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
13027 TREE_STRING_LENGTH (op2), after);
13029 /* Reasonable integer constant can be treated right away */
13030 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
13032 static const char *boolean_true = "true";
13033 static const char *boolean_false = "false";
13034 static const char *null_pointer = "null";
13036 const char *string;
13038 if (op2 == boolean_true_node)
13039 string = boolean_true;
13040 else if (op2 == boolean_false_node)
13041 string = boolean_false;
13042 else if (op2 == null_pointer_node)
13043 string = null_pointer;
13044 else if (TREE_TYPE (op2) == char_type_node)
13046 ch[0] = (char )TREE_INT_CST_LOW (op2);
13051 string = print_int_node (op2);
13053 return do_merge_string_cste (op1, string, strlen (string), after);
13058 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
13059 has to be a STRING_CST and the other part must be a STRING_CST or a
13060 INTEGRAL constant. Return a new STRING_CST if the operation
13061 succeed, NULL_TREE otherwise.
13063 If the case we want to optimize for space, we might want to return
13064 NULL_TREE for each invocation of this routine. FIXME */
13067 string_constant_concatenation (op1, op2)
13070 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
13075 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
13076 rest = (string == op1 ? op2 : op1);
13077 invert = (string == op1 ? 0 : 1 );
13079 /* Walk REST, only if it looks reasonable */
13080 if (TREE_CODE (rest) != STRING_CST
13081 && !IS_CRAFTED_STRING_BUFFER_P (rest)
13082 && !JSTRING_TYPE_P (TREE_TYPE (rest))
13083 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
13085 rest = java_complete_tree (rest);
13086 if (rest == error_mark_node)
13087 return error_mark_node;
13088 rest = fold (rest);
13090 return merge_string_cste (string, rest, invert);
13095 /* Implement the `+' operator. Does static optimization if possible,
13096 otherwise create (if necessary) and append elements to a
13097 StringBuffer. The StringBuffer will be carried around until it is
13098 used for a function call or an assignment. Then toString() will be
13099 called on it to turn it into a String object. */
13102 build_string_concatenation (op1, op2)
13106 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
13108 if (flag_emit_xref)
13109 return build (PLUS_EXPR, string_type_node, op1, op2);
13111 /* Try to do some static optimization */
13112 if ((result = string_constant_concatenation (op1, op2)))
13115 /* Discard empty strings on either side of the expression */
13116 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
13121 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
13124 /* If operands are string constant, turn then into object references */
13125 if (TREE_CODE (op1) == STRING_CST)
13126 op1 = patch_string_cst (op1);
13127 if (op2 && TREE_CODE (op2) == STRING_CST)
13128 op2 = patch_string_cst (op2);
13130 /* If either one of the constant is null and the other non null
13131 operand is a String object, return it. */
13132 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
13135 /* If OP1 isn't already a StringBuffer, create and
13136 initialize a new one */
13137 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
13139 /* Two solutions here:
13140 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
13141 2) OP1 is something else, we call new StringBuffer().append(OP1). */
13142 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
13143 op1 = BUILD_STRING_BUFFER (op1);
13146 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
13147 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
13153 /* OP1 is no longer the last node holding a crafted StringBuffer */
13154 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
13155 /* Create a node for `{new...,xxx}.append (op2)' */
13157 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
13160 /* Mark the last node holding a crafted StringBuffer */
13161 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
13163 TREE_SIDE_EFFECTS (op1) = side_effects;
13167 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
13168 StringBuffer. If no string were found to be patched, return
13172 patch_string (node)
13175 if (node == error_mark_node)
13176 return error_mark_node;
13177 if (TREE_CODE (node) == STRING_CST)
13178 return patch_string_cst (node);
13179 else if (IS_CRAFTED_STRING_BUFFER_P (node))
13181 int saved = ctxp->explicit_constructor_p;
13182 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
13184 /* Temporary disable forbid the use of `this'. */
13185 ctxp->explicit_constructor_p = 0;
13186 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
13187 /* String concatenation arguments must be evaluated in order too. */
13188 ret = force_evaluation_order (ret);
13189 /* Restore it at its previous value */
13190 ctxp->explicit_constructor_p = saved;
13196 /* Build the internal representation of a string constant. */
13199 patch_string_cst (node)
13203 if (! flag_emit_class_files)
13205 node = get_identifier (TREE_STRING_POINTER (node));
13206 location = alloc_name_constant (CONSTANT_String, node);
13207 node = build_ref_from_constant_pool (location);
13209 TREE_TYPE (node) = string_ptr_type_node;
13210 TREE_CONSTANT (node) = 1;
13214 /* Build an incomplete unary operator expression. */
13217 build_unaryop (op_token, op_location, op1)
13218 int op_token, op_location;
13225 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13226 case MINUS_TK: op = NEGATE_EXPR; break;
13227 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13228 case NOT_TK: op = BIT_NOT_EXPR; break;
13229 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
13233 unaryop = build1 (op, NULL_TREE, op1);
13234 TREE_SIDE_EFFECTS (unaryop) = 1;
13235 /* Store the location of the operator, for better error report. The
13236 string of the operator will be rebuild based on the OP value. */
13237 EXPR_WFL_LINECOL (unaryop) = op_location;
13241 /* Special case for the ++/-- operators, since they require an extra
13242 argument to build, which is set to NULL and patched
13243 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13246 build_incdec (op_token, op_location, op1, is_post_p)
13247 int op_token, op_location;
13251 static enum tree_code lookup [2][2] =
13253 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13254 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13256 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13257 NULL_TREE, op1, NULL_TREE);
13258 TREE_SIDE_EFFECTS (node) = 1;
13259 /* Store the location of the operator, for better error report. The
13260 string of the operator will be rebuild based on the OP value. */
13261 EXPR_WFL_LINECOL (node) = op_location;
13265 /* Build an incomplete cast operator, based on the use of the
13266 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13267 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13268 though its type is already set. */
13271 build_cast (location, type, exp)
13275 tree node = build1 (CONVERT_EXPR, type, exp);
13276 EXPR_WFL_LINECOL (node) = location;
13280 /* Build an incomplete class reference operator. */
13282 build_incomplete_class_ref (location, class_name)
13286 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13287 EXPR_WFL_LINECOL (node) = location;
13291 /* Complete an incomplete class reference operator. */
13293 patch_incomplete_class_ref (node)
13296 tree type = TREE_OPERAND (node, 0);
13299 if (!(ref_type = resolve_type_during_patch (type)))
13300 return error_mark_node;
13302 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
13304 /* A class referenced by `foo.class' is initialized. */
13305 return build_class_init (ref_type, build_class_ref (ref_type));
13308 /* If we're emitting class files and we have to deal with non
13309 primitive types, we invoke (and consider generating) the
13310 synthetic static method `class$'. */
13311 if (!TYPE_DOT_CLASS (current_class))
13312 build_dot_class_method (current_class);
13313 ref_type = build_dot_class_method_invocation (ref_type);
13314 return java_complete_tree (ref_type);
13317 /* 15.14 Unary operators. We return error_mark_node in case of error,
13318 but preserve the type of NODE if the type is fixed. */
13321 patch_unaryop (node, wfl_op)
13325 tree op = TREE_OPERAND (node, 0);
13326 tree op_type = TREE_TYPE (op);
13327 tree prom_type = NULL_TREE, value, decl;
13328 int outer_field_flag = 0;
13329 int code = TREE_CODE (node);
13330 int error_found = 0;
13332 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13336 /* 15.13.2 Postfix Increment Operator ++ */
13337 case POSTINCREMENT_EXPR:
13338 /* 15.13.3 Postfix Increment Operator -- */
13339 case POSTDECREMENT_EXPR:
13340 /* 15.14.1 Prefix Increment Operator ++ */
13341 case PREINCREMENT_EXPR:
13342 /* 15.14.2 Prefix Decrement Operator -- */
13343 case PREDECREMENT_EXPR:
13344 op = decl = strip_out_static_field_access_decl (op);
13345 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13346 /* We might be trying to change an outer field accessed using
13348 if (outer_field_flag)
13350 /* Retrieve the decl of the field we're trying to access. We
13351 do that by first retrieving the function we would call to
13352 access the field. It has been already verified that this
13353 field isn't final */
13354 if (flag_emit_class_files)
13355 decl = TREE_OPERAND (op, 0);
13357 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13358 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13360 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
13361 else if (!JDECL_P (decl)
13362 && TREE_CODE (decl) != COMPONENT_REF
13363 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13364 && TREE_CODE (decl) != INDIRECT_REF
13365 && !(TREE_CODE (decl) == COMPOUND_EXPR
13366 && TREE_OPERAND (decl, 1)
13367 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
13370 /* Before screaming, check that we're not in fact trying to
13371 increment a optimized static final access, in which case
13372 we issue an different error message. */
13373 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13374 && resolve_expression_name (wfl_op, &lvalue)
13375 && check_final_assignment (lvalue, wfl_op)))
13376 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13377 operator_string (node));
13378 TREE_TYPE (node) = error_mark_node;
13382 if (check_final_assignment (op, wfl_op))
13385 /* From now on, we know that op if a variable and that it has a
13386 valid wfl. We use wfl_op to locate errors related to the
13388 else if (!JNUMERIC_TYPE_P (op_type))
13390 parse_error_context
13391 (wfl_op, "Invalid argument type `%s' to `%s'",
13392 lang_printable_name (op_type, 0), operator_string (node));
13393 TREE_TYPE (node) = error_mark_node;
13398 /* Before the addition, binary numeric promotion is performed on
13399 both operands, if really necessary */
13400 if (JINTEGRAL_TYPE_P (op_type))
13402 value = build_int_2 (1, 0);
13403 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13407 value = build_int_2 (1, 0);
13409 binary_numeric_promotion (op_type,
13410 TREE_TYPE (value), &op, &value);
13413 /* We remember we might be accessing an outer field */
13414 if (outer_field_flag)
13416 /* We re-generate an access to the field */
13417 value = build (PLUS_EXPR, TREE_TYPE (op),
13418 build_outer_field_access (wfl_op, decl), value);
13420 /* And we patch the original access$() into a write
13421 with plus_op as a rhs */
13422 return outer_field_access_fix (node, op, value);
13425 /* And write back into the node. */
13426 TREE_OPERAND (node, 0) = op;
13427 TREE_OPERAND (node, 1) = value;
13428 /* Convert the overall back into its original type, if
13429 necessary, and return */
13430 if (JINTEGRAL_TYPE_P (op_type))
13431 return fold (node);
13433 return fold (convert (op_type, node));
13437 /* 15.14.3 Unary Plus Operator + */
13438 case UNARY_PLUS_EXPR:
13439 /* 15.14.4 Unary Minus Operator - */
13441 if (!JNUMERIC_TYPE_P (op_type))
13443 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13444 TREE_TYPE (node) = error_mark_node;
13447 /* Unary numeric promotion is performed on operand */
13450 op = do_unary_numeric_promotion (op);
13451 prom_type = TREE_TYPE (op);
13452 if (code == UNARY_PLUS_EXPR)
13457 /* 15.14.5 Bitwise Complement Operator ~ */
13459 if (!JINTEGRAL_TYPE_P (op_type))
13461 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13462 TREE_TYPE (node) = error_mark_node;
13467 op = do_unary_numeric_promotion (op);
13468 prom_type = TREE_TYPE (op);
13472 /* 15.14.6 Logical Complement Operator ! */
13473 case TRUTH_NOT_EXPR:
13474 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13476 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
13477 /* But the type is known. We will report an error if further
13478 attempt of a assignment is made with this rhs */
13479 TREE_TYPE (node) = boolean_type_node;
13483 prom_type = boolean_type_node;
13486 /* 15.15 Cast Expression */
13488 value = patch_cast (node, wfl_operator);
13489 if (value == error_mark_node)
13491 /* If this cast is part of an assignment, we tell the code
13492 that deals with it not to complain about a mismatch,
13493 because things have been cast, anyways */
13494 TREE_TYPE (node) = error_mark_node;
13499 value = fold (value);
13500 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13507 return error_mark_node;
13509 /* There are cases where node has been replaced by something else
13510 and we don't end up returning here: UNARY_PLUS_EXPR,
13511 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
13512 TREE_OPERAND (node, 0) = fold (op);
13513 TREE_TYPE (node) = prom_type;
13514 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
13515 return fold (node);
13518 /* Generic type resolution that sometimes takes place during node
13519 patching. Returned the resolved type or generate an error
13520 message. Return the resolved type or NULL_TREE. */
13523 resolve_type_during_patch (type)
13526 if (unresolved_type_p (type, NULL))
13528 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), type);
13531 parse_error_context (type,
13532 "Class `%s' not found in type declaration",
13533 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13538 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13539 return TREE_TYPE (type_decl);
13544 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
13545 found. Otherwise NODE or something meant to replace it is returned. */
13548 patch_cast (node, wfl_op)
13552 tree op = TREE_OPERAND (node, 0);
13553 tree op_type = TREE_TYPE (op);
13554 tree cast_type = TREE_TYPE (node);
13557 /* First resolve OP_TYPE if unresolved */
13558 if (!(cast_type = resolve_type_during_patch (cast_type)))
13559 return error_mark_node;
13561 /* Check on cast that are proven correct at compile time */
13562 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13565 if (cast_type == op_type)
13568 /* float and double type are converted to the original type main
13569 variant and then to the target type. */
13570 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13571 op = convert (integer_type_node, op);
13573 /* Try widening/narowwing convertion. Potentially, things need
13574 to be worked out in gcc so we implement the extreme cases
13575 correctly. fold_convert() needs to be fixed. */
13576 return convert (cast_type, op);
13579 /* It's also valid to cast a boolean into a boolean */
13580 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13583 /* null can be casted to references */
13584 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13585 return build_null_of_type (cast_type);
13587 /* The remaining legal casts involve conversion between reference
13588 types. Check for their compile time correctness. */
13589 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
13590 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
13592 TREE_TYPE (node) = promote_type (cast_type);
13593 /* Now, the case can be determined correct at compile time if
13594 OP_TYPE can be converted into CAST_TYPE by assignment
13595 conversion (5.2) */
13597 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
13599 TREE_SET_CODE (node, NOP_EXPR);
13603 if (flag_emit_class_files)
13605 TREE_SET_CODE (node, CONVERT_EXPR);
13609 /* The cast requires a run-time check */
13610 return build (CALL_EXPR, promote_type (cast_type),
13611 build_address_of (soft_checkcast_node),
13612 tree_cons (NULL_TREE, build_class_ref (cast_type),
13613 build_tree_list (NULL_TREE, op)),
13617 /* Any other casts are proven incorrect at compile time */
13618 t1 = xstrdup (lang_printable_name (op_type, 0));
13619 parse_error_context (wfl_op, "Invalid cast from `%s' to `%s'",
13620 t1, lang_printable_name (cast_type, 0));
13622 return error_mark_node;
13625 /* Build a null constant and give it the type TYPE. */
13628 build_null_of_type (type)
13631 tree node = build_int_2 (0, 0);
13632 TREE_TYPE (node) = promote_type (type);
13636 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
13637 a list of indices. */
13639 build_array_ref (location, array, index)
13643 tree node = build (ARRAY_REF, NULL_TREE, array, index);
13644 EXPR_WFL_LINECOL (node) = location;
13648 /* 15.12 Array Access Expression */
13651 patch_array_ref (node)
13654 tree array = TREE_OPERAND (node, 0);
13655 tree array_type = TREE_TYPE (array);
13656 tree index = TREE_OPERAND (node, 1);
13657 tree index_type = TREE_TYPE (index);
13658 int error_found = 0;
13660 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13662 if (TREE_CODE (array_type) == POINTER_TYPE)
13663 array_type = TREE_TYPE (array_type);
13665 /* The array reference must be an array */
13666 if (!TYPE_ARRAY_P (array_type))
13668 parse_error_context
13670 "`[]' can only be applied to arrays. It can't be applied to `%s'",
13671 lang_printable_name (array_type, 0));
13672 TREE_TYPE (node) = error_mark_node;
13676 /* The array index undergoes unary numeric promotion. The promoted
13677 type must be int */
13678 index = do_unary_numeric_promotion (index);
13679 if (TREE_TYPE (index) != int_type_node)
13681 if (valid_cast_to_p (index_type, int_type_node))
13682 parse_error_context (wfl_operator,
13683 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
13684 lang_printable_name (index_type, 0));
13686 parse_error_context (wfl_operator,
13687 "Incompatible type for `[]'. Can't convert `%s' to `int'",
13688 lang_printable_name (index_type, 0));
13689 TREE_TYPE (node) = error_mark_node;
13694 return error_mark_node;
13696 array_type = TYPE_ARRAY_ELEMENT (array_type);
13698 if (flag_emit_class_files || flag_emit_xref)
13700 TREE_OPERAND (node, 0) = array;
13701 TREE_OPERAND (node, 1) = index;
13705 /* The save_expr is for correct evaluation order. It would be cleaner
13706 to use force_evaluation_order (see comment there), but that is
13707 difficult when we also have to deal with bounds checking. */
13708 if (TREE_SIDE_EFFECTS (index))
13709 array = save_expr (array);
13710 node = build_java_arrayaccess (array, array_type, index);
13711 if (TREE_SIDE_EFFECTS (index))
13712 node = build (COMPOUND_EXPR, array_type, array, node);
13714 TREE_TYPE (node) = array_type;
13718 /* 15.9 Array Creation Expressions */
13721 build_newarray_node (type, dims, extra_dims)
13727 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
13728 build_int_2 (extra_dims, 0));
13733 patch_newarray (node)
13736 tree type = TREE_OPERAND (node, 0);
13737 tree dims = TREE_OPERAND (node, 1);
13738 tree cdim, array_type;
13739 int error_found = 0;
13741 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
13743 /* Dimension types are verified. It's better for the types to be
13744 verified in order. */
13745 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
13748 tree dim = TREE_VALUE (cdim);
13750 /* Dim might have been saved during its evaluation */
13751 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
13753 /* The type of each specified dimension must be an integral type. */
13754 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
13757 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
13758 promoted type must be int. */
13761 dim = do_unary_numeric_promotion (dim);
13762 if (TREE_TYPE (dim) != int_type_node)
13766 /* Report errors on types here */
13769 parse_error_context
13770 (TREE_PURPOSE (cdim),
13771 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
13772 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
13773 "Explicit cast needed to" : "Can't"),
13774 lang_printable_name (TREE_TYPE (dim), 0));
13778 TREE_PURPOSE (cdim) = NULL_TREE;
13781 /* Resolve array base type if unresolved */
13782 if (!(type = resolve_type_during_patch (type)))
13787 /* We don't want further evaluation of this bogus array creation
13789 TREE_TYPE (node) = error_mark_node;
13790 return error_mark_node;
13793 /* Set array_type to the actual (promoted) array type of the result. */
13794 if (TREE_CODE (type) == RECORD_TYPE)
13795 type = build_pointer_type (type);
13796 while (--xdims >= 0)
13798 type = promote_type (build_java_array_type (type, -1));
13800 dims = nreverse (dims);
13802 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
13806 = build_java_array_type (type,
13807 TREE_CODE (cdim) == INTEGER_CST
13808 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
13810 array_type = promote_type (array_type);
13812 dims = nreverse (dims);
13814 /* The node is transformed into a function call. Things are done
13815 differently according to the number of dimensions. If the number
13816 of dimension is equal to 1, then the nature of the base type
13817 (primitive or not) matters. */
13819 return build_new_array (type, TREE_VALUE (dims));
13821 /* Can't reuse what's already written in expr.c because it uses the
13822 JVM stack representation. Provide a build_multianewarray. FIXME */
13823 return build (CALL_EXPR, array_type,
13824 build_address_of (soft_multianewarray_node),
13825 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
13826 tree_cons (NULL_TREE,
13827 build_int_2 (ndims, 0), dims )),
13831 /* 10.6 Array initializer. */
13833 /* Build a wfl for array element that don't have one, so we can
13834 pin-point errors. */
13837 maybe_build_array_element_wfl (node)
13840 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
13841 return build_expr_wfl (NULL_TREE, ctxp->filename,
13842 ctxp->elc.line, ctxp->elc.prev_col);
13847 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
13848 identification of initialized arrays easier to detect during walk
13852 build_new_array_init (location, values)
13856 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
13857 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
13858 EXPR_WFL_LINECOL (to_return) = location;
13862 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
13863 occurred. Otherwise return NODE after having set its type
13867 patch_new_array_init (type, node)
13870 int error_seen = 0;
13871 tree current, element_type;
13872 HOST_WIDE_INT length;
13873 int all_constant = 1;
13874 tree init = TREE_OPERAND (node, 0);
13876 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
13878 parse_error_context (node,
13879 "Invalid array initializer for non-array type `%s'",
13880 lang_printable_name (type, 1));
13881 return error_mark_node;
13883 type = TREE_TYPE (type);
13884 element_type = TYPE_ARRAY_ELEMENT (type);
13886 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
13888 for (length = 0, current = CONSTRUCTOR_ELTS (init);
13889 current; length++, current = TREE_CHAIN (current))
13891 tree elt = TREE_VALUE (current);
13892 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
13894 error_seen |= array_constructor_check_entry (element_type, current);
13895 elt = TREE_VALUE (current);
13896 /* When compiling to native code, STRING_CST is converted to
13897 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
13898 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
13903 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
13904 TREE_PURPOSE (current) = NULL_TREE;
13907 if (elt && TREE_CODE (elt) == TREE_LIST
13908 && TREE_VALUE (elt) == error_mark_node)
13913 return error_mark_node;
13915 /* Create a new type. We can't reuse the one we have here by
13916 patching its dimension because it originally is of dimension -1
13917 hence reused by gcc. This would prevent triangular arrays. */
13918 type = build_java_array_type (element_type, length);
13919 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
13920 TREE_TYPE (node) = promote_type (type);
13921 TREE_CONSTANT (init) = all_constant;
13922 TREE_CONSTANT (node) = all_constant;
13926 /* Verify that one entry of the initializer element list can be
13927 assigned to the array base type. Report 1 if an error occurred, 0
13931 array_constructor_check_entry (type, entry)
13934 char *array_type_string = NULL; /* For error reports */
13935 tree value, type_value, new_value, wfl_value, patched;
13936 int error_seen = 0;
13938 new_value = NULL_TREE;
13939 wfl_value = TREE_VALUE (entry);
13941 value = java_complete_tree (TREE_VALUE (entry));
13942 /* patch_string return error_mark_node if arg is error_mark_node */
13943 if ((patched = patch_string (value)))
13945 if (value == error_mark_node)
13948 type_value = TREE_TYPE (value);
13950 /* At anytime, try_builtin_assignconv can report a warning on
13951 constant overflow during narrowing. */
13952 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
13953 new_value = try_builtin_assignconv (wfl_operator, type, value);
13954 if (!new_value && (new_value = try_reference_assignconv (type, value)))
13955 type_value = promote_type (type);
13957 /* Check and report errors */
13960 const char *msg = (!valid_cast_to_p (type_value, type) ?
13961 "Can't" : "Explicit cast needed to");
13962 if (!array_type_string)
13963 array_type_string = xstrdup (lang_printable_name (type, 1));
13964 parse_error_context
13965 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
13966 msg, lang_printable_name (type_value, 1), array_type_string);
13972 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
13973 TREE_VALUE (entry) = new_value;
13976 if (array_type_string)
13977 free (array_type_string);
13979 TREE_PURPOSE (entry) = NULL_TREE;
13984 build_this (location)
13987 tree node = build_wfl_node (this_identifier_node);
13988 TREE_SET_CODE (node, THIS_EXPR);
13989 EXPR_WFL_LINECOL (node) = location;
13993 /* 14.15 The return statement. It builds a modify expression that
13994 assigns the returned value to the RESULT_DECL that hold the value
13998 build_return (location, op)
14002 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
14003 EXPR_WFL_LINECOL (node) = location;
14004 node = build_debugable_stmt (location, node);
14009 patch_return (node)
14012 tree return_exp = TREE_OPERAND (node, 0);
14013 tree meth = current_function_decl;
14014 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
14015 int error_found = 0;
14017 TREE_TYPE (node) = error_mark_node;
14018 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14020 /* It's invalid to have a return value within a function that is
14021 declared with the keyword void or that is a constructor */
14022 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
14025 /* It's invalid to use a return statement in a static block */
14026 if (DECL_CLINIT_P (current_function_decl))
14029 /* It's invalid to have a no return value within a function that
14030 isn't declared with the keyword `void' */
14031 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
14034 if (in_instance_initializer)
14039 if (in_instance_initializer)
14040 parse_error_context (wfl_operator,
14041 "`return' inside instance initializer");
14043 else if (DECL_CLINIT_P (current_function_decl))
14044 parse_error_context (wfl_operator,
14045 "`return' inside static initializer");
14047 else if (!DECL_CONSTRUCTOR_P (meth))
14049 char *t = xstrdup (lang_printable_name (mtype, 0));
14050 parse_error_context (wfl_operator,
14051 "`return' with%s value from `%s %s'",
14052 (error_found == 1 ? "" : "out"),
14053 t, lang_printable_name (meth, 0));
14057 parse_error_context (wfl_operator,
14058 "`return' with value from constructor `%s'",
14059 lang_printable_name (meth, 0));
14060 return error_mark_node;
14063 /* If we have a return_exp, build a modify expression and expand
14064 it. Note: at that point, the assignment is declared valid, but we
14065 may want to carry some more hacks */
14068 tree exp = java_complete_tree (return_exp);
14069 tree modify, patched;
14071 /* If the function returned value and EXP are booleans, EXP has
14072 to be converted into the type of DECL_RESULT, which is integer
14073 (see complete_start_java_method) */
14074 if (TREE_TYPE (exp) == boolean_type_node &&
14075 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
14076 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
14078 /* `null' can be assigned to a function returning a reference */
14079 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
14080 exp == null_pointer_node)
14081 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
14083 if ((patched = patch_string (exp)))
14086 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
14087 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
14088 modify = java_complete_tree (modify);
14090 if (modify != error_mark_node)
14092 TREE_SIDE_EFFECTS (modify) = 1;
14093 TREE_OPERAND (node, 0) = modify;
14096 return error_mark_node;
14098 TREE_TYPE (node) = void_type_node;
14099 TREE_SIDE_EFFECTS (node) = 1;
14103 /* 14.8 The if Statement */
14106 build_if_else_statement (location, expression, if_body, else_body)
14108 tree expression, if_body, else_body;
14112 else_body = empty_stmt_node;
14113 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
14114 EXPR_WFL_LINECOL (node) = location;
14115 node = build_debugable_stmt (location, node);
14120 patch_if_else_statement (node)
14123 tree expression = TREE_OPERAND (node, 0);
14125 TREE_TYPE (node) = error_mark_node;
14126 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14128 /* The type of expression must be boolean */
14129 if (TREE_TYPE (expression) != boolean_type_node
14130 && TREE_TYPE (expression) != promoted_boolean_type_node)
14132 parse_error_context
14134 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
14135 lang_printable_name (TREE_TYPE (expression), 0));
14136 return error_mark_node;
14139 TREE_TYPE (node) = void_type_node;
14140 TREE_SIDE_EFFECTS (node) = 1;
14141 CAN_COMPLETE_NORMALLY (node)
14142 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14143 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
14147 /* 14.6 Labeled Statements */
14149 /* Action taken when a lableled statement is parsed. a new
14150 LABELED_BLOCK_EXPR is created. No statement is attached to the
14151 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
14154 build_labeled_block (location, label)
14159 tree label_decl, node;
14160 if (label == NULL_TREE || label == continue_identifier_node)
14161 label_name = label;
14164 label_name = merge_qualified_name (label_id, label);
14165 /* Issue an error if we try to reuse a label that was previously
14167 if (IDENTIFIER_LOCAL_VALUE (label_name))
14169 EXPR_WFL_LINECOL (wfl_operator) = location;
14170 parse_error_context (wfl_operator,
14171 "Declaration of `%s' shadows a previous label declaration",
14172 IDENTIFIER_POINTER (label));
14173 EXPR_WFL_LINECOL (wfl_operator) =
14174 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
14175 parse_error_context (wfl_operator,
14176 "This is the location of the previous declaration of label `%s'",
14177 IDENTIFIER_POINTER (label));
14178 java_error_count--;
14182 label_decl = create_label_decl (label_name);
14183 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14184 EXPR_WFL_LINECOL (node) = location;
14185 TREE_SIDE_EFFECTS (node) = 1;
14189 /* A labeled statement LBE is attached a statement. */
14192 finish_labeled_statement (lbe, statement)
14193 tree lbe; /* Labeled block expr */
14196 /* In anyways, tie the loop to its statement */
14197 LABELED_BLOCK_BODY (lbe) = statement;
14198 pop_labeled_block ();
14199 POP_LABELED_BLOCK ();
14203 /* 14.10, 14.11, 14.12 Loop Statements */
14205 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14209 build_new_loop (loop_body)
14212 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14213 TREE_SIDE_EFFECTS (loop) = 1;
14218 /* Create a loop body according to the following structure:
14220 COMPOUND_EXPR (loop main body)
14221 EXIT_EXPR (this order is for while/for loops.
14222 LABELED_BLOCK_EXPR the order is reversed for do loops)
14223 LABEL_DECL (a continue occuring here branches at the
14224 BODY end of this labeled block)
14227 REVERSED, if non zero, tells that the loop condition expr comes
14228 after the body, like in the do-while loop.
14230 To obtain a loop, the loop body structure described above is
14231 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14234 LABEL_DECL (use this label to exit the loop)
14236 <structure described above> */
14239 build_loop_body (location, condition, reversed)
14244 tree first, second, body;
14246 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14247 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14248 condition = build_debugable_stmt (location, condition);
14249 TREE_SIDE_EFFECTS (condition) = 1;
14251 body = build_labeled_block (0, continue_identifier_node);
14252 first = (reversed ? body : condition);
14253 second = (reversed ? condition : body);
14255 build (COMPOUND_EXPR, NULL_TREE,
14256 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14259 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14260 their order) on the current loop. Unlink the current loop from the
14264 finish_loop_body (location, condition, body, reversed)
14266 tree condition, body;
14269 tree to_return = ctxp->current_loop;
14270 tree loop_body = LOOP_EXPR_BODY (to_return);
14273 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14274 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14275 The real EXIT_EXPR is one operand further. */
14276 EXPR_WFL_LINECOL (cnode) = location;
14277 /* This one is for accurate error reports */
14278 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14279 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14281 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14286 /* Tailored version of finish_loop_body for FOR loops, when FOR
14287 loops feature the condition part */
14290 finish_for_loop (location, condition, update, body)
14292 tree condition, update, body;
14294 /* Put the condition and the loop body in place */
14295 tree loop = finish_loop_body (location, condition, body, 0);
14296 /* LOOP is the current loop which has been now popped of the loop
14297 stack. Install the update block */
14298 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14302 /* Try to find the loop a block might be related to. This comprises
14303 the case where the LOOP_EXPR is found as the second operand of a
14304 COMPOUND_EXPR, because the loop happens to have an initialization
14305 part, then expressed as the first operand of the COMPOUND_EXPR. If
14306 the search finds something, 1 is returned. Otherwise, 0 is
14307 returned. The search is assumed to start from a
14308 LABELED_BLOCK_EXPR's block. */
14311 search_loop (statement)
14314 if (TREE_CODE (statement) == LOOP_EXPR)
14317 if (TREE_CODE (statement) == BLOCK)
14318 statement = BLOCK_SUBBLOCKS (statement);
14322 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14323 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14324 statement = TREE_OPERAND (statement, 1);
14326 return (TREE_CODE (statement) == LOOP_EXPR
14327 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
14330 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14331 returned otherwise. */
14334 labeled_block_contains_loop_p (block, loop)
14340 if (LABELED_BLOCK_BODY (block) == loop)
14343 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
14349 /* If the loop isn't surrounded by a labeled statement, create one and
14350 insert LOOP as its body. */
14353 patch_loop_statement (loop)
14358 TREE_TYPE (loop) = void_type_node;
14359 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14362 loop_label = build_labeled_block (0, NULL_TREE);
14363 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14364 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
14365 LABELED_BLOCK_BODY (loop_label) = loop;
14366 PUSH_LABELED_BLOCK (loop_label);
14370 /* 14.13, 14.14: break and continue Statements */
14372 /* Build a break or a continue statement. a null NAME indicates an
14373 unlabeled break/continue statement. */
14376 build_bc_statement (location, is_break, name)
14377 int location, is_break;
14380 tree break_continue, label_block_expr = NULL_TREE;
14384 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14385 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14386 /* Null means that we don't have a target for this named
14387 break/continue. In this case, we make the target to be the
14388 label name, so that the error can be reported accuratly in
14389 patch_bc_statement. */
14390 label_block_expr = EXPR_WFL_NODE (name);
14392 /* Unlabeled break/continue will be handled during the
14393 break/continue patch operation */
14395 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14397 IS_BREAK_STMT_P (break_continue) = is_break;
14398 TREE_SIDE_EFFECTS (break_continue) = 1;
14399 EXPR_WFL_LINECOL (break_continue) = location;
14400 break_continue = build_debugable_stmt (location, break_continue);
14401 return break_continue;
14404 /* Verification of a break/continue statement. */
14407 patch_bc_statement (node)
14410 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
14411 tree labeled_block = ctxp->current_labeled_block;
14412 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14414 /* Having an identifier here means that the target is unknown. */
14415 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
14417 parse_error_context (wfl_operator, "No label definition found for `%s'",
14418 IDENTIFIER_POINTER (bc_label));
14419 return error_mark_node;
14421 if (! IS_BREAK_STMT_P (node))
14423 /* It's a continue statement. */
14424 for (;; labeled_block = TREE_CHAIN (labeled_block))
14426 if (labeled_block == NULL_TREE)
14428 if (bc_label == NULL_TREE)
14429 parse_error_context (wfl_operator,
14430 "`continue' must be in loop");
14432 parse_error_context
14433 (wfl_operator, "continue label `%s' does not name a loop",
14434 IDENTIFIER_POINTER (bc_label));
14435 return error_mark_node;
14437 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14438 == continue_identifier_node)
14439 && (bc_label == NULL_TREE
14440 || TREE_CHAIN (labeled_block) == bc_label))
14442 bc_label = labeled_block;
14447 else if (!bc_label)
14449 for (;; labeled_block = TREE_CHAIN (labeled_block))
14451 if (labeled_block == NULL_TREE)
14453 parse_error_context (wfl_operator,
14454 "`break' must be in loop or switch");
14455 return error_mark_node;
14457 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14458 if (TREE_CODE (target_stmt) == SWITCH_EXPR
14459 || search_loop (target_stmt))
14461 bc_label = labeled_block;
14467 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
14468 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14470 /* Our break/continue don't return values. */
14471 TREE_TYPE (node) = void_type_node;
14472 /* Encapsulate the break within a compound statement so that it's
14473 expanded all the times by expand_expr (and not clobbered
14474 sometimes, like after a if statement) */
14475 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14476 TREE_SIDE_EFFECTS (node) = 1;
14480 /* Process the exit expression belonging to a loop. Its type must be
14484 patch_exit_expr (node)
14487 tree expression = TREE_OPERAND (node, 0);
14488 TREE_TYPE (node) = error_mark_node;
14489 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14491 /* The type of expression must be boolean */
14492 if (TREE_TYPE (expression) != boolean_type_node)
14494 parse_error_context
14496 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
14497 lang_printable_name (TREE_TYPE (expression), 0));
14498 return error_mark_node;
14500 /* Now we know things are allright, invert the condition, fold and
14502 TREE_OPERAND (node, 0) =
14503 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
14505 if (! integer_zerop (TREE_OPERAND (node, 0))
14506 && ctxp->current_loop != NULL_TREE
14507 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14508 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14509 if (! integer_onep (TREE_OPERAND (node, 0)))
14510 CAN_COMPLETE_NORMALLY (node) = 1;
14513 TREE_TYPE (node) = void_type_node;
14517 /* 14.9 Switch statement */
14520 patch_switch_statement (node)
14523 tree se = TREE_OPERAND (node, 0), se_type;
14525 /* Complete the switch expression */
14526 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14527 se_type = TREE_TYPE (se);
14528 /* The type of the switch expression must be char, byte, short or
14530 if (! JINTEGRAL_TYPE_P (se_type) || se_type == long_type_node)
14532 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14533 parse_error_context (wfl_operator,
14534 "Incompatible type for `switch'. Can't convert `%s' to `int'",
14535 lang_printable_name (se_type, 0));
14536 /* This is what java_complete_tree will check */
14537 TREE_OPERAND (node, 0) = error_mark_node;
14538 return error_mark_node;
14541 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
14543 /* Ready to return */
14544 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
14546 TREE_TYPE (node) = error_mark_node;
14547 return error_mark_node;
14549 TREE_TYPE (node) = void_type_node;
14550 TREE_SIDE_EFFECTS (node) = 1;
14551 CAN_COMPLETE_NORMALLY (node)
14552 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14553 || ! SWITCH_HAS_DEFAULT (node);
14557 /* 14.18 The try/catch statements */
14560 build_try_statement (location, try_block, catches)
14562 tree try_block, catches;
14564 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
14565 EXPR_WFL_LINECOL (node) = location;
14570 build_try_finally_statement (location, try_block, finally)
14572 tree try_block, finally;
14574 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14575 EXPR_WFL_LINECOL (node) = location;
14580 patch_try_statement (node)
14583 int error_found = 0;
14584 tree try = TREE_OPERAND (node, 0);
14585 /* Exception handlers are considered in left to right order */
14586 tree catch = nreverse (TREE_OPERAND (node, 1));
14587 tree current, caught_type_list = NULL_TREE;
14589 /* Check catch clauses, if any. Every time we find an error, we try
14590 to process the next catch clause. We process the catch clause before
14591 the try block so that when processing the try block we can check thrown
14592 exceptions againts the caught type list. */
14593 for (current = catch; current; current = TREE_CHAIN (current))
14595 tree carg_decl, carg_type;
14596 tree sub_current, catch_block, catch_clause;
14599 /* At this point, the structure of the catch clause is
14600 CATCH_EXPR (catch node)
14601 BLOCK (with the decl of the parameter)
14603 MODIFY_EXPR (assignment of the catch parameter)
14604 BLOCK (catch clause block)
14606 catch_clause = TREE_OPERAND (current, 0);
14607 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14608 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14610 /* Catch clauses can't have more than one parameter declared,
14611 but it's already enforced by the grammar. Make sure that the
14612 only parameter of the clause statement in of class Throwable
14613 or a subclass of Throwable, but that was done earlier. The
14614 catch clause parameter type has also been resolved. */
14616 /* Just make sure that the catch clause parameter type inherits
14617 from java.lang.Throwable */
14618 if (!inherits_from_p (carg_type, throwable_type_node))
14620 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14621 parse_error_context (wfl_operator,
14622 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
14623 lang_printable_name (carg_type, 0));
14628 /* Partial check for unreachable catch statement: The catch
14629 clause is reachable iff is no earlier catch block A in
14630 the try statement such that the type of the catch
14631 clause's parameter is the same as or a subclass of the
14632 type of A's parameter */
14634 for (sub_current = catch;
14635 sub_current != current; sub_current = TREE_CHAIN (sub_current))
14637 tree sub_catch_clause, decl;
14638 sub_catch_clause = TREE_OPERAND (sub_current, 0);
14639 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
14641 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
14643 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14644 parse_error_context
14646 "`catch' not reached because of the catch clause at line %d",
14647 EXPR_WFL_LINENO (sub_current));
14648 unreachable = error_found = 1;
14652 /* Complete the catch clause block */
14653 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
14654 if (catch_block == error_mark_node)
14659 if (CAN_COMPLETE_NORMALLY (catch_block))
14660 CAN_COMPLETE_NORMALLY (node) = 1;
14661 TREE_OPERAND (current, 0) = catch_block;
14666 /* Things to do here: the exception must be thrown */
14668 /* Link this type to the caught type list */
14669 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
14672 PUSH_EXCEPTIONS (caught_type_list);
14673 if ((try = java_complete_tree (try)) == error_mark_node)
14675 if (CAN_COMPLETE_NORMALLY (try))
14676 CAN_COMPLETE_NORMALLY (node) = 1;
14679 /* Verification ends here */
14681 return error_mark_node;
14683 TREE_OPERAND (node, 0) = try;
14684 TREE_OPERAND (node, 1) = catch;
14685 TREE_TYPE (node) = void_type_node;
14689 /* 14.17 The synchronized Statement */
14692 patch_synchronized_statement (node, wfl_op1)
14693 tree node, wfl_op1;
14695 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
14696 tree block = TREE_OPERAND (node, 1);
14698 tree enter, exit, expr_decl, assignment;
14700 if (expr == error_mark_node)
14702 block = java_complete_tree (block);
14706 /* The TYPE of expr must be a reference type */
14707 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
14709 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14710 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
14711 lang_printable_name (TREE_TYPE (expr), 0));
14712 return error_mark_node;
14715 if (flag_emit_xref)
14717 TREE_OPERAND (node, 0) = expr;
14718 TREE_OPERAND (node, 1) = java_complete_tree (block);
14719 CAN_COMPLETE_NORMALLY (node) = 1;
14723 /* Generate a try-finally for the synchronized statement, except
14724 that the handler that catches all throw exception calls
14725 _Jv_MonitorExit and then rethrow the exception.
14726 The synchronized statement is then implemented as:
14729 _Jv_MonitorEnter (expression)
14731 _Jv_MonitorExit (expression)
14735 e = _Jv_exception_info ();
14736 _Jv_MonitorExit (expression)
14740 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
14741 BUILD_MONITOR_ENTER (enter, expr_decl);
14742 BUILD_MONITOR_EXIT (exit, expr_decl);
14743 CAN_COMPLETE_NORMALLY (enter) = 1;
14744 CAN_COMPLETE_NORMALLY (exit) = 1;
14745 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
14746 TREE_SIDE_EFFECTS (assignment) = 1;
14747 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
14748 build (COMPOUND_EXPR, NULL_TREE,
14749 build (WITH_CLEANUP_EXPR, NULL_TREE,
14750 build (COMPOUND_EXPR, NULL_TREE,
14751 assignment, enter),
14754 node = build_expr_block (node, expr_decl);
14756 return java_complete_tree (node);
14759 /* 14.16 The throw Statement */
14762 patch_throw_statement (node, wfl_op1)
14763 tree node, wfl_op1;
14765 tree expr = TREE_OPERAND (node, 0);
14766 tree type = TREE_TYPE (expr);
14767 int unchecked_ok = 0, tryblock_throws_ok = 0;
14769 /* Thrown expression must be assignable to java.lang.Throwable */
14770 if (!try_reference_assignconv (throwable_type_node, expr))
14772 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14773 parse_error_context (wfl_operator,
14774 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
14775 lang_printable_name (type, 0));
14776 /* If the thrown expression was a reference, we further the
14777 compile-time check. */
14778 if (!JREFERENCE_TYPE_P (type))
14779 return error_mark_node;
14782 /* At least one of the following must be true */
14784 /* The type of the throw expression is a not checked exception,
14785 i.e. is a unchecked expression. */
14786 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
14788 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14789 /* An instance can't throw a checked excetion unless that exception
14790 is explicitely declared in the `throws' clause of each
14791 constructor. This doesn't apply to anonymous classes, since they
14792 don't have declared constructors. */
14794 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
14797 for (current = TYPE_METHODS (current_class); current;
14798 current = TREE_CHAIN (current))
14799 if (DECL_CONSTRUCTOR_P (current)
14800 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
14802 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)",
14803 lang_printable_name (TREE_TYPE (expr), 0));
14804 return error_mark_node;
14808 /* Throw is contained in a try statement and at least one catch
14809 clause can receive the thrown expression or the current method is
14810 declared to throw such an exception. Or, the throw statement is
14811 contained in a method or constructor declaration and the type of
14812 the Expression is assignable to at least one type listed in the
14813 throws clause the declaration. */
14815 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
14816 if (!(unchecked_ok || tryblock_throws_ok))
14818 /* If there is a surrounding try block that has no matching
14819 clatch clause, report it first. A surrounding try block exits
14820 only if there is something after the list of checked
14821 exception thrown by the current function (if any). */
14822 if (IN_TRY_BLOCK_P ())
14823 parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
14824 lang_printable_name (type, 0));
14825 /* If we have no surrounding try statement and the method doesn't have
14826 any throws, report it now. FIXME */
14828 /* We report that the exception can't be throw from a try block
14829 in all circumstances but when the `throw' is inside a static
14831 else if (!EXCEPTIONS_P (currently_caught_type_list)
14832 && !tryblock_throws_ok)
14834 if (DECL_CLINIT_P (current_function_decl))
14835 parse_error_context (wfl_operator,
14836 "Checked exception `%s' can't be thrown in initializer",
14837 lang_printable_name (type, 0));
14839 parse_error_context (wfl_operator,
14840 "Checked exception `%s' isn't thrown from a `try' block",
14841 lang_printable_name (type, 0));
14843 /* Otherwise, the current method doesn't have the appropriate
14844 throws declaration */
14846 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
14847 lang_printable_name (type, 0));
14848 return error_mark_node;
14851 if (! flag_emit_class_files && ! flag_emit_xref)
14852 BUILD_THROW (node, expr);
14854 /* If doing xrefs, keep the location where the `throw' was seen. */
14855 if (flag_emit_xref)
14856 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
14860 /* Check that exception said to be thrown by method DECL can be
14861 effectively caught from where DECL is invoked. */
14864 check_thrown_exceptions (location, decl)
14869 /* For all the unchecked exceptions thrown by DECL */
14870 for (throws = DECL_FUNCTION_THROWS (decl); throws;
14871 throws = TREE_CHAIN (throws))
14872 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
14875 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
14876 if (DECL_NAME (decl) == get_identifier ("clone"))
14879 EXPR_WFL_LINECOL (wfl_operator) = location;
14880 if (DECL_FINIT_P (current_function_decl))
14881 parse_error_context
14882 (wfl_operator, "Exception `%s' can't be thrown in initializer",
14883 lang_printable_name (TREE_VALUE (throws), 0));
14886 parse_error_context
14887 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
14888 lang_printable_name (TREE_VALUE (throws), 0),
14889 (DECL_INIT_P (current_function_decl) ?
14890 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
14891 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
14896 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
14897 try-catch blocks, OR is listed in the `throws' clause of the
14901 check_thrown_exceptions_do (exception)
14904 tree list = currently_caught_type_list;
14905 resolve_and_layout (exception, NULL_TREE);
14906 /* First, all the nested try-catch-finally at that stage. The
14907 last element contains `throws' clause exceptions, if any. */
14908 if (IS_UNCHECKED_EXCEPTION_P (exception))
14913 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
14914 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
14916 list = TREE_CHAIN (list);
14922 purge_unchecked_exceptions (mdecl)
14925 tree throws = DECL_FUNCTION_THROWS (mdecl);
14926 tree new = NULL_TREE;
14930 tree next = TREE_CHAIN (throws);
14931 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
14933 TREE_CHAIN (throws) = new;
14938 /* List is inverted here, but it doesn't matter */
14939 DECL_FUNCTION_THROWS (mdecl) = new;
14942 /* 15.24 Conditional Operator ?: */
14945 patch_conditional_expr (node, wfl_cond, wfl_op1)
14946 tree node, wfl_cond, wfl_op1;
14948 tree cond = TREE_OPERAND (node, 0);
14949 tree op1 = TREE_OPERAND (node, 1);
14950 tree op2 = TREE_OPERAND (node, 2);
14951 tree resulting_type = NULL_TREE;
14952 tree t1, t2, patched;
14953 int error_found = 0;
14955 /* Operands of ?: might be StringBuffers crafted as a result of a
14956 string concatenation. Obtain a descent operand here. */
14957 if ((patched = patch_string (op1)))
14958 TREE_OPERAND (node, 1) = op1 = patched;
14959 if ((patched = patch_string (op2)))
14960 TREE_OPERAND (node, 2) = op2 = patched;
14962 t1 = TREE_TYPE (op1);
14963 t2 = TREE_TYPE (op2);
14965 /* The first expression must be a boolean */
14966 if (TREE_TYPE (cond) != boolean_type_node)
14968 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
14969 parse_error_context (wfl_operator,
14970 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
14971 lang_printable_name (TREE_TYPE (cond), 0));
14975 /* Second and third can be numeric, boolean (i.e. primitive),
14976 references or null. Anything else results in an error */
14977 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
14978 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
14979 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
14980 || (t1 == boolean_type_node && t2 == boolean_type_node)))
14983 /* Determine the type of the conditional expression. Same types are
14984 easy to deal with */
14986 resulting_type = t1;
14988 /* There are different rules for numeric types */
14989 else if (JNUMERIC_TYPE_P (t1))
14991 /* if byte/short found, the resulting type is short */
14992 if ((t1 == byte_type_node && t2 == short_type_node)
14993 || (t1 == short_type_node && t2 == byte_type_node))
14994 resulting_type = short_type_node;
14996 /* If t1 is a constant int and t2 is of type byte, short or char
14997 and t1's value fits in t2, then the resulting type is t2 */
14998 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
14999 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
15000 resulting_type = t2;
15002 /* If t2 is a constant int and t1 is of type byte, short or char
15003 and t2's value fits in t1, then the resulting type is t1 */
15004 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
15005 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
15006 resulting_type = t1;
15008 /* Otherwise, binary numeric promotion is applied and the
15009 resulting type is the promoted type of operand 1 and 2 */
15011 resulting_type = binary_numeric_promotion (t1, t2,
15012 &TREE_OPERAND (node, 1),
15013 &TREE_OPERAND (node, 2));
15016 /* Cases of a reference and a null type */
15017 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
15018 resulting_type = t1;
15020 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
15021 resulting_type = t2;
15023 /* Last case: different reference types. If a type can be converted
15024 into the other one by assignment conversion, the latter
15025 determines the type of the expression */
15026 else if ((resulting_type = try_reference_assignconv (t1, op2)))
15027 resulting_type = promote_type (t1);
15029 else if ((resulting_type = try_reference_assignconv (t2, op1)))
15030 resulting_type = promote_type (t2);
15032 /* If we don't have any resulting type, we're in trouble */
15033 if (!resulting_type)
15035 char *t = xstrdup (lang_printable_name (t1, 0));
15036 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
15037 parse_error_context (wfl_operator,
15038 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
15039 t, lang_printable_name (t2, 0));
15046 TREE_TYPE (node) = error_mark_node;
15047 return error_mark_node;
15050 TREE_TYPE (node) = resulting_type;
15051 TREE_SET_CODE (node, COND_EXPR);
15052 CAN_COMPLETE_NORMALLY (node) = 1;
15056 /* Try to constant fold NODE.
15057 If NODE is not a constant expression, return NULL_EXPR.
15058 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
15061 fold_constant_for_init (node, context)
15065 tree op0, op1, val;
15066 enum tree_code code = TREE_CODE (node);
15068 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
15076 case TRUNC_MOD_EXPR:
15084 case TRUTH_ANDIF_EXPR:
15085 case TRUTH_ORIF_EXPR:
15092 op0 = TREE_OPERAND (node, 0);
15093 op1 = TREE_OPERAND (node, 1);
15094 val = fold_constant_for_init (op0, context);
15095 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15097 TREE_OPERAND (node, 0) = val;
15098 val = fold_constant_for_init (op1, context);
15099 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15101 TREE_OPERAND (node, 1) = val;
15102 return patch_binop (node, op0, op1);
15104 case UNARY_PLUS_EXPR:
15106 case TRUTH_NOT_EXPR:
15109 op0 = TREE_OPERAND (node, 0);
15110 val = fold_constant_for_init (op0, context);
15111 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15113 TREE_OPERAND (node, 0) = val;
15114 return patch_unaryop (node, op0);
15118 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
15119 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15121 TREE_OPERAND (node, 0) = val;
15122 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
15123 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15125 TREE_OPERAND (node, 1) = val;
15126 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
15127 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15129 TREE_OPERAND (node, 2) = val;
15130 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
15131 : TREE_OPERAND (node, 2);
15135 if (! FIELD_FINAL (node)
15136 || DECL_INITIAL (node) == NULL_TREE)
15138 val = DECL_INITIAL (node);
15139 /* Guard against infinite recursion. */
15140 DECL_INITIAL (node) = NULL_TREE;
15141 val = fold_constant_for_init (val, node);
15142 DECL_INITIAL (node) = val;
15145 case EXPR_WITH_FILE_LOCATION:
15146 /* Compare java_complete_tree and resolve_expression_name. */
15147 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
15148 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
15150 tree name = EXPR_WFL_NODE (node);
15152 if (PRIMARY_P (node))
15154 else if (! QUALIFIED_P (name))
15156 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
15157 if (decl == NULL_TREE
15158 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
15160 return fold_constant_for_init (decl, decl);
15164 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
15165 qualify_ambiguous_name (node);
15166 if (resolve_field_access (node, &decl, NULL)
15167 && decl != NULL_TREE)
15168 return fold_constant_for_init (decl, decl);
15174 op0 = TREE_OPERAND (node, 0);
15175 val = fold_constant_for_init (op0, context);
15176 if (val == NULL_TREE || ! TREE_CONSTANT (val))
15178 TREE_OPERAND (node, 0) = val;
15182 #ifdef USE_COMPONENT_REF
15184 case COMPONENT_REF:
15193 #ifdef USE_COMPONENT_REF
15194 /* Context is 'T' for TypeName, 'P' for PackageName,
15195 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15198 resolve_simple_name (name, context)
15205 resolve_qualified_name (name, context)
15212 /* Mark P, which is really a `struct parser_ctxt **' for GC. */
15215 mark_parser_ctxt (p)
15218 struct parser_ctxt *pc = *((struct parser_ctxt **) p);
15225 for (i = 0; i < 11; ++i)
15226 ggc_mark_tree (pc->modifier_ctx[i]);
15227 ggc_mark_tree (pc->class_type);
15228 ggc_mark_tree (pc->function_decl);
15229 ggc_mark_tree (pc->package);
15230 ggc_mark_tree (pc->incomplete_class);
15231 ggc_mark_tree (pc->gclass_list);
15232 ggc_mark_tree (pc->class_list);
15233 ggc_mark_tree (pc->current_parsed_class);
15234 ggc_mark_tree (pc->current_parsed_class_un);
15235 ggc_mark_tree (pc->non_static_initialized);
15236 ggc_mark_tree (pc->static_initialized);
15237 ggc_mark_tree (pc->instance_initializers);
15238 ggc_mark_tree (pc->import_list);
15239 ggc_mark_tree (pc->import_demand_list);
15240 ggc_mark_tree (pc->current_loop);
15241 ggc_mark_tree (pc->current_labeled_block);
15242 #endif /* JC1_LITE */
15245 mark_parser_ctxt (&pc->next);