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"
70 #define DIR_SEPARATOR '/'
73 /* Local function prototypes */
74 static char *java_accstring_lookup PARAMS ((int));
75 static void classitf_redefinition_error PARAMS ((const char *,tree, tree, tree));
76 static void variable_redefinition_error PARAMS ((tree, tree, tree, int));
77 static tree create_class PARAMS ((int, tree, tree, tree));
78 static tree create_interface PARAMS ((int, tree, tree));
79 static void end_class_declaration PARAMS ((int));
80 static tree find_field PARAMS ((tree, tree));
81 static tree lookup_field_wrapper PARAMS ((tree, tree));
82 static int duplicate_declaration_error_p PARAMS ((tree, tree, tree));
83 static void register_fields PARAMS ((int, tree, tree));
84 static tree parser_qualified_classname PARAMS ((tree));
85 static int parser_check_super PARAMS ((tree, tree, tree));
86 static int parser_check_super_interface PARAMS ((tree, tree, tree));
87 static void check_modifiers_consistency PARAMS ((int));
88 static tree lookup_cl PARAMS ((tree));
89 static tree lookup_java_method2 PARAMS ((tree, tree, int));
90 static tree method_header PARAMS ((int, tree, tree, tree));
91 static void fix_method_argument_names PARAMS ((tree ,tree));
92 static tree method_declarator PARAMS ((tree, tree));
93 static void parse_warning_context PARAMS ((tree cl, const char *msg, ...))
95 static void issue_warning_error_from_context PARAMS ((tree, const char *msg, va_list));
96 static void parse_ctor_invocation_error PARAMS ((void));
97 static tree parse_jdk1_1_error PARAMS ((const char *));
98 static void complete_class_report_errors PARAMS ((jdep *));
99 static int process_imports PARAMS ((void));
100 static void read_import_dir PARAMS ((tree));
101 static int find_in_imports_on_demand PARAMS ((tree));
102 static void find_in_imports PARAMS ((tree));
103 static void check_inner_class_access PARAMS ((tree, tree, tree));
104 static int check_pkg_class_access PARAMS ((tree, tree));
105 static void register_package PARAMS ((tree));
106 static tree resolve_package PARAMS ((tree, tree *));
107 static tree lookup_package_type PARAMS ((const char *, int));
108 static tree lookup_package_type_and_set_next PARAMS ((const char *, int, tree *));
109 static tree resolve_class PARAMS ((tree, tree, tree, tree));
110 static void declare_local_variables PARAMS ((int, tree, tree));
111 static void source_start_java_method PARAMS ((tree));
112 static void source_end_java_method PARAMS ((void));
113 static void expand_start_java_method PARAMS ((tree));
114 static tree find_name_in_single_imports PARAMS ((tree));
115 static void check_abstract_method_header PARAMS ((tree));
116 static tree lookup_java_interface_method2 PARAMS ((tree, tree));
117 static tree resolve_expression_name PARAMS ((tree, tree *));
118 static tree maybe_create_class_interface_decl PARAMS ((tree, tree, tree, tree));
119 static int check_class_interface_creation PARAMS ((int, int, tree,
121 static tree patch_method_invocation PARAMS ((tree, tree, tree,
123 static int breakdown_qualified PARAMS ((tree *, tree *, tree));
124 static tree resolve_and_layout PARAMS ((tree, tree));
125 static tree qualify_and_find PARAMS ((tree, tree, tree));
126 static tree resolve_no_layout PARAMS ((tree, tree));
127 static int invocation_mode PARAMS ((tree, int));
128 static tree find_applicable_accessible_methods_list PARAMS ((int, tree,
130 static void search_applicable_methods_list PARAMS ((int, tree, tree, tree,
132 static tree find_most_specific_methods_list PARAMS ((tree));
133 static int argument_types_convertible PARAMS ((tree, tree));
134 static tree patch_invoke PARAMS ((tree, tree, tree));
135 static int maybe_use_access_method PARAMS ((int, tree *, tree *));
136 static tree lookup_method_invoke PARAMS ((int, tree, tree, tree, tree));
137 static tree register_incomplete_type PARAMS ((int, tree, tree, tree));
138 static tree obtain_incomplete_type PARAMS ((tree));
139 static tree java_complete_lhs PARAMS ((tree));
140 static tree java_complete_tree PARAMS ((tree));
141 static tree maybe_generate_pre_expand_clinit PARAMS ((tree));
142 static int maybe_yank_clinit PARAMS ((tree));
143 static void java_complete_expand_method PARAMS ((tree));
144 static int unresolved_type_p PARAMS ((tree, tree *));
145 static void create_jdep_list PARAMS ((struct parser_ctxt *));
146 static tree build_expr_block PARAMS ((tree, tree));
147 static tree enter_block PARAMS ((void));
148 static tree enter_a_block PARAMS ((tree));
149 static tree exit_block PARAMS ((void));
150 static tree lookup_name_in_blocks PARAMS ((tree));
151 static void maybe_absorb_scoping_blocks PARAMS ((void));
152 static tree build_method_invocation PARAMS ((tree, tree));
153 static tree build_new_invocation PARAMS ((tree, tree));
154 static tree build_assignment PARAMS ((int, int, tree, tree));
155 static tree build_binop PARAMS ((enum tree_code, int, tree, tree));
156 static int check_final_assignment PARAMS ((tree ,tree));
157 static tree patch_assignment PARAMS ((tree, tree, tree ));
158 static tree patch_binop PARAMS ((tree, tree, tree));
159 static tree build_unaryop PARAMS ((int, int, tree));
160 static tree build_incdec PARAMS ((int, int, tree, int));
161 static tree patch_unaryop PARAMS ((tree, tree));
162 static tree build_cast PARAMS ((int, tree, tree));
163 static tree build_null_of_type PARAMS ((tree));
164 static tree patch_cast PARAMS ((tree, tree));
165 static int valid_ref_assignconv_cast_p PARAMS ((tree, tree, int));
166 static int valid_builtin_assignconv_identity_widening_p PARAMS ((tree, tree));
167 static int valid_cast_to_p PARAMS ((tree, tree));
168 static int valid_method_invocation_conversion_p PARAMS ((tree, tree));
169 static tree try_builtin_assignconv PARAMS ((tree, tree, tree));
170 static tree try_reference_assignconv PARAMS ((tree, tree));
171 static tree build_unresolved_array_type PARAMS ((tree));
172 static tree build_array_from_name PARAMS ((tree, tree, tree, tree *));
173 static tree build_array_ref PARAMS ((int, tree, tree));
174 static tree patch_array_ref PARAMS ((tree));
175 static tree make_qualified_name PARAMS ((tree, tree, int));
176 static tree merge_qualified_name PARAMS ((tree, tree));
177 static tree make_qualified_primary PARAMS ((tree, tree, int));
178 static int resolve_qualified_expression_name PARAMS ((tree, tree *,
180 static void qualify_ambiguous_name PARAMS ((tree));
181 static tree resolve_field_access PARAMS ((tree, tree *, tree *));
182 static tree build_newarray_node PARAMS ((tree, tree, int));
183 static tree patch_newarray PARAMS ((tree));
184 static tree resolve_type_during_patch PARAMS ((tree));
185 static tree build_this PARAMS ((int));
186 static tree build_wfl_wrap PARAMS ((tree, int));
187 static tree build_return PARAMS ((int, tree));
188 static tree patch_return PARAMS ((tree));
189 static tree maybe_access_field PARAMS ((tree, tree, tree));
190 static int complete_function_arguments PARAMS ((tree));
191 static int check_for_static_method_reference PARAMS ((tree, tree, tree,
193 static int not_accessible_p PARAMS ((tree, tree, int));
194 static void check_deprecation PARAMS ((tree, tree));
195 static int class_in_current_package PARAMS ((tree));
196 static tree build_if_else_statement PARAMS ((int, tree, tree, tree));
197 static tree patch_if_else_statement PARAMS ((tree));
198 static tree add_stmt_to_compound PARAMS ((tree, tree, tree));
199 static tree add_stmt_to_block PARAMS ((tree, tree, tree));
200 static tree patch_exit_expr PARAMS ((tree));
201 static tree build_labeled_block PARAMS ((int, tree));
202 static tree finish_labeled_statement PARAMS ((tree, tree));
203 static tree build_bc_statement PARAMS ((int, int, tree));
204 static tree patch_bc_statement PARAMS ((tree));
205 static tree patch_loop_statement PARAMS ((tree));
206 static tree build_new_loop PARAMS ((tree));
207 static tree build_loop_body PARAMS ((int, tree, int));
208 static tree finish_loop_body PARAMS ((int, tree, tree, int));
209 static tree build_debugable_stmt PARAMS ((int, tree));
210 static tree finish_for_loop PARAMS ((int, tree, tree, tree));
211 static tree patch_switch_statement PARAMS ((tree));
212 static tree string_constant_concatenation PARAMS ((tree, tree));
213 static tree build_string_concatenation PARAMS ((tree, tree));
214 static tree patch_string_cst PARAMS ((tree));
215 static tree patch_string PARAMS ((tree));
216 static tree build_try_statement PARAMS ((int, tree, tree));
217 static tree build_try_finally_statement PARAMS ((int, tree, tree));
218 static tree patch_try_statement PARAMS ((tree));
219 static tree patch_synchronized_statement PARAMS ((tree, tree));
220 static tree patch_throw_statement PARAMS ((tree, tree));
221 static void check_thrown_exceptions PARAMS ((int, tree));
222 static int check_thrown_exceptions_do PARAMS ((tree));
223 static void purge_unchecked_exceptions PARAMS ((tree));
224 static void check_throws_clauses PARAMS ((tree, tree, tree));
225 static void finish_method_declaration PARAMS ((tree));
226 static tree build_super_invocation PARAMS ((tree));
227 static int verify_constructor_circularity PARAMS ((tree, tree));
228 static char *constructor_circularity_msg PARAMS ((tree, tree));
229 static tree build_this_super_qualified_invocation PARAMS ((int, tree, tree,
231 static const char *get_printable_method_name PARAMS ((tree));
232 static tree patch_conditional_expr PARAMS ((tree, tree, tree));
233 static tree generate_finit PARAMS ((tree));
234 static void add_instance_initializer PARAMS ((tree));
235 static void fix_constructors PARAMS ((tree));
236 static tree build_alias_initializer_parameter_list PARAMS ((int, tree,
238 static void craft_constructor PARAMS ((tree, tree));
239 static int verify_constructor_super PARAMS ((tree));
240 static tree create_artificial_method PARAMS ((tree, int, tree, tree, tree));
241 static void start_artificial_method_body PARAMS ((tree));
242 static void end_artificial_method_body PARAMS ((tree));
243 static int check_method_redefinition PARAMS ((tree, tree));
244 static int reset_method_name PARAMS ((tree));
245 static int check_method_types_complete PARAMS ((tree));
246 static void java_check_regular_methods PARAMS ((tree));
247 static void java_check_abstract_methods PARAMS ((tree));
248 static tree maybe_build_primttype_type_ref PARAMS ((tree, tree));
249 static void unreachable_stmt_error PARAMS ((tree));
250 static tree find_expr_with_wfl PARAMS ((tree));
251 static void missing_return_error PARAMS ((tree));
252 static tree build_new_array_init PARAMS ((int, tree));
253 static tree patch_new_array_init PARAMS ((tree, tree));
254 static tree maybe_build_array_element_wfl PARAMS ((tree));
255 static int array_constructor_check_entry PARAMS ((tree, tree));
256 static const char *purify_type_name PARAMS ((const char *));
257 static tree fold_constant_for_init PARAMS ((tree, tree));
258 static tree strip_out_static_field_access_decl PARAMS ((tree));
259 static jdeplist *reverse_jdep_list PARAMS ((struct parser_ctxt *));
260 static void static_ref_err PARAMS ((tree, tree, tree));
261 static void parser_add_interface PARAMS ((tree, tree, tree));
262 static void add_superinterfaces PARAMS ((tree, tree));
263 static tree jdep_resolve_class PARAMS ((jdep *));
264 static int note_possible_classname PARAMS ((const char *, int));
265 static void java_complete_expand_classes PARAMS ((void));
266 static void java_complete_expand_class PARAMS ((tree));
267 static void java_complete_expand_methods PARAMS ((tree));
268 static tree cut_identifier_in_qualified PARAMS ((tree));
269 static tree java_stabilize_reference PARAMS ((tree));
270 static tree do_unary_numeric_promotion PARAMS ((tree));
271 static char * operator_string PARAMS ((tree));
272 static tree do_merge_string_cste PARAMS ((tree, const char *, int, int));
273 static tree merge_string_cste PARAMS ((tree, tree, int));
274 static tree java_refold PARAMS ((tree));
275 static int java_decl_equiv PARAMS ((tree, tree));
276 static int binop_compound_p PARAMS ((enum tree_code));
277 static tree search_loop PARAMS ((tree));
278 static int labeled_block_contains_loop_p PARAMS ((tree, tree));
279 static void check_abstract_method_definitions PARAMS ((int, tree, tree));
280 static void java_check_abstract_method_definitions PARAMS ((tree));
281 static void java_debug_context_do PARAMS ((int));
282 static void java_parser_context_push_initialized_field PARAMS ((void));
283 static void java_parser_context_pop_initialized_field PARAMS ((void));
284 static tree reorder_static_initialized PARAMS ((tree));
285 static void java_parser_context_suspend PARAMS ((void));
286 static void java_parser_context_resume PARAMS ((void));
288 /* JDK 1.1 work. FIXME */
290 static tree maybe_make_nested_class_name PARAMS ((tree));
291 static void make_nested_class_name PARAMS ((tree));
292 static void set_nested_class_simple_name_value PARAMS ((tree, int));
293 static void link_nested_class_to_enclosing PARAMS ((void));
294 static tree find_as_inner_class PARAMS ((tree, tree, tree));
295 static tree find_as_inner_class_do PARAMS ((tree, tree));
296 static int check_inner_class_redefinition PARAMS ((tree, tree));
298 static tree build_thisn_assign PARAMS ((void));
299 static tree build_current_thisn PARAMS ((tree));
300 static tree build_access_to_thisn PARAMS ((tree, tree, int));
301 static tree maybe_build_thisn_access_method PARAMS ((tree));
303 static tree build_outer_field_access PARAMS ((tree, tree));
304 static tree build_outer_field_access_methods PARAMS ((tree));
305 static tree build_outer_field_access_expr PARAMS ((int, tree, tree,
307 static tree build_outer_method_access_method PARAMS ((tree));
308 static tree build_new_access_id PARAMS ((void));
309 static tree build_outer_field_access_method PARAMS ((tree, tree, tree,
312 static int outer_field_access_p PARAMS ((tree, tree));
313 static int outer_field_expanded_access_p PARAMS ((tree, tree *,
315 static tree outer_field_access_fix PARAMS ((tree, tree, tree));
316 static tree build_incomplete_class_ref PARAMS ((int, tree));
317 static tree patch_incomplete_class_ref PARAMS ((tree));
318 static tree create_anonymous_class PARAMS ((int, tree));
319 static void patch_anonymous_class PARAMS ((tree, tree, tree));
320 static void add_inner_class_fields PARAMS ((tree, tree));
322 static tree build_dot_class_method PARAMS ((tree));
323 static tree build_dot_class_method_invocation PARAMS ((tree));
324 static void create_new_parser_context PARAMS ((int));
326 /* Number of error found so far. */
327 int java_error_count;
328 /* Number of warning found so far. */
329 int java_warning_count;
330 /* Tell when not to fold, when doing xrefs */
332 /* Cyclic inheritance report, as it can be set by layout_class */
333 char *cyclic_inheritance_report;
335 /* Tell when we're within an instance initializer */
336 static int in_instance_initializer;
338 /* The current parser context */
339 struct parser_ctxt *ctxp;
341 /* List of things that were analyzed for which code will be generated */
342 static struct parser_ctxt *ctxp_for_generation = NULL;
344 /* binop_lookup maps token to tree_code. It is used where binary
345 operations are involved and required by the parser. RDIV_EXPR
346 covers both integral/floating point division. The code is changed
347 once the type of both operator is worked out. */
349 static enum tree_code binop_lookup[19] =
351 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
352 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
353 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
354 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
355 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
357 #define BINOP_LOOKUP(VALUE) \
358 binop_lookup [((VALUE) - PLUS_TK)% \
359 (sizeof (binop_lookup) / sizeof (binop_lookup[0]))]
361 /* This is the end index for binary operators that can also be used
362 in compound assignements. */
363 #define BINOP_COMPOUND_CANDIDATES 11
365 /* Fake WFL used to report error message. It is initialized once if
366 needed and reused with it's location information is overriden. */
367 tree wfl_operator = NULL_TREE;
369 /* The "$L" identifier we use to create labels. */
370 static tree label_id = NULL_TREE;
372 /* The "StringBuffer" identifier used for the String `+' operator. */
373 static tree wfl_string_buffer = NULL_TREE;
375 /* The "append" identifier used for String `+' operator. */
376 static tree wfl_append = NULL_TREE;
378 /* The "toString" identifier used for String `+' operator. */
379 static tree wfl_to_string = NULL_TREE;
381 /* The "java.lang" import qualified name. */
382 static tree java_lang_id = NULL_TREE;
384 /* The generated `inst$' identifier used for generated enclosing
385 instance/field access functions. */
386 static tree inst_id = NULL_TREE;
388 /* The "java.lang.Cloneable" qualified name. */
389 static tree java_lang_cloneable = 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 /* Check modifiers. If one doesn't fit, retrieve it in its declaration
401 line and point it out. */
402 /* Should point out the one that don't fit. ASCII/unicode, going
405 #define check_modifiers(__message, __value, __mask) do { \
406 if ((__value) & ~(__mask)) \
408 int i, remainder = (__value) & ~(__mask); \
409 for (i = 0; i <= 10; i++) \
410 if ((1 << i) & remainder) \
411 parse_error_context (ctxp->modifier_ctx [i], (__message), \
412 java_accstring_lookup (1 << i)); \
434 /* Things defined here have to match the order of what's in the
435 binop_lookup table. */
437 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
438 %token LS_TK SRS_TK ZRS_TK
439 %token AND_TK XOR_TK OR_TK
440 %token BOOL_AND_TK BOOL_OR_TK
441 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
443 /* This maps to the same binop_lookup entry than the token above */
445 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
447 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
448 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
451 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
453 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
454 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
455 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
456 %token PAD_TK ABSTRACT_TK MODIFIER_TK
458 /* Keep those two in order, too */
459 %token DECR_TK INCR_TK
461 /* From now one, things can be in any order */
463 %token DEFAULT_TK IF_TK THROW_TK
464 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
465 %token THROWS_TK BREAK_TK IMPORT_TK
466 %token ELSE_TK INSTANCEOF_TK RETURN_TK
467 %token VOID_TK CATCH_TK INTERFACE_TK
468 %token CASE_TK EXTENDS_TK FINALLY_TK
469 %token SUPER_TK WHILE_TK CLASS_TK
470 %token SWITCH_TK CONST_TK TRY_TK
471 %token FOR_TK NEW_TK CONTINUE_TK
472 %token GOTO_TK PACKAGE_TK THIS_TK
474 %token BYTE_TK SHORT_TK INT_TK LONG_TK
475 %token CHAR_TK INTEGRAL_TK
477 %token FLOAT_TK DOUBLE_TK FP_TK
481 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
483 %token ASSIGN_ANY_TK ASSIGN_TK
484 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
486 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
487 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
489 %type <value> modifiers MODIFIER_TK final synchronized
491 %type <node> super ID_TK identifier
492 %type <node> name simple_name qualified_name
493 %type <node> type_declaration compilation_unit
494 field_declaration method_declaration extends_interfaces
495 interfaces interface_type_list
496 class_member_declaration
497 import_declarations package_declaration
498 type_declarations interface_body
499 interface_member_declaration constant_declaration
500 interface_member_declarations interface_type
501 abstract_method_declaration interface_type_list
502 %type <node> class_body_declaration class_member_declaration
503 static_initializer constructor_declaration block
504 %type <node> class_body_declarations constructor_header
505 %type <node> class_or_interface_type class_type class_type_list
506 constructor_declarator explicit_constructor_invocation
507 %type <node> dim_expr dim_exprs this_or_super throws
509 %type <node> variable_declarator_id variable_declarator
510 variable_declarators variable_initializer
511 variable_initializers constructor_body
514 %type <node> class_body block_end constructor_block_end
515 %type <node> statement statement_without_trailing_substatement
516 labeled_statement if_then_statement label_decl
517 if_then_else_statement while_statement for_statement
518 statement_nsi labeled_statement_nsi do_statement
519 if_then_else_statement_nsi while_statement_nsi
520 for_statement_nsi statement_expression_list for_init
521 for_update statement_expression expression_statement
522 primary_no_new_array expression primary
523 array_creation_expression array_type
524 class_instance_creation_expression field_access
525 method_invocation array_access something_dot_new
526 argument_list postfix_expression while_expression
527 post_increment_expression post_decrement_expression
528 unary_expression_not_plus_minus unary_expression
529 pre_increment_expression pre_decrement_expression
530 unary_expression_not_plus_minus cast_expression
531 multiplicative_expression additive_expression
532 shift_expression relational_expression
533 equality_expression and_expression
534 exclusive_or_expression inclusive_or_expression
535 conditional_and_expression conditional_or_expression
536 conditional_expression assignment_expression
537 left_hand_side assignment for_header for_begin
538 constant_expression do_statement_begin empty_statement
539 switch_statement synchronized_statement throw_statement
540 try_statement switch_expression switch_block
541 catches catch_clause catch_clause_parameter finally
542 anonymous_class_creation
543 %type <node> return_statement break_statement continue_statement
545 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
546 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
547 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
548 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
549 %type <operator> ASSIGN_ANY_TK assignment_operator
550 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
551 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
552 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
553 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK
554 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
555 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
556 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
557 %type <operator> NEW_TK
559 %type <node> method_body
561 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
562 STRING_LIT_TK NULL_TK VOID_TK
564 %type <node> IF_TK WHILE_TK FOR_TK
566 %type <node> formal_parameter_list formal_parameter
567 method_declarator method_header
569 %type <node> primitive_type reference_type type
570 BOOLEAN_TK INTEGRAL_TK FP_TK
572 /* Added or modified JDK 1.1 rule types */
573 %type <node> type_literals array_type_literal
576 /* 19.2 Production from 2.3: The Syntactic Grammar */
582 /* 19.3 Productions from 3: Lexical structure */
592 /* 19.4 Productions from 4: Types, Values and Variables */
605 class_or_interface_type
609 class_or_interface_type:
614 class_or_interface_type /* Default rule */
618 class_or_interface_type
622 primitive_type OSB_TK CSB_TK
624 $$ = build_java_array_type ($1, -1);
625 CLASS_LOADED_P ($$) = 1;
628 { $$ = build_unresolved_array_type ($1); }
629 | array_type OSB_TK CSB_TK
630 { $$ = build_unresolved_array_type ($1); }
631 | primitive_type OSB_TK error
632 {RULE ("']' expected"); RECOVER;}
633 | array_type OSB_TK error
634 {RULE ("']' expected"); RECOVER;}
637 /* 19.5 Productions from 6: Names */
639 simple_name /* Default rule */
640 | qualified_name /* Default rule */
644 identifier /* Default rule */
646 if (strchr (IDENTIFIER_POINTER (EXPR_WFL_NODE ($$)), '$'))
647 parse_error_context ($$, "Invalid type name `%s'",
648 IDENTIFIER_POINTER (EXPR_WFL_NODE ($$)));
653 name DOT_TK identifier
654 { $$ = make_qualified_name ($1, $3, $2.location); }
661 /* 19.6: Production from 7: Packages */
664 | package_declaration
665 | import_declarations
667 | package_declaration import_declarations
668 | package_declaration type_declarations
669 | import_declarations type_declarations
670 | package_declaration import_declarations type_declarations
678 | import_declarations import_declaration
686 | type_declarations type_declaration
690 PACKAGE_TK name SC_TK
692 ctxp->package = EXPR_WFL_NODE ($2);
693 register_package (ctxp->package);
696 {yyerror ("Missing name"); RECOVER;}
697 | PACKAGE_TK name error
698 {yyerror ("';' expected"); RECOVER;}
702 single_type_import_declaration
703 | type_import_on_demand_declaration
706 single_type_import_declaration:
709 tree name = EXPR_WFL_NODE ($2), last_name;
710 int i = IDENTIFIER_LENGTH (name)-1;
711 const char *last = &IDENTIFIER_POINTER (name)[i];
712 while (last != IDENTIFIER_POINTER (name))
718 last_name = get_identifier (++last);
719 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
721 tree err = find_name_in_single_imports (last_name);
722 if (err && err != name)
724 ($2, "Ambiguous class: `%s' and `%s'",
725 IDENTIFIER_POINTER (name),
726 IDENTIFIER_POINTER (err));
728 REGISTER_IMPORT ($2, last_name);
731 REGISTER_IMPORT ($2, last_name);
734 {yyerror ("Missing name"); RECOVER;}
735 | IMPORT_TK name error
736 {yyerror ("';' expected"); RECOVER;}
739 type_import_on_demand_declaration:
740 IMPORT_TK name DOT_TK MULT_TK SC_TK
742 tree name = EXPR_WFL_NODE ($2);
743 /* Don't import java.lang.* twice. */
744 if (name != java_lang_id)
746 read_import_dir ($2);
747 ctxp->import_demand_list =
748 chainon (ctxp->import_demand_list,
749 build_tree_list ($2, NULL_TREE));
752 | IMPORT_TK name DOT_TK error
753 {yyerror ("'*' expected"); RECOVER;}
754 | IMPORT_TK name DOT_TK MULT_TK error
755 {yyerror ("';' expected"); RECOVER;}
760 { end_class_declaration (0); }
761 | interface_declaration
762 { end_class_declaration (0); }
768 yyerror ("Class or interface declaration expected");
772 /* 19.7 Shortened from the original:
773 modifiers: modifier | modifiers modifier
774 modifier: any of public... */
780 | modifiers MODIFIER_TK
785 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
786 java_accstring_lookup (acc));
794 /* 19.8.1 Production from $8.1: Class Declaration */
796 modifiers CLASS_TK identifier super interfaces
797 { create_class ($1, $3, $4, $5); }
799 | CLASS_TK identifier super interfaces
800 { create_class (0, $2, $3, $4); }
802 | modifiers CLASS_TK error
803 {yyerror ("Missing class name"); RECOVER;}
805 {yyerror ("Missing class name"); RECOVER;}
806 | CLASS_TK identifier error
808 if (!ctxp->class_err) yyerror ("'{' expected");
811 | modifiers CLASS_TK identifier error
812 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
817 | EXTENDS_TK class_type
819 | EXTENDS_TK class_type error
820 {yyerror ("'{' expected"); ctxp->class_err=1;}
822 {yyerror ("Missing super class name"); ctxp->class_err=1;}
827 | IMPLEMENTS_TK interface_type_list
829 | IMPLEMENTS_TK error
832 yyerror ("Missing interface name");
839 ctxp->interface_number = 1;
840 $$ = build_tree_list ($1, NULL_TREE);
842 | interface_type_list C_TK interface_type
844 ctxp->interface_number++;
845 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
847 | interface_type_list C_TK error
848 {yyerror ("Missing interface name"); RECOVER;}
854 /* Store the location of the `}' when doing xrefs */
856 DECL_END_SOURCE_LINE (GET_CPC ()) =
857 EXPR_WFL_ADD_COL ($2.location, 1);
860 | OCB_TK class_body_declarations CCB_TK
862 /* Store the location of the `}' when doing xrefs */
864 DECL_END_SOURCE_LINE (GET_CPC ()) =
865 EXPR_WFL_ADD_COL ($3.location, 1);
870 class_body_declarations:
871 class_body_declaration
872 | class_body_declarations class_body_declaration
875 class_body_declaration:
876 class_member_declaration
878 | constructor_declaration
879 | block /* Added, JDK1.1, instance initializer */
881 TREE_CHAIN ($1) = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
882 SET_CPC_INSTANCE_INITIALIZER_STMT (ctxp, $1);
886 class_member_declaration:
888 | field_declaration SC_TK
891 | class_declaration /* Added, JDK1.1 inner classes */
892 { end_class_declaration (1); }
893 | interface_declaration /* Added, JDK1.1 inner interfaces */
894 { end_class_declaration (1); }
897 /* 19.8.2 Productions from 8.3: Field Declarations */
899 type variable_declarators SC_TK
900 { register_fields (0, $1, $2); }
901 | modifiers type variable_declarators SC_TK
904 ("Illegal modifier `%s' for field declaration",
905 $1, FIELD_MODIFIERS);
906 check_modifiers_consistency ($1);
907 register_fields ($1, $2, $3);
911 variable_declarators:
912 /* Should we use build_decl_list () instead ? FIXME */
913 variable_declarator /* Default rule */
914 | variable_declarators C_TK variable_declarator
915 { $$ = chainon ($1, $3); }
916 | variable_declarators C_TK error
917 {yyerror ("Missing term"); RECOVER;}
921 variable_declarator_id
922 { $$ = build_tree_list ($1, NULL_TREE); }
923 | variable_declarator_id ASSIGN_TK variable_initializer
925 if (java_error_count)
928 ($1, build_assignment ($2.token, $2.location, $1, $3));
930 | variable_declarator_id ASSIGN_TK error
932 yyerror ("Missing variable initializer");
933 $$ = build_tree_list ($1, NULL_TREE);
936 | variable_declarator_id ASSIGN_TK variable_initializer error
938 yyerror ("';' expected");
939 $$ = build_tree_list ($1, NULL_TREE);
944 variable_declarator_id:
946 | variable_declarator_id OSB_TK CSB_TK
947 { $$ = build_unresolved_array_type ($1); }
949 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
950 | variable_declarator_id OSB_TK error
951 {yyerror ("']' expected"); DRECOVER(vdi);}
952 | variable_declarator_id CSB_TK error
953 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
956 variable_initializer:
961 /* 19.8.3 Productions from 8.4: Method Declarations */
965 current_function_decl = $1;
966 if (current_function_decl
967 && TREE_CODE (current_function_decl) == FUNCTION_DECL)
968 source_start_java_method (current_function_decl);
970 current_function_decl = NULL_TREE;
973 { finish_method_declaration ($3); }
974 | method_header error
975 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
979 type method_declarator throws
980 { $$ = method_header (0, $1, $2, $3); }
981 | VOID_TK method_declarator throws
982 { $$ = method_header (0, void_type_node, $2, $3); }
983 | modifiers type method_declarator throws
984 { $$ = method_header ($1, $2, $3, $4); }
985 | modifiers VOID_TK method_declarator throws
986 { $$ = method_header ($1, void_type_node, $3, $4); }
989 yyerror ("Invalid method declaration, method name required");
992 | modifiers type error
995 {yyerror ("Identifier expected"); RECOVER;}
996 | modifiers VOID_TK error
997 {yyerror ("Identifier expected"); RECOVER;}
1000 yyerror ("Invalid method declaration, return type required");
1006 identifier OP_TK CP_TK
1008 ctxp->formal_parameter_number = 0;
1009 $$ = method_declarator ($1, NULL_TREE);
1011 | identifier OP_TK formal_parameter_list CP_TK
1012 { $$ = method_declarator ($1, $3); }
1013 | method_declarator OSB_TK CSB_TK
1015 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
1017 build_unresolved_array_type (TREE_PURPOSE ($1));
1018 parse_warning_context
1020 "Discouraged form of returned type specification");
1022 | identifier OP_TK error
1023 {yyerror ("')' expected"); DRECOVER(method_declarator);}
1024 | method_declarator OSB_TK error
1025 {yyerror ("']' expected"); RECOVER;}
1028 formal_parameter_list:
1031 ctxp->formal_parameter_number = 1;
1033 | formal_parameter_list C_TK formal_parameter
1035 ctxp->formal_parameter_number += 1;
1036 $$ = chainon ($1, $3);
1038 | formal_parameter_list C_TK error
1039 { yyerror ("Missing formal parameter term"); RECOVER; }
1043 type variable_declarator_id
1045 $$ = build_tree_list ($2, $1);
1047 | final type variable_declarator_id /* Added, JDK1.1 final parms */
1049 $$ = build_tree_list ($3, $2);
1050 ARG_FINAL_P ($$) = 1;
1054 yyerror ("Missing identifier"); RECOVER;
1059 yyerror ("Missing identifier"); RECOVER;
1067 check_modifiers ("Illegal modifier `%s'. Only `final' was expected here",
1069 if ($1 != ACC_FINAL)
1070 MODIFIER_WFL (FINAL_TK) = build_wfl_node (NULL_TREE);
1076 | THROWS_TK class_type_list
1079 {yyerror ("Missing class type term"); RECOVER;}
1084 { $$ = build_tree_list ($1, $1); }
1085 | class_type_list C_TK class_type
1086 { $$ = tree_cons ($3, $3, $1); }
1087 | class_type_list C_TK error
1088 {yyerror ("Missing class type term"); RECOVER;}
1095 { $$ = NULL_TREE; } /* Probably not the right thing to do. */
1098 /* 19.8.4 Productions from 8.5: Static Initializers */
1102 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1103 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1105 | static block SC_TK /* Shouldn't be here. FIXME */
1107 TREE_CHAIN ($2) = CPC_STATIC_INITIALIZER_STMT (ctxp);
1108 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, $2);
1112 static: /* Test lval.sub_token here */
1115 check_modifiers ("Illegal modifier `%s' for static initializer", $1, ACC_STATIC);
1116 /* Can't have a static initializer in an innerclass */
1117 if ($1 | ACC_STATIC &&
1118 GET_CPC_LIST () && !TOPLEVEL_CLASS_DECL_P (GET_CPC ()))
1120 (MODIFIER_WFL (STATIC_TK),
1121 "Can't define static initializer in class `%s'. Static initializer can only be defined in top-level classes",
1122 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())));
1123 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1127 /* 19.8.5 Productions from 8.6: Constructor Declarations */
1128 constructor_declaration:
1131 current_function_decl = $1;
1132 source_start_java_method (current_function_decl);
1135 { finish_method_declaration ($3); }
1139 constructor_declarator throws
1140 { $$ = method_header (0, NULL_TREE, $1, $2); }
1141 | modifiers constructor_declarator throws
1142 { $$ = method_header ($1, NULL_TREE, $2, $3); }
1145 constructor_declarator:
1146 simple_name OP_TK CP_TK
1148 ctxp->formal_parameter_number = 0;
1149 $$ = method_declarator ($1, NULL_TREE);
1151 | simple_name OP_TK formal_parameter_list CP_TK
1152 { $$ = method_declarator ($1, $3); }
1156 /* Unlike regular method, we always need a complete (empty)
1157 body so we can safely perform all the required code
1158 addition (super invocation and field initialization) */
1159 block_begin constructor_block_end
1161 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
1164 | block_begin explicit_constructor_invocation constructor_block_end
1166 | block_begin block_statements constructor_block_end
1168 | block_begin explicit_constructor_invocation block_statements constructor_block_end
1172 constructor_block_end:
1176 /* Error recovery for that rule moved down expression_statement: rule. */
1177 explicit_constructor_invocation:
1178 this_or_super OP_TK CP_TK SC_TK
1180 $$ = build_method_invocation ($1, NULL_TREE);
1181 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1182 $$ = java_method_add_stmt (current_function_decl, $$);
1184 | this_or_super OP_TK argument_list CP_TK SC_TK
1186 $$ = build_method_invocation ($1, $3);
1187 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
1188 $$ = java_method_add_stmt (current_function_decl, $$);
1190 /* Added, JDK1.1 inner classes. Modified because the rule
1191 'primary' couldn't work. */
1192 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
1193 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1194 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
1195 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
1198 this_or_super: /* Added, simplifies error diagnostics */
1201 tree wfl = build_wfl_node (this_identifier_node);
1202 EXPR_WFL_LINECOL (wfl) = $1.location;
1207 tree wfl = build_wfl_node (super_identifier_node);
1208 EXPR_WFL_LINECOL (wfl) = $1.location;
1213 /* 19.9 Productions from 9: Interfaces */
1214 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1215 interface_declaration:
1216 INTERFACE_TK identifier
1217 { create_interface (0, $2, NULL_TREE); }
1219 | modifiers INTERFACE_TK identifier
1220 { create_interface ($1, $3, NULL_TREE); }
1222 | INTERFACE_TK identifier extends_interfaces
1223 { create_interface (0, $2, $3); }
1225 | modifiers INTERFACE_TK identifier extends_interfaces
1226 { create_interface ($1, $3, $4); }
1228 | INTERFACE_TK identifier error
1229 {yyerror ("'{' expected"); RECOVER;}
1230 | modifiers INTERFACE_TK identifier error
1231 {yyerror ("'{' expected"); RECOVER;}
1235 EXTENDS_TK interface_type
1237 ctxp->interface_number = 1;
1238 $$ = build_tree_list ($2, NULL_TREE);
1240 | extends_interfaces C_TK interface_type
1242 ctxp->interface_number++;
1243 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1246 {yyerror ("Invalid interface type"); RECOVER;}
1247 | extends_interfaces C_TK error
1248 {yyerror ("Missing term"); RECOVER;}
1254 | OCB_TK interface_member_declarations CCB_TK
1258 interface_member_declarations:
1259 interface_member_declaration
1260 | interface_member_declarations interface_member_declaration
1263 interface_member_declaration:
1264 constant_declaration
1265 | abstract_method_declaration
1266 | class_declaration /* Added, JDK1.1 inner classes */
1267 { end_class_declaration (1); }
1268 | interface_declaration /* Added, JDK1.1 inner interfaces */
1269 { end_class_declaration (1); }
1272 constant_declaration:
1276 abstract_method_declaration:
1279 check_abstract_method_header ($1);
1280 current_function_decl = NULL_TREE; /* FIXME ? */
1282 | method_header error
1283 {yyerror ("';' expected"); RECOVER;}
1286 /* 19.10 Productions from 10: Arrays */
1289 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1290 | OCB_TK variable_initializers CCB_TK
1291 { $$ = build_new_array_init ($1.location, $2); }
1292 | OCB_TK variable_initializers C_TK CCB_TK
1293 { $$ = build_new_array_init ($1.location, $2); }
1296 variable_initializers:
1297 variable_initializer
1299 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1302 | variable_initializers C_TK variable_initializer
1304 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1306 | variable_initializers C_TK error
1307 {yyerror ("Missing term"); RECOVER;}
1310 /* 19.11 Production from 14: Blocks and Statements */
1314 /* Store the location of the `}' when doing xrefs */
1315 if (current_function_decl && flag_emit_xref)
1316 DECL_END_SOURCE_LINE (current_function_decl) =
1317 EXPR_WFL_ADD_COL ($2.location, 1);
1318 $$ = empty_stmt_node;
1320 | block_begin block_statements block_end
1332 maybe_absorb_scoping_blocks ();
1333 /* Store the location of the `}' when doing xrefs */
1334 if (current_function_decl && flag_emit_xref)
1335 DECL_END_SOURCE_LINE (current_function_decl) =
1336 EXPR_WFL_ADD_COL ($1.location, 1);
1338 if (!BLOCK_SUBBLOCKS ($$))
1339 BLOCK_SUBBLOCKS ($$) = empty_stmt_node;
1345 | block_statements block_statement
1349 local_variable_declaration_statement
1351 { java_method_add_stmt (current_function_decl, $1); }
1352 | class_declaration /* Added, JDK1.1 local classes */
1354 LOCAL_CLASS_P (TREE_TYPE (GET_CPC ())) = 1;
1355 end_class_declaration (1);
1359 local_variable_declaration_statement:
1360 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1363 local_variable_declaration:
1364 type variable_declarators
1365 { declare_local_variables (0, $1, $2); }
1366 | final type variable_declarators /* Added, JDK1.1 final locals */
1367 { declare_local_variables ($1, $2, $3); }
1371 statement_without_trailing_substatement
1374 | if_then_else_statement
1377 { $$ = exit_block (); }
1381 statement_without_trailing_substatement
1382 | labeled_statement_nsi
1383 | if_then_else_statement_nsi
1384 | while_statement_nsi
1386 { $$ = exit_block (); }
1389 statement_without_trailing_substatement:
1392 | expression_statement
1396 | continue_statement
1398 | synchronized_statement
1405 { $$ = empty_stmt_node; }
1409 identifier REL_CL_TK
1411 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1412 EXPR_WFL_NODE ($1));
1414 push_labeled_block ($$);
1415 PUSH_LABELED_BLOCK ($$);
1420 label_decl statement
1421 { $$ = finish_labeled_statement ($1, $2); }
1423 {yyerror ("':' expected"); RECOVER;}
1426 labeled_statement_nsi:
1427 label_decl statement_nsi
1428 { $$ = finish_labeled_statement ($1, $2); }
1431 /* We concentrate here a bunch of error handling rules that we couldn't write
1432 earlier, because expression_statement catches a missing ';'. */
1433 expression_statement:
1434 statement_expression SC_TK
1436 /* We have a statement. Generate a WFL around it so
1438 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1439 /* We know we have a statement, so set the debug
1440 info to be eventually generate here. */
1441 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1445 if (ctxp->prevent_ese != lineno)
1446 yyerror ("Invalid expression statement");
1447 DRECOVER (expr_stmt);
1451 if (ctxp->prevent_ese != lineno)
1452 yyerror ("Invalid expression statement");
1453 DRECOVER (expr_stmt);
1457 if (ctxp->prevent_ese != lineno)
1458 yyerror ("Invalid expression statement");
1459 DRECOVER (expr_stmt);
1461 | this_or_super OP_TK error
1462 {yyerror ("')' expected"); RECOVER;}
1463 | this_or_super OP_TK CP_TK error
1465 parse_ctor_invocation_error ();
1468 | this_or_super OP_TK argument_list error
1469 {yyerror ("')' expected"); RECOVER;}
1470 | this_or_super OP_TK argument_list CP_TK error
1472 parse_ctor_invocation_error ();
1475 | name DOT_TK SUPER_TK error
1476 {yyerror ("'(' expected"); RECOVER;}
1477 | name DOT_TK SUPER_TK OP_TK error
1478 {yyerror ("')' expected"); RECOVER;}
1479 | name DOT_TK SUPER_TK OP_TK argument_list error
1480 {yyerror ("')' expected"); RECOVER;}
1481 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1482 {yyerror ("';' expected"); RECOVER;}
1483 | name DOT_TK SUPER_TK OP_TK CP_TK error
1484 {yyerror ("';' expected"); RECOVER;}
1487 statement_expression:
1489 | pre_increment_expression
1490 | pre_decrement_expression
1491 | post_increment_expression
1492 | post_decrement_expression
1494 | class_instance_creation_expression
1498 IF_TK OP_TK expression CP_TK statement
1500 $$ = build_if_else_statement ($2.location, $3,
1504 {yyerror ("'(' expected"); RECOVER;}
1506 {yyerror ("Missing term"); RECOVER;}
1507 | IF_TK OP_TK expression error
1508 {yyerror ("')' expected"); RECOVER;}
1511 if_then_else_statement:
1512 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1513 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1516 if_then_else_statement_nsi:
1517 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1518 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1528 /* Make into "proper list" of COMPOUND_EXPRs.
1529 I.e. make the last statment also have its own
1531 maybe_absorb_scoping_blocks ();
1532 TREE_OPERAND ($1, 1) = exit_block ();
1533 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1538 SWITCH_TK OP_TK expression CP_TK
1540 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1541 EXPR_WFL_LINECOL ($$) = $2.location;
1544 {yyerror ("'(' expected"); RECOVER;}
1545 | SWITCH_TK OP_TK error
1546 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1547 | SWITCH_TK OP_TK expression CP_TK error
1548 {yyerror ("'{' expected"); RECOVER;}
1551 /* Default assignment is there to avoid type node on switch_block
1557 | OCB_TK switch_labels CCB_TK
1559 | OCB_TK switch_block_statement_groups CCB_TK
1561 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1565 switch_block_statement_groups:
1566 switch_block_statement_group
1567 | switch_block_statement_groups switch_block_statement_group
1570 switch_block_statement_group:
1571 switch_labels block_statements
1576 | switch_labels switch_label
1580 CASE_TK constant_expression REL_CL_TK
1582 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1583 EXPR_WFL_LINECOL (lab) = $1.location;
1584 java_method_add_stmt (current_function_decl, lab);
1586 | DEFAULT_TK REL_CL_TK
1588 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1589 EXPR_WFL_LINECOL (lab) = $1.location;
1590 java_method_add_stmt (current_function_decl, lab);
1593 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1594 | CASE_TK constant_expression error
1595 {yyerror ("':' expected"); RECOVER;}
1597 {yyerror ("':' expected"); RECOVER;}
1601 WHILE_TK OP_TK expression CP_TK
1603 tree body = build_loop_body ($2.location, $3, 0);
1604 $$ = build_new_loop (body);
1609 while_expression statement
1610 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1612 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1613 | WHILE_TK OP_TK error
1614 {yyerror ("Missing term and ')' expected"); RECOVER;}
1615 | WHILE_TK OP_TK expression error
1616 {yyerror ("')' expected"); RECOVER;}
1619 while_statement_nsi:
1620 while_expression statement_nsi
1621 { $$ = finish_loop_body (0, NULL_TREE, $2, 0); }
1627 tree body = build_loop_body (0, NULL_TREE, 1);
1628 $$ = build_new_loop (body);
1630 /* Need error handing here. FIXME */
1634 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1635 { $$ = finish_loop_body ($4.location, $5, $2, 1); }
1639 for_begin SC_TK expression SC_TK for_update CP_TK statement
1641 if (TREE_CODE_CLASS (TREE_CODE ($3)) == 'c')
1642 $3 = build_wfl_node ($3);
1643 $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);
1645 | for_begin SC_TK SC_TK for_update CP_TK statement
1647 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1648 /* We have not condition, so we get rid of the EXIT_EXPR */
1649 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1652 | for_begin SC_TK error
1653 {yyerror ("Invalid control expression"); RECOVER;}
1654 | for_begin SC_TK expression SC_TK error
1655 {yyerror ("Invalid update expression"); RECOVER;}
1656 | for_begin SC_TK SC_TK error
1657 {yyerror ("Invalid update expression"); RECOVER;}
1661 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1662 { $$ = finish_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1663 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1665 $$ = finish_for_loop (0, NULL_TREE, $4, $6);
1666 /* We have not condition, so we get rid of the EXIT_EXPR */
1667 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1675 /* This scope defined for local variable that may be
1676 defined within the scope of the for loop */
1680 {yyerror ("'(' expected"); DRECOVER(for_1);}
1681 | FOR_TK OP_TK error
1682 {yyerror ("Invalid init statement"); RECOVER;}
1688 /* We now declare the loop body. The loop is
1689 declared as a for loop. */
1690 tree body = build_loop_body (0, NULL_TREE, 0);
1691 $$ = build_new_loop (body);
1692 FOR_LOOP_P ($$) = 1;
1693 /* The loop is added to the current block the for
1694 statement is defined within */
1695 java_method_add_stmt (current_function_decl, $$);
1698 for_init: /* Can be empty */
1699 { $$ = empty_stmt_node; }
1700 | statement_expression_list
1702 /* Init statement recorded within the previously
1703 defined block scope */
1704 $$ = java_method_add_stmt (current_function_decl, $1);
1706 | local_variable_declaration
1708 /* Local variable are recorded within the previously
1709 defined block scope */
1712 | statement_expression_list error
1713 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1716 for_update: /* Can be empty */
1717 {$$ = empty_stmt_node;}
1718 | statement_expression_list
1719 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1722 statement_expression_list:
1723 statement_expression
1724 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1725 | statement_expression_list C_TK statement_expression
1726 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1727 | statement_expression_list C_TK error
1728 {yyerror ("Missing term"); RECOVER;}
1733 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1734 | BREAK_TK identifier SC_TK
1735 { $$ = build_bc_statement ($1.location, 1, $2); }
1737 {yyerror ("Missing term"); RECOVER;}
1738 | BREAK_TK identifier error
1739 {yyerror ("';' expected"); RECOVER;}
1744 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1745 | CONTINUE_TK identifier SC_TK
1746 { $$ = build_bc_statement ($1.location, 0, $2); }
1748 {yyerror ("Missing term"); RECOVER;}
1749 | CONTINUE_TK identifier error
1750 {yyerror ("';' expected"); RECOVER;}
1755 { $$ = build_return ($1.location, NULL_TREE); }
1756 | RETURN_TK expression SC_TK
1757 { $$ = build_return ($1.location, $2); }
1759 {yyerror ("Missing term"); RECOVER;}
1760 | RETURN_TK expression error
1761 {yyerror ("';' expected"); RECOVER;}
1765 THROW_TK expression SC_TK
1767 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1768 EXPR_WFL_LINECOL ($$) = $1.location;
1771 {yyerror ("Missing term"); RECOVER;}
1772 | THROW_TK expression error
1773 {yyerror ("';' expected"); RECOVER;}
1776 synchronized_statement:
1777 synchronized OP_TK expression CP_TK block
1779 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1780 EXPR_WFL_LINECOL ($$) =
1781 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1783 | synchronized OP_TK expression CP_TK error
1784 {yyerror ("'{' expected"); RECOVER;}
1785 | synchronized error
1786 {yyerror ("'(' expected"); RECOVER;}
1787 | synchronized OP_TK error CP_TK
1788 {yyerror ("Missing term"); RECOVER;}
1789 | synchronized OP_TK error
1790 {yyerror ("Missing term"); RECOVER;}
1797 "Illegal modifier `%s'. Only `synchronized' was expected here",
1798 $1, ACC_SYNCHRONIZED);
1799 if ($1 != ACC_SYNCHRONIZED)
1800 MODIFIER_WFL (SYNCHRONIZED_TK) =
1801 build_wfl_node (NULL_TREE);
1806 TRY_TK block catches
1807 { $$ = build_try_statement ($1.location, $2, $3); }
1808 | TRY_TK block finally
1809 { $$ = build_try_finally_statement ($1.location, $2, $3); }
1810 | TRY_TK block catches finally
1811 { $$ = build_try_finally_statement
1812 ($1.location, build_try_statement ($1.location,
1816 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1821 | catches catch_clause
1823 TREE_CHAIN ($2) = $1;
1829 catch_clause_parameter block
1831 java_method_add_stmt (current_function_decl, $2);
1836 catch_clause_parameter:
1837 CATCH_TK OP_TK formal_parameter CP_TK
1839 /* We add a block to define a scope for
1840 formal_parameter (CCBP). The formal parameter is
1841 declared initialized by the appropriate function
1843 tree ccpb = enter_block ();
1844 tree init = build_assignment (ASSIGN_TK, $2.location,
1846 soft_exceptioninfo_call_node);
1847 declare_local_variables (0, TREE_VALUE ($3),
1848 build_tree_list (TREE_PURPOSE ($3),
1850 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1851 EXPR_WFL_LINECOL ($$) = $1.location;
1854 {yyerror ("'(' expected"); RECOVER; $$ = NULL_TREE;}
1855 | CATCH_TK OP_TK error
1857 yyerror ("Missing term or ')' expected");
1858 RECOVER; $$ = NULL_TREE;
1860 | CATCH_TK OP_TK error CP_TK /* That's for () */
1861 {yyerror ("Missing term"); RECOVER; $$ = NULL_TREE;}
1868 {yyerror ("'{' expected"); RECOVER; }
1871 /* 19.12 Production from 15: Expressions */
1873 primary_no_new_array
1874 | array_creation_expression
1877 primary_no_new_array:
1880 { $$ = build_this ($1.location); }
1881 | OP_TK expression CP_TK
1883 | class_instance_creation_expression
1888 /* Added, JDK1.1 inner classes. Documentation is wrong
1889 refering to a 'ClassName' (class_name) rule that doesn't
1890 exist. Used name: instead. */
1891 | name DOT_TK THIS_TK
1893 tree wfl = build_wfl_node (this_identifier_node);
1894 $$ = make_qualified_primary ($1, wfl, EXPR_WFL_LINECOL ($1));
1896 | OP_TK expression error
1897 {yyerror ("')' expected"); RECOVER;}
1899 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1900 | primitive_type DOT_TK error
1901 {yyerror ("'class' expected" ); RECOVER;}
1902 | VOID_TK DOT_TK error
1903 {yyerror ("'class' expected" ); RECOVER;}
1906 /* Added, JDK1.1 type literals. We can't use `type' directly, so we
1907 broke the rule down a bit. */
1910 primitive_type OSB_TK CSB_TK
1912 $$ = build_java_array_type ($1, -1);
1913 CLASS_LOADED_P ($$) = 1;
1915 | name OSB_TK CSB_TK
1916 { $$ = build_unresolved_array_type ($1); }
1917 /* This triggers two reduce/reduce conflict between array_type_literal and
1919 | array_type OSB_TK CSB_TK
1920 { $$ = build_unresolved_array_type ($1); }
1925 name DOT_TK CLASS_TK
1926 { $$ = build_incomplete_class_ref ($2.location, $1); }
1927 | array_type_literal DOT_TK CLASS_TK
1928 { $$ = build_incomplete_class_ref ($2.location, $1); }
1929 | primitive_type DOT_TK CLASS_TK
1930 { $$ = build_class_ref ($1); }
1931 | VOID_TK DOT_TK CLASS_TK
1932 { $$ = build_class_ref (void_type_node); }
1935 class_instance_creation_expression:
1936 NEW_TK class_type OP_TK argument_list CP_TK
1937 { $$ = build_new_invocation ($2, $4); }
1938 | NEW_TK class_type OP_TK CP_TK
1939 { $$ = build_new_invocation ($2, NULL_TREE); }
1940 | anonymous_class_creation
1941 /* Added, JDK1.1 inner classes, modified to use name or
1942 primary instead of primary solely which couldn't work in
1944 | something_dot_new identifier OP_TK CP_TK
1946 tree ctor = build_new_invocation ($2, NULL_TREE);
1947 $$ = make_qualified_primary ($1, ctor,
1948 EXPR_WFL_LINECOL ($1));
1950 | something_dot_new identifier OP_TK CP_TK class_body
1951 | something_dot_new identifier OP_TK argument_list CP_TK
1953 tree ctor = build_new_invocation ($2, $4);
1954 $$ = make_qualified_primary ($1, ctor,
1955 EXPR_WFL_LINECOL ($1));
1957 | something_dot_new identifier OP_TK argument_list CP_TK class_body
1958 | NEW_TK error SC_TK
1959 {yyerror ("'(' expected"); DRECOVER(new_1);}
1960 | NEW_TK class_type error
1961 {yyerror ("'(' expected"); RECOVER;}
1962 | NEW_TK class_type OP_TK error
1963 {yyerror ("')' or term expected"); RECOVER;}
1964 | NEW_TK class_type OP_TK argument_list error
1965 {yyerror ("')' expected"); RECOVER;}
1966 | something_dot_new error
1967 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1968 | something_dot_new identifier error
1969 {yyerror ("'(' expected"); RECOVER;}
1972 /* Created after JDK1.1 rules originally added to
1973 class_instance_creation_expression, but modified to use
1974 'class_type' instead of 'TypeName' (type_name) which is mentionned
1975 in the documentation but doesn't exist. */
1977 anonymous_class_creation:
1978 NEW_TK class_type OP_TK argument_list CP_TK
1979 { create_anonymous_class ($1.location, $2); }
1982 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
1983 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
1985 end_class_declaration (1);
1987 /* Now we can craft the new expression */
1988 $$ = build_new_invocation (id, $4);
1990 /* Note that we can't possibly be here if
1991 `class_type' is an interface (in which case the
1992 anonymous class extends Object and implements
1993 `class_type', hence its constructor can't have
1996 /* Otherwise, the innerclass must feature a
1997 constructor matching `argument_list'. Anonymous
1998 classes are a bit special: it's impossible to
1999 define constructor for them, hence constructors
2000 must be generated following the hints provided by
2001 the `new' expression. Whether a super constructor
2002 of that nature exists or not is to be verified
2003 later on in verify_constructor_super.
2005 It's during the expansion of a `new' statement
2006 refering to an anonymous class that a ctor will
2007 be generated for the anonymous class, with the
2011 | NEW_TK class_type OP_TK CP_TK
2012 { create_anonymous_class ($1.location, $2); }
2015 tree id = build_wfl_node (DECL_NAME (GET_CPC ()));
2016 EXPR_WFL_LINECOL (id) = EXPR_WFL_LINECOL ($2);
2018 end_class_declaration (1);
2020 /* Now we can craft the new expression. The
2021 statement doesn't need to be remember so that a
2022 constructor can be generated, since its signature
2023 is already known. */
2024 $$ = build_new_invocation (id, NULL_TREE);
2028 something_dot_new: /* Added, not part of the specs. */
2031 | primary DOT_TK NEW_TK
2038 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
2039 ctxp->formal_parameter_number = 1;
2041 | argument_list C_TK expression
2043 ctxp->formal_parameter_number += 1;
2044 $$ = tree_cons (NULL_TREE, $3, $1);
2046 | argument_list C_TK error
2047 {yyerror ("Missing term"); RECOVER;}
2050 array_creation_expression:
2051 NEW_TK primitive_type dim_exprs
2052 { $$ = build_newarray_node ($2, $3, 0); }
2053 | NEW_TK class_or_interface_type dim_exprs
2054 { $$ = build_newarray_node ($2, $3, 0); }
2055 | NEW_TK primitive_type dim_exprs dims
2056 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
2057 | NEW_TK class_or_interface_type dim_exprs dims
2058 { $$ = build_newarray_node ($2, $3, CURRENT_OSB (ctxp));}
2059 /* Added, JDK1.1 anonymous array. Initial documentation rule
2061 | NEW_TK class_or_interface_type dims array_initializer
2064 while (CURRENT_OSB (ctxp)--)
2065 obstack_1grow (&temporary_obstack, '[');
2066 sig = obstack_finish (&temporary_obstack);
2067 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2068 $2, get_identifier (sig), $4);
2070 | NEW_TK primitive_type dims array_initializer
2073 while (CURRENT_OSB (ctxp)--)
2074 type = build_java_array_type (type, -1);
2075 $$ = build (NEW_ANONYMOUS_ARRAY_EXPR, NULL_TREE,
2076 build_pointer_type (type), NULL_TREE, $4);
2078 | NEW_TK error CSB_TK
2079 {yyerror ("'[' expected"); DRECOVER ("]");}
2080 | NEW_TK error OSB_TK
2081 {yyerror ("']' expected"); RECOVER;}
2086 { $$ = build_tree_list (NULL_TREE, $1); }
2087 | dim_exprs dim_expr
2088 { $$ = tree_cons (NULL_TREE, $2, $$); }
2092 OSB_TK expression CSB_TK
2094 if (JNUMERIC_TYPE_P (TREE_TYPE ($2)))
2096 $2 = build_wfl_node ($2);
2097 TREE_TYPE ($2) = NULL_TREE;
2099 EXPR_WFL_LINECOL ($2) = $1.location;
2102 | OSB_TK expression error
2103 {yyerror ("']' expected"); RECOVER;}
2106 yyerror ("Missing term");
2107 yyerror ("']' expected");
2116 /* If not initialized, allocate memory for the osb
2118 if (!ctxp->osb_limit)
2120 allocate = ctxp->osb_limit = 32;
2121 ctxp->osb_depth = -1;
2123 /* If capacity overflown, reallocate a bigger chunk */
2124 else if (ctxp->osb_depth+1 == ctxp->osb_limit)
2125 allocate = ctxp->osb_limit << 1;
2129 allocate *= sizeof (int);
2130 if (ctxp->osb_number)
2131 ctxp->osb_number = (int *)xrealloc (ctxp->osb_number,
2134 ctxp->osb_number = (int *)xmalloc (allocate);
2137 CURRENT_OSB (ctxp) = 1;
2139 | dims OSB_TK CSB_TK
2140 { CURRENT_OSB (ctxp)++; }
2142 { yyerror ("']' expected"); RECOVER;}
2146 primary DOT_TK identifier
2147 { $$ = make_qualified_primary ($1, $3, $2.location); }
2148 /* FIXME - REWRITE TO:
2149 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
2150 | SUPER_TK DOT_TK identifier
2153 build_wfl_node (super_identifier_node);
2154 EXPR_WFL_LINECOL (super_wfl) = $1.location;
2155 $$ = make_qualified_name (super_wfl, $3, $2.location);
2158 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
2163 { $$ = build_method_invocation ($1, NULL_TREE); }
2164 | name OP_TK argument_list CP_TK
2165 { $$ = build_method_invocation ($1, $3); }
2166 | primary DOT_TK identifier OP_TK CP_TK
2168 if (TREE_CODE ($1) == THIS_EXPR)
2169 $$ = build_this_super_qualified_invocation
2170 (1, $3, NULL_TREE, 0, $2.location);
2173 tree invok = build_method_invocation ($3, NULL_TREE);
2174 $$ = make_qualified_primary ($1, invok, $2.location);
2177 | primary DOT_TK identifier OP_TK argument_list CP_TK
2179 if (TREE_CODE ($1) == THIS_EXPR)
2180 $$ = build_this_super_qualified_invocation
2181 (1, $3, $5, 0, $2.location);
2184 tree invok = build_method_invocation ($3, $5);
2185 $$ = make_qualified_primary ($1, invok, $2.location);
2188 | SUPER_TK DOT_TK identifier OP_TK CP_TK
2190 $$ = build_this_super_qualified_invocation
2191 (0, $3, NULL_TREE, $1.location, $2.location);
2193 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
2195 $$ = build_this_super_qualified_invocation
2196 (0, $3, $5, $1.location, $2.location);
2198 /* Screws up thing. I let it here until I'm convinced it can
2200 | primary DOT_TK error
2201 {yyerror ("'(' expected"); DRECOVER(bad);} */
2202 | SUPER_TK DOT_TK error CP_TK
2203 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2204 | SUPER_TK DOT_TK error DOT_TK
2205 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
2209 name OSB_TK expression CSB_TK
2210 { $$ = build_array_ref ($2.location, $1, $3); }
2211 | primary_no_new_array OSB_TK expression CSB_TK
2212 { $$ = build_array_ref ($2.location, $1, $3); }
2215 yyerror ("Missing term and ']' expected");
2216 DRECOVER(array_access);
2218 | name OSB_TK expression error
2220 yyerror ("']' expected");
2221 DRECOVER(array_access);
2223 | primary_no_new_array OSB_TK error
2225 yyerror ("Missing term and ']' expected");
2226 DRECOVER(array_access);
2228 | primary_no_new_array OSB_TK expression error
2230 yyerror ("']' expected");
2231 DRECOVER(array_access);
2238 | post_increment_expression
2239 | post_decrement_expression
2242 post_increment_expression:
2243 postfix_expression INCR_TK
2244 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2247 post_decrement_expression:
2248 postfix_expression DECR_TK
2249 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
2253 pre_increment_expression
2254 | pre_decrement_expression
2255 | PLUS_TK unary_expression
2256 {$$ = build_unaryop ($1.token, $1.location, $2); }
2257 | MINUS_TK unary_expression
2258 {$$ = build_unaryop ($1.token, $1.location, $2); }
2259 | unary_expression_not_plus_minus
2261 {yyerror ("Missing term"); RECOVER}
2263 {yyerror ("Missing term"); RECOVER}
2266 pre_increment_expression:
2267 INCR_TK unary_expression
2268 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2270 {yyerror ("Missing term"); RECOVER}
2273 pre_decrement_expression:
2274 DECR_TK unary_expression
2275 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
2277 {yyerror ("Missing term"); RECOVER}
2280 unary_expression_not_plus_minus:
2282 | NOT_TK unary_expression
2283 {$$ = build_unaryop ($1.token, $1.location, $2); }
2284 | NEG_TK unary_expression
2285 {$$ = build_unaryop ($1.token, $1.location, $2); }
2288 {yyerror ("Missing term"); RECOVER}
2290 {yyerror ("Missing term"); RECOVER}
2293 cast_expression: /* Error handling here is potentially weak */
2294 OP_TK primitive_type dims CP_TK unary_expression
2297 while (CURRENT_OSB (ctxp)--)
2298 type = build_java_array_type (type, -1);
2300 $$ = build_cast ($1.location, type, $5);
2302 | OP_TK primitive_type CP_TK unary_expression
2303 { $$ = build_cast ($1.location, $2, $4); }
2304 | OP_TK expression CP_TK unary_expression_not_plus_minus
2305 { $$ = build_cast ($1.location, $2, $4); }
2306 | OP_TK name dims CP_TK unary_expression_not_plus_minus
2309 while (CURRENT_OSB (ctxp)--)
2310 obstack_1grow (&temporary_obstack, '[');
2312 obstack_grow0 (&temporary_obstack,
2313 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
2314 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
2315 ptr = obstack_finish (&temporary_obstack);
2316 EXPR_WFL_NODE ($2) = get_identifier (ptr);
2317 $$ = build_cast ($1.location, $2, $5);
2319 | OP_TK primitive_type OSB_TK error
2320 {yyerror ("']' expected, invalid type expression");}
2323 if (ctxp->prevent_ese != lineno)
2324 yyerror ("Invalid type expression"); RECOVER;
2327 | OP_TK primitive_type dims CP_TK error
2328 {yyerror ("Missing term"); RECOVER;}
2329 | OP_TK primitive_type CP_TK error
2330 {yyerror ("Missing term"); RECOVER;}
2331 | OP_TK name dims CP_TK error
2332 {yyerror ("Missing term"); RECOVER;}
2335 multiplicative_expression:
2337 | multiplicative_expression MULT_TK unary_expression
2339 $$ = build_binop (BINOP_LOOKUP ($2.token),
2340 $2.location, $1, $3);
2342 | multiplicative_expression DIV_TK unary_expression
2344 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2347 | multiplicative_expression REM_TK unary_expression
2349 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2352 | multiplicative_expression MULT_TK error
2353 {yyerror ("Missing term"); RECOVER;}
2354 | multiplicative_expression DIV_TK error
2355 {yyerror ("Missing term"); RECOVER;}
2356 | multiplicative_expression REM_TK error
2357 {yyerror ("Missing term"); RECOVER;}
2360 additive_expression:
2361 multiplicative_expression
2362 | additive_expression PLUS_TK multiplicative_expression
2364 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2367 | additive_expression MINUS_TK multiplicative_expression
2369 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2372 | additive_expression PLUS_TK error
2373 {yyerror ("Missing term"); RECOVER;}
2374 | additive_expression MINUS_TK error
2375 {yyerror ("Missing term"); RECOVER;}
2380 | shift_expression LS_TK additive_expression
2382 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2385 | shift_expression SRS_TK additive_expression
2387 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2390 | shift_expression ZRS_TK additive_expression
2392 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2395 | shift_expression LS_TK error
2396 {yyerror ("Missing term"); RECOVER;}
2397 | shift_expression SRS_TK error
2398 {yyerror ("Missing term"); RECOVER;}
2399 | shift_expression ZRS_TK error
2400 {yyerror ("Missing term"); RECOVER;}
2403 relational_expression:
2405 | relational_expression LT_TK shift_expression
2407 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2410 | relational_expression GT_TK shift_expression
2412 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2415 | relational_expression LTE_TK shift_expression
2417 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2420 | relational_expression GTE_TK shift_expression
2422 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2425 | relational_expression INSTANCEOF_TK reference_type
2426 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2427 | relational_expression LT_TK error
2428 {yyerror ("Missing term"); RECOVER;}
2429 | relational_expression GT_TK error
2430 {yyerror ("Missing term"); RECOVER;}
2431 | relational_expression LTE_TK error
2432 {yyerror ("Missing term"); RECOVER;}
2433 | relational_expression GTE_TK error
2434 {yyerror ("Missing term"); RECOVER;}
2435 | relational_expression INSTANCEOF_TK error
2436 {yyerror ("Invalid reference type"); RECOVER;}
2439 equality_expression:
2440 relational_expression
2441 | equality_expression EQ_TK relational_expression
2443 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2446 | equality_expression NEQ_TK relational_expression
2448 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2451 | equality_expression EQ_TK error
2452 {yyerror ("Missing term"); RECOVER;}
2453 | equality_expression NEQ_TK error
2454 {yyerror ("Missing term"); RECOVER;}
2459 | and_expression AND_TK equality_expression
2461 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2464 | and_expression AND_TK error
2465 {yyerror ("Missing term"); RECOVER;}
2468 exclusive_or_expression:
2470 | exclusive_or_expression XOR_TK and_expression
2472 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2475 | exclusive_or_expression XOR_TK error
2476 {yyerror ("Missing term"); RECOVER;}
2479 inclusive_or_expression:
2480 exclusive_or_expression
2481 | inclusive_or_expression OR_TK exclusive_or_expression
2483 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2486 | inclusive_or_expression OR_TK error
2487 {yyerror ("Missing term"); RECOVER;}
2490 conditional_and_expression:
2491 inclusive_or_expression
2492 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2494 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2497 | conditional_and_expression BOOL_AND_TK error
2498 {yyerror ("Missing term"); RECOVER;}
2501 conditional_or_expression:
2502 conditional_and_expression
2503 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2505 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2508 | conditional_or_expression BOOL_OR_TK error
2509 {yyerror ("Missing term"); RECOVER;}
2512 conditional_expression: /* Error handling here is weak */
2513 conditional_or_expression
2514 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2516 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2517 EXPR_WFL_LINECOL ($$) = $2.location;
2519 | conditional_or_expression REL_QM_TK REL_CL_TK error
2522 yyerror ("Missing term");
2525 | conditional_or_expression REL_QM_TK error
2526 {yyerror ("Missing term"); DRECOVER (2);}
2527 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2528 {yyerror ("Missing term"); DRECOVER (3);}
2531 assignment_expression:
2532 conditional_expression
2537 left_hand_side assignment_operator assignment_expression
2538 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2539 | left_hand_side assignment_operator error
2541 if (ctxp->prevent_ese != lineno)
2542 yyerror ("Missing term");
2553 assignment_operator:
2559 assignment_expression
2562 constant_expression:
2569 /* This section of the code deal with save/restoring parser contexts.
2570 Add mode documentation here. FIXME */
2572 /* Helper function. Create a new parser context. With
2573 COPY_FROM_PREVIOUS set to a non zero value, content of the previous
2574 context is copied, otherwise, the new context is zeroed. The newly
2575 created context becomes the current one. */
2578 create_new_parser_context (copy_from_previous)
2579 int copy_from_previous;
2581 struct parser_ctxt *new;
2583 new = (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2584 if (copy_from_previous)
2586 memcpy ((PTR)new, (PTR)ctxp, sizeof (struct parser_ctxt));
2587 new->saved_data_ctx = 1;
2590 bzero ((PTR) new, sizeof (struct parser_ctxt));
2596 /* Create a new parser context and make it the current one. */
2599 java_push_parser_context ()
2601 create_new_parser_context (0);
2604 ctxp->incomplete_class = ctxp->next->incomplete_class;
2605 ctxp->gclass_list = ctxp->next->gclass_list;
2610 java_pop_parser_context (generate)
2614 struct parser_ctxt *toFree, *next;
2623 next->incomplete_class = ctxp->incomplete_class;
2624 next->gclass_list = ctxp->gclass_list;
2625 lineno = ctxp->lineno;
2626 finput = ctxp->finput;
2627 current_class = ctxp->current_class;
2630 /* Set the single import class file flag to 0 for the current list
2631 of imported things */
2632 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2633 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2635 /* And restore those of the previous context */
2636 if ((ctxp = next)) /* Assignment is really meant here */
2637 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2638 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2640 /* If we pushed a context to parse a class intended to be generated,
2641 we keep it so we can remember the class. What we could actually
2642 do is to just update a list of class names. */
2645 toFree->next = ctxp_for_generation;
2646 ctxp_for_generation = toFree;
2652 /* Create a parser context for the use of saving some global
2656 java_parser_context_save_global ()
2660 java_push_parser_context ();
2661 ctxp->saved_data_ctx = 1;
2664 /* If this context already stores data, create a new one suitable
2665 for data storage. */
2666 else if (ctxp->saved_data)
2667 create_new_parser_context (1);
2669 ctxp->finput = finput;
2670 ctxp->lineno = lineno;
2671 ctxp->current_class = current_class;
2672 ctxp->filename = input_filename;
2673 ctxp->current_function_decl = current_function_decl;
2674 ctxp->saved_data = 1;
2677 /* Restore some global variables from the previous context. Make the
2678 previous context the current one. */
2681 java_parser_context_restore_global ()
2683 finput = ctxp->finput;
2684 lineno = ctxp->lineno;
2685 current_class = ctxp->current_class;
2686 input_filename = ctxp->filename;
2687 current_function_decl = ctxp->current_function_decl;
2688 ctxp->saved_data = 0;
2689 if (ctxp->saved_data_ctx)
2690 java_pop_parser_context (0);
2693 /* Suspend vital data for the current class/function being parsed so
2694 that an other class can be parsed. Used to let local/anonymous
2695 classes be parsed. */
2698 java_parser_context_suspend ()
2700 /* This makes debugging through java_debug_context easier */
2701 static const char *name = "<inner buffer context>";
2703 /* Duplicate the previous context, use it to save the globals we're
2705 create_new_parser_context (1);
2706 ctxp->current_function_decl = current_function_decl;
2707 ctxp->current_class = current_class;
2709 /* Then create a new context which inherits all data from the
2710 previous one. This will be the new current context */
2711 create_new_parser_context (1);
2713 /* Help debugging */
2714 ctxp->next->filename = name;
2717 /* Resume vital data for the current class/function being parsed so
2718 that an other class can be parsed. Used to let local/anonymous
2719 classes be parsed. The trick is the data storing file position
2720 informations must be restored to their current value, so parsing
2721 can resume as if no context was ever saved. */
2724 java_parser_context_resume ()
2726 struct parser_ctxt *old = ctxp; /* This one is to be discarded */
2727 struct parser_ctxt *saver = old->next; /* This one contain saved info */
2728 struct parser_ctxt *restored = saver->next; /* This one is the old current */
2730 /* We need to inherit the list of classes to complete/generate */
2731 restored->incomplete_class = old->incomplete_class;
2732 restored->gclass_list = old->gclass_list;
2733 restored->classd_list = old->classd_list;
2734 restored->class_list = old->class_list;
2736 /* Restore the current class and function from the saver */
2737 current_class = saver->current_class;
2738 current_function_decl = saver->current_function_decl;
2740 /* Retrive the restored context */
2743 /* Re-installed the data for the parsing to carry on */
2744 bcopy (&old->marker_begining, &ctxp->marker_begining,
2745 (size_t)(&ctxp->marker_end - &ctxp->marker_begining));
2747 /* Buffer context can now be discarded */
2752 /* Add a new anchor node to which all statement(s) initializing static
2753 and non static initialized upon declaration field(s) will be
2757 java_parser_context_push_initialized_field ()
2761 node = build_tree_list (NULL_TREE, NULL_TREE);
2762 TREE_CHAIN (node) = CPC_STATIC_INITIALIZER_LIST (ctxp);
2763 CPC_STATIC_INITIALIZER_LIST (ctxp) = node;
2765 node = build_tree_list (NULL_TREE, NULL_TREE);
2766 TREE_CHAIN (node) = CPC_INITIALIZER_LIST (ctxp);
2767 CPC_INITIALIZER_LIST (ctxp) = node;
2769 node = build_tree_list (NULL_TREE, NULL_TREE);
2770 TREE_CHAIN (node) = CPC_INSTANCE_INITIALIZER_LIST (ctxp);
2771 CPC_INSTANCE_INITIALIZER_LIST (ctxp) = node;
2774 /* Pop the lists of initialized field. If this lists aren't empty,
2775 remember them so we can use it to create and populate the $finit$
2776 or <clinit> functions. */
2779 java_parser_context_pop_initialized_field ()
2782 tree class_type = TREE_TYPE (GET_CPC ());
2784 if (CPC_INITIALIZER_LIST (ctxp))
2786 stmts = CPC_INITIALIZER_STMT (ctxp);
2787 CPC_INITIALIZER_LIST (ctxp) = TREE_CHAIN (CPC_INITIALIZER_LIST (ctxp));
2788 if (stmts && !java_error_count)
2789 TYPE_FINIT_STMT_LIST (class_type) = reorder_static_initialized (stmts);
2792 if (CPC_STATIC_INITIALIZER_LIST (ctxp))
2794 stmts = CPC_STATIC_INITIALIZER_STMT (ctxp);
2795 CPC_STATIC_INITIALIZER_LIST (ctxp) =
2796 TREE_CHAIN (CPC_STATIC_INITIALIZER_LIST (ctxp));
2797 /* Keep initialization in order to enforce 8.5 */
2798 if (stmts && !java_error_count)
2799 TYPE_CLINIT_STMT_LIST (class_type) = nreverse (stmts);
2802 /* JDK 1.1 instance initializers */
2803 if (CPC_INSTANCE_INITIALIZER_LIST (ctxp))
2805 stmts = CPC_INSTANCE_INITIALIZER_STMT (ctxp);
2806 CPC_INSTANCE_INITIALIZER_LIST (ctxp) =
2807 TREE_CHAIN (CPC_INSTANCE_INITIALIZER_LIST (ctxp));
2808 if (stmts && !java_error_count)
2809 TYPE_II_STMT_LIST (class_type) = nreverse (stmts);
2814 reorder_static_initialized (list)
2817 /* We have to keep things in order. The alias initializer have to
2818 come first, then the initialized regular field, in reverse to
2819 keep them in lexical order. */
2820 tree marker, previous = NULL_TREE;
2821 for (marker = list; marker; previous = marker, marker = TREE_CHAIN (marker))
2822 if (TREE_CODE (marker) == TREE_LIST
2823 && !TREE_VALUE (marker) && !TREE_PURPOSE (marker))
2826 /* No static initialized, the list is fine as is */
2828 list = TREE_CHAIN (marker);
2830 /* No marker? reverse the whole list */
2832 list = nreverse (list);
2834 /* Otherwise, reverse what's after the marker and the new reordered
2835 sublist will replace the marker. */
2838 TREE_CHAIN (previous) = NULL_TREE;
2839 list = nreverse (list);
2840 list = chainon (TREE_CHAIN (marker), list);
2845 /* Helper functions to dump the parser context stack. */
2847 #define TAB_CONTEXT(C) \
2848 {int i; for (i = 0; i < (C); i++) fputc (' ', stderr);}
2851 java_debug_context_do (tab)
2854 struct parser_ctxt *copy = ctxp;
2858 fprintf (stderr, "ctxt: 0x%0lX\n", (unsigned long)copy);
2860 fprintf (stderr, "filename: %s\n", copy->filename);
2862 fprintf (stderr, "lineno: %d\n", copy->lineno);
2864 fprintf (stderr, "package: %s\n",
2866 IDENTIFIER_POINTER (copy->package) : "<none>"));
2868 fprintf (stderr, "context for saving: %d\n", copy->saved_data_ctx);
2870 fprintf (stderr, "saved data: %d\n", copy->saved_data);
2876 /* Dump the stacked up parser contexts. Intended to be called from a
2880 java_debug_context ()
2882 java_debug_context_do (0);
2887 /* Flag for the error report routine to issue the error the first time
2888 it's called (overriding the default behavior which is to drop the
2889 first invocation and honor the second one, taking advantage of a
2891 static int force_error = 0;
2893 /* Reporting an constructor invocation error. */
2895 parse_ctor_invocation_error ()
2897 if (DECL_CONSTRUCTOR_P (current_function_decl))
2898 yyerror ("Constructor invocation must be first thing in a constructor");
2900 yyerror ("Only constructors can invoke constructors");
2903 /* Reporting JDK1.1 features not implemented. */
2906 parse_jdk1_1_error (msg)
2909 sorry (": `%s' JDK1.1(TM) feature", msg);
2911 return empty_stmt_node;
2914 static int do_warning = 0;
2921 static int prev_lineno;
2922 static const char *prev_msg;
2925 char *remainder, *code_from_source;
2926 extern struct obstack temporary_obstack;
2928 if (!force_error && prev_lineno == lineno)
2931 /* Save current error location but report latter, when the context is
2933 if (ctxp->java_error_flag == 0)
2935 ctxp->java_error_flag = 1;
2937 /* Do something to use the previous line if we're reaching the
2938 end of the file... */
2939 #ifdef VERBOSE_SKELETON
2940 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2945 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2946 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2949 ctxp->java_error_flag = 0;
2951 java_warning_count++;
2955 if (elc.col == 0 && msg && msg[1] == ';')
2957 elc.col = ctxp->p_line->char_col-1;
2958 elc.line = ctxp->p_line->lineno;
2961 save_lineno = lineno;
2962 prev_lineno = lineno = elc.line;
2965 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
2966 obstack_grow0 (&temporary_obstack,
2967 code_from_source, strlen (code_from_source));
2968 remainder = obstack_finish (&temporary_obstack);
2970 warning ("%s.\n%s", msg, remainder);
2972 error ("%s.\n%s", msg, remainder);
2974 /* This allow us to cheaply avoid an extra 'Invalid expression
2975 statement' error report when errors have been already reported on
2976 the same line. This occurs when we report an error but don't have
2977 a synchronization point other than ';', which
2978 expression_statement is the only one to take care of. */
2979 ctxp->prevent_ese = lineno = save_lineno;
2983 issue_warning_error_from_context (cl, msg, ap)
2988 const char *saved, *saved_input_filename;
2990 vsprintf (buffer, msg, ap);
2993 ctxp->elc.line = EXPR_WFL_LINENO (cl);
2994 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
2995 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
2997 /* We have a CL, that's a good reason for using it if it contains data */
2998 saved = ctxp->filename;
2999 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
3000 ctxp->filename = EXPR_WFL_FILENAME (cl);
3001 saved_input_filename = input_filename;
3002 input_filename = ctxp->filename;
3004 java_error (buffer);
3005 ctxp->filename = saved;
3006 input_filename = saved_input_filename;
3010 /* Issue an error message at a current source line CL */
3013 parse_error_context VPARAMS ((tree cl, const char *msg, ...))
3015 #ifndef ANSI_PROTOTYPES
3022 #ifndef ANSI_PROTOTYPES
3023 cl = va_arg (ap, tree);
3024 msg = va_arg (ap, const char *);
3026 issue_warning_error_from_context (cl, msg, ap);
3030 /* Issue a warning at a current source line CL */
3033 parse_warning_context VPARAMS ((tree cl, const char *msg, ...))
3035 #ifndef ANSI_PROTOTYPES
3042 #ifndef ANSI_PROTOTYPES
3043 cl = va_arg (ap, tree);
3044 msg = va_arg (ap, const char *);
3047 force_error = do_warning = 1;
3048 issue_warning_error_from_context (cl, msg, ap);
3049 do_warning = force_error = 0;
3054 find_expr_with_wfl (node)
3062 switch (TREE_CODE (node))
3065 node = BLOCK_EXPR_BODY (node);
3069 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
3072 node = TREE_OPERAND (node, 1);
3076 node = TREE_OPERAND (node, 0);
3079 case LABELED_BLOCK_EXPR:
3080 node = TREE_OPERAND (node, 1);
3084 code = TREE_CODE_CLASS (TREE_CODE (node));
3085 if (((code == '1') || (code == '2') || (code == 'e'))
3086 && EXPR_WFL_LINECOL (node))
3094 /* Issue a missing return statement error. Uses METHOD to figure the
3095 last line of the method the error occurs in. */
3098 missing_return_error (method)
3101 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
3102 parse_error_context (wfl_operator, "Missing return statement");
3105 /* Issue an unreachable statement error. From NODE, find the next
3106 statement to report appropriately. */
3108 unreachable_stmt_error (node)
3111 /* Browse node to find the next expression node that has a WFL. Use
3112 the location to report the error */
3113 if (TREE_CODE (node) == COMPOUND_EXPR)
3114 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
3116 node = find_expr_with_wfl (node);
3120 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
3121 parse_error_context (wfl_operator, "Unreachable statement");
3124 fatal ("Can't get valid statement - unreachable_stmt_error");
3128 java_report_errors ()
3130 if (java_error_count)
3131 fprintf (stderr, "%d error%s",
3132 java_error_count, (java_error_count == 1 ? "" : "s"));
3133 if (java_warning_count)
3134 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
3135 java_warning_count, (java_warning_count == 1 ? "" : "s"));
3136 if (java_error_count || java_warning_count)
3137 putc ('\n', stderr);
3138 return java_error_count;
3142 java_accstring_lookup (flags)
3145 static char buffer [80];
3146 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
3148 /* Access modifier looked-up first for easier report on forbidden
3150 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
3151 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
3152 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
3153 if (flags & ACC_STATIC) COPY_RETURN ("static");
3154 if (flags & ACC_FINAL) COPY_RETURN ("final");
3155 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
3156 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
3157 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
3158 if (flags & ACC_NATIVE) COPY_RETURN ("native");
3159 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
3160 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
3167 /* Issuing error messages upon redefinition of classes, interfaces or
3171 classitf_redefinition_error (context, id, decl, cl)
3172 const char *context;
3175 parse_error_context (cl, "%s `%s' already defined in %s:%d",
3176 context, IDENTIFIER_POINTER (id),
3177 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3178 /* Here we should point out where its redefined. It's a unicode. FIXME */
3182 variable_redefinition_error (context, name, type, line)
3183 tree context, name, type;
3186 const char *type_name;
3188 /* Figure a proper name for type. We might haven't resolved it */
3189 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
3190 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
3192 type_name = lang_printable_name (type, 0);
3194 parse_error_context (context,
3195 "Variable `%s' is already defined in this method and was declared `%s %s' at line %d",
3196 IDENTIFIER_POINTER (name),
3197 type_name, IDENTIFIER_POINTER (name), line);
3201 build_array_from_name (type, type_wfl, name, ret_name)
3202 tree type, type_wfl, name, *ret_name;
3207 /* Eventually get more dims */
3208 string = IDENTIFIER_POINTER (name);
3209 while (string [more_dims] == '[')
3212 /* If we have, then craft a new type for this variable */
3215 name = get_identifier (&string [more_dims]);
3217 /* If we have a pointer, use its type */
3218 if (TREE_CODE (type) == POINTER_TYPE)
3219 type = TREE_TYPE (type);
3221 /* Building the first dimension of a primitive type uses this
3223 if (JPRIMITIVE_TYPE_P (type))
3225 type = build_java_array_type (type, -1);
3226 CLASS_LOADED_P (type) = 1;
3229 /* Otherwise, if we have a WFL for this type, use it (the type
3230 is already an array on an unresolved type, and we just keep
3231 on adding dimensions) */
3235 /* Add all the dimensions */
3237 type = build_unresolved_array_type (type);
3239 /* The type may have been incomplete in the first place */
3241 type = obtain_incomplete_type (type);
3249 /* Build something that the type identifier resolver will identify as
3250 being an array to an unresolved type. TYPE_WFL is a WFL on a
3254 build_unresolved_array_type (type_or_wfl)
3259 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
3260 just create a array type */
3261 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
3263 tree type = build_java_array_type (type_or_wfl, -1);
3264 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
3268 obstack_1grow (&temporary_obstack, '[');
3269 obstack_grow0 (&temporary_obstack,
3270 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
3271 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
3272 ptr = obstack_finish (&temporary_obstack);
3273 return build_expr_wfl (get_identifier (ptr),
3274 EXPR_WFL_FILENAME (type_or_wfl),
3275 EXPR_WFL_LINENO (type_or_wfl),
3276 EXPR_WFL_COLNO (type_or_wfl));
3280 parser_add_interface (class_decl, interface_decl, wfl)
3281 tree class_decl, interface_decl, wfl;
3283 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
3284 parse_error_context (wfl, "Interface `%s' repeated",
3285 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
3288 /* Bulk of common class/interface checks. Return 1 if an error was
3289 encountered. TAG is 0 for a class, 1 for an interface. */
3292 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
3293 int is_interface, flags;
3294 tree raw_name, qualified_name, decl, cl;
3297 int sca = 0; /* Static class allowed */
3298 int icaf = 0; /* Inner class allowed flags */
3299 int uaaf = CLASS_MODIFIERS; /* Usually allowed access flags */
3302 fprintf (stderr, " %s%s %s",
3303 (CPC_INNER_P () ? "inner" : ""),
3304 (is_interface ? "interface" : "class"),
3305 IDENTIFIER_POINTER (qualified_name));
3307 /* Scope of an interface/class type name:
3308 - Can't be imported by a single type import
3309 - Can't already exists in the package */
3310 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
3311 && (node = find_name_in_single_imports (raw_name)))
3314 (cl, "%s name `%s' clashes with imported type `%s'",
3315 (is_interface ? "Interface" : "Class"),
3316 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
3319 if (decl && CLASS_COMPLETE_P (decl))
3321 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
3322 qualified_name, decl, cl);
3326 if (check_inner_class_redefinition (raw_name, cl))
3329 /* If public, file name should match class/interface name, except
3330 when dealing with an inner class */
3331 if (!CPC_INNER_P () && (flags & ACC_PUBLIC ))
3335 /* Contains OS dependent assumption on path separator. FIXME */
3336 for (f = &input_filename [strlen (input_filename)];
3337 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
3340 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
3342 if (strncmp (IDENTIFIER_POINTER (raw_name),
3343 f , IDENTIFIER_LENGTH (raw_name)) ||
3344 f [IDENTIFIER_LENGTH (raw_name)] != '.')
3346 (cl, "Public %s `%s' must be defined in a file called `%s.java'",
3347 (is_interface ? "interface" : "class"),
3348 IDENTIFIER_POINTER (qualified_name),
3349 IDENTIFIER_POINTER (raw_name));
3352 /* Static classes can be declared only in top level classes. Note:
3353 once static, a inner class is a top level class. */
3354 if (flags & ACC_STATIC)
3356 /* Catch the specific error of declaring an class inner class
3357 with no toplevel enclosing class. Prevent check_modifiers from
3358 complaining a second time */
3359 if (CPC_INNER_P () && !TOPLEVEL_CLASS_DECL_P (GET_CPC()))
3361 parse_error_context (cl, "Inner class `%s' can't be static. Static classes can only occur in interfaces and top-level classes",
3362 IDENTIFIER_POINTER (qualified_name));
3365 /* Else, in the context of a top-level class declaration, let
3366 `check_modifiers' do its job, otherwise, give it a go */
3368 sca = (GET_CPC_LIST () ? ACC_STATIC : 0);
3371 /* Inner classes can be declared private or protected
3372 within their enclosing classes. */
3375 /* A class which is local to a block can't be public, private,
3376 protected or static. But it is created final, so allow this
3378 if (current_function_decl)
3379 icaf = sca = uaaf = ACC_FINAL;
3382 check_modifiers_consistency (flags);
3383 icaf = ACC_PRIVATE|ACC_PROTECTED;
3390 uaaf = INTERFACE_INNER_MODIFIERS;
3392 uaaf = INTERFACE_MODIFIERS;
3394 check_modifiers ("Illegal modifier `%s' for interface declaration",
3398 check_modifiers ((current_function_decl ?
3399 "Illegal modifier `%s' for local class declaration" :
3400 "Illegal modifier `%s' for class declaration"),
3401 flags, uaaf|sca|icaf);
3406 make_nested_class_name (cpc_list)
3414 make_nested_class_name (TREE_CHAIN (cpc_list));
3416 /* Pick the qualified name when dealing with the first upmost
3418 name = (TREE_CHAIN (cpc_list) ?
3419 TREE_PURPOSE (cpc_list) : DECL_NAME (TREE_VALUE (cpc_list)));
3420 obstack_grow (&temporary_obstack,
3421 IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
3422 /* Why is NO_DOLLAR_IN_LABEL defined? */
3424 #ifdef NO_DOLLAR_IN_LABEL
3425 fatal ("make_nested_class_name: Can't use '$' as a separator "
3426 "for inner classes");
3429 obstack_1grow (&temporary_obstack, '$');
3432 /* Can't redefine a class already defined in an earlier scope. */
3435 check_inner_class_redefinition (raw_name, cl)
3440 for (scope_list = GET_CPC_LIST (); scope_list;
3441 scope_list = GET_NEXT_ENCLOSING_CPC (scope_list))
3442 if (raw_name == GET_CPC_UN_NODE (scope_list))
3445 (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",
3446 IDENTIFIER_POINTER (raw_name));
3453 find_as_inner_class (enclosing, name, cl)
3454 tree enclosing, name, cl;
3456 tree qual, to_return;
3460 name = TYPE_NAME (name);
3462 /* First search: within the scope of `enclosing', search for name */
3463 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3464 qual = EXPR_WFL_QUALIFICATION (cl);
3466 qual = build_tree_list (cl, NULL_TREE);
3468 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3470 if ((to_return = find_as_inner_class_do (qual, enclosing)))
3473 /* We're dealing with a qualified name. Try to resolve thing until
3474 we get something that is an enclosing class. */
3475 if (QUALIFIED_P (name) && cl && EXPR_WFL_NODE (cl) == name)
3477 tree acc = NULL_TREE, decl = NULL_TREE, ptr;
3479 for (qual = EXPR_WFL_QUALIFICATION (cl); qual && !decl;
3480 qual = TREE_CHAIN (qual))
3482 acc = merge_qualified_name (acc,
3483 EXPR_WFL_NODE (TREE_PURPOSE (qual)));
3484 BUILD_PTR_FROM_NAME (ptr, acc);
3485 decl = do_resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3488 /* A NULL qual and a decl means that the search ended
3489 successfully?!? We have to do something then. FIXME */
3494 qual = EXPR_WFL_QUALIFICATION (cl);
3496 /* Otherwise, create a qual for the other part of the resolution. */
3498 qual = build_tree_list (build_expr_wfl (name, NULL, 0, 0), NULL_TREE);
3500 if (!(to_return = find_as_inner_class_do (qual, enclosing)))
3502 /* It might be the case that the enclosing class was loaded as
3503 bytecode, in which case it will be missing the
3504 DECL_INNER_CLASS_LIST. We build a fully qualified internal
3505 innerclass name and we try to load it. */
3506 tree fqin = identifier_subst (name, "", '.', '$', "");
3508 BUILD_PTR_FROM_NAME (ptr, fqin);
3509 to_return = resolve_class (NULL_TREE, ptr, NULL_TREE, cl);
3514 /* We go inside the list of sub classes and try to find a way
3518 find_as_inner_class_do (qual, enclosing)
3519 tree qual, enclosing;
3524 for (; qual && enclosing; qual = TREE_CHAIN (qual))
3526 tree name_to_match = EXPR_WFL_NODE (TREE_PURPOSE (qual));
3527 tree next_enclosing = NULL_TREE;
3530 for (inner_list = DECL_INNER_CLASS_LIST (enclosing);
3531 inner_list; inner_list = TREE_CHAIN (inner_list))
3533 if (TREE_VALUE (inner_list) == name_to_match)
3535 next_enclosing = TREE_PURPOSE (inner_list);
3539 enclosing = next_enclosing;
3542 return (!qual && enclosing ? enclosing : NULL_TREE);
3545 /* Reach all inner classes and tie their unqualified name to a
3549 set_nested_class_simple_name_value (outer, set)
3555 for (l = DECL_INNER_CLASS_LIST (outer); l; l = TREE_CHAIN (l))
3556 IDENTIFIER_GLOBAL_VALUE (TREE_VALUE (l)) = (set ?
3557 TREE_PURPOSE (l) : NULL_TREE);
3561 link_nested_class_to_enclosing ()
3563 if (GET_ENCLOSING_CPC ())
3565 tree enclosing = GET_ENCLOSING_CPC_CONTEXT ();
3566 DECL_INNER_CLASS_LIST (enclosing) =
3567 tree_cons (GET_CPC (), GET_CPC_UN (),
3568 DECL_INNER_CLASS_LIST (enclosing));
3569 enclosing = enclosing;
3574 maybe_make_nested_class_name (name)
3577 tree id = NULL_TREE;
3581 make_nested_class_name (GET_CPC_LIST ());
3582 obstack_grow0 (&temporary_obstack,
3583 IDENTIFIER_POINTER (name),
3584 IDENTIFIER_LENGTH (name));
3585 id = get_identifier (obstack_finish (&temporary_obstack));
3587 QUALIFIED_P (id) = 1;
3592 /* If DECL is NULL, create and push a new DECL, record the current
3593 line CL and do other maintenance things. */
3596 maybe_create_class_interface_decl (decl, raw_name, qualified_name, cl)
3597 tree decl, raw_name, qualified_name, cl;
3600 decl = push_class (make_class (), qualified_name);
3602 /* Take care of the file and line business */
3603 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
3604 /* If we're emiting xrefs, store the line/col number information */
3606 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (cl);
3608 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
3609 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
3610 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
3611 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
3613 PUSH_CPC (decl, raw_name);
3614 DECL_CONTEXT (decl) = GET_ENCLOSING_CPC_CONTEXT ();
3616 /* Link the declaration to the already seen ones */
3617 TREE_CHAIN (decl) = ctxp->class_list;
3618 ctxp->class_list = decl;
3620 /* Create a new nodes in the global lists */
3621 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
3622 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
3624 /* Install a new dependency list element */
3625 create_jdep_list (ctxp);
3627 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
3628 IDENTIFIER_POINTER (qualified_name)));
3633 add_superinterfaces (decl, interface_list)
3634 tree decl, interface_list;
3637 /* Superinterface(s): if present and defined, parser_check_super_interface ()
3638 takes care of ensuring that:
3639 - This is an accessible interface type,
3640 - Circularity detection.
3641 parser_add_interface is then called. If present but not defined,
3642 the check operation is delayed until the super interface gets
3644 for (node = interface_list; node; node = TREE_CHAIN (node))
3646 tree current = TREE_PURPOSE (node);
3647 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
3648 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
3650 if (!parser_check_super_interface (idecl, decl, current))
3651 parser_add_interface (decl, idecl, current);
3654 register_incomplete_type (JDEP_INTERFACE,
3655 current, decl, NULL_TREE);
3659 /* Create an interface in pass1 and return its decl. Return the
3660 interface's decl in pass 2. */
3663 create_interface (flags, id, super)
3667 tree raw_name = EXPR_WFL_NODE (id);
3668 tree q_name = parser_qualified_classname (raw_name);
3669 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
3671 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
3673 /* Basic checks: scope, redefinition, modifiers */
3674 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
3680 /* Suspend the current parsing context if we're parsing an inner
3683 java_parser_context_suspend ();
3685 /* Push a new context for (static) initialized upon declaration fields */
3686 java_parser_context_push_initialized_field ();
3688 /* Interface modifiers check
3689 - public/abstract allowed (already done at that point)
3690 - abstract is obsolete (comes first, it's a warning, or should be)
3691 - Can't use twice the same (checked in the modifier rule) */
3692 if ((flags & ACC_ABSTRACT) && flag_redundant)
3693 parse_warning_context
3694 (MODIFIER_WFL (ABSTRACT_TK),
3695 "Redundant use of `abstract' modifier. Interface `%s' is implicitely abstract", IDENTIFIER_POINTER (raw_name));
3697 /* Create a new decl if DECL is NULL, otherwise fix it */
3698 decl = maybe_create_class_interface_decl (decl, raw_name, q_name, id);
3700 /* Set super info and mark the class a complete */
3701 set_super_info (ACC_INTERFACE | flags, TREE_TYPE (decl),
3702 object_type_node, ctxp->interface_number);
3703 ctxp->interface_number = 0;
3704 CLASS_COMPLETE_P (decl) = 1;
3705 add_superinterfaces (decl, super);
3710 /* Anonymous class counter. Will be reset to 1 every time a non
3711 anonymous class gets created. */
3712 static int anonymous_class_counter = 1;
3714 /* Patch anonymous class CLASS, by either extending or implementing
3718 patch_anonymous_class (type_decl, class_decl, wfl)
3719 tree type_decl, class_decl, wfl;
3721 tree class = TREE_TYPE (class_decl);
3722 tree type = TREE_TYPE (type_decl);
3723 tree binfo = TYPE_BINFO (class);
3725 /* If it's an interface, implement it */
3726 if (CLASS_INTERFACE (type_decl))
3731 if (parser_check_super_interface (type_decl, class_decl, wfl))
3734 s_binfo = TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0);
3735 length = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (class))+1;
3736 TYPE_BINFO_BASETYPES (class) = make_tree_vec (length);
3737 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (class)), 0) = s_binfo;
3738 /* And add the interface */
3739 parser_add_interface (class_decl, type_decl, wfl);
3741 /* Otherwise, it's a type we want to extend */
3744 if (parser_check_super (type_decl, class_decl, wfl))
3746 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (binfo), 0)) = type;
3751 create_anonymous_class (location, type_name)
3756 tree super = NULL_TREE, itf = NULL_TREE;
3757 tree id, type_decl, class;
3759 /* The unqualified name of the anonymous class. It's just a number. */
3760 sprintf (buffer, "%d", anonymous_class_counter++);
3761 id = build_wfl_node (get_identifier (buffer));
3762 EXPR_WFL_LINECOL (id) = location;
3764 /* We know about the type to extend/implement. We go ahead */
3765 if ((type_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (type_name))))
3767 /* Create a class which either implements on extends the designated
3768 class. The class bears an innacessible name. */
3769 if (CLASS_INTERFACE (type_decl))
3771 /* It's OK to modify it here. It's been already used and
3772 shouldn't be reused */
3773 ctxp->interface_number = 1;
3774 /* Interfaces should presented as a list of WFLs */
3775 itf = build_tree_list (type_name, NULL_TREE);
3781 class = create_class (ACC_FINAL, id, super, itf);
3783 /* We didn't know anything about the stuff. We register a dependence. */
3785 register_incomplete_type (JDEP_ANONYMOUS, type_name, class, NULL_TREE);
3787 ANONYMOUS_CLASS_P (TREE_TYPE (class)) = 1;
3791 /* Create a class in pass1 and return its decl. Return class
3792 interface's decl in pass 2. */
3795 create_class (flags, id, super, interfaces)
3797 tree id, super, interfaces;
3799 tree raw_name = EXPR_WFL_NODE (id);
3800 tree class_id, decl;
3801 tree super_decl_type;
3803 class_id = parser_qualified_classname (raw_name);
3804 decl = IDENTIFIER_CLASS_VALUE (class_id);
3805 EXPR_WFL_NODE (id) = class_id;
3807 /* Basic check: scope, redefinition, modifiers */
3808 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
3814 /* Suspend the current parsing context if we're parsing an inner
3815 class or an anonymous class. */
3817 java_parser_context_suspend ();
3818 /* Push a new context for (static) initialized upon declaration fields */
3819 java_parser_context_push_initialized_field ();
3821 /* Class modifier check:
3822 - Allowed modifier (already done at that point)
3823 - abstract AND final forbidden
3824 - Public classes defined in the correct file */
3825 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
3827 (id, "Class `%s' can't be declared both abstract and final",
3828 IDENTIFIER_POINTER (raw_name));
3830 /* Create a new decl if DECL is NULL, otherwise fix it */
3831 decl = maybe_create_class_interface_decl (decl, raw_name, class_id, id);
3833 /* If SUPER exists, use it, otherwise use Object */
3836 /* Can't extend java.lang.Object */
3837 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
3839 parse_error_context (id, "Can't extend `java.lang.Object'");
3844 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
3846 else if (TREE_TYPE (decl) != object_type_node)
3847 super_decl_type = object_type_node;
3848 /* We're defining java.lang.Object */
3850 super_decl_type = NULL_TREE;
3852 /* Set super info and mark the class a complete */
3853 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
3854 ctxp->interface_number);
3855 ctxp->interface_number = 0;
3856 CLASS_COMPLETE_P (decl) = 1;
3857 add_superinterfaces (decl, interfaces);
3859 /* Add the private this$<n> field, Replicate final locals still in
3860 scope as private final fields mangled like val$<local_name>.
3861 This doesn't not occur for top level (static) inner classes. */
3862 if (PURE_INNER_CLASS_DECL_P (decl))
3863 add_inner_class_fields (decl, current_function_decl);
3865 /* If doing xref, store the location at which the inherited class
3866 (if any) was seen. */
3867 if (flag_emit_xref && super)
3868 DECL_INHERITED_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (super);
3870 /* Eventually sets the @deprecated tag flag */
3871 CHECK_DEPRECATED (decl);
3873 /* Reset the anonymous class counter when declaring non inner classes */
3874 if (!INNER_CLASS_DECL_P (decl))
3875 anonymous_class_counter = 1;
3880 /* End a class declaration: register the statements used to create
3881 $finit$ and <clinit>, pop the current class and resume the prior
3882 parser context if necessary. */
3885 end_class_declaration (resume)
3888 /* If an error occured, context weren't pushed and won't need to be
3889 popped by a resume. */
3890 int no_error_occured = ctxp->next && GET_CPC () != error_mark_node;
3892 java_parser_context_pop_initialized_field ();
3894 if (resume && no_error_occured)
3895 java_parser_context_resume ();
3897 /* We're ending a class declaration, this is a good time to reset
3898 the interface cout. Note that might have been already done in
3899 create_interface, but if at that time an inner class was being
3900 dealt with, the interface count was reset in a context created
3901 for the sake of handling inner classes declaration. */
3902 ctxp->interface_number = 0;
3906 add_inner_class_fields (class_decl, fct_decl)
3910 tree block, marker, f;
3912 f = add_field (TREE_TYPE (class_decl),
3913 build_current_thisn (TREE_TYPE (class_decl)),
3914 build_pointer_type (TREE_TYPE (DECL_CONTEXT (class_decl))),
3916 FIELD_THISN (f) = 1;
3921 for (block = GET_CURRENT_BLOCK (fct_decl);
3922 block && TREE_CODE (block) == BLOCK; block = BLOCK_SUPERCONTEXT (block))
3925 for (decl = BLOCK_EXPR_DECLS (block); decl; decl = TREE_CHAIN (decl))
3928 tree wfl, init, list;
3930 /* Avoid non final arguments. */
3931 if (!LOCAL_FINAL (decl))
3934 MANGLE_OUTER_LOCAL_VARIABLE_NAME (name, DECL_NAME (decl));
3935 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_ID (pname, DECL_NAME (decl));
3936 wfl = build_wfl_node (get_identifier (name));
3937 init = build_wfl_node (get_identifier (pname));
3938 /* Build an initialization for the field: it will be
3939 initialized by a parameter added to $finit$, bearing a
3940 mangled name of the field itself (param$<n>.) The
3941 parameter is provided to $finit$ by the constructor
3942 invoking it (hence the constructor will also feature a
3943 hidden parameter, set to the value of the outer context
3944 local at the time the inner class is created.)
3946 Note: we take into account all possible locals that can
3947 be accessed by the inner class. It's actually not trivial
3948 to minimize these aliases down to the ones really
3949 used. One way to do that would be to expand all regular
3950 methods first, then $finit$ to get a picture of what's
3951 used. It works with the exception that we would have to
3952 go back on all constructor invoked in regular methods to
3953 have their invokation reworked (to include the right amount
3954 of alias initializer parameters.)
3956 The only real way around, I think, is a first pass to
3957 identify locals really used in the inner class. We leave
3958 the flag FIELD_LOCAL_ALIAS_USED around for that future
3961 On the other hand, it only affect local inner classes,
3962 whose constructors (and $finit$ call) will be featuring
3963 unecessary arguments. It's easy for a developper to keep
3964 this number of parameter down by using the `final'
3965 keyword only when necessary. For the time being, we can
3966 issue a warning on unecessary finals. FIXME */
3967 init = build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (wfl),
3970 /* Register the field. The TREE_LIST holding the part
3971 initialized/initializer will be marked ARG_FINAL_P so
3972 that the created field can be marked
3973 FIELD_LOCAL_ALIAS. */
3974 list = build_tree_list (wfl, init);
3975 ARG_FINAL_P (list) = 1;
3976 register_fields (ACC_PRIVATE | ACC_FINAL, TREE_TYPE (decl), list);
3980 if (!CPC_INITIALIZER_STMT (ctxp))
3983 /* If we ever registered an alias field, insert and marker to
3984 remeber where the list ends. The second part of the list (the one
3985 featuring initialized fields) so it can be later reversed to
3986 enforce 8.5. The marker will be removed during that operation. */
3987 marker = build_tree_list (NULL_TREE, NULL_TREE);
3988 TREE_CHAIN (marker) = CPC_INITIALIZER_STMT (ctxp);
3989 SET_CPC_INITIALIZER_STMT (ctxp, marker);
3992 /* Can't use lookup_field () since we don't want to load the class and
3993 can't set the CLASS_LOADED_P flag */
3996 find_field (class, name)
4001 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
4003 if (DECL_NAME (decl) == name)
4009 /* Wrap around lookup_field that doesn't potentially upset the value
4013 lookup_field_wrapper (class, name)
4017 tree decl = NULL_TREE;
4018 java_parser_context_save_global ();
4020 /* Last chance: if we're within the context of an inner class, we
4021 might be trying to access a local variable defined in an outer
4022 context. We try to look for it now. */
4023 if (INNER_CLASS_TYPE_P (class))
4027 MANGLE_OUTER_LOCAL_VARIABLE_NAME (alias_buffer, name);
4028 new_name = get_identifier (alias_buffer);
4029 decl = lookup_field (&type, new_name);
4030 if (decl && decl != error_mark_node)
4031 FIELD_LOCAL_ALIAS_USED (decl) = 1;
4033 if (!decl || decl == error_mark_node)
4036 decl = lookup_field (&type, name);
4039 java_parser_context_restore_global ();
4040 return decl == error_mark_node ? NULL : decl;
4043 /* Find duplicate field within the same class declarations and report
4044 the error. Returns 1 if a duplicated field was found, 0
4048 duplicate_declaration_error_p (new_field_name, new_type, cl)
4049 tree new_field_name, new_type, cl;
4051 /* This might be modified to work with method decl as well */
4052 tree decl = find_field (TREE_TYPE (GET_CPC ()), new_field_name);
4055 char *t1 = xstrdup (purify_type_name
4056 ((TREE_CODE (new_type) == POINTER_TYPE
4057 && TREE_TYPE (new_type) == NULL_TREE) ?
4058 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
4059 lang_printable_name (new_type, 1)));
4060 /* The type may not have been completed by the time we report
4062 char *t2 = xstrdup (purify_type_name
4063 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
4064 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
4065 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
4066 lang_printable_name (TREE_TYPE (decl), 1)));
4068 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
4069 t1, IDENTIFIER_POINTER (new_field_name),
4070 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
4071 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
4079 /* Field registration routine. If TYPE doesn't exist, field
4080 declarations are linked to the undefined TYPE dependency list, to
4081 be later resolved in java_complete_class () */
4084 register_fields (flags, type, variable_list)
4086 tree type, variable_list;
4088 tree current, saved_type;
4089 tree class_type = NULL_TREE;
4090 int saved_lineno = lineno;
4092 tree wfl = NULL_TREE;
4095 class_type = TREE_TYPE (GET_CPC ());
4097 if (!class_type || class_type == error_mark_node)
4100 /* If we're adding fields to interfaces, those fields are public,
4102 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
4104 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
4105 flags, ACC_PUBLIC, "interface field(s)");
4106 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
4107 flags, ACC_STATIC, "interface field(s)");
4108 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
4109 flags, ACC_FINAL, "interface field(s)");
4110 check_modifiers ("Illegal interface member modifier `%s'", flags,
4111 INTERFACE_FIELD_MODIFIERS);
4112 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
4115 /* Obtain a suitable type for resolution, if necessary */
4116 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
4118 /* If TYPE is fully resolved and we don't have a reference, make one */
4119 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4121 for (current = variable_list, saved_type = type; current;
4122 current = TREE_CHAIN (current), type = saved_type)
4126 tree cl = TREE_PURPOSE (current);
4127 tree init = TREE_VALUE (current);
4128 tree current_name = EXPR_WFL_NODE (cl);
4130 /* Can't declare non-final static fields in inner classes */
4131 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (class_type)
4132 && !(flags & ACC_FINAL))
4134 (cl, "Field `%s' can't be static in inner class `%s' unless it is final",
4135 IDENTIFIER_POINTER (EXPR_WFL_NODE (cl)),
4136 lang_printable_name (class_type, 0));
4138 /* Process NAME, as it may specify extra dimension(s) for it */
4139 type = build_array_from_name (type, wfl, current_name, ¤t_name);
4141 /* Type adjustment. We may have just readjusted TYPE because
4142 the variable specified more dimensions. Make sure we have
4143 a reference if we can and don't have one already. Also
4144 change the name if we have an init. */
4145 if (type != saved_type)
4147 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4149 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
4152 real_type = GET_REAL_TYPE (type);
4153 /* Check for redeclarations */
4154 if (duplicate_declaration_error_p (current_name, real_type, cl))
4157 /* Set lineno to the line the field was found and create a
4158 declaration for it. Eventually sets the @deprecated tag flag. */
4160 lineno = EXPR_WFL_LINECOL (cl);
4162 lineno = EXPR_WFL_LINENO (cl);
4163 field_decl = add_field (class_type, current_name, real_type, flags);
4164 CHECK_DEPRECATED (field_decl);
4166 /* If the couple initializer/initialized is marked ARG_FINAL_P, we
4167 mark the created field FIELD_LOCAL_ALIAS, so that we can
4168 hide parameters to this inner class $finit$ and constructors. */
4169 if (ARG_FINAL_P (current))
4170 FIELD_LOCAL_ALIAS (field_decl) = 1;
4172 /* Check if we must chain. */
4174 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
4176 /* If we have an initialization value tied to the field */
4179 /* The field is declared static */
4180 if (flags & ACC_STATIC)
4182 /* We include the field and its initialization part into
4183 a list used to generate <clinit>. After <clinit> is
4184 walked, field initializations will be processed and
4185 fields initialized with known constants will be taken
4186 out of <clinit> and have their DECL_INITIAL set
4188 TREE_CHAIN (init) = CPC_STATIC_INITIALIZER_STMT (ctxp);
4189 SET_CPC_STATIC_INITIALIZER_STMT (ctxp, init);
4190 if (TREE_OPERAND (init, 1)
4191 && TREE_CODE (TREE_OPERAND (init, 1)) == NEW_ARRAY_INIT)
4192 TREE_STATIC (TREE_OPERAND (init, 1)) = 1;
4194 /* A non-static field declared with an immediate initialization is
4195 to be initialized in <init>, if any. This field is remembered
4196 to be processed at the time of the generation of <init>. */
4199 TREE_CHAIN (init) = CPC_INITIALIZER_STMT (ctxp);
4200 SET_CPC_INITIALIZER_STMT (ctxp, init);
4202 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4203 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
4206 lineno = saved_lineno;
4209 /* Generate $finit$, using the list of initialized fields to populate
4210 its body. $finit$'s parameter(s) list is adjusted to include the
4211 one(s) used to initialized the field(s) caching outer context
4215 generate_finit (class_type)
4219 tree list = TYPE_FINIT_STMT_LIST (class_type);
4220 tree mdecl, current, parms;
4222 parms = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
4223 class_type, NULL_TREE,
4225 CRAFTED_PARAM_LIST_FIXUP (parms);
4226 mdecl = create_artificial_method (class_type, ACC_PRIVATE, void_type_node,
4227 finit_identifier_node, parms);
4228 fix_method_argument_names (parms, mdecl);
4229 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
4231 DECL_FUNCTION_NAP (mdecl) = count;
4232 start_artificial_method_body (mdecl);
4234 for (current = list; current; current = TREE_CHAIN (current))
4235 java_method_add_stmt (mdecl,
4236 build_debugable_stmt (EXPR_WFL_LINECOL (current),
4238 end_artificial_method_body (mdecl);
4243 add_instance_initializer (mdecl)
4247 tree stmt_list = TYPE_II_STMT_LIST (DECL_CONTEXT (mdecl));
4248 tree compound = NULL_TREE;
4252 for (current = stmt_list; current; current = TREE_CHAIN (current))
4253 compound = add_stmt_to_compound (compound, NULL_TREE, current);
4255 java_method_add_stmt (mdecl, build1 (INSTANCE_INITIALIZERS_EXPR,
4256 NULL_TREE, compound));
4260 /* Shared accros method_declarator and method_header to remember the
4261 patch stage that was reached during the declaration of the method.
4262 A method DECL is built differently is there is no patch
4263 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
4264 pending on the currently defined method. */
4266 static int patch_stage;
4268 /* Check the method declaration and add the method to its current
4269 class. If the argument list is known to contain incomplete types,
4270 the method is partially added and the registration will be resume
4271 once the method arguments resolved. If TYPE is NULL, we're dealing
4272 with a constructor. */
4275 method_header (flags, type, mdecl, throws)
4277 tree type, mdecl, throws;
4279 tree meth = TREE_VALUE (mdecl);
4280 tree id = TREE_PURPOSE (mdecl);
4281 tree type_wfl = NULL_TREE;
4282 tree meth_name = NULL_TREE;
4283 tree current, orig_arg, this_class = NULL;
4285 int constructor_ok = 0, must_chain;
4288 check_modifiers_consistency (flags);
4291 this_class = TREE_TYPE (GET_CPC ());
4293 if (!this_class || this_class == error_mark_node)
4296 /* There are some forbidden modifiers for an abstract method and its
4297 class must be abstract as well. */
4298 if (type && (flags & ACC_ABSTRACT))
4300 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
4301 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
4302 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
4303 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
4304 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
4305 if (!CLASS_ABSTRACT (TYPE_NAME (this_class))
4306 && !CLASS_INTERFACE (TYPE_NAME (this_class)))
4308 (id, "Class `%s' must be declared abstract to define abstract method `%s'",
4309 IDENTIFIER_POINTER (DECL_NAME (GET_CPC ())),
4310 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4313 /* Things to be checked when declaring a constructor */
4316 int ec = java_error_count;
4317 /* 8.6: Constructor declarations: we might be trying to define a
4318 method without specifying a return type. */
4319 if (EXPR_WFL_NODE (id) != GET_CPC_UN ())
4321 (id, "Invalid method declaration, return type required");
4322 /* 8.6.3: Constructor modifiers */
4325 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
4326 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
4327 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
4328 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
4329 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
4331 /* If we found error here, we don't consider it's OK to tread
4332 the method definition as a constructor, for the rest of this
4334 if (ec == java_error_count)
4338 /* Method declared within the scope of an interface are implicitly
4339 abstract and public. Conflicts with other erroneously provided
4340 modifiers are checked right after. */
4342 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
4344 /* If FLAGS isn't set because of a modifier, turn the
4345 corresponding modifier WFL to NULL so we issue a warning on
4346 the obsolete use of the modifier */
4347 if (!(flags & ACC_PUBLIC))
4348 MODIFIER_WFL (PUBLIC_TK) = NULL;
4349 if (!(flags & ACC_ABSTRACT))
4350 MODIFIER_WFL (ABSTRACT_TK) = NULL;
4351 flags |= ACC_PUBLIC;
4352 flags |= ACC_ABSTRACT;
4355 /* Inner class can't declare static methods */
4356 if ((flags & ACC_STATIC) && !TOPLEVEL_CLASS_TYPE_P (this_class))
4359 (id, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
4360 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)),
4361 lang_printable_name (this_class, 0));
4364 /* Modifiers context reset moved up, so abstract method declaration
4365 modifiers can be later checked. */
4367 /* Set constructor returned type to void and method name to <init>,
4368 unless we found an error identifier the constructor (in which
4369 case we retain the original name) */
4372 type = void_type_node;
4374 meth_name = init_identifier_node;
4377 meth_name = EXPR_WFL_NODE (id);
4379 /* Do the returned type resolution and registration if necessary */
4380 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4383 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
4384 EXPR_WFL_NODE (id) = meth_name;
4385 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
4389 patch_stage = JDEP_METHOD_RETURN;
4390 register_incomplete_type (patch_stage, type_wfl, id, type);
4391 TREE_TYPE (meth) = GET_REAL_TYPE (type);
4394 TREE_TYPE (meth) = type;
4396 saved_lineno = lineno;
4397 /* When defining an abstract or interface method, the curly
4398 bracket at level 1 doesn't exist because there is no function
4400 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
4401 EXPR_WFL_LINENO (id));
4403 /* Remember the original argument list */
4404 orig_arg = TYPE_ARG_TYPES (meth);
4406 if (patch_stage) /* includes ret type and/or all args */
4409 meth = add_method_1 (this_class, flags, meth_name, meth);
4410 /* Patch for the return type */
4411 if (patch_stage == JDEP_METHOD_RETURN)
4413 jdep = CLASSD_LAST (ctxp->classd_list);
4414 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
4416 /* This is the stop JDEP. METH allows the function's signature
4418 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
4421 meth = add_method (this_class, flags, meth_name,
4422 build_java_signature (meth));
4424 /* Remember final parameters */
4425 MARK_FINAL_PARMS (meth, orig_arg);
4427 /* Fix the method argument list so we have the argument name
4429 fix_method_argument_names (orig_arg, meth);
4431 /* Register the parameter number and re-install the current line
4433 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
4434 lineno = saved_lineno;
4436 /* Register exception specified by the `throws' keyword for
4437 resolution and set the method decl appropriate field to the list.
4438 Note: the grammar ensures that what we get here are class
4442 throws = nreverse (throws);
4443 for (current = throws; current; current = TREE_CHAIN (current))
4445 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
4446 NULL_TREE, NULL_TREE);
4447 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
4448 &TREE_VALUE (current);
4450 DECL_FUNCTION_THROWS (meth) = throws;
4453 /* We set the DECL_NAME to ID so we can track the location where
4454 the function was declared. This allow us to report
4455 redefinition error accurately. When method are verified,
4456 DECL_NAME is reinstalled properly (using the content of the
4457 WFL node ID) (see check_method_redefinition). We don't do that
4458 when Object is being defined. Constructor <init> names will be
4459 reinstalled the same way. */
4460 if (TREE_TYPE (GET_CPC ()) != object_type_node)
4461 DECL_NAME (meth) = id;
4463 /* Set the flag if we correctly processed a constructor */
4466 DECL_CONSTRUCTOR_P (meth) = 1;
4467 /* Compute and store the number of artificial parameters declared
4468 for this constructor */
4469 for (count = 0, current = TYPE_FIELDS (this_class); current;
4470 current = TREE_CHAIN (current))
4471 if (FIELD_LOCAL_ALIAS (current))
4473 DECL_FUNCTION_NAP (meth) = count;
4476 /* Eventually set the @deprecated tag flag */
4477 CHECK_DEPRECATED (meth);
4479 /* If doing xref, store column and line number information instead
4480 of the line number only. */
4482 DECL_SOURCE_LINE (meth) = EXPR_WFL_LINECOL (id);
4488 fix_method_argument_names (orig_arg, meth)
4489 tree orig_arg, meth;
4491 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
4492 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
4494 TREE_PURPOSE (arg) = this_identifier_node;
4495 arg = TREE_CHAIN (arg);
4497 while (orig_arg != end_params_node)
4499 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
4500 orig_arg = TREE_CHAIN (orig_arg);
4501 arg = TREE_CHAIN (arg);
4505 /* Complete the method declaration with METHOD_BODY. */
4508 finish_method_declaration (method_body)
4513 if (!current_function_decl)
4516 flags = get_access_flags_from_decl (current_function_decl);
4518 /* 8.4.5 Method Body */
4519 if ((flags & ACC_ABSTRACT || flags & ACC_NATIVE) && method_body)
4521 tree wfl = DECL_NAME (current_function_decl);
4522 parse_error_context (wfl,
4523 "%s method `%s' can't have a body defined",
4524 (METHOD_NATIVE (current_function_decl) ?
4525 "Native" : "Abstract"),
4526 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4527 method_body = NULL_TREE;
4529 else if (!(flags & ACC_ABSTRACT) && !(flags & ACC_NATIVE) && !method_body)
4531 tree wfl = DECL_NAME (current_function_decl);
4534 "Non native and non abstract method `%s' must have a body defined",
4535 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
4536 method_body = NULL_TREE;
4539 if (flag_emit_class_files && method_body
4540 && TREE_CODE (method_body) == NOP_EXPR
4541 && TREE_TYPE (current_function_decl)
4542 && TREE_TYPE (TREE_TYPE (current_function_decl)) == void_type_node)
4543 method_body = build1 (RETURN_EXPR, void_type_node, NULL);
4545 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
4546 maybe_absorb_scoping_blocks ();
4547 /* Exit function's body */
4549 /* Merge last line of the function with first line, directly in the
4550 function decl. It will be used to emit correct debug info. */
4551 if (!flag_emit_xref)
4552 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
4554 /* Since function's argument's list are shared, reset the
4555 ARG_FINAL_P parameter that might have been set on some of this
4556 function parameters. */
4557 UNMARK_FINAL_PARMS (current_function_decl);
4559 /* So we don't have an irrelevant function declaration context for
4560 the next static block we'll see. */
4561 current_function_decl = NULL_TREE;
4564 /* Build a an error message for constructor circularity errors. */
4567 constructor_circularity_msg (from, to)
4570 static char string [4096];
4571 char *t = xstrdup (lang_printable_name (from, 0));
4572 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
4577 /* Verify a circular call to METH. Return 1 if an error is found, 0
4581 verify_constructor_circularity (meth, current)
4584 static tree list = NULL_TREE;
4586 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4588 if (TREE_VALUE (c) == meth)
4594 list = nreverse (list);
4595 for (liste = list; liste; liste = TREE_CHAIN (liste))
4598 (TREE_PURPOSE (TREE_PURPOSE (liste)), "%s",
4599 constructor_circularity_msg
4600 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
4604 t = xstrdup (lang_printable_name (meth, 0));
4605 parse_error_context (TREE_PURPOSE (c),
4606 "%s: recursive invocation of constructor `%s'",
4607 constructor_circularity_msg (current, meth), t);
4613 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
4615 list = tree_cons (c, current, list);
4616 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
4618 list = TREE_CHAIN (list);
4623 /* Check modifiers that can be declared but exclusively */
4626 check_modifiers_consistency (flags)
4630 tree cl = NULL_TREE;
4632 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, PUBLIC_TK, acc_count, cl);
4633 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, PRIVATE_TK, acc_count, cl);
4634 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, PROTECTED_TK, acc_count, cl);
4637 (cl, "Inconsistent member declaration. At most one of `public', `private', or `protected' may be specified");
4641 THIS_MODIFIER_ONLY (flags, ACC_FINAL, FINAL_TK, acc_count, cl);
4642 THIS_MODIFIER_ONLY (flags, ACC_VOLATILE, VOLATILE_TK, acc_count, cl);
4644 parse_error_context (cl,
4645 "Inconsistent member declaration. At most one of `final' or `volatile' may be specified");
4648 /* Check the methode header METH for abstract specifics features */
4651 check_abstract_method_header (meth)
4654 int flags = get_access_flags_from_decl (meth);
4655 /* DECL_NAME might still be a WFL node */
4656 tree name = GET_METHOD_NAME (meth);
4658 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (ABSTRACT_TK), flags,
4659 ACC_ABSTRACT, "abstract method",
4660 IDENTIFIER_POINTER (name));
4661 OBSOLETE_MODIFIER_WARNING2 (MODIFIER_WFL (PUBLIC_TK), flags,
4662 ACC_PUBLIC, "abstract method",
4663 IDENTIFIER_POINTER (name));
4665 check_modifiers ("Illegal modifier `%s' for interface method",
4666 flags, INTERFACE_METHOD_MODIFIERS);
4669 /* Create a FUNCTION_TYPE node and start augmenting it with the
4670 declared function arguments. Arguments type that can't be resolved
4671 are left as they are, but the returned node is marked as containing
4672 incomplete types. */
4675 method_declarator (id, list)
4678 tree arg_types = NULL_TREE, current, node;
4679 tree meth = make_node (FUNCTION_TYPE);
4682 patch_stage = JDEP_NO_PATCH;
4684 /* If we're dealing with an inner class constructor, we hide the
4685 this$<n> decl in the name field of its parameter declaration. We
4686 also might have to hide the outer context local alias
4687 initializers. Not done when the class is a toplevel class. */
4688 if (PURE_INNER_CLASS_DECL_P (GET_CPC ())
4689 && EXPR_WFL_NODE (id) == GET_CPC_UN ())
4691 tree aliases_list, type, thisn;
4692 /* First the aliases, linked to the regular parameters */
4694 build_alias_initializer_parameter_list (AIPL_FUNCTION_DECLARATION,
4695 TREE_TYPE (GET_CPC ()),
4697 list = chainon (nreverse (aliases_list), list);
4700 type = TREE_TYPE (DECL_CONTEXT (GET_CPC ()));
4701 thisn = build_current_thisn (TREE_TYPE (GET_CPC ()));
4702 list = tree_cons (build_wfl_node (thisn), build_pointer_type (type),
4706 for (current = list; current; current = TREE_CHAIN (current))
4709 tree wfl_name = TREE_PURPOSE (current);
4710 tree type = TREE_VALUE (current);
4711 tree name = EXPR_WFL_NODE (wfl_name);
4712 tree already, arg_node;
4713 tree type_wfl = NULL_TREE;
4716 /* Obtain a suitable type for resolution, if necessary */
4717 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
4719 /* Process NAME, as it may specify extra dimension(s) for it */
4720 type = build_array_from_name (type, type_wfl, name, &name);
4721 EXPR_WFL_NODE (wfl_name) = name;
4723 real_type = GET_REAL_TYPE (type);
4724 if (TREE_CODE (real_type) == RECORD_TYPE)
4726 real_type = promote_type (real_type);
4727 if (TREE_CODE (type) == TREE_LIST)
4728 TREE_PURPOSE (type) = real_type;
4731 /* Check redefinition */
4732 for (already = arg_types; already; already = TREE_CHAIN (already))
4733 if (TREE_PURPOSE (already) == name)
4736 (wfl_name, "Variable `%s' is used more than once in the argument list of method `%s'",
4737 IDENTIFIER_POINTER (name),
4738 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
4742 /* If we've an incomplete argument type, we know there is a location
4743 to patch when the type get resolved, later. */
4747 patch_stage = JDEP_METHOD;
4748 type = register_incomplete_type (patch_stage,
4749 type_wfl, wfl_name, type);
4750 jdep = CLASSD_LAST (ctxp->classd_list);
4751 JDEP_MISC (jdep) = id;
4754 /* The argument node: a name and a (possibly) incomplete type. */
4755 arg_node = build_tree_list (name, real_type);
4756 /* Remeber arguments declared final. */
4757 ARG_FINAL_P (arg_node) = ARG_FINAL_P (current);
4760 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
4761 TREE_CHAIN (arg_node) = arg_types;
4762 arg_types = arg_node;
4764 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
4765 node = build_tree_list (id, meth);
4770 unresolved_type_p (wfl, returned)
4775 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
4779 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
4780 if (decl && current_class && (decl == TYPE_NAME (current_class)))
4781 *returned = TREE_TYPE (decl);
4782 else if (GET_CPC_UN () == EXPR_WFL_NODE (wfl))
4783 *returned = TREE_TYPE (GET_CPC ());
4785 *returned = NULL_TREE;
4794 /* From NAME, build a qualified identifier node using the
4795 qualification from the current package definition. */
4798 parser_qualified_classname (name)
4801 tree nested_class_name;
4803 if ((nested_class_name = maybe_make_nested_class_name (name)))
4804 return nested_class_name;
4807 return merge_qualified_name (ctxp->package, name);
4812 /* Called once the type a interface extends is resolved. Returns 0 if
4813 everything is OK. */
4816 parser_check_super_interface (super_decl, this_decl, this_wfl)
4817 tree super_decl, this_decl, this_wfl;
4819 tree super_type = TREE_TYPE (super_decl);
4821 /* Has to be an interface */
4822 if (!CLASS_INTERFACE (super_decl))
4825 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
4826 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
4827 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
4828 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
4829 "interface" : "class"),
4830 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
4834 /* Check scope: same package OK, other package: OK if public */
4835 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
4838 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
4839 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4840 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4844 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
4845 0 if everthing is OK. */
4848 parser_check_super (super_decl, this_decl, wfl)
4849 tree super_decl, this_decl, wfl;
4851 tree super_type = TREE_TYPE (super_decl);
4853 /* SUPER should be a CLASS (neither an array nor an interface) */
4854 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
4857 (wfl, "Class `%s' can't subclass %s `%s'",
4858 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4859 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
4860 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4864 if (CLASS_FINAL (TYPE_NAME (super_type)))
4866 parse_error_context (wfl, "Can't subclass final classes: %s",
4867 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
4871 /* Check scope: same package OK, other package: OK if public */
4872 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
4875 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
4876 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
4877 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
4881 /* Create a new dependency list and link it (in a LIFO manner) to the
4882 CTXP list of type dependency list. */
4885 create_jdep_list (ctxp)
4886 struct parser_ctxt *ctxp;
4888 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
4889 new->first = new->last = NULL;
4890 new->next = ctxp->classd_list;
4891 ctxp->classd_list = new;
4895 reverse_jdep_list (ctxp)
4896 struct parser_ctxt *ctxp;
4898 register jdeplist *prev = NULL, *current, *next;
4899 for (current = ctxp->classd_list; current; current = next)
4901 next = current->next;
4902 current->next = prev;
4908 /* Create a fake pointer based on the ID stored in
4909 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
4910 registered again. */
4913 obtain_incomplete_type (type_name)
4918 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
4919 name = EXPR_WFL_NODE (type_name);
4920 else if (INCOMPLETE_TYPE_P (type_name))
4921 name = TYPE_NAME (type_name);
4923 fatal ("invalid type name - obtain_incomplete_type");
4925 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
4926 if (TYPE_NAME (ptr) == name)
4931 push_obstacks (&permanent_obstack, &permanent_obstack);
4932 BUILD_PTR_FROM_NAME (ptr, name);
4935 TREE_CHAIN (ptr) = ctxp->incomplete_class;
4936 ctxp->incomplete_class = ptr;
4942 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
4943 non NULL instead of computing a new fake type based on WFL. The new
4944 dependency is inserted in the current type dependency list, in FIFO
4948 register_incomplete_type (kind, wfl, decl, ptr)
4950 tree wfl, decl, ptr;
4952 jdep *new = (jdep *)xmalloc (sizeof (jdep));
4954 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
4955 ptr = obtain_incomplete_type (wfl);
4957 JDEP_KIND (new) = kind;
4958 JDEP_DECL (new) = decl;
4959 JDEP_SOLV (new) = ptr;
4960 JDEP_WFL (new) = wfl;
4961 JDEP_CHAIN (new) = NULL;
4962 JDEP_MISC (new) = NULL_TREE;
4963 /* For some dependencies, set the enclosing class of the current
4964 class to be the enclosing context */
4965 if ((kind == JDEP_SUPER || kind == JDEP_INTERFACE || kind == JDEP_ANONYMOUS)
4966 && GET_ENCLOSING_CPC ())
4967 JDEP_ENCLOSING (new) = TREE_VALUE (GET_ENCLOSING_CPC ());
4969 JDEP_ENCLOSING (new) = GET_CPC ();
4970 JDEP_GET_PATCH (new) = (tree *)NULL;
4972 JDEP_INSERT (ctxp->classd_list, new);
4978 java_check_circular_reference ()
4981 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
4983 tree type = TREE_TYPE (current);
4984 if (CLASS_INTERFACE (current))
4986 /* Check all interfaces this class extends */
4987 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
4992 n = TREE_VEC_LENGTH (basetype_vec);
4993 for (i = 0; i < n; i++)
4995 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
4996 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
4997 && interface_of_p (type, BINFO_TYPE (vec_elt)))
4998 parse_error_context (lookup_cl (current),
4999 "Cyclic interface inheritance");
5003 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
5004 parse_error_context (lookup_cl (current),
5005 "Cyclic class inheritance%s",
5006 (cyclic_inheritance_report ?
5007 cyclic_inheritance_report : ""));
5011 /* Augment the parameter list PARM with parameters crafted to
5012 initialize outer context locals aliases. Through ARTIFICIAL, a
5013 count is kept of the number of crafted parameters. MODE governs
5014 what eventually gets created: something suitable for a function
5015 creation or a function invocation, either the constructor or
5019 build_alias_initializer_parameter_list (mode, class_type, parm, artificial)
5021 tree class_type, parm;
5025 for (field = TYPE_FIELDS (class_type); field; field = TREE_CHAIN (field))
5026 if (FIELD_LOCAL_ALIAS (field))
5028 char *buffer = IDENTIFIER_POINTER (DECL_NAME (field));
5029 tree purpose = NULL_TREE, value = NULL_TREE, name = NULL_TREE;
5033 case AIPL_FUNCTION_DECLARATION:
5034 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5035 purpose = build_wfl_node (get_identifier (buffer));
5036 if (TREE_CODE (TREE_TYPE (field)) == POINTER_TYPE)
5037 value = build_wfl_node (TYPE_NAME (TREE_TYPE (field)));
5039 value = TREE_TYPE (field);
5042 case AIPL_FUNCTION_CREATION:
5043 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5044 purpose = get_identifier (buffer);
5045 value = TREE_TYPE (field);
5048 case AIPL_FUNCTION_FINIT_INVOCATION:
5049 MANGLE_ALIAS_INITIALIZER_PARAMETER_NAME_STR (buffer, &buffer [4]);
5050 /* Now, this is wrong. purpose should always be the NAME
5051 of something and value its matching value (decl, type,
5052 etc...) FIXME -- but there is a lot to fix. */
5054 /* When invoked for this kind of operation, we already
5055 know whether a field is used or not. */
5056 purpose = TREE_TYPE (field);
5057 value = build_wfl_node (get_identifier (buffer));
5060 case AIPL_FUNCTION_CTOR_INVOCATION:
5061 /* There are two case: the constructor invokation happends
5062 outside the local inner, in which case, locales from the outer
5063 context are directly used.
5065 Otherwise, we fold to using the alias directly. */
5066 if (class_type == current_class)
5070 name = get_identifier (&buffer[4]);
5071 value = IDENTIFIER_LOCAL_VALUE (name);
5075 parm = tree_cons (purpose, value, parm);
5082 /* Craft a constructor for CLASS_DECL -- what we should do when none
5083 where found. ARGS is non NULL when a special signature must be
5084 enforced. This is the case for anonymous classes. */
5087 craft_constructor (class_decl, args)
5088 tree class_decl, args;
5090 tree class_type = TREE_TYPE (class_decl);
5091 tree parm = NULL_TREE;
5092 int flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
5094 int i = 0, artificial = 0;
5095 tree decl, ctor_name;
5098 push_obstacks (&permanent_obstack, &permanent_obstack);
5100 /* The constructor name is <init> unless we're dealing with an
5101 anonymous class, in which case the name will be fixed after having
5103 if (ANONYMOUS_CLASS_P (class_type))
5104 ctor_name = DECL_NAME (class_decl);
5106 ctor_name = init_identifier_node;
5108 /* If we're dealing with an inner class constructor, we hide the
5109 this$<n> decl in the name field of its parameter declaration. */
5110 if (PURE_INNER_CLASS_TYPE_P (class_type))
5112 tree type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class_type)));
5113 parm = tree_cons (build_current_thisn (class_type),
5114 build_pointer_type (type), parm);
5116 /* Some more arguments to be hidden here. The values of the local
5117 variables of the outer context that the inner class needs to see. */
5118 parm = build_alias_initializer_parameter_list (AIPL_FUNCTION_CREATION,
5123 /* Then if there are any args to be enforced, enforce them now */
5124 for (; args && args != end_params_node; args = TREE_CHAIN (args))
5126 sprintf (buffer, "parm%d", i++);
5127 parm = tree_cons (get_identifier (buffer), TREE_VALUE (args), parm);
5130 CRAFTED_PARAM_LIST_FIXUP (parm);
5131 decl = create_artificial_method (class_type, flags, void_type_node,
5133 fix_method_argument_names (parm, decl);
5134 /* Now, mark the artificial parameters. */
5135 DECL_FUNCTION_NAP (decl) = artificial;
5138 DECL_CONSTRUCTOR_P (decl) = 1;
5142 /* Fix the constructors. This will be called right after circular
5143 references have been checked. It is necessary to fix constructors
5144 early even if no code generation will take place for that class:
5145 some generated constructor might be required by the class whose
5146 compilation triggered this one to be simply loaded. */
5149 java_fix_constructors ()
5153 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5155 tree class_type = TREE_TYPE (current);
5159 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
5162 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5164 if (DECL_CONSTRUCTOR_P (decl))
5166 fix_constructors (decl);
5171 /* Anonymous class constructor can't be generated that early. */
5172 if (!saw_ctor && !ANONYMOUS_CLASS_P (class_type))
5173 craft_constructor (current, NULL_TREE);
5177 /* safe_layout_class just makes sure that we can load a class without
5178 disrupting the current_class, input_file, lineno, etc, information
5179 about the class processed currently. */
5182 safe_layout_class (class)
5185 tree save_current_class = current_class;
5186 const char *save_input_filename = input_filename;
5187 int save_lineno = lineno;
5189 push_obstacks (&permanent_obstack, &permanent_obstack);
5191 layout_class (class);
5194 current_class = save_current_class;
5195 input_filename = save_input_filename;
5196 lineno = save_lineno;
5197 CLASS_LOADED_P (class) = 1;
5201 jdep_resolve_class (dep)
5206 if (JDEP_RESOLVED_P (dep))
5207 decl = JDEP_RESOLVED_DECL (dep);
5210 decl = resolve_class (JDEP_ENCLOSING (dep), JDEP_TO_RESOLVE (dep),
5211 JDEP_DECL (dep), JDEP_WFL (dep));
5212 JDEP_RESOLVED (dep, decl);
5216 complete_class_report_errors (dep);
5218 if (PURE_INNER_CLASS_DECL_P (decl))
5219 check_inner_class_access (decl, JDEP_ENCLOSING (dep), JDEP_WFL (dep));
5223 /* Complete unsatisfied class declaration and their dependencies */
5226 java_complete_class ()
5233 push_obstacks (&permanent_obstack, &permanent_obstack);
5235 /* Process imports */
5238 /* Rever things so we have the right order */
5239 ctxp->class_list = nreverse (ctxp->class_list);
5240 ctxp->classd_list = reverse_jdep_list (ctxp);
5242 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
5244 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
5247 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
5250 if (!(decl = jdep_resolve_class (dep)))
5253 /* Now it's time to patch */
5254 switch (JDEP_KIND (dep))
5257 /* Simply patch super */
5258 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
5260 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
5261 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
5266 /* We do part of the job done in add_field */
5267 tree field_decl = JDEP_DECL (dep);
5268 tree field_type = TREE_TYPE (decl);
5269 push_obstacks (&permanent_obstack, &permanent_obstack);
5270 if (TREE_CODE (field_type) == RECORD_TYPE)
5271 field_type = promote_type (field_type);
5273 TREE_TYPE (field_decl) = field_type;
5274 DECL_ALIGN (field_decl) = 0;
5275 DECL_USER_ALIGN (field_decl) = 0;
5276 layout_decl (field_decl, 0);
5277 SOURCE_FRONTEND_DEBUG
5278 (("Completed field/var decl `%s' with `%s'",
5279 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5280 IDENTIFIER_POINTER (DECL_NAME (decl))));
5283 case JDEP_METHOD: /* We start patching a method */
5284 case JDEP_METHOD_RETURN:
5290 type = TREE_TYPE(decl);
5291 if (TREE_CODE (type) == RECORD_TYPE)
5292 type = promote_type (type);
5293 JDEP_APPLY_PATCH (dep, type);
5294 SOURCE_FRONTEND_DEBUG
5295 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
5296 "Completing fct `%s' with ret type `%s'":
5297 "Completing arg `%s' with type `%s'"),
5298 IDENTIFIER_POINTER (EXPR_WFL_NODE
5299 (JDEP_DECL_WFL (dep))),
5300 IDENTIFIER_POINTER (DECL_NAME (decl))));
5304 dep = JDEP_CHAIN (dep);
5305 if (JDEP_KIND (dep) == JDEP_METHOD_END)
5308 decl = jdep_resolve_class (dep);
5312 tree mdecl = JDEP_DECL (dep), signature;
5313 push_obstacks (&permanent_obstack, &permanent_obstack);
5314 /* Recompute and reset the signature, check first that
5315 all types are now defined. If they're not,
5316 dont build the signature. */
5317 if (check_method_types_complete (mdecl))
5319 signature = build_java_signature (TREE_TYPE (mdecl));
5320 set_java_signature (TREE_TYPE (mdecl), signature);
5328 case JDEP_INTERFACE:
5329 if (parser_check_super_interface (decl, JDEP_DECL (dep),
5332 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
5337 type = TREE_TYPE(decl);
5338 if (TREE_CODE (type) == RECORD_TYPE)
5339 type = promote_type (type);
5340 JDEP_APPLY_PATCH (dep, type);
5344 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5345 SOURCE_FRONTEND_DEBUG
5346 (("Completing a random type dependency on a '%s' node",
5347 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
5350 case JDEP_EXCEPTION:
5351 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
5352 SOURCE_FRONTEND_DEBUG
5353 (("Completing `%s' `throws' argument node",
5354 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
5357 case JDEP_ANONYMOUS:
5358 patch_anonymous_class (decl, JDEP_DECL (dep), JDEP_WFL (dep));
5362 fatal ("Can't handle patch code %d - java_complete_class",
5371 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
5375 resolve_class (enclosing, class_type, decl, cl)
5376 tree enclosing, class_type, decl, cl;
5378 const char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
5379 const char *base = name;
5380 tree resolved_type = TREE_TYPE (class_type);
5381 tree resolved_type_decl;
5383 if (resolved_type != NULL_TREE)
5385 tree resolved_type_decl = TYPE_NAME (resolved_type);
5386 if (resolved_type_decl == NULL_TREE
5387 || TREE_CODE (resolved_type_decl) == IDENTIFIER_NODE)
5389 resolved_type_decl = build_decl (TYPE_DECL,
5390 TYPE_NAME (class_type),
5393 return resolved_type_decl;
5396 /* 1- Check to see if we have an array. If true, find what we really
5398 while (name[0] == '[')
5401 TYPE_NAME (class_type) = get_identifier (name);
5403 /* 2- Resolve the bare type */
5404 if (!(resolved_type_decl = do_resolve_class (enclosing, class_type,
5407 resolved_type = TREE_TYPE (resolved_type_decl);
5409 /* 3- If we have and array, reconstruct the array down to its nesting */
5412 while (base != name)
5414 if (TREE_CODE (resolved_type) == RECORD_TYPE)
5415 resolved_type = promote_type (resolved_type);
5416 resolved_type = build_java_array_type (resolved_type, -1);
5417 CLASS_LOADED_P (resolved_type) = 1;
5420 /* Build a fake decl for this, since this is what is expected to
5422 resolved_type_decl =
5423 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
5424 /* Figure how those two things are important for error report. FIXME */
5425 DECL_SOURCE_LINE (resolved_type_decl) = 0;
5426 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
5427 TYPE_NAME (class_type) = TYPE_NAME (resolved_type);
5429 TREE_TYPE (class_type) = resolved_type;
5430 return resolved_type_decl;
5433 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
5434 are used to report error messages. */
5437 do_resolve_class (enclosing, class_type, decl, cl)
5438 tree enclosing, class_type, decl, cl;
5440 tree new_class_decl;
5442 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
5443 it is changed by find_in_imports{_on_demand} and (but it doesn't
5444 really matter) qualify_and_find */
5446 /* 0- Search in the current class as an inner class */
5448 /* Maybe some code here should be added to load the class or
5449 something, at least if the class isn't an inner class and ended
5450 being loaded from class file. FIXME. */
5455 if ((new_class_decl = find_as_inner_class (enclosing, class_type, cl)))
5456 return new_class_decl;
5458 /* Explore enclosing contexts. */
5459 while (INNER_CLASS_DECL_P (enclosing))
5461 enclosing = DECL_CONTEXT (enclosing);
5462 if ((new_class_decl = find_as_inner_class (enclosing,
5464 return new_class_decl;
5467 /* Now go to the upper classes, bail out if necessary. */
5468 enclosing = CLASSTYPE_SUPER (TREE_TYPE (enclosing));
5469 if (!enclosing || enclosing == object_type_node)
5472 if (TREE_CODE (enclosing) == RECORD_TYPE)
5474 enclosing = TYPE_NAME (enclosing);
5478 if (TREE_CODE (enclosing) == IDENTIFIER_NODE)
5479 BUILD_PTR_FROM_NAME (name, enclosing);
5482 enclosing = do_resolve_class (NULL, name, NULL, NULL);
5485 /* 1- Check for the type in single imports. This will change
5486 TYPE_NAME() if something relevant is found */
5487 find_in_imports (class_type);
5489 /* 2- And check for the type in the current compilation unit */
5490 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5492 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5493 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5494 load_class (TYPE_NAME (class_type), 0);
5495 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5498 /* 3- Search according to the current package definition */
5499 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5501 if ((new_class_decl = qualify_and_find (class_type, ctxp->package,
5502 TYPE_NAME (class_type))))
5503 return new_class_decl;
5506 /* 4- Check the import on demands. Don't allow bar.baz to be
5507 imported from foo.* */
5508 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5509 if (find_in_imports_on_demand (class_type))
5512 /* If found in find_in_imports_on_demant, the type has already been
5514 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
5515 return new_class_decl;
5517 /* 5- Try with a name qualified with the package name we've seen so far */
5518 if (!QUALIFIED_P (TYPE_NAME (class_type)))
5522 /* If there is a current package (ctxp->package), it's the first
5523 element of package_list and we can skip it. */
5524 for (package = (ctxp->package ?
5525 TREE_CHAIN (package_list) : package_list);
5526 package; package = TREE_CHAIN (package))
5527 if ((new_class_decl = qualify_and_find (class_type,
5528 TREE_PURPOSE (package),
5529 TYPE_NAME (class_type))))
5530 return new_class_decl;
5533 /* 5- Check an other compilation unit that bears the name of type */
5534 load_class (TYPE_NAME (class_type), 0);
5535 if (check_pkg_class_access (TYPE_NAME (class_type),
5536 (cl ? cl : lookup_cl (decl))))
5539 /* 6- Last call for a resolution */
5540 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5544 qualify_and_find (class_type, package, name)
5545 tree class_type, package, name;
5547 tree new_qualified = merge_qualified_name (package, name);
5548 tree new_class_decl;
5550 if (!IDENTIFIER_CLASS_VALUE (new_qualified))
5551 load_class (new_qualified, 0);
5552 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (new_qualified)))
5554 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
5555 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
5556 load_class (new_qualified, 0);
5557 TYPE_NAME (class_type) = new_qualified;
5558 return IDENTIFIER_CLASS_VALUE (new_qualified);
5563 /* Resolve NAME and lay it out (if not done and if not the current
5564 parsed class). Return a decl node. This function is meant to be
5565 called when type resolution is necessary during the walk pass. */
5568 resolve_and_layout (something, cl)
5574 /* Don't do that on the current class */
5575 if (something == current_class)
5576 return TYPE_NAME (current_class);
5578 /* Don't do anything for void and other primitive types */
5579 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5582 /* Pointer types can be reall pointer types or fake pointers. When
5583 finding a real pointer, recheck for primitive types */
5584 if (TREE_CODE (something) == POINTER_TYPE)
5586 if (TREE_TYPE (something))
5588 something = TREE_TYPE (something);
5589 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
5593 something = TYPE_NAME (something);
5596 /* Don't do anything for arrays of primitive types */
5597 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
5598 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
5601 /* Something might be a WFL */
5602 if (TREE_CODE (something) == EXPR_WITH_FILE_LOCATION)
5603 something = EXPR_WFL_NODE (something);
5605 /* Otherwise, if something is not and IDENTIFIER_NODE, it can be a a
5606 TYPE_DECL or a real TYPE */
5607 else if (TREE_CODE (something) != IDENTIFIER_NODE)
5608 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
5609 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
5611 if (!(decl = resolve_no_layout (something, cl)))
5614 /* Resolve and layout if necessary */
5615 layout_class_methods (TREE_TYPE (decl));
5616 /* Check methods, but only once */
5617 if (CLASS_FROM_SOURCE_P (TREE_TYPE (decl))
5618 && !CLASS_LOADED_P (TREE_TYPE (decl)))
5619 CHECK_METHODS (decl);
5620 if (TREE_TYPE (decl) != current_class && !CLASS_LOADED_P (TREE_TYPE (decl)))
5621 safe_layout_class (TREE_TYPE (decl));
5626 /* Resolve a class, returns its decl but doesn't perform any
5627 layout. The current parsing context is saved and restored */
5630 resolve_no_layout (name, cl)
5634 BUILD_PTR_FROM_NAME (ptr, name);
5635 java_parser_context_save_global ();
5636 decl = resolve_class (TYPE_NAME (current_class), ptr, NULL_TREE, cl);
5637 java_parser_context_restore_global ();
5642 /* Called when reporting errors. Skip leader '[' in a complex array
5643 type description that failed to be resolved. */
5646 purify_type_name (name)
5649 while (*name && *name == '[')
5654 /* The type CURRENT refers to can't be found. We print error messages. */
5657 complete_class_report_errors (dep)
5662 if (!JDEP_WFL (dep))
5665 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
5666 switch (JDEP_KIND (dep))
5670 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
5671 purify_type_name (name),
5672 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5676 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
5677 purify_type_name (name),
5678 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5680 case JDEP_METHOD: /* Covers arguments */
5682 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the argument `%s' of method `%s'",
5683 purify_type_name (name),
5684 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
5685 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
5687 case JDEP_METHOD_RETURN: /* Covers return type */
5689 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the return type of method `%s'",
5690 purify_type_name (name),
5691 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
5693 case JDEP_INTERFACE:
5695 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
5696 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
5697 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
5698 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5702 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the local variable `%s'",
5703 purify_type_name (IDENTIFIER_POINTER
5704 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
5705 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
5707 case JDEP_EXCEPTION: /* As specified by `throws' */
5709 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
5710 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
5713 /* Fix for -Wall. Just break doing nothing. The error will be
5719 /* Return a static string containing the DECL prototype string. If
5720 DECL is a constructor, use the class name instead of the form
5724 get_printable_method_name (decl)
5727 const char *to_return;
5728 tree name = NULL_TREE;
5730 if (DECL_CONSTRUCTOR_P (decl))
5732 name = DECL_NAME (decl);
5733 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
5736 to_return = lang_printable_name (decl, 0);
5737 if (DECL_CONSTRUCTOR_P (decl))
5738 DECL_NAME (decl) = name;
5743 /* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
5744 nevertheless needs to be verfied, 1 otherwise. */
5747 reset_method_name (method)
5750 if (!DECL_CLINIT_P (method) && !DECL_FINIT_P (method))
5752 /* NAME is just the plain name when Object is being defined */
5753 if (DECL_CONTEXT (method) != object_type_node)
5754 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
5755 init_identifier_node : GET_METHOD_NAME (method));
5762 /* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
5765 java_get_real_method_name (method_decl)
5768 tree method_name = DECL_NAME (method_decl);
5769 if (DECL_CONSTRUCTOR_P (method_decl))
5770 return init_identifier_node;
5772 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
5773 and still can be a constructor. FIXME */
5775 /* Don't confuse method only bearing the name of their class as
5777 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
5779 && GET_CPC_UN () == EXPR_WFL_NODE (method_name)
5780 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
5781 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
5782 return init_identifier_node;
5784 return EXPR_WFL_NODE (method_name);
5787 /* Track method being redefined inside the same class. As a side
5788 effect, set DECL_NAME to an IDENTIFIER (prior entering this
5789 function it's a FWL, so we can track errors more accurately.) */
5792 check_method_redefinition (class, method)
5796 tree cl = DECL_NAME (method);
5797 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
5798 /* decl name of artificial <clinit> and $finit$ doesn't need to be
5799 fixed and checked */
5801 /* Reset the method name before running the check. If it returns 1,
5802 the method doesn't need to be verified with respect to method
5803 redeclaration and we return 0 */
5804 if (reset_method_name (method))
5807 name = DECL_NAME (method);
5808 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
5810 if (redef == method)
5812 if (DECL_NAME (redef) == name
5813 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
5816 (cl, "Duplicate %s declaration `%s'",
5817 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
5818 get_printable_method_name (redef));
5826 check_abstract_method_definitions (do_interface, class_decl, type)
5828 tree class_decl, type;
5830 tree class = TREE_TYPE (class_decl);
5831 tree method, end_type;
5833 end_type = (do_interface ? object_type_node : type);
5834 for (method = TYPE_METHODS (type); method; method = TREE_CHAIN (method))
5836 tree other_super, other_method, method_sig, method_name;
5838 int end_type_reached = 0;
5840 if (!METHOD_ABSTRACT (method) || METHOD_FINAL (method))
5843 /* Now verify that somewhere in between TYPE and CLASS,
5844 abstract method METHOD gets a non abstract definition
5845 that is inherited by CLASS. */
5847 method_sig = build_java_signature (TREE_TYPE (method));
5848 method_name = DECL_NAME (method);
5849 if (TREE_CODE (method_name) == EXPR_WITH_FILE_LOCATION)
5850 method_name = EXPR_WFL_NODE (method_name);
5852 other_super = class;
5854 if (other_super == end_type)
5855 end_type_reached = 1;
5858 for (other_method = TYPE_METHODS (other_super); other_method;
5859 other_method = TREE_CHAIN (other_method))
5861 tree s = build_java_signature (TREE_TYPE (other_method));
5862 tree other_name = DECL_NAME (other_method);
5864 if (TREE_CODE (other_name) == EXPR_WITH_FILE_LOCATION)
5865 other_name = EXPR_WFL_NODE (other_name);
5866 if (!DECL_CLINIT_P (other_method)
5867 && !DECL_CONSTRUCTOR_P (other_method)
5868 && method_name == other_name
5870 && !METHOD_ABSTRACT (other_method))
5876 other_super = CLASSTYPE_SUPER (other_super);
5877 } while (!end_type_reached);
5879 /* Report that abstract METHOD didn't find an implementation
5880 that CLASS can use. */
5883 char *t = xstrdup (lang_printable_name
5884 (TREE_TYPE (TREE_TYPE (method)), 0));
5885 tree ccn = DECL_NAME (TYPE_NAME (DECL_CONTEXT (method)));
5886 tree saved_wfl = NULL_TREE;
5888 if (TREE_CODE (DECL_NAME (method)) == EXPR_WITH_FILE_LOCATION)
5890 saved_wfl = DECL_NAME (method);
5891 DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
5895 (lookup_cl (class_decl),
5896 "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",
5897 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
5898 t, lang_printable_name (method, 0),
5899 (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))) ?
5900 "interface" : "class"),
5901 IDENTIFIER_POINTER (ccn),
5902 (CLASS_INTERFACE (class_decl) ? "interface" : "class"),
5903 IDENTIFIER_POINTER (DECL_NAME (class_decl)));
5908 DECL_NAME (method) = saved_wfl;
5913 /* Check that CLASS_DECL somehow implements all inherited abstract
5917 java_check_abstract_method_definitions (class_decl)
5920 tree class = TREE_TYPE (class_decl);
5924 if (CLASS_ABSTRACT (class_decl))
5927 /* Check for inherited types */
5930 super = CLASSTYPE_SUPER (super);
5931 check_abstract_method_definitions (0, class_decl, super);
5932 } while (super != object_type_node);
5934 /* Check for implemented interfaces. */
5935 vector = TYPE_BINFO_BASETYPES (class);
5936 for (i = 1; i < TREE_VEC_LENGTH (vector); i++)
5938 super = BINFO_TYPE (TREE_VEC_ELT (vector, i));
5939 check_abstract_method_definitions (1, class_decl, super);
5943 /* Check all the types method DECL uses and return 1 if all of them
5944 are now complete, 0 otherwise. This is used to check whether its
5945 safe to build a method signature or not. */
5948 check_method_types_complete (decl)
5951 tree type = TREE_TYPE (decl);
5954 if (!INCOMPLETE_TYPE_P (TREE_TYPE (type)))
5957 args = TYPE_ARG_TYPES (type);
5958 if (TREE_CODE (type) == METHOD_TYPE)
5959 args = TREE_CHAIN (args);
5960 for (; args != end_params_node; args = TREE_CHAIN (args))
5961 if (INCOMPLETE_TYPE_P (TREE_VALUE (args)))
5967 /* Check all the methods of CLASS_DECL. Methods are first completed
5968 then checked according to regular method existance rules. If no
5969 constructor for CLASS_DECL were encountered, then build its
5973 java_check_regular_methods (class_decl)
5976 int saw_constructor = ANONYMOUS_CLASS_P (TREE_TYPE (class_decl));
5978 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
5979 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
5982 /* It is not necessary to check methods defined in java.lang.Object */
5983 if (class == object_type_node)
5986 if (!TYPE_NVIRTUALS (class))
5987 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
5989 /* Should take interfaces into account. FIXME */
5990 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
5993 tree method_wfl = DECL_NAME (method);
5996 /* If we previously found something and its name was saved,
5998 if (found && saved_found_wfl)
6000 DECL_NAME (found) = saved_found_wfl;
6001 saved_found_wfl = NULL_TREE;
6004 /* Check for redefinitions */
6005 if (check_method_redefinition (class, method))
6008 /* If we see one constructor a mark so we don't generate the
6009 default one. Also skip other verifications: constructors
6010 can't be inherited hence hiden or overriden */
6011 if (DECL_CONSTRUCTOR_P (method))
6013 saw_constructor = 1;
6017 /* We verify things thrown by the method. They must inherits from
6018 java.lang.Throwable */
6019 for (mthrows = DECL_FUNCTION_THROWS (method);
6020 mthrows; mthrows = TREE_CHAIN (mthrows))
6022 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
6024 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be a subclass of class `java.lang.Throwable'",
6026 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
6029 sig = build_java_argument_signature (TREE_TYPE (method));
6030 found = lookup_argument_method2 (class, DECL_NAME (method), sig);
6032 /* Inner class can't declare static methods */
6033 if (METHOD_STATIC (method) && !TOPLEVEL_CLASS_DECL_P (class_decl))
6035 char *t = xstrdup (lang_printable_name (class, 0));
6037 (method_wfl, "Method `%s' can't be static in inner class `%s'. Only members of interfaces and top-level classes can be static",
6038 lang_printable_name (method, 0), t);
6042 /* Nothing overrides or it's a private method. */
6045 if (METHOD_PRIVATE (found))
6051 /* If found wasn't verified, it's DECL_NAME won't be set properly.
6052 We set it temporarily for the sake of the error report. */
6053 saved_found_wfl = DECL_NAME (found);
6054 reset_method_name (found);
6056 /* If `found' is declared in an interface, make sure the
6057 modifier matches. */
6058 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6059 && clinit_identifier_node != DECL_NAME (found)
6060 && !METHOD_PUBLIC (method))
6062 tree found_decl = TYPE_NAME (DECL_CONTEXT (found));
6063 parse_error_context (method_wfl, "Class `%s' must override `%s' with a public method in order to implement interface `%s'",
6064 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6065 lang_printable_name (method, 0),
6066 IDENTIFIER_POINTER (DECL_NAME (found_decl)));
6069 /* Can't override a method with the same name and different return
6071 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
6074 (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6077 "Method `%s' was defined with return type `%s' in class `%s'",
6078 lang_printable_name (found, 0), t,
6080 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6084 aflags = get_access_flags_from_decl (found);
6085 /* If the method has default, access in an other package, then
6086 issue a warning that the current method doesn't override the
6087 one that was found elsewhere. Do not issue this warning when
6088 the match was found in java.lang.Object. */
6089 if (DECL_CONTEXT (found) != object_type_node
6090 && ((aflags & ACC_VISIBILITY) == 0)
6091 && !class_in_current_package (DECL_CONTEXT (found))
6092 && !DECL_CLINIT_P (found)
6093 && flag_not_overriding)
6095 parse_warning_context
6096 (method_wfl, "Method `%s' in class `%s' does not override the corresponding method in class `%s', which is private to a different package",
6097 lang_printable_name (found, 0),
6098 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
6099 IDENTIFIER_POINTER (DECL_NAME
6100 (TYPE_NAME (DECL_CONTEXT (found)))));
6104 /* Can't override final. Can't override static. */
6105 if (METHOD_FINAL (found) || METHOD_STATIC (found))
6107 /* Static *can* override static */
6108 if (METHOD_STATIC (found) && METHOD_STATIC (method))
6112 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
6113 (METHOD_FINAL (found) ? "Final" : "Static"),
6114 lang_printable_name (found, 0),
6115 (METHOD_FINAL (found) ? "final" : "static"),
6117 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6121 /* Static method can't override instance method. */
6122 if (METHOD_STATIC (method))
6126 "Instance methods can't be overriden by a static method. Method `%s' is an instance method in class `%s'",
6127 lang_printable_name (found, 0),
6129 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6133 /* - Overriding/hiding public must be public
6134 - Overriding/hiding protected must be protected or public
6135 - If the overriden or hidden method has default (package)
6136 access, then the overriding or hiding method must not be
6137 private; otherwise, a compile-time error occurs. If
6138 `found' belongs to an interface, things have been already
6140 if (!CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (found)))
6141 && ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
6142 || (METHOD_PROTECTED (found)
6143 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
6144 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
6145 && METHOD_PRIVATE (method))))
6149 "Methods can't be overridden to be more private. Method `%s' is not %s in class `%s'", lang_printable_name (method, 0),
6150 (METHOD_PUBLIC (method) ? "public" :
6151 (METHOD_PRIVATE (method) ? "private" : "protected")),
6152 IDENTIFIER_POINTER (DECL_NAME
6153 (TYPE_NAME (DECL_CONTEXT (found)))));
6157 /* Overriding methods must have compatible `throws' clauses on checked
6158 exceptions, if any */
6159 check_throws_clauses (method, method_wfl, found);
6161 /* Inheriting multiple methods with the same signature. FIXME */
6164 /* Don't forget eventual pending found and saved_found_wfl. Take
6165 into account that we might have exited because we saw an
6166 artificial method as the last entry. */
6168 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
6169 DECL_NAME (found) = saved_found_wfl;
6171 if (!TYPE_NVIRTUALS (class))
6172 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
6174 /* Search for inherited abstract method not yet implemented in this
6176 java_check_abstract_method_definitions (class_decl);
6178 if (!saw_constructor)
6179 fatal ("No constructor found");
6182 /* Return a non zero value if the `throws' clause of METHOD (if any)
6183 is incompatible with the `throws' clause of FOUND (if any). */
6186 check_throws_clauses (method, method_wfl, found)
6187 tree method, method_wfl, found;
6189 tree mthrows, fthrows;
6191 /* Can't check these things with class loaded from bytecode. FIXME */
6192 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
6195 for (mthrows = DECL_FUNCTION_THROWS (method);
6196 mthrows; mthrows = TREE_CHAIN (mthrows))
6198 /* We don't verify unchecked expressions */
6199 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
6201 /* Checked expression must be compatible */
6202 for (fthrows = DECL_FUNCTION_THROWS (found);
6203 fthrows; fthrows = TREE_CHAIN (fthrows))
6204 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
6209 (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'",
6210 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
6211 lang_printable_name (found, 0),
6213 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6218 /* Check abstract method of interface INTERFACE */
6221 java_check_abstract_methods (interface_decl)
6222 tree interface_decl;
6225 tree method, basetype_vec, found;
6226 tree interface = TREE_TYPE (interface_decl);
6228 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
6230 tree method_wfl = DECL_NAME (method);
6232 /* 2- Check for double definition inside the defining interface */
6233 if (check_method_redefinition (interface, method))
6236 /* 3- Overriding is OK as far as we preserve the return type and
6237 the thrown exceptions (FIXME) */
6238 found = lookup_java_interface_method2 (interface, method);
6242 tree saved_found_wfl = DECL_NAME (found);
6243 reset_method_name (found);
6244 t = xstrdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
6247 "Method `%s' was defined with return type `%s' in class `%s'",
6248 lang_printable_name (found, 0), t,
6250 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6252 DECL_NAME (found) = saved_found_wfl;
6257 /* 4- Inherited methods can't differ by their returned types */
6258 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
6260 n = TREE_VEC_LENGTH (basetype_vec);
6261 for (i = 0; i < n; i++)
6263 tree sub_interface_method, sub_interface;
6264 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
6267 sub_interface = BINFO_TYPE (vec_elt);
6268 for (sub_interface_method = TYPE_METHODS (sub_interface);
6269 sub_interface_method;
6270 sub_interface_method = TREE_CHAIN (sub_interface_method))
6272 found = lookup_java_interface_method2 (interface,
6273 sub_interface_method);
6274 if (found && (found != sub_interface_method))
6276 tree saved_found_wfl = DECL_NAME (found);
6277 reset_method_name (found);
6279 (lookup_cl (sub_interface_method),
6280 "Interface `%s' inherits method `%s' from interface `%s'. This method is redefined with a different return type in interface `%s'",
6281 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
6282 lang_printable_name (found, 0),
6284 (DECL_NAME (TYPE_NAME
6285 (DECL_CONTEXT (sub_interface_method)))),
6287 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
6288 DECL_NAME (found) = saved_found_wfl;
6294 /* Lookup methods in interfaces using their name and partial
6295 signature. Return a matching method only if their types differ. */
6298 lookup_java_interface_method2 (class, method_decl)
6299 tree class, method_decl;
6302 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
6307 n = TREE_VEC_LENGTH (basetype_vec);
6308 for (i = 0; i < n; i++)
6310 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
6311 if ((BINFO_TYPE (vec_elt) != object_type_node)
6313 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
6316 for (i = 0; i < n; i++)
6318 to_return = lookup_java_interface_method2
6319 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
6327 /* Lookup method using their name and partial signature. Return a
6328 matching method only if their types differ. */
6331 lookup_java_method2 (clas, method_decl, do_interface)
6332 tree clas, method_decl;
6335 tree method, method_signature, method_name, method_type, name;
6337 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
6338 name = DECL_NAME (method_decl);
6339 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6340 EXPR_WFL_NODE (name) : name);
6341 method_type = TREE_TYPE (TREE_TYPE (method_decl));
6343 while (clas != NULL_TREE)
6345 for (method = TYPE_METHODS (clas);
6346 method != NULL_TREE; method = TREE_CHAIN (method))
6348 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
6349 tree name = DECL_NAME (method);
6350 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
6351 EXPR_WFL_NODE (name) : name) == method_name
6352 && method_sig == method_signature
6353 && TREE_TYPE (TREE_TYPE (method)) != method_type)
6356 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
6361 /* Return the line that matches DECL line number, and try its best to
6362 position the column number. Used during error reports. */
6368 static tree cl = NULL_TREE;
6374 if (cl == NULL_TREE)
6375 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
6377 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
6378 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
6380 line = java_get_line_col (IDENTIFIER_POINTER (EXPR_WFL_FILENAME_NODE (cl)),
6381 EXPR_WFL_LINENO (cl), EXPR_WFL_COLNO (cl));
6383 found = strstr ((const char *)line,
6384 (const char *)IDENTIFIER_POINTER (DECL_NAME (decl)));
6386 EXPR_WFL_SET_LINECOL (cl, EXPR_WFL_LINENO (cl), found - line);
6391 /* Look for a simple name in the single-type import list */
6394 find_name_in_single_imports (name)
6399 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
6400 if (TREE_VALUE (node) == name)
6401 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
6406 /* Process all single-type import. */
6414 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6416 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
6418 /* Don't load twice something already defined. */
6419 if (IDENTIFIER_CLASS_VALUE (to_be_found))
6421 QUALIFIED_P (to_be_found) = 1;
6422 load_class (to_be_found, 0);
6424 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
6425 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
6427 parse_error_context (TREE_PURPOSE (import),
6428 "Class or interface `%s' not found in import",
6429 IDENTIFIER_POINTER (to_be_found));
6438 /* Possibly find and mark a class imported by a single-type import
6442 find_in_imports (class_type)
6447 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
6448 if (TREE_VALUE (import) == TYPE_NAME (class_type))
6450 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
6451 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6456 note_possible_classname (name, len)
6461 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
6463 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
6467 node = ident_subst (name, len, "", '/', '.', "");
6468 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
6469 QUALIFIED_P (node) = strchr (name, '/') ? 1 : 0;
6473 /* Read a import directory, gathering potential match for further type
6474 references. Indifferently reads a filesystem or a ZIP archive
6478 read_import_dir (wfl)
6481 tree package_id = EXPR_WFL_NODE (wfl);
6482 const char *package_name = IDENTIFIER_POINTER (package_id);
6483 int package_length = IDENTIFIER_LENGTH (package_id);
6485 JCF *saved_jcf = current_jcf;
6490 struct buffer filename[1];
6493 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
6495 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
6497 BUFFER_INIT (filename);
6498 buffer_grow (filename, package_length + 100);
6500 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
6502 const char *entry_name = jcf_path_name (entry);
6503 int entry_length = strlen (entry_name);
6504 if (jcf_path_is_zipfile (entry))
6507 buffer_grow (filename, entry_length);
6508 memcpy (filename->data, entry_name, entry_length - 1);
6509 filename->data[entry_length-1] = '\0';
6510 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
6512 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
6515 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
6516 BUFFER_RESET (filename);
6517 for (k = 0; k < package_length; k++)
6519 char ch = package_name[k];
6520 *filename->ptr++ = ch == '.' ? '/' : ch;
6522 *filename->ptr++ = '/';
6524 for (k = 0; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
6526 const char *current_entry = ZIPDIR_FILENAME (zipd);
6527 int current_entry_len = zipd->filename_length;
6529 if (current_entry_len >= BUFFER_LENGTH (filename)
6530 && strncmp (filename->data, current_entry,
6531 BUFFER_LENGTH (filename)) != 0)
6533 found |= note_possible_classname (current_entry,
6540 BUFFER_RESET (filename);
6541 buffer_grow (filename, entry_length + package_length + 4);
6542 strcpy (filename->data, entry_name);
6543 filename->ptr = filename->data + entry_length;
6544 for (k = 0; k < package_length; k++)
6546 char ch = package_name[k];
6547 *filename->ptr++ = ch == '.' ? '/' : ch;
6549 *filename->ptr = '\0';
6551 dirp = opendir (filename->data);
6554 *filename->ptr++ = '/';
6559 struct dirent *direntp = readdir (dirp);
6562 d_name = direntp->d_name;
6563 len = strlen (direntp->d_name);
6564 buffer_grow (filename, len+1);
6565 strcpy (filename->ptr, d_name);
6566 found |= note_possible_classname (filename->data + entry_length,
6567 package_length+len+1);
6574 free (filename->data);
6576 /* Here we should have a unified way of retrieving an entry, to be
6580 static int first = 1;
6583 error ("Can't find default package `%s'. Check the CLASSPATH environment variable and the access to the archives.", package_name);
6588 parse_error_context (wfl, "Package `%s' not found in import",
6590 current_jcf = saved_jcf;
6593 current_jcf = saved_jcf;
6596 /* Possibly find a type in the import on demands specified
6597 types. Returns 1 if an error occured, 0 otherwise. Run throught the
6598 entire list, to detected potential double definitions. */
6601 find_in_imports_on_demand (class_type)
6604 tree node, import, node_to_use = NULL_TREE;
6606 tree cl = NULL_TREE;
6608 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
6610 const char *id_name;
6611 obstack_grow (&temporary_obstack,
6612 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
6613 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6614 obstack_1grow (&temporary_obstack, '.');
6615 obstack_grow0 (&temporary_obstack,
6616 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6617 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
6618 id_name = obstack_finish (&temporary_obstack);
6620 node = maybe_get_identifier (id_name);
6621 if (node && IS_A_CLASSFILE_NAME (node))
6625 cl = TREE_PURPOSE (import);
6633 (import, "Type `%s' also potentially defined in package `%s'",
6634 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
6635 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
6642 /* Setup lineno so that it refers to the line of the import (in
6643 case we parse a class file and encounter errors */
6645 int saved_lineno = lineno;
6646 lineno = EXPR_WFL_LINENO (cl);
6647 TYPE_NAME (class_type) = node_to_use;
6648 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
6649 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
6650 /* If there is no DECL set for the class or if the class isn't
6651 loaded and not seen in source yet, the load */
6652 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
6653 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
6654 load_class (node_to_use, 0);
6655 lineno = saved_lineno;
6656 return check_pkg_class_access (TYPE_NAME (class_type), cl);
6659 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
6662 /* Add package NAME to the list of package encountered so far. To
6663 speed up class lookup in do_resolve_class, we make sure a
6664 particular package is added only once. */
6667 register_package (name)
6670 static struct hash_table _pht, *pht = NULL;
6674 hash_table_init (&_pht, hash_newfunc,
6675 java_hash_hash_tree_node, java_hash_compare_tree_node);
6679 if (!hash_lookup (pht, (const hash_table_key) name, FALSE, NULL))
6681 package_list = chainon (package_list, build_tree_list (name, NULL));
6682 hash_lookup (pht, (const hash_table_key) name, TRUE, NULL);
6687 resolve_package (pkg, next)
6691 tree type_name = NULL_TREE;
6692 const char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
6694 /* The trick is to determine when the package name stops and were
6695 the name of something contained in the package starts. Then we
6696 return a fully qualified name of what we want to get. */
6698 /* Do a quick search on well known package names */
6699 if (!strncmp (name, "java.lang.reflect", 17))
6702 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
6703 type_name = lookup_package_type (name, 17);
6705 else if (!strncmp (name, "java.lang", 9))
6707 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
6708 type_name = lookup_package_type (name, 9);
6711 /* If we found something here, return */
6715 *next = EXPR_WFL_QUALIFICATION (pkg);
6717 /* Try the current package. */
6718 if (ctxp->package && !strncmp (name, IDENTIFIER_POINTER (ctxp->package),
6719 IDENTIFIER_LENGTH (ctxp->package)))
6722 lookup_package_type_and_set_next (name,
6723 IDENTIFIER_LENGTH (ctxp->package),
6729 /* Search in imported package */
6730 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
6732 tree current_pkg_name = EXPR_WFL_NODE (TREE_PURPOSE (current));
6733 int len = IDENTIFIER_LENGTH (current_pkg_name);
6734 if (!strncmp (name, IDENTIFIER_POINTER (current_pkg_name), len))
6738 breakdown_qualified (&left, &dummy, current_pkg_name);
6739 len = IDENTIFIER_LENGTH (left);
6740 type_name = lookup_package_type_and_set_next (name, len, next);
6746 /* Try to progressively construct a type name */
6747 if (TREE_CODE (pkg) == EXPR_WITH_FILE_LOCATION)
6748 for (acc = NULL_TREE, current = EXPR_WFL_QUALIFICATION (pkg);
6749 current; current = TREE_CHAIN (current))
6751 acc = merge_qualified_name (acc, EXPR_WFL_NODE (QUAL_WFL (current)));
6752 if ((type_name = resolve_no_layout (acc, NULL_TREE)))
6755 /* resolve_package should be used in a loop, hence we
6756 point at this one to naturally process the next one at
6757 the next iteration. */
6766 lookup_package_type_and_set_next (name, len, next)
6772 tree type_name = lookup_package_type (name, len);
6777 ptr = IDENTIFIER_POINTER (type_name);
6778 while (ptr && (ptr = strchr (ptr, '.')))
6780 *next = TREE_CHAIN (*next);
6787 lookup_package_type (name, from)
6792 const char *sub = &name[from+1];
6793 while (*sub != '.' && *sub)
6795 strncpy (subname, name, sub-name);
6796 subname [sub-name] = '\0';
6797 return get_identifier (subname);
6801 check_inner_class_access (decl, enclosing_decl, cl)
6802 tree decl, enclosing_decl, cl;
6806 /* We don't issue an error message when CL is null. CL can be null
6807 as a result of processing a JDEP crafted by source_start_java_method
6808 for the purpose of patching its parm decl. But the error would
6809 have been already trapped when fixing the method's signature.
6810 DECL can also be NULL in case of earlier errors. */
6814 /* We grant access to private and protected inner classes if the
6815 location from where we're trying to access DECL is an enclosing
6816 context for DECL or if both have a common enclosing context. */
6817 if (CLASS_PRIVATE (decl))
6819 if (CLASS_PROTECTED (decl))
6824 if (common_enclosing_context_p (TREE_TYPE (enclosing_decl),
6826 || enclosing_context_p (TREE_TYPE (enclosing_decl),
6830 parse_error_context (cl, "Can't access %s nested %s %s. Only public classes and interfaces in other packages can be accessed",
6831 (access == 1 ? "private" : "protected"),
6832 (CLASS_INTERFACE (decl) ? "interface" : "class"),
6833 lang_printable_name (decl, 0));
6836 /* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
6837 access violations were found, 1 otherwise. */
6840 check_pkg_class_access (class_name, cl)
6846 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
6849 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
6852 if (!CLASS_PUBLIC (TYPE_NAME (type)))
6854 /* Access to a private class within the same package is
6857 breakdown_qualified (&l, &r, class_name);
6858 if (l == ctxp->package)
6862 (cl, "Can't access %s `%s'. Only public classes and interfaces in other packages can be accessed",
6863 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
6864 IDENTIFIER_POINTER (class_name));
6870 /* Local variable declaration. */
6873 declare_local_variables (modifier, type, vlist)
6878 tree decl, current, saved_type;
6879 tree type_wfl = NULL_TREE;
6883 /* Push a new block if statements were seen between the last time we
6884 pushed a block and now. Keep a cound of block to close */
6885 if (BLOCK_EXPR_BODY (GET_CURRENT_BLOCK (current_function_decl)))
6887 tree body = GET_CURRENT_BLOCK (current_function_decl);
6888 tree b = enter_block ();
6889 BLOCK_EXPR_ORIGIN (b) = body;
6895 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
6896 if (modifier == ACC_FINAL)
6901 (ctxp->modifier_ctx [i],
6902 "Only `final' is allowed as a local variables modifier");
6907 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
6908 hold the TYPE value if a new incomplete has to be created (as
6909 opposed to being found already existing and reused). */
6910 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
6912 /* If TYPE is fully resolved and we don't have a reference, make one */
6913 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6915 /* Go through all the declared variables */
6916 for (current = vlist, saved_type = type; current;
6917 current = TREE_CHAIN (current), type = saved_type)
6919 tree other, real_type;
6920 tree wfl = TREE_PURPOSE (current);
6921 tree name = EXPR_WFL_NODE (wfl);
6922 tree init = TREE_VALUE (current);
6924 /* Process NAME, as it may specify extra dimension(s) for it */
6925 type = build_array_from_name (type, type_wfl, name, &name);
6927 /* Variable redefinition check */
6928 if ((other = lookup_name_in_blocks (name)))
6930 variable_redefinition_error (wfl, name, TREE_TYPE (other),
6931 DECL_SOURCE_LINE (other));
6935 /* Type adjustment. We may have just readjusted TYPE because
6936 the variable specified more dimensions. Make sure we have
6937 a reference if we can and don't have one already. */
6938 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
6940 real_type = GET_REAL_TYPE (type);
6941 /* Never layout this decl. This will be done when its scope
6943 decl = build_decl (VAR_DECL, name, real_type);
6944 LOCAL_FINAL (decl) = final_p;
6945 BLOCK_CHAIN_DECL (decl);
6947 /* If doing xreferencing, replace the line number with the WFL
6950 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINECOL (wfl);
6952 /* Don't try to use an INIT statement when an error was found */
6953 if (init && java_error_count)
6956 /* Add the initialization function to the current function's code */
6959 /* Name might have been readjusted */
6960 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
6961 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
6962 java_method_add_stmt (current_function_decl,
6963 build_debugable_stmt (EXPR_WFL_LINECOL (init),
6967 /* Setup dependency the type of the decl */
6971 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
6972 dep = CLASSD_LAST (ctxp->classd_list);
6973 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
6976 SOURCE_FRONTEND_DEBUG (("Defined locals"));
6979 /* Called during parsing. Build decls from argument list. */
6982 source_start_java_method (fndecl)
6992 current_function_decl = fndecl;
6994 /* New scope for the function */
6996 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
6997 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
6999 tree type = TREE_VALUE (tem);
7000 tree name = TREE_PURPOSE (tem);
7002 /* If type is incomplete. Create an incomplete decl and ask for
7003 the decl to be patched later */
7004 if (INCOMPLETE_TYPE_P (type))
7007 tree real_type = GET_REAL_TYPE (type);
7008 parm_decl = build_decl (PARM_DECL, name, real_type);
7009 type = obtain_incomplete_type (type);
7010 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
7011 jdep = CLASSD_LAST (ctxp->classd_list);
7012 JDEP_MISC (jdep) = name;
7013 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
7016 parm_decl = build_decl (PARM_DECL, name, type);
7018 /* Remember if a local variable was declared final (via its
7019 TREE_LIST of type/name.) Set LOCAL_FINAL accordingly. */
7020 if (ARG_FINAL_P (tem))
7021 LOCAL_FINAL (parm_decl) = 1;
7023 BLOCK_CHAIN_DECL (parm_decl);
7025 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7026 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
7028 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
7029 DECL_MAX_LOCALS (current_function_decl) = i;
7032 /* Called during parsing. Creates an artificial method declaration. */
7035 create_artificial_method (class, flags, type, name, args)
7038 tree type, name, args;
7042 java_parser_context_save_global ();
7044 mdecl = make_node (FUNCTION_TYPE);
7045 TREE_TYPE (mdecl) = type;
7046 TYPE_ARG_TYPES (mdecl) = args;
7047 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
7048 java_parser_context_restore_global ();
7049 DECL_ARTIFICIAL (mdecl) = 1;
7053 /* Starts the body if an artifical method. */
7056 start_artificial_method_body (mdecl)
7059 DECL_SOURCE_LINE (mdecl) = 1;
7060 DECL_SOURCE_LINE_MERGE (mdecl, 1);
7061 source_start_java_method (mdecl);
7066 end_artificial_method_body (mdecl)
7069 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
7073 /* Called during expansion. Push decls formerly built from argument
7074 list so they're usable during expansion. */
7077 expand_start_java_method (fndecl)
7082 current_function_decl = fndecl;
7085 fprintf (stderr, " [%s.", lang_printable_name (DECL_CONTEXT (fndecl), 0));
7086 announce_function (fndecl);
7088 fprintf (stderr, "]");
7090 pushlevel (1); /* Prepare for a parameter push */
7091 ptr = &DECL_ARGUMENTS (fndecl);
7092 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
7095 tree next = TREE_CHAIN (tem);
7096 tree type = TREE_TYPE (tem);
7097 if (PROMOTE_PROTOTYPES
7098 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
7099 && INTEGRAL_TYPE_P (type))
7100 type = integer_type_node;
7101 DECL_ARG_TYPE (tem) = type;
7102 layout_decl (tem, 0);
7105 ptr = &TREE_CHAIN (tem);
7109 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7110 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
7113 /* Terminate a function and expand its body. */
7116 source_end_java_method ()
7118 tree fndecl = current_function_decl;
7119 int flag_asynchronous_exceptions = asynchronous_exceptions;
7124 java_parser_context_save_global ();
7125 lineno = ctxp->last_ccb_indent1;
7127 /* Set EH language codes */
7128 java_set_exception_lang_code ();
7130 /* Turn function bodies with only a NOP expr null, so they don't get
7131 generated at all and we won't get warnings when using the -W
7133 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) == empty_stmt_node)
7134 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = NULL_TREE;
7136 /* Generate function's code */
7137 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
7138 && ! flag_emit_class_files
7139 && ! flag_emit_xref)
7140 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
7142 /* pop out of its parameters */
7143 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
7145 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
7147 /* Generate rtl for function exit. */
7148 if (! flag_emit_class_files && ! flag_emit_xref)
7150 lineno = DECL_SOURCE_LINE_LAST (fndecl);
7151 /* Emit catch-finally clauses */
7153 expand_function_end (input_filename, lineno, 0);
7155 /* FIXME: If the current method contains any exception handlers,
7156 force asynchronous_exceptions: this is necessary because signal
7157 handlers in libjava may throw exceptions. This is far from being
7158 a perfect solution, but it's better than doing nothing at all.*/
7160 asynchronous_exceptions = 1;
7162 /* Run the optimizers and output assembler code for this function. */
7163 rest_of_compilation (fndecl);
7166 current_function_decl = NULL_TREE;
7167 permanent_allocation (1);
7168 java_parser_context_restore_global ();
7169 asynchronous_exceptions = flag_asynchronous_exceptions;
7172 /* Record EXPR in the current function block. Complements compound
7173 expression second operand if necessary. */
7176 java_method_add_stmt (fndecl, expr)
7179 if (!GET_CURRENT_BLOCK (fndecl))
7181 return add_stmt_to_block (GET_CURRENT_BLOCK (fndecl), NULL_TREE, expr);
7185 add_stmt_to_block (b, type, stmt)
7188 tree body = BLOCK_EXPR_BODY (b), c;
7190 if (java_error_count)
7193 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
7196 BLOCK_EXPR_BODY (b) = c;
7197 TREE_SIDE_EFFECTS (c) = 1;
7201 /* Add STMT to EXISTING if possible, otherwise create a new
7202 COMPOUND_EXPR and add STMT to it. */
7205 add_stmt_to_compound (existing, type, stmt)
7206 tree existing, type, stmt;
7209 return build (COMPOUND_EXPR, type, existing, stmt);
7214 /* Hold THIS for the scope of the current public method decl. */
7215 static tree current_this;
7217 void java_layout_seen_class_methods ()
7219 tree previous_list = all_class_list;
7220 tree end = NULL_TREE;
7225 for (current = previous_list;
7226 current != end; current = TREE_CHAIN (current))
7227 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
7229 if (previous_list != all_class_list)
7231 end = previous_list;
7232 previous_list = all_class_list;
7240 java_reorder_fields ()
7242 static tree stop_reordering = NULL_TREE;
7245 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7247 current_class = TREE_TYPE (TREE_VALUE (current));
7249 if (current_class == stop_reordering)
7252 /* Reverse the fields, but leave the dummy field in front.
7253 Fields are already ordered for Object and Class */
7254 if (TYPE_FIELDS (current_class) && current_class != object_type_node
7255 && current_class != class_type_node)
7257 /* If the dummy field is there, reverse the right fields and
7258 just layout the type for proper fields offset */
7259 if (!DECL_NAME (TYPE_FIELDS (current_class)))
7261 tree fields = TYPE_FIELDS (current_class);
7262 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
7263 TYPE_SIZE (current_class) = NULL_TREE;
7265 /* We don't have a dummy field, we need to layout the class,
7266 after having reversed the fields */
7269 TYPE_FIELDS (current_class) =
7270 nreverse (TYPE_FIELDS (current_class));
7271 TYPE_SIZE (current_class) = NULL_TREE;
7275 stop_reordering = TREE_TYPE (TREE_VALUE (ctxp->gclass_list));
7278 /* Layout the methods of all classes loaded in one way on an
7279 other. Check methods of source parsed classes. Then reorder the
7280 fields and layout the classes or the type of all source parsed
7284 java_layout_classes ()
7287 int save_error_count = java_error_count;
7289 /* Layout the methods of all classes seen so far */
7290 java_layout_seen_class_methods ();
7291 java_parse_abort_on_error ();
7292 all_class_list = NULL_TREE;
7294 /* Then check the methods of all parsed classes */
7295 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7296 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
7297 CHECK_METHODS (TREE_VALUE (current));
7298 java_parse_abort_on_error ();
7300 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
7302 current_class = TREE_TYPE (TREE_VALUE (current));
7303 layout_class (current_class);
7305 /* From now on, the class is considered completely loaded */
7306 CLASS_LOADED_P (current_class) = 1;
7308 /* Error reported by the caller */
7309 if (java_error_count)
7313 /* We might have reloaded classes durign the process of laying out
7314 classes for code generation. We must layout the methods of those
7315 late additions, as constructor checks might use them */
7316 java_layout_seen_class_methods ();
7317 java_parse_abort_on_error ();
7320 /* Expand methods in the current set of classes rememebered for
7324 java_complete_expand_classes ()
7328 do_not_fold = flag_emit_xref;
7330 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
7331 if (!INNER_CLASS_DECL_P (current))
7332 java_complete_expand_class (current);
7335 /* Expand the methods found in OUTER, starting first by OUTER's inner
7339 java_complete_expand_class (outer)
7344 set_nested_class_simple_name_value (outer, 1); /* Set */
7346 /* We need to go after all inner classes and start expanding them,
7347 starting with most nested ones. We have to do that because nested
7348 classes might add functions to outer classes */
7350 for (inner_list = DECL_INNER_CLASS_LIST (outer);
7351 inner_list; inner_list = TREE_CHAIN (inner_list))
7352 java_complete_expand_class (TREE_PURPOSE (inner_list));
7354 java_complete_expand_methods (outer);
7355 set_nested_class_simple_name_value (outer, 0); /* Reset */
7358 /* Expand methods registered in CLASS_DECL. The general idea is that
7359 we expand regular methods first. This allows us get an estimate on
7360 how outer context local alias fields are really used so we can add
7361 to the constructor just enough code to initialize them properly (it
7362 also lets us generate $finit$ correctly.) Then we expand the
7363 constructors and then <clinit>. */
7366 java_complete_expand_methods (class_decl)
7369 tree clinit, finit, decl, first_decl;
7371 current_class = TREE_TYPE (class_decl);
7373 /* Initialize a new constant pool */
7374 init_outgoing_cpool ();
7376 /* Pre-expand <clinit> to figure whether we really need it or
7377 not. If we do need it, we pre-expand the static fields so they're
7378 ready to be used somewhere else. <clinit> will be fully expanded
7379 after we processed the constructors. */
7380 first_decl = TYPE_METHODS (current_class);
7381 clinit = maybe_generate_pre_expand_clinit (current_class);
7383 /* Then generate $finit$ (if we need to) because constructor will
7385 if (TYPE_FINIT_STMT_LIST (current_class))
7387 finit = generate_finit (current_class);
7388 java_complete_expand_method (finit);
7391 /* Now do the constructors */
7392 for (decl = first_decl ; !java_error_count && decl; decl = TREE_CHAIN (decl))
7396 if (!DECL_CONSTRUCTOR_P (decl))
7399 no_body = !DECL_FUNCTION_BODY (decl);
7400 /* Don't generate debug info on line zero when expanding a
7401 generated constructor. */
7403 restore_line_number_status (1);
7405 java_complete_expand_method (decl);
7408 restore_line_number_status (0);
7411 /* First, do the ordinary methods. */
7412 for (decl = first_decl; decl; decl = TREE_CHAIN (decl))
7414 /* Skip abstract or native methods -- but do handle native
7415 methods when generating JNI stubs. */
7416 if (METHOD_ABSTRACT (decl)
7417 || (! flag_jni && METHOD_NATIVE (decl))
7418 || DECL_CONSTRUCTOR_P (decl) || DECL_CLINIT_P (decl))
7421 if (METHOD_NATIVE (decl))
7423 tree body = build_jni_stub (decl);
7424 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (decl)) = body;
7427 java_complete_expand_method (decl);
7430 /* If there is indeed a <clinit>, fully expand it now */
7433 /* Prevent the use of `this' inside <clinit> */
7434 ctxp->explicit_constructor_p = 1;
7435 java_complete_expand_method (clinit);
7436 ctxp->explicit_constructor_p = 0;
7439 /* We might have generated a class$ that we now want to expand */
7440 if (TYPE_DOT_CLASS (current_class))
7441 java_complete_expand_method (TYPE_DOT_CLASS (current_class));
7443 /* Now verify constructor circularity (stop after the first one we
7445 if (!CLASS_INTERFACE (class_decl))
7446 for (decl = TYPE_METHODS (current_class); decl; decl = TREE_CHAIN (decl))
7447 if (DECL_CONSTRUCTOR_P (decl)
7448 && verify_constructor_circularity (decl, decl))
7451 /* Save the constant pool. We'll need to restore it later. */
7452 TYPE_CPOOL (current_class) = outgoing_cpool;
7455 /* Hold a list of catch clauses list. The first element of this list is
7456 the list of the catch clauses of the currently analysed try block. */
7457 static tree currently_caught_type_list;
7459 /* Attempt to create <clinit>. Pre-expand static fields so they can be
7460 safely used in some other methods/constructors. */
7463 maybe_generate_pre_expand_clinit (class_type)
7466 tree current, mdecl;
7468 if (!TYPE_CLINIT_STMT_LIST (class_type))
7471 /* Go through all static fields and pre expand them */
7472 for (current = TYPE_FIELDS (class_type); current;
7473 current = TREE_CHAIN (current))
7474 if (FIELD_STATIC (current))
7475 build_field_ref (NULL_TREE, class_type, DECL_NAME (current));
7477 /* Then build the <clinit> method */
7478 mdecl = create_artificial_method (class_type, ACC_STATIC, void_type_node,
7479 clinit_identifier_node, end_params_node);
7480 layout_class_method (class_type, CLASSTYPE_SUPER (class_type),
7482 start_artificial_method_body (mdecl);
7484 /* We process the list of assignment we produced as the result of
7485 the declaration of initialized static field and add them as
7486 statement to the <clinit> method. */
7487 for (current = TYPE_CLINIT_STMT_LIST (class_type); current;
7488 current = TREE_CHAIN (current))
7490 tree stmt = current;
7491 /* We build the assignment expression that will initialize the
7492 field to its value. There are strict rules on static
7493 initializers (8.5). FIXME */
7494 if (TREE_CODE (stmt) != BLOCK && stmt != empty_stmt_node)
7495 stmt = build_debugable_stmt (EXPR_WFL_LINECOL (stmt), stmt);
7496 java_method_add_stmt (mdecl, stmt);
7499 end_artificial_method_body (mdecl);
7501 /* Now we want to place <clinit> as the last method (because we need
7502 it at least for interface so that it doesn't interfere with the
7503 dispatch table based lookup. */
7504 if (TREE_CHAIN (TYPE_METHODS (class_type)))
7506 current = TREE_CHAIN (TYPE_METHODS (class_type));
7507 TYPE_METHODS (class_type) = current;
7509 while (TREE_CHAIN (current))
7510 current = TREE_CHAIN (current);
7512 TREE_CHAIN (current) = mdecl;
7513 TREE_CHAIN (mdecl) = NULL_TREE;
7519 /* See whether we could get rid of <clinit>. Criteria are: all static
7520 final fields have constant initial values and the body of <clinit>
7521 is empty. Return 1 if <clinit> was discarded, 0 otherwise. */
7524 maybe_yank_clinit (mdecl)
7530 if (!DECL_CLINIT_P (mdecl))
7533 /* If the body isn't empty, then we keep <clinit> */
7534 fbody = DECL_FUNCTION_BODY (mdecl);
7535 if ((bbody = BLOCK_EXPR_BODY (fbody)))
7536 bbody = BLOCK_EXPR_BODY (bbody);
7537 if (bbody && bbody != empty_stmt_node)
7540 type = DECL_CONTEXT (mdecl);
7541 current = TYPE_FIELDS (type);
7543 for (current = (current ? TREE_CHAIN (current) : current);
7544 current; current = TREE_CHAIN (current))
7545 if (!(FIELD_STATIC (current) && FIELD_FINAL (current)
7546 && DECL_INITIAL (current) && TREE_CONSTANT (DECL_INITIAL (current))))
7552 /* Get rid of <clinit> in the class' list of methods */
7553 if (TYPE_METHODS (type) == mdecl)
7554 TYPE_METHODS (type) = TREE_CHAIN (mdecl);
7556 for (current = TYPE_METHODS (type); current;
7557 current = TREE_CHAIN (current))
7558 if (TREE_CHAIN (current) == mdecl)
7560 TREE_CHAIN (current) = TREE_CHAIN (mdecl);
7568 /* Complete and expand a method. */
7571 java_complete_expand_method (mdecl)
7574 int yank_clinit = 0;
7576 current_function_decl = mdecl;
7577 /* Fix constructors before expanding them */
7578 if (DECL_CONSTRUCTOR_P (mdecl))
7579 fix_constructors (mdecl);
7581 /* Expand functions that have a body */
7582 if (DECL_FUNCTION_BODY (mdecl))
7584 tree fbody = DECL_FUNCTION_BODY (mdecl);
7585 tree block_body = BLOCK_EXPR_BODY (fbody);
7586 tree exception_copy = NULL_TREE;
7587 expand_start_java_method (mdecl);
7588 build_result_decl (mdecl);
7591 = (!METHOD_STATIC (mdecl) ?
7592 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
7594 /* Purge the `throws' list of unchecked exceptions. If we're
7595 doing xref, save a copy of the list and re-install it
7598 exception_copy = copy_list (DECL_FUNCTION_THROWS (mdecl));
7600 purge_unchecked_exceptions (mdecl);
7602 /* Install exceptions thrown with `throws' */
7603 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
7605 if (block_body != NULL_TREE)
7607 block_body = java_complete_tree (block_body);
7609 if (! flag_emit_xref && ! METHOD_NATIVE (mdecl))
7610 check_for_initialization (block_body);
7611 ctxp->explicit_constructor_p = 0;
7614 BLOCK_EXPR_BODY (fbody) = block_body;
7616 /* If we saw a return but couldn't evaluate it properly, we'll
7617 have an error_mark_node here. */
7618 if (block_body != error_mark_node
7619 && (block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
7620 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE
7622 missing_return_error (current_function_decl);
7624 /* Check wether we could just get rid of clinit, now the picture
7626 if (!(yank_clinit = maybe_yank_clinit (mdecl)))
7627 complete_start_java_method (mdecl);
7629 /* Don't go any further if we've found error(s) during the
7631 if (!java_error_count && !yank_clinit)
7632 source_end_java_method ();
7635 if (java_error_count)
7636 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
7640 /* Pop the exceptions and sanity check */
7642 if (currently_caught_type_list)
7643 fatal ("Exception list non empty - java_complete_expand_method");
7646 DECL_FUNCTION_THROWS (mdecl) = exception_copy;
7652 /* This section of the code deals with accessing enclosing context
7653 fields either directly by using the relevant access to this$<n> or
7654 by invoking an access method crafted for that purpose. */
7656 /* Build the necessary access from an inner class to an outer
7657 class. This routine could be optimized to cache previous result
7658 (decl, current_class and returned access). When an access method
7659 needs to be generated, it always takes the form of a read. It might
7660 be later turned into a write by calling outer_field_access_fix. */
7663 build_outer_field_access (id, decl)
7666 tree access = NULL_TREE;
7667 tree ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
7669 /* If decl's class is the direct outer class of the current_class,
7670 build the access as `this$<n>.<field>'. Not that we will break
7671 the `private' barrier if we're not emitting bytecodes. */
7672 if (ctx == DECL_CONTEXT (decl)
7673 && (!FIELD_PRIVATE (decl) || !flag_emit_class_files ))
7675 tree thisn = build_current_thisn (current_class);
7676 access = make_qualified_primary (build_wfl_node (thisn),
7677 id, EXPR_WFL_LINECOL (id));
7679 /* Otherwise, generate access methods to outer this and access the
7680 field (either using an access method or by direct access.) */
7683 int lc = EXPR_WFL_LINECOL (id);
7685 /* Now we chain the required number of calls to the access$0 to
7686 get a hold to the enclosing instance we need, and the we
7687 build the field access. */
7688 access = build_access_to_thisn (ctx, DECL_CONTEXT (decl), lc);
7690 /* If the field is private and we're generating bytecode, then
7691 we generate an access method */
7692 if (FIELD_PRIVATE (decl) && flag_emit_class_files )
7694 tree name = build_outer_field_access_methods (decl);
7695 access = build_outer_field_access_expr (lc, DECL_CONTEXT (decl),
7696 name, access, NULL_TREE);
7698 /* Otherwise we use `access$(this$<j>). ... access$(this$<i>).<field>'.
7699 Once again we break the `private' access rule from a foreign
7702 access = make_qualified_primary (access, id, lc);
7704 return resolve_expression_name (access, NULL);
7707 /* Return a non zero value if NODE describes an outer field inner
7711 outer_field_access_p (type, decl)
7714 if (!INNER_CLASS_TYPE_P (type)
7715 || TREE_CODE (decl) != FIELD_DECL
7716 || DECL_CONTEXT (decl) == type)
7719 for (type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))); ;
7720 type = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))))
7722 if (type == DECL_CONTEXT (decl))
7724 if (!DECL_CONTEXT (TYPE_NAME (type)))
7731 /* Return a non zero value if NODE represents an outer field inner
7732 access that was been already expanded. As a side effect, it returns
7733 the name of the field being accessed and the argument passed to the
7734 access function, suitable for a regeneration of the access method
7735 call if necessary. */
7738 outer_field_expanded_access_p (node, name, arg_type, arg)
7739 tree node, *name, *arg_type, *arg;
7743 if (TREE_CODE (node) != CALL_EXPR)
7746 /* Well, gcj generates slightly different tree nodes when compiling
7747 to native or bytecodes. It's the case for function calls. */
7749 if (flag_emit_class_files
7750 && TREE_CODE (node) == CALL_EXPR
7751 && OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (TREE_OPERAND (node, 0))))
7753 else if (!flag_emit_class_files)
7755 node = TREE_OPERAND (node, 0);
7757 if (node && TREE_OPERAND (node, 0)
7758 && TREE_CODE (TREE_OPERAND (node, 0)) == ADDR_EXPR)
7760 node = TREE_OPERAND (node, 0);
7761 if (TREE_OPERAND (node, 0)
7762 && TREE_CODE (TREE_OPERAND (node, 0)) == FUNCTION_DECL
7763 && (OUTER_FIELD_ACCESS_IDENTIFIER_P
7764 (DECL_NAME (TREE_OPERAND (node, 0)))))
7769 if (identified && name && arg_type && arg)
7771 tree argument = TREE_OPERAND (node, 1);
7772 *name = DECL_NAME (TREE_OPERAND (node, 0));
7773 *arg_type = TREE_TYPE (TREE_TYPE (TREE_VALUE (argument)));
7774 *arg = TREE_VALUE (argument);
7779 /* Detect in NODE an outer field read access from an inner class and
7780 transform it into a write with RHS as an argument. This function is
7781 called from the java_complete_lhs when an assignment to a LHS can
7785 outer_field_access_fix (wfl, node, rhs)
7786 tree wfl, node, rhs;
7788 tree name, arg_type, arg;
7790 if (outer_field_expanded_access_p (node, &name, &arg_type, &arg))
7792 /* At any rate, check whether we're trying to assign a value to
7794 tree accessed = (JDECL_P (node) ? node :
7795 (TREE_CODE (node) == COMPONENT_REF ?
7796 TREE_OPERAND (node, 1) : node));
7797 if (check_final_assignment (accessed, wfl))
7798 return error_mark_node;
7800 node = build_outer_field_access_expr (EXPR_WFL_LINECOL (wfl),
7801 arg_type, name, arg, rhs);
7802 return java_complete_tree (node);
7807 /* Construct the expression that calls an access method:
7808 <type>.access$<n>(<arg1> [, <arg2>]);
7810 ARG2 can be NULL and will be omitted in that case. It will denote a
7814 build_outer_field_access_expr (lc, type, access_method_name, arg1, arg2)
7816 tree type, access_method_name, arg1, arg2;
7818 tree args, cn, access;
7820 args = arg1 ? arg1 :
7821 build_wfl_node (build_current_thisn (current_class));
7822 args = build_tree_list (NULL_TREE, args);
7825 args = tree_cons (NULL_TREE, arg2, args);
7827 access = build_method_invocation (build_wfl_node (access_method_name), args);
7828 cn = build_wfl_node (DECL_NAME (TYPE_NAME (type)));
7829 return make_qualified_primary (cn, access, lc);
7833 build_new_access_id ()
7835 static int access_n_counter = 1;
7838 sprintf (buffer, "access$%d", access_n_counter++);
7839 return get_identifier (buffer);
7842 /* Create the static access functions for the outer field DECL. We define a
7844 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$) {
7848 TREE_TYPE (<field>) access$<n> (DECL_CONTEXT (<field>) inst$,
7849 TREE_TYPE (<field>) value$) {
7850 return inst$.field = value$;
7852 We should have a usage flags on the DECL so we can lazily turn the ones
7853 we're using for code generation. FIXME.
7857 build_outer_field_access_methods (decl)
7860 tree id, args, stmt, mdecl;
7862 /* Check point, to be removed. FIXME */
7863 if (FIELD_INNER_ACCESS (decl)
7864 && TREE_CODE (FIELD_INNER_ACCESS (decl)) != IDENTIFIER_NODE)
7867 if (FIELD_INNER_ACCESS (decl))
7868 return FIELD_INNER_ACCESS (decl);
7870 push_obstacks (&permanent_obstack, &permanent_obstack);
7872 /* Create the identifier and a function named after it. */
7873 id = build_new_access_id ();
7875 /* The identifier is marked as bearing the name of a generated write
7876 access function for outer field accessed from inner classes. */
7877 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7879 /* Create the read access */
7880 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7881 TREE_CHAIN (args) = end_params_node;
7882 stmt = make_qualified_primary (build_wfl_node (inst_id),
7883 build_wfl_node (DECL_NAME (decl)), 0);
7884 stmt = build_return (0, stmt);
7885 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7886 TREE_TYPE (decl), id, args, stmt);
7887 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7889 /* Create the write access method */
7890 args = build_tree_list (inst_id, build_pointer_type (DECL_CONTEXT (decl)));
7891 TREE_CHAIN (args) = build_tree_list (wpv_id, TREE_TYPE (decl));
7892 TREE_CHAIN (TREE_CHAIN (args)) = end_params_node;
7893 stmt = make_qualified_primary (build_wfl_node (inst_id),
7894 build_wfl_node (DECL_NAME (decl)), 0);
7895 stmt = build_return (0, build_assignment (ASSIGN_TK, 0, stmt,
7896 build_wfl_node (wpv_id)));
7898 mdecl = build_outer_field_access_method (DECL_CONTEXT (decl),
7899 TREE_TYPE (decl), id, args, stmt);
7900 DECL_FUNCTION_ACCESS_DECL (mdecl) = decl;
7903 /* Return the access name */
7904 return FIELD_INNER_ACCESS (decl) = id;
7907 /* Build an field access method NAME. */
7910 build_outer_field_access_method (class, type, name, args, body)
7911 tree class, type, name, args, body;
7913 tree saved_current_function_decl, mdecl;
7915 /* Create the method */
7916 mdecl = create_artificial_method (class, ACC_STATIC, type, name, args);
7917 fix_method_argument_names (args, mdecl);
7918 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
7920 /* Attach the method body. */
7921 saved_current_function_decl = current_function_decl;
7922 start_artificial_method_body (mdecl);
7923 java_method_add_stmt (mdecl, body);
7924 end_artificial_method_body (mdecl);
7925 current_function_decl = saved_current_function_decl;
7931 /* This section deals with building access function necessary for
7932 certain kinds of method invocation from inner classes. */
7935 build_outer_method_access_method (decl)
7938 tree saved_current_function_decl, mdecl;
7939 tree args = NULL_TREE, call_args = NULL_TREE;
7940 tree carg, id, body, class;
7942 int parm_id_count = 0;
7944 /* Test this abort with an access to a private field */
7945 if (!strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "access$"))
7948 /* Check the cache first */
7949 if (DECL_FUNCTION_INNER_ACCESS (decl))
7950 return DECL_FUNCTION_INNER_ACCESS (decl);
7952 class = DECL_CONTEXT (decl);
7954 /* Obtain an access identifier and mark it */
7955 id = build_new_access_id ();
7956 OUTER_FIELD_ACCESS_IDENTIFIER_P (id) = 1;
7958 push_obstacks (&permanent_obstack, &permanent_obstack);
7960 carg = TYPE_ARG_TYPES (TREE_TYPE (decl));
7961 /* Create the arguments, as much as the original */
7962 for (; carg && carg != end_params_node;
7963 carg = TREE_CHAIN (carg))
7965 sprintf (buffer, "write_parm_value$%d", parm_id_count++);
7966 args = chainon (args, build_tree_list (get_identifier (buffer),
7967 TREE_VALUE (carg)));
7969 args = chainon (args, end_params_node);
7971 /* Create the method */
7972 mdecl = create_artificial_method (class, ACC_STATIC,
7973 TREE_TYPE (TREE_TYPE (decl)), id, args);
7974 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
7975 /* There is a potential bug here. We should be able to use
7976 fix_method_argument_names, but then arg names get mixed up and
7977 eventually a constructor will have its this$0 altered and the
7978 outer context won't be assignment properly. The test case is
7980 TYPE_ARG_TYPES (TREE_TYPE (mdecl)) = args;
7982 /* Attach the method body. */
7983 saved_current_function_decl = current_function_decl;
7984 start_artificial_method_body (mdecl);
7986 /* The actual method invocation uses the same args. When invoking a
7987 static methods that way, we don't want to skip the first
7990 if (!METHOD_STATIC (decl))
7991 carg = TREE_CHAIN (carg);
7992 for (; carg && carg != end_params_node; carg = TREE_CHAIN (carg))
7993 call_args = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (carg)),
7996 body = build_method_invocation (build_wfl_node (DECL_NAME (decl)),
7998 if (!METHOD_STATIC (decl))
7999 body = make_qualified_primary (build_wfl_node (TREE_PURPOSE (args)),
8001 if (TREE_TYPE (TREE_TYPE (decl)) != void_type_node)
8002 body = build_return (0, body);
8003 java_method_add_stmt (mdecl,body);
8004 end_artificial_method_body (mdecl);
8005 current_function_decl = saved_current_function_decl;
8008 /* Back tag the access function so it know what it accesses */
8009 DECL_FUNCTION_ACCESS_DECL (decl) = mdecl;
8011 /* Tag the current method so it knows it has an access generated */
8012 return DECL_FUNCTION_INNER_ACCESS (decl) = mdecl;
8016 /* This section of the code deals with building expressions to access
8017 the enclosing instance of an inner class. The enclosing instance is
8018 kept in a generated field called this$<n>, with <n> being the
8019 inner class nesting level (starting from 0.) */
8021 /* Build an access to a given this$<n>, possibly by chaining access
8022 call to others. Access methods to this$<n> are build on the fly if
8026 build_access_to_thisn (from, to, lc)
8030 tree access = NULL_TREE;
8034 tree access0_wfl, cn;
8036 maybe_build_thisn_access_method (from);
8037 access0_wfl = build_wfl_node (access0_identifier_node);
8038 cn = build_wfl_node (DECL_NAME (TYPE_NAME (from)));
8039 EXPR_WFL_LINECOL (access0_wfl) = lc;
8043 access = build_current_thisn (current_class);
8044 access = build_wfl_node (access);
8046 access = build_tree_list (NULL_TREE, access);
8047 access = build_method_invocation (access0_wfl, access);
8048 access = make_qualified_primary (cn, access, lc);
8050 from = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (from)));
8055 /* Build an access function to the this$<n> local to TYPE. NULL_TREE
8056 is returned if nothing needs to be generated. Otherwise, the method
8057 generated and a method decl is returned.
8059 NOTE: These generated methods should be declared in a class file
8060 attribute so that they can't be referred to directly. */
8063 maybe_build_thisn_access_method (type)
8066 tree mdecl, args, stmt, rtype;
8067 tree saved_current_function_decl;
8069 /* If TYPE is a top-level class, no access method is required.
8070 If there already is such an access method, bail out. */
8071 if (CLASS_ACCESS0_GENERATED_P (type) || !INNER_CLASS_TYPE_P (type))
8074 /* We generate the method. The method looks like:
8075 static <outer_of_type> access$0 (<type> inst$) { return inst$.this$<n>; }
8077 push_obstacks (&permanent_obstack, &permanent_obstack);
8078 args = build_tree_list (inst_id, build_pointer_type (type));
8079 TREE_CHAIN (args) = end_params_node;
8080 rtype = build_pointer_type (TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))));
8081 mdecl = create_artificial_method (type, ACC_STATIC, rtype,
8082 access0_identifier_node, args);
8083 fix_method_argument_names (args, mdecl);
8084 layout_class_method (type, NULL_TREE, mdecl, NULL_TREE);
8085 stmt = build_current_thisn (type);
8086 stmt = make_qualified_primary (build_wfl_node (inst_id),
8087 build_wfl_node (stmt), 0);
8088 stmt = build_return (0, stmt);
8090 saved_current_function_decl = current_function_decl;
8091 start_artificial_method_body (mdecl);
8092 java_method_add_stmt (mdecl, stmt);
8093 end_artificial_method_body (mdecl);
8094 current_function_decl = saved_current_function_decl;
8097 CLASS_ACCESS0_GENERATED_P (type) = 1;
8102 /* Craft an correctly numbered `this$<n>'string. this$0 is used for
8103 the first level of innerclassing. this$1 for the next one, etc...
8104 This function can be invoked with TYPE to NULL, available and then
8105 has to count the parser context. */
8108 build_current_thisn (type)
8111 static int saved_i = -1;
8112 static tree saved_thisn = NULL_TREE;
8120 static tree saved_type = NULL_TREE;
8121 static int saved_type_i = 0;
8123 if (type == saved_type)
8127 for (i = -1, decl = DECL_CONTEXT (TYPE_NAME (type));
8128 decl; decl = DECL_CONTEXT (decl), i++)
8136 i = list_length (GET_CPC_LIST ())-2;
8141 sprintf (buffer, "this$%d", i);
8143 saved_thisn = get_identifier (buffer);
8147 /* Return the assignement to the hidden enclosing context `this$<n>'
8148 by the second incoming parameter to the innerclass constructor. The
8149 form used is `this.this$<n> = this$<n>;'. */
8152 build_thisn_assign ()
8154 if (current_class && PURE_INNER_CLASS_TYPE_P (current_class))
8156 tree thisn = build_current_thisn (current_class);
8157 tree lhs = make_qualified_primary (build_wfl_node (this_identifier_node),
8158 build_wfl_node (thisn), 0);
8159 tree rhs = build_wfl_node (thisn);
8160 EXPR_WFL_SET_LINECOL (lhs, lineno, 0);
8161 return build_assignment (ASSIGN_TK, EXPR_WFL_LINECOL (lhs), lhs, rhs);
8167 /* Building the synthetic `class$' used to implement the `.class' 1.1
8168 extension for non primitive types. This method looks like:
8170 static Class class$(String type) throws NoClassDefFoundError
8172 try {return (java.lang.Class.forName (String));}
8173 catch (ClassNotFoundException e) {
8174 throw new NoClassDefFoundError(e.getMessage());}
8178 build_dot_class_method (class)
8181 #define BWF(S) build_wfl_node (get_identifier ((S)))
8182 #define MQN(X,Y) make_qualified_name ((X), (Y), 0)
8183 tree args, tmp, saved_current_function_decl, mdecl;
8184 tree stmt, throw_stmt, catch, catch_block, try_block;
8185 tree catch_clause_param;
8186 tree class_not_found_exception, no_class_def_found_error;
8188 static tree get_message_wfl, type_parm_wfl;
8190 if (!get_message_wfl)
8192 get_message_wfl = build_wfl_node (get_identifier ("getMessage"));
8193 type_parm_wfl = build_wfl_node (get_identifier ("type$"));
8196 /* Build the arguments */
8197 args = build_tree_list (get_identifier ("type$"),
8198 build_pointer_type (string_type_node));
8199 TREE_CHAIN (args) = end_params_node;
8201 /* Build the qualified name java.lang.Class.forName */
8202 tmp = MQN (MQN (MQN (BWF ("java"),
8203 BWF ("lang")), BWF ("Class")), BWF ("forName"));
8205 /* For things we have to catch and throw */
8206 class_not_found_exception =
8207 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
8208 no_class_def_found_error =
8209 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
8210 load_class (class_not_found_exception, 1);
8211 load_class (no_class_def_found_error, 1);
8213 /* Create the "class$" function */
8214 mdecl = create_artificial_method (class, ACC_STATIC,
8215 build_pointer_type (class_type_node),
8216 get_identifier ("class$"), args);
8217 DECL_FUNCTION_THROWS (mdecl) = build_tree_list (NULL_TREE,
8218 no_class_def_found_error);
8220 /* We start by building the try block. We need to build:
8221 return (java.lang.Class.forName (type)); */
8222 stmt = build_method_invocation (tmp,
8223 build_tree_list (NULL_TREE, type_parm_wfl));
8224 stmt = build_return (0, stmt);
8225 /* Put it in a block. That's the try block */
8226 try_block = build_expr_block (stmt, NULL_TREE);
8228 /* Now onto the catch block. We start by building the expression
8229 throwing a new exception:
8230 throw new NoClassDefFoundError (_.getMessage); */
8231 throw_stmt = make_qualified_name (build_wfl_node (wpv_id),
8232 get_message_wfl, 0);
8233 throw_stmt = build_method_invocation (throw_stmt, NULL_TREE);
8235 /* Build new NoClassDefFoundError (_.getMessage) */
8236 throw_stmt = build_new_invocation
8237 (build_wfl_node (get_identifier ("NoClassDefFoundError")),
8238 build_tree_list (build_pointer_type (string_type_node), throw_stmt));
8240 /* Build the throw, (it's too early to use BUILD_THROW) */
8241 throw_stmt = build1 (THROW_EXPR, NULL_TREE, throw_stmt);
8243 /* Build the catch block to encapsulate all this. We begin by
8244 building an decl for the catch clause parameter and link it to
8245 newly created block, the catch block. */
8246 catch_clause_param =
8247 build_decl (VAR_DECL, wpv_id,
8248 build_pointer_type (class_not_found_exception));
8249 catch_block = build_expr_block (NULL_TREE, catch_clause_param);
8251 /* We initialize the variable with the exception handler. */
8252 catch = build (MODIFY_EXPR, NULL_TREE, catch_clause_param,
8253 soft_exceptioninfo_call_node);
8254 add_stmt_to_block (catch_block, NULL_TREE, catch);
8256 /* We add the statement throwing the new exception */
8257 add_stmt_to_block (catch_block, NULL_TREE, throw_stmt);
8259 /* Build a catch expression for all this */
8260 catch_block = build1 (CATCH_EXPR, NULL_TREE, catch_block);
8262 /* Build the try/catch sequence */
8263 stmt = build_try_statement (0, try_block, catch_block);
8265 fix_method_argument_names (args, mdecl);
8266 layout_class_method (class, NULL_TREE, mdecl, NULL_TREE);
8267 saved_current_function_decl = current_function_decl;
8268 start_artificial_method_body (mdecl);
8269 java_method_add_stmt (mdecl, stmt);
8270 end_artificial_method_body (mdecl);
8271 current_function_decl = saved_current_function_decl;
8272 TYPE_DOT_CLASS (class) = mdecl;
8278 build_dot_class_method_invocation (name)
8281 tree s = make_node (STRING_CST);
8282 TREE_STRING_LENGTH (s) = IDENTIFIER_LENGTH (name);
8283 TREE_STRING_POINTER (s) = obstack_alloc (expression_obstack,
8284 TREE_STRING_LENGTH (s)+1);
8285 strcpy (TREE_STRING_POINTER (s), IDENTIFIER_POINTER (name));
8286 return build_method_invocation (build_wfl_node (get_identifier ("class$")),
8287 build_tree_list (NULL_TREE, s));
8290 /* This section of the code deals with constructor. */
8292 /* Craft a body for default constructor. Patch existing constructor
8293 bodies with call to super() and field initialization statements if
8297 fix_constructors (mdecl)
8300 tree body = DECL_FUNCTION_BODY (mdecl);
8301 tree thisn_assign, compound = NULL_TREE;
8302 tree class_type = DECL_CONTEXT (mdecl);
8306 /* It is an error for the compiler to generate a default
8307 constructor if the superclass doesn't have a constructor that
8308 takes no argument, or the same args for an anonymous class */
8309 if (verify_constructor_super (mdecl))
8311 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (class_type));
8312 tree save = DECL_NAME (mdecl);
8313 const char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
8314 DECL_NAME (mdecl) = DECL_NAME (sclass_decl);
8316 (lookup_cl (TYPE_NAME (class_type)),
8317 "No constructor matching `%s' found in class `%s'",
8318 lang_printable_name (mdecl, 0), n);
8319 DECL_NAME (mdecl) = save;
8322 /* The constructor body must be crafted by hand. It's the
8323 constructor we defined when we realize we didn't have the
8324 CLASSNAME() constructor */
8325 start_artificial_method_body (mdecl);
8327 /* We don't generate a super constructor invocation if we're
8328 compiling java.lang.Object. build_super_invocation takes care
8330 compound = java_method_add_stmt (mdecl, build_super_invocation (mdecl));
8332 /* Insert the instance initializer block right here, after the
8333 super invocation. */
8334 add_instance_initializer (mdecl);
8336 /* Insert an assignment to the this$<n> hidden field, if
8338 if ((thisn_assign = build_thisn_assign ()))
8339 java_method_add_stmt (mdecl, thisn_assign);
8341 end_artificial_method_body (mdecl);
8343 /* Search for an explicit constructor invocation */
8347 tree main_block = BLOCK_EXPR_BODY (body);
8350 switch (TREE_CODE (body))
8353 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
8357 case EXPR_WITH_FILE_LOCATION:
8358 body = TREE_OPERAND (body, 0);
8361 body = BLOCK_EXPR_BODY (body);
8367 /* The constructor is missing an invocation of super() */
8369 compound = add_stmt_to_compound (compound, NULL_TREE,
8370 build_super_invocation (mdecl));
8372 /* Insert the instance initializer block right here, after the
8373 super invocation. */
8374 add_instance_initializer (mdecl);
8376 /* Generate the assignment to this$<n>, if necessary */
8377 if ((thisn_assign = build_thisn_assign ()))
8378 compound = add_stmt_to_compound (compound, NULL_TREE, thisn_assign);
8380 /* Fix the constructor main block if we're adding extra stmts */
8383 compound = add_stmt_to_compound (compound, NULL_TREE,
8384 BLOCK_EXPR_BODY (main_block));
8385 BLOCK_EXPR_BODY (main_block) = compound;
8390 /* Browse constructors in the super class, searching for a constructor
8391 that doesn't take any argument. Return 0 if one is found, 1
8392 otherwise. If the current class is an anonymous inner class, look
8393 for something that has the same signature. */
8396 verify_constructor_super (mdecl)
8399 tree class = CLASSTYPE_SUPER (current_class);
8400 int super_inner = PURE_INNER_CLASS_TYPE_P (class);
8406 if (ANONYMOUS_CLASS_P (current_class))
8408 tree mdecl_arg_type;
8409 SKIP_THIS_AND_ARTIFICIAL_PARMS (mdecl_arg_type, mdecl);
8410 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8411 if (DECL_CONSTRUCTOR_P (sdecl))
8414 tree arg_type = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8416 arg_type = TREE_CHAIN (arg_type);
8417 for (m_arg_type = mdecl_arg_type;
8418 (arg_type != end_params_node
8419 && m_arg_type != end_params_node);
8420 arg_type = TREE_CHAIN (arg_type),
8421 m_arg_type = TREE_CHAIN (m_arg_type))
8422 if (TREE_VALUE (arg_type) != TREE_VALUE (m_arg_type))
8425 if (arg_type == end_params_node && m_arg_type == end_params_node)
8431 for (sdecl = TYPE_METHODS (class); sdecl; sdecl = TREE_CHAIN (sdecl))
8433 tree arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (sdecl)));
8435 arg = TREE_CHAIN (arg);
8436 if (DECL_CONSTRUCTOR_P (sdecl) && arg == end_params_node)
8443 /* Generate code for all context remembered for code generation. */
8446 java_expand_classes ()
8448 int save_error_count = 0;
8449 static struct parser_ctxt *saved_ctxp = NULL;
8451 java_parse_abort_on_error ();
8452 if (!(ctxp = ctxp_for_generation))
8454 java_layout_classes ();
8455 java_parse_abort_on_error ();
8457 saved_ctxp = ctxp_for_generation;
8458 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8460 ctxp = ctxp_for_generation;
8461 lang_init_source (2); /* Error msgs have method prototypes */
8462 java_complete_expand_classes (); /* Complete and expand classes */
8463 java_parse_abort_on_error ();
8466 /* Find anonymous classes and expand their constructor, now they
8468 for (ctxp_for_generation = saved_ctxp;
8469 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8472 ctxp = ctxp_for_generation;
8473 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8475 current_class = TREE_TYPE (current);
8476 if (ANONYMOUS_CLASS_P (current_class))
8479 for (d = TYPE_METHODS (current_class); d; d = TREE_CHAIN (d))
8481 if (DECL_CONSTRUCTOR_P (d))
8483 restore_line_number_status (1);
8484 reset_method_name (d);
8485 java_complete_expand_method (d);
8486 restore_line_number_status (0);
8487 break; /* We now there are no other ones */
8494 /* If we've found error at that stage, don't try to generate
8495 anything, unless we're emitting xrefs or checking the syntax only
8496 (but not using -fsyntax-only for the purpose of generating
8498 if (java_error_count && !flag_emit_xref
8499 && (!flag_syntax_only && !flag_emit_class_files))
8502 /* Now things are stable, go for generation of the class data. */
8503 for (ctxp_for_generation = saved_ctxp;
8504 ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
8507 ctxp = ctxp_for_generation;
8508 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
8510 current_class = TREE_TYPE (current);
8511 outgoing_cpool = TYPE_CPOOL (current_class);
8512 if (flag_emit_class_files)
8513 write_classfile (current_class);
8515 expand_xref (current_class);
8516 else if (! flag_syntax_only)
8522 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
8523 a tree list node containing RIGHT. Fore coming RIGHTs will be
8524 chained to this hook. LOCATION contains the location of the
8525 separating `.' operator. */
8528 make_qualified_primary (primary, right, location)
8529 tree primary, right;
8534 if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
8535 wfl = build_wfl_wrap (primary, location);
8539 /* If wfl wasn't qualified, we build a first anchor */
8540 if (!EXPR_WFL_QUALIFICATION (wfl))
8541 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (wfl, NULL_TREE);
8544 /* And chain them */
8545 EXPR_WFL_LINECOL (right) = location;
8546 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
8547 PRIMARY_P (wfl) = 1;
8551 /* Simple merge of two name separated by a `.' */
8554 merge_qualified_name (left, right)
8558 if (!left && !right)
8567 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
8568 IDENTIFIER_LENGTH (left));
8569 obstack_1grow (&temporary_obstack, '.');
8570 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
8571 IDENTIFIER_LENGTH (right));
8572 node = get_identifier (obstack_base (&temporary_obstack));
8573 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
8574 QUALIFIED_P (node) = 1;
8578 /* Merge the two parts of a qualified name into LEFT. Set the
8579 location information of the resulting node to LOCATION, usually
8580 inherited from the location information of the `.' operator. */
8583 make_qualified_name (left, right, location)
8587 #ifdef USE_COMPONENT_REF
8588 tree node = build (COMPONENT_REF, NULL_TREE, left, right);
8589 EXPR_WFL_LINECOL (node) = location;
8592 tree left_id = EXPR_WFL_NODE (left);
8593 tree right_id = EXPR_WFL_NODE (right);
8596 merge = merge_qualified_name (left_id, right_id);
8598 /* Left wasn't qualified and is now qualified */
8599 if (!QUALIFIED_P (left_id))
8601 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
8602 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
8603 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
8606 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
8607 EXPR_WFL_LINECOL (wfl) = location;
8608 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
8610 EXPR_WFL_NODE (left) = merge;
8615 /* Extract the last identifier component of the qualified in WFL. The
8616 last identifier is removed from the linked list */
8619 cut_identifier_in_qualified (wfl)
8623 tree previous = NULL_TREE;
8624 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
8625 if (!TREE_CHAIN (q))
8628 fatal ("Operating on a non qualified qualified WFL - cut_identifier_in_qualified");
8629 TREE_CHAIN (previous) = NULL_TREE;
8630 return TREE_PURPOSE (q);
8634 /* Resolve the expression name NAME. Return its decl. */
8637 resolve_expression_name (id, orig)
8641 tree name = EXPR_WFL_NODE (id);
8644 /* 6.5.5.1: Simple expression names */
8645 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
8647 /* 15.13.1: NAME can appear within the scope of a local variable
8649 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
8652 /* 15.13.1: NAME can appear within a class declaration */
8655 decl = lookup_field_wrapper (current_class, name);
8658 tree access = NULL_TREE;
8659 int fs = FIELD_STATIC (decl);
8661 /* If we're accessing an outer scope local alias, make
8662 sure we change the name of the field we're going to
8664 if (FIELD_LOCAL_ALIAS_USED (decl))
8665 name = DECL_NAME (decl);
8667 /* Instance variable (8.3.1.1) can't appear within
8668 static method, static initializer or initializer for
8669 a static variable. */
8670 if (!fs && METHOD_STATIC (current_function_decl))
8672 static_ref_err (id, name, current_class);
8673 return error_mark_node;
8675 /* Instance variables can't appear as an argument of
8676 an explicit constructor invocation */
8677 if (!fs && ctxp->explicit_constructor_p)
8680 (id, "Can't reference `%s' before the superclass constructor has been called", IDENTIFIER_POINTER (name));
8681 return error_mark_node;
8684 /* If we're processing an inner class and we're trying
8685 to access a field belonging to an outer class, build
8686 the access to the field */
8687 if (!fs && outer_field_access_p (current_class, decl))
8688 return build_outer_field_access (id, decl);
8690 /* Otherwise build what it takes to access the field */
8691 access = build_field_ref ((fs ? NULL_TREE : current_this),
8692 DECL_CONTEXT (decl), name);
8693 if (fs && !flag_emit_class_files && !flag_emit_xref)
8694 access = build_class_init (DECL_CONTEXT (access), access);
8695 /* We may be asked to save the real field access node */
8698 /* And we return what we got */
8701 /* Fall down to error report on undefined variable */
8704 /* 6.5.5.2 Qualified Expression Names */
8709 qualify_ambiguous_name (id);
8710 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
8711 /* 15.10.2: Accessing Superclass Members using super */
8712 return resolve_field_access (id, orig, NULL);
8715 /* We've got an error here */
8716 parse_error_context (id, "Undefined variable `%s'",
8717 IDENTIFIER_POINTER (name));
8719 return error_mark_node;
8723 static_ref_err (wfl, field_id, class_type)
8724 tree wfl, field_id, class_type;
8728 "Can't make a static reference to nonstatic variable `%s' in class `%s'",
8729 IDENTIFIER_POINTER (field_id),
8730 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))));
8733 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
8734 We return something suitable to generate the field access. We also
8735 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
8736 recipient's address can be null. */
8739 resolve_field_access (qual_wfl, field_decl, field_type)
8741 tree *field_decl, *field_type;
8745 tree decl, where_found, type_found;
8747 if (resolve_qualified_expression_name (qual_wfl, &decl,
8748 &where_found, &type_found))
8749 return error_mark_node;
8751 /* Resolve the LENGTH field of an array here */
8752 if (DECL_P (decl) && DECL_NAME (decl) == length_identifier_node
8753 && type_found && TYPE_ARRAY_P (type_found)
8754 && ! flag_emit_class_files && ! flag_emit_xref)
8756 tree length = build_java_array_length_access (where_found);
8758 build_java_arraynull_check (type_found, length, int_type_node);
8760 /* In case we're dealing with a static array, we need to
8761 initialize its class before the array length can be fetched.
8762 It's also a good time to create a DECL_RTL for the field if
8763 none already exists, otherwise if the field was declared in a
8764 class found in an external file and hasn't been (and won't
8765 be) accessed for its value, none will be created. */
8766 if (TREE_CODE (where_found) == VAR_DECL && FIELD_STATIC (where_found))
8768 build_static_field_ref (where_found);
8769 field_ref = build_class_init (DECL_CONTEXT (where_found), field_ref);
8772 /* We might have been trying to resolve field.method(). In which
8773 case, the resolution is over and decl is the answer */
8774 else if (JDECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
8776 else if (JDECL_P (decl))
8778 int static_final_found = 0;
8780 type_found = DECL_CONTEXT (decl);
8781 is_static = JDECL_P (decl) && FIELD_STATIC (decl);
8782 if (FIELD_FINAL (decl) && FIELD_STATIC (decl)
8783 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
8784 && DECL_INITIAL (decl))
8786 /* When called on a FIELD_DECL of the right (primitive)
8787 type, java_complete_tree will try to substitue the decl
8788 for it's initial value. */
8789 field_ref = java_complete_tree (decl);
8790 static_final_found = 1;
8793 field_ref = build_field_ref ((is_static && !flag_emit_xref?
8794 NULL_TREE : where_found),
8795 type_found, DECL_NAME (decl));
8796 if (field_ref == error_mark_node)
8797 return error_mark_node;
8798 if (is_static && !static_final_found
8799 && !flag_emit_class_files && !flag_emit_xref)
8800 field_ref = build_class_init (DECL_CONTEXT (decl), field_ref);
8808 *field_type = (QUAL_DECL_TYPE (decl) ?
8809 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
8813 /* If NODE is an access to f static field, strip out the class
8814 initialization part and return the field decl, otherwise, return
8818 strip_out_static_field_access_decl (node)
8821 if (TREE_CODE (node) == COMPOUND_EXPR)
8823 tree op1 = TREE_OPERAND (node, 1);
8824 if (TREE_CODE (op1) == COMPOUND_EXPR)
8826 tree call = TREE_OPERAND (op1, 0);
8827 if (TREE_CODE (call) == CALL_EXPR
8828 && TREE_CODE (TREE_OPERAND (call, 0)) == ADDR_EXPR
8829 && TREE_OPERAND (TREE_OPERAND (call, 0), 0)
8830 == soft_initclass_node)
8831 return TREE_OPERAND (op1, 1);
8833 else if (JDECL_P (op1))
8839 /* 6.5.5.2: Qualified Expression Names */
8842 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
8844 tree *found_decl, *type_found, *where_found;
8846 int from_type = 0; /* Field search initiated from a type */
8847 int from_super = 0, from_cast = 0, from_qualified_this = 0;
8848 int previous_call_static = 0;
8850 tree decl = NULL_TREE, type = NULL_TREE, q;
8851 /* For certain for of inner class instantiation */
8852 tree saved_current, saved_this;
8853 #define RESTORE_THIS_AND_CURRENT_CLASS \
8854 { current_class = saved_current; current_this = saved_this;}
8856 *type_found = *where_found = NULL_TREE;
8858 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
8860 tree qual_wfl = QUAL_WFL (q);
8861 tree ret_decl; /* for EH checking */
8862 int location; /* for EH checking */
8864 /* 15.10.1 Field Access Using a Primary */
8865 switch (TREE_CODE (qual_wfl))
8868 case NEW_CLASS_EXPR:
8869 /* If the access to the function call is a non static field,
8870 build the code to access it. */
8871 if (JDECL_P (decl) && !FIELD_STATIC (decl))
8873 decl = maybe_access_field (decl, *where_found,
8874 DECL_CONTEXT (decl));
8875 if (decl == error_mark_node)
8879 /* And code for the function call */
8880 if (complete_function_arguments (qual_wfl))
8883 /* We might have to setup a new current class and a new this
8884 for the search of an inner class, relative to the type of
8885 a expression resolved as `decl'. The current values are
8886 saved and restored shortly after */
8887 saved_current = current_class;
8888 saved_this = current_this;
8889 if (decl && TREE_CODE (qual_wfl) == NEW_CLASS_EXPR)
8891 current_class = type;
8892 current_this = decl;
8895 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
8896 CALL_USING_SUPER (qual_wfl) = 1;
8897 location = (TREE_CODE (qual_wfl) == CALL_EXPR ?
8898 EXPR_WFL_LINECOL (TREE_OPERAND (qual_wfl, 0)) : 0);
8899 *where_found = patch_method_invocation (qual_wfl, decl, type,
8900 &is_static, &ret_decl);
8901 if (*where_found == error_mark_node)
8903 RESTORE_THIS_AND_CURRENT_CLASS;
8906 *type_found = type = QUAL_DECL_TYPE (*where_found);
8908 /* If we're creating an inner class instance, check for that
8909 an enclosing instance is in scope */
8910 if (TREE_CODE (qual_wfl) == NEW_CLASS_EXPR
8911 && INNER_ENCLOSING_SCOPE_CHECK (type))
8914 (qual_wfl, "No enclosing instance for inner class `%s' is in scope%s",
8915 lang_printable_name (type, 0),
8916 (!current_this ? "" :
8917 "; an explicit one must be provided when creating this inner class"));
8918 RESTORE_THIS_AND_CURRENT_CLASS;
8922 /* In case we had to change then to resolve a inner class
8923 instantiation using a primary qualified by a `new' */
8924 RESTORE_THIS_AND_CURRENT_CLASS;
8928 check_thrown_exceptions (location, ret_decl);
8930 /* If the previous call was static and this one is too,
8931 build a compound expression to hold the two (because in
8932 that case, previous function calls aren't transported as
8933 forcoming function's argument. */
8934 if (previous_call_static && is_static)
8936 decl = build (COMPOUND_EXPR, type, decl, *where_found);
8937 TREE_SIDE_EFFECTS (decl) = 1;
8941 previous_call_static = is_static;
8942 decl = *where_found;
8947 case NEW_ARRAY_EXPR:
8948 case NEW_ANONYMOUS_ARRAY_EXPR:
8949 *where_found = decl = java_complete_tree (qual_wfl);
8950 if (decl == error_mark_node)
8952 *type_found = type = QUAL_DECL_TYPE (decl);
8953 CLASS_LOADED_P (type) = 1;
8957 *where_found = decl = java_complete_tree (qual_wfl);
8958 if (decl == error_mark_node)
8960 *type_found = type = QUAL_DECL_TYPE (decl);
8964 case CONDITIONAL_EXPR:
8967 *where_found = decl = java_complete_tree (qual_wfl);
8968 if (decl == error_mark_node)
8970 *type_found = type = QUAL_DECL_TYPE (decl);
8974 /* If the access to the function call is a non static field,
8975 build the code to access it. */
8976 if (JDECL_P (decl) && !FIELD_STATIC (decl))
8978 decl = maybe_access_field (decl, *where_found, type);
8979 if (decl == error_mark_node)
8982 /* And code for the array reference expression */
8983 decl = java_complete_tree (qual_wfl);
8984 if (decl == error_mark_node)
8986 type = QUAL_DECL_TYPE (decl);
8990 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
8992 if ((type = patch_string (decl)))
8994 *where_found = QUAL_RESOLUTION (q) = decl;
8995 *type_found = type = TREE_TYPE (decl);
8999 if ((decl = java_complete_tree (qual_wfl)) == error_mark_node)
9001 *where_found = QUAL_RESOLUTION (q) = decl;
9002 *type_found = type = TREE_TYPE (decl);
9006 /* Fix for -Wall Just go to the next statement. Don't
9011 /* If we fall here, we weren't processing a (static) function call. */
9012 previous_call_static = 0;
9014 /* It can be the keyword THIS */
9015 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
9020 (wfl, "Keyword `this' used outside allowed context");
9023 if (ctxp->explicit_constructor_p)
9025 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9028 /* We have to generate code for intermediate acess */
9029 if (!from_type || TREE_TYPE (TREE_TYPE (current_this)) == type)
9031 *where_found = decl = current_this;
9032 *type_found = type = QUAL_DECL_TYPE (decl);
9034 /* We're trying to access the this from somewhere else. Make sure
9035 it's allowed before doing so. */
9038 if (!enclosing_context_p (type, current_class))
9040 char *p = xstrdup (lang_printable_name (type, 0));
9041 parse_error_context (qual_wfl, "Can't use variable `%s.this': type `%s' isn't an outer type of type `%s'",
9043 lang_printable_name (current_class, 0));
9047 *where_found = decl = build_current_thisn (type);
9048 from_qualified_this = 1;
9055 /* 15.10.2 Accessing Superclass Members using SUPER */
9056 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
9059 /* Check on the restricted use of SUPER */
9060 if (METHOD_STATIC (current_function_decl)
9061 || current_class == object_type_node)
9064 (wfl, "Keyword `super' used outside allowed context");
9067 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
9068 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
9069 CLASSTYPE_SUPER (current_class),
9070 build_this (EXPR_WFL_LINECOL (qual_wfl)));
9071 *where_found = decl = java_complete_tree (node);
9072 if (decl == error_mark_node)
9074 *type_found = type = QUAL_DECL_TYPE (decl);
9075 from_super = from_type = 1;
9079 /* 15.13.1: Can't search for field name in packages, so we
9080 assume a variable/class name was meant. */
9081 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
9083 tree name = resolve_package (wfl, &q);
9087 *where_found = decl = resolve_no_layout (name, qual_wfl);
9088 /* We want to be absolutely sure that the class is laid
9089 out. We're going to search something inside it. */
9090 *type_found = type = TREE_TYPE (decl);
9091 layout_class (type);
9094 /* Fix them all the way down, if any are left. */
9097 list = TREE_CHAIN (q);
9100 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (list)) = 1;
9101 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (list)) = 0;
9102 list = TREE_CHAIN (list);
9108 if (from_super || from_cast)
9110 ((from_cast ? qual_wfl : wfl),
9111 "No variable `%s' defined in class `%s'",
9112 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9113 lang_printable_name (type, 0));
9116 (qual_wfl, "Undefined variable or class name: `%s'",
9117 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
9122 /* We have a type name. It's been already resolved when the
9123 expression was qualified. */
9124 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
9126 if (!(decl = QUAL_RESOLUTION (q)))
9127 return 1; /* Error reported already */
9129 /* Sneak preview. If next we see a `new', we're facing a
9130 qualification with resulted in a type being selected
9131 instead of a field. Report the error */
9133 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR)
9135 parse_error_context (qual_wfl, "Undefined variable `%s'",
9136 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9140 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
9143 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
9144 java_accstring_lookup (get_access_flags_from_decl (decl)),
9145 GET_TYPE_NAME (type),
9146 IDENTIFIER_POINTER (DECL_NAME (decl)),
9147 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9150 check_deprecation (qual_wfl, decl);
9152 type = TREE_TYPE (decl);
9155 /* We resolve and expression name */
9158 tree field_decl = NULL_TREE;
9160 /* If there exists an early resolution, use it. That occurs
9161 only once and we know that there are more things to
9162 come. Don't do that when processing something after SUPER
9163 (we need more thing to be put in place below */
9164 if (!from_super && QUAL_RESOLUTION (q))
9166 decl = QUAL_RESOLUTION (q);
9169 if (TREE_CODE (decl) == FIELD_DECL && !FIELD_STATIC (decl))
9172 *where_found = current_this;
9175 static_ref_err (qual_wfl, DECL_NAME (decl),
9182 *where_found = TREE_TYPE (decl);
9183 if (TREE_CODE (*where_found) == POINTER_TYPE)
9184 *where_found = TREE_TYPE (*where_found);
9189 /* We have to search for a field, knowing the type of its
9190 container. The flag FROM_TYPE indicates that we resolved
9191 the last member of the expression as a type name, which
9192 means that for the resolution of this field, we'll look
9193 for other errors than if it was resolved as a member of
9198 tree field_decl_type; /* For layout */
9200 if (!from_type && !JREFERENCE_TYPE_P (type))
9203 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
9204 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9205 lang_printable_name (type, 0),
9206 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
9210 field_decl = lookup_field_wrapper (type,
9211 EXPR_WFL_NODE (qual_wfl));
9213 /* Maybe what we're trying to access an inner class. */
9216 tree ptr, inner_decl;
9218 BUILD_PTR_FROM_NAME (ptr, EXPR_WFL_NODE (qual_wfl));
9219 inner_decl = resolve_class (decl, ptr, NULL_TREE, qual_wfl);
9222 check_inner_class_access (inner_decl, decl, qual_wfl);
9223 type = TREE_TYPE (inner_decl);
9230 if (field_decl == NULL_TREE)
9233 (qual_wfl, "No variable `%s' defined in type `%s'",
9234 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
9235 GET_TYPE_NAME (type));
9238 if (field_decl == error_mark_node)
9241 /* Layout the type of field_decl, since we may need
9242 it. Don't do primitive types or loaded classes. The
9243 situation of non primitive arrays may not handled
9244 properly here. FIXME */
9245 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9246 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
9248 field_decl_type = TREE_TYPE (field_decl);
9249 if (!JPRIMITIVE_TYPE_P (field_decl_type)
9250 && !CLASS_LOADED_P (field_decl_type)
9251 && !TYPE_ARRAY_P (field_decl_type))
9252 resolve_and_layout (field_decl_type, NULL_TREE);
9253 if (TYPE_ARRAY_P (field_decl_type))
9254 CLASS_LOADED_P (field_decl_type) = 1;
9256 /* Check on accessibility here */
9257 if (not_accessible_p (type, field_decl, from_super))
9261 "Can't access %s field `%s.%s' from `%s'",
9262 java_accstring_lookup
9263 (get_access_flags_from_decl (field_decl)),
9264 GET_TYPE_NAME (type),
9265 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
9267 (DECL_NAME (TYPE_NAME (current_class))));
9270 check_deprecation (qual_wfl, field_decl);
9272 /* There are things to check when fields are accessed
9273 from type. There are no restrictions on a static
9274 declaration of the field when it is accessed from an
9276 is_static = FIELD_STATIC (field_decl);
9277 if (!from_super && from_type
9278 && !TYPE_INTERFACE_P (type)
9280 && (current_function_decl
9281 && METHOD_STATIC (current_function_decl)))
9283 static_ref_err (qual_wfl, EXPR_WFL_NODE (qual_wfl), type);
9286 from_cast = from_super = 0;
9288 /* It's an access from a type but it isn't static, we
9289 make it relative to `this'. */
9290 if (!is_static && from_type)
9291 decl = current_this;
9293 /* If we need to generate something to get a proper
9294 handle on what this field is accessed from, do it
9298 decl = maybe_access_field (decl, *where_found, *type_found);
9299 if (decl == error_mark_node)
9303 /* We want to keep the location were found it, and the type
9305 *where_found = decl;
9308 /* Generate the correct expression for field access from
9310 if (from_qualified_this)
9312 field_decl = build_outer_field_access (qual_wfl, field_decl);
9313 from_qualified_this = 0;
9316 /* This is the decl found and eventually the next one to
9321 type = QUAL_DECL_TYPE (decl);
9323 /* Sneak preview. If decl is qualified by a `new', report
9324 the error here to be accurate on the peculiar construct */
9326 && TREE_CODE (TREE_PURPOSE (TREE_CHAIN (q))) == NEW_CLASS_EXPR
9327 && !JREFERENCE_TYPE_P (type))
9329 parse_error_context (qual_wfl, "Attempt to reference field `new' in a `%s'",
9330 lang_printable_name (type, 0));
9334 /* `q' might have changed due to a after package resolution
9343 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
9344 can't be accessed from REFERENCE (a record type). This should be
9345 used when decl is a field or a method.*/
9348 not_accessible_p (reference, member, from_super)
9349 tree reference, member;
9352 int access_flag = get_access_flags_from_decl (member);
9354 /* Inner classes are processed by check_inner_class_access */
9355 if (INNER_CLASS_TYPE_P (reference))
9358 /* Access always granted for members declared public */
9359 if (access_flag & ACC_PUBLIC)
9362 /* Check access on protected members */
9363 if (access_flag & ACC_PROTECTED)
9365 /* Access granted if it occurs from within the package
9366 containing the class in which the protected member is
9368 if (class_in_current_package (DECL_CONTEXT (member)))
9371 /* If accessed with the form `super.member', then access is granted */
9375 /* Otherwise, access is granted if occuring from the class where
9376 member is declared or a subclass of it. Find the right
9377 context to perform the check */
9378 if (PURE_INNER_CLASS_TYPE_P (reference))
9380 while (INNER_CLASS_TYPE_P (reference))
9382 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9384 reference = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (reference)));
9387 if (inherits_from_p (reference, DECL_CONTEXT (member)))
9392 /* Check access on private members. Access is granted only if it
9393 occurs from within the class in which it is declared. Exceptions
9394 are accesses from inner-classes. */
9395 if (access_flag & ACC_PRIVATE)
9396 return (current_class == DECL_CONTEXT (member) ? 0 :
9397 (INNER_CLASS_TYPE_P (current_class) ? 0 : 1));
9399 /* Default access are permitted only when occuring within the
9400 package in which the type (REFERENCE) is declared. In other words,
9401 REFERENCE is defined in the current package */
9403 return !class_in_current_package (reference);
9405 /* Otherwise, access is granted */
9409 /* Test deprecated decl access. */
9411 check_deprecation (wfl, decl)
9414 const char *file = DECL_SOURCE_FILE (decl);
9415 /* Complain if the field is deprecated and the file it was defined
9416 in isn't compiled at the same time the file which contains its
9418 if (DECL_DEPRECATED (decl)
9419 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
9422 switch (TREE_CODE (decl))
9425 strcpy (the, "method");
9428 strcpy (the, "field");
9431 strcpy (the, "class");
9434 fatal ("unexpected DECL code - check_deprecation");
9436 parse_warning_context
9437 (wfl, "The %s `%s' in class `%s' has been deprecated",
9438 the, lang_printable_name (decl, 0),
9439 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
9443 /* Returns 1 if class was declared in the current package, 0 otherwise */
9446 class_in_current_package (class)
9449 static tree cache = NULL_TREE;
9456 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
9458 /* If the current package is empty and the name of CLASS is
9459 qualified, class isn't in the current package. If there is a
9460 current package and the name of the CLASS is not qualified, class
9461 isn't in the current package */
9462 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
9465 /* If there is not package and the name of CLASS isn't qualified,
9466 they belong to the same unnamed package */
9467 if (!ctxp->package && !qualified_flag)
9470 /* Compare the left part of the name of CLASS with the package name */
9471 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
9472 if (ctxp->package == left)
9480 /* This function may generate code to access DECL from WHERE. This is
9481 done only if certain conditions meet. */
9484 maybe_access_field (decl, where, type)
9485 tree decl, where, type;
9487 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
9488 && !FIELD_STATIC (decl))
9489 decl = build_field_ref (where ? where : current_this,
9490 (type ? type : DECL_CONTEXT (decl)),
9495 /* Build a method invocation, by patching PATCH. If non NULL
9496 and according to the situation, PRIMARY and WHERE may be
9497 used. IS_STATIC is set to 1 if the invoked function is static. */
9500 patch_method_invocation (patch, primary, where, is_static, ret_decl)
9501 tree patch, primary, where;
9505 tree wfl = TREE_OPERAND (patch, 0);
9506 tree args = TREE_OPERAND (patch, 1);
9507 tree name = EXPR_WFL_NODE (wfl);
9509 int is_static_flag = 0;
9510 int is_super_init = 0;
9511 tree this_arg = NULL_TREE;
9513 /* Should be overriden if everything goes well. Otherwise, if
9514 something fails, it should keep this value. It stop the
9515 evaluation of a bogus assignment. See java_complete_tree,
9516 MODIFY_EXPR: for the reasons why we sometimes want to keep on
9517 evaluating an assignment */
9518 TREE_TYPE (patch) = error_mark_node;
9520 /* Since lookup functions are messing with line numbers, save the
9522 java_parser_context_save_global ();
9524 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
9526 /* Resolution of qualified name, excluding constructors */
9527 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
9529 tree identifier, identifier_wfl, type, resolved;
9530 /* Extract the last IDENTIFIER of the qualified
9531 expression. This is a wfl and we will use it's location
9532 data during error report. */
9533 identifier_wfl = cut_identifier_in_qualified (wfl);
9534 identifier = EXPR_WFL_NODE (identifier_wfl);
9536 /* Given the context, IDENTIFIER is syntactically qualified
9537 as a MethodName. We need to qualify what's before */
9538 qualify_ambiguous_name (wfl);
9539 resolved = resolve_field_access (wfl, NULL, NULL);
9541 if (resolved == error_mark_node)
9542 PATCH_METHOD_RETURN_ERROR ();
9544 type = GET_SKIP_TYPE (resolved);
9545 resolve_and_layout (type, NULL_TREE);
9547 if (JPRIMITIVE_TYPE_P (type))
9551 "Can't invoke a method on primitive type `%s'",
9552 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9553 PATCH_METHOD_RETURN_ERROR ();
9556 list = lookup_method_invoke (0, identifier_wfl, type, identifier, args);
9557 args = nreverse (args);
9559 /* We're resolving a call from a type */
9560 if (TREE_CODE (resolved) == TYPE_DECL)
9562 if (CLASS_INTERFACE (resolved))
9566 "Can't make static reference to method `%s' in interface `%s'",
9567 IDENTIFIER_POINTER (identifier),
9568 IDENTIFIER_POINTER (name));
9569 PATCH_METHOD_RETURN_ERROR ();
9571 if (list && !METHOD_STATIC (list))
9573 char *fct_name = xstrdup (lang_printable_name (list, 0));
9576 "Can't make static reference to method `%s %s' in class `%s'",
9577 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9578 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
9580 PATCH_METHOD_RETURN_ERROR ();
9584 this_arg = primary = resolved;
9586 /* IDENTIFIER_WFL will be used to report any problem further */
9587 wfl = identifier_wfl;
9589 /* Resolution of simple names, names generated after a primary: or
9593 tree class_to_search = NULL_TREE;
9594 int lc; /* Looking for Constructor */
9596 /* We search constructor in their target class */
9597 if (CALL_CONSTRUCTOR_P (patch))
9599 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9600 class_to_search = EXPR_WFL_NODE (wfl);
9601 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9602 this_identifier_node)
9603 class_to_search = NULL_TREE;
9604 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
9605 super_identifier_node)
9608 if (CLASSTYPE_SUPER (current_class))
9610 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
9613 parse_error_context (wfl, "Can't invoke super constructor on java.lang.Object");
9614 PATCH_METHOD_RETURN_ERROR ();
9618 /* Class to search is NULL if we're searching the current one */
9619 if (class_to_search)
9621 class_to_search = resolve_and_layout (class_to_search, wfl);
9623 if (!class_to_search)
9626 (wfl, "Class `%s' not found in type declaration",
9627 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9628 PATCH_METHOD_RETURN_ERROR ();
9631 /* Can't instantiate an abstract class, but we can
9632 invoke it's constructor. It's use within the `new'
9633 context is denied here. */
9634 if (CLASS_ABSTRACT (class_to_search)
9635 && TREE_CODE (patch) == NEW_CLASS_EXPR)
9638 (wfl, "Class `%s' is an abstract class. It can't be instantiated",
9639 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
9640 PATCH_METHOD_RETURN_ERROR ();
9643 class_to_search = TREE_TYPE (class_to_search);
9646 class_to_search = current_class;
9649 /* This is a regular search in the local class, unless an
9650 alternate class is specified. */
9653 class_to_search = (where ? where : current_class);
9657 /* NAME is a simple identifier or comes from a primary. Search
9658 in the class whose declaration contain the method being
9660 resolve_and_layout (class_to_search, NULL_TREE);
9662 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
9663 /* Don't continue if no method were found, as the next statement
9664 can't be executed then. */
9666 PATCH_METHOD_RETURN_ERROR ();
9668 /* Check for static reference if non static methods */
9669 if (check_for_static_method_reference (wfl, patch, list,
9670 class_to_search, primary))
9671 PATCH_METHOD_RETURN_ERROR ();
9673 /* Check for inner classes creation from illegal contexts */
9674 if (lc && (INNER_CLASS_TYPE_P (class_to_search)
9675 && !CLASS_STATIC (TYPE_NAME (class_to_search)))
9676 && INNER_ENCLOSING_SCOPE_CHECK (class_to_search))
9679 (wfl, "No enclosing instance for inner class `%s' is in scope%s",
9680 lang_printable_name (class_to_search, 0),
9681 (!current_this ? "" :
9682 "; an explicit one must be provided when creating this inner class"));
9683 PATCH_METHOD_RETURN_ERROR ();
9686 /* Non static methods are called with the current object extra
9687 argument. If patch a `new TYPE()', the argument is the value
9688 returned by the object allocator. If method is resolved as a
9689 primary, use the primary otherwise use the current THIS. */
9690 args = nreverse (args);
9691 if (TREE_CODE (patch) != NEW_CLASS_EXPR)
9693 this_arg = primary ? primary : current_this;
9695 /* If we're using an access method, things are different.
9696 There are two familly of cases:
9698 1) We're not generating bytecodes:
9700 - LIST is non static. It's invocation is transformed from
9701 x(a1,...,an) into this$<n>.x(a1,....an).
9702 - LIST is static. It's invocation is transformed from
9703 x(a1,...,an) into TYPE_OF(this$<n>).x(a1,....an)
9705 2) We're generating bytecodes:
9707 - LIST is non static. It's invocation is transformed from
9708 x(a1,....,an) into access$<n>(this$<n>,a1,...,an).
9709 - LIST is static. It's invocation is transformed from
9710 x(a1,....,an) into TYPEOF(this$<n>).x(a1,....an).
9712 Of course, this$<n> can be abitrary complex, ranging from
9713 this$0 (the immediate outer context) to
9714 access$0(access$0(...(this$0))).
9716 maybe_use_access_method returns a non zero value if the
9717 this_arg has to be moved into the (then generated) stub
9718 argument list. In the meantime, the selected function
9719 might have be replaced by a generated stub. */
9720 if (maybe_use_access_method (is_super_init, &list, &this_arg))
9721 args = tree_cons (NULL_TREE, this_arg, args);
9725 /* Merge point of all resolution schemes. If we have nothing, this
9726 is an error, already signaled */
9728 PATCH_METHOD_RETURN_ERROR ();
9730 /* Check accessibility, position the is_static flag, build and
9732 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
9734 char *fct_name = xstrdup (lang_printable_name (list, 0));
9736 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
9737 java_accstring_lookup (get_access_flags_from_decl (list)),
9738 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
9739 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
9740 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
9742 PATCH_METHOD_RETURN_ERROR ();
9744 check_deprecation (wfl, list);
9746 /* If invoking a innerclass constructor, there are hidden parameters
9748 if (TREE_CODE (patch) == NEW_CLASS_EXPR
9749 && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9751 /* And make sure we add the accessed local variables to be saved
9752 in field aliases. */
9753 args = build_alias_initializer_parameter_list
9754 (AIPL_FUNCTION_CTOR_INVOCATION, DECL_CONTEXT (list), args, NULL);
9756 /* We have to reverse things. Find out why. FIXME */
9757 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (list)))
9758 args = nreverse (args);
9760 /* Secretely pass the current_this/primary as a second argument */
9761 if (primary || current_this)
9762 args = tree_cons (NULL_TREE, (primary ? primary : current_this), args);
9764 args = tree_cons (NULL_TREE, integer_zero_node, args);
9767 /* This handles the situation where a constructor invocation needs
9768 to have an enclosing context passed as a second parameter (the
9769 constructor is one of an inner class. We extract it from the
9770 current function. */
9771 if (is_super_init && PURE_INNER_CLASS_TYPE_P (DECL_CONTEXT (list)))
9773 tree enclosing_decl = DECL_CONTEXT (TYPE_NAME (current_class));
9776 if (ANONYMOUS_CLASS_P (current_class) || !DECL_CONTEXT (enclosing_decl))
9778 extra_arg = DECL_FUNCTION_BODY (current_function_decl);
9779 extra_arg = TREE_CHAIN (BLOCK_EXPR_DECLS (extra_arg));
9783 tree dest = TREE_TYPE (DECL_CONTEXT (enclosing_decl));
9785 build_access_to_thisn (TREE_TYPE (enclosing_decl), dest, 0);
9786 extra_arg = java_complete_tree (extra_arg);
9788 args = tree_cons (NULL_TREE, extra_arg, args);
9791 is_static_flag = METHOD_STATIC (list);
9792 if (! METHOD_STATIC (list) && this_arg != NULL_TREE)
9793 args = tree_cons (NULL_TREE, this_arg, args);
9795 /* In the context of an explicit constructor invocation, we can't
9796 invoke any method relying on `this'. Exceptions are: we're
9797 invoking a static function, primary exists and is not the current
9798 this, we're creating a new object. */
9799 if (ctxp->explicit_constructor_p
9801 && (!primary || primary == current_this)
9802 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
9804 parse_error_context (wfl, "Can't reference `this' before the superclass constructor has been called");
9805 PATCH_METHOD_RETURN_ERROR ();
9807 java_parser_context_restore_global ();
9809 *is_static = is_static_flag;
9810 /* Sometimes, we want the decl of the selected method. Such as for
9814 patch = patch_invoke (patch, list, args);
9815 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
9817 tree finit_parms, finit_call;
9819 /* Prepare to pass hidden parameters to $finit$, if any. */
9820 finit_parms = build_alias_initializer_parameter_list
9821 (AIPL_FUNCTION_FINIT_INVOCATION, current_class, NULL_TREE, NULL);
9824 build_method_invocation (build_wfl_node (finit_identifier_node),
9827 /* Generate the code used to initialize fields declared with an
9828 initialization statement and build a compound statement along
9829 with the super constructor invocation. */
9830 patch = build (COMPOUND_EXPR, void_type_node, patch,
9831 java_complete_tree (finit_call));
9832 CAN_COMPLETE_NORMALLY (patch) = 1;
9837 /* Check that we're not trying to do a static reference to a method in
9838 non static method. Return 1 if it's the case, 0 otherwise. */
9841 check_for_static_method_reference (wfl, node, method, where, primary)
9842 tree wfl, node, method, where, primary;
9844 if (METHOD_STATIC (current_function_decl)
9845 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
9847 char *fct_name = xstrdup (lang_printable_name (method, 0));
9849 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
9850 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
9851 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
9858 /* Fix the invocation of *MDECL if necessary in the case of a
9859 invocation from an inner class. *THIS_ARG might be modified
9860 appropriately and an alternative access to *MDECL might be
9864 maybe_use_access_method (is_super_init, mdecl, this_arg)
9866 tree *mdecl, *this_arg;
9869 tree md = *mdecl, ta = *this_arg;
9871 int non_static_context = !METHOD_STATIC (md);
9874 || DECL_CONTEXT (md) == current_class
9875 || !PURE_INNER_CLASS_TYPE_P (current_class)
9876 || DECL_FINIT_P (md))
9879 /* If we're calling a method found in an enclosing class, generate
9880 what it takes to retrieve the right this. Don't do that if we're
9881 invoking a static method. */
9883 if (non_static_context)
9885 ctx = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current_class)));
9886 if (inherits_from_p (ctx, DECL_CONTEXT (md)))
9888 ta = build_current_thisn (current_class);
9889 ta = build_wfl_node (ta);
9896 maybe_build_thisn_access_method (type);
9897 if (inherits_from_p (type, DECL_CONTEXT (md)))
9899 ta = build_access_to_thisn (ctx, type, 0);
9902 type = (DECL_CONTEXT (TYPE_NAME (type)) ?
9903 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type))) : NULL_TREE);
9906 ta = java_complete_tree (ta);
9909 /* We might have to use an access method to get to MD. We can
9910 break the method access rule as far as we're not generating
9912 if (METHOD_PRIVATE (md) && flag_emit_class_files)
9914 md = build_outer_method_access_method (md);
9921 /* Returnin a non zero value indicates we were doing a non static
9922 method invokation that is now a static invocation. It will have
9923 callee displace `this' to insert it in the regular argument
9925 return (non_static_context && to_return);
9928 /* Patch an invoke expression METHOD and ARGS, based on its invocation
9932 patch_invoke (patch, method, args)
9933 tree patch, method, args;
9936 tree original_call, t, ta;
9937 tree cond = NULL_TREE;
9939 /* Last step for args: convert build-in types. If we're dealing with
9940 a new TYPE() type call, the first argument to the constructor
9941 isn't found in the incoming argument list, but delivered by
9943 t = TYPE_ARG_TYPES (TREE_TYPE (method));
9944 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
9946 for (ta = args; t != end_params_node && ta;
9947 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
9948 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
9949 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
9950 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
9952 /* Resolve unresolved returned type isses */
9953 t = TREE_TYPE (TREE_TYPE (method));
9954 if (TREE_CODE (t) == POINTER_TYPE && !CLASS_LOADED_P (TREE_TYPE (t)))
9955 resolve_and_layout (TREE_TYPE (t), NULL);
9957 if (flag_emit_class_files || flag_emit_xref)
9961 tree signature = build_java_signature (TREE_TYPE (method));
9962 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
9964 case INVOKE_VIRTUAL:
9965 dtable = invoke_build_dtable (0, args);
9966 func = build_invokevirtual (dtable, method);
9969 case INVOKE_NONVIRTUAL:
9970 /* If the object for the method call is null, we throw an
9971 exception. We don't do this if the object is the current
9972 method's `this'. In other cases we just rely on an
9973 optimization pass to eliminate redundant checks. */
9974 if (TREE_VALUE (args) != current_this)
9976 /* We use a SAVE_EXPR here to make sure we only evaluate
9977 the new `self' expression once. */
9978 tree save_arg = save_expr (TREE_VALUE (args));
9979 TREE_VALUE (args) = save_arg;
9980 cond = build (EQ_EXPR, boolean_type_node, save_arg,
9987 func = build_known_method_ref (method, TREE_TYPE (method),
9988 DECL_CONTEXT (method),
9992 case INVOKE_INTERFACE:
9993 dtable = invoke_build_dtable (1, args);
9994 func = build_invokeinterface (dtable, method);
9998 fatal ("internal error - unknown invocation_mode result");
10001 /* Ensure self_type is initialized, (invokestatic). FIXME */
10002 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
10005 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
10006 TREE_OPERAND (patch, 0) = func;
10007 TREE_OPERAND (patch, 1) = args;
10008 original_call = patch;
10010 /* We're processing a `new TYPE ()' form. New is called and its
10011 returned value is the first argument to the constructor. We build
10012 a COMPOUND_EXPR and use saved expression so that the overall NEW
10013 expression value is a pointer to a newly created and initialized
10015 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
10017 tree class = DECL_CONTEXT (method);
10018 tree c1, saved_new, size, new;
10019 if (flag_emit_class_files || flag_emit_xref)
10021 TREE_TYPE (patch) = build_pointer_type (class);
10024 if (!TYPE_SIZE (class))
10025 safe_layout_class (class);
10026 size = size_in_bytes (class);
10027 new = build (CALL_EXPR, promote_type (class),
10028 build_address_of (alloc_object_node),
10029 tree_cons (NULL_TREE, build_class_ref (class),
10030 build_tree_list (NULL_TREE,
10031 size_in_bytes (class))),
10033 saved_new = save_expr (new);
10034 c1 = build_tree_list (NULL_TREE, saved_new);
10035 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
10036 TREE_OPERAND (original_call, 1) = c1;
10037 TREE_SET_CODE (original_call, CALL_EXPR);
10038 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
10041 /* If COND is set, then we are building a check to see if the object
10043 if (cond != NULL_TREE)
10045 /* We have to make the `then' branch a compound expression to
10046 make the types turn out right. This seems bizarre. */
10047 patch = build (COND_EXPR, TREE_TYPE (patch), cond,
10048 build (COMPOUND_EXPR, TREE_TYPE (patch),
10049 build (CALL_EXPR, void_type_node,
10050 build_address_of (soft_nullpointer_node),
10051 NULL_TREE, NULL_TREE),
10052 (FLOAT_TYPE_P (TREE_TYPE (patch))
10053 ? build_real (TREE_TYPE (patch), dconst0)
10054 : build1 (CONVERT_EXPR, TREE_TYPE (patch),
10055 integer_zero_node))),
10057 TREE_SIDE_EFFECTS (patch) = 1;
10064 invocation_mode (method, super)
10068 int access = get_access_flags_from_decl (method);
10071 return INVOKE_SUPER;
10073 if (access & ACC_STATIC)
10074 return INVOKE_STATIC;
10076 /* We have to look for a constructor before we handle nonvirtual
10077 calls; otherwise the constructor will look nonvirtual. */
10078 if (DECL_CONSTRUCTOR_P (method))
10079 return INVOKE_STATIC;
10081 if (access & ACC_FINAL || access & ACC_PRIVATE)
10082 return INVOKE_NONVIRTUAL;
10084 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
10085 return INVOKE_NONVIRTUAL;
10087 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
10088 return INVOKE_INTERFACE;
10090 return INVOKE_VIRTUAL;
10093 /* Retrieve a refined list of matching methods. It covers the step
10094 15.11.2 (Compile-Time Step 2) */
10097 lookup_method_invoke (lc, cl, class, name, arg_list)
10100 tree class, name, arg_list;
10102 tree atl = end_params_node; /* Arg Type List */
10103 tree method, signature, list, node;
10104 const char *candidates; /* Used for error report */
10107 /* Fix the arguments */
10108 for (node = arg_list; node; node = TREE_CHAIN (node))
10110 tree current_arg = TREE_TYPE (TREE_VALUE (node));
10111 /* Non primitive type may have to be resolved */
10112 if (!JPRIMITIVE_TYPE_P (current_arg))
10113 resolve_and_layout (current_arg, NULL_TREE);
10115 if (TREE_CODE (current_arg) == RECORD_TYPE)
10116 current_arg = promote_type (current_arg);
10117 atl = tree_cons (NULL_TREE, current_arg, atl);
10120 /* Presto. If we're dealing with an anonymous class and a
10121 constructor call, generate the right constructor now, since we
10122 know the arguments' types. */
10124 if (lc && ANONYMOUS_CLASS_P (class))
10125 craft_constructor (TYPE_NAME (class), atl);
10127 /* Find all candidates and then refine the list, searching for the
10128 most specific method. */
10129 list = find_applicable_accessible_methods_list (lc, class, name, atl);
10130 list = find_most_specific_methods_list (list);
10131 if (list && !TREE_CHAIN (list))
10132 return TREE_VALUE (list);
10134 /* Issue an error. List candidates if any. Candidates are listed
10135 only if accessible (non accessible methods may end-up here for
10136 the sake of a better error report). */
10141 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
10142 for (current = list; current; current = TREE_CHAIN (current))
10144 tree cm = TREE_VALUE (current);
10145 char string [4096];
10146 if (!cm || not_accessible_p (class, cm, 0))
10149 (string, " `%s' in `%s'%s",
10150 get_printable_method_name (cm),
10151 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
10152 (TREE_CHAIN (current) ? "\n" : ""));
10153 obstack_grow (&temporary_obstack, string, strlen (string));
10155 obstack_1grow (&temporary_obstack, '\0');
10156 candidates = obstack_finish (&temporary_obstack);
10158 /* Issue the error message */
10159 method = make_node (FUNCTION_TYPE);
10160 TYPE_ARG_TYPES (method) = atl;
10161 signature = build_java_argument_signature (method);
10162 dup = xstrdup (lang_printable_name (class, 0));
10163 parse_error_context (cl, "Can't find %s `%s(%s)' in type `%s'%s",
10164 (lc ? "constructor" : "method"),
10165 (lc ? dup : IDENTIFIER_POINTER (name)),
10166 IDENTIFIER_POINTER (signature), dup,
10167 (candidates ? candidates : ""));
10172 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
10173 when we're looking for a constructor. */
10176 find_applicable_accessible_methods_list (lc, class, name, arglist)
10178 tree class, name, arglist;
10180 static struct hash_table t, *searched_classes = NULL;
10181 static int search_not_done = 0;
10182 tree list = NULL_TREE, all_list = NULL_TREE;
10184 /* Check the hash table to determine if this class has been searched
10186 if (searched_classes)
10188 if (hash_lookup (searched_classes,
10189 (const hash_table_key) class, FALSE, NULL))
10194 hash_table_init (&t, hash_newfunc, java_hash_hash_tree_node,
10195 java_hash_compare_tree_node);
10196 searched_classes = &t;
10200 hash_lookup (searched_classes,
10201 (const hash_table_key) class, TRUE, NULL);
10203 if (!CLASS_LOADED_P (class) && !CLASS_FROM_SOURCE_P (class))
10205 load_class (class, 1);
10206 safe_layout_class (class);
10209 /* Search interfaces */
10210 if (TREE_CODE (TYPE_NAME (class)) == TYPE_DECL
10211 && CLASS_INTERFACE (TYPE_NAME (class)))
10214 tree basetype_vec = TYPE_BINFO_BASETYPES (class);
10215 search_applicable_methods_list (lc, TYPE_METHODS (class),
10216 name, arglist, &list, &all_list);
10217 n = TREE_VEC_LENGTH (basetype_vec);
10218 for (i = 1; i < n; i++)
10220 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10223 rlist = find_applicable_accessible_methods_list (lc, t, name,
10225 list = chainon (rlist, list);
10228 /* Search classes */
10232 int seen_inner_class = 0;
10233 search_applicable_methods_list (lc, TYPE_METHODS (class),
10234 name, arglist, &list, &all_list);
10236 /* We must search all interfaces of this class */
10239 tree basetype_vec = TYPE_BINFO_BASETYPES (sc);
10240 int n = TREE_VEC_LENGTH (basetype_vec), i;
10241 for (i = 1; i < n; i++)
10243 tree t = BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i));
10244 if (t != object_type_node)
10247 = find_applicable_accessible_methods_list (lc, t,
10249 list = chainon (rlist, list);
10254 /* Search enclosing context of inner classes before looking
10256 while (!lc && INNER_CLASS_TYPE_P (class))
10259 seen_inner_class = 1;
10260 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
10261 rlist = find_applicable_accessible_methods_list (lc, class,
10263 list = chainon (rlist, list);
10266 if (!lc && seen_inner_class
10267 && TREE_TYPE (DECL_CONTEXT (TYPE_NAME (sc))) == CLASSTYPE_SUPER (sc))
10268 class = CLASSTYPE_SUPER (sc);
10272 /* Search superclass */
10273 if (!lc && CLASSTYPE_SUPER (class) != NULL_TREE)
10276 class = CLASSTYPE_SUPER (class);
10277 rlist = find_applicable_accessible_methods_list (lc, class,
10279 list = chainon (rlist, list);
10285 /* We're done. Reset the searched classes list and finally search
10286 java.lang.Object if it wasn't searched already. */
10287 if (!search_not_done)
10290 && TYPE_METHODS (object_type_node)
10291 && !hash_lookup (searched_classes,
10292 (const hash_table_key) object_type_node,
10295 search_applicable_methods_list (lc,
10296 TYPE_METHODS (object_type_node),
10297 name, arglist, &list, &all_list);
10299 hash_table_free (searched_classes);
10300 searched_classes = NULL;
10303 /* Either return the list obtained or all selected (but
10304 inaccessible) methods for better error report. */
10305 return (!list ? all_list : list);
10308 /* Effectively search for the appropriate method in method */
10311 search_applicable_methods_list (lc, method, name, arglist, list, all_list)
10313 tree method, name, arglist;
10314 tree *list, *all_list;
10316 for (; method; method = TREE_CHAIN (method))
10318 /* When dealing with constructor, stop here, otherwise search
10320 if (lc && !DECL_CONSTRUCTOR_P (method))
10322 else if (!lc && (DECL_CONSTRUCTOR_P (method)
10323 || (GET_METHOD_NAME (method) != name)))
10326 if (argument_types_convertible (method, arglist))
10328 /* Retain accessible methods only */
10329 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
10331 *list = tree_cons (NULL_TREE, method, *list);
10333 /* Also retain all selected method here */
10334 *all_list = tree_cons (NULL_TREE, method, *list);
10339 /* 15.11.2.2 Choose the Most Specific Method */
10342 find_most_specific_methods_list (list)
10346 int abstract, candidates;
10347 tree current, new_list = NULL_TREE;
10348 for (current = list; current; current = TREE_CHAIN (current))
10351 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
10353 for (method = list; method; method = TREE_CHAIN (method))
10355 tree method_v, current_v;
10356 /* Don't test a method against itself */
10357 if (method == current)
10360 method_v = TREE_VALUE (method);
10361 current_v = TREE_VALUE (current);
10363 /* Compare arguments and location where methods where declared */
10364 if (argument_types_convertible (method_v, current_v))
10366 if (valid_method_invocation_conversion_p
10367 (DECL_CONTEXT (method_v), DECL_CONTEXT (current_v))
10368 || (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v))
10369 && enclosing_context_p (DECL_CONTEXT (method_v),
10370 DECL_CONTEXT (current_v))))
10372 int v = (DECL_SPECIFIC_COUNT (current_v) +=
10373 (INNER_CLASS_TYPE_P (DECL_CONTEXT (current_v)) ? 2 : 1));
10374 max = (v > max ? v : max);
10380 /* Review the list and select the maximally specific methods */
10381 for (current = list, abstract = -1, candidates = -1;
10382 current; current = TREE_CHAIN (current))
10383 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10385 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10386 abstract += (METHOD_ABSTRACT (TREE_VALUE (current)) ? 1 : 0);
10390 /* If we have several and they're all abstract, just pick the
10392 if (candidates > 0 && (candidates == abstract))
10394 new_list = nreverse (new_list);
10395 TREE_CHAIN (new_list) = NULL_TREE;
10398 /* We have several, we couldn't find a most specific, all but one are
10399 abstract, we pick the only non abstract one. */
10400 if (candidates > 0 && !max && (candidates == abstract+1))
10402 for (current = new_list; current; current = TREE_CHAIN (current))
10403 if (!METHOD_ABSTRACT (TREE_VALUE (current)))
10405 TREE_CHAIN (current) = NULL_TREE;
10406 new_list = current;
10410 /* If we can't find one, lower expectations and try to gather multiple
10411 maximally specific methods */
10412 while (!new_list && max)
10416 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
10417 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
10424 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
10425 converted by method invocation conversion (5.3) to the type of the
10426 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
10427 to change less often than M1. */
10430 argument_types_convertible (m1, m2_or_arglist)
10431 tree m1, m2_or_arglist;
10433 static tree m2_arg_value = NULL_TREE;
10434 static tree m2_arg_cache = NULL_TREE;
10436 register tree m1_arg, m2_arg;
10438 SKIP_THIS_AND_ARTIFICIAL_PARMS (m1_arg, m1)
10440 if (m2_arg_value == m2_or_arglist)
10441 m2_arg = m2_arg_cache;
10444 /* M2_OR_ARGLIST can be a function DECL or a raw list of
10446 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
10448 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
10449 if (!METHOD_STATIC (m2_or_arglist))
10450 m2_arg = TREE_CHAIN (m2_arg);
10453 m2_arg = m2_or_arglist;
10455 m2_arg_value = m2_or_arglist;
10456 m2_arg_cache = m2_arg;
10459 while (m1_arg != end_params_node && m2_arg != end_params_node)
10461 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
10462 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
10463 TREE_VALUE (m2_arg)))
10465 m1_arg = TREE_CHAIN (m1_arg);
10466 m2_arg = TREE_CHAIN (m2_arg);
10468 return m1_arg == end_params_node && m2_arg == end_params_node;
10471 /* Qualification routines */
10474 qualify_ambiguous_name (id)
10477 tree qual, qual_wfl, name = NULL_TREE, decl, ptr_type = NULL_TREE,
10478 saved_current_class;
10479 int again, super_found = 0, this_found = 0, new_array_found = 0;
10482 /* We first qualify the first element, then derive qualification of
10483 others based on the first one. If the first element is qualified
10484 by a resolution (field or type), this resolution is stored in the
10485 QUAL_RESOLUTION of the qual element being examined. We need to
10486 save the current_class since the use of SUPER might change the
10488 saved_current_class = current_class;
10489 qual = EXPR_WFL_QUALIFICATION (id);
10492 /* Simple qualified expression feature a qual_wfl that is a
10493 WFL. Expression derived from a primary feature more complicated
10494 things like a CALL_EXPR. Expression from primary need to be
10495 worked out to extract the part on which the qualification will
10497 qual_wfl = QUAL_WFL (qual);
10498 switch (TREE_CODE (qual_wfl))
10501 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10502 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
10504 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10505 qual_wfl = QUAL_WFL (qual);
10508 case NEW_ARRAY_EXPR:
10509 case NEW_ANONYMOUS_ARRAY_EXPR:
10510 qual = TREE_CHAIN (qual);
10511 again = new_array_found = 1;
10515 case NEW_CLASS_EXPR:
10516 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10519 while (TREE_CODE (qual_wfl) == ARRAY_REF)
10520 qual_wfl = TREE_OPERAND (qual_wfl, 0);
10523 qual = TREE_CHAIN (qual);
10524 qual_wfl = QUAL_WFL (qual);
10526 case CLASS_LITERAL:
10527 qual = TREE_CHAIN (qual);
10528 qual_wfl = QUAL_WFL (qual);
10531 /* Fix for -Wall. Just break doing nothing */
10535 ptr_type = current_class;
10537 code = TREE_CODE (qual_wfl);
10539 /* Pos evaluation: non WFL leading expression nodes */
10540 if (code == CONVERT_EXPR
10541 && TREE_CODE (TREE_TYPE (qual_wfl)) == EXPR_WITH_FILE_LOCATION)
10542 name = EXPR_WFL_NODE (TREE_TYPE (qual_wfl));
10544 else if (code == INTEGER_CST)
10547 else if ((code == ARRAY_REF || code == CALL_EXPR || code == MODIFY_EXPR) &&
10548 TREE_CODE (TREE_OPERAND (qual_wfl, 0)) == EXPR_WITH_FILE_LOCATION)
10549 name = EXPR_WFL_NODE (TREE_OPERAND (qual_wfl, 0));
10551 else if (code == TREE_LIST)
10552 name = EXPR_WFL_NODE (TREE_PURPOSE (qual_wfl));
10554 else if (code == STRING_CST || code == CONDITIONAL_EXPR
10555 || code == PLUS_EXPR)
10557 qual = TREE_CHAIN (qual);
10558 qual_wfl = QUAL_WFL (qual);
10563 name = EXPR_WFL_NODE (qual_wfl);
10566 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
10571 /* If we have a THIS (from a primary), we set the context accordingly */
10572 if (name == this_identifier_node)
10574 qual = TREE_CHAIN (qual);
10575 qual_wfl = QUAL_WFL (qual);
10576 if (TREE_CODE (qual_wfl) == CALL_EXPR)
10579 name = EXPR_WFL_NODE (qual_wfl);
10582 /* If we have a SUPER, we set the context accordingly */
10583 if (name == super_identifier_node)
10585 current_class = CLASSTYPE_SUPER (ptr_type);
10586 /* Check that there is such a thing as a super class. If not,
10587 return. The error will be caught later on, during the
10589 if (!current_class)
10591 current_class = saved_current_class;
10594 qual = TREE_CHAIN (qual);
10595 /* Do one more interation to set things up */
10596 super_found = again = 1;
10600 /* If name appears within the scope of a local variable declaration
10601 or parameter declaration, then it is an expression name. We don't
10602 carry this test out if we're in the context of the use of SUPER
10604 if (!this_found && !super_found
10605 && TREE_CODE (name) != STRING_CST && TREE_CODE (name) != INTEGER_CST
10606 && (decl = IDENTIFIER_LOCAL_VALUE (name)))
10608 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10609 QUAL_RESOLUTION (qual) = decl;
10612 /* If within the class/interface NAME was found to be used there
10613 exists a (possibly inherited) field named NAME, then this is an
10614 expression name. If we saw a NEW_ARRAY_EXPR before and want to
10615 address length, it is OK. */
10616 else if ((decl = lookup_field_wrapper (ptr_type, name))
10617 || (new_array_found && name == length_identifier_node))
10619 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10620 QUAL_RESOLUTION (qual) = (new_array_found ? NULL_TREE : decl);
10623 /* We reclassify NAME as yielding to a type name resolution if:
10624 - NAME is a class/interface declared within the compilation
10625 unit containing NAME,
10626 - NAME is imported via a single-type-import declaration,
10627 - NAME is declared in an another compilation unit of the package
10628 of the compilation unit containing NAME,
10629 - NAME is declared by exactly on type-import-on-demand declaration
10630 of the compilation unit containing NAME.
10631 - NAME is actually a STRING_CST. */
10632 else if (TREE_CODE (name) == STRING_CST || TREE_CODE (name) == INTEGER_CST
10633 || (decl = resolve_and_layout (name, NULL_TREE)))
10635 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
10636 QUAL_RESOLUTION (qual) = decl;
10639 /* Method call, array references and cast are expression name */
10640 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
10641 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF
10642 || TREE_CODE (QUAL_WFL (qual)) == CONVERT_EXPR)
10643 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
10645 /* Check here that NAME isn't declared by more than one
10646 type-import-on-demand declaration of the compilation unit
10647 containing NAME. FIXME */
10649 /* Otherwise, NAME is reclassified as a package name */
10651 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
10653 /* Propagate the qualification accross other components of the
10655 for (qual = TREE_CHAIN (qual); qual;
10656 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
10658 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10659 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
10661 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
10664 /* Store the global qualification for the ambiguous part of ID back
10666 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
10667 RESOLVE_EXPRESSION_NAME_P (id) = 1;
10668 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
10669 RESOLVE_TYPE_NAME_P (id) = 1;
10670 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
10671 RESOLVE_PACKAGE_NAME_P (id) = 1;
10673 /* Restore the current class */
10674 current_class = saved_current_class;
10678 breakdown_qualified (left, right, source)
10679 tree *left, *right, source;
10681 char *p = IDENTIFIER_POINTER (source), *base;
10682 int l = IDENTIFIER_LENGTH (source);
10684 /* Breakdown NAME into REMAINDER . IDENTIFIER */
10687 while (*p != '.' && p != base)
10690 /* We didn't find a '.'. Return an error */
10696 *right = get_identifier (p+1);
10697 *left = get_identifier (IDENTIFIER_POINTER (source));
10703 /* Patch tree nodes in a function body. When a BLOCK is found, push
10704 local variable decls if present.
10705 Same as java_complete_lhs, but does resolve static finals to values. */
10708 java_complete_tree (node)
10711 node = java_complete_lhs (node);
10712 if (JDECL_P (node) && FIELD_STATIC (node) && FIELD_FINAL (node)
10713 && DECL_INITIAL (node) != NULL_TREE
10714 && !flag_emit_xref)
10716 tree value = DECL_INITIAL (node);
10717 DECL_INITIAL (node) = NULL_TREE;
10718 push_obstacks (&permanent_obstack, &permanent_obstack);
10719 value = fold_constant_for_init (value, node);
10721 DECL_INITIAL (node) = value;
10722 if (value != NULL_TREE)
10724 /* fold_constant_for_init sometimes widen the original type
10725 of the constant (i.e. byte to int.) It's not desirable,
10726 especially if NODE is a function argument. */
10727 if (TREE_CODE (value) == INTEGER_CST
10728 && TREE_TYPE (node) != TREE_TYPE (value))
10729 return convert (TREE_TYPE (node), value);
10738 java_stabilize_reference (node)
10741 if (TREE_CODE (node) == COMPOUND_EXPR)
10743 tree op0 = TREE_OPERAND (node, 0);
10744 tree op1 = TREE_OPERAND (node, 1);
10745 TREE_OPERAND (node, 0) = save_expr (op0);
10746 TREE_OPERAND (node, 1) = java_stabilize_reference (op1);
10749 return stabilize_reference (node);
10752 /* Patch tree nodes in a function body. When a BLOCK is found, push
10753 local variable decls if present.
10754 Same as java_complete_tree, but does not resolve static finals to values. */
10757 java_complete_lhs (node)
10760 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
10763 /* CONVERT_EXPR always has its type set, even though it needs to be
10765 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
10768 /* The switch block implements cases processing container nodes
10769 first. Contained nodes are always written back. Leaves come
10770 next and return a value. */
10771 switch (TREE_CODE (node))
10775 /* 1- Block section.
10776 Set the local values on decl names so we can identify them
10777 faster when they're referenced. At that stage, identifiers
10778 are legal so we don't check for declaration errors. */
10779 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10781 DECL_CONTEXT (cn) = current_function_decl;
10782 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
10784 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
10785 CAN_COMPLETE_NORMALLY (node) = 1;
10788 tree stmt = BLOCK_EXPR_BODY (node);
10790 int error_seen = 0;
10791 if (TREE_CODE (stmt) == COMPOUND_EXPR)
10793 /* Re-order from (((A; B); C); ...; Z) to
10794 (A; (B; (C ; (...; Z)))).
10795 This makes it easier to scan the statements left-to-right
10796 without using recursion (which might overflow the stack
10797 if the block has many statements. */
10800 tree left = TREE_OPERAND (stmt, 0);
10801 if (TREE_CODE (left) != COMPOUND_EXPR)
10803 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
10804 TREE_OPERAND (left, 1) = stmt;
10807 BLOCK_EXPR_BODY (node) = stmt;
10810 /* Now do the actual complete, without deep recursion for
10812 ptr = &BLOCK_EXPR_BODY (node);
10813 while (TREE_CODE (*ptr) == COMPOUND_EXPR
10814 && TREE_OPERAND (*ptr, 1) != empty_stmt_node)
10816 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
10817 tree *next = &TREE_OPERAND (*ptr, 1);
10818 TREE_OPERAND (*ptr, 0) = cur;
10819 if (cur == empty_stmt_node)
10821 /* Optimization; makes it easier to detect empty bodies.
10822 Most useful for <clinit> with all-constant initializer. */
10826 if (TREE_CODE (cur) == ERROR_MARK)
10828 else if (! CAN_COMPLETE_NORMALLY (cur))
10833 if (TREE_CODE (wfl_op2) == BLOCK)
10834 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
10835 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
10836 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
10840 if (TREE_CODE (wfl_op2) != CASE_EXPR
10841 && TREE_CODE (wfl_op2) != DEFAULT_EXPR)
10842 unreachable_stmt_error (*ptr);
10846 *ptr = java_complete_tree (*ptr);
10848 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
10849 return error_mark_node;
10850 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
10852 /* Turn local bindings to null */
10853 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
10854 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
10856 TREE_TYPE (node) = void_type_node;
10859 /* 2- They are expressions but ultimately deal with statements */
10862 wfl_op1 = TREE_OPERAND (node, 0);
10863 COMPLETE_CHECK_OP_0 (node);
10864 /* 14.19 A throw statement cannot complete normally. */
10865 CAN_COMPLETE_NORMALLY (node) = 0;
10866 return patch_throw_statement (node, wfl_op1);
10868 case SYNCHRONIZED_EXPR:
10869 wfl_op1 = TREE_OPERAND (node, 0);
10870 return patch_synchronized_statement (node, wfl_op1);
10873 return patch_try_statement (node);
10875 case TRY_FINALLY_EXPR:
10876 COMPLETE_CHECK_OP_0 (node);
10877 COMPLETE_CHECK_OP_1 (node);
10878 CAN_COMPLETE_NORMALLY (node)
10879 = (CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0))
10880 && CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1)));
10881 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 0));
10884 case CLEANUP_POINT_EXPR:
10885 COMPLETE_CHECK_OP_0 (node);
10886 TREE_TYPE (node) = void_type_node;
10887 CAN_COMPLETE_NORMALLY (node) =
10888 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
10891 case WITH_CLEANUP_EXPR:
10892 COMPLETE_CHECK_OP_0 (node);
10893 COMPLETE_CHECK_OP_2 (node);
10894 CAN_COMPLETE_NORMALLY (node) =
10895 CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
10896 TREE_TYPE (node) = void_type_node;
10899 case LABELED_BLOCK_EXPR:
10900 PUSH_LABELED_BLOCK (node);
10901 if (LABELED_BLOCK_BODY (node))
10902 COMPLETE_CHECK_OP_1 (node);
10903 TREE_TYPE (node) = void_type_node;
10904 POP_LABELED_BLOCK ();
10906 if (LABELED_BLOCK_BODY (node) == empty_stmt_node)
10908 LABELED_BLOCK_BODY (node) = NULL_TREE;
10909 CAN_COMPLETE_NORMALLY (node) = 1;
10911 else if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
10912 CAN_COMPLETE_NORMALLY (node) = 1;
10915 case EXIT_BLOCK_EXPR:
10916 /* We don't complete operand 1, because it's the return value of
10917 the EXIT_BLOCK_EXPR which doesn't exist it Java */
10918 return patch_bc_statement (node);
10921 cn = java_complete_tree (TREE_OPERAND (node, 0));
10922 if (cn == error_mark_node)
10925 /* First, the case expression must be constant. Values of final
10926 fields are accepted. */
10928 if ((TREE_CODE (cn) == COMPOUND_EXPR || TREE_CODE (cn) == COMPONENT_REF)
10929 && JDECL_P (TREE_OPERAND (cn, 1))
10930 && FIELD_FINAL (TREE_OPERAND (cn, 1))
10931 && DECL_INITIAL (TREE_OPERAND (cn, 1)))
10933 push_obstacks (&permanent_obstack, &permanent_obstack);
10934 cn = fold_constant_for_init (DECL_INITIAL (TREE_OPERAND (cn, 1)),
10935 TREE_OPERAND (cn, 1));
10939 if (!TREE_CONSTANT (cn) && !flag_emit_xref)
10941 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10942 parse_error_context (node, "Constant expression required");
10943 return error_mark_node;
10946 nn = ctxp->current_loop;
10948 /* It must be assignable to the type of the switch expression. */
10949 if (!try_builtin_assignconv (NULL_TREE,
10950 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
10952 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10953 parse_error_context
10955 "Incompatible type for case. Can't convert `%s' to `int'",
10956 lang_printable_name (TREE_TYPE (cn), 0));
10957 return error_mark_node;
10960 cn = fold (convert (int_type_node, cn));
10962 /* Multiple instance of a case label bearing the same
10963 value is checked during code generation. The case
10964 expression is allright so far. */
10965 TREE_OPERAND (node, 0) = cn;
10966 TREE_TYPE (node) = void_type_node;
10967 CAN_COMPLETE_NORMALLY (node) = 1;
10968 TREE_SIDE_EFFECTS (node) = 1;
10972 nn = ctxp->current_loop;
10973 /* Only one default label is allowed per switch statement */
10974 if (SWITCH_HAS_DEFAULT (nn))
10976 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10977 parse_error_context (wfl_operator,
10978 "Duplicate case label: `default'");
10979 return error_mark_node;
10982 SWITCH_HAS_DEFAULT (nn) = 1;
10983 TREE_TYPE (node) = void_type_node;
10984 TREE_SIDE_EFFECTS (node) = 1;
10985 CAN_COMPLETE_NORMALLY (node) = 1;
10991 /* Check whether the loop was enclosed in a labeled
10992 statement. If not, create one, insert the loop in it and
10994 nn = patch_loop_statement (node);
10996 /* Anyways, walk the body of the loop */
10997 if (TREE_CODE (node) == LOOP_EXPR)
10998 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
10999 /* Switch statement: walk the switch expression and the cases */
11001 node = patch_switch_statement (node);
11003 if (node == error_mark_node || TREE_OPERAND (node, 0) == error_mark_node)
11004 nn = error_mark_node;
11007 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
11008 /* If we returned something different, that's because we
11009 inserted a label. Pop the label too. */
11012 if (CAN_COMPLETE_NORMALLY (node))
11013 CAN_COMPLETE_NORMALLY (nn) = 1;
11014 POP_LABELED_BLOCK ();
11021 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11022 return patch_exit_expr (node);
11026 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11027 if (TREE_OPERAND (node, 0) == error_mark_node)
11028 return error_mark_node;
11029 /* then-else branches */
11030 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11031 if (TREE_OPERAND (node, 1) == error_mark_node)
11032 return error_mark_node;
11033 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
11034 if (TREE_OPERAND (node, 2) == error_mark_node)
11035 return error_mark_node;
11036 return patch_if_else_statement (node);
11039 case CONDITIONAL_EXPR:
11041 wfl_op1 = TREE_OPERAND (node, 0);
11042 COMPLETE_CHECK_OP_0 (node);
11043 wfl_op2 = TREE_OPERAND (node, 1);
11044 COMPLETE_CHECK_OP_1 (node);
11045 wfl_op3 = TREE_OPERAND (node, 2);
11046 COMPLETE_CHECK_OP_2 (node);
11047 return patch_conditional_expr (node, wfl_op1, wfl_op2);
11049 /* 3- Expression section */
11050 case COMPOUND_EXPR:
11051 wfl_op2 = TREE_OPERAND (node, 1);
11052 TREE_OPERAND (node, 0) = nn =
11053 java_complete_tree (TREE_OPERAND (node, 0));
11054 if (wfl_op2 == empty_stmt_node)
11055 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (nn);
11058 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK)
11060 /* An unreachable condition in a do-while statement
11061 is *not* (technically) an unreachable statement. */
11063 if (TREE_CODE (nn) == EXPR_WITH_FILE_LOCATION)
11064 nn = EXPR_WFL_NODE (nn);
11065 if (TREE_CODE (nn) != EXIT_EXPR)
11067 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
11068 parse_error_context (wfl_operator, "Unreachable statement");
11071 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
11072 if (TREE_OPERAND (node, 1) == error_mark_node)
11073 return error_mark_node;
11074 CAN_COMPLETE_NORMALLY (node)
11075 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
11077 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
11081 /* CAN_COMPLETE_NORMALLY (node) = 0; */
11082 return patch_return (node);
11084 case EXPR_WITH_FILE_LOCATION:
11085 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11086 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11089 node = resolve_expression_name (node, NULL);
11090 if (node == error_mark_node)
11092 /* Keep line number information somewhere were it doesn't
11093 disrupt the completion process. */
11094 if (flag_emit_xref && TREE_CODE (node) != CALL_EXPR)
11096 EXPR_WFL_NODE (wfl) = TREE_OPERAND (node, 1);
11097 TREE_OPERAND (node, 1) = wfl;
11099 CAN_COMPLETE_NORMALLY (node) = 1;
11104 int save_lineno = lineno;
11105 lineno = EXPR_WFL_LINENO (node);
11106 body = java_complete_tree (EXPR_WFL_NODE (node));
11107 lineno = save_lineno;
11108 EXPR_WFL_NODE (node) = body;
11109 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (body);
11110 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
11111 if (body == empty_stmt_node)
11113 /* Optimization; makes it easier to detect empty bodies. */
11116 if (body == error_mark_node)
11118 /* Its important for the evaluation of assignment that
11119 this mark on the TREE_TYPE is propagated. */
11120 TREE_TYPE (node) = error_mark_node;
11121 return error_mark_node;
11124 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
11129 case NEW_ARRAY_EXPR:
11130 /* Patch all the dimensions */
11132 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11134 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
11135 tree dim = convert (int_type_node,
11136 java_complete_tree (TREE_VALUE (cn)));
11137 if (dim == error_mark_node)
11144 TREE_VALUE (cn) = dim;
11145 /* Setup the location of the current dimension, for
11146 later error report. */
11147 TREE_PURPOSE (cn) =
11148 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
11149 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
11152 /* They complete the array creation expression, if no errors
11154 CAN_COMPLETE_NORMALLY (node) = 1;
11155 return (flag ? error_mark_node
11156 : force_evaluation_order (patch_newarray (node)));
11158 case NEW_ANONYMOUS_ARRAY_EXPR:
11159 /* Create the array type if necessary. */
11160 if (ANONYMOUS_ARRAY_DIMS_SIG (node))
11162 tree type = ANONYMOUS_ARRAY_BASE_TYPE (node);
11163 if (!(type = resolve_type_during_patch (type)))
11164 return error_mark_node;
11165 type = build_array_from_name (type, NULL_TREE,
11166 ANONYMOUS_ARRAY_DIMS_SIG (node), NULL);
11167 ANONYMOUS_ARRAY_BASE_TYPE (node) = build_pointer_type (type);
11169 node = patch_new_array_init (ANONYMOUS_ARRAY_BASE_TYPE (node),
11170 ANONYMOUS_ARRAY_INITIALIZER (node));
11171 if (node == error_mark_node)
11172 return error_mark_node;
11173 CAN_COMPLETE_NORMALLY (node) = 1;
11176 case NEW_CLASS_EXPR:
11178 /* Complete function's argument(s) first */
11179 if (complete_function_arguments (node))
11180 return error_mark_node;
11183 tree decl, wfl = TREE_OPERAND (node, 0);
11184 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
11186 node = patch_method_invocation (node, NULL_TREE,
11187 NULL_TREE, 0, &decl);
11188 if (node == error_mark_node)
11189 return error_mark_node;
11191 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
11192 /* If we call this(...), register signature and positions */
11194 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
11195 tree_cons (wfl, decl,
11196 DECL_CONSTRUCTOR_CALLS (current_function_decl));
11197 CAN_COMPLETE_NORMALLY (node) = 1;
11198 return force_evaluation_order (node);
11202 /* Save potential wfls */
11203 wfl_op1 = TREE_OPERAND (node, 0);
11204 TREE_OPERAND (node, 0) = nn = java_complete_lhs (wfl_op1);
11206 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node)
11207 && TREE_CODE (nn) == VAR_DECL && TREE_STATIC (nn)
11208 && DECL_INITIAL (nn) != NULL_TREE)
11212 push_obstacks (&permanent_obstack, &permanent_obstack);
11213 value = fold_constant_for_init (nn, nn);
11216 if (value != NULL_TREE)
11218 tree type = TREE_TYPE (value);
11219 if (JPRIMITIVE_TYPE_P (type) ||
11220 (type == string_ptr_type_node && ! flag_emit_class_files))
11221 return empty_stmt_node;
11223 if (! flag_emit_class_files)
11224 DECL_INITIAL (nn) = NULL_TREE;
11226 wfl_op2 = TREE_OPERAND (node, 1);
11228 if (TREE_OPERAND (node, 0) == error_mark_node)
11229 return error_mark_node;
11231 flag = COMPOUND_ASSIGN_P (wfl_op2);
11234 /* This might break when accessing outer field from inner
11235 class. TESTME, FIXME */
11236 tree lvalue = java_stabilize_reference (TREE_OPERAND (node, 0));
11238 /* Hand stablize the lhs on both places */
11239 TREE_OPERAND (node, 0) = lvalue;
11240 TREE_OPERAND (TREE_OPERAND (node, 1), 0) =
11241 (flag_emit_class_files ? lvalue : save_expr (lvalue));
11243 /* 15.25.2.a: Left hand is not an array access. FIXME */
11244 /* Now complete the RHS. We write it back later on. */
11245 nn = java_complete_tree (TREE_OPERAND (node, 1));
11247 if ((cn = patch_string (nn)))
11250 /* The last part of the rewrite for E1 op= E2 is to have
11251 E1 = (T)(E1 op E2), with T being the type of E1. */
11252 nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2),
11253 TREE_TYPE (lvalue), nn));
11255 /* 15.25.2.b: Left hand is an array access. FIXME */
11258 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
11259 function to complete this RHS. Note that a NEW_ARRAY_INIT
11260 might have been already fully expanded if created as a result
11261 of processing an anonymous array initializer. We avoid doing
11262 the operation twice by testing whether the node already bears
11264 else if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT && !TREE_TYPE (wfl_op2))
11265 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
11266 TREE_OPERAND (node, 1));
11267 /* Otherwise we simply complete the RHS */
11269 nn = java_complete_tree (TREE_OPERAND (node, 1));
11271 if (nn == error_mark_node)
11272 return error_mark_node;
11274 /* Write back the RHS as we evaluated it. */
11275 TREE_OPERAND (node, 1) = nn;
11277 /* In case we're handling = with a String as a RHS, we need to
11278 produce a String out of the RHS (it might still be a
11279 STRING_CST or a StringBuffer at this stage */
11280 if ((nn = patch_string (TREE_OPERAND (node, 1))))
11281 TREE_OPERAND (node, 1) = nn;
11283 if ((nn = outer_field_access_fix (wfl_op1, TREE_OPERAND (node, 0),
11284 TREE_OPERAND (node, 1))))
11286 /* We return error_mark_node if outer_field_access_fix
11287 detects we write into a final. */
11288 if (nn == error_mark_node)
11289 return error_mark_node;
11294 node = patch_assignment (node, wfl_op1, wfl_op2);
11295 /* Reorganize the tree if necessary. */
11296 if (flag && (!JREFERENCE_TYPE_P (TREE_TYPE (node))
11297 || JSTRING_P (TREE_TYPE (node))))
11298 node = java_refold (node);
11301 CAN_COMPLETE_NORMALLY (node) = 1;
11313 case TRUNC_MOD_EXPR:
11314 case TRUNC_DIV_EXPR:
11316 case TRUTH_ANDIF_EXPR:
11317 case TRUTH_ORIF_EXPR:
11324 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
11325 knows how to handle those cases. */
11326 wfl_op1 = TREE_OPERAND (node, 0);
11327 wfl_op2 = TREE_OPERAND (node, 1);
11329 CAN_COMPLETE_NORMALLY (node) = 1;
11330 /* Don't complete string nodes if dealing with the PLUS operand. */
11331 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
11333 nn = java_complete_tree (wfl_op1);
11334 if (nn == error_mark_node)
11335 return error_mark_node;
11337 TREE_OPERAND (node, 0) = nn;
11339 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
11341 nn = java_complete_tree (wfl_op2);
11342 if (nn == error_mark_node)
11343 return error_mark_node;
11345 TREE_OPERAND (node, 1) = nn;
11347 return force_evaluation_order (patch_binop (node, wfl_op1, wfl_op2));
11349 case INSTANCEOF_EXPR:
11350 wfl_op1 = TREE_OPERAND (node, 0);
11351 COMPLETE_CHECK_OP_0 (node);
11352 if (flag_emit_xref)
11354 TREE_TYPE (node) = boolean_type_node;
11357 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
11359 case UNARY_PLUS_EXPR:
11361 case TRUTH_NOT_EXPR:
11363 case PREDECREMENT_EXPR:
11364 case PREINCREMENT_EXPR:
11365 case POSTDECREMENT_EXPR:
11366 case POSTINCREMENT_EXPR:
11368 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
11369 how to handle those cases. */
11370 wfl_op1 = TREE_OPERAND (node, 0);
11371 CAN_COMPLETE_NORMALLY (node) = 1;
11372 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11373 if (TREE_OPERAND (node, 0) == error_mark_node)
11374 return error_mark_node;
11375 node = patch_unaryop (node, wfl_op1);
11376 CAN_COMPLETE_NORMALLY (node) = 1;
11380 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
11381 how to handle those cases. */
11382 wfl_op1 = TREE_OPERAND (node, 0);
11383 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
11384 if (TREE_OPERAND (node, 0) == error_mark_node)
11385 return error_mark_node;
11386 if (!flag_emit_class_files && !flag_emit_xref)
11387 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
11388 /* The same applies to wfl_op2 */
11389 wfl_op2 = TREE_OPERAND (node, 1);
11390 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
11391 if (TREE_OPERAND (node, 1) == error_mark_node)
11392 return error_mark_node;
11393 if (!flag_emit_class_files && !flag_emit_xref)
11394 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
11395 return patch_array_ref (node);
11400 case COMPONENT_REF:
11401 /* The first step in the re-write of qualified name handling. FIXME.
11402 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
11403 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
11404 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
11406 tree name = TREE_OPERAND (node, 1);
11407 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
11408 if (field == NULL_TREE)
11410 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
11411 return error_mark_node;
11413 if (! FIELD_STATIC (field))
11415 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
11416 return error_mark_node;
11421 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
11425 /* Can't use THIS in a static environment */
11428 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11429 parse_error_context (wfl_operator,
11430 "Keyword `this' used outside allowed context");
11431 TREE_TYPE (node) = error_mark_node;
11432 return error_mark_node;
11434 if (ctxp->explicit_constructor_p)
11436 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11437 parse_error_context
11438 (wfl_operator, "Can't reference `this' or `super' before the superclass constructor has been called");
11439 TREE_TYPE (node) = error_mark_node;
11440 return error_mark_node;
11442 return current_this;
11444 case CLASS_LITERAL:
11445 CAN_COMPLETE_NORMALLY (node) = 1;
11446 node = patch_incomplete_class_ref (node);
11447 if (node == error_mark_node)
11448 return error_mark_node;
11451 case INSTANCE_INITIALIZERS_EXPR:
11452 in_instance_initializer++;
11453 node = java_complete_tree (TREE_OPERAND (node, 0));
11454 in_instance_initializer--;
11455 if (node != error_mark_node)
11456 TREE_TYPE (node) = void_type_node;
11458 return error_mark_node;
11462 CAN_COMPLETE_NORMALLY (node) = 1;
11463 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
11464 and it's time to turn it into the appropriate String object */
11465 if ((nn = patch_string (node)))
11468 fatal ("No case for tree code `%s' - java_complete_tree\n",
11469 tree_code_name [TREE_CODE (node)]);
11474 /* Complete function call's argument. Return a non zero value is an
11475 error was found. */
11478 complete_function_arguments (node)
11484 ctxp->explicit_constructor_p += (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11485 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
11487 tree wfl = TREE_VALUE (cn), parm, temp;
11488 parm = java_complete_tree (wfl);
11490 if (parm == error_mark_node)
11495 /* If have a string literal that we haven't transformed yet or a
11496 crafted string buffer, as a result of use of the the String
11497 `+' operator. Build `parm.toString()' and expand it. */
11498 if ((temp = patch_string (parm)))
11500 /* Inline PRIMTYPE.TYPE read access */
11501 parm = maybe_build_primttype_type_ref (parm, wfl);
11503 TREE_VALUE (cn) = parm;
11505 ctxp->explicit_constructor_p -= (CALL_EXPLICIT_CONSTRUCTOR_P (node) ? 1 : 0);
11509 /* Sometimes (for loops and variable initialized during their
11510 declaration), we want to wrap a statement around a WFL and turn it
11514 build_debugable_stmt (location, stmt)
11518 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
11520 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
11521 EXPR_WFL_LINECOL (stmt) = location;
11523 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
11528 build_expr_block (body, decls)
11531 tree node = make_node (BLOCK);
11532 BLOCK_EXPR_DECLS (node) = decls;
11533 BLOCK_EXPR_BODY (node) = body;
11535 TREE_TYPE (node) = TREE_TYPE (body);
11536 TREE_SIDE_EFFECTS (node) = 1;
11540 /* Create a new function block and link it approriately to current
11541 function block chain */
11546 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
11549 /* Link block B supercontext to the previous block. The current
11550 function DECL is used as supercontext when enter_a_block is called
11551 for the first time for a given function. The current function body
11552 (DECL_FUNCTION_BODY) is set to be block B. */
11558 tree fndecl = current_function_decl;
11561 BLOCK_SUPERCONTEXT (b) = current_static_block;
11562 current_static_block = b;
11565 else if (!DECL_FUNCTION_BODY (fndecl))
11567 BLOCK_SUPERCONTEXT (b) = fndecl;
11568 DECL_FUNCTION_BODY (fndecl) = b;
11572 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
11573 DECL_FUNCTION_BODY (fndecl) = b;
11578 /* Exit a block by changing the current function body
11579 (DECL_FUNCTION_BODY) to the current block super context, only if
11580 the block being exited isn't the method's top level one. */
11586 if (current_function_decl)
11588 b = DECL_FUNCTION_BODY (current_function_decl);
11589 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
11590 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
11594 b = current_static_block;
11596 if (BLOCK_SUPERCONTEXT (b))
11597 current_static_block = BLOCK_SUPERCONTEXT (b);
11602 /* Lookup for NAME in the nested function's blocks, all the way up to
11603 the current toplevel one. It complies with Java's local variable
11607 lookup_name_in_blocks (name)
11610 tree b = GET_CURRENT_BLOCK (current_function_decl);
11612 while (b != current_function_decl)
11616 /* Paranoid sanity check. To be removed */
11617 if (TREE_CODE (b) != BLOCK)
11618 fatal ("non block expr function body - lookup_name_in_blocks");
11620 for (current = BLOCK_EXPR_DECLS (b); current;
11621 current = TREE_CHAIN (current))
11622 if (DECL_NAME (current) == name)
11624 b = BLOCK_SUPERCONTEXT (b);
11630 maybe_absorb_scoping_blocks ()
11632 while (BLOCK_EXPR_ORIGIN (GET_CURRENT_BLOCK (current_function_decl)))
11634 tree b = exit_block ();
11635 java_method_add_stmt (current_function_decl, b);
11636 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
11641 /* This section of the source is reserved to build_* functions that
11642 are building incomplete tree nodes and the patch_* functions that
11643 are completing them. */
11645 /* Wrap a non WFL node around a WFL. */
11647 build_wfl_wrap (node, location)
11651 tree wfl, node_to_insert = node;
11653 /* We want to process THIS . xxx symbolicaly, to keep it consistent
11654 with the way we're processing SUPER. A THIS from a primary as a
11655 different form than a SUPER. Turn THIS into something symbolic */
11656 if (TREE_CODE (node) == THIS_EXPR)
11657 node_to_insert = wfl = build_wfl_node (this_identifier_node);
11659 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
11661 EXPR_WFL_LINECOL (wfl) = location;
11662 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (node_to_insert, NULL_TREE);
11667 /* Build a super() constructor invocation. Returns empty_stmt_node if
11668 we're currently dealing with the class java.lang.Object. */
11671 build_super_invocation (mdecl)
11674 if (DECL_CONTEXT (mdecl) == object_type_node)
11675 return empty_stmt_node;
11678 tree super_wfl = build_wfl_node (super_identifier_node);
11679 tree a = NULL_TREE, t;
11680 /* If we're dealing with an anonymous class, pass the arguments
11681 of the crafted constructor along. */
11682 if (ANONYMOUS_CLASS_P (DECL_CONTEXT (mdecl)))
11684 SKIP_THIS_AND_ARTIFICIAL_PARMS (t, mdecl);
11685 for (; t != end_params_node; t = TREE_CHAIN (t))
11686 a = tree_cons (NULL_TREE, build_wfl_node (TREE_PURPOSE (t)), a);
11688 return build_method_invocation (super_wfl, a);
11692 /* Build a SUPER/THIS qualified method invocation. */
11695 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
11702 build_wfl_node (use_this ? this_identifier_node : super_identifier_node);
11703 EXPR_WFL_LINECOL (wfl) = lloc;
11704 invok = build_method_invocation (name, args);
11705 return make_qualified_primary (wfl, invok, rloc);
11708 /* Build an incomplete CALL_EXPR node. */
11711 build_method_invocation (name, args)
11715 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
11716 TREE_SIDE_EFFECTS (call) = 1;
11717 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11721 /* Build an incomplete new xxx(...) node. */
11724 build_new_invocation (name, args)
11727 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
11728 TREE_SIDE_EFFECTS (call) = 1;
11729 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
11733 /* Build an incomplete assignment expression. */
11736 build_assignment (op, op_location, lhs, rhs)
11737 int op, op_location;
11741 /* Build the corresponding binop if we deal with a Compound
11742 Assignment operator. Mark the binop sub-tree as part of a
11743 Compound Assignment expression */
11744 if (op != ASSIGN_TK)
11746 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
11747 COMPOUND_ASSIGN_P (rhs) = 1;
11749 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
11750 TREE_SIDE_EFFECTS (assignment) = 1;
11751 EXPR_WFL_LINECOL (assignment) = op_location;
11755 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
11758 print_int_node (node)
11761 static char buffer [80];
11762 if (TREE_CONSTANT_OVERFLOW (node))
11763 sprintf (buffer, "<overflow>");
11765 if (TREE_INT_CST_HIGH (node) == 0)
11766 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
11767 TREE_INT_CST_LOW (node));
11768 else if (TREE_INT_CST_HIGH (node) == -1
11769 && TREE_INT_CST_LOW (node) != 0)
11772 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
11773 -TREE_INT_CST_LOW (node));
11776 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
11777 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
11782 /* Return 1 if an assignment to a FINAL is attempted in a non suitable
11786 check_final_assignment (lvalue, wfl)
11789 if (TREE_CODE (lvalue) == COMPOUND_EXPR
11790 && JDECL_P (TREE_OPERAND (lvalue, 1)))
11791 lvalue = TREE_OPERAND (lvalue, 1);
11793 /* When generating class files, references to the `length' field
11794 look a bit different. */
11795 if ((flag_emit_class_files
11796 && TREE_CODE (lvalue) == COMPONENT_REF
11797 && TYPE_ARRAY_P (TREE_TYPE (TREE_OPERAND (lvalue, 0)))
11798 && FIELD_FINAL (TREE_OPERAND (lvalue, 1)))
11799 || (TREE_CODE (lvalue) == FIELD_DECL
11800 && FIELD_FINAL (lvalue)
11801 && !DECL_CLINIT_P (current_function_decl)
11802 && !DECL_FINIT_P (current_function_decl)))
11804 parse_error_context
11805 (wfl, "Can't assign a value to the final variable `%s'",
11806 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
11812 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
11813 read. This is needed to avoid circularities in the implementation
11814 of these fields in libjava. */
11817 maybe_build_primttype_type_ref (rhs, wfl)
11820 tree to_return = NULL_TREE;
11821 tree rhs_type = TREE_TYPE (rhs);
11822 if (TREE_CODE (rhs) == COMPOUND_EXPR)
11824 tree n = TREE_OPERAND (rhs, 1);
11825 if (TREE_CODE (n) == VAR_DECL
11826 && DECL_NAME (n) == TYPE_identifier_node
11827 && rhs_type == class_ptr_type
11828 && TREE_CODE (EXPR_WFL_NODE (wfl)) == IDENTIFIER_NODE)
11830 const char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
11831 if (!strncmp (self_name, "java.lang.", 10))
11832 to_return = build_primtype_type_ref (self_name);
11835 return (to_return ? to_return : rhs );
11838 /* 15.25 Assignment operators. */
11841 patch_assignment (node, wfl_op1, wfl_op2)
11846 tree rhs = TREE_OPERAND (node, 1);
11847 tree lvalue = TREE_OPERAND (node, 0), llvalue;
11848 tree lhs_type = NULL_TREE, rhs_type, new_rhs = NULL_TREE;
11849 int error_found = 0;
11850 int lvalue_from_array = 0;
11852 /* Can't assign to a (blank) final. */
11853 if (check_final_assignment (lvalue, wfl_op1))
11856 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
11858 /* Lhs can be a named variable */
11859 if (JDECL_P (lvalue))
11861 lhs_type = TREE_TYPE (lvalue);
11863 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
11864 comment on reason why */
11865 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
11867 lhs_type = TREE_TYPE (lvalue);
11868 lvalue_from_array = 1;
11870 /* Or a field access */
11871 else if (TREE_CODE (lvalue) == COMPONENT_REF)
11872 lhs_type = TREE_TYPE (lvalue);
11873 /* Or a function return slot */
11874 else if (TREE_CODE (lvalue) == RESULT_DECL)
11875 lhs_type = TREE_TYPE (lvalue);
11876 /* Otherwise, we might want to try to write into an optimized static
11877 final, this is an of a different nature, reported further on. */
11878 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
11879 && resolve_expression_name (wfl_op1, &llvalue))
11881 if (!error_found && check_final_assignment (llvalue, wfl_op1))
11883 /* What we should do instead is resetting the all the flags
11884 previously set, exchange lvalue for llvalue and continue. */
11886 return error_mark_node;
11889 lhs_type = TREE_TYPE (lvalue);
11893 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
11897 rhs_type = TREE_TYPE (rhs);
11898 /* 5.1 Try the assignment conversion for builtin type. */
11899 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
11901 /* 5.2 If it failed, try a reference conversion */
11902 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
11903 lhs_type = promote_type (rhs_type);
11905 /* 15.25.2 If we have a compound assignment, convert RHS into the
11907 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
11908 new_rhs = convert (lhs_type, rhs);
11910 /* Explicit cast required. This is an error */
11913 char *t1 = xstrdup (lang_printable_name (TREE_TYPE (rhs), 0));
11914 char *t2 = xstrdup (lang_printable_name (lhs_type, 0));
11916 char operation [32]; /* Max size known */
11918 /* If the assignment is part of a declaration, we use the WFL of
11919 the declared variable to point out the error and call it a
11920 declaration problem. If the assignment is a genuine =
11921 operator, we call is a operator `=' problem, otherwise we
11922 call it an assignment problem. In both of these last cases,
11923 we use the WFL of the operator to indicate the error. */
11925 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
11928 strcpy (operation, "declaration");
11932 wfl = wfl_operator;
11933 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
11934 strcpy (operation, "assignment");
11935 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
11936 strcpy (operation, "`return'");
11938 strcpy (operation, "`='");
11941 if (!valid_cast_to_p (rhs_type, lhs_type))
11942 parse_error_context
11943 (wfl, "Incompatible type for %s. Can't convert `%s' to `%s'",
11944 operation, t1, t2);
11946 parse_error_context (wfl, "Incompatible type for %s. Explicit cast needed to convert `%s' to `%s'",
11947 operation, t1, t2);
11948 free (t1); free (t2);
11952 /* Inline read access to java.lang.PRIMTYPE.TYPE */
11954 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
11957 return error_mark_node;
11959 /* 10.10: Array Store Exception runtime check */
11960 if (!flag_emit_class_files
11962 && lvalue_from_array
11963 && JREFERENCE_TYPE_P (TYPE_ARRAY_ELEMENT (lhs_type)))
11966 tree base = lvalue;
11968 /* We need to retrieve the right argument for _Jv_CheckArrayStore */
11969 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
11970 base = TREE_OPERAND (lvalue, 0);
11973 if (flag_bounds_check)
11974 base = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (base, 0), 1), 0);
11976 base = TREE_OPERAND (TREE_OPERAND (base, 0), 0);
11979 /* Build the invocation of _Jv_CheckArrayStore */
11980 new_rhs = save_expr (new_rhs);
11981 check = build (CALL_EXPR, void_type_node,
11982 build_address_of (soft_checkarraystore_node),
11983 tree_cons (NULL_TREE, base,
11984 build_tree_list (NULL_TREE, new_rhs)),
11986 TREE_SIDE_EFFECTS (check) = 1;
11988 /* We have to decide on an insertion point */
11989 if (TREE_CODE (lvalue) == COMPOUND_EXPR)
11992 if (flag_bounds_check)
11994 t = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0);
11995 TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (lvalue, 1), 0), 0) =
11996 build (COMPOUND_EXPR, void_type_node, t, check);
11999 TREE_OPERAND (lvalue, 1) = build (COMPOUND_EXPR, lhs_type,
12000 check, TREE_OPERAND (lvalue, 1));
12004 /* Make sure the bound check will happen before the store check */
12005 if (flag_bounds_check)
12006 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0) =
12007 build (COMPOUND_EXPR, void_type_node,
12008 TREE_OPERAND (TREE_OPERAND (lvalue, 0), 0), check);
12010 lvalue = build (COMPOUND_EXPR, lhs_type, check, lvalue);
12014 TREE_OPERAND (node, 0) = lvalue;
12015 TREE_OPERAND (node, 1) = new_rhs;
12016 TREE_TYPE (node) = lhs_type;
12020 /* Check that type SOURCE can be cast into type DEST. If the cast
12021 can't occur at all, return 0 otherwise 1. This function is used to
12022 produce accurate error messages on the reasons why an assignment
12026 try_reference_assignconv (lhs_type, rhs)
12027 tree lhs_type, rhs;
12029 tree new_rhs = NULL_TREE;
12030 tree rhs_type = TREE_TYPE (rhs);
12032 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
12034 /* `null' may be assigned to any reference type */
12035 if (rhs == null_pointer_node)
12036 new_rhs = null_pointer_node;
12037 /* Try the reference assignment conversion */
12038 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
12040 /* This is a magic assignment that we process differently */
12041 else if (rhs == soft_exceptioninfo_call_node)
12047 /* Check that RHS can be converted into LHS_TYPE by the assignment
12048 conversion (5.2), for the cases of RHS being a builtin type. Return
12049 NULL_TREE if the conversion fails or if because RHS isn't of a
12050 builtin type. Return a converted RHS if the conversion is possible. */
12053 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
12054 tree wfl_op1, lhs_type, rhs;
12056 tree new_rhs = NULL_TREE;
12057 tree rhs_type = TREE_TYPE (rhs);
12059 /* Zero accepted everywhere */
12060 if (TREE_CODE (rhs) == INTEGER_CST
12061 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
12062 && JPRIMITIVE_TYPE_P (rhs_type))
12063 new_rhs = convert (lhs_type, rhs);
12065 /* 5.1.1 Try Identity Conversion,
12066 5.1.2 Try Widening Primitive Conversion */
12067 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
12068 new_rhs = convert (lhs_type, rhs);
12070 /* Try a narrowing primitive conversion (5.1.3):
12071 - expression is a constant expression of type int AND
12072 - variable is byte, short or char AND
12073 - The value of the expression is representable in the type of the
12075 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
12076 && (lhs_type == byte_type_node || lhs_type == char_type_node
12077 || lhs_type == short_type_node))
12079 if (int_fits_type_p (rhs, lhs_type))
12080 new_rhs = convert (lhs_type, rhs);
12081 else if (wfl_op1) /* Might be called with a NULL */
12082 parse_warning_context
12083 (wfl_op1, "Constant expression `%s' to wide for narrowing primitive conversion to `%s'",
12084 print_int_node (rhs), lang_printable_name (lhs_type, 0));
12085 /* Reported a warning that will turn into an error further
12086 down, so we don't return */
12092 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
12093 conversion (5.1.1) or widening primitve conversion (5.1.2). Return
12094 0 is the conversion test fails. This implements parts the method
12095 invocation convertion (5.3). */
12098 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
12099 tree lhs_type, rhs_type;
12101 /* 5.1.1: This is the identity conversion part. */
12102 if (lhs_type == rhs_type)
12105 /* Reject non primitive types */
12106 if (!JPRIMITIVE_TYPE_P (lhs_type) || !JPRIMITIVE_TYPE_P (rhs_type))
12109 /* 5.1.2: widening primitive conversion. byte, even if it's smaller
12110 than a char can't be converted into a char. Short can't too, but
12111 the < test below takes care of that */
12112 if (lhs_type == char_type_node && rhs_type == byte_type_node)
12115 /* Accept all promoted type here. Note, we can't use <= in the test
12116 below, because we still need to bounce out assignments of short
12117 to char and the likes */
12118 if (lhs_type == int_type_node
12119 && (rhs_type == promoted_byte_type_node
12120 || rhs_type == promoted_short_type_node
12121 || rhs_type == promoted_char_type_node
12122 || rhs_type == promoted_boolean_type_node))
12125 /* From here, an integral is widened if its precision is smaller
12126 than the precision of the LHS or if the LHS is a floating point
12127 type, or the RHS is a float and the RHS a double. */
12128 if ((JINTEGRAL_TYPE_P (rhs_type) && JINTEGRAL_TYPE_P (lhs_type)
12129 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
12130 || (JINTEGRAL_TYPE_P (rhs_type) && JFLOAT_TYPE_P (lhs_type))
12131 || (rhs_type == float_type_node && lhs_type == double_type_node))
12137 /* Check that something of SOURCE type can be assigned or cast to
12138 something of DEST type at runtime. Return 1 if the operation is
12139 valid, 0 otherwise. If CAST is set to 1, we're treating the case
12140 were SOURCE is cast into DEST, which borrows a lot of the
12141 assignment check. */
12144 valid_ref_assignconv_cast_p (source, dest, cast)
12149 /* SOURCE or DEST might be null if not from a declared entity. */
12150 if (!source || !dest)
12152 if (JNULLP_TYPE_P (source))
12154 if (TREE_CODE (source) == POINTER_TYPE)
12155 source = TREE_TYPE (source);
12156 if (TREE_CODE (dest) == POINTER_TYPE)
12157 dest = TREE_TYPE (dest);
12158 /* Case where SOURCE is a class type */
12159 if (TYPE_CLASS_P (source))
12161 if (TYPE_CLASS_P (dest))
12162 return (source == dest
12163 || inherits_from_p (source, dest)
12164 || (cast && inherits_from_p (dest, source)));
12165 if (TYPE_INTERFACE_P (dest))
12167 /* If doing a cast and SOURCE is final, the operation is
12168 always correct a compile time (because even if SOURCE
12169 does not implement DEST, a subclass of SOURCE might). */
12170 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
12172 /* Otherwise, SOURCE must implement DEST */
12173 return interface_of_p (dest, source);
12175 /* DEST is an array, cast permited if SOURCE is of Object type */
12176 return (cast && source == object_type_node ? 1 : 0);
12178 if (TYPE_INTERFACE_P (source))
12180 if (TYPE_CLASS_P (dest))
12182 /* If not casting, DEST must be the Object type */
12184 return dest == object_type_node;
12185 /* We're doing a cast. The cast is always valid is class
12186 DEST is not final, otherwise, DEST must implement SOURCE */
12187 else if (!CLASS_FINAL (TYPE_NAME (dest)))
12190 return interface_of_p (source, dest);
12192 if (TYPE_INTERFACE_P (dest))
12194 /* If doing a cast, then if SOURCE and DEST contain method
12195 with the same signature but different return type, then
12196 this is a (compile time) error */
12199 tree method_source, method_dest;
12203 for (method_source = TYPE_METHODS (source); method_source;
12204 method_source = TREE_CHAIN (method_source))
12207 build_java_argument_signature (TREE_TYPE (method_source));
12208 source_type = TREE_TYPE (TREE_TYPE (method_source));
12209 source_name = DECL_NAME (method_source);
12210 for (method_dest = TYPE_METHODS (dest);
12211 method_dest; method_dest = TREE_CHAIN (method_dest))
12213 build_java_argument_signature (TREE_TYPE (method_dest))
12214 && source_name == DECL_NAME (method_dest)
12215 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
12221 return source == dest || interface_of_p (dest, source);
12225 (DECL_NAME (TYPE_NAME (source)) == java_lang_cloneable) : 0);
12227 if (TYPE_ARRAY_P (source))
12229 if (TYPE_CLASS_P (dest))
12230 return dest == object_type_node;
12231 /* Can't cast an array to an interface unless the interface is
12232 java.lang.Cloneable */
12233 if (TYPE_INTERFACE_P (dest))
12234 return (DECL_NAME (TYPE_NAME (dest)) == java_lang_cloneable ? 1 : 0);
12237 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
12238 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
12240 /* In case of severe errors, they turn out null */
12241 if (!dest_element_type || !source_element_type)
12243 if (source_element_type == dest_element_type)
12245 return valid_ref_assignconv_cast_p (source_element_type,
12246 dest_element_type, cast);
12254 valid_cast_to_p (source, dest)
12258 if (TREE_CODE (source) == POINTER_TYPE)
12259 source = TREE_TYPE (source);
12260 if (TREE_CODE (dest) == POINTER_TYPE)
12261 dest = TREE_TYPE (dest);
12263 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
12264 return valid_ref_assignconv_cast_p (source, dest, 1);
12266 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
12272 /* Method invocation conversion test. Return 1 if type SOURCE can be
12273 converted to type DEST through the methond invocation conversion
12277 do_unary_numeric_promotion (arg)
12280 tree type = TREE_TYPE (arg);
12281 if (TREE_CODE (type) == INTEGER_TYPE ? TYPE_PRECISION (type) < 32
12282 : TREE_CODE (type) == CHAR_TYPE)
12283 arg = convert (int_type_node, arg);
12287 /* Return a non zero value if SOURCE can be converted into DEST using
12288 the method invocation conversion rule (5.3). */
12290 valid_method_invocation_conversion_p (dest, source)
12293 return ((JPRIMITIVE_TYPE_P (source) && JPRIMITIVE_TYPE_P (dest)
12294 && valid_builtin_assignconv_identity_widening_p (dest, source))
12295 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
12296 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
12297 && valid_ref_assignconv_cast_p (source, dest, 0)));
12300 /* Build an incomplete binop expression. */
12303 build_binop (op, op_location, op1, op2)
12308 tree binop = build (op, NULL_TREE, op1, op2);
12309 TREE_SIDE_EFFECTS (binop) = 1;
12310 /* Store the location of the operator, for better error report. The
12311 string of the operator will be rebuild based on the OP value. */
12312 EXPR_WFL_LINECOL (binop) = op_location;
12316 /* Build the string of the operator retained by NODE. If NODE is part
12317 of a compound expression, add an '=' at the end of the string. This
12318 function is called when an error needs to be reported on an
12319 operator. The string is returned as a pointer to a static character
12323 operator_string (node)
12326 #define BUILD_OPERATOR_STRING(S) \
12328 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
12332 static char buffer [10];
12333 switch (TREE_CODE (node))
12335 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
12336 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
12337 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
12338 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12339 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
12340 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
12341 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
12342 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
12343 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
12344 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
12345 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
12346 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
12347 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
12348 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
12349 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
12350 case GT_EXPR: BUILD_OPERATOR_STRING (">");
12351 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
12352 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
12353 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
12354 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
12355 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
12356 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
12357 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
12358 case PREINCREMENT_EXPR: /* Fall through */
12359 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
12360 case PREDECREMENT_EXPR: /* Fall through */
12361 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
12363 fatal ("unregistered operator %s - operator_string",
12364 tree_code_name [TREE_CODE (node)]);
12367 #undef BUILD_OPERATOR_STRING
12370 /* Return 1 if VAR_ACCESS1 is equivalent to VAR_ACCESS2. */
12373 java_decl_equiv (var_acc1, var_acc2)
12374 tree var_acc1, var_acc2;
12376 if (JDECL_P (var_acc1))
12377 return (var_acc1 == var_acc2);
12379 return (TREE_CODE (var_acc1) == COMPONENT_REF
12380 && TREE_CODE (var_acc2) == COMPONENT_REF
12381 && TREE_OPERAND (TREE_OPERAND (var_acc1, 0), 0)
12382 == TREE_OPERAND (TREE_OPERAND (var_acc2, 0), 0)
12383 && TREE_OPERAND (var_acc1, 1) == TREE_OPERAND (var_acc2, 1));
12386 /* Return a non zero value if CODE is one of the operators that can be
12387 used in conjunction with the `=' operator in a compound assignment. */
12390 binop_compound_p (code)
12391 enum tree_code code;
12394 for (i = 0; i < BINOP_COMPOUND_CANDIDATES; i++)
12395 if (binop_lookup [i] == code)
12398 return i < BINOP_COMPOUND_CANDIDATES;
12401 /* Reorganize after a fold to get SAVE_EXPR to generate what we want. */
12407 tree c, b, ns, decl;
12409 if (TREE_CODE (t) != MODIFY_EXPR)
12412 c = TREE_OPERAND (t, 1);
12413 if (! (c && TREE_CODE (c) == COMPOUND_EXPR
12414 && TREE_CODE (TREE_OPERAND (c, 0)) == MODIFY_EXPR
12415 && binop_compound_p (TREE_CODE (TREE_OPERAND (c, 1)))))
12418 /* Now the left branch of the binary operator. */
12419 b = TREE_OPERAND (TREE_OPERAND (c, 1), 0);
12420 if (! (b && TREE_CODE (b) == NOP_EXPR
12421 && TREE_CODE (TREE_OPERAND (b, 0)) == SAVE_EXPR))
12424 ns = TREE_OPERAND (TREE_OPERAND (b, 0), 0);
12425 if (! (ns && TREE_CODE (ns) == NOP_EXPR
12426 && TREE_CODE (TREE_OPERAND (ns, 0)) == SAVE_EXPR))
12429 decl = TREE_OPERAND (TREE_OPERAND (ns, 0), 0);
12430 if ((JDECL_P (decl) || TREE_CODE (decl) == COMPONENT_REF)
12431 /* It's got to be the an equivalent decl */
12432 && java_decl_equiv (decl, TREE_OPERAND (TREE_OPERAND (c, 0), 0)))
12434 /* Shorten the NOP_EXPR/SAVE_EXPR path. */
12435 TREE_OPERAND (TREE_OPERAND (c, 1), 0) = TREE_OPERAND (ns, 0);
12436 /* Substitute the COMPOUND_EXPR by the BINOP_EXPR */
12437 TREE_OPERAND (t, 1) = TREE_OPERAND (c, 1);
12438 /* Change the right part of the BINOP_EXPR */
12439 TREE_OPERAND (TREE_OPERAND (t, 1), 1) = TREE_OPERAND (c, 0);
12445 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
12446 errors but we modify NODE so that it contains the type computed
12447 according to the expression, when it's fixed. Otherwise, we write
12448 error_mark_node as the type. It allows us to further the analysis
12449 of remaining nodes and detects more errors in certain cases. */
12452 patch_binop (node, wfl_op1, wfl_op2)
12457 tree op1 = TREE_OPERAND (node, 0);
12458 tree op2 = TREE_OPERAND (node, 1);
12459 tree op1_type = TREE_TYPE (op1);
12460 tree op2_type = TREE_TYPE (op2);
12461 tree prom_type = NULL_TREE, cn;
12462 int code = TREE_CODE (node);
12464 /* If 1, tell the routine that we have to return error_mark_node
12465 after checking for the initialization of the RHS */
12466 int error_found = 0;
12468 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
12472 /* 15.16 Multiplicative operators */
12473 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
12474 case RDIV_EXPR: /* 15.16.2 Division Operator / */
12475 case TRUNC_DIV_EXPR: /* 15.16.2 Integral type Division Operator / */
12476 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
12477 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12479 if (!JPRIMITIVE_TYPE_P (op1_type))
12480 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12481 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12482 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12483 TREE_TYPE (node) = error_mark_node;
12487 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12488 /* Change the division operator if necessary */
12489 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
12490 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
12492 if (TREE_CODE (prom_type) == INTEGER_TYPE
12493 && flag_use_divide_subroutine
12494 && ! flag_emit_class_files
12495 && (code == RDIV_EXPR || code == TRUNC_MOD_EXPR))
12496 return build_java_soft_divmod (TREE_CODE (node), prom_type, op1, op2);
12498 /* This one is more complicated. FLOATs are processed by a
12499 function call to soft_fmod. Duplicate the value of the
12500 COMPOUND_ASSIGN_P flag. */
12501 if (code == TRUNC_MOD_EXPR)
12503 tree mod = build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
12504 COMPOUND_ASSIGN_P (mod) = COMPOUND_ASSIGN_P (node);
12505 TREE_SIDE_EFFECTS (mod)
12506 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12511 /* 15.17 Additive Operators */
12512 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
12514 /* Operation is valid if either one argument is a string
12515 constant, a String object or a StringBuffer crafted for the
12516 purpose of the a previous usage of the String concatenation
12519 if (TREE_CODE (op1) == STRING_CST
12520 || TREE_CODE (op2) == STRING_CST
12521 || JSTRING_TYPE_P (op1_type)
12522 || JSTRING_TYPE_P (op2_type)
12523 || IS_CRAFTED_STRING_BUFFER_P (op1)
12524 || IS_CRAFTED_STRING_BUFFER_P (op2))
12525 return build_string_concatenation (op1, op2);
12527 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
12529 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
12531 if (!JPRIMITIVE_TYPE_P (op1_type))
12532 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12533 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
12534 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12535 TREE_TYPE (node) = error_mark_node;
12539 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12542 /* 15.18 Shift Operators */
12546 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
12548 if (!JINTEGRAL_TYPE_P (op1_type))
12549 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12552 if (JPRIMITIVE_TYPE_P (op2_type))
12553 parse_error_context (wfl_operator,
12554 "Incompatible type for `%s'. Explicit cast needed to convert shift distance from `%s' to integral",
12555 operator_string (node),
12556 lang_printable_name (op2_type, 0));
12558 parse_error_context (wfl_operator,
12559 "Incompatible type for `%s'. Can't convert shift distance from `%s' to integral",
12560 operator_string (node),
12561 lang_printable_name (op2_type, 0));
12563 TREE_TYPE (node) = error_mark_node;
12568 /* Unary numeric promotion (5.6.1) is performed on each operand
12570 op1 = do_unary_numeric_promotion (op1);
12571 op2 = do_unary_numeric_promotion (op2);
12573 /* The type of the shift expression is the type of the promoted
12574 type of the left-hand operand */
12575 prom_type = TREE_TYPE (op1);
12577 /* Shift int only up to 0x1f and long up to 0x3f */
12578 if (prom_type == int_type_node)
12579 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12580 build_int_2 (0x1f, 0)));
12582 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
12583 build_int_2 (0x3f, 0)));
12585 /* The >>> operator is a >> operating on unsigned quantities */
12586 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
12589 tree utype = unsigned_type (prom_type);
12590 op1 = convert (utype, op1);
12591 TREE_SET_CODE (node, RSHIFT_EXPR);
12592 TREE_OPERAND (node, 0) = op1;
12593 TREE_OPERAND (node, 1) = op2;
12594 TREE_TYPE (node) = utype;
12595 to_return = convert (prom_type, node);
12596 /* Copy the original value of the COMPOUND_ASSIGN_P flag */
12597 COMPOUND_ASSIGN_P (to_return) = COMPOUND_ASSIGN_P (node);
12598 TREE_SIDE_EFFECTS (to_return)
12599 = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12604 /* 15.19.1 Type Comparison Operator instaceof */
12605 case INSTANCEOF_EXPR:
12607 TREE_TYPE (node) = boolean_type_node;
12609 if (!(op2_type = resolve_type_during_patch (op2)))
12610 return error_mark_node;
12612 /* The first operand must be a reference type or the null type */
12613 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
12614 error_found = 1; /* Error reported further below */
12616 /* The second operand must be a reference type */
12617 if (!JREFERENCE_TYPE_P (op2_type))
12619 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
12620 parse_error_context
12621 (wfl_operator, "Invalid argument `%s' for `instanceof'",
12622 lang_printable_name (op2_type, 0));
12626 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
12628 /* If the first operand is null, the result is always false */
12629 if (op1 == null_pointer_node)
12630 return boolean_false_node;
12631 else if (flag_emit_class_files)
12633 TREE_OPERAND (node, 1) = op2_type;
12634 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1);
12637 /* Otherwise we have to invoke instance of to figure it out */
12639 return build_instanceof (op1, op2_type);
12641 /* There is no way the expression operand can be an instance of
12642 the type operand. This is a compile time error. */
12645 char *t1 = xstrdup (lang_printable_name (op1_type, 0));
12646 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
12647 parse_error_context
12648 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
12649 t1, lang_printable_name (op2_type, 0));
12656 /* 15.21 Bitwise and Logical Operators */
12660 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
12661 /* Binary numeric promotion is performed on both operand and the
12662 expression retain that type */
12663 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12665 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
12666 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
12667 /* The type of the bitwise operator expression is BOOLEAN */
12668 prom_type = boolean_type_node;
12671 if (!JINTEGRAL_TYPE_P (op1_type))
12672 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
12673 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
12674 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
12675 TREE_TYPE (node) = error_mark_node;
12677 /* Insert a break here if adding thing before the switch's
12678 break for this case */
12682 /* 15.22 Conditional-And Operator */
12683 case TRUTH_ANDIF_EXPR:
12684 /* 15.23 Conditional-Or Operator */
12685 case TRUTH_ORIF_EXPR:
12686 /* Operands must be of BOOLEAN type */
12687 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
12688 TREE_CODE (op2_type) != BOOLEAN_TYPE)
12690 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
12691 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
12692 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
12693 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
12694 TREE_TYPE (node) = boolean_type_node;
12698 /* The type of the conditional operators is BOOLEAN */
12699 prom_type = boolean_type_node;
12702 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
12707 /* The type of each of the operands must be a primitive numeric
12709 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
12711 if (!JNUMERIC_TYPE_P (op1_type))
12712 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
12713 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
12714 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
12715 TREE_TYPE (node) = boolean_type_node;
12719 /* Binary numeric promotion is performed on the operands */
12720 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12721 /* The type of the relation expression is always BOOLEAN */
12722 prom_type = boolean_type_node;
12725 /* 15.20 Equality Operator */
12728 /* It's time for us to patch the strings. */
12729 if ((cn = patch_string (op1)))
12732 op1_type = TREE_TYPE (op1);
12734 if ((cn = patch_string (op2)))
12737 op2_type = TREE_TYPE (op2);
12740 /* 15.20.1 Numerical Equality Operators == and != */
12741 /* Binary numeric promotion is performed on the operands */
12742 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
12743 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
12745 /* 15.20.2 Boolean Equality Operators == and != */
12746 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
12747 TREE_CODE (op2_type) == BOOLEAN_TYPE)
12748 ; /* Nothing to do here */
12750 /* 15.20.3 Reference Equality Operators == and != */
12751 /* Types have to be either references or the null type. If
12752 they're references, it must be possible to convert either
12753 type to the other by casting conversion. */
12754 else if (op1 == null_pointer_node || op2 == null_pointer_node
12755 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
12756 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
12757 || valid_ref_assignconv_cast_p (op2_type,
12759 ; /* Nothing to do here */
12761 /* Else we have an error figure what can't be converted into
12762 what and report the error */
12766 t1 = xstrdup (lang_printable_name (op1_type, 0));
12767 parse_error_context
12769 "Incompatible type for `%s'. Can't convert `%s' to `%s'",
12770 operator_string (node), t1,
12771 lang_printable_name (op2_type, 0));
12773 TREE_TYPE (node) = boolean_type_node;
12777 prom_type = boolean_type_node;
12782 return error_mark_node;
12784 TREE_OPERAND (node, 0) = op1;
12785 TREE_OPERAND (node, 1) = op2;
12786 TREE_TYPE (node) = prom_type;
12787 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12789 if (flag_emit_xref)
12792 /* fold does not respect side-effect order as required for Java but not C.
12793 * Also, it sometimes create SAVE_EXPRs which are bad when emitting
12796 if (flag_emit_class_files ? (TREE_CONSTANT (op1) && TREE_CONSTANT (op2))
12797 : ! TREE_SIDE_EFFECTS (node))
12798 node = fold (node);
12802 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
12803 zero value, the value of CSTE comes after the valude of STRING */
12806 do_merge_string_cste (cste, string, string_len, after)
12808 const char *string;
12809 int string_len, after;
12811 int len = TREE_STRING_LENGTH (cste) + string_len;
12812 const char *old = TREE_STRING_POINTER (cste);
12813 TREE_STRING_LENGTH (cste) = len;
12814 TREE_STRING_POINTER (cste) = obstack_alloc (expression_obstack, len+1);
12817 strcpy (TREE_STRING_POINTER (cste), string);
12818 strcat (TREE_STRING_POINTER (cste), old);
12822 strcpy (TREE_STRING_POINTER (cste), old);
12823 strcat (TREE_STRING_POINTER (cste), string);
12828 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
12829 new STRING_CST on success, NULL_TREE on failure */
12832 merge_string_cste (op1, op2, after)
12836 /* Handle two string constants right away */
12837 if (TREE_CODE (op2) == STRING_CST)
12838 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
12839 TREE_STRING_LENGTH (op2), after);
12841 /* Reasonable integer constant can be treated right away */
12842 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
12844 static const char *boolean_true = "true";
12845 static const char *boolean_false = "false";
12846 static const char *null_pointer = "null";
12848 const char *string;
12850 if (op2 == boolean_true_node)
12851 string = boolean_true;
12852 else if (op2 == boolean_false_node)
12853 string = boolean_false;
12854 else if (op2 == null_pointer_node)
12855 string = null_pointer;
12856 else if (TREE_TYPE (op2) == char_type_node)
12858 ch[0] = (char )TREE_INT_CST_LOW (op2);
12863 string = print_int_node (op2);
12865 return do_merge_string_cste (op1, string, strlen (string), after);
12870 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
12871 has to be a STRING_CST and the other part must be a STRING_CST or a
12872 INTEGRAL constant. Return a new STRING_CST if the operation
12873 succeed, NULL_TREE otherwise.
12875 If the case we want to optimize for space, we might want to return
12876 NULL_TREE for each invocation of this routine. FIXME */
12879 string_constant_concatenation (op1, op2)
12882 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
12887 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
12888 rest = (string == op1 ? op2 : op1);
12889 invert = (string == op1 ? 0 : 1 );
12891 /* Walk REST, only if it looks reasonable */
12892 if (TREE_CODE (rest) != STRING_CST
12893 && !IS_CRAFTED_STRING_BUFFER_P (rest)
12894 && !JSTRING_TYPE_P (TREE_TYPE (rest))
12895 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
12897 rest = java_complete_tree (rest);
12898 if (rest == error_mark_node)
12899 return error_mark_node;
12900 rest = fold (rest);
12902 return merge_string_cste (string, rest, invert);
12907 /* Implement the `+' operator. Does static optimization if possible,
12908 otherwise create (if necessary) and append elements to a
12909 StringBuffer. The StringBuffer will be carried around until it is
12910 used for a function call or an assignment. Then toString() will be
12911 called on it to turn it into a String object. */
12914 build_string_concatenation (op1, op2)
12918 int side_effects = TREE_SIDE_EFFECTS (op1) | TREE_SIDE_EFFECTS (op2);
12920 if (flag_emit_xref)
12921 return build (PLUS_EXPR, string_type_node, op1, op2);
12923 /* Try to do some static optimization */
12924 if ((result = string_constant_concatenation (op1, op2)))
12927 /* Discard empty strings on either side of the expression */
12928 if (TREE_CODE (op1) == STRING_CST && TREE_STRING_LENGTH (op1) == 0)
12933 else if (TREE_CODE (op2) == STRING_CST && TREE_STRING_LENGTH (op2) == 0)
12936 /* If operands are string constant, turn then into object references */
12937 if (TREE_CODE (op1) == STRING_CST)
12938 op1 = patch_string_cst (op1);
12939 if (op2 && TREE_CODE (op2) == STRING_CST)
12940 op2 = patch_string_cst (op2);
12942 /* If either one of the constant is null and the other non null
12943 operand is a String object, return it. */
12944 if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
12947 /* If OP1 isn't already a StringBuffer, create and
12948 initialize a new one */
12949 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
12951 /* Two solutions here:
12952 1) OP1 is a constant string reference, we call new StringBuffer(OP1)
12953 2) OP1 is something else, we call new StringBuffer().append(OP1). */
12954 if (TREE_CONSTANT (op1) && JSTRING_TYPE_P (TREE_TYPE (op1)))
12955 op1 = BUILD_STRING_BUFFER (op1);
12958 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
12959 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
12965 /* OP1 is no longer the last node holding a crafted StringBuffer */
12966 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
12967 /* Create a node for `{new...,xxx}.append (op2)' */
12969 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
12972 /* Mark the last node holding a crafted StringBuffer */
12973 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
12975 TREE_SIDE_EFFECTS (op1) = side_effects;
12979 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
12980 StringBuffer. If no string were found to be patched, return
12984 patch_string (node)
12987 if (node == error_mark_node)
12988 return error_mark_node;
12989 if (TREE_CODE (node) == STRING_CST)
12990 return patch_string_cst (node);
12991 else if (IS_CRAFTED_STRING_BUFFER_P (node))
12993 int saved = ctxp->explicit_constructor_p;
12994 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
12996 /* Temporary disable forbid the use of `this'. */
12997 ctxp->explicit_constructor_p = 0;
12998 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
12999 /* String concatenation arguments must be evaluated in order too. */
13000 ret = force_evaluation_order (ret);
13001 /* Restore it at its previous value */
13002 ctxp->explicit_constructor_p = saved;
13008 /* Build the internal representation of a string constant. */
13011 patch_string_cst (node)
13015 if (! flag_emit_class_files)
13017 push_obstacks (&permanent_obstack, &permanent_obstack);
13018 node = get_identifier (TREE_STRING_POINTER (node));
13019 location = alloc_name_constant (CONSTANT_String, node);
13020 node = build_ref_from_constant_pool (location);
13023 TREE_TYPE (node) = string_ptr_type_node;
13024 TREE_CONSTANT (node) = 1;
13028 /* Build an incomplete unary operator expression. */
13031 build_unaryop (op_token, op_location, op1)
13032 int op_token, op_location;
13039 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
13040 case MINUS_TK: op = NEGATE_EXPR; break;
13041 case NEG_TK: op = TRUTH_NOT_EXPR; break;
13042 case NOT_TK: op = BIT_NOT_EXPR; break;
13043 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
13047 unaryop = build1 (op, NULL_TREE, op1);
13048 TREE_SIDE_EFFECTS (unaryop) = 1;
13049 /* Store the location of the operator, for better error report. The
13050 string of the operator will be rebuild based on the OP value. */
13051 EXPR_WFL_LINECOL (unaryop) = op_location;
13055 /* Special case for the ++/-- operators, since they require an extra
13056 argument to build, which is set to NULL and patched
13057 later. IS_POST_P is 1 if the operator, 0 otherwise. */
13060 build_incdec (op_token, op_location, op1, is_post_p)
13061 int op_token, op_location;
13065 static enum tree_code lookup [2][2] =
13067 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
13068 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
13070 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
13071 NULL_TREE, op1, NULL_TREE);
13072 TREE_SIDE_EFFECTS (node) = 1;
13073 /* Store the location of the operator, for better error report. The
13074 string of the operator will be rebuild based on the OP value. */
13075 EXPR_WFL_LINECOL (node) = op_location;
13079 /* Build an incomplete cast operator, based on the use of the
13080 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
13081 set. java_complete_tree is trained to walk a CONVERT_EXPR even
13082 though its type is already set. */
13085 build_cast (location, type, exp)
13089 tree node = build1 (CONVERT_EXPR, type, exp);
13090 EXPR_WFL_LINECOL (node) = location;
13094 /* Build an incomplete class reference operator. */
13096 build_incomplete_class_ref (location, class_name)
13100 tree node = build1 (CLASS_LITERAL, NULL_TREE, class_name);
13101 EXPR_WFL_LINECOL (node) = location;
13105 /* Complete an incomplete class reference operator. */
13107 patch_incomplete_class_ref (node)
13110 tree type = TREE_OPERAND (node, 0);
13113 if (!(ref_type = resolve_type_during_patch (type)))
13114 return error_mark_node;
13116 if (!flag_emit_class_files || JPRIMITIVE_TYPE_P (ref_type))
13118 /* A class referenced by `foo.class' is initialized. */
13119 return build_class_init (ref_type, build_class_ref (ref_type));
13122 /* If we're emitting class files and we have to deal with non
13123 primitive types, we invoke (and consider generating) the
13124 synthetic static method `class$'. */
13125 if (!TYPE_DOT_CLASS (current_class))
13126 build_dot_class_method (current_class);
13128 build_dot_class_method_invocation (DECL_NAME (TYPE_NAME (ref_type)));
13129 return java_complete_tree (ref_type);
13132 /* 15.14 Unary operators. We return error_mark_node in case of error,
13133 but preserve the type of NODE if the type is fixed. */
13136 patch_unaryop (node, wfl_op)
13140 tree op = TREE_OPERAND (node, 0);
13141 tree op_type = TREE_TYPE (op);
13142 tree prom_type = NULL_TREE, value, decl;
13143 int outer_field_flag = 0;
13144 int code = TREE_CODE (node);
13145 int error_found = 0;
13147 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13151 /* 15.13.2 Postfix Increment Operator ++ */
13152 case POSTINCREMENT_EXPR:
13153 /* 15.13.3 Postfix Increment Operator -- */
13154 case POSTDECREMENT_EXPR:
13155 /* 15.14.1 Prefix Increment Operator ++ */
13156 case PREINCREMENT_EXPR:
13157 /* 15.14.2 Prefix Decrement Operator -- */
13158 case PREDECREMENT_EXPR:
13159 op = decl = strip_out_static_field_access_decl (op);
13160 outer_field_flag = outer_field_expanded_access_p (op, NULL, NULL, NULL);
13161 /* We might be trying to change an outer field accessed using
13163 if (outer_field_flag)
13165 /* Retrieve the decl of the field we're trying to access. We
13166 do that by first retrieving the function we would call to
13167 access the field. It has been already verified that this
13168 field isn't final */
13169 if (flag_emit_class_files)
13170 decl = TREE_OPERAND (op, 0);
13172 decl = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (op, 0), 0), 0);
13173 decl = DECL_FUNCTION_ACCESS_DECL (decl);
13175 /* We really should have a JAVA_ARRAY_EXPR to avoid this */
13176 else if (!JDECL_P (decl)
13177 && TREE_CODE (decl) != COMPONENT_REF
13178 && !(flag_emit_class_files && TREE_CODE (decl) == ARRAY_REF)
13179 && TREE_CODE (decl) != INDIRECT_REF
13180 && !(TREE_CODE (decl) == COMPOUND_EXPR
13181 && TREE_OPERAND (decl, 1)
13182 && (TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)))
13185 /* Before screaming, check that we're not in fact trying to
13186 increment a optimized static final access, in which case
13187 we issue an different error message. */
13188 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
13189 && resolve_expression_name (wfl_op, &lvalue)
13190 && check_final_assignment (lvalue, wfl_op)))
13191 parse_error_context (wfl_operator, "Invalid argument to `%s'",
13192 operator_string (node));
13193 TREE_TYPE (node) = error_mark_node;
13197 if (check_final_assignment (op, wfl_op))
13200 /* From now on, we know that op if a variable and that it has a
13201 valid wfl. We use wfl_op to locate errors related to the
13203 else if (!JNUMERIC_TYPE_P (op_type))
13205 parse_error_context
13206 (wfl_op, "Invalid argument type `%s' to `%s'",
13207 lang_printable_name (op_type, 0), operator_string (node));
13208 TREE_TYPE (node) = error_mark_node;
13213 /* Before the addition, binary numeric promotion is performed on
13214 both operands, if really necessary */
13215 if (JINTEGRAL_TYPE_P (op_type))
13217 value = build_int_2 (1, 0);
13218 TREE_TYPE (value) = TREE_TYPE (node) = op_type;
13222 value = build_int_2 (1, 0);
13224 binary_numeric_promotion (op_type,
13225 TREE_TYPE (value), &op, &value);
13228 /* We remember we might be accessing an outer field */
13229 if (outer_field_flag)
13231 /* We re-generate an access to the field */
13232 value = build (PLUS_EXPR, TREE_TYPE (op),
13233 build_outer_field_access (wfl_op, decl), value);
13235 /* And we patch the original access$() into a write
13236 with plus_op as a rhs */
13237 return outer_field_access_fix (node, op, value);
13240 /* And write back into the node. */
13241 TREE_OPERAND (node, 0) = op;
13242 TREE_OPERAND (node, 1) = value;
13243 /* Convert the overall back into its original type, if
13244 necessary, and return */
13245 if (JINTEGRAL_TYPE_P (op_type))
13246 return fold (node);
13248 return fold (convert (op_type, node));
13252 /* 15.14.3 Unary Plus Operator + */
13253 case UNARY_PLUS_EXPR:
13254 /* 15.14.4 Unary Minus Operator - */
13256 if (!JNUMERIC_TYPE_P (op_type))
13258 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
13259 TREE_TYPE (node) = error_mark_node;
13262 /* Unary numeric promotion is performed on operand */
13265 op = do_unary_numeric_promotion (op);
13266 prom_type = TREE_TYPE (op);
13267 if (code == UNARY_PLUS_EXPR)
13272 /* 15.14.5 Bitwise Complement Operator ~ */
13274 if (!JINTEGRAL_TYPE_P (op_type))
13276 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
13277 TREE_TYPE (node) = error_mark_node;
13282 op = do_unary_numeric_promotion (op);
13283 prom_type = TREE_TYPE (op);
13287 /* 15.14.6 Logical Complement Operator ! */
13288 case TRUTH_NOT_EXPR:
13289 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
13291 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
13292 /* But the type is known. We will report an error if further
13293 attempt of a assignment is made with this rhs */
13294 TREE_TYPE (node) = boolean_type_node;
13298 prom_type = boolean_type_node;
13301 /* 15.15 Cast Expression */
13303 value = patch_cast (node, wfl_operator);
13304 if (value == error_mark_node)
13306 /* If this cast is part of an assignment, we tell the code
13307 that deals with it not to complain about a mismatch,
13308 because things have been cast, anyways */
13309 TREE_TYPE (node) = error_mark_node;
13314 value = fold (value);
13315 TREE_SIDE_EFFECTS (value) = TREE_SIDE_EFFECTS (op);
13322 return error_mark_node;
13324 /* There are cases where node has been replaced by something else
13325 and we don't end up returning here: UNARY_PLUS_EXPR,
13326 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
13327 TREE_OPERAND (node, 0) = fold (op);
13328 TREE_TYPE (node) = prom_type;
13329 TREE_SIDE_EFFECTS (node) = TREE_SIDE_EFFECTS (op);
13330 return fold (node);
13333 /* Generic type resolution that sometimes takes place during node
13334 patching. Returned the resolved type or generate an error
13335 message. Return the resolved type or NULL_TREE. */
13338 resolve_type_during_patch (type)
13341 if (unresolved_type_p (type, NULL))
13343 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), type);
13346 parse_error_context (type,
13347 "Class `%s' not found in type declaration",
13348 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
13353 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
13354 return TREE_TYPE (type_decl);
13359 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
13360 found. Otherwise NODE or something meant to replace it is returned. */
13363 patch_cast (node, wfl_operator)
13367 tree op = TREE_OPERAND (node, 0);
13368 tree op_type = TREE_TYPE (op);
13369 tree cast_type = TREE_TYPE (node);
13372 /* First resolve OP_TYPE if unresolved */
13373 if (!(cast_type = resolve_type_during_patch (cast_type)))
13374 return error_mark_node;
13376 /* Check on cast that are proven correct at compile time */
13377 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
13380 if (cast_type == op_type)
13383 /* float and double type are converted to the original type main
13384 variant and then to the target type. */
13385 if (JFLOAT_TYPE_P (op_type) && TREE_CODE (cast_type) == CHAR_TYPE)
13386 op = convert (integer_type_node, op);
13388 /* Try widening/narowwing convertion. Potentially, things need
13389 to be worked out in gcc so we implement the extreme cases
13390 correctly. fold_convert() needs to be fixed. */
13391 return convert (cast_type, op);
13394 /* It's also valid to cast a boolean into a boolean */
13395 if (op_type == boolean_type_node && cast_type == boolean_type_node)
13398 /* null can be casted to references */
13399 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
13400 return build_null_of_type (cast_type);
13402 /* The remaining legal casts involve conversion between reference
13403 types. Check for their compile time correctness. */
13404 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
13405 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
13407 TREE_TYPE (node) = promote_type (cast_type);
13408 /* Now, the case can be determined correct at compile time if
13409 OP_TYPE can be converted into CAST_TYPE by assignment
13410 conversion (5.2) */
13412 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
13414 TREE_SET_CODE (node, NOP_EXPR);
13418 if (flag_emit_class_files)
13420 TREE_SET_CODE (node, CONVERT_EXPR);
13424 /* The cast requires a run-time check */
13425 return build (CALL_EXPR, promote_type (cast_type),
13426 build_address_of (soft_checkcast_node),
13427 tree_cons (NULL_TREE, build_class_ref (cast_type),
13428 build_tree_list (NULL_TREE, op)),
13432 /* Any other casts are proven incorrect at compile time */
13433 t1 = xstrdup (lang_printable_name (op_type, 0));
13434 parse_error_context (wfl_operator, "Invalid cast from `%s' to `%s'",
13435 t1, lang_printable_name (cast_type, 0));
13437 return error_mark_node;
13440 /* Build a null constant and give it the type TYPE. */
13443 build_null_of_type (type)
13446 tree node = build_int_2 (0, 0);
13447 TREE_TYPE (node) = promote_type (type);
13451 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
13452 a list of indices. */
13454 build_array_ref (location, array, index)
13458 tree node = build (ARRAY_REF, NULL_TREE, array, index);
13459 EXPR_WFL_LINECOL (node) = location;
13463 /* 15.12 Array Access Expression */
13466 patch_array_ref (node)
13469 tree array = TREE_OPERAND (node, 0);
13470 tree array_type = TREE_TYPE (array);
13471 tree index = TREE_OPERAND (node, 1);
13472 tree index_type = TREE_TYPE (index);
13473 int error_found = 0;
13475 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13477 if (TREE_CODE (array_type) == POINTER_TYPE)
13478 array_type = TREE_TYPE (array_type);
13480 /* The array reference must be an array */
13481 if (!TYPE_ARRAY_P (array_type))
13483 parse_error_context
13485 "`[]' can only be applied to arrays. It can't be applied to `%s'",
13486 lang_printable_name (array_type, 0));
13487 TREE_TYPE (node) = error_mark_node;
13491 /* The array index undergoes unary numeric promotion. The promoted
13492 type must be int */
13493 index = do_unary_numeric_promotion (index);
13494 if (TREE_TYPE (index) != int_type_node)
13496 if (valid_cast_to_p (index_type, int_type_node))
13497 parse_error_context (wfl_operator,
13498 "Incompatible type for `[]'. Explicit cast needed to convert `%s' to `int'",
13499 lang_printable_name (index_type, 0));
13501 parse_error_context (wfl_operator,
13502 "Incompatible type for `[]'. Can't convert `%s' to `int'",
13503 lang_printable_name (index_type, 0));
13504 TREE_TYPE (node) = error_mark_node;
13509 return error_mark_node;
13511 array_type = TYPE_ARRAY_ELEMENT (array_type);
13513 if (flag_emit_class_files || flag_emit_xref)
13515 TREE_OPERAND (node, 0) = array;
13516 TREE_OPERAND (node, 1) = index;
13520 /* The save_expr is for correct evaluation order. It would be cleaner
13521 to use force_evaluation_order (see comment there), but that is
13522 difficult when we also have to deal with bounds checking. */
13523 if (TREE_SIDE_EFFECTS (index))
13524 array = save_expr (array);
13525 node = build_java_arrayaccess (array, array_type, index);
13526 if (TREE_SIDE_EFFECTS (index))
13527 node = build (COMPOUND_EXPR, array_type, array, node);
13529 TREE_TYPE (node) = array_type;
13533 /* 15.9 Array Creation Expressions */
13536 build_newarray_node (type, dims, extra_dims)
13542 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
13543 build_int_2 (extra_dims, 0));
13548 patch_newarray (node)
13551 tree type = TREE_OPERAND (node, 0);
13552 tree dims = TREE_OPERAND (node, 1);
13553 tree cdim, array_type;
13554 int error_found = 0;
13556 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
13558 /* Dimension types are verified. It's better for the types to be
13559 verified in order. */
13560 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
13563 tree dim = TREE_VALUE (cdim);
13565 /* Dim might have been saved during its evaluation */
13566 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
13568 /* The type of each specified dimension must be an integral type. */
13569 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
13572 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
13573 promoted type must be int. */
13576 dim = do_unary_numeric_promotion (dim);
13577 if (TREE_TYPE (dim) != int_type_node)
13581 /* Report errors on types here */
13584 parse_error_context
13585 (TREE_PURPOSE (cdim),
13586 "Incompatible type for dimension in array creation expression. %s convert `%s' to `int'",
13587 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
13588 "Explicit cast needed to" : "Can't"),
13589 lang_printable_name (TREE_TYPE (dim), 0));
13593 TREE_PURPOSE (cdim) = NULL_TREE;
13596 /* Resolve array base type if unresolved */
13597 if (!(type = resolve_type_during_patch (type)))
13602 /* We don't want further evaluation of this bogus array creation
13604 TREE_TYPE (node) = error_mark_node;
13605 return error_mark_node;
13608 /* Set array_type to the actual (promoted) array type of the result. */
13609 if (TREE_CODE (type) == RECORD_TYPE)
13610 type = build_pointer_type (type);
13611 while (--xdims >= 0)
13613 type = promote_type (build_java_array_type (type, -1));
13615 dims = nreverse (dims);
13617 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
13621 = build_java_array_type (type,
13622 TREE_CODE (cdim) == INTEGER_CST
13623 ? (HOST_WIDE_INT) TREE_INT_CST_LOW (cdim)
13625 array_type = promote_type (array_type);
13627 dims = nreverse (dims);
13629 /* The node is transformed into a function call. Things are done
13630 differently according to the number of dimensions. If the number
13631 of dimension is equal to 1, then the nature of the base type
13632 (primitive or not) matters. */
13634 return build_new_array (type, TREE_VALUE (dims));
13636 /* Can't reuse what's already written in expr.c because it uses the
13637 JVM stack representation. Provide a build_multianewarray. FIXME */
13638 return build (CALL_EXPR, array_type,
13639 build_address_of (soft_multianewarray_node),
13640 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
13641 tree_cons (NULL_TREE,
13642 build_int_2 (ndims, 0), dims )),
13646 /* 10.6 Array initializer. */
13648 /* Build a wfl for array element that don't have one, so we can
13649 pin-point errors. */
13652 maybe_build_array_element_wfl (node)
13655 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
13656 return build_expr_wfl (NULL_TREE, ctxp->filename,
13657 ctxp->elc.line, ctxp->elc.prev_col);
13662 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
13663 identification of initialized arrays easier to detect during walk
13667 build_new_array_init (location, values)
13671 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
13672 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
13673 EXPR_WFL_LINECOL (to_return) = location;
13677 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
13678 occurred. Otherwise return NODE after having set its type
13682 patch_new_array_init (type, node)
13685 int error_seen = 0;
13686 tree current, element_type;
13687 HOST_WIDE_INT length;
13688 int all_constant = 1;
13689 tree init = TREE_OPERAND (node, 0);
13691 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
13693 parse_error_context (node,
13694 "Invalid array initializer for non-array type `%s'",
13695 lang_printable_name (type, 1));
13696 return error_mark_node;
13698 type = TREE_TYPE (type);
13699 element_type = TYPE_ARRAY_ELEMENT (type);
13701 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
13703 for (length = 0, current = CONSTRUCTOR_ELTS (init);
13704 current; length++, current = TREE_CHAIN (current))
13706 tree elt = TREE_VALUE (current);
13707 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
13709 error_seen |= array_constructor_check_entry (element_type, current);
13710 elt = TREE_VALUE (current);
13711 /* When compiling to native code, STRING_CST is converted to
13712 INDIRECT_REF, but still with a TREE_CONSTANT flag. */
13713 if (! TREE_CONSTANT (elt) || TREE_CODE (elt) == INDIRECT_REF)
13718 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
13719 TREE_PURPOSE (current) = NULL_TREE;
13722 if (elt && TREE_CODE (elt) == TREE_LIST
13723 && TREE_VALUE (elt) == error_mark_node)
13728 return error_mark_node;
13730 /* Create a new type. We can't reuse the one we have here by
13731 patching its dimension because it originally is of dimension -1
13732 hence reused by gcc. This would prevent triangular arrays. */
13733 type = build_java_array_type (element_type, length);
13734 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
13735 TREE_TYPE (node) = promote_type (type);
13736 TREE_CONSTANT (init) = all_constant;
13737 TREE_CONSTANT (node) = all_constant;
13741 /* Verify that one entry of the initializer element list can be
13742 assigned to the array base type. Report 1 if an error occurred, 0
13746 array_constructor_check_entry (type, entry)
13749 char *array_type_string = NULL; /* For error reports */
13750 tree value, type_value, new_value, wfl_value, patched;
13751 int error_seen = 0;
13753 new_value = NULL_TREE;
13754 wfl_value = TREE_VALUE (entry);
13756 push_obstacks (&permanent_obstack, &permanent_obstack);
13757 value = java_complete_tree (TREE_VALUE (entry));
13758 /* patch_string return error_mark_node if arg is error_mark_node */
13759 if ((patched = patch_string (value)))
13761 if (value == error_mark_node)
13764 type_value = TREE_TYPE (value);
13766 /* At anytime, try_builtin_assignconv can report a warning on
13767 constant overflow during narrowing. */
13768 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
13769 new_value = try_builtin_assignconv (wfl_operator, type, value);
13770 if (!new_value && (new_value = try_reference_assignconv (type, value)))
13771 type_value = promote_type (type);
13774 /* Check and report errors */
13777 const char *msg = (!valid_cast_to_p (type_value, type) ?
13778 "Can't" : "Explicit cast needed to");
13779 if (!array_type_string)
13780 array_type_string = xstrdup (lang_printable_name (type, 1));
13781 parse_error_context
13782 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
13783 msg, lang_printable_name (type_value, 1), array_type_string);
13789 new_value = maybe_build_primttype_type_ref (new_value, wfl_value);
13790 TREE_VALUE (entry) = new_value;
13793 if (array_type_string)
13794 free (array_type_string);
13796 TREE_PURPOSE (entry) = NULL_TREE;
13801 build_this (location)
13804 tree node = build_wfl_node (this_identifier_node);
13805 TREE_SET_CODE (node, THIS_EXPR);
13806 EXPR_WFL_LINECOL (node) = location;
13810 /* 14.15 The return statement. It builds a modify expression that
13811 assigns the returned value to the RESULT_DECL that hold the value
13815 build_return (location, op)
13819 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
13820 EXPR_WFL_LINECOL (node) = location;
13821 node = build_debugable_stmt (location, node);
13826 patch_return (node)
13829 tree return_exp = TREE_OPERAND (node, 0);
13830 tree meth = current_function_decl;
13831 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
13832 int error_found = 0;
13834 TREE_TYPE (node) = error_mark_node;
13835 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13837 /* It's invalid to have a return value within a function that is
13838 declared with the keyword void or that is a constructor */
13839 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
13842 /* It's invalid to use a return statement in a static block */
13843 if (DECL_CLINIT_P (current_function_decl))
13846 /* It's invalid to have a no return value within a function that
13847 isn't declared with the keyword `void' */
13848 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
13851 if (in_instance_initializer)
13856 if (in_instance_initializer)
13857 parse_error_context (wfl_operator,
13858 "`return' inside instance initializer");
13860 else if (DECL_CLINIT_P (current_function_decl))
13861 parse_error_context (wfl_operator,
13862 "`return' inside static initializer");
13864 else if (!DECL_CONSTRUCTOR_P (meth))
13866 char *t = xstrdup (lang_printable_name (mtype, 0));
13867 parse_error_context (wfl_operator,
13868 "`return' with%s value from `%s %s'",
13869 (error_found == 1 ? "" : "out"),
13870 t, lang_printable_name (meth, 0));
13874 parse_error_context (wfl_operator,
13875 "`return' with value from constructor `%s'",
13876 lang_printable_name (meth, 0));
13877 return error_mark_node;
13880 /* If we have a return_exp, build a modify expression and expand
13881 it. Note: at that point, the assignment is declared valid, but we
13882 may want to carry some more hacks */
13885 tree exp = java_complete_tree (return_exp);
13886 tree modify, patched;
13888 /* If the function returned value and EXP are booleans, EXP has
13889 to be converted into the type of DECL_RESULT, which is integer
13890 (see complete_start_java_method) */
13891 if (TREE_TYPE (exp) == boolean_type_node &&
13892 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
13893 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
13895 /* `null' can be assigned to a function returning a reference */
13896 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
13897 exp == null_pointer_node)
13898 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
13900 if ((patched = patch_string (exp)))
13903 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
13904 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
13905 modify = java_complete_tree (modify);
13907 if (modify != error_mark_node)
13909 TREE_SIDE_EFFECTS (modify) = 1;
13910 TREE_OPERAND (node, 0) = modify;
13913 return error_mark_node;
13915 TREE_TYPE (node) = void_type_node;
13916 TREE_SIDE_EFFECTS (node) = 1;
13920 /* 14.8 The if Statement */
13923 build_if_else_statement (location, expression, if_body, else_body)
13925 tree expression, if_body, else_body;
13929 else_body = empty_stmt_node;
13930 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
13931 EXPR_WFL_LINECOL (node) = location;
13932 node = build_debugable_stmt (location, node);
13937 patch_if_else_statement (node)
13940 tree expression = TREE_OPERAND (node, 0);
13942 TREE_TYPE (node) = error_mark_node;
13943 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
13945 /* The type of expression must be boolean */
13946 if (TREE_TYPE (expression) != boolean_type_node
13947 && TREE_TYPE (expression) != promoted_boolean_type_node)
13949 parse_error_context
13951 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
13952 lang_printable_name (TREE_TYPE (expression), 0));
13953 return error_mark_node;
13956 TREE_TYPE (node) = void_type_node;
13957 TREE_SIDE_EFFECTS (node) = 1;
13958 CAN_COMPLETE_NORMALLY (node)
13959 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
13960 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
13964 /* 14.6 Labeled Statements */
13966 /* Action taken when a lableled statement is parsed. a new
13967 LABELED_BLOCK_EXPR is created. No statement is attached to the
13968 label, yet. LABEL can be NULL_TREE for artificially-generated blocks. */
13971 build_labeled_block (location, label)
13976 tree label_decl, node;
13977 if (label == NULL_TREE || label == continue_identifier_node)
13978 label_name = label;
13981 label_name = merge_qualified_name (label_id, label);
13982 /* Issue an error if we try to reuse a label that was previously
13984 if (IDENTIFIER_LOCAL_VALUE (label_name))
13986 EXPR_WFL_LINECOL (wfl_operator) = location;
13987 parse_error_context (wfl_operator,
13988 "Declaration of `%s' shadows a previous label declaration",
13989 IDENTIFIER_POINTER (label));
13990 EXPR_WFL_LINECOL (wfl_operator) =
13991 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
13992 parse_error_context (wfl_operator,
13993 "This is the location of the previous declaration of label `%s'",
13994 IDENTIFIER_POINTER (label));
13995 java_error_count--;
13999 label_decl = create_label_decl (label_name);
14000 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
14001 EXPR_WFL_LINECOL (node) = location;
14002 TREE_SIDE_EFFECTS (node) = 1;
14006 /* A labeled statement LBE is attached a statement. */
14009 finish_labeled_statement (lbe, statement)
14010 tree lbe; /* Labeled block expr */
14013 /* In anyways, tie the loop to its statement */
14014 LABELED_BLOCK_BODY (lbe) = statement;
14015 pop_labeled_block ();
14016 POP_LABELED_BLOCK ();
14020 /* 14.10, 14.11, 14.12 Loop Statements */
14022 /* Create an empty LOOP_EXPR and make it the last in the nested loop
14026 build_new_loop (loop_body)
14029 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
14030 TREE_SIDE_EFFECTS (loop) = 1;
14035 /* Create a loop body according to the following structure:
14037 COMPOUND_EXPR (loop main body)
14038 EXIT_EXPR (this order is for while/for loops.
14039 LABELED_BLOCK_EXPR the order is reversed for do loops)
14040 LABEL_DECL (a continue occuring here branches at the
14041 BODY end of this labeled block)
14044 REVERSED, if non zero, tells that the loop condition expr comes
14045 after the body, like in the do-while loop.
14047 To obtain a loop, the loop body structure described above is
14048 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
14051 LABEL_DECL (use this label to exit the loop)
14053 <structure described above> */
14056 build_loop_body (location, condition, reversed)
14061 tree first, second, body;
14063 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
14064 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
14065 condition = build_debugable_stmt (location, condition);
14066 TREE_SIDE_EFFECTS (condition) = 1;
14068 body = build_labeled_block (0, continue_identifier_node);
14069 first = (reversed ? body : condition);
14070 second = (reversed ? condition : body);
14072 build (COMPOUND_EXPR, NULL_TREE,
14073 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
14076 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
14077 their order) on the current loop. Unlink the current loop from the
14081 finish_loop_body (location, condition, body, reversed)
14083 tree condition, body;
14086 tree to_return = ctxp->current_loop;
14087 tree loop_body = LOOP_EXPR_BODY (to_return);
14090 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
14091 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
14092 The real EXIT_EXPR is one operand further. */
14093 EXPR_WFL_LINECOL (cnode) = location;
14094 /* This one is for accurate error reports */
14095 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
14096 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
14098 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
14103 /* Tailored version of finish_loop_body for FOR loops, when FOR
14104 loops feature the condition part */
14107 finish_for_loop (location, condition, update, body)
14109 tree condition, update, body;
14111 /* Put the condition and the loop body in place */
14112 tree loop = finish_loop_body (location, condition, body, 0);
14113 /* LOOP is the current loop which has been now popped of the loop
14114 stack. Install the update block */
14115 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
14119 /* Try to find the loop a block might be related to. This comprises
14120 the case where the LOOP_EXPR is found as the second operand of a
14121 COMPOUND_EXPR, because the loop happens to have an initialization
14122 part, then expressed as the first operand of the COMPOUND_EXPR. If
14123 the search finds something, 1 is returned. Otherwise, 0 is
14124 returned. The search is assumed to start from a
14125 LABELED_BLOCK_EXPR's block. */
14128 search_loop (statement)
14131 if (TREE_CODE (statement) == LOOP_EXPR)
14134 if (TREE_CODE (statement) == BLOCK)
14135 statement = BLOCK_SUBBLOCKS (statement);
14139 if (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14140 while (statement && TREE_CODE (statement) == COMPOUND_EXPR)
14141 statement = TREE_OPERAND (statement, 1);
14143 return (TREE_CODE (statement) == LOOP_EXPR
14144 && FOR_LOOP_P (statement) ? statement : NULL_TREE);
14147 /* Return 1 if LOOP can be found in the labeled block BLOCK. 0 is
14148 returned otherwise. */
14151 labeled_block_contains_loop_p (block, loop)
14157 if (LABELED_BLOCK_BODY (block) == loop)
14160 if (FOR_LOOP_P (loop) && search_loop (LABELED_BLOCK_BODY (block)) == loop)
14166 /* If the loop isn't surrounded by a labeled statement, create one and
14167 insert LOOP as its body. */
14170 patch_loop_statement (loop)
14175 TREE_TYPE (loop) = void_type_node;
14176 if (labeled_block_contains_loop_p (ctxp->current_labeled_block, loop))
14179 loop_label = build_labeled_block (0, NULL_TREE);
14180 /* LOOP is an EXPR node, so it should have a valid EXPR_WFL_LINECOL
14181 that LOOP_LABEL could enquire about, for a better accuracy. FIXME */
14182 LABELED_BLOCK_BODY (loop_label) = loop;
14183 PUSH_LABELED_BLOCK (loop_label);
14187 /* 14.13, 14.14: break and continue Statements */
14189 /* Build a break or a continue statement. a null NAME indicates an
14190 unlabeled break/continue statement. */
14193 build_bc_statement (location, is_break, name)
14194 int location, is_break;
14197 tree break_continue, label_block_expr = NULL_TREE;
14201 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
14202 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
14203 /* Null means that we don't have a target for this named
14204 break/continue. In this case, we make the target to be the
14205 label name, so that the error can be reported accuratly in
14206 patch_bc_statement. */
14207 label_block_expr = EXPR_WFL_NODE (name);
14209 /* Unlabeled break/continue will be handled during the
14210 break/continue patch operation */
14212 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
14214 IS_BREAK_STMT_P (break_continue) = is_break;
14215 TREE_SIDE_EFFECTS (break_continue) = 1;
14216 EXPR_WFL_LINECOL (break_continue) = location;
14217 break_continue = build_debugable_stmt (location, break_continue);
14218 return break_continue;
14221 /* Verification of a break/continue statement. */
14224 patch_bc_statement (node)
14227 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
14228 tree labeled_block = ctxp->current_labeled_block;
14229 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14231 /* Having an identifier here means that the target is unknown. */
14232 if (bc_label != NULL_TREE && TREE_CODE (bc_label) == IDENTIFIER_NODE)
14234 parse_error_context (wfl_operator, "No label definition found for `%s'",
14235 IDENTIFIER_POINTER (bc_label));
14236 return error_mark_node;
14238 if (! IS_BREAK_STMT_P (node))
14240 /* It's a continue statement. */
14241 for (;; labeled_block = TREE_CHAIN (labeled_block))
14243 if (labeled_block == NULL_TREE)
14245 if (bc_label == NULL_TREE)
14246 parse_error_context (wfl_operator,
14247 "`continue' must be in loop");
14249 parse_error_context
14250 (wfl_operator, "continue label `%s' does not name a loop",
14251 IDENTIFIER_POINTER (bc_label));
14252 return error_mark_node;
14254 if ((DECL_NAME (LABELED_BLOCK_LABEL (labeled_block))
14255 == continue_identifier_node)
14256 && (bc_label == NULL_TREE
14257 || TREE_CHAIN (labeled_block) == bc_label))
14259 bc_label = labeled_block;
14264 else if (!bc_label)
14266 for (;; labeled_block = TREE_CHAIN (labeled_block))
14268 if (labeled_block == NULL_TREE)
14270 parse_error_context (wfl_operator,
14271 "`break' must be in loop or switch");
14272 return error_mark_node;
14274 target_stmt = LABELED_BLOCK_BODY (labeled_block);
14275 if (TREE_CODE (target_stmt) == SWITCH_EXPR
14276 || search_loop (target_stmt))
14278 bc_label = labeled_block;
14284 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
14285 CAN_COMPLETE_NORMALLY (bc_label) = 1;
14287 /* Our break/continue don't return values. */
14288 TREE_TYPE (node) = void_type_node;
14289 /* Encapsulate the break within a compound statement so that it's
14290 expanded all the times by expand_expr (and not clobbered
14291 sometimes, like after a if statement) */
14292 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
14293 TREE_SIDE_EFFECTS (node) = 1;
14297 /* Process the exit expression belonging to a loop. Its type must be
14301 patch_exit_expr (node)
14304 tree expression = TREE_OPERAND (node, 0);
14305 TREE_TYPE (node) = error_mark_node;
14306 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14308 /* The type of expression must be boolean */
14309 if (TREE_TYPE (expression) != boolean_type_node)
14311 parse_error_context
14313 "Incompatible type for loop conditional. Can't convert `%s' to `boolean'",
14314 lang_printable_name (TREE_TYPE (expression), 0));
14315 return error_mark_node;
14317 /* Now we know things are allright, invert the condition, fold and
14319 TREE_OPERAND (node, 0) =
14320 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
14322 if (! integer_zerop (TREE_OPERAND (node, 0))
14323 && ctxp->current_loop != NULL_TREE
14324 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
14325 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
14326 if (! integer_onep (TREE_OPERAND (node, 0)))
14327 CAN_COMPLETE_NORMALLY (node) = 1;
14330 TREE_TYPE (node) = void_type_node;
14334 /* 14.9 Switch statement */
14337 patch_switch_statement (node)
14340 tree se = TREE_OPERAND (node, 0), se_type;
14342 /* Complete the switch expression */
14343 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
14344 se_type = TREE_TYPE (se);
14345 /* The type of the switch expression must be char, byte, short or
14347 if (!JINTEGRAL_TYPE_P (se_type))
14349 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
14350 parse_error_context (wfl_operator,
14351 "Incompatible type for `switch'. Can't convert `%s' to `int'",
14352 lang_printable_name (se_type, 0));
14353 /* This is what java_complete_tree will check */
14354 TREE_OPERAND (node, 0) = error_mark_node;
14355 return error_mark_node;
14358 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
14360 /* Ready to return */
14361 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
14363 TREE_TYPE (node) = error_mark_node;
14364 return error_mark_node;
14366 TREE_TYPE (node) = void_type_node;
14367 TREE_SIDE_EFFECTS (node) = 1;
14368 CAN_COMPLETE_NORMALLY (node)
14369 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
14370 || ! SWITCH_HAS_DEFAULT (node);
14374 /* 14.18 The try/catch statements */
14377 build_try_statement (location, try_block, catches)
14379 tree try_block, catches;
14381 tree node = build (TRY_EXPR, NULL_TREE, try_block, catches);
14382 EXPR_WFL_LINECOL (node) = location;
14387 build_try_finally_statement (location, try_block, finally)
14389 tree try_block, finally;
14391 tree node = build (TRY_FINALLY_EXPR, NULL_TREE, try_block, finally);
14392 EXPR_WFL_LINECOL (node) = location;
14397 patch_try_statement (node)
14400 int error_found = 0;
14401 tree try = TREE_OPERAND (node, 0);
14402 /* Exception handlers are considered in left to right order */
14403 tree catch = nreverse (TREE_OPERAND (node, 1));
14404 tree current, caught_type_list = NULL_TREE;
14406 /* Check catch clauses, if any. Every time we find an error, we try
14407 to process the next catch clause. We process the catch clause before
14408 the try block so that when processing the try block we can check thrown
14409 exceptions againts the caught type list. */
14410 for (current = catch; current; current = TREE_CHAIN (current))
14412 tree carg_decl, carg_type;
14413 tree sub_current, catch_block, catch_clause;
14416 /* At this point, the structure of the catch clause is
14417 CATCH_EXPR (catch node)
14418 BLOCK (with the decl of the parameter)
14420 MODIFY_EXPR (assignment of the catch parameter)
14421 BLOCK (catch clause block)
14423 catch_clause = TREE_OPERAND (current, 0);
14424 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
14425 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
14427 /* Catch clauses can't have more than one parameter declared,
14428 but it's already enforced by the grammar. Make sure that the
14429 only parameter of the clause statement in of class Throwable
14430 or a subclass of Throwable, but that was done earlier. The
14431 catch clause parameter type has also been resolved. */
14433 /* Just make sure that the catch clause parameter type inherits
14434 from java.lang.Throwable */
14435 if (!inherits_from_p (carg_type, throwable_type_node))
14437 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14438 parse_error_context (wfl_operator,
14439 "Can't catch class `%s'. Catch clause parameter type must be a subclass of class `java.lang.Throwable'",
14440 lang_printable_name (carg_type, 0));
14445 /* Partial check for unreachable catch statement: The catch
14446 clause is reachable iff is no earlier catch block A in
14447 the try statement such that the type of the catch
14448 clause's parameter is the same as or a subclass of the
14449 type of A's parameter */
14451 for (sub_current = catch;
14452 sub_current != current; sub_current = TREE_CHAIN (sub_current))
14454 tree sub_catch_clause, decl;
14455 sub_catch_clause = TREE_OPERAND (sub_current, 0);
14456 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
14458 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
14460 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
14461 parse_error_context
14463 "`catch' not reached because of the catch clause at line %d",
14464 EXPR_WFL_LINENO (sub_current));
14465 unreachable = error_found = 1;
14469 /* Complete the catch clause block */
14470 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
14471 if (catch_block == error_mark_node)
14476 if (CAN_COMPLETE_NORMALLY (catch_block))
14477 CAN_COMPLETE_NORMALLY (node) = 1;
14478 TREE_OPERAND (current, 0) = catch_block;
14483 /* Things to do here: the exception must be thrown */
14485 /* Link this type to the caught type list */
14486 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
14489 PUSH_EXCEPTIONS (caught_type_list);
14490 if ((try = java_complete_tree (try)) == error_mark_node)
14492 if (CAN_COMPLETE_NORMALLY (try))
14493 CAN_COMPLETE_NORMALLY (node) = 1;
14496 /* Verification ends here */
14498 return error_mark_node;
14500 TREE_OPERAND (node, 0) = try;
14501 TREE_OPERAND (node, 1) = catch;
14502 TREE_TYPE (node) = void_type_node;
14506 /* 14.17 The synchronized Statement */
14509 patch_synchronized_statement (node, wfl_op1)
14510 tree node, wfl_op1;
14512 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
14513 tree block = TREE_OPERAND (node, 1);
14515 tree enter, exit, expr_decl, assignment;
14517 if (expr == error_mark_node)
14519 block = java_complete_tree (block);
14523 /* The TYPE of expr must be a reference type */
14524 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
14526 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14527 parse_error_context (wfl_operator, "Incompatible type for `synchronized'. Can't convert `%s' to `java.lang.Object'",
14528 lang_printable_name (TREE_TYPE (expr), 0));
14529 return error_mark_node;
14532 if (flag_emit_xref)
14534 TREE_OPERAND (node, 0) = expr;
14535 TREE_OPERAND (node, 1) = java_complete_tree (block);
14536 CAN_COMPLETE_NORMALLY (node) = 1;
14540 /* Generate a try-finally for the synchronized statement, except
14541 that the handler that catches all throw exception calls
14542 _Jv_MonitorExit and then rethrow the exception.
14543 The synchronized statement is then implemented as:
14546 _Jv_MonitorEnter (expression)
14548 _Jv_MonitorExit (expression)
14552 e = _Jv_exception_info ();
14553 _Jv_MonitorExit (expression)
14557 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
14558 BUILD_MONITOR_ENTER (enter, expr_decl);
14559 BUILD_MONITOR_EXIT (exit, expr_decl);
14560 CAN_COMPLETE_NORMALLY (enter) = 1;
14561 CAN_COMPLETE_NORMALLY (exit) = 1;
14562 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
14563 TREE_SIDE_EFFECTS (assignment) = 1;
14564 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
14565 build (COMPOUND_EXPR, NULL_TREE,
14566 build (WITH_CLEANUP_EXPR, NULL_TREE,
14567 build (COMPOUND_EXPR, NULL_TREE,
14568 assignment, enter),
14571 node = build_expr_block (node, expr_decl);
14573 return java_complete_tree (node);
14576 /* 14.16 The throw Statement */
14579 patch_throw_statement (node, wfl_op1)
14580 tree node, wfl_op1;
14582 tree expr = TREE_OPERAND (node, 0);
14583 tree type = TREE_TYPE (expr);
14584 int unchecked_ok = 0, tryblock_throws_ok = 0;
14586 /* Thrown expression must be assignable to java.lang.Throwable */
14587 if (!try_reference_assignconv (throwable_type_node, expr))
14589 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14590 parse_error_context (wfl_operator,
14591 "Can't throw `%s'; it must be a subclass of class `java.lang.Throwable'",
14592 lang_printable_name (type, 0));
14593 /* If the thrown expression was a reference, we further the
14594 compile-time check. */
14595 if (!JREFERENCE_TYPE_P (type))
14596 return error_mark_node;
14599 /* At least one of the following must be true */
14601 /* The type of the throw expression is a not checked exception,
14602 i.e. is a unchecked expression. */
14603 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
14605 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14606 /* An instance can't throw a checked excetion unless that exception
14607 is explicitely declared in the `throws' clause of each
14608 constructor. This doesn't apply to anonymous classes, since they
14609 don't have declared constructors. */
14611 && in_instance_initializer && !ANONYMOUS_CLASS_P (current_class))
14614 for (current = TYPE_METHODS (current_class); current;
14615 current = TREE_CHAIN (current))
14616 if (DECL_CONSTRUCTOR_P (current)
14617 && !check_thrown_exceptions_do (TREE_TYPE (expr)))
14619 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)",
14620 lang_printable_name (TREE_TYPE (expr), 0));
14621 return error_mark_node;
14625 /* Throw is contained in a try statement and at least one catch
14626 clause can receive the thrown expression or the current method is
14627 declared to throw such an exception. Or, the throw statement is
14628 contained in a method or constructor declaration and the type of
14629 the Expression is assignable to at least one type listed in the
14630 throws clause the declaration. */
14632 tryblock_throws_ok = check_thrown_exceptions_do (TREE_TYPE (expr));
14633 if (!(unchecked_ok || tryblock_throws_ok))
14635 /* If there is a surrounding try block that has no matching
14636 clatch clause, report it first. A surrounding try block exits
14637 only if there is something after the list of checked
14638 exception thrown by the current function (if any). */
14639 if (IN_TRY_BLOCK_P ())
14640 parse_error_context (wfl_operator, "Checked exception `%s' can't be caught by any of the catch clause(s) of the surrounding `try' block",
14641 lang_printable_name (type, 0));
14642 /* If we have no surrounding try statement and the method doesn't have
14643 any throws, report it now. FIXME */
14645 /* We report that the exception can't be throw from a try block
14646 in all circumstances but when the `throw' is inside a static
14648 else if (!EXCEPTIONS_P (currently_caught_type_list)
14649 && !tryblock_throws_ok)
14651 if (DECL_CLINIT_P (current_function_decl))
14652 parse_error_context (wfl_operator,
14653 "Checked exception `%s' can't be thrown in initializer",
14654 lang_printable_name (type, 0));
14656 parse_error_context (wfl_operator,
14657 "Checked exception `%s' isn't thrown from a `try' block",
14658 lang_printable_name (type, 0));
14660 /* Otherwise, the current method doesn't have the appropriate
14661 throws declaration */
14663 parse_error_context (wfl_operator, "Checked exception `%s' doesn't match any of current method's `throws' declaration(s)",
14664 lang_printable_name (type, 0));
14665 return error_mark_node;
14668 if (! flag_emit_class_files && ! flag_emit_xref)
14669 BUILD_THROW (node, expr);
14671 /* If doing xrefs, keep the location where the `throw' was seen. */
14672 if (flag_emit_xref)
14673 EXPR_WFL_LINECOL (node) = EXPR_WFL_LINECOL (wfl_op1);
14677 /* Check that exception said to be thrown by method DECL can be
14678 effectively caught from where DECL is invoked. */
14681 check_thrown_exceptions (location, decl)
14686 /* For all the unchecked exceptions thrown by DECL */
14687 for (throws = DECL_FUNCTION_THROWS (decl); throws;
14688 throws = TREE_CHAIN (throws))
14689 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
14692 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
14693 if (DECL_NAME (decl) == get_identifier ("clone"))
14696 EXPR_WFL_LINECOL (wfl_operator) = location;
14697 if (DECL_FINIT_P (current_function_decl))
14698 parse_error_context
14699 (wfl_operator, "Exception `%s' can't be thrown in initializer",
14700 lang_printable_name (TREE_VALUE (throws), 0));
14703 parse_error_context
14704 (wfl_operator, "Exception `%s' must be caught, or it must be declared in the `throws' clause of `%s'",
14705 lang_printable_name (TREE_VALUE (throws), 0),
14706 (DECL_INIT_P (current_function_decl) ?
14707 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))) :
14708 IDENTIFIER_POINTER (DECL_NAME (current_function_decl))));
14713 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
14714 try-catch blocks, OR is listed in the `throws' clause of the
14718 check_thrown_exceptions_do (exception)
14721 tree list = currently_caught_type_list;
14722 resolve_and_layout (exception, NULL_TREE);
14723 /* First, all the nested try-catch-finally at that stage. The
14724 last element contains `throws' clause exceptions, if any. */
14725 if (IS_UNCHECKED_EXCEPTION_P (exception))
14730 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
14731 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
14733 list = TREE_CHAIN (list);
14739 purge_unchecked_exceptions (mdecl)
14742 tree throws = DECL_FUNCTION_THROWS (mdecl);
14743 tree new = NULL_TREE;
14747 tree next = TREE_CHAIN (throws);
14748 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
14750 TREE_CHAIN (throws) = new;
14755 /* List is inverted here, but it doesn't matter */
14756 DECL_FUNCTION_THROWS (mdecl) = new;
14759 /* 15.24 Conditional Operator ?: */
14762 patch_conditional_expr (node, wfl_cond, wfl_op1)
14763 tree node, wfl_cond, wfl_op1;
14765 tree cond = TREE_OPERAND (node, 0);
14766 tree op1 = TREE_OPERAND (node, 1);
14767 tree op2 = TREE_OPERAND (node, 2);
14768 tree resulting_type = NULL_TREE;
14769 tree t1, t2, patched;
14770 int error_found = 0;
14772 /* Operands of ?: might be StringBuffers crafted as a result of a
14773 string concatenation. Obtain a descent operand here. */
14774 if ((patched = patch_string (op1)))
14775 TREE_OPERAND (node, 1) = op1 = patched;
14776 if ((patched = patch_string (op2)))
14777 TREE_OPERAND (node, 2) = op2 = patched;
14779 t1 = TREE_TYPE (op1);
14780 t2 = TREE_TYPE (op2);
14782 /* The first expression must be a boolean */
14783 if (TREE_TYPE (cond) != boolean_type_node)
14785 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
14786 parse_error_context (wfl_operator,
14787 "Incompatible type for `?:'. Can't convert `%s' to `boolean'",
14788 lang_printable_name (TREE_TYPE (cond), 0));
14792 /* Second and third can be numeric, boolean (i.e. primitive),
14793 references or null. Anything else results in an error */
14794 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
14795 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
14796 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
14797 || (t1 == boolean_type_node && t2 == boolean_type_node)))
14800 /* Determine the type of the conditional expression. Same types are
14801 easy to deal with */
14803 resulting_type = t1;
14805 /* There are different rules for numeric types */
14806 else if (JNUMERIC_TYPE_P (t1))
14808 /* if byte/short found, the resulting type is short */
14809 if ((t1 == byte_type_node && t2 == short_type_node)
14810 || (t1 == short_type_node && t2 == byte_type_node))
14811 resulting_type = short_type_node;
14813 /* If t1 is a constant int and t2 is of type byte, short or char
14814 and t1's value fits in t2, then the resulting type is t2 */
14815 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
14816 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
14817 resulting_type = t2;
14819 /* If t2 is a constant int and t1 is of type byte, short or char
14820 and t2's value fits in t1, then the resulting type is t1 */
14821 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
14822 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
14823 resulting_type = t1;
14825 /* Otherwise, binary numeric promotion is applied and the
14826 resulting type is the promoted type of operand 1 and 2 */
14828 resulting_type = binary_numeric_promotion (t1, t2,
14829 &TREE_OPERAND (node, 1),
14830 &TREE_OPERAND (node, 2));
14833 /* Cases of a reference and a null type */
14834 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
14835 resulting_type = t1;
14837 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
14838 resulting_type = t2;
14840 /* Last case: different reference types. If a type can be converted
14841 into the other one by assignment conversion, the latter
14842 determines the type of the expression */
14843 else if ((resulting_type = try_reference_assignconv (t1, op2)))
14844 resulting_type = promote_type (t1);
14846 else if ((resulting_type = try_reference_assignconv (t2, op1)))
14847 resulting_type = promote_type (t2);
14849 /* If we don't have any resulting type, we're in trouble */
14850 if (!resulting_type)
14852 char *t = xstrdup (lang_printable_name (t1, 0));
14853 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
14854 parse_error_context (wfl_operator,
14855 "Incompatible type for `?:'. Can't convert `%s' to `%s'",
14856 t, lang_printable_name (t2, 0));
14863 TREE_TYPE (node) = error_mark_node;
14864 return error_mark_node;
14867 TREE_TYPE (node) = resulting_type;
14868 TREE_SET_CODE (node, COND_EXPR);
14869 CAN_COMPLETE_NORMALLY (node) = 1;
14873 /* Try to constant fold NODE.
14874 If NODE is not a constant expression, return NULL_EXPR.
14875 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
14878 fold_constant_for_init (node, context)
14882 tree op0, op1, val;
14883 enum tree_code code = TREE_CODE (node);
14885 if (code == STRING_CST || code == INTEGER_CST || code == REAL_CST)
14893 case TRUNC_MOD_EXPR:
14901 case TRUTH_ANDIF_EXPR:
14902 case TRUTH_ORIF_EXPR:
14909 op0 = TREE_OPERAND (node, 0);
14910 op1 = TREE_OPERAND (node, 1);
14911 val = fold_constant_for_init (op0, context);
14912 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14914 TREE_OPERAND (node, 0) = val;
14915 val = fold_constant_for_init (op1, context);
14916 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14918 TREE_OPERAND (node, 1) = val;
14919 return patch_binop (node, op0, op1);
14921 case UNARY_PLUS_EXPR:
14923 case TRUTH_NOT_EXPR:
14926 op0 = TREE_OPERAND (node, 0);
14927 val = fold_constant_for_init (op0, context);
14928 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14930 TREE_OPERAND (node, 0) = val;
14931 return patch_unaryop (node, op0);
14935 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
14936 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14938 TREE_OPERAND (node, 0) = val;
14939 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
14940 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14942 TREE_OPERAND (node, 1) = val;
14943 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
14944 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14946 TREE_OPERAND (node, 2) = val;
14947 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
14948 : TREE_OPERAND (node, 2);
14952 if (! FIELD_FINAL (node)
14953 || DECL_INITIAL (node) == NULL_TREE)
14955 val = DECL_INITIAL (node);
14956 /* Guard against infinite recursion. */
14957 DECL_INITIAL (node) = NULL_TREE;
14958 val = fold_constant_for_init (val, node);
14959 DECL_INITIAL (node) = val;
14962 case EXPR_WITH_FILE_LOCATION:
14963 /* Compare java_complete_tree and resolve_expression_name. */
14964 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
14965 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
14967 tree name = EXPR_WFL_NODE (node);
14969 if (PRIMARY_P (node))
14971 else if (! QUALIFIED_P (name))
14973 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
14974 if (decl == NULL_TREE
14975 || (! FIELD_STATIC (decl) && ! FIELD_FINAL (decl)))
14977 return fold_constant_for_init (decl, decl);
14981 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
14982 qualify_ambiguous_name (node);
14983 if (resolve_field_access (node, &decl, NULL)
14984 && decl != NULL_TREE)
14985 return fold_constant_for_init (decl, decl);
14991 op0 = TREE_OPERAND (node, 0);
14992 val = fold_constant_for_init (op0, context);
14993 if (val == NULL_TREE || ! TREE_CONSTANT (val))
14995 TREE_OPERAND (node, 0) = val;
14999 #ifdef USE_COMPONENT_REF
15001 case COMPONENT_REF:
15010 #ifdef USE_COMPONENT_REF
15011 /* Context is 'T' for TypeName, 'P' for PackageName,
15012 'M' for MethodName, 'E' for ExpressionName, and 'A' for AmbiguousName. */
15015 resolve_simple_name (name, context)
15022 resolve_qualified_name (name, context)